1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Add auto-shebang functionality.

This commit is contained in:
Calvin Rose 2021-03-11 18:10:33 -06:00
parent c9097623d6
commit 78f6b6a507

14
jpm
View File

@ -1080,12 +1080,14 @@ int main(int argc, const char **argv) {
(install-rule dest (dyn :binpath JANET_BINPATH)))))) (install-rule dest (dyn :binpath JANET_BINPATH))))))
(defn declare-binscript (defn declare-binscript
"Declare a janet file to be installed as an executable script. Creates ``Declare a janet file to be installed as an executable script. Creates
a shim on windows. If hardcode is true, will insert code into the script a shim on windows. If hardcode is true, will insert code into the script
such that it will run correctly even when JANET_PATH is changed." such that it will run correctly even when JANET_PATH is changed. if auto-shebang
[&keys {:main main :hardcode-syspath hardcode}] is truthy, will also automatically insert a correct shebang line.
``
[&keys {:main main :hardcode-syspath hardcode :auto-shebang auto-shebang}]
(def binpath (dyn :binpath JANET_BINPATH)) (def binpath (dyn :binpath JANET_BINPATH))
(if hardcode (if (or auto-shebang hardcode)
(let [syspath (dyn :modpath JANET_MODPATH)] (let [syspath (dyn :modpath JANET_MODPATH)]
(def parts (peg/match path-splitter main)) (def parts (peg/match path-splitter main))
(def name (last parts)) (def name (last parts))
@ -1097,7 +1099,9 @@ int main(int argc, const char **argv) {
(def first-line (:read f :line)) (def first-line (:read f :line))
(def second-line (string/format "(put root-env :syspath %v)\n" syspath)) (def second-line (string/format "(put root-env :syspath %v)\n" syspath))
(def rest (:read f :all)) (def rest (:read f :all))
(string first-line second-line rest))) (string (if auto-shebang
(string "#!" JANET_BINPATH "/janet\n"))
first-line (if hardcode second-line) rest)))
(create-dirs path) (create-dirs path)
(spit path contents) (spit path contents)
(unless is-win (shell "chmod" "+x" path)))) (unless is-win (shell "chmod" "+x" path))))