Skip to content

Commit

Permalink
[#29] Create buildScr module for ProjectSettings (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaozhan authored Nov 5, 2023
1 parent 864555a commit e317f21
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ concurrency:

env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
CI: true

jobs:

Expand Down
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repositories {
mavenCentral()
}

plugins {
`kotlin-dsl`
}
79 changes: 79 additions & 0 deletions buildSrc/src/main/kotlin/ProjectSettings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import java.io.ByteArrayOutputStream
import java.io.File

object ProjectSettings {

private const val MAYOR_VERSION = 0
private const val MINOR_VERSION = 0

// git rev-list --first-parent --count origin/master +1
private const val VERSION_DIF = 0
private const val BASE_VERSION_CODE = 0

const val PROJECT_ID = "com.oztechan.tracefit"

const val COMPILE_SDK_VERSION = 34
const val MIN_SDK_VERSION = 24
const val TARGET_SDK_VERSION = 34

val JAVA_VERSION = JavaVersion.VERSION_1_8

@Suppress("TooGenericExceptionCaught", "SwallowedException")
fun getVersionCode(project: Project) = try {
gitCommitCount(project).toInt() + BASE_VERSION_CODE
} catch (e: Exception) {
1
}

@Suppress("TooGenericExceptionCaught", "SwallowedException")
fun getVersionName(
project: Project
): String = try {
if (isMaster(project)) {
"$MAYOR_VERSION.$MINOR_VERSION.${getVersionCode(project) - VERSION_DIF - BASE_VERSION_CODE}"
} else {
"0.0.${getVersionCode(project)}" // testing build
}.also {
if (isCI()) project.setIOSVersion(it)
}
} catch (e: Exception) {
"0.0.1"
}

private fun gitCommitCount(project: Project): String {
val os = ByteArrayOutputStream()
project.exec {
commandLine = "git rev-list --first-parent --count HEAD".split(" ")
standardOutput = os
}
return String(os.toByteArray()).trim()
}

private fun isMaster(project: Project): Boolean {
val os = ByteArrayOutputStream()
project.exec {
commandLine = "git rev-parse --abbrev-ref HEAD".split(" ")
standardOutput = os
}
return String(os.toByteArray()).trim() == "master"
}

private fun isCI() = System.getenv("CI") == "true"

@Suppress("TooGenericExceptionCaught")
private fun Project.setIOSVersion(versionName: String) = try {
exec {
workingDir = File("${project.rootDir}/ios")
commandLine = "agvtool new-version -all ${getVersionCode(project)}".split(" ")
}
exec {
workingDir = File("${project.rootDir}/ios")
commandLine = "agvtool new-marketing-version $versionName".split(" ")
}
} catch (e: Exception) {
println("agvtool exist only mac environment")
println(e.localizedMessage)
}
}
36 changes: 20 additions & 16 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ kotlin {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = ProjectSettings.JAVA_VERSION.toString()
}
}
}
Expand Down Expand Up @@ -51,19 +51,26 @@ kotlin {
}

android {
namespace = "com.oztechan.tracefit"
compileSdk = libs.versions.android.compileSdk.get().toInt()
ProjectSettings.apply {
namespace = "com.oztechan.tracefit"
compileSdk = COMPILE_SDK_VERSION

sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].res.srcDirs("src/androidMain/res")
sourceSets["main"].resources.srcDirs("src/commonMain/resources")
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].res.srcDirs("src/androidMain/res")
sourceSets["main"].resources.srcDirs("src/commonMain/resources")

defaultConfig {
applicationId = "com.oztechan.tracefit"
minSdk = libs.versions.android.minSdk.get().toInt()
targetSdk = libs.versions.android.targetSdk.get().toInt()
versionCode = 1
versionName = "1.0"
defaultConfig {
applicationId = PROJECT_ID
minSdk = MIN_SDK_VERSION
targetSdk = TARGET_SDK_VERSION
versionCode = getVersionCode(project)
versionName = getVersionName(project)
}

compileOptions {
sourceCompatibility = JAVA_VERSION
targetCompatibility = JAVA_VERSION
}
}
buildFeatures {
compose = true
Expand All @@ -81,10 +88,7 @@ android {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
debugImplementation(libs.compose.ui.tooling)
}
Expand Down
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ compose = "1.5.4"
compose-plugin = "1.5.3"
compose-compiler = "1.5.3"
agp = "8.1.2"
android-minSdk = "24"
android-compileSdk = "34"
android-targetSdk = "34"
androidx-activityCompose = "1.8.0"
androidx-core-ktx = "1.12.0"
androidx-appcompat = "1.6.1"
Expand Down

0 comments on commit e317f21

Please sign in to comment.