2017-02-20 13:40:42 -05:00
|
|
|
;;; system.el
|
|
|
|
(provide 'core-lib-system)
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-system-os ()
|
|
|
|
"Returns the OS: arch, debian, macos, general linux, cygwin or windows."
|
|
|
|
(let ((gnu-linux-p (eq system-type 'gnu/linux)))
|
|
|
|
(cond ((and gnu-linux-p (file-exists-p "/etc/arch-release"))
|
|
|
|
'arch)
|
|
|
|
((and gnu-linux-p (file-exists-p "/etc/debian_version"))
|
|
|
|
'debian)
|
|
|
|
(gnu-linux-p
|
|
|
|
'linux)
|
|
|
|
((eq system-type 'darwin)
|
|
|
|
'macos)
|
|
|
|
((memq system-type '(windows-nt cygwin))
|
|
|
|
'windows)
|
|
|
|
(t (error "Unknown OS: %s" system-type)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
2017-03-20 02:45:31 -04:00
|
|
|
(defun doom-sh (command &rest args)
|
2017-02-21 03:45:24 -05:00
|
|
|
"Runs a shell command and prints any output to the DOOM buffer."
|
2017-03-20 15:57:05 -04:00
|
|
|
(let ((cmd-list (split-string command " ")))
|
2017-03-23 02:13:11 -04:00
|
|
|
(cond ((equal (car cmd-list) "sudo")
|
2017-04-17 02:17:10 -04:00
|
|
|
(apply #'doom-sudo (string-join (cdr cmd-list) " ") args))
|
2017-03-23 02:13:11 -04:00
|
|
|
((let ((bin (executable-find "npm")))
|
|
|
|
(and (file-exists-p bin)
|
|
|
|
(not (file-writable-p bin))))
|
2017-04-17 02:17:10 -04:00
|
|
|
(apply #'doom-sudo (string-join cmd-list " ") args))
|
2017-03-23 02:13:11 -04:00
|
|
|
(t
|
2017-04-17 02:17:10 -04:00
|
|
|
(princ (shell-command-to-string (apply #'format command args)))))))
|
2017-02-20 13:40:42 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-03-20 02:45:31 -04:00
|
|
|
(defun doom-sudo (command &rest args)
|
2017-02-21 03:45:24 -05:00
|
|
|
"Like `doom-sh', but runs as root (prompts for password)."
|
2017-03-20 15:57:05 -04:00
|
|
|
(let ((tramp-verbose 2))
|
|
|
|
(with-current-buffer (get-buffer-create "*doom-sudo*")
|
2017-03-20 02:45:31 -04:00
|
|
|
(unless (string-prefix-p "/sudo::/" default-directory)
|
|
|
|
(cd "/sudo::/"))
|
2017-04-17 02:17:10 -04:00
|
|
|
(princ (shell-command-to-string (apply #'format command args))))))
|
2017-02-20 13:40:42 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-fetch (fetcher location dest)
|
|
|
|
"Clone a remote version-controlled repo at REPO-URL to PATH, if it exists.
|
|
|
|
Requires the corresponding client, e.g. git for git repos, hg for mercurial,
|
|
|
|
etc."
|
|
|
|
(let* ((command (pcase fetcher
|
2017-03-20 15:57:05 -04:00
|
|
|
(:github "git clone --depth 1 --recursive https://github.com/%s.git")
|
|
|
|
(:git "git clone --depth 1 --recursive %s")
|
2017-02-20 13:40:42 -05:00
|
|
|
(:gist "git clone https://gist.github.com/%s.git")
|
2017-04-17 02:19:37 -04:00
|
|
|
;; TODO Add hg
|
2017-02-20 13:40:42 -05:00
|
|
|
(_ (error "%s is not a valid fetcher" fetcher))))
|
2017-04-17 02:19:37 -04:00
|
|
|
(argv (split-string command " " t))
|
|
|
|
(args (format (string-join (cdr argv) " ") location))
|
2017-02-20 13:40:42 -05:00
|
|
|
(bin (executable-find (car argv)))
|
2017-03-20 15:57:05 -04:00
|
|
|
(dest (expand-file-name dest)))
|
2017-02-20 13:40:42 -05:00
|
|
|
(unless bin
|
|
|
|
(error "%s couldn't be found" command))
|
2017-03-20 15:57:05 -04:00
|
|
|
(unless (file-directory-p dest)
|
2017-04-17 02:19:37 -04:00
|
|
|
(funcall (if noninteractive
|
|
|
|
(lambda (&rest args) (princ (shell-command-to-string args)))
|
|
|
|
#'async-shell-command)
|
|
|
|
(format "%s %s %s" bin args (shell-quote-argument dest)))
|
|
|
|
(message! "Cloning %s -> %s" location dest))))
|
2017-03-20 02:45:31 -04:00
|
|
|
|