-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(multi-os-engine/multi-os-engine#162) Make reflection collector a sep…
…arated task
- Loading branch information
Showing
5 changed files
with
195 additions
and
87 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
179 changes: 179 additions & 0 deletions
179
src/main/kotlin/org/moe/gradle/tasks/ReflectionCollect.kt
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,179 @@ | ||
package org.moe.gradle.tasks | ||
|
||
import com.dd.plist.NSDictionary | ||
import com.dd.plist.NSString | ||
import com.dd.plist.PropertyListParser | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputFiles | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.SourceSet | ||
import org.moe.generator.project.writer.XcodeEditor | ||
import org.moe.gradle.MoePlugin | ||
import org.moe.gradle.anns.IgnoreUnused | ||
import org.moe.gradle.anns.NotNull | ||
import org.moe.gradle.anns.Nullable | ||
import org.moe.gradle.options.ProGuardOptions | ||
import org.moe.gradle.utils.FileUtils | ||
import org.moe.gradle.utils.Mode | ||
import org.moe.tools.classvalidator.ReflectionCollector | ||
import java.io.File | ||
import java.nio.file.Paths | ||
|
||
open class ReflectionCollect : AbstractBaseTask() { | ||
|
||
private var inputFiles: Set<Any>? = null | ||
|
||
@InputFiles | ||
@NotNull | ||
fun getInputFiles(): ConfigurableFileCollection { | ||
return project.files(getOrConvention(inputFiles, CONVENTION_INPUT_FILES)) | ||
} | ||
|
||
@IgnoreUnused | ||
fun setInputFiles(inputFiles: Collection<Any>?) { | ||
this.inputFiles = inputFiles?.toSet() | ||
} | ||
|
||
private var classpathFiles: Set<Any>? = null | ||
|
||
@InputFiles | ||
@NotNull | ||
fun getClasspathFiles(): ConfigurableFileCollection { | ||
return project.files(getOrConvention(classpathFiles, CONVENTION_CLASSPATH_FILES)) | ||
} | ||
|
||
@IgnoreUnused | ||
fun setClasspathFiles(classpathFiles: Collection<Any>?) { | ||
this.classpathFiles = classpathFiles?.toSet() | ||
} | ||
|
||
private var outputDir: File? = null | ||
|
||
@Input | ||
@NotNull | ||
fun getOutputDir(): File { | ||
return project.file(getOrConvention(outputDir, CONVENTION_OUTPUT_DIR)) | ||
} | ||
|
||
@IgnoreUnused | ||
fun setOutputDir(outputDir: File) { | ||
this.outputDir = outputDir | ||
} | ||
|
||
private var appMainClassName: String? = null | ||
|
||
@Input | ||
@Nullable | ||
fun getAppMainClassName(): String? { | ||
return getOrConvention(appMainClassName, CONVENTION_APP_MAIN_CLASS_NAME) | ||
} | ||
|
||
@IgnoreUnused | ||
fun setAppMainClassName(appMainClassName: String?) { | ||
this.appMainClassName = appMainClassName | ||
} | ||
|
||
val reflectionConfigFile: File | ||
@OutputFile | ||
get() = getOutputDir().resolve(ReflectionCollector.OUTPUT_REFLECTION) | ||
|
||
override fun run() { | ||
// Delete output file | ||
FileUtils.deleteFileOrFolder(reflectionConfigFile) | ||
|
||
// Collect platform reflection settings | ||
// TODO: Move this to a separate task | ||
// val libraryJars: MutableSet<File> = linkedSetOf(moeSDK.coreJar) | ||
// moeExtension.platformJar?.let { | ||
// libraryJars.add(it) | ||
// } | ||
when (moeExtension.proguard.levelRaw) { | ||
ProGuardOptions.LEVEL_ALL -> { | ||
// Nothing to be done here, should be handled by [ClassValidator] above | ||
} | ||
else -> { | ||
throw GradleException("Only proguardLevel 'all' is supported atm.") | ||
} | ||
} | ||
// when (moeExtension.proguardLevelRaw) { | ||
// MoeExtension.PROGUARD_LEVEL_APP -> { | ||
//// libraryJars.add(moeSDK.coreJar) | ||
// moeExtension.platformJar?.let { | ||
// libraryJars.add(it) | ||
// } | ||
// } | ||
// MoeExtension.PROGUARD_LEVEL_PLATFORM -> { | ||
//// libraryJars.add(moeSDK.coreJar) | ||
// } | ||
// } | ||
ReflectionCollector.process( | ||
mainClassName = getAppMainClassName(), | ||
inputFiles = getInputFiles().toSet(), | ||
outputDir = getOutputDir().absoluteFile.toPath(), | ||
classpath = getClasspathFiles().toSet() | ||
// Add input to classpath | ||
+ getInputFiles().toSet(), | ||
) | ||
} | ||
|
||
@get:Internal | ||
lateinit var classValidateTaskDep: ClassValidate | ||
private set | ||
|
||
protected fun setupMoeTask( | ||
@NotNull sourceSet: SourceSet, | ||
@NotNull mode: Mode, | ||
) { | ||
setSupportsRemoteBuild(false) | ||
|
||
// Construct default output path | ||
val out = Paths.get(MoePlugin.MOE, sourceSet.name, "reflection", mode.name) | ||
|
||
description = "Collect reflection config (sourceset: ${sourceSet.name}, mode: ${mode.name})." | ||
|
||
// Add dependencies | ||
val classValidateTask = moePlugin.getTaskBy(ClassValidate::class.java, sourceSet, mode) | ||
classValidateTaskDep = classValidateTask | ||
dependsOn(classValidateTask) | ||
|
||
// Update convention mapping | ||
addConvention(CONVENTION_INPUT_FILES) { setOf(classValidateTask.classesOutputDir) } | ||
addConvention(CONVENTION_CLASSPATH_FILES) { setOf(classValidateTask.getClasspathFiles()) } | ||
addConvention(CONVENTION_OUTPUT_DIR) { resolvePathInBuildDir(out) } | ||
addConvention(CONVENTION_APP_MAIN_CLASS_NAME) { | ||
try { | ||
// Figure out the app main class from Info.plist | ||
val xcode = moeExtension.xcode | ||
val target = if (SourceSet.MAIN_SOURCE_SET_NAME == sourceSet.name) { | ||
xcode.mainTarget | ||
} else { | ||
xcode.testTarget | ||
} | ||
target?.let { | ||
val xcodeFile = project.file(xcode.project) | ||
val xcodeEditor = XcodeEditor(xcodeFile) | ||
val infoPlistFile = xcodeEditor.getInfoPlist(target, mode.xcodeCompatibleName) | ||
|
||
val rootDict = PropertyListParser.parse(infoPlistFile.readBytes()) as NSDictionary | ||
val mainClass = rootDict["MOE.Main.Class"] as NSString | ||
|
||
mainClass.content | ||
} | ||
} catch (e: Exception) { | ||
project.logger.warn("Unable to get the MOE.Main.Class from Info.plist, app may not work properly", e) | ||
null | ||
} | ||
} | ||
addConvention(CONVENTION_LOG_FILE) { resolvePathInBuildDir(out, "ReflectionCollect.log") } | ||
} | ||
|
||
companion object { | ||
private const val CONVENTION_INPUT_FILES = "inputFiles" | ||
private const val CONVENTION_CLASSPATH_FILES = "classpathFiles" | ||
private const val CONVENTION_OUTPUT_DIR = "outputDir" | ||
private const val CONVENTION_APP_MAIN_CLASS_NAME = "appMainClassName" | ||
} | ||
} |