-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): Add custom command (#195)
* feat(command): Add custom command * Changelog * Fix desc * add doc * fix options
- Loading branch information
Showing
11 changed files
with
204 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (C) 2023 Jen-Chieh Shen | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3, or (at your option) | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with GNU Emacs; see the file COPYING. If not, write to the | ||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
* Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
"use strict"; | ||
|
||
exports.command = ['command [names..]', 'cmd [names..]']; | ||
exports.desc = 'Run custom command'; | ||
exports.builder = yargs => yargs | ||
.positional( | ||
'[names..]', { | ||
description: 'list of function commands to execute', | ||
type: 'array', | ||
}); | ||
|
||
exports.handler = async (argv) => { | ||
await UTIL.e_call(argv, 'core/command' | ||
, argv.names); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
;;; core/command.el --- Run custom command -*- lexical-binding: t; -*- | ||
|
||
;;; Commentary: | ||
;; | ||
;; Command use to run custom command | ||
;; | ||
;; $ eask command [names..] | ||
;; | ||
;; | ||
;; Positionals: | ||
;; | ||
;; [names..] name of the function command | ||
;; | ||
|
||
;;; Code: | ||
|
||
(let ((dir (file-name-directory (nth 1 (member "-scriptload" command-line-args))))) | ||
(load (expand-file-name "_prepare.el" | ||
(locate-dominating-file dir "_prepare.el")) | ||
nil t)) | ||
|
||
(defun eask--command-desc (name) | ||
"Return command's description by its command's NAME." | ||
(car (split-string (documentation name) "\n"))) | ||
|
||
(defun eask--print-commands () | ||
"Print all available commands." | ||
(eask-msg "available via `eask command`") | ||
(eask-msg "") | ||
(let* ((keys (reverse eask-commands)) | ||
(offset (eask-seq-str-max keys)) | ||
(fmt (concat " %-" (eask-2str offset) "s %s"))) | ||
(dolist (key keys) | ||
(eask-msg fmt key (eask--command-desc key))) | ||
(eask-msg "") | ||
(eask-info "(Total of %s available script%s)" (length keys) | ||
(eask--sinr keys "" "s")))) | ||
|
||
(defun eask--execute-command (name) | ||
"Execute the command by NAME." | ||
(eask-info "[RUN]: %s" name) | ||
(funcall (eask-intern name))) | ||
|
||
(defun eask--unmatched-commands (commands) | ||
"Return a list of COMMANDS that cannot be found in `eask-commands'." | ||
(let (unmatched) | ||
(dolist (command commands) | ||
(unless (memq (eask-intern command) eask-commands) | ||
(push command unmatched))) | ||
unmatched)) | ||
|
||
(eask-start | ||
(cond ((null eask-commands) | ||
(eask-info "(No command specified)") | ||
(eask-help "core/command")) | ||
((eask-all-p) | ||
(dolist (name (reverse eask-commands)) | ||
(eask--execute-command name))) | ||
((when-let ((commands (eask-args))) | ||
(if-let ((unmatched (eask--unmatched-commands commands))) | ||
(progn ; if there are unmatched commands, don't even try to execute | ||
(eask-info "(Missing command%s: `%s`)" | ||
(eask--sinr unmatched "" "s") | ||
(mapconcat #'identity unmatched ", ")) | ||
(eask-msg "") | ||
(eask--print-commands)) | ||
(dolist (command commands) | ||
(eask--execute-command command)) | ||
t))) | ||
(t (eask--print-commands)))) | ||
|
||
;;; core/command.el ends here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
💡 Make sure you have specify a (eask-defcommand ..) function inside your Eask file! | ||
|
||
[+] (eask-defcommand my-command | ||
[+] "This is a test command." | ||
[+] (message "My test commnad!")) |