-
Notifications
You must be signed in to change notification settings - Fork 19
Add Gradle Plugin #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Gradle Plugin #479
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
885c865
Squashed/rebased
jonapoul 7c18b92
No task group
jonapoul ff81bb6
Make task registration non-public
jonapoul a1a3280
Handle user-set source sets
jonapoul 92cbd4f
Update android sdk dir
jonapoul 0ccf30d
Convention extension
jonapoul 9972b58
Task function
jonapoul c15d39c
given -> transform
jonapoul 4928b7d
Shuffle androidHome
jonapoul 1d3d9d8
spotless
jonapoul 9f6be5c
Update
jonapoul 80e2df8
Merge branch 'main' into gradle-plugin
jonapoul 9156a70
More
jonapoul a06c9d0
Update tools/gradle-plugin/src/test/kotlin/io/github/composegears/val…
Goooler 259f6a8
Update tools/gradle-plugin/src/test/kotlin/io/github/composegears/val…
Goooler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 +1,5 @@ | ||
| public final class io/github/composegears/valkyrie/sdk/core/extensions/PathUtilsKt { | ||
| public static final fun writeToKt (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)V | ||
| public static synthetic fun writeToKt$default (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZILjava/lang/Object;)V | ||
| public static final fun writeToKt (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZ)Ljava/nio/file/Path; | ||
| public static synthetic fun writeToKt$default (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZILjava/lang/Object;)Ljava/nio/file/Path; | ||
| } | ||
|
|
This file contains hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| public abstract class io/github/composegears/valkyrie/gradle/GenerateImageVectorsTask : org/gradle/api/DefaultTask { | ||
| public fun <init> ()V | ||
| public final fun execute ()V | ||
| public abstract fun getAddTrailingComma ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getDrawableFiles ()Lorg/gradle/api/file/ConfigurableFileCollection; | ||
| public abstract fun getGeneratePreview ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getIconPackName ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getIndentSize ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getNestedPackName ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getOutputDirectory ()Lorg/gradle/api/file/DirectoryProperty; | ||
| public abstract fun getOutputFormat ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getPackageName ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getPreviewAnnotationType ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getSvgFiles ()Lorg/gradle/api/file/ConfigurableFileCollection; | ||
| public abstract fun getUseComposeColors ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getUseExplicitMode ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getUseFlatPackage ()Lorg/gradle/api/provider/Property; | ||
| } | ||
|
|
||
| public abstract interface class io/github/composegears/valkyrie/gradle/ValkyrieExtension { | ||
| public abstract fun getAddTrailingComma ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getGenerateAtSync ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getGeneratePreview ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getIconPackName ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getIndentSize ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getNestedPackName ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getOutputDirectory ()Lorg/gradle/api/file/DirectoryProperty; | ||
| public abstract fun getOutputFormat ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getPackageName ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getPreviewAnnotationType ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getUseComposeColors ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getUseExplicitMode ()Lorg/gradle/api/provider/Property; | ||
| public abstract fun getUseFlatPackage ()Lorg/gradle/api/provider/Property; | ||
| } | ||
|
|
||
| public final class io/github/composegears/valkyrie/gradle/ValkyrieGradlePlugin : org/gradle/api/Plugin { | ||
| public fun <init> ()V | ||
| public synthetic fun apply (Ljava/lang/Object;)V | ||
| public fun apply (Lorg/gradle/api/Project;)V | ||
| } | ||
jonapoul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
This file contains hidden or 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,109 @@ | ||
| import java.nio.file.Paths | ||
| import java.util.Properties | ||
| import kotlin.io.path.exists | ||
|
|
||
| plugins { | ||
| alias(libs.plugins.kotlin.jvm) | ||
| alias(libs.plugins.valkyrie.kover) | ||
| alias(libs.plugins.valkyrie.abi) | ||
| alias(libs.plugins.buildConfig) | ||
| `java-gradle-plugin` | ||
| } | ||
|
|
||
| tasks.validatePlugins { | ||
| // TODO: https://github.com/gradle/gradle/issues/22600 | ||
| enableStricterValidation = true | ||
Goooler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| gradlePlugin { | ||
| vcsUrl = "https://github.com/ComposeGears/Valkyrie" | ||
| website = "https://github.com/ComposeGears/Valkyrie" | ||
|
|
||
| plugins { | ||
| create("valkyrie") { | ||
| id = "io.github.composegears.valkyrie" | ||
| displayName = name | ||
| implementationClass = "io.github.composegears.valkyrie.gradle.ValkyrieGradlePlugin" | ||
| description = "Generates Kotlin accessors for ImageVectors, based on input SVG files" | ||
| tags.addAll("kotlin", "svg", "xml", "imagevector", "valkyrie") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val sharedTestResourcesDir: File = | ||
| project(projects.components.test.path) | ||
| .layout | ||
| .projectDirectory | ||
| .dir("sharedTestResources/imagevector") | ||
| .asFile | ||
|
|
||
| buildConfig.sourceSets.getByName("test") { | ||
| packageName = "io.github.composegears.valkyrie.gradle" | ||
| useKotlinOutput { topLevelConstants = true } | ||
|
|
||
| // So we can copy the shared test SVG/XML files into our test cases | ||
| buildConfigField("RESOURCES_DIR_SVG", sharedTestResourcesDir.resolve("svg")) | ||
| buildConfigField("RESOURCES_DIR_XML", sharedTestResourcesDir.resolve("xml")) | ||
| buildConfigField<String?>("ANDROID_HOME", androidHome()) | ||
| buildConfigField("COMPOSE_UI", libs.compose.ui.get().toString()) | ||
|
|
||
| // TODO: Set up tests to run for different gradle versions? | ||
| buildConfigField("GRADLE_VERSION", GradleVersion.current().version) | ||
| } | ||
|
|
||
| // Adapted from https://github.com/GradleUp/shadow/blob/1d7b0863fed3126bf376f11d563e9176de176cd3/build.gradle.kts#L63-L65 | ||
| // Allows gradle test cases to use the same classpath as the parent build - meaning we don't need to specify versions | ||
| // when loading plugins into test projects. | ||
| val testPluginClasspath by configurations.registering { | ||
| isCanBeResolved = true | ||
| } | ||
|
|
||
| tasks.pluginUnderTestMetadata { | ||
| // Plugins used in tests could be resolved in classpath. | ||
| pluginClasspath.from(testPluginClasspath) | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly(libs.agp.api) | ||
| compileOnly(libs.kotlin.gradle.plugin) | ||
|
|
||
| api(projects.sdk.core.extensions) | ||
| api(projects.components.generator.iconpack) | ||
| api(projects.components.generator.jvm.imagevector) | ||
| api(projects.components.ir) | ||
| api(projects.components.parser.unified) | ||
|
|
||
| testImplementation(libs.bundles.test) | ||
| testRuntimeOnly(libs.junit.launcher) | ||
|
|
||
| testPluginClasspath(libs.agp.full) | ||
Goooler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| testPluginClasspath(libs.kotlin.gradle.plugin) | ||
| } | ||
|
|
||
| fun androidHome(): String? { | ||
| val androidSdkRoot = System.getenv("ANDROID_SDK_ROOT") | ||
| if (!androidSdkRoot.isNullOrBlank() && Paths.get(androidSdkRoot).exists()) { | ||
| logger.info("Using ANDROID_SDK_ROOT=$androidSdkRoot") | ||
| return androidSdkRoot | ||
| } | ||
|
|
||
| val androidHome = System.getenv("ANDROID_HOME") | ||
| if (!androidHome.isNullOrBlank() && Paths.get(androidHome).exists()) { | ||
| logger.info("Using ANDROID_HOME=$androidHome") | ||
| return androidHome | ||
| } | ||
|
|
||
| val localProps = rootProject.file("local.properties") | ||
| if (localProps.exists()) { | ||
| val properties = Properties() | ||
| localProps.inputStream().use { properties.load(it) } | ||
| val sdkHome = properties.getProperty("sdk.dir")?.takeIf { it.isNotBlank() } | ||
| if (sdkHome != null && Paths.get(sdkHome).exists()) { | ||
| logger.info("Using local.properties sdk.dir $sdkHome") | ||
| return sdkHome | ||
| } | ||
| } | ||
|
|
||
| logger.warn("No Android SDK found - Android unit tests will be skipped") | ||
| return null | ||
| } | ||
112 changes: 112 additions & 0 deletions
112
...plugin/src/main/kotlin/io/github/composegears/valkyrie/gradle/GenerateImageVectorsTask.kt
This file contains hidden or 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,112 @@ | ||
| package io.github.composegears.valkyrie.gradle | ||
|
|
||
| import io.github.composegears.valkyrie.generator.jvm.imagevector.ImageVectorGenerator | ||
| import io.github.composegears.valkyrie.generator.jvm.imagevector.ImageVectorGeneratorConfig | ||
| import io.github.composegears.valkyrie.generator.jvm.imagevector.OutputFormat | ||
| import io.github.composegears.valkyrie.generator.jvm.imagevector.PreviewAnnotationType | ||
| import io.github.composegears.valkyrie.parser.unified.ParserType | ||
| import io.github.composegears.valkyrie.parser.unified.SvgXmlParser | ||
| import io.github.composegears.valkyrie.parser.unified.ext.toIOPath | ||
| import io.github.composegears.valkyrie.sdk.core.extensions.writeToKt | ||
| import kotlinx.io.files.Path | ||
| import org.gradle.api.DefaultTask | ||
| import org.gradle.api.GradleException | ||
| import org.gradle.api.file.ConfigurableFileCollection | ||
| import org.gradle.api.file.DirectoryProperty | ||
| import org.gradle.api.provider.Property | ||
| import org.gradle.api.tasks.CacheableTask | ||
| import org.gradle.api.tasks.Input | ||
| import org.gradle.api.tasks.InputFiles | ||
| import org.gradle.api.tasks.Optional | ||
| import org.gradle.api.tasks.OutputDirectory | ||
| import org.gradle.api.tasks.PathSensitive | ||
| import org.gradle.api.tasks.PathSensitivity.RELATIVE | ||
| import org.gradle.api.tasks.TaskAction | ||
|
|
||
| @CacheableTask | ||
| abstract class GenerateImageVectorsTask : DefaultTask() { | ||
| @get:[PathSensitive(RELATIVE) InputFiles] abstract val svgFiles: ConfigurableFileCollection | ||
|
|
||
| @get:[PathSensitive(RELATIVE) InputFiles] abstract val drawableFiles: ConfigurableFileCollection | ||
|
|
||
| @get:Input abstract val packageName: Property<String> | ||
|
|
||
| @get:[Input Optional] abstract val iconPackName: Property<String> | ||
|
|
||
| @get:[Input Optional] abstract val nestedPackName: Property<String> | ||
|
|
||
| @get:Input abstract val outputFormat: Property<OutputFormat> | ||
|
|
||
| @get:Input abstract val useComposeColors: Property<Boolean> | ||
|
|
||
| @get:Input abstract val generatePreview: Property<Boolean> | ||
|
|
||
| @get:Input abstract val previewAnnotationType: Property<PreviewAnnotationType> | ||
|
|
||
| @get:Input abstract val useFlatPackage: Property<Boolean> | ||
|
|
||
| @get:Input abstract val useExplicitMode: Property<Boolean> | ||
|
|
||
| @get:Input abstract val addTrailingComma: Property<Boolean> | ||
|
|
||
| @get:Input abstract val indentSize: Property<Int> | ||
|
|
||
| @get:OutputDirectory abstract val outputDirectory: DirectoryProperty | ||
|
|
||
| @TaskAction | ||
| fun execute() { | ||
| val packageName = packageName.orNull | ||
| ?: throw GradleException("No package name configured for $this") | ||
|
|
||
| // e.g. "<project-root>/build/generated/sources/valkyrie/main" | ||
| val outputDirectory = outputDirectory.get().asFile | ||
| outputDirectory.deleteRecursively() // make sure nothing is left over from previous run | ||
| outputDirectory.mkdirs() | ||
|
|
||
jonapoul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val generatedFiles = arrayListOf<Path>() | ||
jonapoul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var fileIndex = 0 | ||
|
|
||
| val useFlatPackage = useFlatPackage.get() | ||
| val nestedPackName = nestedPackName.getOrElse("") | ||
| val config = ImageVectorGeneratorConfig( | ||
| packageName = packageName, | ||
| iconPackPackage = packageName, | ||
| packName = iconPackName.getOrElse(""), | ||
| nestedPackName = nestedPackName, | ||
| outputFormat = outputFormat.get(), | ||
| useComposeColors = useComposeColors.get(), | ||
| generatePreview = generatePreview.get(), | ||
| previewAnnotationType = previewAnnotationType.get(), | ||
| useFlatPackage = useFlatPackage, | ||
| useExplicitMode = useExplicitMode.get(), | ||
| addTrailingComma = addTrailingComma.get(), | ||
| indentSize = indentSize.get(), | ||
| ) | ||
|
|
||
| (svgFiles + drawableFiles).files.forEach { file -> | ||
| val parseOutput = SvgXmlParser.toIrImageVector(ParserType.Jvm, Path(file.absolutePath)) | ||
| val vectorSpecOutput = ImageVectorGenerator.convert( | ||
| vector = parseOutput.irImageVector, | ||
| iconName = parseOutput.iconName, | ||
| config = config, | ||
| ) | ||
|
|
||
| val path = vectorSpecOutput.content.writeToKt( | ||
| outputDir = when { | ||
| useFlatPackage -> outputDirectory | ||
| else -> outputDirectory.resolve(nestedPackName.lowercase()) | ||
| }.absolutePath, | ||
| nameWithoutExtension = vectorSpecOutput.name, | ||
| ) | ||
| generatedFiles.add(path.toIOPath()) | ||
| fileIndex++ | ||
| logger.info("File $fileIndex = $path") | ||
| } | ||
|
|
||
| logger.lifecycle("Generated ${generatedFiles.size} ImageVectors in package $packageName") | ||
| } | ||
|
|
||
| internal companion object { | ||
| internal const val TASK_NAME = "generateImageVectors" | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.