diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/FileContentsTask.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/FileContentsTask.groovy deleted file mode 100644 index 248083af5e0d5..0000000000000 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/FileContentsTask.groovy +++ /dev/null @@ -1,51 +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.OutputFile -import org.gradle.api.tasks.TaskAction - -/** - * Creates a file and sets it contents to something. - */ -class FileContentsTask extends DefaultTask { - /** - * The file to be built. Must be of type File to make @OutputFile happy. - */ - @OutputFile - File file - - @Input - Object contents - - /** - * The file to be built. Takes any objecct and coerces to a file. - */ - void setFile(Object file) { - this.file = file as File - } - - @TaskAction - void setContents() { - file = file as File - file.text = contents.toString() - } -} diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy index 0aa57502c39c1..1c2ce4ec3776a 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy @@ -245,8 +245,8 @@ class VagrantTestPlugin implements Plugin { Task createLinuxRunnerScript = project.tasks.create('createLinuxRunnerScript', FileContentsTask) { dependsOn copyPackagingTests - file "${testsDir}/run-tests.sh" - contents """\ + file = "${testsDir}/run-tests.sh" + contents = """\ if [ "\$#" -eq 0 ]; then test_args=( "${-> project.extensions.esvagrant.testClass}" ) else @@ -257,10 +257,10 @@ class VagrantTestPlugin implements Plugin { } Task createWindowsRunnerScript = project.tasks.create('createWindowsRunnerScript', FileContentsTask) { dependsOn copyPackagingTests - file "${testsDir}/run-tests.ps1" + file = "${testsDir}/run-tests.ps1" // the use of $args rather than param() here is deliberate because the syntax for array (multivalued) parameters is likely // a little trappy for those unfamiliar with powershell - contents """\ + contents = """\ if (\$args.Count -eq 0) { \$testArgs = @("${-> project.extensions.esvagrant.testClass}") } else { @@ -273,14 +273,14 @@ class VagrantTestPlugin implements Plugin { Task createVersionFile = project.tasks.create('createVersionFile', FileContentsTask) { dependsOn copyPackagingArchives - file "${archivesDir}/version" - contents project.version + file = "${archivesDir}/version" + contents = project.version } Task createUpgradeFromFile = project.tasks.create('createUpgradeFromFile', FileContentsTask) { dependsOn copyPackagingArchives - file "${archivesDir}/upgrade_from_version" - contents project.extensions.esvagrant.upgradeFromVersion.toString() + file = "${archivesDir}/upgrade_from_version" + contents = project.extensions.esvagrant.upgradeFromVersion.toString() } Task createUpgradeIsOssFile = project.tasks.create('createUpgradeIsOssFile', FileContentsTask) { @@ -291,8 +291,8 @@ class VagrantTestPlugin implements Plugin { throw new StopExecutionException("upgrade version is before 6.3.0") } } - file "${archivesDir}/upgrade_is_oss" - contents '' + file = "${archivesDir}/upgrade_is_oss" + contents = '' } File batsDir = new File(packagingDir, BATS) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/FileContentsTask.java b/buildSrc/src/main/java/org/elasticsearch/gradle/FileContentsTask.java new file mode 100644 index 0000000000000..e52c31c671ba2 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/FileContentsTask.java @@ -0,0 +1,68 @@ +/* + * 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 static java.nio.file.Files.write; + +import java.io.File; +import java.io.IOException; + +import org.gradle.api.DefaultTask; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.OutputFile; +import org.gradle.api.tasks.TaskAction; + +/** + * Creates a file and sets it contents to something. + */ +public class FileContentsTask extends DefaultTask { + + /** + * The file to be built. + */ + @OutputFile + private File file; + + @Input + private String contents; + + + /** + * the contents to be written in the file + */ + public void setContents(String contents) { + this.contents = contents; + } + + /** + * The file to be built. Takes a String or a File and coerces it to a file. + */ + public void setFile(File file) { + this.file = file; + } + + public void setFile(String file) { + this.file = getProject().file(file); + } + + @TaskAction + public void writeFile() throws IOException { + write(file.toPath(), contents.getBytes("UTF-8")); + } +} diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/FileContentsTaskTests.java b/buildSrc/src/test/java/org/elasticsearch/gradle/FileContentsTaskTests.java new file mode 100644 index 0000000000000..6fcbf5d166f06 --- /dev/null +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/FileContentsTaskTests.java @@ -0,0 +1,57 @@ +package org.elasticsearch.gradle; + + +import static java.util.Collections.singletonMap; +import static org.apache.commons.io.FileUtils.readFileToString; + +import java.io.File; +import java.io.IOException; + +import org.elasticsearch.gradle.test.GradleUnitTestCase; +import org.gradle.api.Project; +import org.gradle.testfixtures.ProjectBuilder; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +public class FileContentsTaskTests extends GradleUnitTestCase { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + private FileContentsTask fileContentsTask; + private Project project; + + public void testWriteFile() throws Exception { + project = createProject(); + fileContentsTask = createTask(project); + File targetFile = new File(project.getProjectDir(),"testFile"); + String expectedContent = "the expected content\nis here."; + + fileContentsTask.setFile(targetFile); + fileContentsTask.setContents(expectedContent); + fileContentsTask.writeFile(); + + assertEquals(expectedContent, readFileToString(targetFile)); + } + + public void testWriteFileSpecifiedByString() throws Exception { + project = createProject(); + fileContentsTask = createTask(project); + File targetFile = new File(project.getProjectDir(),"testFile"); + String expectedContent = "the expected content\nis here."; + + fileContentsTask.setFile("testFile"); + fileContentsTask.setContents(expectedContent); + fileContentsTask.writeFile(); + + assertEquals(expectedContent, readFileToString(targetFile)); + } + + private Project createProject() throws IOException { + return ProjectBuilder.builder().withProjectDir(temporaryFolder.newFolder()).build(); + } + + private FileContentsTask createTask(Project project) { + return (FileContentsTask) project.task(singletonMap("type", FileContentsTask.class), "fileContentTask"); + } + +}