refactor(launch): 将 AppPaths 从 Launcher 解耦.

解耦后有助于后续改进, 以及单元测试的编写.
This commit is contained in:
LamGC 2022-11-07 11:16:41 +08:00
parent c94e0476b5
commit 30a2cb34d5
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -61,7 +61,10 @@ internal fun startMetricsServer(config: MetricsConfig = Const.config.metrics): H
return httpServer
}
internal class Launcher(private val config: AppConfig = Const.config) : AutoCloseable {
internal class Launcher(
private val config: AppConfig = Const.config,
private val configFile: File = AppPaths.CONFIG_APPLICATION.file,
) : AutoCloseable {
companion object {
@JvmStatic
@ -75,27 +78,25 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
private fun getMavenLocalRepository(): LocalRepository {
val localPath =
if (config.mavenLocalRepository != null && config.mavenLocalRepository!!.isNotEmpty()) {
val repoPath = AppPaths.DATA_ROOT.file.toPath()
.resolve(config.mavenLocalRepository!!)
.apply {
if (!exists()) {
if (!parent.isWritable() || !parent.isReadable()) {
throw IOException("Unable to read and write the directory where Maven repository is located.")
}
if (System.getProperty("os.name").lowercase().startsWith("windows")) {
createDirectories()
} else {
val fileAttributes = setOf(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.GROUP_READ,
PosixFilePermission.GROUP_WRITE,
PosixFilePermission.OTHERS_READ,
)
createDirectories(PosixFilePermissions.asFileAttribute(fileAttributes))
}
val repoPath = configFile.toPath().resolve(config.mavenLocalRepository!!).apply {
if (!exists()) {
if (!parent.isWritable() || !parent.isReadable()) {
throw IOException("Unable to read and write the directory where Maven repository is located.")
}
if (System.getProperty("os.name").lowercase().startsWith("windows")) {
createDirectories()
} else {
val fileAttributes = setOf(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.GROUP_READ,
PosixFilePermission.GROUP_WRITE,
PosixFilePermission.OTHERS_READ,
)
createDirectories(PosixFilePermissions.asFileAttribute(fileAttributes))
}
}
}
.toRealPath()
.toFile()
repoPath