diff --git a/README.md b/README.md index b4403ba..24eff10 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ mapped to extended configuration dictionaries. | `ignore-missing` | Do not fail if the source is missing and create the link anyway (default: false) | | `glob` | Treat `path` as a glob pattern, expanding patterns referenced below, linking all *files** matched. (default: false) | | `exclude` | Array of glob patterns to remove from glob matches. Uses same syntax as `path`. Ignored if `glob` is `false`. (default: empty, keep all matches) | +| `prefix` | Prepend prefix prefix to basename of each file when linked, when `glob` is `true`. (default: '') | When `glob: True`, Dotbot uses [glob.glob](https://docs.python.org/3/library/glob.html#glob.glob) to resolve glob paths, expanding Unix shell-style wildcards, which are **not** the same as regular expressions; Only the following are expanded: @@ -217,6 +218,10 @@ However, due to the design of `glob.glob`, using a glob pattern such as `config/ path: hammerspoon ~/.config/: path: dotconf/config/** + ~/: + glob: true + path: dotconf/* + prefix: '.' ``` If the source location is omitted or set to `null`, Dotbot will use the diff --git a/dotbot/plugins/link.py b/dotbot/plugins/link.py index bb3fc7c..555bcea 100644 --- a/dotbot/plugins/link.py +++ b/dotbot/plugins/link.py @@ -34,6 +34,7 @@ class Link(dotbot.Plugin): relink = defaults.get('relink', False) create = defaults.get('create', False) use_glob = defaults.get('glob', False) + base_prefix = defaults.get('prefix', '') test = defaults.get('if', None) ignore_missing = defaults.get('ignore-missing', False) exclude_paths = defaults.get('exclude', []) @@ -46,6 +47,7 @@ class Link(dotbot.Plugin): relink = source.get('relink', relink) create = source.get('create', create) use_glob = source.get('glob', use_glob) + base_prefix = source.get('prefix', base_prefix) ignore_missing = source.get('ignore-missing', ignore_missing) exclude_paths = source.get('exclude', exclude_paths) path = self._default_source(destination, source.get('path')) @@ -81,6 +83,9 @@ class Link(dotbot.Plugin): # Find common dirname between pattern and the item: glob_dirname = os.path.dirname(os.path.commonprefix([path, glob_full_item])) glob_item = (glob_full_item if len(glob_dirname) == 0 else glob_full_item[len(glob_dirname) + 1:]) + # Add prefix to basepath, if provided + if base_prefix: + glob_item = base_prefix + glob_item # where is it going glob_link_destination = os.path.join(destination, glob_item) if create: diff --git a/test/tests/link-prefix.bash b/test/tests/link-prefix.bash new file mode 100644 index 0000000..e3db102 --- /dev/null +++ b/test/tests/link-prefix.bash @@ -0,0 +1,23 @@ +test_description='link prefix' +. '../test-lib.bash' + +test_expect_success 'setup' ' +mkdir ${DOTFILES}/conf && +echo "apple" > ${DOTFILES}/conf/a && +echo "banana" > ${DOTFILES}/conf/b && +echo "cherry" > ${DOTFILES}/conf/c +' + +test_expect_success 'test glob w/ prefix' ' +run_dotbot -v <