mirror of
https://github.com/janet-lang/janet
synced 2024-12-26 08:20:27 +00:00
Add :@all: to module/expand-path
Allow more easily importing modules from custom directories without jumping through too many hoops. Technically, this was possible before but required circumventing the built-in module/paths and was just a hassle. Also add entries to module/path (and module/add-path) to allow code like the following. (setdyn :my-libs "/home/me/janet-stuff/") (import @my-libs/toolbox) Intended for things like test harnesses where code might not be installed to the usual directories.
This commit is contained in:
parent
57356781a9
commit
b75fb8dc9e
@ -2,6 +2,10 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
## ??? - Unreleased
|
## ??? - Unreleased
|
||||||
|
- Allow importing modules from custom directories more easily with the `@` prefix
|
||||||
|
to module paths. For example, if there is a dynamic binding :custom-modules that
|
||||||
|
is a file system path to a directory of modules, import from that directory with
|
||||||
|
`(import @custom-modules/mymod)`.
|
||||||
- Fix error message bug in FFI library.
|
- Fix error message bug in FFI library.
|
||||||
|
|
||||||
## 1.25.1 - 2022-10-29
|
## 1.25.1 - 2022-10-29
|
||||||
|
@ -2642,9 +2642,10 @@
|
|||||||
[image]
|
[image]
|
||||||
(unmarshal image load-image-dict))
|
(unmarshal image load-image-dict))
|
||||||
|
|
||||||
|
(defn- check-dyn-relative [x] (if (string/has-prefix? "@" x) x))
|
||||||
(defn- check-relative [x] (if (string/has-prefix? "." x) x))
|
(defn- check-relative [x] (if (string/has-prefix? "." x) x))
|
||||||
(defn- check-not-relative [x] (if-not (string/has-prefix? "." x) x))
|
(defn- check-not-relative [x] (if-not (string/has-prefix? "." x) x))
|
||||||
(defn- check-is-dep [x] (unless (or (string/has-prefix? "/" x) (string/has-prefix? "." x)) x))
|
(defn- check-is-dep [x] (unless (or (string/has-prefix? "/" x) (string/has-prefix? "@" x) (string/has-prefix? "." x)) x))
|
||||||
(defn- check-project-relative [x] (if (string/has-prefix? "/" x) x))
|
(defn- check-project-relative [x] (if (string/has-prefix? "/" x) x))
|
||||||
|
|
||||||
(def module/cache
|
(def module/cache
|
||||||
@ -2678,6 +2679,8 @@
|
|||||||
(defn- find-prefix
|
(defn- find-prefix
|
||||||
[pre]
|
[pre]
|
||||||
(or (find-index |(and (string? ($ 0)) (string/has-prefix? pre ($ 0))) module/paths) 0))
|
(or (find-index |(and (string? ($ 0)) (string/has-prefix? pre ($ 0))) module/paths) 0))
|
||||||
|
(def dyn-index (find-prefix ":@all:"))
|
||||||
|
(array/insert module/paths dyn-index [(string ":@all:" ext) loader check-dyn-relative])
|
||||||
(def all-index (find-prefix ".:all:"))
|
(def all-index (find-prefix ".:all:"))
|
||||||
(array/insert module/paths all-index [(string ".:all:" ext) loader check-project-relative])
|
(array/insert module/paths all-index [(string ".:all:" ext) loader check-project-relative])
|
||||||
(def sys-index (find-prefix ":sys:"))
|
(def sys-index (find-prefix ":sys:"))
|
||||||
|
@ -111,7 +111,10 @@ JANET_CORE_FN(janet_core_expand_path,
|
|||||||
"This takes in a path (the argument to require) and a template string, "
|
"This takes in a path (the argument to require) and a template string, "
|
||||||
"to expand the path to a path that can be "
|
"to expand the path to a path that can be "
|
||||||
"used for importing files. The replacements are as follows:\n\n"
|
"used for importing files. The replacements are as follows:\n\n"
|
||||||
"* :all: -- the value of path verbatim\n\n"
|
"* :all: -- the value of path verbatim.\n\n"
|
||||||
|
"* :@all: -- Same as :all:, bu if `path` starts with the @ character,\n"
|
||||||
|
" the first path segment is replaced with a dynamic binding\n"
|
||||||
|
" `(dyn <first path segment as keyword>)`.\n\n"
|
||||||
"* :cur: -- the current file, or (dyn :current-file)\n\n"
|
"* :cur: -- the current file, or (dyn :current-file)\n\n"
|
||||||
"* :dir: -- the directory containing the current file\n\n"
|
"* :dir: -- the directory containing the current file\n\n"
|
||||||
"* :name: -- the name component of path, with extension if given\n\n"
|
"* :name: -- the name component of path, with extension if given\n\n"
|
||||||
@ -157,6 +160,21 @@ JANET_CORE_FN(janet_core_expand_path,
|
|||||||
if (strncmp(template + i, ":all:", 5) == 0) {
|
if (strncmp(template + i, ":all:", 5) == 0) {
|
||||||
janet_buffer_push_cstring(out, input);
|
janet_buffer_push_cstring(out, input);
|
||||||
i += 4;
|
i += 4;
|
||||||
|
} else if (strncmp(template + i, ":@all:", 6) == 0) {
|
||||||
|
if (input[0] == '@') {
|
||||||
|
const char *p = input;
|
||||||
|
while (*p && !is_path_sep(*p)) p++;
|
||||||
|
size_t len = p - input - 1;
|
||||||
|
char *str = janet_smalloc(len + 1);
|
||||||
|
memcpy(str, input + 1, len);
|
||||||
|
str[len] = '\0';
|
||||||
|
janet_formatb(out, "%V", janet_dyn(str));
|
||||||
|
janet_sfree(str);
|
||||||
|
janet_buffer_push_cstring(out, p);
|
||||||
|
} else {
|
||||||
|
janet_buffer_push_cstring(out, input);
|
||||||
|
}
|
||||||
|
i += 5;
|
||||||
} else if (strncmp(template + i, ":cur:", 5) == 0) {
|
} else if (strncmp(template + i, ":cur:", 5) == 0) {
|
||||||
janet_buffer_push_bytes(out, (const uint8_t *)curdir, curlen);
|
janet_buffer_push_bytes(out, (const uint8_t *)curdir, curlen);
|
||||||
i += 4;
|
i += 4;
|
||||||
|
Loading…
Reference in New Issue
Block a user