This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves GH-37: Allow loading dependency versions from a properties file
- Loading branch information
Showing
17 changed files
with
284 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
org.starchartlabs.alloy:alloy-core=0.4.1 | ||
|
||
org.testng:testng=6.9.10 |
This file contains 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,34 @@ | ||
# org.starchartlabs.flare.dependency-versions | ||
|
||
The `dependency-versions` plug-in is a configuration plug-in which adds a standard DSL extension `dependencyVersions` to allow loading dependency version constraints from a properties file | ||
|
||
## Application | ||
|
||
Apply the plug-in via the buildscript classpath: | ||
|
||
``` | ||
buildscript { | ||
repositories { | ||
jcenter() | ||
} | ||
dependencies { | ||
classpath group: 'org.starchartlabs.flare', name: 'flare-operations-plugins', version: '3.0.0' | ||
} | ||
} | ||
apply plugin: 'org.starchartlabs.flare.dependency-versions' | ||
``` | ||
|
||
## Use | ||
|
||
To specify the publication information: | ||
|
||
``` | ||
dependencyVersions { | ||
propertiesFile file('...') | ||
} | ||
``` | ||
|
||
The file referenced is expected to be a properties file with lines in the form of | ||
|
||
`group:artifact=version` |
This file contains 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 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,3 @@ | ||
org.starchartlabs.alloy:alloy-core=0.4.1 | ||
|
||
org.testng:testng=6.9.10 |
Binary file not shown.
3 changes: 1 addition & 2 deletions
3
flare-ops-gradle-test/gradle/wrapper/gradle-wrapper.properties
This file contains 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#Wed Jun 19 21:06:27 EDT 2019 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip |
This file contains 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 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
108 changes: 108 additions & 0 deletions
108
src/main/java/org/starchartlabs/flare/operations/model/DependencyVersions.java
This file contains 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,108 @@ | ||
/* | ||
* Copyright (C) 2019 [email protected] Authors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
package org.starchartlabs.flare.operations.model; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.AbstractMap; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.gradle.api.GradleException; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.dsl.DependencyConstraintHandler; | ||
import org.starchartlabs.alloy.core.Preconditions; | ||
import org.starchartlabs.alloy.core.Strings; | ||
|
||
/** | ||
* Represents configuration for loading dependency version constraints from simple files | ||
* | ||
* @author romeara | ||
* @since 3.0.0 | ||
*/ | ||
public class DependencyVersions { | ||
|
||
private final Project project; | ||
|
||
private Map<String, String> versionsByGroupArtifact; | ||
|
||
public DependencyVersions(Project project) { | ||
this.project = Objects.requireNonNull(project); | ||
|
||
versionsByGroupArtifact = new HashMap<>(); | ||
} | ||
|
||
public DependencyVersions propertiesFile(File propertiesFile) { | ||
Objects.requireNonNull(propertiesFile); | ||
Preconditions.checkArgument(propertiesFile.exists(), | ||
Strings.format("Dependencies properties file %s does not exist", propertiesFile.getAbsolutePath())); | ||
|
||
try { | ||
// Can't load via Java Properties class, as it considers the ':' character the same way as '=' | ||
Map<String, String> dependencies = Files.lines(propertiesFile.toPath()) | ||
.map(String::trim) | ||
.filter(line -> !line.isEmpty()) | ||
.filter(line -> !isCommentLine(line)) | ||
.map(this::parseVersions) | ||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)); | ||
|
||
versionsByGroupArtifact.putAll(dependencies); | ||
} catch (IOException e) { | ||
throw new GradleException( | ||
Strings.format("Error loading properties file %s", propertiesFile.getAbsolutePath())); | ||
} | ||
|
||
project.getPluginManager().withPlugin("java-library", plugin -> { | ||
project.getConfigurations().all(configuration -> { | ||
apply(project.getDependencies().getConstraints(), configuration.getName()); | ||
}); | ||
}); | ||
|
||
project.getPluginManager().withPlugin("java", plugin -> { | ||
project.getConfigurations().all(configuration -> { | ||
apply(project.getDependencies().getConstraints(), configuration.getName()); | ||
}); | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
private void apply(DependencyConstraintHandler constraintHandler, String configuration) { | ||
versionsByGroupArtifact.entrySet().stream() | ||
.map(entry -> entry.getKey() + ":" + entry.getValue()) | ||
.forEach(dependency -> constraintHandler.add(configuration, dependency)); | ||
|
||
versionsByGroupArtifact.entrySet().stream() | ||
.forEach(entry -> project.getLogger().info("Applied {} dependency constraint: {}:{}", | ||
configuration, entry.getKey(), entry.getValue())); | ||
} | ||
|
||
private boolean isCommentLine(String line) { | ||
Objects.requireNonNull(line); | ||
|
||
return line.startsWith("#"); | ||
} | ||
|
||
private Entry<String, String> parseVersions(String line) { | ||
Objects.requireNonNull(line); | ||
|
||
// Handle lines with comments on the end | ||
String propertyLine = line.split("#")[0].trim(); | ||
|
||
String[] parts = propertyLine.split("="); | ||
|
||
Preconditions.checkArgument(parts.length == 2, | ||
Strings.format("Invalid properties line format - expect format 'group:artifact=version' (%s)", line)); | ||
|
||
return new AbstractMap.SimpleEntry<>(parts[0], parts[1]); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/starchartlabs/flare/operations/plugin/DependencyVersionsPlugin.java
This file contains 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,26 @@ | ||
/* | ||
* Copyright (C) 2019 [email protected] Authors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
package org.starchartlabs.flare.operations.plugin; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.starchartlabs.flare.operations.model.DependencyVersions; | ||
|
||
/** | ||
* Configuration plug-in that adds structures for defining dependency constraints | ||
* | ||
* @author romeara | ||
* @since 3.0.0 | ||
*/ | ||
public class DependencyVersionsPlugin implements Plugin<Project> { | ||
|
||
@Override | ||
public void apply(Project project) { | ||
project.getExtensions().add("dependencyVersions", new DependencyVersions(project)); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/gradle-plugins/org.starchartlabs.flare.dependency-versions.properties
This file contains 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=org.starchartlabs.flare.operations.plugin.DependencyVersionsPlugin |
43 changes: 43 additions & 0 deletions
43
...g/starchartlabs/flare/test/operations/plugin/DependencyVersionsPluginIntegrationTest.java
This file contains 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,43 @@ | ||
/* | ||
* Copyright (C) 2019 [email protected] Authors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
package org.starchartlabs.flare.test.operations.plugin; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
|
||
import org.gradle.testkit.runner.BuildResult; | ||
import org.gradle.testkit.runner.GradleRunner; | ||
import org.gradle.testkit.runner.TaskOutcome; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
public class DependencyVersionsPluginIntegrationTest { | ||
|
||
private File testProjectDirectory; | ||
|
||
@BeforeClass | ||
public void setup() throws Exception { | ||
URL directory = this.getClass().getClassLoader() | ||
.getResource("org/starchartlabs/flare/test/dependency/versions"); | ||
|
||
testProjectDirectory = new File(directory.toURI()); | ||
} | ||
|
||
@Test | ||
public void appliesWithoutError() throws Exception { | ||
BuildResult result = GradleRunner.create() | ||
.withPluginClasspath() | ||
.withProjectDir(testProjectDirectory) | ||
.withArguments("clean") | ||
.build(); | ||
|
||
TaskOutcome outcome = result.task(":clean").getOutcome(); | ||
Assert.assertTrue(TaskOutcome.SUCCESS.equals(outcome) || TaskOutcome.UP_TO_DATE.equals(outcome)); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...est/java/org/starchartlabs/flare/test/operations/plugin/DependencyVersionsPluginTest.java
This file contains 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,36 @@ | ||
/* | ||
* Copyright (C) 2019 [email protected] Authors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
package org.starchartlabs.flare.test.operations.plugin; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.testfixtures.ProjectBuilder; | ||
import org.starchartlabs.flare.operations.model.DependencyVersions; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
public class DependencyVersionsPluginTest { | ||
|
||
private static final String PLUGIN_ID = "org.starchartlabs.flare.dependency-versions"; | ||
|
||
private Project project; | ||
|
||
@BeforeClass | ||
public void setupProject() { | ||
project = ProjectBuilder.builder().build(); | ||
project.getPluginManager().apply(PLUGIN_ID); | ||
} | ||
|
||
@Test | ||
public void configurationApplied() throws Exception { | ||
Object found = project.getExtensions().findByName("dependencyVersions"); | ||
|
||
Assert.assertNotNull(found); | ||
Assert.assertTrue(found instanceof DependencyVersions); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/test/resources/org/starchartlabs/flare/test/dependency/versions/build.gradle
This file contains 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,4 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.starchartlabs.flare.dependency-versions' | ||
} |