Skip to content

Commit de32917

Browse files
vboulayealpar-t
authored andcommitted
convert EmptyDirTask.groovy to .java (#34672)
1 parent 15908ac commit de32917

File tree

4 files changed

+165
-53
lines changed

4 files changed

+165
-53
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/EmptyDirTask.groovy

-49
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.gradle;
20+
21+
import java.io.File;
22+
23+
import javax.inject.Inject;
24+
25+
import org.gradle.api.DefaultTask;
26+
import org.gradle.api.tasks.Input;
27+
import org.gradle.api.tasks.TaskAction;
28+
import org.gradle.internal.nativeintegration.filesystem.Chmod;
29+
30+
/**
31+
* Creates an empty directory.
32+
*/
33+
public class EmptyDirTask extends DefaultTask {
34+
35+
private File dir;
36+
private int dirMode = 0755;
37+
38+
/**
39+
* Creates an empty directory with the configured permissions.
40+
*/
41+
@TaskAction
42+
public void create() {
43+
dir.mkdirs();
44+
getChmod().chmod(dir, dirMode);
45+
}
46+
47+
@Inject
48+
public Chmod getChmod() {
49+
throw new UnsupportedOperationException();
50+
}
51+
52+
@Input
53+
public File getDir() {
54+
return dir;
55+
}
56+
57+
/**
58+
* @param dir The directory to create
59+
*/
60+
public void setDir(File dir) {
61+
this.dir = dir;
62+
}
63+
64+
/**
65+
* @param dir The path of the directory to create. Takes a String and coerces it to a file.
66+
*/
67+
public void setDir(String dir) {
68+
this.dir = getProject().file(dir);
69+
}
70+
71+
@Input
72+
public int getDirMode() {
73+
return dirMode;
74+
}
75+
76+
/**
77+
* @param dirMode The permissions to apply to the new directory
78+
*/
79+
public void setDirMode(int dirMode) {
80+
this.dirMode = dirMode;
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.gradle;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
24+
import org.elasticsearch.gradle.test.GradleUnitTestCase;
25+
import org.gradle.api.Project;
26+
import org.gradle.testfixtures.ProjectBuilder;
27+
28+
public class EmptyDirTaskTests extends GradleUnitTestCase {
29+
30+
public void testCreateEmptyDir() throws Exception {
31+
Project project = ProjectBuilder.builder().build();
32+
EmptyDirTask emptyDirTask = project.getTasks().create("emptyDirTask", EmptyDirTask.class);
33+
assertEquals(0755, emptyDirTask.getDirMode());
34+
35+
// generate a new temporary folder and make sure it does not exists
36+
File newEmptyFolder = getNewNonExistingTempFolderFile(project);
37+
38+
emptyDirTask.setDir(newEmptyFolder);
39+
emptyDirTask.create();
40+
41+
assertTrue(newEmptyFolder.exists());
42+
assertTrue(newEmptyFolder.isDirectory());
43+
assertTrue(newEmptyFolder.canExecute());
44+
assertTrue(newEmptyFolder.canRead());
45+
assertTrue(newEmptyFolder.canWrite());
46+
47+
// cleanup
48+
newEmptyFolder.delete();
49+
}
50+
51+
public void testCreateEmptyDirNoPermissions() throws Exception {
52+
Project project = ProjectBuilder.builder().build();
53+
EmptyDirTask emptyDirTask = project.getTasks().create("emptyDirTask", EmptyDirTask.class);
54+
emptyDirTask.setDirMode(0000);
55+
56+
// generate a new temporary folder and make sure it does not exists
57+
File newEmptyFolder = getNewNonExistingTempFolderFile(project);
58+
59+
emptyDirTask.setDir(newEmptyFolder);
60+
emptyDirTask.create();
61+
62+
assertTrue(newEmptyFolder.exists());
63+
assertTrue(newEmptyFolder.isDirectory());
64+
assertFalse(newEmptyFolder.canExecute());
65+
assertFalse(newEmptyFolder.canRead());
66+
assertFalse(newEmptyFolder.canWrite());
67+
68+
// cleanup
69+
newEmptyFolder.delete();
70+
}
71+
72+
private File getNewNonExistingTempFolderFile(Project project) throws IOException {
73+
File newEmptyFolder = new File(project.getBuildDir(), "empty-dir");
74+
assertFalse(newEmptyFolder.exists());
75+
return newEmptyFolder;
76+
}
77+
78+
}

distribution/archives/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ apply plugin: 'base'
3636
// parent to copy to the root of the distribution
3737
ext.logsDir = new File(buildDir, 'logs-hack/logs')
3838
task createLogsDir(type: EmptyDirTask) {
39-
dir "${logsDir}"
40-
dirMode 0755
39+
dir = "${logsDir}"
40+
dirMode = 0755
4141
}
4242
ext.pluginsDir= new File(buildDir, 'plugins-hack/plugins')
4343
task createPluginsDir(type: EmptyDirTask) {
44-
dir "${pluginsDir}"
45-
dirMode 0755
44+
dir = "${pluginsDir}"
45+
dirMode = 0755
4646
}
4747

4848
CopySpec archiveFiles(CopySpec modulesFiles, String distributionType, String platform, boolean oss, boolean jdk) {

0 commit comments

Comments
 (0)