forked from Splitties/refreshVersions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BuildSrcVersions{Task,Plugin} & clean output Splitties#26
- Loading branch information
Showing
11 changed files
with
261 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
include::doc/includes.adoc[] | ||
|
||
:toc: right | ||
|
||
|
||
= buildSrcVersions | ||
|
||
{badge_mit} {badge_version} {badge_issues} {badge_pr} {badge_build} | ||
|
||
This Gradle plugin adds a **:buildSrcVersions** task to your build that | ||
|
||
- Extract automatically all your Gradle dependencies to {root}/buildSrc/src/main/kotlin/Versions.kt[buildSrc/src/main/kotlin/Versions.kt]. | ||
- Inform you about available updates inside {root}/buildSrc/src/main/kotlin/Versions.kt[buildSrc/src/main/kotlin/Versions.kt]. | ||
|
||
|
||
|
||
|
||
|
||
|
||
== Extract your dependencies | ||
|
||
|
||
```bash | ||
$ ./gradlew buildSrcVersions | ||
> Task :dependencyUpdates | ||
> Task :buildSrcVersions | ||
new file: buildSrc/build.gradle.kts | ||
new file: buildSrc/.gitignore | ||
new file: buildSrc/settings.gradle.kts | ||
new file: buildSrc/src/main/kotlin/Libs.kt | ||
new file: buildSrc/src/main/kotlin/Versions.kt | ||
``` | ||
|
||
|
||
== IDE integration | ||
|
||
TODO: screencast | ||
|
||
|
||
|
||
```kotlin | ||
dependencies { | ||
implementation(Libs.okio) | ||
implementation(Libs.com_squareup_moshi_moshi) | ||
implementation(Libs.retrofit) | ||
} | ||
``` | ||
|
||
|
||
|
||
== Update your dependencies | ||
|
||
```bash | ||
$ ./gradlew buildSrcVersions | ||
> Task :dependencyUpdates | ||
> Task :buildSrcVersions | ||
modified: buildSrc/src/main/kotlin/Libs.kt | ||
modified: buildSrc/src/main/kotlin/Versions.kt | ||
|
||
``` | ||
|
||
TODO: screencast | ||
|
||
== Prerequisites | ||
|
||
- Kotlin, JVM and Android projects. | ||
- Gradle 4.1+ and Gradle 5.0+ | ||
- The IDE integration works: | ||
- for both `build.gradle` (Groovy) and `build.gradle.kts` (Kotlin) files. | ||
- in IntelliJ >= 2008.03+ and Android Studio 3.2+. | ||
|
||
== Installation | ||
|
||
Edit your root `build.gradle(.kts)` file | ||
|
||
[source,kotlin,subs=attributes] | ||
---- | ||
buildscript { | ||
//... | ||
} | ||
plugins { | ||
id("jmfayard.github.io.gradle-kotlin-dsl-libs") version "{plugin_version}" | ||
} | ||
// Don't put any code before the buildscripts {} and plugins {} block | ||
---- | ||
|
||
|
||
== Changelog | ||
|
||
See https://github.com/jmfayard/gradle-kotlin-dsl-libs/blob/master/CHANGELOG.md[CHANGELOG.md] | ||
|
||
//== Contributors | ||
// | ||
//include::doc/contributors.adoc[] | ||
|
||
== Contributing | ||
|
||
- This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details | ||
- Explain your use case and start the discussion before your submit a pull-request | ||
- Run the automated tests with | ||
- Format your code | ||
|
||
Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us. | ||
|
||
|
||
== Acknowledgments | ||
|
||
This project sits on the shoulder of giants: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
https://plugins.gradle.org/plugin/de.fayard.buildSrcVersions | ||
|
||
:plugin_version: 0.3 | ||
:repo: jmfayard/gradle-kotlin-dsl-libs | ||
:github: https://github.com/{repo} | ||
:plugin_issues: https://github.com/gradle/kotlin-dsl/issues?utf8=%E2%9C%93&q=author%3Ajmfayard+ | ||
:plugin_gradle_portal: https://plugins.gradle.org/plugin/jmfayard.github.io.gradle-kotlin-dsl-libs | ||
:gradle_kotlin_dsl: https://github.com/gradle/kotlin-dsl | ||
:gradle_guide_new: https://guides.gradle.org/creating-new-gradle-builds | ||
:gradle_guide_buidllogic: https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/ | ||
:medium_puzzle: https://blog.kotlin-academy.com/gradle-kotlin-the-missing-piece-of-the-puzzle-7528a85f0d2c | ||
:medium_protips: https://proandroiddev.com/android-studio-pro-tips-for-working-with-gradle-8a7aa61a8cc4 | ||
:medium_mvp: https://dev.to/jmfayard/the-minimum-viable-pull-request-5e7p | ||
:devto_readme: https://dev.to/jmfayard/how-to-write-a-good-readme-discuss-4hkl | ||
:root: file:https:///Users/jmfayard/Dev/mautinoa/gradle-kotlin-dsl-libs | ||
|
||
// todo | ||
:badge_mit: image:https://img.shields.io/github/license/mashape/apistatus.svg["GitHub",link="{github}/blob/master/LICENSE.txt"] | ||
|
||
:badge_version: image:https://img.shields.io/maven-metadata/v/https/plugins.gradle.org/m2/jmfayard/github/io/gradle-kotlin-dsl-libs/maven-metadata.xml.svg?label=gradlePluginPortal["Maven Central",link="https://plugins.gradle.org/plugin/jmfayard.github.io.gradle-kotlin-dsl-libs"] | ||
|
||
:badge_issues: image:https://img.shields.io/github/issues/{repo}.svg["GitHub issues",link="{github}/issues"] | ||
|
||
:badge_pr: image:https://img.shields.io/github/issues-pr/{repo}.svg["GitHub pull requests",link="{github}/pulls?utf8=%E2%9C%93&q=is%3Apr+"] | ||
|
||
:badge_build: image:https://img.shields.io/travis/com/{repo}/documentation.svg["Travis (.org)",link="https://travis-ci.com/{repo}"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package de.fayard | ||
|
||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.Moshi | ||
import okio.buffer | ||
import okio.source | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.Project | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.File | ||
|
||
open class BuildSrcVersionsTask : DefaultTask() { | ||
|
||
init { | ||
description = "Update buildSrc/src/main/kotlin/Libs.kt" | ||
group = "build" | ||
} | ||
|
||
companion object { | ||
val moshiAdapter: JsonAdapter<DependencyGraph> by lazy { | ||
Moshi.Builder().build().adapter(DependencyGraph::class.java) | ||
} | ||
|
||
fun readGraphFromJsonFile(jsonInput: File): DependencyGraph { | ||
return moshiAdapter.fromJson(jsonInput.source().buffer())!! | ||
} | ||
|
||
} | ||
|
||
|
||
var jsonInputPath = "build/dependencyUpdates/report.json" | ||
|
||
|
||
@TaskAction | ||
fun taskAction() { | ||
val jsonInput = project.file(jsonInputPath) | ||
val outputDir = project.file(OutputFile.OUTPUTDIR.path).also { | ||
if (!it.isDirectory) it.mkdirs() | ||
} | ||
|
||
checkIfFilesExistInitially(project) | ||
|
||
val initializationMap = mapOf( | ||
OutputFile.BUILD to INITIAL_BUILD_GRADLE_KTS, | ||
OutputFile.GIT_IGNORE to INITIAL_GITIGNORE, | ||
OutputFile.SETTINGS to INITIAL_SETTINGS) | ||
|
||
for ((outputFile, initialContent) in initializationMap) { | ||
if (outputFile.existed.not()) { | ||
project.file(outputFile.path).writeText(initialContent) | ||
outputFile.logFileWasModified() | ||
} | ||
} | ||
|
||
val dependencyGraph = readGraphFromJsonFile(jsonInput) | ||
|
||
val dependencies: List<Dependency> = parseGraph(dependencyGraph) | ||
|
||
val kotlinPoetry: KotlinPoetry = kotlinpoet(dependencies, dependencyGraph.gradle) | ||
|
||
kotlinPoetry.Libs.writeTo(outputDir) | ||
OutputFile.LIBS.logFileWasModified() | ||
|
||
kotlinPoetry.Versions.writeTo(outputDir) | ||
OutputFile.VERSIONS.logFileWasModified() | ||
} | ||
|
||
fun checkIfFilesExistInitially(project: Project) { | ||
for (output in OutputFile.values()) { | ||
output.existed = project.file(output.path).exists() | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
internal enum class OutputFile(val path: String, var existed: Boolean = false) { | ||
OUTPUTDIR("buildSrc/src/main/kotlin"), | ||
BUILD("buildSrc/build.gradle.kts"), | ||
SETTINGS("buildSrc/settings.gradle.kts"), | ||
GIT_IGNORE("buildSrc/.gitignore"), | ||
LIBS("buildSrc/src/main/kotlin/Libs.kt"), | ||
VERSIONS("buildSrc/src/main/kotlin/Versions.kt"); | ||
|
||
fun logFileWasModified() { | ||
val ANSI_RESET = "\u001B[0m" | ||
val ANSI_GREEN = "\u001B[32m" | ||
|
||
val status = if (existed) { | ||
" modified: " | ||
} else { | ||
" new file: " | ||
} | ||
println("$ANSI_GREEN$status$path$ANSI_RESET") | ||
} | ||
} | ||
|
Oops, something went wrong.