Function: eca-process--download-server

eca-process--download-server is a natively compiled function defined in eca-process.el.

Signature

(eca-process--download-server ON-DOWNLOADED VERSION &optional ON-ERROR)

Documentation

Download eca server of VERSION calling ON-DOWNLOADED when success.

On failure calls ON-ERROR with the error when provided (at most once, even for errors in async download callbacks), otherwise just reports the error.

Source Code

;; Defined in /nix/store/jklqzw6pzz6j4jrbv8wy0iwgi7jlrbld-emacs-packages-deps/share/emacs/site-lisp/elpa/eca-20260928.1827/eca-process.el
(defun eca-process--download-server (on-downloaded version &optional on-error)
  "Download eca server of VERSION calling ON-DOWNLOADED when success.
On failure calls ON-ERROR with the error when provided (at most
once, even for errors in async download callbacks), otherwise just
reports the error."
  (-let ((url (eca-process--download-url version))
         ((download-path . store-path) (eca-process--download-and-store-path))
         (old-path (concat eca-server-install-path ".old"))
         (temp-extract-dir (concat (f-parent eca-server-install-path) "-temp"))
         (download-fn (pcase eca-server-download-method
                        ('url-retrieve #'eca--url-retrieve-download-file)
                        ('curl #'eca--curl-download-file)
                        (_ (error (eca-error (format "Unknown download method '%s' for eca-server-download-method" eca-server-download-method))))))
         (failed nil)
         (fail-fn nil))
    (setq fail-fn (lambda (err)
                    (unless failed
                      (setq failed t)
                      (if on-error
                          (funcall on-error err)
                        (eca-error "Failed to download eca server %s" err)))))
    (condition-case err
        (progn
          ;; Clean up any old files from previous updates
          (eca-process--cleanup-old-server)
          (when (f-exists? download-path) (f-delete download-path))
          (when (f-exists? temp-extract-dir) (f-delete temp-extract-dir t))
          (mkdir (f-parent download-path) t)
          (eca-info "Downloading eca server from %s to %s..." url download-path)
          (funcall
           download-fn
           :url url
           :path download-path
           :on-error fail-fn
           :on-done (lambda ()
                      (condition-case err
                          (progn
                            (eca-info "Downloaded eca. Checking sha256...")
                            (eca-process--check-sha256 download-path url version)
                            (eca-info "Unzipping eca...")
                            (unless (and eca-unzip-script (funcall eca-unzip-script))
                              (error "Unable to find `unzip' or `powershell' on the path, please customize `eca-unzip-script'"))
                            ;; Extract to temp directory first
                            (mkdir temp-extract-dir t)
                            (shell-command (format (funcall eca-unzip-script) download-path temp-extract-dir))
                            (let ((new-binary (eca-process--find-extracted-binary
                                               temp-extract-dir (f-filename store-path))))
                              (unless new-binary
                                (error "Expected binary not found after extraction: %s"
                                       (f-join temp-extract-dir (f-filename store-path))))
                              ;; Rename old binary to .old if it exists
                              ;; On Windows, running executables can be renamed but not deleted
                              (when (f-exists? store-path)
                                (when (f-exists? old-path)
                                  (condition-case nil (f-delete old-path) (error nil)))
                                (rename-file store-path old-path))
                              ;; Move new binary into place
                              (rename-file new-binary store-path))
                            ;; Clean up temp directory
                            (when (f-exists? temp-extract-dir)
                              (condition-case nil (f-delete temp-extract-dir t) (error nil)))
                            ;; Try to delete old binary (may fail if still in use, that's ok)
                            (when (f-exists? old-path)
                              (condition-case nil (f-delete old-path) (error nil)))
                            (f-write-text version 'utf-8 eca-server-version-file-path)
                            (set-file-modes store-path #o0700)
                            (eca-info "Installed eca successfully!")
                            (funcall on-downloaded))
                        (error (funcall fail-fn err))))))
      (error (funcall fail-fn err)))))