@@ -112,42 +112,54 @@ fun zipPreserveStructure(sources: List<Path>, outputFile: Path, base: Path) {
112112 }
113113}
114114
115- // --------------------------- GIT ---------------------------
116-
117- fun prepareRepo (): Path {
118- val dir = Paths .get(WORKDIR )
119- if (Files .exists(dir.resolve(" .git" ))) {
120- log.info(" Repo exists — fetching and checking out $GIT_REF ..." )
121- Git .open(dir.toFile()).use { git ->
122- git.fetch().call()
123- git.checkout().setName(GIT_REF ).call()
124- }
125- } else {
126- log.info(" Cloning $REPO_URL -> $dir ..." )
127- Git .cloneRepository()
128- .setURI(REPO_URL )
129- .setDirectory(dir.toFile())
130- .call()
131- Git .open(dir.toFile()).use { git ->
132- git.checkout().setName(GIT_REF ).call()
115+ data class FeatureModGitRepo (
116+ val workDir : Path ,
117+ val repoUrl : String ,
118+ val gitRef : String ,
119+ ) {
120+ fun checkout (): Path {
121+ if (Files .exists(workDir.resolve(" .git" ))) {
122+ log.info(" Repo exists — fetching and checking out $gitRef ..." )
123+ Git .open(workDir.toFile()).use { git ->
124+ git.fetch().call()
125+ git.checkout().setName(gitRef).call()
126+ }
127+ } else {
128+ log.info(" Cloning repository $repoUrl " )
129+ Git .cloneRepository()
130+ .setURI(repoUrl)
131+ .setDirectory(workDir.toFile())
132+ .call()
133+ log.info(" Checking out $gitRef " )
134+ Git .open(workDir.toFile()).use { git ->
135+ git.checkout().setName(gitRef).call()
136+ }
133137 }
138+
139+ return workDir
134140 }
135- return dir
136141}
137142
138- object GithubAssetDownloader {
139- private const val API_URL = " https://api.github.com/repos/FAForever/fa-coop/releases/tags/v%s"
140- private val DOWNLOAD_URL_REGEX = Regex (""" "browser_download_url"\s*:\s*"(?<url>[^"]+)"""" )
143+ data class GithubAssetDownloader (
144+ val repoOwner : String = " FAForever" ,
145+ val repoName : String ,
146+ val suffix : String ,
147+ val version : String ,
148+ ) {
149+ companion object {
150+ private const val API_URL = " https://api.github.com/repos/%s/%s/releases/tags/v%s"
151+ private val DOWNLOAD_URL_REGEX = Regex (""" "browser_download_url"\s*:\s*"(?<url>[^"]+)"""" )
152+ }
141153
142154 private val httpClient = HttpClient .newBuilder()
143155 .connectTimeout(Duration .ofSeconds(10 ))
144156 .followRedirects(HttpClient .Redirect .NORMAL )
145157 .build()
146158
147- fun downloadVoiceOverAssets ( version : String , targetDir : Path ): List <Path > {
159+ fun downloadAssets ( targetDir : Path ): List <Path > {
148160 log.info(" Downloading VO assets for version $version from GitHub releases..." )
149161
150- val urls = getAssetUrisBySuffix(version, " .nx2 " )
162+ val urls = getAssetUrisBySuffix()
151163 if (urls.isEmpty()) {
152164 log.info(" No .nx2 assets found in release v{}" , version)
153165 return emptyList()
@@ -185,9 +197,9 @@ object GithubAssetDownloader {
185197 return downloaded.map { outDir.resolve(it.fileName) }
186198 }
187199
188- private fun getAssetUrisBySuffix (version : String , suffix : String ): List <URI > {
200+ private fun getAssetUrisBySuffix (): List <URI > {
189201 val request = HttpRequest .newBuilder()
190- .uri(URI .create(API_URL .format(version)))
202+ .uri(URI .create(API_URL .format(repoOwner, repoName, version)))
191203 .timeout(Duration .ofSeconds(10 ))
192204 .header(" Accept" , " application/vnd.github.v3+json" )
193205 .GET ()
@@ -224,17 +236,20 @@ object GithubAssetDownloader {
224236
225237}
226238
227-
228- // --------------------------- DATABASE ---------------------------
229-
230- class FafDatabase (val host : String , val database : String , val dryRun : Boolean ) : AutoCloseable {
239+ data class FafDatabase (
240+ val host : String ,
241+ val database : String ,
242+ val username : String ,
243+ val password : String ,
244+ val dryRun : Boolean
245+ ) : AutoCloseable {
231246 /* *
232247 * Definition of an existing file in the database
233248 */
234249 data class PatchFile (val mod : String , val fileId : Int , val name : String , val md5 : String , val version : Int )
235250
236251 private val connection: Connection =
237- DriverManager .getConnection(" jdbc:mariadb://$host /$database ?useSSL=false&serverTimezone=UTC" , DB_USER , DB_PASS )
252+ DriverManager .getConnection(" jdbc:mariadb://$host /$database ?useSSL=false&serverTimezone=UTC" , username, password )
238253
239254 fun getCurrentPatchFile (mod : String , fileId : Int ): PatchFile ? {
240255 val sql = """
@@ -289,7 +304,6 @@ class FafDatabase(val host: String, val database: String, val dryRun: Boolean) :
289304 }
290305}
291306
292-
293307// --------------------------- PROCESS FILES ---------------------------
294308
295309fun processItem (
@@ -387,11 +401,19 @@ fun processItem(
387401
388402fun main () {
389403 log.info(" === Kotlin Coop Deployer v{} ===" , PATCH_VERSION )
390- val repo = prepareRepo()
391- log.info(" Repo ready at {}" , repo)
404+
405+ val repo = FeatureModGitRepo (
406+ workDir = Paths .get(WORKDIR ),
407+ repoUrl = REPO_URL ,
408+ gitRef = GIT_REF
409+ ).checkout()
392410
393411 // Download VO assets first
394- GithubAssetDownloader .downloadVoiceOverAssets(PATCH_VERSION , TARGET_DIR ) // returns list of paths in target dir
412+ GithubAssetDownloader (
413+ repoName = " fa-coop" ,
414+ suffix = " .nx2" ,
415+ version = PATCH_VERSION
416+ ).downloadAssets(TARGET_DIR )
395417
396418 val filesList = listOf (
397419 PatchFile (1 , " init_coop.v%d.lua" , listOf (repo.resolve(" init_coop.lua" ))),
@@ -432,9 +454,15 @@ fun main() {
432454 // … add the rest
433455 )
434456
435- FafDatabase (DB_HOST , DB_NAME , DRYRUN ).use { db ->
457+ FafDatabase (
458+ host = DB_HOST ,
459+ database = DB_NAME ,
460+ username = DB_USER ,
461+ password = DB_PASS ,
462+ dryRun = DRYRUN
463+ ).use { db ->
436464 filesList.forEach { processItem(db, it) }
437465 }
438466
439- log.info(" === Done ===" )
467+ log.info(" === Deployment complete ===" )
440468}
0 commit comments