Skip to content

Commit

Permalink
Migrate plugin conventions to extensions (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Oct 1, 2023
1 parent 2511649 commit 1e30e99
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.jfrog.gradle.plugin.artifactory.listener.ArtifactoryDependencyResolutionListener;
import org.jfrog.gradle.plugin.artifactory.listener.ProjectsEvaluatedBuildListener;
import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask;
import org.jfrog.gradle.plugin.artifactory.utils.ConventionUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ExtensionsUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ProjectUtils;
import org.jfrog.gradle.plugin.artifactory.utils.TaskUtils;

Expand All @@ -25,8 +25,8 @@ public void apply(Project project) {
if (!shouldApplyPluginOnProject(project)) {
return;
}
// Get / Add an Artifactory plugin convention to the project module
ArtifactoryPluginConvention convention = ConventionUtils.getOrCreateArtifactoryConvention(project);
// Get / Add an Artifactory plugin extension to the project module
ArtifactoryPluginConvention extension = ExtensionsUtils.getOrCreateArtifactoryExtension(project);
// Add the collect publications for deploy details and extract module-info tasks to the project module
TaskProvider<ArtifactoryTask> collectDeployDetailsTask = TaskUtils.addCollectDeployDetailsTask(project);
TaskUtils.addExtractModuleInfoTask(collectDeployDetailsTask, project);
Expand All @@ -50,9 +50,9 @@ public void apply(Project project) {
}

// Set build started if not set
String buildStarted = convention.getClientConfig().info.getBuildStarted();
String buildStarted = extension.getClientConfig().info.getBuildStarted();
if (buildStarted == null || buildStarted.isEmpty()) {
convention.getClientConfig().info.setBuildStarted(System.currentTimeMillis());
extension.getClientConfig().info.setBuildStarted(System.currentTimeMillis());
}

log.debug("Using Artifactory Plugin for " + project.getPath());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.jfrog.gradle.plugin.artifactory.dsl;

import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.util.ConfigureUtil;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.gradle.plugin.artifactory.utils.GradleClientLogger;

Expand All @@ -29,29 +27,17 @@ public void setContextUrl(String contextUrl) {
clientConfig.publisher.setContextUrl(contextUrl);
}

public void publish(Closure<PublisherConfig> closure) {
publish(ConfigureUtil.configureUsing(closure));
}

public void publish(Action<PublisherConfig> publishAction) {
publisherConfig = new PublisherConfig(this);
publisherConfig = project.getObjects().newInstance(PublisherConfig.class, project.getObjects(), this);
publishAction.execute(publisherConfig);
}

@SuppressWarnings("unused")
public void buildInfo(Closure<ArtifactoryClientConfiguration.BuildInfoHandler> closure) {
buildInfo(ConfigureUtil.configureUsing(closure));
}

public void buildInfo(Action<ArtifactoryClientConfiguration.BuildInfoHandler> buildInfoAction) {
buildInfoAction.execute(clientConfig.info);
}

@SuppressWarnings("unused")
public void proxy(Closure<ArtifactoryClientConfiguration.ProxyHandler> closure) {
proxy(ConfigureUtil.configureUsing(closure));
}

public void proxy(Action<ArtifactoryClientConfiguration.ProxyHandler> buildInfoAction) {
buildInfoAction.execute(clientConfig.proxy);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.jfrog.gradle.plugin.artifactory.dsl;

import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.util.ConfigureUtil;
import org.gradle.api.model.ObjectFactory;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask;

import javax.inject.Inject;

/**
* Main publish configuration object for the plugin. a DSL object that controls all the plugin publishing configurations.
* This object is defined and used at the user build script ander 'publish' in the 'artifactory' closure.
Expand All @@ -18,9 +19,10 @@ public class PublisherConfig {
// Configure global task that will be applied to all the projects
Action<ArtifactoryTask> defaultsAction;

public PublisherConfig(ArtifactoryPluginConvention convention) {
this.publisher = convention.getClientConfig().publisher;
repository = new Repository(this.publisher);
@Inject
public PublisherConfig(ObjectFactory objectFactory, ArtifactoryPluginConvention extension) {
this.publisher = extension.getClientConfig().publisher;
repository = objectFactory.newInstance(Repository.class, publisher);
}

@SuppressWarnings("unused")
Expand All @@ -33,11 +35,6 @@ public void setContextUrl(String contextUrl) {
this.publisher.setContextUrl(contextUrl);
}

@SuppressWarnings("unused")
public void defaults(Closure<ArtifactoryTask> closure) {
defaults(ConfigureUtil.configureUsing(closure));
}

public void defaults(Action<ArtifactoryTask> defaultsAction) {
this.defaultsAction = defaultsAction;
}
Expand Down Expand Up @@ -67,9 +64,9 @@ public void setForkCount(int forkCount) {
}

@SuppressWarnings("unused")
public void repository(Closure<Repository> closure) { repository(ConfigureUtil.configureUsing(closure)); }

public void repository(Action<Repository> repositoryAction) { repositoryAction.execute(repository); }
public void repository(Action<Repository> repositoryAction) {
repositoryAction.execute(repository);
}

@SuppressWarnings("unused")
public Repository getRepository() {
Expand All @@ -80,6 +77,7 @@ public static class Repository {
private final ArtifactoryClientConfiguration.PublisherHandler publisher;
private final IvyPublishInfo ivyPublishInfo;

@Inject
public Repository(ArtifactoryClientConfiguration.PublisherHandler publisher) {
this.publisher = publisher;
this.ivyPublishInfo = new IvyPublishInfo(publisher);
Expand Down Expand Up @@ -112,10 +110,6 @@ public void setPassword(String password) {
}

@SuppressWarnings("unused")
public void ivy(Closure<IvyPublishInfo> closure) {
ivy(ConfigureUtil.configureUsing(closure));
}

public void ivy(Action<IvyPublishInfo> ivyAction) {
ivyAction.execute(ivyPublishInfo);
}
Expand All @@ -128,6 +122,7 @@ public IvyPublishInfo getIvy() {

public static class IvyPublishInfo {
private final ArtifactoryClientConfiguration.PublisherHandler publisher;

public IvyPublishInfo(ArtifactoryClientConfiguration.PublisherHandler publisher) {
this.publisher = publisher;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin;
import org.jfrog.gradle.plugin.artifactory.listener.ArtifactoryDependencyResolutionListener;
import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask;
import org.jfrog.gradle.plugin.artifactory.utils.ConventionUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ExtensionsUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ProjectUtils;
import org.jfrog.gradle.plugin.artifactory.utils.TaskUtils;

Expand Down Expand Up @@ -86,7 +86,7 @@ private ModuleBuilder getModuleBuilder(Project project, Set<GradleDeployDetails>
// Extract the module's dependencies
builder.dependencies(calculateDependencies(project, moduleId));
// Extract the module's artifacts
ArtifactoryClientConfiguration.PublisherHandler publisher = ConventionUtils.getPublisherHandler(project);
ArtifactoryClientConfiguration.PublisherHandler publisher = ExtensionsUtils.getPublisherHandler(project);
if (publisher == null) {
log.warn("No publisher config found for project: " + project.getName());
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.jfrog.gradle.plugin.artifactory.Constant;
import org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention;
import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask;
import org.jfrog.gradle.plugin.artifactory.utils.ConventionUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ExtensionsUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ProjectUtils;
import org.jfrog.gradle.plugin.artifactory.utils.PublicationUtils;

Expand All @@ -39,18 +39,18 @@ public class ProjectsEvaluatedBuildListener {
private void evaluate(ArtifactoryTask collectDeployDetailsTask) {
log.debug("Try to evaluate {}", collectDeployDetailsTask);
Project project = collectDeployDetailsTask.getProject();
ArtifactoryPluginConvention convention = ConventionUtils.getArtifactoryConvention(project);
if (convention == null) {
log.debug("Can't find artifactory convention.");
ArtifactoryPluginConvention extension = ExtensionsUtils.getArtifactoryExtension(project);
if (extension == null) {
log.debug("Can't find artifactory extension.");
return;
}
ArtifactoryClientConfiguration clientConfiguration = convention.getClientConfig();
ArtifactoryClientConfiguration clientConfiguration = extension.getClientConfig();
if (clientConfiguration == null) {
log.debug("Client configuration not defined.");
return;
}
// Fill-in the client config with current user/system properties for the given project
ConventionUtils.updateConfig(clientConfiguration, project);
ExtensionsUtils.updateConfig(clientConfiguration, project);
// Set task attributes if running on CI Server
if (collectDeployDetailsTask.isCiServerBuild()) {
addCiAttributesToTask(collectDeployDetailsTask, clientConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import groovy.lang.Closure;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.*;
import org.gradle.api.artifacts.Configuration;
Expand All @@ -20,15 +19,14 @@
import org.gradle.api.publish.maven.internal.publication.MavenPublicationInternal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.*;
import org.gradle.util.ConfigureUtil;
import org.jfrog.build.extractor.clientConfiguration.ArtifactSpecs;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.gradle.plugin.artifactory.Constant;
import org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention;
import org.jfrog.gradle.plugin.artifactory.dsl.PropertiesConfig;
import org.jfrog.gradle.plugin.artifactory.dsl.PublisherConfig;
import org.jfrog.gradle.plugin.artifactory.extractor.GradleDeployDetails;
import org.jfrog.gradle.plugin.artifactory.utils.ConventionUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ExtensionsUtils;
import org.jfrog.gradle.plugin.artifactory.utils.PublicationUtils;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -95,16 +93,16 @@ public void evaluateTask() {
}
}

ArtifactoryPluginConvention convention = ConventionUtils.getConventionWithPublisher(project);
if (convention == null) {
log.debug("Can't find convention configured for {}", getPath());
ArtifactoryPluginConvention extension = ExtensionsUtils.getExtensionWithPublisher(project);
if (extension == null) {
log.debug("Can't find extension configured for {}", getPath());
return;
}
// Add global properties to the specs
artifactSpecs.clear();
artifactSpecs.addAll(convention.getClientConfig().publisher.getArtifactSpecs());
artifactSpecs.addAll(extension.getClientConfig().publisher.getArtifactSpecs());
// Configure the task using the "defaults" action if exists (delegate to the task)
PublisherConfig config = convention.getPublisherConfig();
PublisherConfig config = extension.getPublisherConfig();
if (config != null) {
Action<ArtifactoryTask> defaultsAction = config.getDefaultsAction();
if (defaultsAction != null) {
Expand Down Expand Up @@ -159,7 +157,7 @@ private void collectDetailsFromMavenPublications() {
}

private void collectDetailsFromConfigurations() {
ArtifactoryClientConfiguration.PublisherHandler publisher = ConventionUtils.getPublisherHandler(getProject());
ArtifactoryClientConfiguration.PublisherHandler publisher = ExtensionsUtils.getPublisherHandler(getProject());
if (publisher == null) {
return;
}
Expand Down Expand Up @@ -366,10 +364,6 @@ public void finalizeByDeployTask(Project project) {
}
}

public void properties(Closure<PropertiesConfig> closure) {
properties(ConfigureUtil.configureUsing(closure));
}

public void properties(Action<PropertiesConfig> propertiesAction) {
PropertiesConfig propertiesConfig = new PropertiesConfig(getProject());
propertiesAction.execute(propertiesConfig);
Expand Down Expand Up @@ -436,7 +430,7 @@ public Map<String, String> getDefaultProps() {
defaultProps = new HashMap<>();
PublicationUtils.addProps(defaultProps, getProperties());
// Add the publisher properties
ArtifactoryClientConfiguration.PublisherHandler publisher = ConventionUtils.getPublisherHandler(getProject().getRootProject());
ArtifactoryClientConfiguration.PublisherHandler publisher = ExtensionsUtils.getPublisherHandler(getProject().getRootProject());
if (publisher != null) {
defaultProps.putAll(publisher.getMatrixParams());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.jfrog.gradle.plugin.artifactory.extractor.GradleBuildInfoExtractor;
import org.jfrog.gradle.plugin.artifactory.extractor.ModuleInfoFileProducer;
import org.jfrog.gradle.plugin.artifactory.Constant;
import org.jfrog.gradle.plugin.artifactory.utils.ConventionUtils;
import org.jfrog.gradle.plugin.artifactory.utils.ExtensionsUtils;
import org.jfrog.gradle.plugin.artifactory.utils.DeployUtils;
import org.jfrog.gradle.plugin.artifactory.utils.TaskUtils;

Expand Down Expand Up @@ -51,7 +51,7 @@ public FileCollection getModuleInfoFiles() {
@TaskAction
public void extractBuildInfoAndDeploy() throws IOException {
log.debug("Extracting build-info and deploying build details in task '{}'", getPath());
ArtifactoryClientConfiguration accRoot = ConventionUtils.getArtifactoryConvention(getProject()).getClientConfig();
ArtifactoryClientConfiguration accRoot = ExtensionsUtils.getArtifactoryExtension(getProject()).getClientConfig();
// Deploy Artifacts to artifactory
Map<String, Set<DeployDetails>> allDeployedDetails = deployArtifactsFromTasks(accRoot);
// Generate build-info and handle deployment (and artifact exports if configured)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void deployTaskArtifacts(ArtifactoryClientConfiguration accRoot, M
log.debug("Task '{}' has nothing to deploy", artifactoryTask.getPath());
return;
}
ArtifactoryClientConfiguration.PublisherHandler taskPublisher = ConventionUtils.getPublisherHandler(artifactoryTask.getProject());
ArtifactoryClientConfiguration.PublisherHandler taskPublisher = ExtensionsUtils.getPublisherHandler(artifactoryTask.getProject());
if (taskPublisher == null) {
log.debug("Task '{}' does not have publisher configured", artifactoryTask.getPath());
return;
Expand Down
Loading

0 comments on commit 1e30e99

Please sign in to comment.