Skip to content

Commit d73894b

Browse files
committed
👕 extract replace logic to different module
1 parent 4882579 commit d73894b

File tree

9 files changed

+173
-111
lines changed

9 files changed

+173
-111
lines changed

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group 'com.theapache64'
6-
version '1.1.3'
6+
version '1.2.0'
77

88
repositories {
99
mavenCentral()
@@ -12,4 +12,5 @@ repositories {
1212

1313
dependencies {
1414
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
15+
implementation project(':corvette')
1516
}

corvette/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# corvette
2+
3+
Corvette is a simple engine to pull a GitHub repo and replace its contents

corvette/build.gradle

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id 'org.jetbrains.kotlin.jvm'
3+
}
4+
5+
group 'com.theapache64'
6+
version '1.1.3'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.github.theapache64.corvetee
2+
3+
import com.github.theapache64.corvetee.util.InputUtils
4+
import com.github.theapache64.corvetee.util.unzip
5+
import java.io.File
6+
import java.io.FileOutputStream
7+
import java.net.URL
8+
import java.nio.file.Path
9+
import kotlin.io.path.*
10+
11+
class Corvette(
12+
githubRepoUrl: String, // Eg :https://github.com/theapache64/compose-desktop-template
13+
private val srcPackagePath: Path = Path("com") / "myapp",
14+
private val modules: Array<String> = arrayOf(MAIN_MODULE),
15+
private val srcDirs: Array<String> = arrayOf("main", "test"),
16+
branch: String = "master",
17+
private val isDebug: Boolean = false,
18+
private val debugProjectName: String = "Super Project",
19+
private val debugPackageName: String = "com.theapache64.superproject",
20+
) {
21+
22+
companion object {
23+
const val MAIN_MODULE = "src"
24+
private val REPLACEABLE_FILE_EXT = arrayOf("kt", "kts")
25+
}
26+
27+
private val templateUrl = "$githubRepoUrl/archive/refs/heads/$branch.zip"
28+
private val repoName: String
29+
private val extractedDirName: String
30+
val projectName: String
31+
val packageName: String
32+
33+
34+
init {
35+
val urlSplit = githubRepoUrl.split("/")
36+
repoName = urlSplit.last()
37+
extractedDirName = "$repoName-master"
38+
39+
// Ask project name
40+
projectName = if (isDebug) {
41+
debugProjectName
42+
} else {
43+
InputUtils.promptString("Enter project name", true)
44+
}
45+
46+
// Ask package name
47+
packageName = if (isDebug) {
48+
debugPackageName
49+
} else {
50+
InputUtils.promptString("Enter package name", true)
51+
}
52+
}
53+
54+
55+
fun start(
56+
replaceMap: Map<String, String>,
57+
) {
58+
println("💻 Initializing...")
59+
60+
val currentDir = if (isDebug) {
61+
"tmp"
62+
} else {
63+
System.getProperty("user.dir")
64+
}
65+
66+
// Get source code
67+
println("⬇️ Downloading template...")
68+
val outputFile = Path(currentDir) / "compose-desktop-template.zip"
69+
if (outputFile.notExists()) {
70+
if (outputFile.parent.notExists()) {
71+
outputFile.parent.createDirectories()
72+
}
73+
val os = FileOutputStream(outputFile.toFile())
74+
URL(templateUrl).openStream().copyTo(os)
75+
}
76+
77+
// Unzip
78+
val extractDir = outputFile.parent
79+
println("📦 Unzipping...")
80+
outputFile.unzip(extractDir)
81+
82+
// Rename dir
83+
val extractedProjectDir = extractDir / extractedDirName
84+
val targetProjectDir = extractDir / projectName
85+
targetProjectDir.toFile().deleteRecursively()
86+
extractedProjectDir.moveTo(targetProjectDir, overwrite = true)
87+
88+
// Move source
89+
println("🚚 Preparing source and test files (1/2) ...")
90+
for (module in modules) {
91+
for (type in srcDirs) {
92+
val baseSrc = if (module == MAIN_MODULE) {
93+
// main module
94+
Path(module) / type / "kotlin"
95+
} else {
96+
Path(module) / "src" / type / "kotlin"
97+
}
98+
val myAppSrcPath = targetProjectDir / baseSrc / srcPackagePath
99+
if (myAppSrcPath.exists()) {
100+
val targetSrcPath = targetProjectDir / baseSrc / packageName.replace(".", File.separator)
101+
targetSrcPath.createDirectories()
102+
myAppSrcPath.moveTo(targetSrcPath, overwrite = true)
103+
}
104+
}
105+
}
106+
107+
println("🚚 Verifying file contents (2/2) ...")
108+
109+
targetProjectDir.toFile().walk().forEach { file ->
110+
if (REPLACEABLE_FILE_EXT.contains(file.extension)) {
111+
var newContent = file.readText()
112+
for ((key, value) in replaceMap) {
113+
newContent = newContent.replace(
114+
key, value
115+
)
116+
}
117+
file.writeText(newContent)
118+
}
119+
}
120+
121+
// Give execute permission to ./gradlew
122+
val gradlewFile = targetProjectDir / "gradlew"
123+
gradlewFile.toFile().setExecutable(true, false)
124+
125+
// Acknowledge
126+
if (!isDebug) {
127+
println("♻️ Removing temp files...")
128+
outputFile.deleteIfExists()
129+
}
130+
131+
println("✔️ Finished. [Project Dir: '$targetProjectDir']")
132+
}
133+
134+
}

src/main/kotlin/com/theapache64/ccdp/util/InputUtils.kt renamed to corvette/src/main/kotlin/com/github/theapache64/corvetee/util/InputUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.theapache64.ccdp.util
1+
package com.github.theapache64.corvetee.util
22

33
import java.util.*
44

src/main/kotlin/com/theapache64/ccdp/util/UnZip.kt renamed to corvette/src/main/kotlin/com/github/theapache64/corvetee/util/UnZip.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.theapache64.ccdp.util
1+
package com.github.theapache64.corvetee.util
22

33

44
import java.nio.file.Path

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"main": "index.js",
88
"preferGlobal": true,
99
"repository": "",
10-
"version": "1.1.3",
10+
"version": "1.2.0",
1111
"jdeploy": {
1212
"jar": "create-compose-desktop-app.main.jar"
1313
},

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
rootProject.name = 'create-compose-desktop-app'
2+
include 'corvette'
23

+16-107
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,26 @@
11
package com.theapache64.ccdp
22

3-
import com.theapache64.ccdp.util.InputUtils
4-
import com.theapache64.ccdp.util.unzip
5-
import java.io.File
6-
import java.io.FileOutputStream
7-
import java.net.URL
8-
import kotlin.io.path.*
9-
10-
private const val IS_DEBUG = false
11-
private const val TEMPLATE_URL = "https://github.com/theapache64/compose-desktop-template/archive/refs/heads/master.zip"
12-
private const val EXTRACTED_DIR_NAME = "compose-desktop-template-master"
13-
private val REPLACEABLE_FILE_EXT = arrayOf("kt", "kts")
14-
private const val MAIN_MODULE = "src"
15-
private val MODULES = arrayOf(
16-
MAIN_MODULE, // main module
17-
"data", // data module
18-
)
19-
private val SRC_DIRS = arrayOf("main", "test")
3+
import com.github.theapache64.corvetee.Corvette
204

215
fun main(args: Array<String>) {
22-
println("💻 Initializing...")
23-
// Ask project name
24-
val projectName = if (IS_DEBUG) {
25-
"Super Project"
26-
} else {
27-
InputUtils.promptString("Enter project name", true)
28-
}
29-
30-
// Ask package name
31-
val packageName = if (IS_DEBUG) {
32-
"com.theapache64.superproject"
33-
} else {
34-
InputUtils.promptString("Enter package name", true)
35-
}
36-
37-
val currentDir = if (IS_DEBUG) {
38-
"tmp"
39-
} else {
40-
System.getProperty("user.dir")
41-
}
42-
43-
// Get source code
44-
println("⬇️ Downloading template...")
45-
val outputFile = Path(currentDir) / "compose-desktop-template.zip"
46-
if (outputFile.notExists()) {
47-
if (outputFile.parent.notExists()) {
48-
outputFile.parent.createDirectories()
49-
}
50-
val os = FileOutputStream(outputFile.toFile())
51-
URL(TEMPLATE_URL).openStream().copyTo(os)
52-
}
53-
54-
// Unzip
55-
val extractDir = outputFile.parent
56-
println("📦 Unzipping...")
57-
outputFile.unzip(extractDir)
586

59-
// Rename dir
60-
val extractedProjectDir = extractDir / EXTRACTED_DIR_NAME
61-
val targetProjectDir = extractDir / projectName
62-
targetProjectDir.toFile().deleteRecursively()
63-
extractedProjectDir.moveTo(targetProjectDir, overwrite = true)
64-
65-
// Move source
66-
println("🚚 Preparing source and test files (1/2) ...")
67-
for (module in MODULES) {
68-
for (type in SRC_DIRS) {
69-
val baseSrc = if (module == MAIN_MODULE) {
70-
// main module
71-
Path(module) / type / "kotlin"
72-
} else {
73-
Path(module) / "src" / type / "kotlin"
74-
}
75-
val myAppSrcPath = targetProjectDir / baseSrc / "com" / "myapp"
76-
if (myAppSrcPath.exists()) {
77-
val targetSrcPath = targetProjectDir / baseSrc / packageName.replace(".", File.separator)
78-
targetSrcPath.createDirectories()
79-
myAppSrcPath.moveTo(targetSrcPath, overwrite = true)
80-
}
81-
}
82-
}
7+
val corvette = Corvette(
8+
githubRepoUrl = "https://github.com/theapache64/compose-desktop-template",
9+
modules = arrayOf(
10+
"src",
11+
"data"
12+
),
13+
isDebug = true
14+
)
8315

84-
println("🚚 Verifying file contents (2/2) ...")
8516
val replaceMap = mapOf(
86-
"rootProject.name = \"compose-desktop-template\"" to "rootProject.name = \"$projectName\"", // settings.gradle.kt
87-
"mainClass = \"com.myapp.AppKt\"" to "mainClass = \"$packageName.AppKt\"", // build.gradle
88-
"packageName = \"myapp\"" to "packageName = \"$projectName\"", // build.gradle
89-
"com.myapp" to packageName, // app kt files
90-
"appName = \"My App\"," to "appName = \"$projectName\",", // App.kt
91-
"Hello Desktop!" to "Hello $projectName"
17+
"rootProject.name = \"compose-desktop-template\"" to "rootProject.name = \"${corvette.projectName}\"", // settings.gradle.kt
18+
"mainClass = \"com.myapp.AppKt\"" to "mainClass = \"${corvette.packageName}.AppKt\"", // build.gradle
19+
"packageName = \"myapp\"" to "packageName = \"${corvette.projectName}\"", // build.gradle
20+
"com.myapp" to corvette.packageName, // app kt files
21+
"appName = \"My App\"," to "appName = \"${corvette.projectName}\",", // App.kt
22+
"Hello Desktop!" to "Hello ${corvette.projectName}"
9223
)
9324

94-
targetProjectDir.toFile().walk().forEach { file ->
95-
if (REPLACEABLE_FILE_EXT.contains(file.extension)) {
96-
var newContent = file.readText()
97-
for ((key, value) in replaceMap) {
98-
newContent = newContent.replace(
99-
key, value
100-
)
101-
}
102-
file.writeText(newContent)
103-
}
104-
}
105-
106-
// Give execute permission to ./gradlew
107-
val gradlewFile = targetProjectDir / "gradlew"
108-
gradlewFile.toFile().setExecutable(true, false)
109-
110-
// Acknowledge
111-
if (!IS_DEBUG) {
112-
println("♻️ Removing temp files...")
113-
outputFile.deleteIfExists()
114-
}
115-
116-
println("✔️ Finished. [Project Dir: '$targetProjectDir']")
25+
corvette.start(replaceMap)
11726
}

0 commit comments

Comments
 (0)