Deduplicate installation of required tools in macos builder
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Wed, 19 Jun 2024 10:08:43 +0000 (18:08 +0800)
committerClaudio Cambra <developer@claudiocambra.com>
Mon, 8 Jul 2024 07:41:45 +0000 (15:41 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
admin/osx/craft-client.swift

index 4d8461aa3ec095d9a021df33ef9f8efe4bcdb289..68bcc77b1b0daa5d224e7467f8cd861f65df751e 100644 (file)
@@ -45,56 +45,35 @@ func commandExists(_ command: String) -> Bool {
     return run("/usr/bin/type", command, quiet: true) == 0
 }
 
-print("Configuring build tooling.")
-
-if commandExists("git") {
-    print("Git is installed.")
-} else {
-    print("Git is missing. Installing xcode command line tools.")
-    guard shell("xcode-select --install") == 0 else {
-        print("Failed to install xcode command line tools.")
-        exit(1)
-    }
+func installIfMissing(
+    _ command: String, 
+    _ installCommand: String, 
+    installCommandEnv: [String: String]? = nil
+) {
+    if commandExists(command) {
+        print("\(command) is installed.")
+    } else {
+        print("\(command) is missing. Installing...")
+        guard shell(installCommand, env: installCommandEnv) == 0 else {
+            print("Failed to install \(command).")
+            exit(1)
+        }
+        print("\(command) installed.")
+    } 
 }
 
-if commandExists("brew") {
-    print("Brew is installed.")
-} else {
-    print("Installing Homebrew...")
-    guard shell(
-        "curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh | /bin/bash", 
-        env: ["NONINTERACTIVE": "1"]
-    ) == 0 else {
-        print("Failed to install Homebrew.")
-        exit(1)
-    }
-    print("Brew installed.")
-}
+print("Configuring build tooling.")
 
-if commandExists("inkscape") {
-    print("Inkscape is installed.")
-} else {
-    guard shell("brew install inkscape") == 0 else {
-        print("Failed to install Inkscape.")
-        exit(1)
-    }
-    print("Inkscape installed.")
-}
+installIfMissing("git", "xcode-select --install")
+installIfMissing(
+    "brew", 
+    "curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh | /bin/bash", 
+    installCommandEnv: ["NONINTERACTIVE": "1"]
+)
+installIfMissing("inkscape", "brew install inkscape")
+installIfMissing("python3", "brew install pyenv && pyenv install 3.12.4")
+
+print("Build tooling configured.")
+print("Configuring KDE Craft.")
 
-if commandExists("python3") {
-    print("Python 3 is installed.")
-} else {
-    if commandExists("pyenv") {
-        print("Pyenv is installed.")
-    } else {
-        guard shell("brew install pyenv") == 0 else {
-            print("Failed to install Pyenv.")
-            exit(1)
-        }
-    }
 
-    guard shell ("pyenv install 3.12.4") == 0 else {
-        print("Failed to install Python 3.")
-        exit(1)
-    }
-}