Skip to content

Commit

Permalink
Fix the predicate for checking the package directory on generate upm …
Browse files Browse the repository at this point in the history
…packages (#159)
  • Loading branch information
Azurelol authored Jul 4, 2022
1 parent e55ad9c commit daac104
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ class GenerateUpmPackageTaskIntegrationSpec extends UnityIntegrationSpec {
and:
def generateUpmPackageTaskName = "upmPack"
List<String> taskStatements = new ArrayList<String>()
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)}")
}

Expand All @@ -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
}


}
48 changes: 30 additions & 18 deletions src/main/groovy/wooga/gradle/unity/tasks/GenerateUpmPackage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ 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
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
Expand All @@ -21,29 +22,26 @@ 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

String getMessage() {
message
}

Failure(String message) {
Message(String message) {
this.message = message
}
}

/**
* @return The directory where the package source files are located
*/
@SkipWhenEmpty
@InputDirectory
DirectoryProperty getPackageDirectory() {
packageDirectory
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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<String> packageNameOnFile = packageManifestFile.map({
def slurper = new JsonSlurper()
if (!it.asFile.exists()){
if (!it.asFile.exists()) {
return null
}
slurper.parse(it.asFile)["name"].toString()
Expand All @@ -122,16 +130,20 @@ class GenerateUpmPackage extends Tar implements BaseSpec {
onlyIf(new Spec<GenerateUpmPackage>() {
@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
Expand Down

0 comments on commit daac104

Please sign in to comment.