diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/EmptyDirTask.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/EmptyDirTask.groovy deleted file mode 100644 index 12971b7d701..00000000000 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/EmptyDirTask.groovy +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.gradle - -import org.gradle.api.DefaultTask -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.TaskAction -import org.gradle.internal.nativeintegration.filesystem.Chmod - -import javax.inject.Inject - -/** - * Creates an empty directory. - */ -class EmptyDirTask extends DefaultTask { - @Input - Object dir - - @Input - int dirMode = 0755 - - @TaskAction - void create() { - dir = dir as File - dir.mkdirs() - getChmod().chmod(dir, dirMode) - } - - @Inject - Chmod getChmod() { - throw new UnsupportedOperationException() - } -} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/EmptyDirTask.java b/buildSrc/src/main/java/org/elasticsearch/gradle/EmptyDirTask.java new file mode 100644 index 00000000000..ec9b774767c --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/EmptyDirTask.java @@ -0,0 +1,83 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle; + +import java.io.File; + +import javax.inject.Inject; + +import org.gradle.api.DefaultTask; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.TaskAction; +import org.gradle.internal.nativeintegration.filesystem.Chmod; + +/** + * Creates an empty directory. + */ +public class EmptyDirTask extends DefaultTask { + + private File dir; + private int dirMode = 0755; + + /** + * Creates an empty directory with the configured permissions. + */ + @TaskAction + public void create() { + dir.mkdirs(); + getChmod().chmod(dir, dirMode); + } + + @Inject + public Chmod getChmod() { + throw new UnsupportedOperationException(); + } + + @Input + public File getDir() { + return dir; + } + + /** + * @param dir The directory to create + */ + public void setDir(File dir) { + this.dir = dir; + } + + /** + * @param dir The path of the directory to create. Takes a String and coerces it to a file. + */ + public void setDir(String dir) { + this.dir = getProject().file(dir); + } + + @Input + public int getDirMode() { + return dirMode; + } + + /** + * @param dirMode The permissions to apply to the new directory + */ + public void setDirMode(int dirMode) { + this.dirMode = dirMode; + } + +} diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/EmptyDirTaskTests.java b/buildSrc/src/test/java/org/elasticsearch/gradle/EmptyDirTaskTests.java new file mode 100644 index 00000000000..9cb49e8efd4 --- /dev/null +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/EmptyDirTaskTests.java @@ -0,0 +1,78 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle; + +import java.io.File; +import java.io.IOException; + +import org.elasticsearch.gradle.test.GradleUnitTestCase; +import org.gradle.api.Project; +import org.gradle.testfixtures.ProjectBuilder; + +public class EmptyDirTaskTests extends GradleUnitTestCase { + + public void testCreateEmptyDir() throws Exception { + Project project = ProjectBuilder.builder().build(); + EmptyDirTask emptyDirTask = project.getTasks().create("emptyDirTask", EmptyDirTask.class); + assertEquals(0755, emptyDirTask.getDirMode()); + + // generate a new temporary folder and make sure it does not exists + File newEmptyFolder = getNewNonExistingTempFolderFile(project); + + emptyDirTask.setDir(newEmptyFolder); + emptyDirTask.create(); + + assertTrue(newEmptyFolder.exists()); + assertTrue(newEmptyFolder.isDirectory()); + assertTrue(newEmptyFolder.canExecute()); + assertTrue(newEmptyFolder.canRead()); + assertTrue(newEmptyFolder.canWrite()); + + // cleanup + newEmptyFolder.delete(); + } + + public void testCreateEmptyDirNoPermissions() throws Exception { + Project project = ProjectBuilder.builder().build(); + EmptyDirTask emptyDirTask = project.getTasks().create("emptyDirTask", EmptyDirTask.class); + emptyDirTask.setDirMode(0000); + + // generate a new temporary folder and make sure it does not exists + File newEmptyFolder = getNewNonExistingTempFolderFile(project); + + emptyDirTask.setDir(newEmptyFolder); + emptyDirTask.create(); + + assertTrue(newEmptyFolder.exists()); + assertTrue(newEmptyFolder.isDirectory()); + assertFalse(newEmptyFolder.canExecute()); + assertFalse(newEmptyFolder.canRead()); + assertFalse(newEmptyFolder.canWrite()); + + // cleanup + newEmptyFolder.delete(); + } + + private File getNewNonExistingTempFolderFile(Project project) throws IOException { + File newEmptyFolder = new File(project.getBuildDir(), "empty-dir"); + assertFalse(newEmptyFolder.exists()); + return newEmptyFolder; + } + +} diff --git a/distribution/archives/build.gradle b/distribution/archives/build.gradle index 14a7e566587..ad4c77aaa5e 100644 --- a/distribution/archives/build.gradle +++ b/distribution/archives/build.gradle @@ -36,13 +36,13 @@ apply plugin: 'base' // parent to copy to the root of the distribution ext.logsDir = new File(buildDir, 'logs-hack/logs') task createLogsDir(type: EmptyDirTask) { - dir "${logsDir}" - dirMode 0755 + dir = "${logsDir}" + dirMode = 0755 } ext.pluginsDir= new File(buildDir, 'plugins-hack/plugins') task createPluginsDir(type: EmptyDirTask) { - dir "${pluginsDir}" - dirMode 0755 + dir = "${pluginsDir}" + dirMode = 0755 } CopySpec archiveFiles(CopySpec modulesFiles, String distributionType, String platform, boolean oss, boolean jdk) {