From daac1045c6b12d57aae5964d1931cc837a8ddd2c Mon Sep 17 00:00:00 2001 From: Christian Sagel Date: Mon, 4 Jul 2022 13:05:06 +0200 Subject: [PATCH] Fix the predicate for checking the package directory on generate upm packages (#159) --- ...nerateUpmPackageTaskIntegrationSpec.groovy | 21 ++++---- .../unity/tasks/GenerateUpmPackage.groovy | 48 ++++++++++++------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/integrationTest/groovy/wooga/gradle/unity/tasks/GenerateUpmPackageTaskIntegrationSpec.groovy b/src/integrationTest/groovy/wooga/gradle/unity/tasks/GenerateUpmPackageTaskIntegrationSpec.groovy index 3e3e2cf..29f7696 100644 --- a/src/integrationTest/groovy/wooga/gradle/unity/tasks/GenerateUpmPackageTaskIntegrationSpec.groovy +++ b/src/integrationTest/groovy/wooga/gradle/unity/tasks/GenerateUpmPackageTaskIntegrationSpec.groovy @@ -130,13 +130,13 @@ class GenerateUpmPackageTaskIntegrationSpec extends UnityIntegrationSpec { and: def generateUpmPackageTaskName = "upmPack" List taskStatements = new ArrayList() - if (failure != GenerateUpmPackage.Failure.packageDirectoryNotSet) { + if (predicate != GenerateUpmPackage.Message.packageDirectoryNotSet) { taskStatements.add("packageDirectory.set(${wrapValueBasedOnType(projectPath, Directory)})") } - if (failure != GenerateUpmPackage.Failure.versionNotSet) { + if (predicate != GenerateUpmPackage.Message.versionNotSet) { taskStatements.add("archiveVersion.set(${wrapValueBasedOnType(packageVersion, String)})") } - if (failure != GenerateUpmPackage.Failure.packageNameNotSet) { + if (predicate != GenerateUpmPackage.Message.packageNameNotSet) { taskStatements.add("packageName = ${wrapValueBasedOnType(packageName, String)}") } @@ -146,14 +146,17 @@ class GenerateUpmPackageTaskIntegrationSpec extends UnityIntegrationSpec { def result = runTasks(generateUpmPackageTaskName) then: - result.failure || result.wasSkipped(generateUpmPackageTaskName) + // TODO: Utility method to check either skip reason (NO-SOURCES, etc) + result.wasSkipped(generateUpmPackageTaskName) || outputContains(result, "${generateUpmPackageTaskName} NO-SOURCE") outputContains(result, reason) where: - packageName | packageVersion | failure - "com.wooga.foobar" | "0.0.1" | GenerateUpmPackage.Failure.packageDirectoryNotSet - "com.wooga.foobar" | "0.0.1" | GenerateUpmPackage.Failure.versionNotSet - "com.wooga.foobar" | "0.0.1" | GenerateUpmPackage.Failure.packageNameNotSet - reason = failure.message + packageName | packageVersion | predicate + "com.wooga.foobar" | "0.0.1" | GenerateUpmPackage.Message.packageDirectoryNotSet + "com.wooga.foobar" | "0.0.1" | GenerateUpmPackage.Message.versionNotSet + "com.wooga.foobar" | "0.0.1" | GenerateUpmPackage.Message.packageNameNotSet + reason = predicate.message } + + } diff --git a/src/main/groovy/wooga/gradle/unity/tasks/GenerateUpmPackage.groovy b/src/main/groovy/wooga/gradle/unity/tasks/GenerateUpmPackage.groovy index e7b8ee9..7c328ac 100644 --- a/src/main/groovy/wooga/gradle/unity/tasks/GenerateUpmPackage.groovy +++ b/src/main/groovy/wooga/gradle/unity/tasks/GenerateUpmPackage.groovy @@ -2,9 +2,9 @@ package wooga.gradle.unity.tasks import com.wooga.gradle.BaseSpec import groovy.json.JsonSlurper -import groovyjarjarcommonscli.MissingArgumentException import org.gradle.api.file.Directory import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.FileCollection import org.gradle.api.file.RegularFile import org.gradle.api.provider.Property import org.gradle.api.provider.Provider @@ -12,6 +12,7 @@ import org.gradle.api.specs.Spec import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputDirectory import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.bundling.Compression import org.gradle.api.tasks.bundling.Tar @@ -21,13 +22,11 @@ import org.gradle.api.tasks.bundling.Tar */ class GenerateUpmPackage extends Tar implements BaseSpec { - enum Failure { + enum Message { packageDirectoryNotSet("No package directory was set"), packageManifestFileNotFound("No package manifest file (package.json) was found"), packageNameNotSet("No package name was set"), versionNotSet("No version was set for the package by the archive") - // TODO: Add if the gradle error for it isnt enough - //packageManifestNotFound("No package manifest (package.json) was found in the package directory"), String message @@ -35,7 +34,7 @@ class GenerateUpmPackage extends Tar implements BaseSpec { message } - Failure(String message) { + Message(String message) { this.message = message } } @@ -43,7 +42,6 @@ class GenerateUpmPackage extends Tar implements BaseSpec { /** * @return The directory where the package source files are located */ - @SkipWhenEmpty @InputDirectory DirectoryProperty getPackageDirectory() { packageDirectory @@ -59,6 +57,15 @@ class GenerateUpmPackage extends Tar implements BaseSpec { packageDirectory.set(value) } + @SkipWhenEmpty + @InputFiles + FileCollection getPackageFiles() { + if (packageDirectory.present) { + return project.fileTree(packageDirectory) + } + project.files() + } + /** * @return The package manifest file, `package.json`, which defines the package dependencies and other metadata. */ @@ -90,24 +97,25 @@ class GenerateUpmPackage extends Tar implements BaseSpec { public static final packageManifestFileName = "package.json" + @Override + protected void copy() { + if (!packageDirectory.present) { + logger.warn(Message.packageDirectoryNotSet.message) + } + from(packageDirectory) + super.copy() + } GenerateUpmPackage() { setCompression(Compression.GZIP) - // Gathers the sources of the package. (We have to wrap it like this in case package directory is never set) - from(providers.provider({ - if (!packageDirectory.present) { - throw new MissingArgumentException(Failure.packageDirectoryNotSet.message) - } - packageDirectory - })) // Creates a root directory inside the package into("package") // The name of the package, in reverse domain name notation Provider packageNameOnFile = packageManifestFile.map({ def slurper = new JsonSlurper() - if (!it.asFile.exists()){ + if (!it.asFile.exists()) { return null } slurper.parse(it.asFile)["name"].toString() @@ -122,16 +130,20 @@ class GenerateUpmPackage extends Tar implements BaseSpec { onlyIf(new Spec() { @Override boolean isSatisfiedBy(GenerateUpmPackage t) { + if (!packageDirectory.present) { + logger.warn(Message.packageDirectoryNotSet.message) + return false + } if (!t.archiveVersion.present) { - logger.error(Failure.versionNotSet.message) + logger.warn(Message.versionNotSet.message) return false } - if (t.packageManifestFile.present && !t.packageManifestFile.get().asFile.exists()){ - logger.error(Failure.packageManifestFileNotFound.message) + if (t.packageManifestFile.present && !t.packageManifestFile.get().asFile.exists()) { + logger.warn(Message.packageManifestFileNotFound.message) return false } if (!t.packageName.present || t.packageName.get().empty) { - logger.error(Failure.packageNameNotSet.message) + logger.warn(Message.packageNameNotSet.message) return false } true