Navigation Menu

Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ded, error handling fixed

Fixed https://issues.jenkins-ci.org/browse/JENKINS-32694 : If file path doesn't start with variable or not absolute then file is created at ${WORKSPACE}
Fixed adding multiple end of line when existing file is modified
  • Loading branch information
Sanketh P B committed Mar 19, 2016
1 parent ae53bc1 commit 643305c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .project
@@ -1,5 +1,5 @@
<projectDescription>
<name>create_text_file</name>
<name>text-file-operations</name>
<comment>Creates or updates text file with specified contents. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects/>
<buildSpec>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -10,7 +10,7 @@

<groupId>com.etas.jenkins.plugins</groupId>
<artifactId>text-file-operations</artifactId>
<version>1.2.2-SNAPSHOT</version>
<version>1.3.0</version>
<packaging>hpi</packaging>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Text+File+Operations+Plugin</url>

Expand Down
Expand Up @@ -46,13 +46,15 @@ public class CreateFileBuilder extends Builder {
private final String textFilePath;
private final String textFileContent;
private final String fileOption;
private final boolean useWorkspace;

// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public CreateFileBuilder(String textFilePath, String textFileContent,String fileOption){
public CreateFileBuilder(String textFilePath, String textFileContent,String fileOption,boolean useWorkspace){
this.textFilePath = textFilePath;
this.textFileContent = textFileContent;
this.fileOption = fileOption;
this.useWorkspace = useWorkspace;

}

Expand All @@ -71,24 +73,35 @@ public String getFileOption(){
return fileOption;
}

public boolean getUseWorkspace(){
return useWorkspace;
}

@SuppressWarnings("rawtypes")
@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {

boolean result = false;
try {
String resolvedFilePath = build.getEnvironment(listener).expand(textFilePath);

String tempFilePath = textFilePath;
if(this.useWorkspace && !(textFilePath.startsWith("${WORKSPACE}")||textFilePath.startsWith("$WORKSPACE")))
{
tempFilePath = "${WORKSPACE}\\" + textFilePath;
}
String resolvedFilePath = build.getEnvironment(listener).expand(tempFilePath);
String resolvedContent = build.getEnvironment(listener).expand(textFileContent);

launcher.getChannel().call(new CreateFileTask(resolvedFilePath, resolvedContent, fileOption));
result = launcher.getChannel().call(new CreateFileTask(resolvedFilePath, resolvedContent, fileOption,listener));


} catch (Exception e) {

e.printStackTrace(listener.getLogger());
listener.getLogger().println("Failed to create/update file.");
listener.getLogger().println(e.getMessage());
return false;
}

return true;
return result;
}


Expand Down
Expand Up @@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
package com.etas.jenkins.plugins.CreateTextFile;

import hudson.FilePath;
import hudson.model.BuildListener;
import hudson.remoting.Callable;

import java.io.File;
Expand All @@ -41,12 +42,14 @@ public class CreateFileTask implements Serializable,Callable<Boolean,IOException
private final String fileContent;
private final String filePath;
private final String fileOption;
private static BuildListener listener;


public CreateFileTask(String filePath,String fileContent,String fileOption){
public CreateFileTask(String filePath,String fileContent,String fileOption,BuildListener listener){
this.filePath = filePath;
this.fileContent = fileContent;
this.fileOption = fileOption;
CreateFileTask.listener = listener;
}

@Override
Expand All @@ -63,26 +66,34 @@ public Boolean call() throws IOException {
String existingFileContents = "";
String eol = System.getProperty("line.separator");

listener.getLogger().println(String.format("Creating/updating file at '%s'", filePath));

if(!textFile.exists()){

listener.getLogger().println(String.format("File does not exist at '%s', new file will be created.", filePath));
finalFileContent = fileContent;
}
else{

listener.getLogger().println(String.format("File already exists at '%s', selected write option is '%s'", filePath,fileOption));
existingFileContents = textFile.readToString();

if(fileOption.equalsIgnoreCase("overWrite")){

finalFileContent = fileContent;

}else if(fileOption.equalsIgnoreCase("appendToEnd")){

finalFileContent = existingFileContents.concat(eol + fileContent);
if(existingFileContents.endsWith(eol)){
finalFileContent = existingFileContents.concat(fileContent);
}else{
finalFileContent = existingFileContents.concat(eol + fileContent);
}

}else if(fileOption.equalsIgnoreCase("insertAtStart")){

}else if(fileOption.equalsIgnoreCase("insertAtStart")){

finalFileContent = fileContent.concat(eol + existingFileContents);

if(existingFileContents.startsWith(eol)){
finalFileContent = fileContent.concat(existingFileContents);
}else{
finalFileContent = fileContent.concat(eol + existingFileContents);
}
}

textFile.deleteContents();
Expand All @@ -93,16 +104,13 @@ public Boolean call() throws IOException {

textFile.write(finalFileContent, "UTF-8");

} catch (InterruptedException e) {
} catch (Exception e) {

e.printStackTrace();
return false;
} catch (IOException e){

e.printStackTrace();
listener.getLogger().println("Failed to create/update file.");
listener.getLogger().println(e.getMessage());
return false;
}

}
listener.getLogger().println("File successfully created/updated at "+ filePath);
return true;
}

Expand Down
Expand Up @@ -3,9 +3,12 @@
<f:entry title="File Path: " field="textFilePath">
<f:textbox></f:textbox>
</f:entry>
<f:entry title="Create at Workspace:" field="useWorkspace">
<f:checkbox/>
</f:entry>
<f:entry title="Text File Content: " field="textFileContent">
<f:textarea></f:textarea>
</f:entry>
</f:entry>
<f:entry title="File options: " field="fileOption">
<f:entry>
<f:radio name="fileOption" title="Insert at start of file"
Expand Down
@@ -0,0 +1,3 @@
<div>
Enable this to create file in the workspace directory.
</div>
Expand Up @@ -60,7 +60,7 @@ public void testCreateNewFile() throws InterruptedException, IOException, Except
j.jenkins.getGlobalNodeProperties().add(prop);
FreeStyleProject project = j.createFreeStyleProject();

CreateFileBuilder builder = new CreateFileBuilder("${WORKSPACE}\\${FILE_NAME}", "${LINE1}", "overWrite");
CreateFileBuilder builder = new CreateFileBuilder("${WORKSPACE}\\${FILE_NAME}", "${LINE1}", "overWrite",false);
project.getBuildersList().add(builder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
String content = build.getWorkspace().child("NewFile.txt").readToString();
Expand Down Expand Up @@ -92,7 +92,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
});


CreateFileBuilder builder = new CreateFileBuilder("${WORKSPACE}\\${FILE_NAME}", "${LINE2}", "insertAtStart");
CreateFileBuilder builder = new CreateFileBuilder("${FILE_NAME}", "${LINE2}", "insertAtStart",true);
project.getBuildersList().add(builder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
InputStream content = build.getWorkspace().child("ExistingFile.txt").read();
Expand Down Expand Up @@ -128,7 +128,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
});


CreateFileBuilder builder = new CreateFileBuilder("${WORKSPACE}\\${FILE_NAME}", "${LINE2}", "appendToEnd");
CreateFileBuilder builder = new CreateFileBuilder("${WORKSPACE}\\${FILE_NAME}", "${LINE2}", "appendToEnd",true);
project.getBuildersList().add(builder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
String filePath = build.getWorkspace().child("ExistingFile.txt").getRemote();
Expand Down

0 comments on commit 643305c

Please sign in to comment.