Skip to content

Commit

Permalink
Fix lazy project version
Browse files Browse the repository at this point in the history
The project version had to be set before the plugin gets applied.
This commit will bind the version property lazy. The version gets
evaluated at execution time.
  • Loading branch information
Larusso committed Nov 2, 2017
1 parent 4ea78e4 commit 7e88251
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.gradle
build/
out/

# Ignore Gradle GUI config
gradle-app.setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@ class PaketPackIntegrationSpec extends PaketIntegrationDependencyFileSpec {
taskToRun << ["paketPack-WoogaTest", "buildNupkg", "assemble"]
}

@Unroll
def "make version parameter lazy #taskToRun"(String taskToRun) {
given: "a build file with version set after the plugin"
buildFile.text = ""
buildFile << """
group = 'test'
${applyPlugin(PaketPackPlugin)}
version = "$version"
""".stripIndent()

and: "a future output file"
def outputFile = new File(new File(new File(projectDir, 'build'), "outputs"), "${packageID}.${version}.nupkg")
assert !outputFile.exists()

and: "a empty paket.dependencies file"
createFile("paket.dependencies")

when:
def result = runTasksSuccessfully(taskToRun)

then:
outputFile.exists()
result.wasExecuted("paketPack-WoogaTest")

where:
taskToRun << ["paketPack-WoogaTest", "buildNupkg", "assemble"]
}

def "skips pack task creation for duplicate package id"() {
given: "some paket template files in the file system with same id"
def subDir1 = new File(projectDir, "sub1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ abstract class AbstractPaketTask<T extends AbstractPaketTask> extends Convention
def stdErr = new ByteArrayOutputStream()

@Internal
PaketPluginExtension paketExtension
private PaketPluginExtension paketExtension

PaketPluginExtension getPaketExtension() {
return paketExtension
}

void setPaketExtension(PaketPluginExtension value) {
paketExtension = value
}

AbstractPaketTask(Class<T> taskType) {
super()
Expand Down Expand Up @@ -92,7 +100,7 @@ abstract class AbstractPaketTask<T extends AbstractPaketTask> extends Convention

String getExecutable() {
if (executable == null) {
executable = paketExtension.paketExecuteablePath
executable = getPaketExtension().paketExecuteablePath
}

if (executable == null) {
Expand Down Expand Up @@ -128,7 +136,7 @@ abstract class AbstractPaketTask<T extends AbstractPaketTask> extends Convention

if (!osName.contains("windows")) {
logger.info("Use mono: {}.", true)
paketArgs << paketExtension.monoExecutable
paketArgs << getPaketExtension().monoExecutable
}

paketArgs << project.file(getExecutable())
Expand Down
28 changes: 21 additions & 7 deletions src/main/groovy/wooga/gradle/paket/pack/PaketPackPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

package wooga.gradle.paket.pack

import org.gradle.api.Action
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.ConventionMapping
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.TaskContainer
import wooga.gradle.paket.base.DefaultPaketPluginExtension
import wooga.gradle.paket.base.PaketBasePlugin
import wooga.gradle.paket.base.PaketPluginExtension
import wooga.gradle.paket.get.PaketGetPlugin
import wooga.gradle.paket.pack.tasks.PaketPack

Expand Down Expand Up @@ -85,20 +88,31 @@ class PaketPackPlugin implements Plugin<Project> {
}

packTask = tasks.create(taskName, PaketPack.class)

packTask.group = BasePlugin.BUILD_GROUP
packTask.templateFile = file
packTask.outputDir = project.file("${project.buildDir}/outputs")
packTask.version = project.version
packTask.description = "Pack package ${templateReader.getPackageId()}"
packTask.paketExtension = extension
packTask.templateFile = file
packTask.packageId = packageID

tasks[BasePlugin.ASSEMBLE_TASK_NAME].dependsOn packTask

project.artifacts.add(PaketBasePlugin.PAKET_CONFIGURATION, [file: packTask.outputFile, name: packageID, builtBy: packTask])
}

configurePaketInstallIfPresent()
configurePaketPackDefaults(extension)
}

private void configurePaketPackDefaults(PaketPluginExtension extention) {
tasks.withType(PaketPack, new Action<PaketPack>() {
@Override
void execute(PaketPack task) {
ConventionMapping taskConventionMapping = task.getConventionMapping()

taskConventionMapping.map("templateFile", { extention.getBaseUrl() })
taskConventionMapping.map("outputDir", { project.file("${project.buildDir}/outputs") })
taskConventionMapping.map("version", { project.version })
taskConventionMapping.map("paketExtension", { extention })
project.artifacts.add(PaketBasePlugin.PAKET_CONFIGURATION, [file: task.outputFile, name: task.packageId, builtBy: task])
}
})
}

void configurePaketInstallIfPresent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class PaketPack extends AbstractPaketTask {

static String COMMAND = "pack"

@Internal
def packageId

@Optional
@Input
def version
Expand Down Expand Up @@ -57,7 +60,7 @@ class PaketPack extends AbstractPaketTask {
File getOutputFile() {
def templateReader = new PaketTemplateReader(getTemplateFile())
def packageID = templateReader.getPackageId()
project.file({"$outputDir/${packageID}.${version}.nupkg"})
project.file({"$outputDir/${packageID}.${getVersion()}.nupkg"})
}

PaketPack() {
Expand Down

0 comments on commit 7e88251

Please sign in to comment.