Skip to content

Commit

Permalink
Merge pull request #31 from melix/cc/add-codeready-event
Browse files Browse the repository at this point in the history
Add a `codeReady` event
  • Loading branch information
melix authored Jan 23, 2023
2 parents 6b4fe05 + af8d9ab commit 2ef45c6
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,40 @@ gitRepositories {
}
----

== Performing actions before the build is included

It is possible to perform actions right after a project has been cloned and before it is included.
This can be useful, for example, if the project needs to be analyzed in order to properly configure the included builds.

The action will always be called, even if sources are already available locally.
It is **not** recommended to mutate sources as part of this callback, since this will likely break updating the sources later.

.Executing code before the build is included
[source,groovy,role="multi-language-sample",subs="attributes+"]
----
gitRepositories {
include('my-project') {
// ...
codeReady { event ->
println("Project cloned in ${event.checkoutDirectory}")
}
}
}
----

[source,kotlin,role="multi-language-sample",subs="attributes+"]
----
gitRepositories {
include("my-project") {
// ...
codeReady {
println("Project cloned in ${checkoutDirectory}")
}
}
}
----


[[comparison]]
== Comparison of solutions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class BasicFunctionalTest extends AbstractFunctionalTest {
\\--- com.acme.somelib:somelib1:0.0 -> project :testlib0
'''

outputContains 'Code ready'

where:
useCommit << [false, true]
}
Expand Down
33 changes: 33 additions & 0 deletions plugin/src/main/java/me/champeau/gradle/igp/IncludedGitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.gradle.api.initialization.ConfigurableIncludedBuild;
import org.gradle.api.provider.Property;

import java.io.File;

/**
* Configures an included Git repository.
*/
Expand Down Expand Up @@ -94,4 +96,35 @@ default void includeBuild(String relativePath) {
* @param config the authentication configuration
*/
void authentication(Action<? super Authentication> config);

/**
* Allows executing some code right after a repository has been checked out, typically
* before the directory is included in a composite build.
* @param configuration the configuration action
*/
void codeReady(Action<? super CodeReadyEvent> configuration);

/**
* Base interface for events.
*/
interface Event {
/**
* The git repository for which the event was called
* @return the git repository
*/
IncludedGitRepo getIncludedGitRepo();
}

/**
* Event triggered when the code of an included repository
* is ready (e.g checked out). It is _not_ recommended to alter
* the contents of the directory as it will likely break updates.
*/
interface CodeReadyEvent extends Event {
/**
* The directory where the project is checked out.
* @return the directory
*/
File getCheckoutDirectory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public abstract class DefaultIncludedGitRepo implements IncludedGitRepo {
private final ObjectFactory objects;
private final Action<ConfigurableIncludedBuild> rootSpec;
private final List<IncludedBuild> includes = new ArrayList<>();
private final List<Action<? super CodeReadyEvent>> codeReadyEvents = new ArrayList<>();
private DefaultAuthentication auth;

@Inject
Expand All @@ -56,6 +57,11 @@ public void includeBuild(Action<? super ConfigurableIncludedBuild> spec) {
});
}

@Override
public void codeReady(Action<? super CodeReadyEvent> configuration) {
codeReadyEvents.add(configuration);
}

/**
* If this method is called, then the auto-include property will
* automatically be set to false.
Expand Down Expand Up @@ -86,6 +92,9 @@ Optional<DefaultAuthentication> getAuth() {
}

void configure(Settings settings, File checkoutDirectory) {
if (!codeReadyEvents.isEmpty()) {
fireCodeReadyEvents(checkoutDirectory);
}
if (getAutoInclude().get()) {
settings.includeBuild(checkoutDirectory, rootSpec);
} else {
Expand All @@ -97,6 +106,23 @@ void configure(Settings settings, File checkoutDirectory) {
}
}

private void fireCodeReadyEvents(File checkoutDirectory) {
CodeReadyEvent codeReadyEvent = new CodeReadyEvent() {
@Override
public IncludedGitRepo getIncludedGitRepo() {
return DefaultIncludedGitRepo.this;
}

@Override
public File getCheckoutDirectory() {
return checkoutDirectory;
}
};
for (Action<? super CodeReadyEvent> action : codeReadyEvents) {
action.execute(codeReadyEvent);
}
}

private static class IncludedBuild {
private final String directory;
private final Action<? super ConfigurableIncludedBuild> spec;
Expand Down
4 changes: 4 additions & 0 deletions samples/basic/groovy/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ gitRepositories {
if (gradle.startParameter.projectProperties.containsKey('checkoutDir')) {
checkoutDirectory.set(file(gradle.startParameter.projectProperties.get('checkoutDir')))
}
codeReady { event ->
println "Code ready"
println "Checkout directory: ${event.checkoutDirectory}"
}
}
}
4 changes: 4 additions & 0 deletions samples/basic/kotlin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ gitRepositories {
if (gradle.startParameter.projectProperties.containsKey("checkoutDir")) {
checkoutDirectory.set(file(gradle.startParameter.projectProperties.get("checkoutDir")!!))
}
codeReady {
println("Code ready")
println("Checkout directory: ${checkoutDirectory}")
}
}
}

0 comments on commit 2ef45c6

Please sign in to comment.