Skip to content

Commit

Permalink
[Fixed JENKINS-11927] Added possibility to delete also directories wh…
Browse files Browse the repository at this point in the history
…en delete pattern is spedified.
  • Loading branch information
vjuranek committed Dec 6, 2011
1 parent 864c5a4 commit ae33d1e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 69 deletions.
20 changes: 17 additions & 3 deletions src/main/java/hudson/plugins/ws_cleanup/Cleanup.java
@@ -1,6 +1,7 @@
package hudson.plugins.ws_cleanup;

import hudson.FilePath.FileCallable;
import hudson.Util;
import hudson.remoting.VirtualChannel;

import java.io.File;
Expand All @@ -12,11 +13,17 @@
class Cleanup implements FileCallable<Object> {

private final List<Pattern> patterns;
private final boolean deleteDirs;

public Cleanup(List<Pattern> patterns) {
public Cleanup(List<Pattern> patterns, boolean deleteDirs) {
this.patterns = patterns;
this.deleteDirs = deleteDirs;
}

public boolean getDeleteDirs(){
return deleteDirs;
}

// Can't use FileCallable<Void> to return void
public Object invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
DirectoryScanner ds = new DirectoryScanner();
Expand All @@ -28,8 +35,15 @@ public Object invoke(File f, VirtualChannel channel) throws IOException, Interru
}
ds.setIncludes(includes);
ds.scan();
for (String path : ds.getIncludedFiles()) {
new File(f, path).delete();
int length = ds.getIncludedFilesCount();
if(deleteDirs)
length += ds.getIncludedDirsCount();
String[] toDelete = new String[length];
System.arraycopy(ds.getIncludedFiles(), 0, toDelete, 0, ds.getIncludedFilesCount());
if(deleteDirs)
System.arraycopy(ds.getIncludedDirectories(), 0, toDelete, ds.getIncludedFilesCount(),ds.getIncludedDirsCount());
for (String path : toDelete) {
Util.deleteRecursive(new File(f, path));
}
return null;
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/hudson/plugins/ws_cleanup/PreBuildCleanup.java
Expand Up @@ -22,16 +22,22 @@
public class PreBuildCleanup extends BuildWrapper {

private final List<Pattern> patterns;
private final boolean deleteDirs;

@DataBoundConstructor
public PreBuildCleanup(List<Pattern> patterns) {
public PreBuildCleanup(List<Pattern> patterns, boolean deleteDirs) {
this.patterns = patterns;
this.deleteDirs = deleteDirs;
}

public List<Pattern> getPatterns(){
return patterns;
}

public boolean getDeleteDirs(){
return deleteDirs;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
Expand All @@ -55,7 +61,7 @@ public void preCheckout(AbstractBuild build, Launcher launcher,
if (patterns == null || patterns.isEmpty()) {
ws.deleteContents();
} else {
build.getWorkspace().act(new Cleanup(patterns));
build.getWorkspace().act(new Cleanup(patterns,deleteDirs));
}

listener.getLogger().append("done\n\n");
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/hudson/plugins/ws_cleanup/WsCleanup.java
Expand Up @@ -27,17 +27,23 @@
public class WsCleanup extends Notifier {

private final List<Pattern> patterns;
private final boolean deleteDirs;

@DataBoundConstructor
// FIXME can't get repeteable to work with a List<String>
public WsCleanup(List<Pattern> patterns) {
public WsCleanup(List<Pattern> patterns, boolean deleteDirs) {
this.patterns = patterns;
this.deleteDirs = deleteDirs;
}

public List<Pattern> getPatterns(){
return patterns;
}

public boolean getDeleteDirs(){
return deleteDirs;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
listener.getLogger().append("\nDeleting project workspace... ");
Expand All @@ -48,7 +54,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
if (patterns == null || patterns.isEmpty()) {
workspace.deleteRecursive();
} else {
workspace.act(new Cleanup(patterns));
workspace.act(new Cleanup(patterns,deleteDirs));
}
listener.getLogger().append("done\n\n");
} catch (InterruptedException ex) {
Expand Down
@@ -1,33 +1,30 @@
<!--
The MIT License
Copyright (c) 2010, CloudBees Inc, Nicolas De loof
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!-- The MIT License Copyright (c) 2010, CloudBees Inc, Nicolas De loof Permission
is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions: The above copyright notice
and this permission notice shall be included in all copies or substantial
portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY
OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. -->
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
<f:entry title="${%Patterns for files to be deleted}" help="/plugin/ws-cleanup/help/patterns.html">
<f:repeatable field="patterns">
<st:include page="config.jelly" class="hudson.plugins.ws_cleanup.Pattern"/>
<div align="right">
<f:repeatableDeleteButton />
</div>
</f:repeatable>
</f:entry>
<f:advanced align="left">
<f:entry title="${%Patterns for files to be deleted}" help="/plugin/ws-cleanup/help/patterns.html">
<f:repeatable field="patterns">
<st:include page="config.jelly" class="hudson.plugins.ws_cleanup.Pattern" />
<div align="right">
<f:repeatableDeleteButton />
</div>
</f:repeatable>
</f:entry>
<f:entry title="${%Apply pattern also on directories}">
<f:checkbox field="deleteDirs" checked="${it.deleteDirs}" />
</f:entry>
</f:advanced>
</j:jelly>
59 changes: 28 additions & 31 deletions src/main/resources/hudson/plugins/ws_cleanup/WsCleanup/config.jelly
@@ -1,33 +1,30 @@
<!--
The MIT License
Copyright (c) 2010, CloudBees Inc, Nicolas De loof
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!-- The MIT License Copyright (c) 2010, CloudBees Inc, Nicolas De loof Permission
is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions: The above copyright notice
and this permission notice shall be included in all copies or substantial
portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY
OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. -->
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
<f:entry title="${%Patterns for files to be deleted}" help="/plugin/ws-cleanup/help/patterns.html">
<f:repeatable field="patterns">
<st:include page="config.jelly" class="hudson.plugins.ws_cleanup.Pattern"/>
<div align="right">
<f:repeatableDeleteButton />
</div>
</f:repeatable>
</f:entry>
<f:advanced align="left">
<f:entry title="${%Patterns for files to be deleted}" help="/plugin/ws-cleanup/help/patterns.html">
<f:repeatable field="patterns">
<st:include page="config.jelly" class="hudson.plugins.ws_cleanup.Pattern" />
<div align="right">
<f:repeatableDeleteButton />
</div>
</f:repeatable>
</f:entry>
<f:entry title="${%Apply pattern also on directories}">
<f:checkbox field="deleteDirs" checked="${it.deleteDirs}" />
</f:entry>
</f:advanced>
</j:jelly>

0 comments on commit ae33d1e

Please sign in to comment.