Skip to content

Commit

Permalink
Make the plugin also applicable for initialization scripts and settin…
Browse files Browse the repository at this point in the history
…gs scripts (#18)

Address issue #15
  • Loading branch information
molian6 authored Sep 26, 2019
1 parent b7c66b3 commit 6d7a426
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,18 @@ publishing {
Where
* **PROJECT_ID** is the ID of the project.
* **REPOSITORY_ID** is the ID of the repository.

### Alternatives

If you need to use BuildArtifacts repositories inside your `init.gradle` or `settings.gradle`, please add the following configuration to the `~/.gradle/init.gradle` file.
```gradle
initscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "gradle.plugin.com.google.cloud.buildartifacts:buildartifacts-gradle-plugin:1.1.4"
}
}
apply plugin: com.google.cloud.buildartifacts.gradle.plugin.BuildArtifactsGradlePlugin
```
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@
import java.net.URISyntaxException;

import org.gradle.api.artifacts.repositories.ArtifactRepository;
import org.gradle.api.artifacts.repositories.AuthenticationContainer;
import org.gradle.api.artifacts.repositories.PasswordCredentials;
import org.gradle.api.Action;
import org.gradle.api.credentials.Credentials;
import org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository;
import org.gradle.api.initialization.Settings;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.ProjectConfigurationException;
import org.gradle.api.UncheckedIOException;
import org.gradle.internal.authentication.DefaultBasicAuthentication;
import org.gradle.plugin.management.PluginManagementSpec;

public class BuildArtifactsGradlePlugin implements Plugin<Project> {
public class BuildArtifactsGradlePlugin implements Plugin<Object> {

static class BuildArtifactsPasswordCredentials implements PasswordCredentials {
private String username;
private String password;

public BuildArtifactsPasswordCredentials(String username, String password) {
BuildArtifactsPasswordCredentials(String username, String password) {
this.username = username;
this.password = password;
}
Expand Down Expand Up @@ -73,24 +73,55 @@ public void setPassword(String password) {
private CredentialProvider credentialProvider = new DefaultCredentialProvider();

@Override
public void apply(Project project) {
project.afterEvaluate(p -> {
project.getRepositories().all(this::configureBuildArtifactsRepositories);
final PublishingExtension publishingExtension = project.getExtensions().findByType(PublishingExtension.class);
if (publishingExtension != null) {
publishingExtension.getRepositories().all(this::configureBuildArtifactsRepositories);
}
});
public void apply(Object o) {
if (o instanceof Project) {
applyProject((Project) o);
} else if (o instanceof Gradle) {
applyGradle((Gradle) o);
} else if (o instanceof Settings) {
applySettings((Settings) o);
}
}

// The plugin for Gradle will apply CBA repo settings inside settings.gradle and build.gradle.
private void applyGradle(Gradle gradle) {
gradle.settingsEvaluated​(s -> modifySettings(s));
gradle.projectsEvaluated(g -> g.allprojects(p -> modifyProject(p)));
}

// The plugin for settings will apply CBA repo settings inside settings.gradle and build.gradle.
private void applySettings(Settings settings) {
applyGradle(settings.getGradle());
}

// The plugin for projects will only apply CBA repo settings inside build.gradle.
private void applyProject(Project project) {
project.afterEvaluate(p -> modifyProject(p));
}

public void configureBuildArtifactsRepositories(ArtifactRepository repo)
private void modifyProject(Project p) {
p.getRepositories().all(this::configureBuildArtifactsRepository);
final PublishingExtension publishingExtension = p.getExtensions().findByType(PublishingExtension.class);
if (publishingExtension != null) {
publishingExtension.getRepositories().all(this::configureBuildArtifactsRepository);
}
}

private void modifySettings(Settings s) {
final PluginManagementSpec pluginManagement = s.getPluginManagement();
if (pluginManagement != null) {
pluginManagement.getRepositories().all(this::configureBuildArtifactsRepository);
}
}

private void configureBuildArtifactsRepository(ArtifactRepository repo)
throws ProjectConfigurationException, UncheckedIOException
{
if (!(repo instanceof DefaultMavenArtifactRepository)) {
return;
}
final DefaultMavenArtifactRepository cbaRepo = (DefaultMavenArtifactRepository) repo;
final URI u = cbaRepo.getUrl();
final URI u = cbaRepo.getUrl();
if (u != null && u.getScheme() != null && u.getScheme().equals("buildartifacts")) {
try {
cbaRepo.setUrl(new URI("https", u.getHost(), u.getPath(), u.getFragment()));
Expand All @@ -105,18 +136,12 @@ public void configureBuildArtifactsRepositories(ArtifactRepository repo)
AccessToken accessToken = credentials.getAccessToken();
String token = accessToken.getTokenValue();
BuildArtifactsPasswordCredentials crd = new BuildArtifactsPasswordCredentials("oauth2accesstoken", token);
cbaRepo.setConfiguredCredentials((Credentials)crd);
cbaRepo.authentication(new Action<AuthenticationContainer>() {
@Override
public void execute(AuthenticationContainer authenticationContainer) {
authenticationContainer.add(new DefaultBasicAuthentication("basic"));
}
});
cbaRepo.setConfiguredCredentials(crd);
cbaRepo.authentication(authenticationContainer -> authenticationContainer.add(new DefaultBasicAuthentication("basic")));
} catch (IOException e) {
throw new UncheckedIOException("Failed to get access token from gcloud or Application Default Credentials", e);
}
}
}
}
}

0 comments on commit 6d7a426

Please sign in to comment.