Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #147 from zoonooz/support-new-configuration
Browse files Browse the repository at this point in the history
Support new dependency configuration
  • Loading branch information
ataulm authored Nov 28, 2017
2 parents d658567 + b582c33 commit 7f55424
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.novoda.bintray-release'
apply plugin: 'groovy'
apply plugin: 'java-gradle-plugin'
apply plugin: 'maven'

buildscript {
Expand All @@ -18,7 +19,6 @@ repositories {
jcenter()
}


dependencies {
compile gradleApi()
compile localGroovy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AndroidArtifacts implements Artifacts {
}

def from(Project project) {
project.components.add(AndroidLibrary.newInstance(project))
project.components.add(new AndroidLibrary(project))
project.components.android
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.novoda.gradle.release

import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.api.UnknownDomainObjectException
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.PublishArtifact
Expand All @@ -10,63 +11,72 @@ import org.gradle.api.internal.DefaultDomainObjectSet
import org.gradle.api.internal.component.SoftwareComponentInternal
import org.gradle.api.internal.component.UsageContext
import org.gradle.api.model.ObjectFactory
import org.gradle.util.GradleVersion

class AndroidLibrary implements SoftwareComponentInternal {

private final UsageContext runtimeUsage
private final String CONF_COMPILE = "compile"
private final String CONF_API = "api"
private final String CONF_IMPLEMENTATION = "implementation"

public static AndroidLibrary newInstance(Project project) {
private final Set<UsageContext> usages = new DefaultDomainObjectSet<UsageContext>(UsageContext)

ObjectFactory objectFactory = project.getObjects();
Usage usage = objectFactory.named(Usage.class, Usage.JAVA_RUNTIME);
AndroidLibrary(Project project) {
ObjectFactory objectFactory = project.getObjects()

// Using the new Usage in 4.1 will make the plugin crash
// as comparing logic is still using the old Usage.
// For more details: https://github.com/novoda/bintray-release/pull/147
def isNewerThan4_1 = GradleVersion.current() > GradleVersion.version("4.1")
Usage api = objectFactory.named(Usage.class, isNewerThan4_1 ? Usage.JAVA_API : "for compile")
Usage runtime = objectFactory.named(Usage.class, isNewerThan4_1 ? Usage.JAVA_RUNTIME : "for runtime")

def configuration = project.configurations.getByName("compile")
return configuration ? from(configuration, usage) : empty()
addUsageContextFromConfiguration(project, CONF_COMPILE, api)
addUsageContextFromConfiguration(project, CONF_API, api)
addUsageContextFromConfiguration(project, CONF_IMPLEMENTATION, runtime)
}

static AndroidLibrary from(def configuration, Usage usage) {
def runtimeUsage = new RuntimeUsage(configuration.dependencies, usage)
new AndroidLibrary(runtimeUsage)
}

static AndroidLibrary empty() {
def usage = new RuntimeUsage(new DefaultDomainObjectSet<Dependency>(Dependency))
new AndroidLibrary(usage)
}

AndroidLibrary(UsageContext runtimeUsage) {
this.runtimeUsage = runtimeUsage
String getName() {
return "android"
}

public String getName() {
return "android"
Set<UsageContext> getUsages() {
return usages
}

public Set<UsageContext> getUsages() {
return Collections.singleton(runtimeUsage);
private addUsageContextFromConfiguration(Project project, String configuration, Usage usage) {
try {
def configurationObj = project.configurations.getByName(configuration)
def dependency = configurationObj.dependencies
if (!dependency.isEmpty()) {
def libraryUsage = new LibraryUsage(dependency, usage)
usages.add(libraryUsage)
}
} catch (UnknownDomainObjectException ignore) {
// cannot find configuration
}
}

private static class RuntimeUsage implements UsageContext {
private static class LibraryUsage implements UsageContext {

private final DomainObjectSet<Dependency> runtimeDependencies
private final Usage usage;
private final DomainObjectSet<Dependency> dependencies
private final Usage usage

RuntimeUsage(DomainObjectSet<Dependency> runtimeDependencies, Usage usage) {
this.usage = usage;
this.runtimeDependencies = runtimeDependencies
LibraryUsage(DomainObjectSet<Dependency> dependencies, Usage usage) {
this.usage = usage
this.dependencies = dependencies
}

Usage getUsage() {
return usage;
return usage
}

public Set<PublishArtifact> getArtifacts() {
Set<PublishArtifact> getArtifacts() {
new LinkedHashSet<PublishArtifact>()
}

public Set<ModuleDependency> getDependencies() {
runtimeDependencies.withType(ModuleDependency)
Set<ModuleDependency> getDependencies() {
dependencies.withType(ModuleDependency)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.novoda.gradle.release

import org.gradle.testkit.runner.GradleRunner
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

class TestGeneratePomTask {

@Rule public TemporaryFolder testProjectDir = new TemporaryFolder()
private File buildFile

@Before
void setup() {
buildFile = testProjectDir.newFile('build.gradle')
}

@Test
void testGeneratePomTaskForJavaLib() {
buildFile << """
plugins {
id 'java-library'
id 'bintray-release'
}
publish {
userOrg = 'novoda'
groupId = 'com.novoda'
artifactId = 'test'
publishVersion = '1.0'
}
dependencies {
compile 'com.abc:hello:1.0.0'
implementation 'com.xyz:world:2.0.0'
api 'com.xxx:haha:3.0.0'
}
"""

def result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments("generatePomFileForMavenPublication")
.withPluginClasspath()
.build()

assert result.task(":generatePomFileForMavenPublication").outcome == SUCCESS

File pomFile = new File(testProjectDir.root, '/build/publications/maven/pom-default.xml')
def nodes = new XmlSlurper().parse(pomFile)
def dependencies = nodes.dependencies.dependency

assert dependencies.size() == 3
assert dependencies.find { dep -> dep.artifactId == "hello" && dep.scope == "compile" } != null
assert dependencies.find { dep -> dep.artifactId == "haha" && dep.scope == "compile" } != null
assert dependencies.find { dep -> dep.artifactId == "world" && dep.scope == "runtime" } != null
}

@Test
void testGeneratePomTaskForAndroidLibrary() {
File manifestFile = new File(testProjectDir.root, "/src/main/AndroidManifest.xml")
manifestFile.getParentFile().mkdirs()
manifestFile.createNewFile()
manifestFile << """
<manifest package="com.novoda.test"/>
"""

buildFile << """
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
plugins {
id 'bintray-release' apply false
}
apply plugin: "com.android.library"
apply plugin: "bintray-release"
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
minSdkVersion 16
versionCode 1
versionName "0.0.1"
}
}
publish {
userOrg = 'novoda'
groupId = 'com.novoda'
artifactId = 'test'
publishVersion = '1.0'
}
dependencies {
compile 'com.abc:hello:1.0.0'
implementation 'com.xyz:world:2.0.0'
api 'com.xxx:haha:3.0.0'
}
"""

def result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments("generatePomFileForReleasePublication")
.withPluginClasspath()
.build()

assert result.task(":generatePomFileForReleasePublication").outcome == SUCCESS

File pomFile = new File(testProjectDir.root, '/build/publications/release/pom-default.xml')
def nodes = new XmlSlurper().parse(pomFile)
def dependencies = nodes.dependencies.dependency

assert dependencies.size() == 3
assert dependencies.find { dep -> dep.artifactId == "hello" && dep.scope == "compile" } != null
assert dependencies.find { dep -> dep.artifactId == "haha" && dep.scope == "compile" } != null
assert dependencies.find { dep -> dep.artifactId == "world" && dep.scope == "runtime" } != null
}
}

0 comments on commit 7f55424

Please sign in to comment.