1
1
package com.theapache64.ccdp
2
2
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
20
4
21
5
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)
58
6
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
+ )
83
15
84
- println (" 🚚 Verifying file contents (2/2) ..." )
85
16
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} "
92
23
)
93
24
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)
117
26
}
0 commit comments