From 5a107bcb768fa1e7fb110ba65e22e49b3b004eed Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 2 Jul 2019 23:22:11 -0700 Subject: Convert install task to kotlin using delegation --- buildSrc/src/main/kotlin/FrostPlugin.kt | 6 +- buildSrc/src/main/kotlin/WebGenInstallTask.groovy | 55 ------------------ buildSrc/src/main/kotlin/WebGenInstallTask.kt | 68 +++++++++++++++++++++++ 3 files changed, 70 insertions(+), 59 deletions(-) delete mode 100644 buildSrc/src/main/kotlin/WebGenInstallTask.groovy create mode 100644 buildSrc/src/main/kotlin/WebGenInstallTask.kt (limited to 'buildSrc/src') diff --git a/buildSrc/src/main/kotlin/FrostPlugin.kt b/buildSrc/src/main/kotlin/FrostPlugin.kt index 8e640f04..0fd4ea9d 100644 --- a/buildSrc/src/main/kotlin/FrostPlugin.kt +++ b/buildSrc/src/main/kotlin/FrostPlugin.kt @@ -17,16 +17,14 @@ class FrostPlugin : Plugin { } project.gradle.taskGraph.whenReady { if (!project.plugins.hasPlugin("java-base")) { - throw IllegalArgumentException( - "Frost plugin can't be applied without Android or Java or Kotlin plugin." - ) + throw IllegalArgumentException("Frost plugin can't be applied without Android or Java or Kotlin plugin.") } } } private fun Project.applyWebGenPlugin() { setupNode() -// tasks.create(WebGenInstallTask.NAME, WebGenInstallTask::class) + tasks.create(WebGenInstallTask.NAME, WebGenInstallTask::class) } private fun Project.setupNode() { diff --git a/buildSrc/src/main/kotlin/WebGenInstallTask.groovy b/buildSrc/src/main/kotlin/WebGenInstallTask.groovy deleted file mode 100644 index a70effee..00000000 --- a/buildSrc/src/main/kotlin/WebGenInstallTask.groovy +++ /dev/null @@ -1,55 +0,0 @@ -import com.moowork.gradle.node.npm.NpmTask -import org.gradle.api.file.DirectoryProperty -import org.gradle.api.file.RegularFileProperty -import org.gradle.api.model.ObjectFactory -import org.gradle.api.tasks.OutputDirectory -import org.gradle.api.tasks.OutputFile - -/** - * Based on https://github.com/apollographql/apollo-android/blob/master/apollo-gradle-plugin/src/main/groovy/com/apollographql/apollo/gradle/ApolloCodegenInstallTask.groovy - */ -class WebGenInstallTask extends NpmTask { - - static final String TAG = "frost-web-gen" - public static final String NAME = "installWebGen" - static final String INSTALLATION_PATH = TAG + File.separator + "node_modules" - static final String PACKAGE_FILE_PATH = TAG + File.separator + "package.json" - - static final String TYPESCRIPT_VERSION = "3.3.1" - static final String SASS_VERSION = "1.19.0" - - @OutputDirectory - final DirectoryProperty installDir = ObjectFactory.directoryProperty() - @OutputFile - final RegularFileProperty packageFile = ObjectFactory.fileProperty() - - WebGenInstallTask() { - setGroup("frost") - setDescription("Runs npm install for $TAG") - installDir.set(project.file(new File(project.buildDir, INSTALLATION_PATH))) - packageFile.set(project.file(new File(project.buildDir, PACKAGE_FILE_PATH))) - } - - @Override - void exec() { - installDir.get().getAsFile().deleteDir() - writePackageFile(packageFile.get().getAsFile()) - - setArgs(Lists.newArrayList("install", "typescript@" + TYPESCRIPT_VERSION, "sass@" + SASS_VERSION, "--save", "--save-exact")) - getLogging().captureStandardOutput(LogLevel.INFO) - - super.exec() - } - - /** - * Generates a dummy package.json file to silence npm warnings - */ - private static void writePackageFile(File apolloPackageFile) { - apolloPackageFile.write( - '''{ - "name": "frost-web-gen" -} -''' - ) - } -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/WebGenInstallTask.kt b/buildSrc/src/main/kotlin/WebGenInstallTask.kt new file mode 100644 index 00000000..2076ee84 --- /dev/null +++ b/buildSrc/src/main/kotlin/WebGenInstallTask.kt @@ -0,0 +1,68 @@ +import com.moowork.gradle.node.npm.NpmTask +import org.gradle.api.DefaultTask +import org.gradle.api.logging.LogLevel +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction +import java.io.File + +/** + * Based on https://github.com/apollographql/apollo-android/blob/master/apollo-gradle-plugin/src/main/groovy/com/apollographql/apollo/gradle/ApolloCodegenInstallTask.groovy + */ +class WebGenInstallTask : DefaultTask() { + + companion object { + const val TAG = "frost-web-gen" + const val NAME = "installWebGen" + val INSTALLATION_PATH = TAG + File.separator + "node_modules" + val PACKAGE_FILE_PATH = TAG + File.separator + "package.json" + + const val TYPESCRIPT_VERSION = "3.3.1" + const val SASS_VERSION = "1.19.0" + } + + @OutputDirectory + val installDir = project.layout.directoryProperty() + @OutputFile + val packageFile = project.layout.fileProperty() + + val npmTask = NpmTask() + + init { + group = "frost" + description = "Runs npm install for $TAG" + + installDir.set(project.file(File(project.buildDir, INSTALLATION_PATH))) + packageFile.set(project.file(File(project.buildDir, PACKAGE_FILE_PATH))) + + npmTask.setWorkingDir(File(project.buildDir, TAG)) + } + + @TaskAction + fun exec() { + installDir.get().asFile.takeIf { it.isDirectory }?.deleteRecursively() + writePackageFile(packageFile.get().asFile) + npmTask.setArgs( + listOf( + "install", + "typescript@$TYPESCRIPT_VERSION", + "sass@$SASS_VERSION", + "--save", + "--save-exact" + ) + ) + npmTask.logging.captureStandardOutput(LogLevel.INFO) + npmTask.exec() + } + + private fun writePackageFile(packageFile: File) { + packageFile.writeText( + """ + { + "name": "$TAG", + "version": "1.0" + } + """.trimIndent() + ) + } +} \ No newline at end of file -- cgit v1.2.3