Skip to content
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

Pre release/0.3.0 #21

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
## boring-plugins
[![](https://jitpack.io/v/foodiestudio/boring-plugins.svg)](https://jitpack.io/#foodiestudio/boring-plugins)
## convention-plugins
[![](https://jitpack.io/v/foodiestudio/convention-plugins.svg)](https://jitpack.io/#foodiestudio/convention-plugins)

存放一些自定义 gradle plugin。

> 主要使用 kts 来实现,然后实现类可以直接用 Kotlin 来写,主要是这样能有语法高亮。

## Compatible Specification

| AGP Version | Latest Support Version |
|:-----------:|:--------------------------------------------------------------------------:|
| 7.4.x | [0.3.x](https://github.com/foodiestudio/convention-plugins/releases/tag/0.3.0) |
| 8.0.x | todo |

### Quick Start

- register plugin
Expand All @@ -17,8 +24,8 @@
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "rust-desktop") {
useModule("com.github.foodiestudio:boring-plugins:$version")
if (requested.id.id.startsWith("foodiestudio")) {
useModule("com.github.foodiestudio:convention-plugins:0.3.0-beta1")
}
}
}
Expand All @@ -27,9 +34,28 @@
- use plugin in module
```kotlin
plugins {
id("rust-desktop") // for example
id("foodiestudio.android.application.compose") // for example
}
```

#### android
The top-level build file

```kotlin
plugins {
id("foodiestudio.android.application.compose") apply false
id("foodiestudio.android.library.compose") apply false
}
```

The module-level build file

```kotlin
plugins {
id("foodiestudio.android.application.compose")
// or id("foodiestudio.android.library.compose") for library module
}
```

#### rust-desktop
```kotlin
Expand Down
45 changes: 34 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,46 +1,69 @@
plugins {
`kotlin-dsl`
`maven-publish`
id("com.github.gmazzo.buildconfig") version "4.1.2"
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlin {
jvmToolchain(11)
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-Xallow-result-return-type"
jvmTarget = JavaVersion.VERSION_11.toString()
}
}


buildConfig {
buildConfigField("String", "COMPOSE_COMPILER_VERSION", "\"${sharedLibs.versions.compose.compiler.get()}\"")
buildConfigField("String", "COMPOSE_BOM", provider { "\"${sharedLibs.compose.bom.get()}\"" })
}

dependencies {
// kotlin 版本由 gradle 里的版本决定,升级 gradle 会升级对应的 kotlin 依赖
implementation(kotlin("stdlib"))
implementation("com.larksuite.oapi:oapi-sdk:2.0.24")
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.9.2")
compileOnly("com.larksuite.oapi:oapi-sdk:2.0.24")
compileOnly("com.github.doyaaaaaken:kotlin-csv-jvm:1.9.2")
// 作为 api的依赖方式,这样外面可以不用声明 AGP 的依赖
implementation(sharedLibs.android.gradlePlugin)
implementation(sharedLibs.kotlin.gradlePlugin)
}

group = "com.github.foodiestudio"
version = "0.2.1"
version = "0.3.0"

// 为 buildSrc 里的 Plugin 创建对应的 id,这样才能在 plugins{} 中使用
gradlePlugin {
plugins {
create("androidApplicationCompose") {
id = "foodiestudio.android.application.compose"
implementationClass = "com.github.foodiestudio.plugin.android.AndroidApplicationComposePlugin"
}
create("androidApplication") {
id = "foodiestudio.android.application"
implementationClass = "com.github.foodiestudio.plugin.android.AndroidApplicationPlugin"
}
create("androidLibrary") {
id = "foodiestudio.android.library"
implementationClass = "com.github.foodiestudio.plugin.android.AndroidLibraryPlugin"
}
create("androidLibraryCompose") {
id = "foodiestudio.android.library.compose"
implementationClass = "com.github.foodiestudio.plugin.android.AndroidLibraryComposePlugin"
}
// ================
create("rustDesktop") {
id = "rust-desktop"
id = "foodiestudio.rust.desktop"
implementationClass = "com.github.foodiestudio.plugin.rust.CargoDesktopPlugin"
}
create("larkSheet") {
id = "lark-sheet"
id = "foodiestudio.lark.sheet"
implementationClass = "com.github.foodiestudio.plugin.lark.LarkSheetPlugin"
}
create("githubPublish") {
id = "github-publish"
id = "foodiestudio.github.publish"
implementationClass = "com.github.foodiestudio.plugin.github.GithubPublishPlugin"
}
}
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
org.gradle.caching=true
org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8
org.gradle.parallel=true
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon May 29 10:34:03 CST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
7 changes: 7 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ dependencyResolutionManagement {
mavenCentral()
maven("https://jitpack.io")
}
versionCatalogs {
create("sharedLibs") {
from("io.github.foodiestudio:libs-versions:2023.04.00")
library("android-gradlePlugin", "com.android.tools.build", "gradle").versionRef("agp")
library("kotlin-gradlePlugin", "org.jetbrains.kotlin", "kotlin-gradle-plugin").versionRef("kotlin")
}
}
}
rootProject.name = "boring-plugins"
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@file:Suppress("UnstableApiUsage")

package com.github.foodiestudio.plugin.android

import com.android.build.api.dsl.ApplicationExtension
import com.github.foodiestudio.plugin.android.internal.configureAndroidCompose
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getByType

/**
* 带 compose 的 android application plugin
*/
class AndroidApplicationComposePlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
pluginManager.apply(AndroidApplicationPlugin::class)

val extension = extensions.getByType<ApplicationExtension>()
configureAndroidCompose(extension)
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.github.foodiestudio.plugin.android

import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.CommonExtension
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.github.foodiestudio.plugin.android.internal.DefaultSdkVersions
import com.github.foodiestudio.plugin.android.internal.configureGradleManagedDevices
import com.github.foodiestudio.plugin.android.internal.configurePrintApksTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

class AndroidApplicationPlugin : Plugin<Project> {

override fun apply(target: Project) {
with(target) {
with(pluginManager) {
// 这里不需要指定 plugin 的版本
apply("com.android.application")
apply("org.jetbrains.kotlin.android")
}

extensions.configure<ApplicationExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = DefaultSdkVersions.targetSdk
configureGradleManagedDevices(this)
}
extensions.configure<ApplicationAndroidComponentsExtension> {
configurePrintApksTask(this)
}
}
}
}

/**
* Configure base Kotlin with Android options
*/
internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *>
) {
commonExtension.apply {
compileSdk = DefaultSdkVersions.compileSdk

defaultConfig {
minSdk = DefaultSdkVersions.minSdk
}

compileOptions {
// Up to Java 11 APIs are available through desugaring
// https://developer.android.com/studio/write/java11-minimal-support-table
sourceCompatibility = DefaultSdkVersions.java
targetCompatibility = DefaultSdkVersions.java
isCoreLibraryDesugaringEnabled = true
}
}

configureKotlin()

dependencies {
add("coreLibraryDesugaring", "com.android.tools:desugar_jdk_libs:2.0.3")
}
}

/**
* Configure base Kotlin options for JVM (non-Android)
*/
internal fun Project.configureKotlinJvm() {
extensions.configure<JavaPluginExtension> {
// Up to Java 11 APIs are available through desugaring
// https://developer.android.com/studio/write/java11-minimal-support-table
sourceCompatibility = DefaultSdkVersions.java
targetCompatibility = DefaultSdkVersions.java
}

configureKotlin()
}

/**
* Configure base Kotlin options
*/
private fun Project.configureKotlin() {
// Use withType to workaround https://youtrack.jetbrains.com/issue/KT-55947
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
// Set JVM target to 11
jvmTarget = DefaultSdkVersions.java.toString()
// Treat all Kotlin warnings as errors (disabled by default)
// Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
val warningsAsErrors: String? by project
allWarningsAsErrors = warningsAsErrors.toBoolean()
freeCompilerArgs = freeCompilerArgs + listOf(
"-opt-in=kotlin.RequiresOptIn",
// Enable experimental coroutines APIs, including Flow
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=kotlinx.coroutines.FlowPreview",
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@file:Suppress("UnstableApiUsage")

package com.github.foodiestudio.plugin.android

import com.android.build.api.dsl.LibraryExtension
import com.github.foodiestudio.plugin.android.internal.configureAndroidCompose
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getByType

class AndroidLibraryComposePlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
pluginManager.apply(AndroidLibraryPlugin::class)

val extension = extensions.getByType<LibraryExtension>()
configureAndroidCompose(extension)
extension.buildFeatures.buildConfig = false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.foodiestudio.plugin.android

import org.gradle.api.Plugin
import org.gradle.api.Project
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.gradle.LibraryExtension
import com.github.foodiestudio.plugin.android.internal.DefaultSdkVersions
import com.github.foodiestudio.plugin.android.internal.configureGradleManagedDevices
import com.github.foodiestudio.plugin.android.internal.configurePrintApksTask
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin

@Suppress("UnstableApiUsage")
class AndroidLibraryPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.android.library")
apply("org.jetbrains.kotlin.android")
}

extensions.configure<LibraryExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = DefaultSdkVersions.targetSdk
buildFeatures.buildConfig = false
configureGradleManagedDevices(this)
}
extensions.configure<LibraryAndroidComponentsExtension> {
configurePrintApksTask(this)
}
dependencies {
add("testImplementation", kotlin("test"))
add("androidTestImplementation", kotlin("test"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.foodiestudio.plugin.android.internal

import org.gradle.api.JavaVersion

object DefaultSdkVersions {

val java = JavaVersion.VERSION_11

const val minSdk: Int = 21

const val compileSdk: Int = 33

const val targetSdk: Int = 33
}
Loading