@@ -7,9 +7,7 @@ import kotlin.io.path.reader
77
88@Suppress(" MemberVisibilityCanBePrivate" )
99class ConfigBuilder (
10- private val osType : OsType ,
11- private val systemProperties : Properties ,
12- private val environment : Map <String , String ?>
10+ private val osType : OsType , private val systemProperties : Properties , private val environment : Map <String , String ?>
1311) {
1412 var userHomeDir: OsPath ? = null
1513 var tempDir: OsPath ? = null
@@ -27,11 +25,16 @@ class ConfigBuilder(
2725 var repositoryPassword: String? = null
2826 var artifactsDir: OsPath ? = null
2927
30- // Env variables paths read by Java are always in native format; All paths should be stored in Config as native,
28+ // Java properties paths are always in native format. All paths should be stored in Config as native,
3129 // and then converted to shell format as needed.
32- // private fun path(path: String) = OsPath.createOrThrow(OsType.native, path)
3330 private fun String.toOsPathFromNative () = OsPath .createOrThrow(OsType .native, this )
34- private fun String.toOsPathFromOsSpecific (osType : OsType ) = OsPath .createOrThrow(osType, this ).toNativeOsPath()
31+ private fun String.toOsPathFromOsSpecific (osType : OsType ) = OsPath .createOrThrow(osType, this )
32+ private fun String.toOsPathFromEnvVariable (osType : OsType ) = when (osType) {
33+ // MSYS automatically converts any path in env to windows format
34+ OsType .MSYS -> OsPath .createOrThrow(OsType .native, this )
35+ else -> OsPath .createOrThrow(osType, this )
36+ }.toNativeOsPath()
37+
3538 private fun Properties.getPropertyOrNull (name : String ) = this .getProperty(name).nullIfBlank()
3639 private fun Map <String , String ?>.getEnvVariableOrNull (name : String ) = this [name].nullIfBlank()
3740 private fun String?.nullIfBlank () = if (this .isNullOrBlank()) null else this
@@ -46,7 +49,7 @@ class ConfigBuilder(
4649 val selfName: String = selfName ? : environment.getEnvVariableOrNull(" KSCRIPT_NAME" ) ? : " kscript"
4750
4851 val kscriptDir: OsPath ? =
49- kscriptDir ? : environment.getEnvVariableOrNull(" KSCRIPT_DIRECTORY" )?.toOsPathFromNative( )
52+ kscriptDir ? : environment.getEnvVariableOrNull(" KSCRIPT_DIRECTORY" )?.toOsPathFromEnvVariable(osType )
5053
5154 val cacheDir: OsPath = cacheDir ? : kscriptDir?.resolve(" cache" ) ? : when {
5255 osType.isWindowsLike() -> environment.getEnvVariableOrNull(" LOCALAPPDATA" )?.toOsPathFromNative() ? : tempDir
@@ -63,6 +66,7 @@ class ConfigBuilder(
6366 val configFile: OsPath = configFile ? : kscriptDir?.resolve(" kscript.properties" ) ? : when {
6467 osType.isWindowsLike() -> environment.getEnvVariableOrNull(" LOCALAPPDATA" )?.toOsPathFromNative()
6568 ? : userHomeDir.resolve(" .config" )
69+
6670 osType == OsType .MACOS -> userHomeDir.resolve(" Library" , " Application Support" )
6771 else -> environment.getEnvVariableOrNull(" XDG_CONFIG_DIR" )?.toOsPathFromNative()
6872 ? : userHomeDir.resolve(" .config" )
@@ -108,17 +112,12 @@ class ConfigBuilder(
108112 val repositoryPassword = repositoryPassword ? : environment.getEnvVariableOrNull(" KSCRIPT_REPOSITORY_PASSWORD" )
109113 ? : configProperties.getPropertyOrNull(" scripting.repository.password" ) ? : " "
110114
111- val artifactsDir: OsPath ? =
112- artifactsDir ? : environment.getEnvVariableOrNull( " KSCRIPT_DIRECTORY_ARTIFACTS " )?.toOsPathFromOsSpecific(osType )
113- ? : configProperties.getPropertyOrNull( " scripting.directory.artifacts " )? .toOsPathFromOsSpecific(osType)
115+ val artifactsDir: OsPath ? = artifactsDir ? : environment.getEnvVariableOrNull( " KSCRIPT_DIRECTORY_ARTIFACTS " )
116+ ?.toOsPathFromEnvVariable(osType) ? : configProperties.getPropertyOrNull( " scripting.directory.artifacts " )
117+ ?.toOsPathFromOsSpecific(osType)
114118
115119 val scriptingConfig = ScriptingConfig (
116- customPreamble,
117- providedKotlinOpts,
118- repositoryUrl,
119- repositoryUser,
120- repositoryPassword,
121- artifactsDir
120+ customPreamble, providedKotlinOpts, repositoryUrl, repositoryUser, repositoryPassword, artifactsDir
122121 )
123122
124123 return Config (osConfig, scriptingConfig)
0 commit comments