From 970ee8ade486b9be0ebf881f910d159070dccd75 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Tue, 5 Dec 2023 23:07:38 -0800 Subject: [PATCH] fix(init): Enhance guessed package name and entry point --- cmds/core/init.js | 33 ++++++++++++++++++++++++++------- lisp/_prepare.el | 35 +++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/cmds/core/init.js b/cmds/core/init.js index 0e8c025a..4f55ccaa 100644 --- a/cmds/core/init.js +++ b/cmds/core/init.js @@ -115,10 +115,18 @@ async function create_eask_file(dir) { // Starting writing Eask-file! let name, version, description, entry_point, emacs_version, website_url, keywords; - await ask(`package name: (${basename}) `, (answer) => { name = answer || basename; }); + + let guessed_package_name = guess_package_name(basename); + let guessed_entry_point = guess_entry_point(basename); + + await ask(`package name: (${guessed_package_name}) `, (answer) => { + name = answer || guessed_package_name; + }); await ask(`version: (1.0.0) `, (answer) => { version = answer || '1.0.0'; }); await ask(`description: `, (answer) => { description = answer; }); - await ask(`entry point: (${guess_entry_point(basename)}) `, (answer) => { entry_point = answer || `${basename}.el`; }); + await ask(`entry point: (${guessed_entry_point}) `, (answer) => { + entry_point = answer || guessed_entry_point; + }); await ask(`emacs version: (26.1) `, (answer) => { emacs_version = answer || '26.1'; }); await ask(`website: `, (answer) => { website_url = answer; }); await ask(`keywords: `, (answer) => { keywords = answer; }); @@ -189,14 +197,25 @@ function check_eask_filename(name) { } /** - * Return the guess entry point by its basname. - * @param { String } basename - The directory name. + * Return the guessed project name by its basename. + * @param { String } basename - The working directory name. + * @return Guessed project name. + */ +function guess_package_name(basename) { + basename = basename.toLowerCase(); + basename = basename.replaceAll('emacs-', ''); + basename = basename.replaceAll('-emacs', ''); + basename = basename.replace(/[-.]el$/, ''); + return basename; +} + +/** + * Return the guessed entry point by its basname. + * @param { String } basename - The working directory name. * @return Guessed entry point filename. */ function guess_entry_point(basename) { - if (basename.endsWith(".el")) { - return basename; - } + basename = guess_package_name(basename); return basename + ".el"; } diff --git a/lisp/_prepare.el b/lisp/_prepare.el index 8197c273..9a22dafb 100644 --- a/lisp/_prepare.el +++ b/lisp/_prepare.el @@ -270,18 +270,29 @@ The function `directory-empty-p' only exists 28.1 or above; copied it." ;; 27.2 or lower! (null (directory-files dir nil directory-files-no-dot-files-regexp t))))) -(defun eask-guess-package-name () - "Return the possible package name." - (or (eask-package-name) - (ignore-errors (file-name-nondirectory - (file-name-sans-extension eask-package-file))))) - -(defun eask-guess-entry-point (&optional project-name) - "Return the guess entry point by its PROJECT-NAME." - (let ((project-name (or project-name (eask-guess-package-name)))) - (if (string-suffix-p ".el" project-name) - project-name - (format "%s.el" project-name)))) +(defun eask--guess-package-name (basename) + "Convert the BASENAME to a valid, commonly seen package name." + (when-let ((name (downcase basename))) + (setq name (eask-s-replace "emacs-" "" name) + name (eask-s-replace "-emacs" "" name) + name (replace-regexp-in-string "[.-]el$" "" name)) + name)) + +(defun eask-guess-package-name (&optional basename) + "Return the possible package name. + +Optional argument BASENAME accepts a string; it will convert the string to a +valid, commonly seen package name." + (or (eask--guess-package-name basename) + (eask-package-name) + (eask--guess-package-name + (ignore-errors (file-name-nondirectory + (file-name-sans-extension eask-package-file)))))) + +(defun eask-guess-entry-point (&optional basename) + "Return the guess entry point by its BASENAME." + (let ((name (eask-guess-package-name basename))) + (format "%s.el" name))) (defun eask-read-string (prompt &optional initial-input