|
1 | 1 | package kscript.app.model |
2 | 2 |
|
3 | | -import kscript.app.util.ShellUtils.guessKotlinHome |
| 3 | +import kscript.app.util.ShellUtils |
4 | 4 | import java.io.File |
5 | 5 | import java.nio.file.Path |
6 | 6 | import java.nio.file.Paths |
7 | 7 | import kotlin.io.path.absolute |
8 | 8 |
|
9 | 9 | class ConfigBuilder internal constructor() { |
10 | | - var selfName: String = System.getenv("CUSTOM_KSCRIPT_NAME") ?: "kscript" |
11 | | - var kscriptDir: Path = Paths.get(System.getenv("KSCRIPT_DIR") ?: (System.getProperty("user.home")!! + "/.kscript")) |
12 | | - var customPreamble: String = System.getenv("CUSTOM_KSCRIPT_PREAMBLE") ?: "" |
13 | | - var intellijCommand: String = System.getenv("KSCRIPT_IDEA_COMMAND") ?: "idea" |
14 | | - var gradleCommand: String = System.getenv("KSCRIPT_GRADLE_COMMAND") ?: "gradle" |
15 | | - var kotlinHome: Path? = (System.getenv("KOTLIN_HOME") ?: guessKotlinHome())?.let { Paths.get(it).absolute() } |
16 | | - var osName: String = System.getProperty("os.name") |
17 | | - var homeDir: Path = Paths.get(System.getProperty("user.home")!!) |
18 | | - var classPathSeparator: String = if (osName.lowercase().contains("windows")) ";" else ":" |
19 | | - var separatorChar: Char = File.separatorChar |
20 | | - var kotlinOptsEnvVariable = System.getenv("KSCRIPT_KOTLIN_OPTS") ?: "" |
21 | | - var repositoryUrlEnvVariable = System.getenv("KSCRIPT_REPOSITORY_URL") ?: "" |
22 | | - var repositoryUserEnvVariable = System.getenv("KSCRIPT_REPOSITORY_USER") ?: "" |
23 | | - var repositoryPasswordEnvVariable = System.getenv("KSCRIPT_REPOSITORY_PASSWORD") ?: "" |
| 10 | + var osType: String? = null |
| 11 | + var classPathSeparator: Char? = null |
| 12 | + var hostPathSeparatorChar: Char? = null |
| 13 | + var shellPathSeparatorChar: Char? = null |
| 14 | + var selfName: String? = null |
| 15 | + var kscriptDir: Path? = null |
| 16 | + var customPreamble: String? = null |
| 17 | + var intellijCommand: String? = null |
| 18 | + var gradleCommand: String? = null |
| 19 | + var kotlinHome: Path? = null |
| 20 | + var homeDir: Path? = null |
| 21 | + var kotlinOptsEnvVariable: String? = null |
| 22 | + var repositoryUrlEnvVariable: String? = null |
| 23 | + var repositoryUserEnvVariable: String? = null |
| 24 | + var repositoryPasswordEnvVariable: String? = null |
24 | 25 |
|
25 | 26 | fun build(): Config { |
| 27 | + //Java resolved env variables paths are always in native format; All paths should be stored in Config as native. |
| 28 | + |
| 29 | + //TODO: can it be read from: System.getProperty("os.name"); what about cygwin and msys? |
| 30 | + val osType = OsType.findOrThrow(requireNotNull(osType)) |
| 31 | + val classPathSeparator = classPathSeparator ?: if (osType.isWindowsLike() || osType.isUnixHostedOnWindows()) ';' else ':' |
| 32 | + val hostPathSeparatorChar = hostPathSeparatorChar ?: File.separatorChar |
| 33 | + val shellPathSeparatorChar = shellPathSeparatorChar ?: if (osType.isUnixHostedOnWindows()) '/' else hostPathSeparatorChar |
| 34 | + val selfName = selfName ?: System.getenv("KSCRIPT_NAME") ?: "kscript" |
| 35 | + val kscriptDir = kscriptDir ?: Paths.get(System.getenv("KSCRIPT_DIR") ?: (System.getProperty("user.home")!! + "/.kscript")) |
| 36 | + val customPreamble = customPreamble ?: System.getenv("KSCRIPT_PREAMBLE") ?: "" |
| 37 | + val intellijCommand = intellijCommand ?: System.getenv("KSCRIPT_COMMAND_IDEA") ?: "idea" |
| 38 | + val gradleCommand = gradleCommand ?: System.getenv("KSCRIPT_COMMAND_GRADLE") ?: "gradle" |
| 39 | + val kotlinHome = kotlinHome ?: (System.getenv("KOTLIN_HOME") ?: ShellUtils.guessKotlinHome(osType))?.let { Paths.get(it).absolute() } |
| 40 | + val homeDir = homeDir ?: Paths.get(System.getProperty("user.home")!!) |
| 41 | + val kotlinOptsEnvVariable = kotlinOptsEnvVariable ?: System.getenv("KSCRIPT_KOTLIN_OPTS") ?: "" |
| 42 | + val repositoryUrlEnvVariable = repositoryUrlEnvVariable ?: System.getenv("KSCRIPT_REPOSITORY_URL") ?: "" |
| 43 | + val repositoryUserEnvVariable = repositoryUserEnvVariable ?: System.getenv("KSCRIPT_REPOSITORY_USER") ?: "" |
| 44 | + val repositoryPasswordEnvVariable = repositoryPasswordEnvVariable ?: System.getenv("KSCRIPT_REPOSITORY_PASSWORD") ?: "" |
| 45 | + |
26 | 46 | return Config( |
| 47 | + osType, |
27 | 48 | selfName, |
28 | 49 | kscriptDir, |
29 | 50 | customPreamble, |
30 | 51 | intellijCommand, |
31 | 52 | gradleCommand, |
32 | 53 | kotlinHome, |
33 | | - osName, |
34 | 54 | classPathSeparator, |
35 | | - separatorChar, |
| 55 | + hostPathSeparatorChar, |
| 56 | + shellPathSeparatorChar, |
36 | 57 | homeDir, |
37 | 58 | kotlinOptsEnvVariable, |
38 | 59 | repositoryUrlEnvVariable, |
|
0 commit comments