Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Filter .asmdef and .asmdef.meta from Paket.Unity cleanup (#46)
Browse files Browse the repository at this point in the history
Description
===========

Unity 2018 adds more control to the script compilation
process with [assembly definition files]. If a user wants to define a
definition file for the dependencies inside the `Paket.Unity3D` directory
she has to recreate it after each install process because the logic in
the `net.wooga.paket-unity` plugin takes ownership of this directory and
might clean it completely. This patch adds a custom pattern to keep all
`.asmdef` and `asmdef.meta` files in the directory structure (at root
and deep nested).

We see this as a middleground solution for projects that want to use
[assembly definition files] or opt out fromi using them.

Changes
=======

![IMPROVE] output cleanup filters out `.asmdef` and `.asmdef.meta`

[assembly definition files]:    https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html
  • Loading branch information
Larusso authored Nov 27, 2018
1 parent 2c3a2bc commit f95a8f8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,73 @@ class PaketUnityChangeSpec extends IntegrationSpec {
out2.exists()
}

@Unroll
def "task :paketInstall keeps files with #filePattern in #location paket install directory"() {
given:
buildFile << """
${applyPlugin(PaketGetPlugin)}
""".stripIndent()

and: "paket dependency file"
createDependencies(project3References)
createFile("packages/${project3References[0]}/content/ContentFile.cs")
createFile("packages/${project3References[1]}/content/ContentFile.cs")

def paketDir = new File(projectDir, "${project3Name}/Assets/${DefaultPaketUnityPluginExtension.DEFAULT_PAKET_DIRECTORY}")

and: "unity project #unityProjectName with references #projectReferences"
createOrUpdateReferenceFile(project1Name, project3References)

and: "a file matching the file pattern"
def baseDir = (location == "root") ? paketDir : new File(paketDir, "some/nested/directory")
baseDir.mkdirs()
def fileToKeep = createFile("test${filePattern}", baseDir) << "random content"

when:
runTasksSuccessfully(PaketUnityPlugin.INSTALL_TASK_NAME)

then:
fileToKeep.exists()

where:
filePattern | location
".asmdef" | "root"
".asmdef" | "nested"
".asmdef.meta" | "root"
".asmdef.meta" | "nested"
}

def "task :paketInstall deletes empty directories"() {
given:
buildFile << """
${applyPlugin(PaketGetPlugin)}
""".stripIndent()

and: "paket dependency file"
createDependencies(project3References)
def dep1 = createFile("packages/${project3References[0]}/content/ContentFile.cs")
def dep2 = createFile("packages/${project3References[1]}/content/ContentFile.cs")

def paketDir = new File(projectDir, "${project3Name}/Assets/${DefaultPaketUnityPluginExtension.DEFAULT_PAKET_DIRECTORY}")

and: "unity project #unityProjectName with references #projectReferences"
createOrUpdateReferenceFile(project1Name, project3References)

and: "some empty directories in output directory"
def rootDir = new File(paketDir, "dirAtRoot")
def secondLevel = new File(rootDir, "dirAtSecondLevel")
def thirdLevel = new File(secondLevel, "dirAtThirdLevel")
thirdLevel.mkdirs()

when:
runTasksSuccessfully(PaketUnityPlugin.INSTALL_TASK_NAME)

then:
!rootDir.exists()
!secondLevel.exists()
!thirdLevel.exists()
}

def containsHasChangedOrDeletedOutput(String stdOut, String filePath) {
containsHasChangedOutput(stdOut, filePath) || containsHasRemovedOutput(stdOut, filePath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ package wooga.gradle.paket.unity.tasks
import org.apache.commons.io.FileUtils
import org.gradle.api.Action
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTreeElement
import org.gradle.api.file.FileVisitDetails
import org.gradle.api.file.FileVisitor
import org.gradle.api.internal.ConventionTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
Expand Down Expand Up @@ -131,9 +134,7 @@ class PaketUnityInstall extends ConventionTask {

if (!inputs.incremental) {
if (getOutputDirectory().exists()) {
getOutputDirectory().deleteDir()
logger.info("delete target directory: ${getOutputDirectory()}")
assert !getOutputDirectory().exists()
cleanOutputDirectory()
}
}

Expand Down Expand Up @@ -168,6 +169,33 @@ class PaketUnityInstall extends ConventionTask {
})
}

protected void cleanOutputDirectory() {
def tree = project.fileTree(getOutputDirectory()) {
exclude("**/*.asmdef")
exclude("**/*.asmdef.meta")
}

logger.info("delete files in directory: ${getOutputDirectory()}")
project.delete(tree)

def emptyDirs = []
project.fileTree(getOutputDirectory()).visit(new FileVisitor() {
@Override
void visitDir(FileVisitDetails dirDetails) {
File f = dirDetails.file
def children = project.fileTree(f).filter { it.isFile() }.files
if (children.size() == 0) {
emptyDirs << f
}
}

@Override
void visitFile(FileVisitDetails fileDetails) {
}
})
emptyDirs.reverseEach { it.delete() }
}

private File transformInputToOutputPath(File inputFile, File baseDirectory) {
def relativePath = baseDirectory.toURI().relativize(inputFile.toURI()).getPath()
def pathSegments = relativePath.split("/").toList()
Expand Down

0 comments on commit f95a8f8

Please sign in to comment.