Skip to content

Commit

Permalink
Merge branch 'jesseleite/create-directive'
Browse files Browse the repository at this point in the history
  • Loading branch information
anishathalye committed Oct 12, 2019
2 parents 32741ea + 5a0f667 commit 04c113b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 6 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ The conventional name for the configuration file is `install.conf.yaml`.
~/.vim: vim
~/.vimrc: vimrc

- create:
- ~/downloads
- ~/.vim/undo-history

- shell:
- [git submodule update --init --recursive, Installing submodules]
```
Expand All @@ -119,9 +123,9 @@ Configuration

Dotbot uses YAML or JSON-formatted configuration files to let you specify how
to set up your dotfiles. Currently, Dotbot knows how to [link](#link) files and
folders, execute [shell](#shell) commands, and [clean](#clean) directories of
broken symbolic links. Dotbot also supports user [plugins](#plugins) for custom
commands.
folders, [create](#create) folders, execute [shell](#shell) commands, and
[clean](#clean) directories of broken symbolic links. Dotbot also supports user
[plugins](#plugins) for custom commands.

**Ideally, bootstrap configurations should be idempotent. That is, the
installer should be able to be run multiple times without causing any
Expand Down Expand Up @@ -219,6 +223,25 @@ the following config files equivalent:
relink: true
```

### Create

Create commands specify empty directories to be created. This can be useful
for scaffolding out folders or parent folder structure required for various
apps, plugins, shell commands, etc.

#### Format

Create commands are specified as an array of directories to be created.

#### Example

```yaml
- create:
- ~/projects
- ~/downloads
- ~/.vim/undo-history
```

### Shell

Shell commands specify shell commands to be run. Shell commands are run in the
Expand All @@ -243,8 +266,8 @@ command itself.

```yaml
- shell:
- mkdir -p ~/src
- [mkdir -p ~/downloads, Creating downloads directory]
- chsh -s $(which zsh)
- [chsh -s $(which zsh), Making zsh the default shell]
-
command: read var && echo Your variable is $var
stdin: true
Expand Down
2 changes: 1 addition & 1 deletion dotbot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def main():
log.use_color(False)
plugin_directories = list(options.plugin_dirs)
if not options.disable_built_in_plugins:
from .plugins import Clean, Link, Shell
from .plugins import Clean, Create, Link, Shell
plugin_paths = []
for directory in plugin_directories:
for plugin_path in glob.glob(os.path.join(directory, '*.py')):
Expand Down
1 change: 1 addition & 0 deletions dotbot/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .clean import Clean
from .create import Create
from .link import Link
from .shell import Shell
53 changes: 53 additions & 0 deletions dotbot/plugins/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
import glob
import shutil
import dotbot
import subprocess


class Create(dotbot.Plugin):
'''
Create empty paths.
'''

_directive = 'create'

def can_handle(self, directive):
return directive == self._directive

def handle(self, directive, data):
if directive != self._directive:
raise ValueError('Create cannot handle directive %s' % directive)
return self._process_paths(data)

def _process_paths(self, paths):
success = True
for path in paths:
path = os.path.expandvars(os.path.expanduser(path))
success &= self._create(path)
if success:
self._log.info('All paths have been set up')
else:
self._log.error('Some paths were not successfully set up')
return success

def _exists(self, path):
'''
Returns true if the path exists.
'''
path = os.path.expanduser(path)
return os.path.exists(path)

def _create(self, path):
success = True
if not self._exists(path):
self._log.debug('Trying to create path %s' % path)
try:
self._log.lowinfo('Creating path %s' % path)
os.makedirs(path)
except OSError:
self._log.warning('Failed to create path %s' % path)
success = False
else:
self._log.lowinfo('Path exists %s' % path)
return success
23 changes: 23 additions & 0 deletions test/tests/create.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_description='create folders'
. '../test-lib.bash'

test_expect_success 'run' '
run_dotbot <<EOF
- create:
- ~/somedir
- ~/nested/somedir
EOF
'

test_expect_success 'test' '
[ -d ~/somedir ] &&
[ -d ~/nested/somedir ]
'

test_expect_success 'run 2' '
run_dotbot <<EOF
- create:
- ~/somedir
- ~/nested/somedir
EOF
'

0 comments on commit 04c113b

Please sign in to comment.