Skip to content

Commit 6227eed

Browse files
committed
Allow fetching .devkit modules from github repos
1 parent 74f4414 commit 6227eed

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Dependencies are installed to a `.deps` directory, with executables in `.deps/bi
2929
- [Configuration and Extension](#configuration-and-extension)
3030
* [Automatic Dependency Fetching](#automatic-dependency-fetching)
3131
- [.devkit Modules](#devkit-modules)
32+
* [External Modules](#external-modules)
3233
- [All-Purpose Modules](#all-purpose-modules)
3334
* [cram](#cram)
3435
* [shell-console](#shell-console)
@@ -136,11 +137,26 @@ For projects using bash 3.2
136137

137138
You can activate any of them by adding "`dk use:` *modules...*" to your `.dkrc`, then defining any needed variable or function overrides. (Typically, you override variables by defining them *before* the `dk use:` line(s), and functions by defining them *after*.)
138139

139-
Note that these modules are not specially privileged in any way: you are not *required* to use them to obtain the specified functionality. They are simply defaults and examples.
140+
Note that these modules are not specially privileged in any way: you are not *required* to use them to obtain the specified functionality. They are simply defaults and examples. You can write your own modules and put them in a `.devkit-modules` subdirectory of your project root, and `dk use:` will look for modules there before searching .devkit's bundled modules.
140141

141-
So, for example, if you don't like how devkit's `entr-watch` module works, you can write your own functions in `.dkrc` or in a package that you load as a development dependency (e.g. with `require mycommand github mygithubaccount/mycommand mycommand; source "$(command -v mycommand)"`).
142+
#### External Modules
142143

143-
You can also place your own devkit modules under a `.devkit-modules` directory in your project root, and `dk use:` will look for modules there before searching .devkit's bundled modules. You can also access modules from your `.deps` subdirectories by adding symlinks to them from your project's `.devkit-modules`. (Just make sure your `.dkrc` installs those dependencies *before* `dk use:`-ing them, if they're not there yet.)
144+
You can also load basically any file from github as a .devkit module, by specifying a module name of the form:
145+
146+
`+` *org* `/` *repo [* `@`*ref ] [* `:`*module-path ]*
147+
148+
That is, doing e.g. `dk use: +foo/bar@baz:spam` will check out the `baz` branch or tag of `foo/bar` from github into your `.deps` directory (if there's not already a repo there), and then search for one of these files:
149+
150+
* `.deps/foo/bar/.devkit-modules/spam`
151+
* `.deps/foo/bar/bin/spam/`
152+
* `.deps/foo/bar/spam/`
153+
154+
Both the `@`*ref* and `:`*module-path* parts are optional, defaulting to `master` and `.devkit-module` respectively, with `dk use: +foo/bar` checking out the master branch of `foo/bar` and searching for one of these files:
155+
156+
* `.deps/foo/bar/.devkit-modules/default`
157+
* `.deps/foo/bar/.devkit-module`
158+
159+
This means that projects that want to provide .devkit support can include a `.devkit-modules/default` or `.devkit-module` file, allowing others to use it with `dk use: +some/project`, automatically including the repo at build time, and adding its executables to `.deps/bin`. A project can also be created to just publish a bunch of `.devkit-modules`, or you can just literally source any file you like from any project on github by using an explicit *module-path* in the module name.
144160

145161
### All-Purpose Modules
146162

dk

+13-6
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,20 @@ relative-symlink() {
452452
# Used to create relative links in .deps/bin
453453
realpath.dirname "$2"; realpath.relative "$1" "$REPLY"; ln -sf "$REPLY" "$2"; return $?
454454
}
455+
__dk_find_file() {
456+
for REPLY in "${@:3}"; do REPLY=$1/$REPLY${2:+/$2}; [[ -f $REPLY ]] || continue; return; done
457+
false
458+
}
459+
455460
__find_dk_module() {
456-
local p
457-
for p in .devkit-modules .devkit/modules; do
458-
p=$LOCO_ROOT/$p/$1
459-
if [[ -f $p ]]; then REPLY=$p; return; fi
460-
done
461-
return 1
461+
case $1 in
462+
+?*/?*:*) set -- "${1%:*}" "${1#*:}" .devkit-modules bin . ;;
463+
+?*/?*) set -- "$1" "" .devkit-modules/default .devkit-module ;;
464+
*)
465+
__dk_find_file "$LOCO_ROOT" "$1" .devkit-modules .devkit/modules
466+
return
467+
esac
468+
github "${1#+}"; __dk_find_file "$BASHER_PACKAGES_PATH/${1#+}" "${@:2}"
462469
}
463470
464471
dk.use:() {

dk.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,29 @@ relative-symlink() {
236236

237237
## `dk use:`
238238

239-
devkit modules are loaded using the `dk use` command, which loads modules from `.devkit/modules` a maximum of once.
239+
devkit modules are loaded using the `dk use:` command, which loads modules a maximum of once.
240+
241+
A module name of the form `+` *org* `/` *repo [*`@` *ref ] [* `:`*module ]* is loaded from the specified github repository (and possible reference), using the `github` function. (i.e., it is cached as a dependency in `.deps`, and not refetched unless a `clean` is run).
242+
243+
If the *module* part is given, it is searched for in that repo's `.devkit-modules/`, `bin/`, and root directories; otherwise the module is exected to be named `.devkit-modules/default `, or `.devkit-module` in the repo's root.
244+
245+
Module names *not* beginning with `+` are searched for in the project's own `.devkit-modules` directory, then in `.devkit/modules`.
240246

241247
```shell
248+
__dk_find_file() {
249+
for REPLY in "${@:3}"; do REPLY=$1/$REPLY${2:+/$2}; [[ -f $REPLY ]] || continue; return; done
250+
false
251+
}
252+
242253
__find_dk_module() {
243-
local p
244-
for p in .devkit-modules .devkit/modules; do
245-
p=$LOCO_ROOT/$p/$1
246-
if [[ -f $p ]]; then REPLY=$p; return; fi
247-
done
248-
return 1
254+
case $1 in
255+
+?*/?*:*) set -- "${1%:*}" "${1#*:}" .devkit-modules bin . ;;
256+
+?*/?*) set -- "$1" "" .devkit-modules/default .devkit-module ;;
257+
*)
258+
__dk_find_file "$LOCO_ROOT" "$1" .devkit-modules .devkit/modules
259+
return
260+
esac
261+
github "${1#+}"; __dk_find_file "$BASHER_PACKAGES_PATH/${1#+}" "${@:2}"
249262
}
250263

251264
dk.use:() {

0 commit comments

Comments
 (0)