Skip to content

Commit

Permalink
build: add buildSrc
Browse files Browse the repository at this point in the history
  • Loading branch information
xfqwdsj committed Feb 18, 2024
1 parent 596c498 commit f6f5a92
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 143 deletions.
151 changes: 57 additions & 94 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
* with Fhraise. If not, see <https://www.gnu.org/licenses/>.
*/

import org.gradle.internal.os.OperatingSystem
import xyz.xfqlittlefan.fhraise.buildsrc.*
import java.io.ByteArrayOutputStream
import java.util.*

plugins {
// this is necessary to avoid the plugins to be loaded multiple times
// in each subproject's classloader
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.kotlinJvm) apply false
alias(libs.plugins.kotlinSerialization) apply false
Expand All @@ -41,37 +38,7 @@ allprojects {
apply(plugin = "kotlinx-atomicfu")
}

val versionPropertiesFile = file("version.properties")
val versionProperties = Properties().apply {
with(versionPropertiesFile) {
if (exists()) {
load(inputStream())
}
}
}

var devVer: String by versionProperties
var commitSha: String by versionProperties
var reversion: String by versionProperties
var version: String by versionProperties

val buildNumberPropertiesFile = file("build-number.properties")
val buildNumberProperties = Properties().apply {
with(buildNumberPropertiesFile) {
if (exists()) {
load(inputStream())
}
}
}

var buildNumber: String by buildNumberProperties

val String.outputDirectory
get() = rootProject.layout.buildDirectory.dir("outputs/binaries/$version.$buildNumber/$this")

val isLinux = OperatingSystem.current().isLinux
val isWindows = OperatingSystem.current().isWindows
val arch: String = System.getProperty("os.arch")
RootProject.project = rootProject

tasks.register("updateDevVer") {
group = "versioning"
Expand All @@ -84,9 +51,8 @@ tasks.register("updateDevVer") {
standardOutput = stream
isIgnoreExitValue = true
}
devVer = if (result.exitValue == 0) stream.toString().trim().substringAfter("dev") else "0.0"
versionProperties.store(versionPropertiesFile.outputStream(), null)
logger.lifecycle("devVer: $devVer")
projectDevVer = if (result.exitValue == 0) stream.toString().trim().substringAfter("dev") else "0.0"
logger.lifecycle("devVer: $projectDevVer")
}
}

Expand All @@ -100,9 +66,8 @@ tasks.register("updateCommitSha") {
commandLine = listOf("git", "rev-parse", "--short", "HEAD")
standardOutput = stream
}
commitSha = stream.toString().trim()
versionProperties.store(versionPropertiesFile.outputStream(), null)
logger.lifecycle("commitSha: $commitSha")
projectCommitSha = stream.toString().trim()
logger.lifecycle("commitSha: $projectCommitSha")
}
}

Expand All @@ -113,13 +78,12 @@ tasks.register("updateReversion") {
doLast {
val stream = ByteArrayOutputStream()
val result = exec {
commandLine = listOf("git", "rev-list", "--count", "dev$devVer..main")
commandLine = listOf("git", "rev-list", "--count", "dev$projectDevVer..main")
standardOutput = stream
isIgnoreExitValue = true
}
reversion = if (result.exitValue == 0) stream.toString().trim() else "0"
versionProperties.store(versionPropertiesFile.outputStream(), null)
logger.lifecycle("reversion: $reversion")
projectReversion = if (result.exitValue == 0) stream.toString().trim().toInt() else 0
logger.lifecycle("reversion: $projectReversion")
}
}

Expand All @@ -130,9 +94,8 @@ tasks.register("updateVersion") {
dependsOn("updateDevVer", "updateCommitSha", "updateReversion")

doLast {
version = "$devVer.$reversion+$commitSha"
versionProperties.store(versionPropertiesFile.outputStream(), null)
logger.lifecycle("version: $version")
version = "$projectDevVer.$projectReversion+$projectCommitSha"
logger.lifecycle("version: $projectVersion")
}
}

Expand All @@ -141,13 +104,8 @@ tasks.register("increaseBuildNumber") {
description = "Increase the build number"

doLast {
buildNumber = try {
(buildNumber.toInt() + 1).toString()
} catch (e: Throwable) {
"1"
}
buildNumberProperties.store(buildNumberPropertiesFile.outputStream(), null)
logger.lifecycle("buildNumber: $buildNumber")
projectBuildNumber += 1
logger.lifecycle("buildNumber: $projectBuildNumber")
}
}

Expand All @@ -166,7 +124,7 @@ tasks.register("releaseAndroidApp") {

doLast {
val apkDir = file(project(":compose-app").layout.buildDirectory.dir("outputs/apk/release"))
val outputDir = file("android".outputDirectory)
val outputDir = file(outputDirectoryOf("android"))
apkDir.copyRecursively(outputDir, overwrite = true)
logger.lifecycle("output directory: ${outputDir.absolutePath}")
}
Expand All @@ -176,7 +134,7 @@ tasks.register("releaseLinuxApp") {
group = "project build"
description = "Build the Linux release executable"

if (!isLinux) {
if (!SystemEnvironment.isLinux) {
enabled = false
}

Expand All @@ -187,27 +145,27 @@ tasks.register<Tar>("releaseTarLinuxApp") {
group = "project build"
description = "Build the Linux release tar"

if (!isLinux) {
if (!SystemEnvironment.isLinux) {
enabled = false
}

dependsOn("releaseLinuxApp")

archiveBaseName = "fhraise"
archiveAppendix = "linux"
archiveVersion = "$version.$buildNumber"
archiveClassifier = arch
archiveVersion = "$projectVersion.$projectBuildNumber"
archiveClassifier = SystemEnvironment.arch
archiveExtension = "tar"
compression = Compression.GZIP
destinationDirectory = file("linux-tar".outputDirectory)
from(file("desktop/main-release/app/Fhraise".outputDirectory))
destinationDirectory = file(outputDirectoryOf("linux-tar"))
from(file(outputDirectoryOf("desktop/main-release/app/Fhraise")))
}

tasks.register("releaseWindowsApp") {
group = "project build"
description = "Build the Windows release executable"

if (!isWindows) {
if (!SystemEnvironment.isWindows) {
enabled = false
}

Expand All @@ -218,28 +176,28 @@ tasks.register<Zip>("releaseZipWindowsApp") {
group = "project build"
description = "Build the Windows release zip"

if (!isWindows) {
if (!SystemEnvironment.isWindows) {
enabled = false
}

dependsOn("releaseWindowsApp")

archiveBaseName = "fhraise"
archiveAppendix = "windows"
archiveVersion = "$version.$buildNumber"
archiveClassifier = arch
archiveVersion = "$projectVersion.$projectBuildNumber"
archiveClassifier = SystemEnvironment.arch
archiveExtension = "zip"
destinationDirectory = file("windows-zip".outputDirectory)
from(file("desktop/main-release/app/Fhraise".outputDirectory))
destinationDirectory = file(outputDirectoryOf("windows-zip"))
from(file(outputDirectoryOf("desktop/main-release/app/Fhraise")))
}

tasks.register("releaseDesktopApp") {
group = "project build"
description = "Build the desktop release"

if (isLinux) {
if (SystemEnvironment.isLinux) {
dependsOn("releaseLinuxApp")
} else if (isWindows) {
} else if (SystemEnvironment.isWindows) {
dependsOn("releaseWindowsApp")
}
}
Expand All @@ -248,9 +206,9 @@ tasks.register("releaseArchiveDesktopApp") {
group = "project build"
description = "Build the desktop release archive"

if (isLinux) {
if (SystemEnvironment.isLinux) {
dependsOn("releaseTarLinuxApp")
} else if (isWindows) {
} else if (SystemEnvironment.isWindows) {
dependsOn("releaseZipWindowsApp")
}
}
Expand All @@ -262,7 +220,7 @@ tasks.register("releaseWebApp") {
dependsOn("compose-app:wasmJsBrowserProductionWebpack")

doLast {
logger.lifecycle("output directory: ${file("web".outputDirectory).absolutePath}")
logger.lifecycle("output directory: ${file(outputDirectoryOf("web")).absolutePath}")
}
}

Expand All @@ -274,11 +232,11 @@ tasks.register<Tar>("releaseTarWebApp") {

archiveBaseName = "fhraise"
archiveAppendix = "web"
archiveVersion = "$version.$buildNumber"
archiveVersion = "$projectVersion.$projectBuildNumber"
archiveExtension = "tar"
compression = Compression.GZIP
destinationDirectory = file("web-tar".outputDirectory)
from(file("web".outputDirectory))
destinationDirectory = file(outputDirectoryOf("web-tar"))
from(file(outputDirectoryOf("web")))
}

tasks.register("releaseServer") {
Expand All @@ -289,8 +247,8 @@ tasks.register("releaseServer") {

doLast {
val jar = file(project(":server").layout.buildDirectory.file("libs/server-all.jar"))
val outputDir = file("server".outputDirectory)
jar.copyTo(outputDir.resolve("fhraise-server-$version.$buildNumber.jar"), overwrite = true)
val outputDir = file(outputDirectoryOf("server"))
jar.copyTo(outputDir.resolve("fhraise-server-$projectVersion.$projectBuildNumber.jar"), overwrite = true)
logger.lifecycle("output directory: ${outputDir.absolutePath}")
}
}
Expand All @@ -302,7 +260,7 @@ tasks.register("ciVersioning") {
dependsOn("updateVersion")

doLast {
val outputVersion = "v${version.substringBefore('+')}"
val outputVersion = "v${projectVersion.substringBefore('+')}"
file(System.getenv("GITHUB_OUTPUT")).writeText("version=$outputVersion")
logger.lifecycle("Wrote version to GitHub output: $outputVersion")
}
Expand All @@ -316,20 +274,24 @@ tasks.register("ciReleaseLinuxApp") {

doLast {
val assetsDir = file(layout.buildDirectory.dir("assets"))
file("android".outputDirectory).listFiles()?.filter { it.name.endsWith(".apk") }?.forEach { file ->
file(outputDirectoryOf("android")).listFiles()?.filter { it.name.endsWith(".apk") }?.forEach { file ->
file.name.substringAfter("compose-app-").substringBefore("-release").let {
file.copyTo(assetsDir.resolve("fhraise-android-$version.$buildNumber-$it.apk"), overwrite = true)
file.copyTo(
assetsDir.resolve("fhraise-android-$projectVersion.$projectBuildNumber-$it.apk"), overwrite = true
)
}
}
file("desktop/main-release/deb".outputDirectory).listFiles()?.first()?.copyTo(
assetsDir.resolve("fhraise-linux-$version.$buildNumber-$arch.deb"), overwrite = true
file(outputDirectoryOf("desktop/main-release/deb")).listFiles()?.first()?.copyTo(
assetsDir.resolve("fhraise-linux-$projectVersion.$projectBuildNumber-${SystemEnvironment.arch}.deb"),
overwrite = true
)
file("desktop/main-release/rpm".outputDirectory).listFiles()?.first()?.copyTo(
assetsDir.resolve("fhraise-linux-$version.$buildNumber-$arch.rpm"), overwrite = true
file(outputDirectoryOf("desktop/main-release/rpm")).listFiles()?.first()?.copyTo(
assetsDir.resolve("fhraise-linux-$projectVersion.$projectBuildNumber-${SystemEnvironment.arch}.rpm"),
overwrite = true
)
file("linux-tar".outputDirectory).copyRecursively(assetsDir, overwrite = true)
file("web-tar".outputDirectory).copyRecursively(assetsDir, overwrite = true)
file("server".outputDirectory).copyRecursively(assetsDir, overwrite = true)
file(outputDirectoryOf("linux-tar")).copyRecursively(assetsDir, overwrite = true)
file(outputDirectoryOf("web-tar")).copyRecursively(assetsDir, overwrite = true)
file(outputDirectoryOf("server")).copyRecursively(assetsDir, overwrite = true)
}
}

Expand All @@ -341,9 +303,10 @@ tasks.register("ciReleaseWindowsApp") {

doLast {
val assetsDir = file(layout.buildDirectory.dir("assets"))
file("windows-zip".outputDirectory).copyRecursively(assetsDir, overwrite = true)
file("desktop/main-release/msi".outputDirectory).listFiles()?.first()?.copyTo(
assetsDir.resolve("fhraise-windows-$version.$buildNumber-$arch.msi"), overwrite = true
file(outputDirectoryOf("windows-zip")).copyRecursively(assetsDir, overwrite = true)
file(outputDirectoryOf("desktop/main-release/msi")).listFiles()?.first()?.copyTo(
assetsDir.resolve("fhraise-windows-$projectVersion.$projectBuildNumber-${SystemEnvironment.arch}.msi"),
overwrite = true
)
}
}
Expand All @@ -352,9 +315,9 @@ tasks.register("ciReleaseApp") {
group = "ci"
description = "Build the release app"

if (isLinux) {
if (SystemEnvironment.isLinux) {
dependsOn("ciReleaseLinuxApp")
} else if (isWindows) {
} else if (SystemEnvironment.isWindows) {
dependsOn("ciReleaseWindowsApp")
}
}
Expand Down Expand Up @@ -410,7 +373,7 @@ tasks.register("installReleaseAndroidApp") {
dependsOn("releaseAndroidApp")

doLast {
val apk = file("android".outputDirectory).listFiles()!!.first { it.name.endsWith(".apk") }!!
val apk = file(outputDirectoryOf("android")).listFiles()!!.first { it.name.endsWith(".apk") }!!
val cmd = mutableListOf("adb")
if (hasProperty("device")) {
cmd.add("-s")
Expand Down
22 changes: 22 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This file is part of Fhraise.
* Copyright (c) 2024 HSAS Foodies. All Rights Reserved.
*
* Fhraise is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Fhraise is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Fhraise. If not, see <https://www.gnu.org/licenses/>.
*/

plugins {
alias(libs.plugins.kotlinJvm)
`kotlin-dsl`
}
39 changes: 39 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of Fhraise.
* Copyright (c) 2024 HSAS Foodies. All Rights Reserved.
*
* Fhraise is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Fhraise is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Fhraise. If not, see <https://www.gnu.org/licenses/>.
*/

rootProject.name = "buildSrc"

pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenCentral()
}
}

dependencyResolutionManagement {
repositories {
google()
mavenCentral()
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
}
Loading

0 comments on commit f6f5a92

Please sign in to comment.