-
Notifications
You must be signed in to change notification settings - Fork 25.6k
refactor: convert FileContentsTask to Java #34539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
baptistemesta
wants to merge
1
commit into
elastic:master
from
baptistemesta:refactor/filecontentstask-to-java
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 0 additions & 51 deletions
51
buildSrc/src/main/groovy/org/elasticsearch/gradle/FileContentsTask.groovy
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
buildSrc/src/main/java/org/elasticsearch/gradle/FileContentsTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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")); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
buildSrc/src/test/java/org/elasticsearch/gradle/FileContentsTaskTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.