@@ -10,13 +10,21 @@ import java.io.File
1010
1111val Project .isRootProject get() = this == rootProject
1212
13- fun shell (
14- command : String ,
13+ fun cli (
14+ vararg command : String ,
1515 workingDir : File ? = null,
1616 env : Map <String , String > = emptyMap(),
1717 inheritIO : Boolean = false,
1818): String {
19- val processBuilder = ProcessBuilder (" /bin/bash" , " -c" , command)
19+ var cmd = command.first().replace(" /" , File .separator)
20+ if (OS .current == OS .Windows ) {
21+ if (File (" $cmd .bat" ).exists()) {
22+ cmd + = " .bat"
23+ } else if (File (" $cmd .exe" ).exists()) {
24+ cmd + = " .exe"
25+ }
26+ }
27+ val processBuilder = ProcessBuilder (cmd, * command.drop(1 ).toTypedArray())
2028 workingDir?.let { processBuilder.directory(it) }
2129 processBuilder.redirectErrorStream(true )
2230 processBuilder.environment().putAll(env)
@@ -26,10 +34,18 @@ fun shell(
2634 if (inheritIO) {
2735 println (it)
2836 }
29- check(exitCode == 0 ) { " Process exit code was: $exitCode \n Original command: $command " }
37+ check(exitCode == 0 ) { " Process exit code was: $exitCode \n Original command: ${ command.toList()} \n Result: $it " }
3038 }
3139}
3240
41+ fun shell (
42+ command : String ,
43+ workingDir : File ? = null,
44+ env : Map <String , String > = emptyMap(),
45+ inheritIO : Boolean = false,
46+ ): String =
47+ cli(" /bin/bash" , " -c" , command, workingDir = workingDir, env = env, inheritIO = inheritIO)
48+
3349fun Project.withGeneratedBuildFile (category : String , path : String , sourceSet : String? = null, content : () -> String ) {
3450 val generatedDir = file(" ${getGeneratedBuildFilesRoot()} /$category " )
3551 extensions.findByType<KotlinJvmExtension >()?.apply {
@@ -61,16 +77,35 @@ internal fun Project.getGeneratedBuildFilesRoot(): File =
6177 file(" $projectDir /build/generated/source/build-logic" )
6278
6379internal fun Project.detectProjectVersion (): String =
64- System .getenv(" OVERRIDE_VERSION" )?.takeIf { it.isNotBlank() }
65- ? : shell (" git tag --points-at HEAD" ).split(" \n " ).filter {
80+ System .getenv(" OVERRIDE_VERSION" )?.removePrefix( " v " )?.removePrefix( " - " )?. takeIf { it.isNotBlank() }
81+ ? : cli (" git" , " tag" , " --points-at" , " HEAD" ).split(" \n " ).filter {
6682 versionRegex.matchEntire(it) != null
6783 }.maxByOrNull {
6884 VersionComparable (versionRegex.matchEntire(it)!! .destructured.toList())
6985 }?.removePrefix(" v" )?.removePrefix(" -" ) ? : run {
70- val branchName = shell (" git rev-parse --abbrev-ref HEAD" )
86+ val branchName = cli (" git" , " rev-parse" , " --abbrev-ref" , " HEAD" )
7187 " 999999.0.0-${sanitizeBranchName(branchName)} .1"
7288 }
7389
90+ enum class OS {
91+ Linux ,
92+ macOS,
93+ Windows ,
94+ ;
95+
96+ companion object Companion {
97+ val current: OS by lazy {
98+ val osName = System .getProperty(" os.name" ).lowercase()
99+ when {
100+ " mac" in osName || " darwin" in osName -> macOS
101+ " linux" in osName -> Linux
102+ " windows" in osName -> Windows
103+ else -> error(" Unknown operating system: $osName " )
104+ }
105+ }
106+ }
107+ }
108+
74109private class VersionComparable (val parts : List <String >) : Comparable<VersionComparable> {
75110 override fun compareTo (other : VersionComparable ): Int {
76111 for ((l, r) in parts.zip(other.parts)) {
0 commit comments