-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generateSolution supports unity >= 2022 (#196)
## Description FIx the `generateSolution` task to work for unity 2022 and above, due to `"UnityEditor.SyncVS.SyncSolution"` not being available anymore on those versions. We now put a script inside of the unity project and run a method in it to generate the solution. It is more complex, but its the more stable solution as it doesn't depend on the project having a specific **active** external editor. However, we still need to have **some** external editor UPM package, so we now install the `com.unity.ide.rider` upm package if its not present. To run the script we now have a task to execute scripts in a unity project through a task (RunCSScript). ## Changes * ![FIX] fix `generateSolution` not working out of the box on unity 2022 and above * ![ADD] `RunCSScript` task type * ![ADD] installs `com.unity.ide.rider` upm package [NEW]: https://resources.atlas.wooga.com/icons/icon_new.svg "New" [ADD]: https://resources.atlas.wooga.com/icons/icon_add.svg "Add" [IMPROVE]: https://resources.atlas.wooga.com/icons/icon_improve.svg "Improve" [CHANGE]: https://resources.atlas.wooga.com/icons/icon_change.svg "Change" [FIX]: https://resources.atlas.wooga.com/icons/icon_fix.svg "Fix" [UPDATE]: https://resources.atlas.wooga.com/icons/icon_update.svg "Update" [BREAK]: https://resources.atlas.wooga.com/icons/icon_break.svg "Remove" [REMOVE]: https://resources.atlas.wooga.com/icons/icon_remove.svg "Remove" [IOS]: https://resources.atlas.wooga.com/icons/icon_iOS.svg "iOS" [ANDROID]: https://resources.atlas.wooga.com/icons/icon_android.svg "Android" [WEBGL]: https://resources.atlas.wooga.com/icons/icon_webGL.svg "WebGL" [GRADLE]: https://resources.atlas.wooga.com/icons/icon_gradle.svg "GRADLE" [UNITY]: https://resources.atlas.wooga.com/icons/icon_unity.svg "Unity" [LINUX]: https://resources.atlas.wooga.com/icons/icon_linux.svg "Linux" [WIN]: https://resources.atlas.wooga.com/icons/icon_windows.svg "Windows" [MACOS]: https://resources.atlas.wooga.com/icons/icon_iOS.svg "macOS"
- Loading branch information
1 parent
77b5628
commit 0b2b025
Showing
10 changed files
with
324 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/groovy/wooga/gradle/unity/tasks/ExecuteCsharpScript.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package wooga.gradle.unity.tasks | ||
|
||
import org.gradle.api.file.RegularFile | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputFile | ||
import wooga.gradle.unity.UnityTask | ||
|
||
import java.nio.file.Files | ||
|
||
/** | ||
* Copies a script from {@code sourceScript} into a temporary {@code destinationScript} then executes Unity with {@code executeMethod} | ||
*/ | ||
class ExecuteCsharpScript extends UnityTask { | ||
|
||
private final RegularFileProperty sourceScript = objects.fileProperty() | ||
|
||
@InputFile | ||
RegularFileProperty getSourceScript() { | ||
return sourceScript | ||
} | ||
|
||
void setSourceScript(Provider<RegularFile> sourceCsScript) { | ||
this.sourceScript.set(sourceCsScript) | ||
} | ||
|
||
void setSourceScript(RegularFile sourceCsScript) { | ||
this.sourceScript.set(sourceCsScript) | ||
} | ||
|
||
void setSourceScript(File sourceCsScript) { | ||
this.sourceScript.set(sourceCsScript) | ||
} | ||
|
||
private final RegularFileProperty destinationScript = objects.fileProperty() | ||
|
||
@InputFile | ||
RegularFileProperty getDestinationScript() { | ||
return destinationScript | ||
} | ||
|
||
void setDestinationScript(Provider<RegularFile> destCsScript) { | ||
this.destinationScript.set(destCsScript) | ||
} | ||
|
||
void setDestinationScript(RegularFile destCsScript) { | ||
this.destinationScript.set(destCsScript) | ||
} | ||
|
||
void setDestinationScript(File destCsScript) { | ||
this.destinationScript.set(destCsScript) | ||
} | ||
|
||
@Override //this input is mandatory for this task, so overriding the previous annotation. | ||
@Input | ||
Property<String> getExecuteMethod() { | ||
return super.getExecuteMethod() | ||
} | ||
|
||
ExecuteCsharpScript() { | ||
finalizedBy(project.tasks.register("_${this.name}_cleanup") { | ||
onlyIf { | ||
destinationScript.present && destinationScript.get().asFile.file | ||
} | ||
doLast { | ||
def baseFile = destinationScript.get().asFile | ||
def metafile = new File(baseFile.absolutePath + ".meta") | ||
baseFile.delete() | ||
metafile.delete() | ||
} | ||
}) | ||
} | ||
|
||
@Override | ||
protected void preExecute() { | ||
Files.copy(sourceScript.get().asFile.toPath(), destinationScript.get().asFile.toPath()) | ||
destinationScript.asFile.get().deleteOnExit() | ||
} | ||
} |
50 changes: 47 additions & 3 deletions
50
src/main/groovy/wooga/gradle/unity/tasks/GenerateSolution.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,55 @@ | ||
package wooga.gradle.unity.tasks | ||
|
||
import wooga.gradle.unity.UnityTask | ||
import org.gradle.api.file.Directory | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.tasks.InputDirectory | ||
import org.gradle.api.tasks.Optional | ||
|
||
class GenerateSolution extends UnityTask { | ||
class GenerateSolution extends ExecuteCsharpScript { | ||
private final DirectoryProperty assetsDir = objects.directoryProperty() | ||
|
||
@InputDirectory | ||
@Optional | ||
DirectoryProperty getAssetsDir() { | ||
return assetsDir | ||
} | ||
|
||
void setAssetsDir(Provider<Directory> assetsDir) { | ||
this.assetsDir.set(assetsDir) | ||
} | ||
|
||
void setAssetsDir(Directory assetsDir) { | ||
this.assetsDir.set(assetsDir) | ||
} | ||
|
||
void setAssetsDir(File assetsDir) { | ||
this.assetsDir.set(assetsDir) | ||
} | ||
|
||
GenerateSolution() { | ||
outputs.upToDateWhen { false } | ||
executeMethod = "UnityEditor.SyncVS.SyncSolution" | ||
|
||
// Not the usual approach we take. Usually we would like to put the default in the plugin conventions and such, but this | ||
// task seems to be designed to be independent from plugin configuration, so we still apply plugin configuration, | ||
// but we set __sensible defaults__. | ||
// This runs before the config block in the plugin, so it can and will be overwritten by plugin configuration when the plugin is applied as well | ||
this.assetsDir.convention(this.projectDirectory.dir("Assets").map {it.asFile.mkdirs();return it}) | ||
|
||
def defaultScript = this.assetsDir | ||
.map { it.file("SolutionGenerator.cs") } | ||
.map { script -> | ||
script.asFile.text = GenerateSolution.classLoader.getResourceAsStream("DefaultSolutionGenerator.cs").text | ||
script.asFile.deleteOnExit() | ||
return script | ||
} | ||
this.sourceScript.convention(defaultScript) | ||
this.destinationScript.convention(this.sourceScript) | ||
this.executeMethod.set(project.provider { | ||
unityVersion.majorVersion >= 2022? | ||
"Wooga.UnityPlugin.DefaultSolutionGenerator.GenerateSolution" : | ||
"UnityEditor.SyncVS.SyncSolution" | ||
}) | ||
|
||
} | ||
} |
Oops, something went wrong.