Skip to content

Commit

Permalink
feat: Init from source file
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Aug 23, 2023
1 parent 8a2a7bd commit 62efee5
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 7 deletions.
7 changes: 2 additions & 5 deletions cmds/core/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ exports.handler = async (argv) => {
if (argv.from) {
switch (argv.from) {
case 'cask':
await UTIL.e_call(argv, 'init/cask'
, UTIL.def_flag(argv.from, '--from', argv.from)
, argv.files);
break;
case 'keg':
await UTIL.e_call(argv, 'init/keg'
case 'source':
await UTIL.e_call(argv, 'init/' + argv.from
, UTIL.def_flag(argv.from, '--from', argv.from)
, argv.files);
break;
Expand Down
7 changes: 5 additions & 2 deletions lisp/_prepare.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ will return `lint/checkdoc' with a dash between two subcommands."
(defun eask-special-p ()
"Return t if the command that can be run without Eask-file existence."
(member (eask-command) '("init/cask" "init/keg"
"init/source"
"cat" "keywords"
"generate/ignore" "generate/license")))

Expand Down Expand Up @@ -1356,7 +1357,8 @@ Argument ARGS are direct arguments for functions `eask-error' or `eask-warn'."
Arguments FNC and ARGS are used for advice `:around'."
(let ((msg (eask--ansi 'error (apply #'format-message args))))
(eask--unsilent (eask-msg "%s" msg))
(unless eask--ignore-error-p
(eask--unsilent (eask-msg "%s" msg)))
(run-hook-with-args 'eask-on-error-hook 'error msg)
(eask--trigger-error))
(when debug-on-error (apply fnc args)))
Expand All @@ -1368,7 +1370,8 @@ Arguments FNC and ARGS are used for advice `:around'."
Arguments FNC and ARGS are used for advice `:around'."
(let ((msg (eask--ansi 'warn (apply #'format-message args))))
(eask--unsilent (eask-msg "%s" msg))
(unless eask--ignore-error-p
(eask--unsilent (eask-msg "%s" msg)))
(run-hook-with-args 'eask-on-warning-hook 'warn msg))
(eask--silent (apply fnc args)))

Expand Down
6 changes: 6 additions & 0 deletions lisp/help/init/source
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

💡 Make sure you have a valid elisp file in your directory

💡 Or specify elisp file explicitly, like:

$ eask init --from=source /path/to/source.el
107 changes: 107 additions & 0 deletions lisp/init/source.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
;;; init/source.el --- Initialize Eask from Source -*- lexical-binding: t; -*-

;;; Commentary:
;;
;; Commmand use to convert elisp-file to Eask-file
;;
;; $ eask init --from source
;;

;;; 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--convert-source (filename)
"Convert elisp source FILENAME to Eask."
(let* ((filename (expand-file-name filename))
(file (file-name-nondirectory (eask-root-del filename)))
(new-file (concat "Eask-" (file-name-sans-extension file)))
(new-filename (expand-file-name new-file))
(pkg-desc (with-temp-buffer
(insert-file-contents filename)
(eask-ignore-errors (package-buffer-info)))) ; Read it!
(converted))
(eask-with-progress
(format "Converting file `%s` to `%s`... " file new-file)
(eask-with-verbosity 'debug
(cond ((not (string-suffix-p ".el" file))
(eask-debug "✗ Invalid elisp filename, the file should end with `.el`"))
((file-exists-p new-filename)
(eask-debug "✗ The file `%s` already presented" new-file))
(t
(when pkg-desc
(with-current-buffer (find-file new-filename)
(goto-char (point-min))



(let* ((eask-package-desc pkg-desc)
(package-name (package-desc-name pkg-desc))
(version (package-desc-version pkg-desc))
(version (package-version-join version))
(description (package-desc-summary pkg-desc))
(website (eask-package-desc-url))
(keywords (eask-package-desc-keywords))
(keywords (string-join keywords "\" \""))
(reqs (package-desc-reqs pkg-desc))
(content (format
"(package \"%s\"
\"%s\"
\"%s\")
(website-url \"%s\")
(keywords \"%s\")
(package-file \"%s\")
(script \"test\" \"echo \\\"Error: no test specified\\\" && exit 1\")
(source \"gnu\")
"
package-name version description website keywords
file)))
(insert content)

(dolist (req reqs)
(let* ((req-name (car req))
(req-version (cdr req))
(req-version (package-version-join (car req-version))))
(insert (format "(depends-on \"%s\" \"%s\")\n" req-name req-version)))))

(save-buffer))
(setq converted t)))))
(if converted "done ✓" "skipped ✗"))
converted))

(eask-start
(let* ((patterns (eask-args))
(files (if patterns
(eask-expand-file-specs patterns)
(directory-files default-directory t ".el")))
(files (cl-remove-if-not (lambda (file)
(string-suffix-p ".el" (file-name-nondirectory file)))
files))
(converted 0))
(cond
;; Files found, do the action!
(files
(dolist (file files)
(when (eask--convert-source file)
(cl-incf converted)))
(eask-msg "")
(eask-info "(Total of %s elisp file%s converted)" converted
(eask--sinr converted "" "s")))
;; Pattern defined, but no file found!
(patterns
(eask-info "(No files match wildcard: %s)"
(mapconcat #'identity patterns " ")))
;; Default, print help!
(t
(eask-info "(No elisp files have been converted to Eask)")
(eask-help "init/source")))))

;;; init/source.el ends here

0 comments on commit 62efee5

Please sign in to comment.