Skip to content

Commit

Permalink
Merge pull request #105 from RandomInEqualities/operations
Browse files Browse the repository at this point in the history
Allow copy and delete of ant default exclude files
  • Loading branch information
jonesbusy authored Jul 19, 2024
2 parents 886dac4 + b5c3e8f commit 9d4e1eb
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 36 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<version>1.87</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.20</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/sp/sd/fileoperations/FileCopyOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.jenkinsci.Symbol;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.File;
import hudson.util.DirScanner;
import java.io.Serializable;

public class FileCopyOperation extends FileOperation implements Serializable {
Expand All @@ -23,6 +25,7 @@ public class FileCopyOperation extends FileOperation implements Serializable {
private final boolean renameFiles;
private final String sourceCaptureExpression;
private final String targetNameExpression;
private Boolean useDefaultExcludes;

@DataBoundConstructor
public FileCopyOperation(String includes,
Expand All @@ -39,6 +42,7 @@ public FileCopyOperation(String includes,
this.renameFiles = renameFiles;
this.sourceCaptureExpression = sourceCaptureExpression;
this.targetNameExpression = targetNameExpression;
this.useDefaultExcludes = true;
}

public String getIncludes() {
Expand Down Expand Up @@ -69,6 +73,10 @@ public String getTargetNameExpression() {
return targetNameExpression;
}

public boolean getUseDefaultExcludes() {
return useDefaultExcludes;
}

public boolean runOperation(Run<?, ?> run, FilePath buildWorkspace, Launcher launcher, TaskListener listener) {
boolean result = false;
try {
Expand All @@ -83,7 +91,8 @@ public boolean runOperation(Run<?, ?> run, FilePath buildWorkspace, Launcher lau
flattenFiles,
renameFiles,
sourceCaptureExpression,
targetNameExpression));
targetNameExpression,
useDefaultExcludes));
} catch (RuntimeException e) {
listener.getLogger().println(e.getMessage());
throw e;
Expand All @@ -108,6 +117,7 @@ private static final class TargetFileCallable implements FileCallable<Boolean> {
private final boolean renameFiles;
private final String sourceCaptureExpression;
private final String targetNameExpression;
private final boolean useDefaultExcludes;

public TargetFileCallable(TaskListener Listener,
String ResolvedIncludes,
Expand All @@ -116,7 +126,8 @@ public TargetFileCallable(TaskListener Listener,
boolean flattenFiles,
boolean renameFiles,
String sourceCaptureExpression,
String targetNameExpression) {
String targetNameExpression,
boolean UseDefaultExcludes) {
this.listener = Listener;
this.resolvedIncludes = ResolvedIncludes;
this.resolvedExcludes = ResolvedExcludes;
Expand All @@ -125,6 +136,7 @@ public TargetFileCallable(TaskListener Listener,
this.renameFiles = renameFiles;
this.sourceCaptureExpression = sourceCaptureExpression;
this.targetNameExpression = targetNameExpression;
this.useDefaultExcludes = UseDefaultExcludes;
}

@Override
Expand All @@ -133,7 +145,7 @@ public Boolean invoke(File ws, VirtualChannel channel) {
try {
FilePath fpWS = new FilePath(ws);
FilePath fpTL = new FilePath(fpWS, resolvedTargetLocation);
FilePath[] resolvedFiles = fpWS.list(resolvedIncludes, resolvedExcludes);
FilePath[] resolvedFiles = fpWS.list(resolvedIncludes, resolvedExcludes, useDefaultExcludes);
if (resolvedFiles.length == 0) {
listener.getLogger().println("0 files found for include pattern '" + resolvedIncludes + "' and exclude pattern '" + resolvedExcludes + "'");
}
Expand All @@ -158,7 +170,7 @@ public Boolean invoke(File ws, VirtualChannel channel) {
for (FilePath item : resolvedFiles) {
listener.getLogger().println(item.getRemote());
}
fpWS.copyRecursiveTo(resolvedIncludes, resolvedExcludes, fpTL);
fpWS.copyRecursiveTo(new DirScanner.Glob(resolvedIncludes, resolvedExcludes, useDefaultExcludes), fpTL, resolvedIncludes);
result = true;
}
} catch (RuntimeException e) {
Expand All @@ -183,6 +195,17 @@ public static class DescriptorImpl extends FileOperationDescriptor {
public String getDisplayName() {
return "File Copy";
}
}

@DataBoundSetter
public void setUseDefaultExcludes(boolean useDefaultExcludes) {
this.useDefaultExcludes = useDefaultExcludes;
}

protected Object readResolve() {
if (useDefaultExcludes == null) {
useDefaultExcludes = true;
}
return this;
}
}
26 changes: 23 additions & 3 deletions src/main/java/sp/sd/fileoperations/FileDeleteOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.File;

Expand All @@ -21,11 +22,13 @@
public class FileDeleteOperation extends FileOperation implements Serializable {
private final String includes;
private final String excludes;
private Boolean useDefaultExcludes;

@DataBoundConstructor
public FileDeleteOperation(String includes, String excludes) {
this.includes = includes;
this.excludes = excludes;
this.useDefaultExcludes = true;
}

public String getIncludes() {
Expand All @@ -36,14 +39,18 @@ public String getExcludes() {
return excludes;
}

public boolean getUseDefaultExcludes() {
return useDefaultExcludes;
}

public boolean runOperation(Run<?, ?> run, FilePath buildWorkspace, Launcher launcher, TaskListener listener) {
boolean result = false;
try {
listener.getLogger().println("File Delete Operation:");
EnvVars envVars = run.getEnvironment(listener);
try {
FilePath ws = new FilePath(buildWorkspace, ".");
result = ws.act(new TargetFileCallable(listener, envVars.expand(includes), envVars.expand(excludes)));
result = ws.act(new TargetFileCallable(listener, envVars.expand(includes), envVars.expand(excludes), useDefaultExcludes));
} catch (Exception e) {
listener.fatalError(e.getMessage());
return false;
Expand All @@ -60,19 +67,21 @@ private static final class TargetFileCallable implements FileCallable<Boolean> {
private final TaskListener listener;
private final String resolvedIncludes;
private final String resolvedExcludes;
private final boolean useDefaultExcludes;

public TargetFileCallable(TaskListener Listener, String ResolvedIncludes, String ResolvedExcludes) {
public TargetFileCallable(TaskListener Listener, String ResolvedIncludes, String ResolvedExcludes, boolean UseDefaultExcludes) {
this.listener = Listener;
this.resolvedIncludes = ResolvedIncludes;
this.resolvedExcludes = ResolvedExcludes;
this.useDefaultExcludes = UseDefaultExcludes;
}

@Override
public Boolean invoke(File ws, VirtualChannel channel) {
boolean result = false;
try {
FilePath fpWS = new FilePath(ws);
FilePath[] resolvedFiles = fpWS.list(resolvedIncludes, resolvedExcludes);
FilePath[] resolvedFiles = fpWS.list(resolvedIncludes, resolvedExcludes, useDefaultExcludes);
if (resolvedFiles.length == 0) {
listener.getLogger().println("0 files found for include pattern '" + resolvedIncludes + "' and exclude pattern '" + resolvedExcludes + "'");
result = true;
Expand Down Expand Up @@ -127,6 +136,17 @@ public static class DescriptorImpl extends FileOperationDescriptor {
public String getDisplayName() {
return "File Delete";
}
}

@DataBoundSetter
public void setUseDefaultExcludes(boolean useDefaultExcludes) {
this.useDefaultExcludes = useDefaultExcludes;
}

protected Object readResolve() {
if (useDefaultExcludes == null) {
useDefaultExcludes = true;
}
return this;
}
}
26 changes: 23 additions & 3 deletions src/main/java/sp/sd/fileoperations/FileTransformOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.File;

Expand All @@ -21,11 +22,13 @@
public class FileTransformOperation extends FileOperation implements Serializable {
private final String includes;
private final String excludes;
private Boolean useDefaultExcludes;

@DataBoundConstructor
public FileTransformOperation(String includes, String excludes) {
this.includes = includes;
this.excludes = excludes;
this.useDefaultExcludes = true;
}

public String getIncludes() {
Expand All @@ -36,14 +39,18 @@ public String getExcludes() {
return excludes;
}

public boolean getUseDefaultExcludes() {
return useDefaultExcludes;
}

public boolean runOperation(Run<?, ?> run, FilePath buildWorkspace, Launcher launcher, TaskListener listener) {
boolean result = false;
try {
listener.getLogger().println("File Transform Operation:");
EnvVars envVars = run.getEnvironment(listener);
try {
FilePath ws = new FilePath(buildWorkspace, ".");
result = ws.act(new TargetFileCallable(listener, envVars.expand(includes), envVars.expand(excludes), envVars));
result = ws.act(new TargetFileCallable(listener, envVars.expand(includes), envVars.expand(excludes), useDefaultExcludes, envVars));
} catch (Exception e) {
listener.fatalError(e.getMessage());
return false;
Expand All @@ -61,11 +68,13 @@ private static final class TargetFileCallable implements FileCallable<Boolean> {
private final EnvVars environment;
private final String resolvedIncludes;
private final String resolvedExcludes;
private final boolean useDefaultExcludes;

public TargetFileCallable(TaskListener Listener, String ResolvedIncludes, String ResolvedExcludes, EnvVars environment) {
public TargetFileCallable(TaskListener Listener, String ResolvedIncludes, String ResolvedExcludes, boolean UseDefaultExcludes, EnvVars environment) {
this.listener = Listener;
this.resolvedIncludes = ResolvedIncludes;
this.resolvedExcludes = ResolvedExcludes;
this.useDefaultExcludes = UseDefaultExcludes;
this.environment = environment;
}

Expand All @@ -74,7 +83,7 @@ public Boolean invoke(File ws, VirtualChannel channel) {
boolean result = false;
try {
FilePath fpWS = new FilePath(ws);
FilePath[] resolvedFiles = fpWS.list(resolvedIncludes, resolvedExcludes);
FilePath[] resolvedFiles = fpWS.list(resolvedIncludes, resolvedExcludes, useDefaultExcludes);
if (resolvedFiles.length == 0) {
listener.getLogger().println("0 files found for include pattern '" + resolvedIncludes + "' and exclude pattern '" + resolvedExcludes + "'");
result = true;
Expand Down Expand Up @@ -109,6 +118,17 @@ public static class DescriptorImpl extends FileOperationDescriptor {
public String getDisplayName() {
return "File Transform";
}
}

@DataBoundSetter
public void setUseDefaultExcludes(boolean useDefaultExcludes) {
this.useDefaultExcludes = useDefaultExcludes;
}

protected Object readResolve() {
if (useDefaultExcludes == null) {
useDefaultExcludes = true;
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,46 @@ public void fileCopyOperation(String includes,
boolean flattenFiles,
boolean renameFiles,
String sourceCaptureExpression,
String targetNameExpression) {
String targetNameExpression,
boolean useDefaultExcludes) {
FileCopyOperation fileCopyOperation = new FileCopyOperation(includes,
excludes,
targetLocation,
flattenFiles,
renameFiles,
sourceCaptureExpression,
targetNameExpression);
excludes,
targetLocation,
flattenFiles,
renameFiles,
sourceCaptureExpression,
targetNameExpression);
fileCopyOperation.setUseDefaultExcludes(useDefaultExcludes);
fileOperations.add(fileCopyOperation);
}

public void fileDeleteOperation(String includes, String excludes) {
public void fileCopyOperation(String includes,
String excludes,
String targetLocation,
boolean flattenFiles,
boolean renameFiles,
String sourceCaptureExpression,
String targetNameExpression) {
fileCopyOperation(includes,
excludes,
targetLocation,
flattenFiles,
renameFiles,
sourceCaptureExpression,
targetNameExpression,
true);
}

public void fileDeleteOperation(String includes, String excludes, boolean useDefaultExcludes) {
FileDeleteOperation fileDeleteOperation = new FileDeleteOperation(includes, excludes);
fileDeleteOperation.setUseDefaultExcludes(useDefaultExcludes);
fileOperations.add(fileDeleteOperation);
}

public void fileDeleteOperation(String includes, String excludes) {
fileDeleteOperation(includes, excludes, true);
}

public void fileDownloadOperation(String url, String userName, String password, String targetLocation, String targetFileName, String proxyHost, String proxyPort) {
FileDownloadOperation fileDownloadOperation = new FileDownloadOperation(url, userName, password, targetLocation, targetFileName, proxyHost, proxyPort);
fileOperations.add(fileDownloadOperation);
Expand All @@ -66,11 +90,16 @@ public void filePropertiesToJsonOperation(String sourceFile, String targetFile)
fileOperations.add(filePropertiesToJsonOperation);
}

public void fileTransformOperation(String includes, String excludes) {
public void fileTransformOperation(String includes, String excludes, boolean useDefaultExcludes) {
FileTransformOperation fileTransformOperation = new FileTransformOperation(includes, excludes);
fileTransformOperation.setUseDefaultExcludes(useDefaultExcludes);
fileOperations.add(fileTransformOperation);
}

public void fileTransformOperation(String includes, String excludes) {
fileTransformOperation(includes, excludes, true);
}

Check warning on line 101 in src/main/java/sp/sd/fileoperations/dsl/FileOperationsJobDslContext.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 47-101 are not covered by tests

public void fileUnTarOperation(String filePath, String targetLocation, boolean isGZIP) {
FileUnTarOperation fileUnTarOperation = new FileUnTarOperation(filePath, targetLocation, isGZIP);
fileOperations.add(fileUnTarOperation);
Expand Down
Loading

0 comments on commit 9d4e1eb

Please sign in to comment.