branch: externals/ssh-deploy commit 05ed4c8c8433d1d340e7e030c6d60ee0860b02e1 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Made changes to comply with MELPA guidelines. --- README.md | 26 +++++++++++------ ssh-deploy.el | 90 ++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 67 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 4f2b3f4..7c3bc57 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,12 @@ The `ssh-deploy` plugin makes it possible to effortlessly deploy local files to `ssh-deploy` works with `DirectoryVariables` so you can have different deploy setups in different ways for different folders. +The idea for this plug-in was to mimic the behavior of PhpStorm deployment functionality. + This application is made by Christian Johansson <christ...@cvj.se> 2016 and is licensed under GNU General Public License 3. -## An example +## A setup example * Download ssh-deploy and place it at `~/.emacs.d/ssh-deploy/`. @@ -18,25 +20,33 @@ This application is made by Christian Johansson <christ...@cvj.se> 2016 and is l (ssh-deploy-root-remote . "/Volumes/myserver.com/MySite/") (ssh-deploy-on-explicity-save . t)))) ``` + * And add this to your *emacs-init-script*: ``` elisp ;; ssh-deploy - prefix = C-c C-z, u = upload, d = download, x = diff (add-to-list 'load-path "~/.emacs.d/ssh-deploy/") -(use-package ssh-deploy) +(use-package ssh-deploy + :config + (add-hook 'after-save-hook (lambda() (if ssh-deploy-on-explicity-save (ssh-deploy-upload-handler)) )) + (global-set-key (kbd "C-c C-z u") (lambda() (interactive)(ssh-deploy-upload-handler) )) + (global-set-key (kbd "C-c C-z d") (lambda() (interactive)(ssh-deploy-download-handler)(revert-buffer) )) + (global-set-key (kbd "C-c C-z x") (lambda() (interactive)(ssh-deploy-diff-handler) ))) ``` -* Now when you save a file somewhere under the root `/Users/username/Web/MySite/`, the script will launch and deploy the file with the remote server. + +* Now when you save a file somewhere under the root `/Users/username/Web/MySite/`, the script will launch and deploy the file with the remote server. * If you press `C-c C-z x` you will launch a `ediff` session showing differences between local file and remote file using `tramp`. * If you press `C-c C-z u` you will upload local file to remote host. * If you press `C-c C-z d` you will download file from remote host and reload current buffer. -If you want to change the key-binding prefix you only need to set the variable `ssh-deploy-key-binding-prefix` with something like this: +The above configuration uses the plugin `use-package` which I highly recommend. -``` elisp -(setq ssh-deploy-key-binding-prefix "your-key-binding-here") -``` +## TODO -The above configuration uses the plugin `use-package` which I highly recommend. +* Add a message if remote contents has changed since last upload. +* Add support for specific ssh identity files +* Add support for specific ssh ports +* Add support for directories ## Read more * <https://www.emacswiki.org/emacs/DirectoryVariables> diff --git a/ssh-deploy.el b/ssh-deploy.el index 04fa6f7..5e729ee 100644 --- a/ssh-deploy.el +++ b/ssh-deploy.el @@ -5,9 +5,9 @@ ;; Author: Christian Johansson <christ...@cvj.se> ;; Maintainer: Christian Johansson <christ...@cvj.se> ;; Created: 5 Jul 2016 -;; Modified: 6 Jul 2016 +;; Modified: 11 Jul 2016 ;; Version: 1.0 -;; Keywords: ssh deploy package +;; Keywords: SSH deploy package ;; URL: https://github.com/cjohansson/emacs-ssh-deploy ;; This program is free software; you can redistribute it and/or @@ -33,95 +33,103 @@ ;; scp and ediff. By setting the variables (globally or per directory): ;; `ssh-deploy-root-local`,`ssh-deploy-root-remote`, ;; `ssh-deploy-on-explicity-save` you can setup a directory for -;; SSH deploy. The variable `ssh-deploy-key-binding-prefix` controls -;; they key-binding-prefix for `u` (upload), `x` (difference) or -;; `d` (download). The default key-binding-prefix is "C-c C-z". ' +;; SSH deploy. +;; +;; To setup hook on explicit save do this: +;; (add-hook 'after-save-hook (lambda() (if ssh-deploy-on-explicity-save (ssh-deploy-upload-handler)) )) +;; +;; To set key-bindings do something like this: +;; (global-set-key (kbd "C-c C-z u") (lambda() (interactive)(ssh-deploy-upload-handler) )) +;; (global-set-key (kbd "C-c C-z d") (lambda() (interactive)(ssh-deploy-download-handler)(revert-buffer) )) +;; (global-set-key (kbd "C-c C-z x") (lambda() (interactive)(ssh-deploy-diff-handler) )) ;; ;; Please see README.md from the same repository for documentation. ;;; Code: +(defgroup ssh-deploy nil + "Upload, download and difference handler for files on remote hosts via SSH." +:group 'ssh-deploy) + ;; Variables (defcustom ssh-deploy-root-local nil "String variable of local root, nil by default." :type 'string :group 'ssh-deploy) + (defcustom ssh-deploy-root-remote nil "String variable of remote root, nil by default." :type 'string :group 'ssh-deploy) + (defcustom ssh-deploy-on-explicity-save nil "Boolean variable if deploy should be made on explicit save, nil by default." :type 'boolean - :group 'ssh-deploy - ) -(defcustom ssh-deploy-key-binding-prefix "C-c C-z" - "String variable of prefix 'key-binding', default is C-c C-z." - :type 'string - :group 'ssh-deploy -) + :group 'ssh-deploy) + ;; Functions -(defun ssh-deploy-diff (localRootRaw remoteRoot) - "Find differences between the path LOCALROOTRAW with REMOTEROOT via ssh." +(defun ssh-deploy-diff (local-root-raw remote-root) + "Find differences between the path LOCAL-ROOT-RAW with REMOTE-ROOT via ssh." (let ((filename (shell-quote-argument buffer-file-name)) - (localRoot (shell-quote-argument localRootRaw))) - (let ((remotePath (concat "/" remoteRoot (replace-regexp-in-string localRoot "" filename)))) - (if (string-match localRoot filename) + (local-root (shell-quote-argument local-root-raw))) + (let ((remote-path (concat "/" remote-root (replace-regexp-in-string local-root "" filename)))) + (if (string-match local-root filename) (progn - (message "Comparing file '%s' to '%s'.." filename remotePath) - (ediff filename remotePath)))))) + (message "Comparing file '%s' to '%s'.." filename remote-path) + (ediff filename remote-path)))))) + (defun ssh-deploy-is-not-empty-string (string) "Return true if the STRING is not empty and not nil. Expects string." (and (not (null string)) (not (zerop (length string))))) -(defun ssh-deploy (localRootRaw remoteRootRaw uploadOrDownload) - "Upload/Download the path LOCALROOTRAW with REMOTEROOTRAW via ssh according to UPLOADORDOWNLOAD." + +(defun ssh-deploy (local-root-raw remote-root-raw upload-or-download) + "Upload/Download the path LOCAL-ROOT-RAW with REMOTE-ROOT-RAW via SSH according to UPLOAD-OR-DOWNLOAD." (let ((filename (shell-quote-argument buffer-file-name)) - (localRoot (shell-quote-argument localRootRaw)) - (remoteRoot (shell-quote-argument remoteRootRaw))) - (let ((remotePath (concat remoteRoot (replace-regexp-in-string localRoot "" filename)))) - (if (string-match localRoot filename) + (local-root (shell-quote-argument local-root-raw)) + (remote-root (shell-quote-argument remote-root-raw))) + (let ((remote-path (concat remote-root (replace-regexp-in-string local-root "" filename)))) + (if (string-match local-root filename) (progn - (if (not (null uploadOrDownload)) + (if (not (null upload-or-download)) (progn - (message "Uploading file '%s' to '%s'.." filename remotePath) - (let ((command (concat "scp " filename " " remotePath))) + (message "Uploading file '%s' to '%s'.." filename remote-path) + (let ((command (concat "scp " filename " " remote-path))) (message "Upload command: '%s'" command) (if (= (shell-command command) 0) - (message "Successfully uploaded '%s' to '%s'" filename remotePath) - (message "Failed to upload '%s' to '%s'" filename remotePath)))) + (message "Successfully uploaded '%s' to '%s'" filename remote-path) + (message "Failed to upload '%s' to '%s'" filename remote-path)))) (progn - (message "Downloading file '%s' to '%s'.." remotePath filename) - (let ((command (concat "scp " remotePath " " filename))) + (message "Downloading file '%s' to '%s'.." remote-path filename) + (let ((command (concat "scp " remote-path " " filename))) (message "Upload command: '%s'" command) (if (= (shell-command command) 0) - (message "Successfully downloaded '%s' to '%s'" remotePath filename) - (message "Failed to download '%s' to '%s'" remotePath filename) + (message "Successfully downloaded '%s' to '%s'" remote-path filename) + (message "Failed to download '%s' to '%s'" remote-path filename) ))))))))) + +;;;### autoload (defun ssh-deploy-upload-handler () "Upload current file if it is configured for SSH deployment." (if (and (ssh-deploy-is-not-empty-string ssh-deploy-root-local) (ssh-deploy-is-not-empty-string ssh-deploy-root-remote) (ssh-deploy-is-not-empty-string buffer-file-name)) (ssh-deploy ssh-deploy-root-local ssh-deploy-root-remote t) )) + +;;;### autoload (defun ssh-deploy-download-handler () "Download current file if it is configured for SSH deployment." (if (and (ssh-deploy-is-not-empty-string ssh-deploy-root-local) (ssh-deploy-is-not-empty-string ssh-deploy-root-remote) (ssh-deploy-is-not-empty-string buffer-file-name)) (ssh-deploy ssh-deploy-root-local ssh-deploy-root-remote nil) )) + +;;;### autoload (defun ssh-deploy-diff-handler () "Compare current file with remote if it is configured for SSH deployment." (if (and (ssh-deploy-is-not-empty-string ssh-deploy-root-local) (ssh-deploy-is-not-empty-string ssh-deploy-root-remote) (ssh-deploy-is-not-empty-string buffer-file-name)) (ssh-deploy-diff ssh-deploy-root-local ssh-deploy-root-remote) )) -;; Hooks -(add-hook 'after-save-hook (lambda() (if ssh-deploy-on-explicity-save (ssh-deploy-upload-handler)) )) - -;; Key-bindings -(global-set-key (kbd (concat ssh-deploy-key-binding-prefix " u")) (lambda() (interactive)(ssh-deploy-upload-handler) )) -(global-set-key (kbd (concat ssh-deploy-key-binding-prefix " d")) (lambda() (interactive)(ssh-deploy-download-handler)(revert-buffer) )) -(global-set-key (kbd (concat ssh-deploy-key-binding-prefix " x")) (lambda() (interactive)(ssh-deploy-diff-handler) )) (provide 'ssh-deploy) ;;; ssh-deploy.el ends here