From 46a95c35588a908bf87be9b1e2902e371e7ebb78 Mon Sep 17 00:00:00 2001
From: Cam Saul <1455846+camsaul@users.noreply.github.com>
Date: Mon, 16 Nov 2020 13:04:38 -0800
Subject: [PATCH] Mac app build script improvements :apple: :wrench: (#13810)

---
 OSX/deps.edn                                      |  2 +-
 OSX/{ => src}/macos_release.clj                   |  2 ++
 OSX/{ => src}/macos_release/addShortcut.scpt      |  0
 OSX/{ => src}/macos_release/build.clj             |  9 +++++----
 OSX/{ => src}/macos_release/codesign.clj          |  0
 OSX/{ => src}/macos_release/common.clj            |  2 ++
 OSX/{ => src}/macos_release/create_dmg.clj        |  0
 OSX/src/macos_release/download_jar.clj            | 15 +++++++++++++++
 OSX/{ => src}/macos_release/notarize.clj          |  0
 OSX/{ => src}/macos_release/sparkle_artifacts.clj |  0
 OSX/{ => src}/macos_release/upload.clj            |  0
 bin/common/src/metabuild_common/core.clj          |  1 +
 bin/common/src/metabuild_common/files.clj         |  9 +++++++++
 bin/release/src/release/elastic_beanstalk.clj     |  4 +---
 bin/release/src/release/uberjar.clj               |  3 +--
 docs/developers-guide-osx.md                      | 11 +++--------
 16 files changed, 40 insertions(+), 18 deletions(-)
 rename OSX/{ => src}/macos_release.clj (93%)
 rename OSX/{ => src}/macos_release/addShortcut.scpt (100%)
 rename OSX/{ => src}/macos_release/build.clj (92%)
 rename OSX/{ => src}/macos_release/codesign.clj (100%)
 rename OSX/{ => src}/macos_release/common.clj (97%)
 rename OSX/{ => src}/macos_release/create_dmg.clj (100%)
 create mode 100644 OSX/src/macos_release/download_jar.clj
 rename OSX/{ => src}/macos_release/notarize.clj (100%)
 rename OSX/{ => src}/macos_release/sparkle_artifacts.clj (100%)
 rename OSX/{ => src}/macos_release/upload.clj (100%)

diff --git a/OSX/deps.edn b/OSX/deps.edn
index b1c58d89fe7..3e4032186cb 100644
--- a/OSX/deps.edn
+++ b/OSX/deps.edn
@@ -1,4 +1,4 @@
-{:paths ["./"]
+{:paths ["src"]
 
  :deps
  {common/common           {:local/root "../bin/common"}
diff --git a/OSX/macos_release.clj b/OSX/src/macos_release.clj
similarity index 93%
rename from OSX/macos_release.clj
rename to OSX/src/macos_release.clj
index 2a486e83305..6ed28f0e998 100644
--- a/OSX/macos_release.clj
+++ b/OSX/src/macos_release.clj
@@ -5,6 +5,7 @@
              [build :as build]
              [codesign :as codesign]
              [create-dmg :as create-dmg]
+             [download-jar :as download-jar]
              [notarize :as notarize]
              [sparkle-artifacts :as sparkle-artifacts]
              [upload :as upload]]
@@ -12,6 +13,7 @@
 
 (def ^:private steps*
   (ordered-map/ordered-map
+   :download-jar               download-jar/download-jar!
    :build                      build/build!
    :codesign                   codesign/codesign!
    :generate-sparkle-artifacts sparkle-artifacts/generate-sparkle-artifacts!
diff --git a/OSX/macos_release/addShortcut.scpt b/OSX/src/macos_release/addShortcut.scpt
similarity index 100%
rename from OSX/macos_release/addShortcut.scpt
rename to OSX/src/macos_release/addShortcut.scpt
diff --git a/OSX/macos_release/build.clj b/OSX/src/macos_release/build.clj
similarity index 92%
rename from OSX/macos_release/build.clj
rename to OSX/src/macos_release/build.clj
index 5a7a2b4c643..624202ca66e 100644
--- a/OSX/macos_release/build.clj
+++ b/OSX/src/macos_release/build.clj
@@ -1,6 +1,7 @@
 (ns macos-release.build
   (:require [clojure.string :as str]
-            [macos-release.common :as c]))
+            [macos-release.common :as c]
+            [metabuild-common.core :as u]))
 
 (def ^String info-plist-file
   (c/assert-file-exists (str c/macos-source-dir "/Metabase/Metabase-Info.plist")))
@@ -44,11 +45,11 @@
 (defn- clean! []
   (c/step "Clean XCode build artifacts"
     (xcode-build "-project" xcode-project-file "clean")
-    (c/delete-file! c/artifacts-directory)))
+    (u/delete-file-if-exists! c/artifacts-directory)))
 
 (defn- build-xcarchive! []
   (let [filename (c/artifact "Metabase.xcarchive")]
-    (c/delete-file! filename)
+    (u/delete-file-if-exists! filename)
     (c/step (format "Build %s" filename)
       (xcode-build "-project"       xcode-project-file
                    "-scheme"        "Metabase"
@@ -59,7 +60,7 @@
 
 (defn- build-app! []
   (let [filename (c/artifact "Metabase.app")]
-    (c/delete-file! filename)
+    (u/delete-file-if-exists! filename)
     (c/step (format "Create %s" filename)
       (xcode-build "-exportArchive"
                    "-exportOptionsPlist" export-options-plist-file
diff --git a/OSX/macos_release/codesign.clj b/OSX/src/macos_release/codesign.clj
similarity index 100%
rename from OSX/macos_release/codesign.clj
rename to OSX/src/macos_release/codesign.clj
diff --git a/OSX/macos_release/common.clj b/OSX/src/macos_release/common.clj
similarity index 97%
rename from OSX/macos_release/common.clj
rename to OSX/src/macos_release/common.clj
index 4459042e06d..c092255c68a 100644
--- a/OSX/macos_release/common.clj
+++ b/OSX/src/macos_release/common.clj
@@ -7,6 +7,8 @@
 
 (comment u/keep-me)
 
+(set! *warn-on-reflection* true)
+
 (p/import-vars
  [u
   announce
diff --git a/OSX/macos_release/create_dmg.clj b/OSX/src/macos_release/create_dmg.clj
similarity index 100%
rename from OSX/macos_release/create_dmg.clj
rename to OSX/src/macos_release/create_dmg.clj
diff --git a/OSX/src/macos_release/download_jar.clj b/OSX/src/macos_release/download_jar.clj
new file mode 100644
index 00000000000..8c018620b0a
--- /dev/null
+++ b/OSX/src/macos_release/download_jar.clj
@@ -0,0 +1,15 @@
+(ns macos-release.download-jar
+  (:require [macos-release.common :as c]
+            [metabuild-common.core :as u]))
+
+(defn- uberjar-url []
+  (format "https://downloads.metabase.com/v%s/metabase.jar" (c/version)))
+
+(def ^:private uberjar-dest-location
+  (u/filename c/root-directory "OSX" "Resources" "metabase.jar"))
+
+(defn download-jar!
+  "Download the uberjar for the version of the Mac App we're building."
+  []
+  (u/step (format "Download JAR for version %s" (c/version))
+    (u/download-file! (uberjar-url) uberjar-dest-location)))
diff --git a/OSX/macos_release/notarize.clj b/OSX/src/macos_release/notarize.clj
similarity index 100%
rename from OSX/macos_release/notarize.clj
rename to OSX/src/macos_release/notarize.clj
diff --git a/OSX/macos_release/sparkle_artifacts.clj b/OSX/src/macos_release/sparkle_artifacts.clj
similarity index 100%
rename from OSX/macos_release/sparkle_artifacts.clj
rename to OSX/src/macos_release/sparkle_artifacts.clj
diff --git a/OSX/macos_release/upload.clj b/OSX/src/macos_release/upload.clj
similarity index 100%
rename from OSX/macos_release/upload.clj
rename to OSX/src/macos_release/upload.clj
diff --git a/bin/common/src/metabuild_common/core.clj b/bin/common/src/metabuild_common/core.clj
index db228554d51..5cabe58adbd 100644
--- a/bin/common/src/metabuild_common/core.clj
+++ b/bin/common/src/metabuild_common/core.clj
@@ -39,6 +39,7 @@
   create-directory-unless-exists!
   delete-file!
   delete-file-if-exists!
+  download-file!
   file-exists?
   filename
   find-files
diff --git a/bin/common/src/metabuild_common/files.clj b/bin/common/src/metabuild_common/files.clj
index 3bfd9998647..e35ae22a4db 100644
--- a/bin/common/src/metabuild_common/files.clj
+++ b/bin/common/src/metabuild_common/files.clj
@@ -111,3 +111,12 @@
       (throw (ex-info (format "Can't find project root directory: no parent directory of %s has a project.clj file"
                               (env/env :user-dir))
                       {:dir (env/env :user-dir)})))))
+
+(defn download-file!
+  "Download a file from `url` to `dest-path` using `wget`."
+  [url dest-path]
+  {:pre [(string? url) (string? dest-path) (str/starts-with? url "http")]}
+  (steps/step (format "Download %s -> %s" url dest-path)
+    (delete-file-if-exists! dest-path)
+    (sh/sh {:quiet? true} "wget" "--quiet" "--no-cache" "--output-document" dest-path url)
+    (assert-file-exists dest-path)))
diff --git a/bin/release/src/release/elastic_beanstalk.clj b/bin/release/src/release/elastic_beanstalk.clj
index 2cf695d24e6..a64bb737c17 100644
--- a/bin/release/src/release/elastic_beanstalk.clj
+++ b/bin/release/src/release/elastic_beanstalk.clj
@@ -36,9 +36,7 @@
 (defn- validate-json-docker-tag []
   (u/step (format "Check that Dockerrun.aws.json Docker tag is %s" (c/docker-tag))
     (u/step "Download archive"
-      (u/delete-file-if-exists! archive-path)
-      (u/sh {:quiet? true} "wget" "--quiet" "--no-cache" "--output-document" archive-path
-            (c/artifact-download-url "metabase-aws-eb.zip")))
+      (u/download-file! (c/artifact-download-url "metabase-aws-eb.zip") archive-path))
     (u/step "Unzip archive"
       (u/delete-file-if-exists! archive-temp-dir)
       (u/sh "unzip" (u/assert-file-exists archive-path) "-d" archive-temp-dir))
diff --git a/bin/release/src/release/uberjar.clj b/bin/release/src/release/uberjar.clj
index 3d7addf0859..da68dd70550 100644
--- a/bin/release/src/release/uberjar.clj
+++ b/bin/release/src/release/uberjar.clj
@@ -27,8 +27,7 @@
         (common.http/check-url-exists url)
         (u/step (format "Check hash of %s" url)
           (let [temp-location "/tmp/metabase.jar"]
-            (u/delete-file-if-exists! temp-location)
-            (u/sh {:quiet? true} "wget" "--quiet" "--no-cache" "--output-document" temp-location url)
+            (u/download-file! url temp-location)
             (let [uberjar-hash (hash/sha-256-sum c/uberjar-path)
                   url-hash     (hash/sha-256-sum temp-location)]
               (u/announce "Hash of local metabase.jar is %s" uberjar-hash)
diff --git a/docs/developers-guide-osx.md b/docs/developers-guide-osx.md
index 5da84029042..17179f159da 100644
--- a/docs/developers-guide-osx.md
+++ b/docs/developers-guide-osx.md
@@ -123,10 +123,11 @@ The following steps are prereqs for *releasing* the Mac App:
         -p <secret_password>
         ```
 
-1)  Install Clojure CLI
+1) Install Clojure CLI. See [the instructions on
+clojure.org](https://www.clojure.org/guides/getting_started) for more details.
 
     ```bash
-    brew install clojure
+    brew install clojure/tools/clojure
     ```
 
 </details>
@@ -139,12 +140,6 @@ After following the configuration steps above, to build and release the app you
 
 1. Make sure you're on the appropriate release branch locally. The script reads the version number from the most recent tag
 
-1. Copy latest uberjar to the Mac App build directory
-
-   ```bash
-   cp path/to/metabase.jar OSX/Resources/metabase.jar
-   ```
-
 1. Bundle entire app, and upload to s3
 
    ```bash
-- 
GitLab