Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes warning in system admin section + color highlighting of warning and errors #25

Merged
merged 4 commits into from
Jul 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<name>Kyle Sweeney</name>
<email>kyle.sweeney@valtech.com</email>
</developer>
<developer>
<id>marshall777</id>
<name>Lionel Cabasson</name>
<email>marshall777@gmail.com</email>
</developer>
</developers>

<scm>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package hudson.plugins.msbuild;

import hudson.console.LineTransformationOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;

public class MSBuildConsoleAnnotator extends LineTransformationOutputStream {
private final OutputStream out;
private final Charset charset;

private int numberOfWarnings = 0;
private int numberOfErrors = 0;

public MSBuildConsoleAnnotator(OutputStream out, Charset charset) {
this.out = out;
this.charset = charset;
}

public int getNumberOfWarnings() {
return numberOfWarnings;
}

public int getNumberOfErrors() {
return numberOfErrors;
}

@Override
protected void eol(byte[] b, int len) throws IOException {
String line = charset.decode(ByteBuffer.wrap(b, 0, len)).toString();

// trim off CR/LF from the end
line = trimEOL(line);

// Error messages handler
Matcher m = MSBuildErrorNote.PATTERN.matcher(line);
if (m.matches()) { // Match the number of warnings
new MSBuildErrorNote().encodeTo(out);
this.numberOfErrors++;
}

// Warning messages handler
m = MSBuildWarningNote.PATTERN.matcher(line);
if (m.matches()) { // Match the number of warnings
new MSBuildWarningNote().encodeTo(out);
this.numberOfWarnings++;
}

out.write(b, 0, len);
}

@Override
public void close() throws IOException {
super.close();
out.close();
}
}
33 changes: 33 additions & 0 deletions src/main/java/hudson/plugins/msbuild/MSBuildErrorNote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package hudson.plugins.msbuild;

import hudson.Extension;
import hudson.MarkupText;
import hudson.console.ConsoleAnnotationDescriptor;
import hudson.console.ConsoleAnnotator;
import hudson.console.ConsoleNote;
import java.util.regex.Pattern;

/**
* Annotation for MSBuild and CSC error messages
*/
public class MSBuildErrorNote extends ConsoleNote {
/** Pattern to identify doxygen error message */
public final static Pattern PATTERN = Pattern.compile("(.*)[Ee]rror\\s(CS|MSB)\\d+(.*)");

public MSBuildErrorNote() {
}

@Override
public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) {
text.addMarkup(0, text.length(), "<span class=error-inline>", "</span>");
return null;
}

@Extension
public static final class DescriptorImpl extends ConsoleAnnotationDescriptor {

public String getDisplayName() {
return Messages.MsBuildBuilder_ErrorNoteDescription();
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/hudson/plugins/msbuild/MSBuildWarningNote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package hudson.plugins.msbuild;

import hudson.Extension;
import hudson.MarkupText;
import hudson.console.ConsoleAnnotationDescriptor;
import hudson.console.ConsoleAnnotator;
import hudson.console.ConsoleNote;
import java.util.regex.Pattern;

/**
* Annotation for MSBuild warning messages
*/
public class MSBuildWarningNote extends ConsoleNote {
/** Pattern to identify doxygen warning message */
public final static Pattern PATTERN = Pattern.compile("(.*)\\(\\d+,\\d+\\):\\swarning\\s((MSB|CS)\\d+){0,1}:\\s(.*)");

public MSBuildWarningNote() {
}

@Override
public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) {
text.addMarkup(0, text.length(), "<span class=warning-inline>", "</span>");
return null;
}

@Extension
public static final class DescriptorImpl extends ConsoleAnnotationDescriptor {

public String getDisplayName() {
return Messages.MsBuildBuilder_WarningNoteDescription();
}
}
}
26 changes: 24 additions & 2 deletions src/main/java/hudson/plugins/msbuild/MsBuildBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
if (node != null) {
ai = ai.forNode(node, listener);
ai = ai.forEnvironment(env);
String pathToMsBuild = ai.getHome();
String pathToMsBuild = getToolFullPath(launcher, ai.getHome(), execName);
FilePath exec = new FilePath(launcher.getChannel(), pathToMsBuild);

try {
Expand Down Expand Up @@ -199,8 +199,9 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
listener.getLogger().println(String.format("Executing the command %s from %s", args.toStringWithQuote(), pwd));
// Parser to find the number of Warnings/Errors
MsBuildConsoleParser mbcp = new MsBuildConsoleParser(listener.getLogger(), build.getCharset());
MSBuildConsoleAnnotator annotator = new MSBuildConsoleAnnotator(listener.getLogger(), build.getCharset());
// Launch the msbuild.exe
int r = launcher.launch().cmds(args).envs(env).stdout(mbcp).pwd(pwd).join();
int r = launcher.launch().cmds(args).envs(env).stdout(mbcp).stdout(annotator).pwd(pwd).join();
// Check the number of warnings
if (unstableIfWarnings && mbcp.getNumberOfWarnings() > 0) {
listener.getLogger().println("> Set build UNSTABLE because there are warnings.");
Expand Down Expand Up @@ -232,6 +233,27 @@ private Map<String, String> getPropertiesVariables(AbstractBuild build) {
return buildVariables;
}

/**
* Get the full path of the tool to run.
* If given path is a directory, this will append the executable name.
*/
static String getToolFullPath(Launcher launcher, String pathToTool, String execName) throws IOException, InterruptedException
{
String fullPathToMsBuild = pathToTool;

FilePath exec = new FilePath(launcher.getChannel(), fullPathToMsBuild);
if (exec.isDirectory())
{
if (!fullPathToMsBuild.endsWith("\\"))
{
fullPathToMsBuild = fullPathToMsBuild + "\\";
}

fullPathToMsBuild = fullPathToMsBuild + execName;
}

return fullPathToMsBuild;
}

@Override
public Descriptor<Builder> getDescriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
# THE SOFTWARE.

MsBuildBuilder.DisplayName=Build a Visual Studio project or solution using MSBuild
MsBuildBuilder.ErrorNoteDescription=MSBuild error
MsBuildBuilder.WarningNoteDescription=MSBuild warning
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
# THE SOFTWARE.

MsBuildBuilder.DisplayName=Construire un projet Visual Studio avec MSBuild
MsBuildBuilder.ErrorNoteDescription=Erreur MSBuild
MsBuildBuilder.WarningNoteDescription=Avertissement MSBuild