Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-evitt committed Apr 19, 2016
1 parent 8f2dfba commit e6cde6d
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 29 deletions.
6 changes: 3 additions & 3 deletions deploy/core/node_modules/shelljs/shell.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

193 changes: 167 additions & 26 deletions src/lt/objs/deploy.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,64 @@

(def shell (load/node-module "shelljs"))
(def fs (js/require "fs"))
; Update the comment for the following to describe the full situation:
(def original-fs (js/require "original-fs")) ;; Required per [fs - reading asar files fails · Issue #1658 · atom/electron](https://github.com/atom/electron/issues/1658)
(def zlib (js/require "zlib"))
(def request (load/node-module "request"))
(def tar (load/node-module "tar"))
(def tar (load/node-module "tar-fs"))
(def zip (load/node-module "node-stream-zip"))
(def home-path (files/lt-home ""))
;; TODO: get-proxy
;; (def get-proxy (.-App.getProxyForURL (js/require "nw.gui")))
(def get-proxy)

(defn tar-path [v]
; TODO: Cleanup platform name code

; TODO: Rename to release-file-extension
(def tar-extension (if (platform/win?) ".zip" ".tar.gz"))

(defn get-release-download-url
"Retrieve the download URL for the specified release version and this computer's platform."
[v cb]
(let [release-info-url (str "https://api.github.com/repos/LightTable/LightTable/releases/tags/"
v)]
(fetch/xhr release-info-url
{}
(fn [data]
(let [parsed-data (js/JSON.parse data)
assets (.-assets parsed-data)
platform-name (name platform/platform)
platform-asset-name (str "lighttable-" v "-" platform-name tar-extension)
platform-asset (first (filter #(= (.-name %) platform-asset-name)
assets))
download-url (.-browser_download_url platform-asset)
]
(cb download-url)
)))))

;(get-release-download-url "0.8.1" (fn [u] (console/log u)))

(defn tar-path [v cb]
(if (cache/fetch :edge)
(str "https://api.github.com/repos/LightTable/LightTable/tarball/master")
(str "https://api.github.com/repos/LightTable/LightTable/tarball/" v)))
(cb (str "https://api.github.com/repos/LightTable/LightTable/tarball/master")) ; TODO: Fix to use CI build artifact.
(get-release-download-url v cb)))

;(tar-path "0.8.1" (fn [url] (console/log url)))

;

(def version-regex #"^\d+\.\d+\.\d+(-.*)?$")

(defn get-versions []
(let [vstr (:content (files/open-sync (files/lt-home "core/version.json")))]
(js->clj (.parse js/JSON vstr) :keywordize-keys true)))

(defn proxy? []
(let [p (get-proxy (tar-path "0.5.0"))]
(when (str-contains? p "PROXY")
(-> p
(string/split " ")
(second)))))
;; (defn proxy? []
;; (let [p (get-proxy (tar-path "0.5.0"))]
;; (when (str-contains? p "PROXY")
;; (-> p
;; (string/split " ")
;; (second)))))

(def version-timeout (* 60 60 1000))
(def version (get-versions))
Expand Down Expand Up @@ -85,38 +118,145 @@
(throw (js/Error. (str "Error downloading: " from " status code: " (.-statusCode resp)))))))
(.pipe out))))

(def temp-download-tar-path (str home-path "/tmp" tar-extension))

; TODO: Rename to `download-release-file`?
(defn download-zip [ver cb]
(let [n (notifos/working (str "Downloading version " ver " .."))]
(download-file (tar-path ver) (str home-path "/tmp.tar.gz") (fn [e r body]
(notifos/done-working)
(cb e r body)))))
(tar-path ver
(fn [url]
(console/log url)
(download-file url
temp-download-tar-path
(fn [e r body]
(notifos/done-working)
(cb e r body)))))))

;(download-zip "0.8.1" (fn [e r body] nil))

(defn untar [from to cb]
(let [t (.createReadStream fs from)]
(.. t
(pipe (.createGunzip zlib))
(pipe (.Extract tar (js-obj "path" to)))
(pipe (.extract tar to (js-obj "fs" original-fs)))
(on "end" cb))))

(comment
(untar temp-download-tar-path
(str home-path "/tmp")
(fn [] (console/log "Called-back!")))
)

(defn move-tmp []
(let [parent-dir (first (files/full-path-ls (str home-path "/tmp/")))]
(doseq [file (files/full-path-ls (str parent-dir "/deploy/"))]
; TODO: Support Linux
(defn release-file-sub-dir
[v]
(str "lighttable-"
v
"-"
(name platform/platform)
(case platform/platform
:windows "/resources"
:mac "/LightTable.app/Contents/Resources"
:linux nil) ; TODO: Finish
"/app/"))

;(release-file-sub-dir "0.8.1")


(defn unzip [from to v cb]
(let [z (zip. (js-obj "file" from "storeEntries" true))]
(aset js/process "noAsar" true)
(if (not (files/exists? to)) (files/mkdir to))
(.on z "ready" (fn []
(.extract z
(release-file-sub-dir v)
to
(fn [error count-]
(.log js/console error)
(console/log (str "Zip file entries extracted: " count-))
(cb)))) )))

; This didn't work initially because of the *lighttable-0.8.1-windows\resources\atom.asar* file but now it works because only the *lighttable-version-platform/resources/app* sub-directory is extracted:
;(unzip temp-download-tar-path (str home-path "/tmp") "0.8.1" (fn [] (console/log "Called-back!")))


(defn move-tmp-tar
[v]
(let [parent-dir (str home-path "/tmp/" (release-file-sub-dir v))]
(doseq [file (files/full-path-ls parent-dir)]
(.cp shell "-rf" file home-path)))
(.rm shell "-rf" (str home-path "/tmp*"))
;; (.rm shell "-rf" (str home-path "/tmp"))
;; (.rm shell "-f" temp-download-tar-path)
)

;; (let [parent-dir home-path]
;; (files/full-path-ls parent-dir))

;; (let [parent-dir (str home-path "/tmp/" (release-file-sub-dir "0.8.1"))]
;; (files/full-path-ls parent-dir))

; This worked:
;; (.cp shell "-rf" "/Users/kenny/@code/LightTable/builds/lighttable-0.8.1-mac/LightTable.app/Contents/Resources/app/tmp/lighttable-0.8.1-mac/LightTable.app/Contents/resources/app/text.txt" home-path)

; This hung LT when I was manually eval-ing all of the new code but worked with a clean source build:
;; (.cp shell "-rf" "/Users/kenny/@code/LightTable/builds/lighttable-0.8.1-mac/LightTable.app/Contents/Resources/app/tmp/lighttable-0.8.1-mac/LightTable.app/Contents/resources/app/core" home-path)

; This worked:
;; (.rm shell "-rf" (str home-path "/text.txt"))

; This hung LT at first but worked when I commented out the `rm` call and then with the explicit `rm` calls and then it kinda worked as originally written when I swapped the 'fs' required library (but it didn't delete the temp directory or file):
;; (move-tmp-tar "0.8.1")

; This doesn't work:
;; (.rm shell "-rf" (str home-path "/tmp"))

; This doesn't work either:
;; (.rm shell "-rf" (str home-path "/tmp/*"))

;; (.existsSync fs (str home-path "/tmp")) ; => true
;; (.rm shell "-r" (str home-path "/tmp")) ; Does nothing
;; (.log js/console (.statSync fs (str home-path "/tmp")))
;; (.isFile (.statSync fs (str home-path "/tmp"))) ; => false
;; (.isDirectory (.statSync fs (str home-path "/tmp"))) ; => true


(defn move-tmp-zip []
(let [parent-dir (str home-path "/tmp/")]
(doseq [file (files/full-path-ls parent-dir)]
(.cp shell "-rf" file home-path)))
(.rm shell "-rf" (str home-path "/tmp*")))

;(move-tmp-zip)


;home-path
;; (files/full-path-ls (str home-path "/tmp/"))

(defn fetch-and-deploy [ver]
(download-zip ver (fn []
(notifos/working "Extracting update...")
(untar (str home-path "/tmp.tar.gz") (str home-path "/tmp")
(fn []
(move-tmp)
(notifos/done-working)
(set! version (assoc version :version ver))
(popup/popup! {:header "Light Table has been updated!"
:body (str "Light Table has been updated to " ver "! Just
restart to get the latest and greatest.")
:buttons [{:label "ok"}]}))))))
(let [base-cb (fn []
(notifos/done-working)
(set! version (assoc version :version ver))
(popup/popup! {:header "Light Table has been updated!"
:body (str "Light Table has been updated to " ver "! Just
restart to get the latest and greatest.")
:buttons [{:label "ok"}]}))]
(if (platform/win?)
(unzip temp-download-tar-path
(str home-path "/tmp")
ver
(fn []
(move-tmp-zip)
(base-cb)))
(untar temp-download-tar-path
(str home-path "/tmp")
(fn []
(move-tmp-tar ver)
(base-cb))))))))

;; (fetch-and-deploy "0.8.1")

(def tags-url "https://api.github.com/repos/LightTable/LightTable/tags")

Expand All @@ -141,6 +281,7 @@
sort
last)))


(defn check-version [& [notify?]]
(fetch/xhr tags-url {}
(fn [data]
Expand Down

0 comments on commit e6cde6d

Please sign in to comment.