-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Verify Samples in Build #10031
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
Merged
Merged
Verify Samples in Build #10031
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
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
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
104 changes: 104 additions & 0 deletions
104
buildSrc/src/main/groovy/io/spring/gradle/IncludeRepoTask.groovy
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,104 @@ | ||
/* | ||
* Copyright 2002-2021 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.spring.gradle | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.transform.TypeChecked | ||
import groovy.transform.TypeCheckingMode | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.Task | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
/** | ||
* Checkout a project template from a git repository. | ||
* | ||
* @author Marcus Da Coregio | ||
*/ | ||
@CompileStatic | ||
abstract class IncludeRepoTask extends DefaultTask { | ||
|
||
private static final String DEFAULT_URI_PREFIX = 'https://github.com/' | ||
|
||
/** | ||
* Git repository to use. Will be prefixed with {@link #DEFAULT_URI_PREFIX} if it isn't already | ||
* @return | ||
*/ | ||
@Input | ||
abstract Property<String> getRepository(); | ||
|
||
/** | ||
* Git reference to use. | ||
*/ | ||
@Input | ||
abstract Property<String> getRef() | ||
|
||
/** | ||
* Directory where the project template should be copied. | ||
*/ | ||
@OutputDirectory | ||
final File outputDirectory = project.file("$project.buildDir/$name") | ||
|
||
@TaskAction | ||
void checkoutAndCopy() { | ||
outputDirectory.deleteDir() | ||
File checkoutDir = checkout(this, getRemoteUri(), ref.get()) | ||
moveToOutputDir(checkoutDir, outputDirectory) | ||
} | ||
|
||
private static File cleanTemporaryDir(Task task, File tmpDir) { | ||
if (tmpDir.exists()) { | ||
task.project.delete(tmpDir) | ||
} | ||
return tmpDir | ||
} | ||
|
||
static File checkout(Task task, String remoteUri, String ref) { | ||
checkout(task, remoteUri, ref, task.getTemporaryDir()) | ||
} | ||
|
||
@TypeChecked(TypeCheckingMode.SKIP) | ||
static File checkout(Task task, String remoteUri, String ref, File checkoutDir) { | ||
cleanTemporaryDir(task, checkoutDir) | ||
task.project.exec { | ||
commandLine = ["git", "clone", "--no-checkout", remoteUri, checkoutDir.absolutePath] | ||
errorOutput = System.err | ||
} | ||
task.project.exec { | ||
commandLine = ["git", "checkout", ref] | ||
workingDir = checkoutDir | ||
errorOutput = System.err | ||
} | ||
return checkoutDir | ||
} | ||
|
||
private static void moveToOutputDir(File tmpDir, File outputDirectory) { | ||
File baseDir = tmpDir | ||
baseDir.renameTo(outputDirectory) | ||
} | ||
|
||
private String getRemoteUri() { | ||
String remoteUri = this.repository.get() | ||
if (remoteUri.startsWith(DEFAULT_URI_PREFIX)) { | ||
return remoteUri | ||
} | ||
return DEFAULT_URI_PREFIX + remoteUri | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
buildSrc/src/main/groovy/io/spring/gradle/convention/IncludeCheckRemotePlugin.groovy
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,65 @@ | ||
/* | ||
* Copyright 2002-2021 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.spring.gradle.convention | ||
|
||
import io.spring.gradle.IncludeRepoTask | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.GradleBuild | ||
import org.gradle.api.tasks.TaskProvider | ||
|
||
/** | ||
* Adds a set of tasks that make easy to clone a remote repository and perform some task | ||
* | ||
* @author Marcus Da Coregio | ||
*/ | ||
class IncludeCheckRemotePlugin implements Plugin<Project> { | ||
@Override | ||
void apply(Project project) { | ||
IncludeCheckRemoteExtension extension = project.extensions.create('includeCheckRemote', IncludeCheckRemoteExtension) | ||
TaskProvider<IncludeRepoTask> includeRepoTask = project.tasks.register('includeRepo', IncludeRepoTask) { IncludeRepoTask it -> | ||
it.repository = extension.repository.get() | ||
it.ref = extension.ref.get() | ||
} | ||
project.tasks.register('checkRemote', GradleBuild) { | ||
it.dependsOn 'includeRepo' | ||
it.dir = includeRepoTask.get().outputDirectory | ||
it.tasks = extension.getTasks() | ||
} | ||
} | ||
|
||
abstract static class IncludeCheckRemoteExtension { | ||
/** | ||
* Git repository to clone | ||
*/ | ||
abstract Property<String> getRepository(); | ||
/** | ||
* Git ref to checkout | ||
*/ | ||
abstract Property<String> getRef(); | ||
/** | ||
* Task to run in the repository | ||
*/ | ||
List<String> tasks = ['check'] | ||
|
||
void setTask(List<String> tasks) { | ||
this.tasks = tasks | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/gradle-plugins/io.spring.convention.include-check-remote.properties
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 @@ | ||
implementation-class=io.spring.gradle.convention.IncludeCheckRemotePlugin |
100 changes: 100 additions & 0 deletions
100
buildSrc/src/test/java/io/spring/gradle/convention/IncludeCheckRemotePluginTest.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,100 @@ | ||
/* | ||
* Copyright 2002-2021 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.spring.gradle.convention; | ||
|
||
import io.spring.gradle.IncludeRepoTask; | ||
import org.apache.commons.io.FileUtils; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.tasks.GradleBuild; | ||
import org.gradle.testfixtures.ProjectBuilder; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class IncludeCheckRemotePluginTest { | ||
|
||
Project rootProject; | ||
|
||
@AfterEach | ||
public void cleanup() throws Exception { | ||
if (rootProject != null) { | ||
FileUtils.deleteDirectory(rootProject.getProjectDir()); | ||
} | ||
} | ||
|
||
@Test | ||
void applyWhenExtensionPropertiesNoTasksThenCreateCheckRemoteTaskWithDefaultTask() { | ||
this.rootProject = ProjectBuilder.builder().build(); | ||
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); | ||
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, | ||
(includeCheckRemoteExtension) -> { | ||
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); | ||
includeCheckRemoteExtension.setProperty("ref", "main"); | ||
}); | ||
|
||
GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get(); | ||
assertThat(checkRemote.getTasks()).containsExactly("check"); | ||
} | ||
|
||
@Test | ||
void applyWhenExtensionPropertiesTasksThenCreateCheckRemoteWithProvidedTasks() { | ||
this.rootProject = ProjectBuilder.builder().build(); | ||
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); | ||
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, | ||
(includeCheckRemoteExtension) -> { | ||
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); | ||
includeCheckRemoteExtension.setProperty("ref", "main"); | ||
includeCheckRemoteExtension.setProperty("tasks", Arrays.asList("clean", "build", "test")); | ||
}); | ||
|
||
GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get(); | ||
assertThat(checkRemote.getTasks()).containsExactly("clean", "build", "test"); | ||
} | ||
|
||
@Test | ||
void applyWhenExtensionPropertiesThenRegisterIncludeRepoTaskWithExtensionProperties() { | ||
this.rootProject = ProjectBuilder.builder().build(); | ||
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); | ||
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, | ||
(includeCheckRemoteExtension) -> { | ||
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); | ||
includeCheckRemoteExtension.setProperty("ref", "main"); | ||
}); | ||
|
||
IncludeRepoTask includeRepo = (IncludeRepoTask) this.rootProject.getTasks().named("includeRepo").get(); | ||
assertThat(includeRepo).isNotNull(); | ||
assertThat(includeRepo.getRepository().get()).isEqualTo("my-project/my-repository"); | ||
assertThat(includeRepo.getRef().get()).isEqualTo("main"); | ||
} | ||
|
||
@Test | ||
void applyWhenRegisterTasksThenCheckRemoteDirSameAsIncludeRepoOutputDir() { | ||
this.rootProject = ProjectBuilder.builder().build(); | ||
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); | ||
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, | ||
(includeCheckRemoteExtension) -> { | ||
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); | ||
includeCheckRemoteExtension.setProperty("ref", "main"); | ||
}); | ||
IncludeRepoTask includeRepo = (IncludeRepoTask) this.rootProject.getTasks().named("includeRepo").get(); | ||
GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get(); | ||
assertThat(checkRemote.getDir()).isEqualTo(includeRepo.getOutputDirectory()); | ||
} | ||
} |
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.