Skip to content

Commit

Permalink
no xcode build logs in console but separate file (#60)
Browse files Browse the repository at this point in the history
* No console Log + Log to File
  • Loading branch information
domdop authored and aheritier committed Aug 21, 2017
1 parent 172df88 commit 3937e54
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
55 changes: 54 additions & 1 deletion src/main/java/au/com/rayh/JenkinsXCodeBuildOutputParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@

import hudson.FilePath;
import hudson.model.TaskListener;

import java.io.BufferedOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;

/**
*
Expand All @@ -42,16 +48,48 @@
public class JenkinsXCodeBuildOutputParser extends XCodeBuildOutputParser {
protected TaskListener buildListener;
private FilePath testReportsDir;
private OutputStream logFileOutputStream;

public JenkinsXCodeBuildOutputParser(FilePath workspace, TaskListener buildListener) throws IOException, InterruptedException {
super();
this.buildListener = buildListener;
this.captureOutputStream = new LineBasedFilterOutputStream();
this.consoleLog = true;
this.logFileOutputStream = null;

testReportsDir = workspace.child("test-reports");
testReportsDir.mkdirs();
}

public void setConsoleLog(boolean consoleLog) {
this.consoleLog = consoleLog;
}

public void setLogfilePath(final FilePath buildDirectory, final String logfileOutputDirectory) throws IOException, InterruptedException {
if(buildDirectory.exists() && buildDirectory.isDirectory() && !StringUtils.isEmpty(logfileOutputDirectory)) {
final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd_hhmmssSSS");
final String logfileName = dateFormatter.format(new GregorianCalendar().getTime());
FilePath logFilePath = buildDirectory.child(logfileOutputDirectory);
// clean Directory
if(logFilePath.exists()) {
logFilePath.deleteRecursive();
}
// Create if non-existent
if (!logFilePath.exists()) {
logFilePath.mkdirs();
}
logFileOutputStream = new BufferedOutputStream(logFilePath.child(logfileName + ".log").write(),1024*512);
}
}

public void closeLogfile() throws IOException {
if(logFileOutputStream != null) {
logFileOutputStream.flush();
logFileOutputStream.close();
logFileOutputStream = null;
}
}

public class LineBasedFilterOutputStream extends FilterOutputStream {
StringBuilder buffer = new StringBuilder();

Expand All @@ -61,7 +99,6 @@ public LineBasedFilterOutputStream() {

@Override
public void write(int b) throws IOException {
super.write(b);
if((char)b == '\n') {
try {
handleLine(buffer.toString());
Expand All @@ -73,6 +110,22 @@ public void write(int b) throws IOException {
} else {
buffer.append((char)b);
}
if(consoleLog) {
super.write(b);
}
if(logFileOutputStream != null) {
logFileOutputStream.write(b);
}
}

@Override
public void close() throws IOException {
if(logFileOutputStream != null) {
logFileOutputStream.flush();
logFileOutputStream.close();
logFileOutputStream = null;
}
super.close();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/au/com/rayh/XCodeBuildOutputParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class XCodeBuildOutputParser {
protected int exitCode;
protected TestSuite currentTestSuite;
protected TestCase currentTestCase;
protected boolean consoleLog;

protected XCodeBuildOutputParser() {
super();
Expand All @@ -87,6 +88,7 @@ public XCodeBuildOutputParser(File workspace, OutputStream log) {
this();
this.captureOutputStream = new LineBasedFilterOutputStream(log);
this.testReportsDir = workspace;
this.consoleLog = true;
}

public class LineBasedFilterOutputStream extends FilterOutputStream {
Expand Down Expand Up @@ -173,6 +175,7 @@ protected OutputStream outputForSuite() throws IOException,
protected void handleLine(String line) throws ParseException, IOException, InterruptedException, JAXBException {
Matcher m = START_SUITE.matcher(line);
if(m.matches()) {
consoleLog = true;
currentTestSuite = new TestSuite(InetAddress.getLocalHost().getHostName(), m.group(1), parseDate(m.group(2)));
return;
}
Expand All @@ -185,6 +188,7 @@ protected void handleLine(String line) throws ParseException, IOException, Inter
writeTestReport();

currentTestSuite = null;
consoleLog = false;
return;
}

Expand Down
47 changes: 44 additions & 3 deletions src/main/java/au/com/rayh/XCodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import javax.inject.Inject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -145,6 +146,14 @@ public class XCodeBuilder extends Builder implements SimpleBuildStep {
* @since 1.0
*/
public final Boolean generateArchive;
/**
* @since 2.1.0
*/
public final Boolean noConsoleLog;
/**
* @since 2.1.0
*/
public final String logfileOutputDirectory;
/**
* @since 1.5
**/
Expand Down Expand Up @@ -206,7 +215,8 @@ public class XCodeBuilder extends Builder implements SimpleBuildStep {

// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public XCodeBuilder(Boolean buildIpa, Boolean generateArchive, Boolean cleanBeforeBuild, Boolean cleanTestReports, String configuration,
public XCodeBuilder(Boolean buildIpa, Boolean generateArchive, Boolean noConsoleLog, String logfileOutputDirectory, Boolean cleanBeforeBuild,
Boolean cleanTestReports, String configuration,
String target, String sdk, String xcodeProjectPath, String xcodeProjectFile, String xcodebuildArguments,
String cfBundleVersionValue, String cfBundleShortVersionStringValue, Boolean unlockKeychain,
String keychainName, String keychainPath, String keychainPwd, String symRoot, String xcodeWorkspaceFile,
Expand All @@ -216,6 +226,8 @@ public XCodeBuilder(Boolean buildIpa, Boolean generateArchive, Boolean cleanBefo

this.buildIpa = buildIpa;
this.generateArchive = generateArchive;
this.noConsoleLog = noConsoleLog;
this.logfileOutputDirectory = logfileOutputDirectory;
this.sdk = sdk;
this.target = target;
this.cleanBeforeBuild = cleanBeforeBuild;
Expand Down Expand Up @@ -248,6 +260,24 @@ public XCodeBuilder(Boolean buildIpa, Boolean generateArchive, Boolean cleanBefo
this.ipaExportMethod = ipaExportMethod;
}

@Deprecated
public XCodeBuilder(Boolean buildIpa, Boolean generateArchive, Boolean cleanBeforeBuild, Boolean cleanTestReports, String configuration,
String target, String sdk, String xcodeProjectPath, String xcodeProjectFile, String xcodebuildArguments,
String cfBundleVersionValue, String cfBundleShortVersionStringValue, Boolean unlockKeychain,
String keychainName, String keychainPath, String keychainPwd, String symRoot, String xcodeWorkspaceFile,
String xcodeSchema, String buildDir, String developmentTeamName, String developmentTeamID, Boolean allowFailingBuildResults,
String ipaName, Boolean provideApplicationVersion, String ipaOutputDirectory, Boolean changeBundleID, String bundleID,
String bundleIDInfoPlistPath, String ipaManifestPlistUrl, Boolean interpretTargetAsRegEx, String ipaExportMethod) {

this(buildIpa, generateArchive, false, null, cleanBeforeBuild, cleanTestReports, configuration,
target, sdk, xcodeProjectPath, xcodeProjectFile, xcodebuildArguments,
cfBundleVersionValue, cfBundleShortVersionStringValue, unlockKeychain,
keychainName, keychainPath, keychainPwd, symRoot, xcodeWorkspaceFile,
xcodeSchema, buildDir, developmentTeamName, developmentTeamID, allowFailingBuildResults,
ipaName, provideApplicationVersion, ipaOutputDirectory, changeBundleID, bundleID,
bundleIDInfoPlistPath, ipaManifestPlistUrl, interpretTargetAsRegEx, ipaExportMethod);
}

@Deprecated
public XCodeBuilder(Boolean buildIpa, Boolean generateArchive, Boolean cleanBeforeBuild, Boolean cleanTestReports, String configuration,
String target, String sdk, String xcodeProjectPath, String xcodeProjectFile, String xcodebuildArguments,
Expand All @@ -257,7 +287,7 @@ public XCodeBuilder(Boolean buildIpa, Boolean generateArchive, Boolean cleanBefo
String ipaName, Boolean provideApplicationVersion, String ipaOutputDirectory, Boolean changeBundleID, String bundleID,
String bundleIDInfoPlistPath, String ipaManifestPlistUrl, Boolean interpretTargetAsRegEx, Boolean signIpaOnXcrun) {

this(buildIpa, generateArchive, cleanBeforeBuild, cleanTestReports, configuration,
this(buildIpa, generateArchive, false, null, cleanBeforeBuild, cleanTestReports, configuration,
target, sdk, xcodeProjectPath, xcodeProjectFile, xcodebuildArguments,
cfBundleVersionValue, cfBundleShortVersionStringValue, unlockKeychain,
keychainName, keychainPath, keychainPwd, symRoot, xcodeWorkspaceFile,
Expand Down Expand Up @@ -552,7 +582,7 @@ private boolean _perform(Run<?,?> build, FilePath projectRoot, Launcher launcher

// Build
StringBuilder xcodeReport = new StringBuilder(Messages.XCodeBuilder_invokeXcodebuild());
XCodeBuildOutputParser reportGenerator = new JenkinsXCodeBuildOutputParser(projectRoot, listener);
JenkinsXCodeBuildOutputParser reportGenerator = new JenkinsXCodeBuildOutputParser(projectRoot, listener);
List<String> commandLine = Lists.newArrayList(getGlobalConfiguration().getXcodebuildPath());

// Prioritizing schema over target setting
Expand Down Expand Up @@ -636,6 +666,17 @@ private boolean _perform(Run<?,?> build, FilePath projectRoot, Launcher launcher
}
//END Bug JENKINS-30362

if(noConsoleLog != null && noConsoleLog){
xcodeReport.append(", consolelog:NO");
reportGenerator.setConsoleLog(false);
}else{
xcodeReport.append(", consolelog:YES");
}
if(!StringUtils.isEmpty(logfileOutputDirectory)) {
xcodeReport.append(", logfileOutputDirectory: ").append(logfileOutputDirectory);
reportGenerator.setLogfilePath(buildDirectory,logfileOutputDirectory);
}

if (!StringUtils.isEmpty(symRootValue)) {
commandLine.add("SYMROOT=" + symRootValue);
xcodeReport.append(", symRoot: ").append(symRootValue);
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/au/com/rayh/XCodeBuilder/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@
description="Checking this option will generate an xcarchive of the specified scheme. A workspace and scheme are are also needed for archives">
<f:checkbox title="Yes" />
</f:entry>

<f:entry title="${%No Console Log?}" field="noConsoleLog" description="Checking this option will not log xcode build output to console output.">
<f:checkbox title="Yes" />
</f:entry>
<f:entry title="${%Logfile Output directory}" field="logfileOutputDirectory"
description="Leave empty for Log to project-directory/logs. The output directory for the separate logfile, relative to the project-directory.">
<f:textbox />
</f:entry>

<f:entry title="${%Configuration}" field="configuration" description="This is the name of the configuration as defined in the Xcode project.">
<f:textbox default="Release" />
Expand Down

0 comments on commit 3937e54

Please sign in to comment.