Skip to content

Commit b401a28

Browse files
committed
Add make-workflow command
The command creates a pre-filled script for configuring a workflow. It has bash and zsh completions enabled for all 3 mandatory arguments. Also, in order to prevent a data loss, the command does not override already existing files.
1 parent c28762e commit b401a28

8 files changed

Lines changed: 195 additions & 9 deletions

File tree

completions/_git-elegant

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
_git-elegant (){
88
# update this function if either new commands or options are added
9+
# all commands should be added in alphabetical order
910
local curcontext='$curcontext' state line
1011
typeset -A opt_args
1112
_arguments -C ':command:->command' '*::option:->option'
@@ -17,6 +18,7 @@ _git-elegant (){
1718
'clone-repository:clones a repository and configures it'
1819
'deliver-work:publishes current branch to a remote repository'
1920
'init-repository:initializes a new repository and configures it'
21+
'make-workflow:makes a new workflow file for the repository'
2022
'obtain-work:checkouts a remote branch matching by a name'
2123
'polish-work:reapplies branch commits interactively'
2224
'prune-repository:removes useless local branches'
@@ -64,6 +66,7 @@ __ge_complete_commands () {
6466
accept-work) __ge_all_branches ;;
6567
show-release-notes) __ge_show_release_notes ;;
6668
start-work) __ge_start_work ;;
69+
make-workflow) __ge_make_workflow ;;
6770
*) _arguments '--help' '--no-workflows' ;;
6871
esac
6972
}
@@ -116,3 +119,28 @@ __ge_start_work() {
116119
'1:name:()' \
117120
'2:from:(${all[@]})'
118121
}
122+
123+
__ge_make_workflow_types() {
124+
local types=(
125+
'ahead:runs this workflow ahead of the command execution'
126+
'after:runs this workflow after of the command execution'
127+
)
128+
_describe 'types' types
129+
}
130+
131+
__ge_make_workflow_locations() {
132+
local locations=(
133+
'common:stores this workflow in ".workflows" directory (public)'
134+
'personal:stores this workflow in ".git/.workflows" directory (private)'
135+
)
136+
_describe 'locations' locations
137+
}
138+
139+
__ge_make_workflow() {
140+
local commands=( $(git elegant show-commands) )
141+
_arguments '--help' \
142+
'--no-workflows' \
143+
'1:command:(${commands[@]})' \
144+
'2:type:__ge_make_workflow_types' \
145+
'3:location:__ge_make_workflow_locations'
146+
}

completions/git-elegant.bash

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ _git_elegant() {
1616
# the first word prior to the ${cursor}
1717
if [[ ${#COMP_WORDS[*]} > $(( 1 + ${offset} )) ]]; then
1818
case "${COMP_WORDS[COMP_CWORD-$(( 1 + ${offset} ))]}" in
19-
elegant|git-elegant)
19+
elegant|git-elegant|make-workflow)
2020
local opts=(
2121
${geops}
2222
$(git elegant show-commands)
@@ -59,6 +59,9 @@ _git_elegant() {
5959
)
6060
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
6161
;;
62+
make-workflow)
63+
COMPREPLY=( $(compgen -W "ahead after" -- ${cursor}) )
64+
;;
6265
*) ;;
6366
esac
6467
fi
@@ -72,6 +75,9 @@ _git_elegant() {
7275
)
7376
COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cursor}) )
7477
;;
78+
make-workflow)
79+
COMPREPLY=( $(compgen -W "common personal" -- ${cursor}) )
80+
;;
7581
*) ;;
7682
esac
7783
fi

docs/commands.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ There are commands used in various situations such as
2525

2626
enhance contribution rules
2727
show-workflows Shows configured workflows in the repository.
28+
make-workflow Makes a new workflow file for the repository.
2829

2930
make day-to-day contributions
3031
start-work Creates a new branch.
@@ -189,6 +190,34 @@ git commit --allow-empty --file a-message-of-initial-commit
189190
git show
190191
```
191192

193+
# `make-workflow`
194+
195+
```bash
196+
usage: git elegant make-workflow <command> <type> <location>
197+
```
198+
199+
Makes a new workflow file for a given Elegant Git command and opens it for the
200+
further scripting in the default text editor. The created file will have a
201+
shebang line pointing to `#!/usr/bin/env sh -e` and executable permissions.
202+
That's why the created file becomes an executable one by default. All needed
203+
directories will be created if any.
204+
205+
A `type` defines whether it should be executed `ahead` or `after` the
206+
given command.
207+
208+
And `location` defines the availability such as personal (located at
209+
`.git/.workflows`) or common (located at `.workflows`).
210+
211+
212+
Approximate commands flow is
213+
```bash
214+
==>> git elegant make-workflow show-work ahead common
215+
mkdir -p .workflows
216+
touch .workflows/show-work-ahead
217+
chmod +x .workflows/show-work-ahead
218+
vim .workflows/show-work-ahead
219+
```
220+
192221
# `obtain-work`
193222

194223
```bash

libexec/git-elegant

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ $(--print-command-in-usage prune-repository)
8282
8383
enhance contribution rules
8484
$(--print-command-in-usage show-workflows)
85+
$(--print-command-in-usage make-workflow)
8586
8687
make day-to-day contributions
8788
$(--print-command-in-usage start-work)

libexec/git-elegant-make-workflow

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
command-purpose() {
4+
cat <<MESSAGE
5+
Makes a new workflow file for the repository.
6+
MESSAGE
7+
}
8+
9+
command-synopsis() {
10+
cat <<MESSAGE
11+
usage: git elegant make-workflow <command> <type> <location>
12+
MESSAGE
13+
}
14+
15+
command-description() {
16+
cat<<MESSAGE
17+
Makes a new workflow file for a given Elegant Git command and opens it for the
18+
further scripting in the default text editor. The created file will have a
19+
shebang line pointing to \`#!/usr/bin/env sh -e\` and executable permissions.
20+
That's why the created file becomes an executable one by default. All needed
21+
directories will be created if any.
22+
23+
A \`type\` defines whether it should be executed \`ahead\` or \`after\` the
24+
given command.
25+
26+
And \`location\` defines the availability such as personal (located at
27+
\`.git/.workflows\`) or common (located at \`.workflows\`).
28+
29+
30+
Approximate commands flow is
31+
\`\`\`bash
32+
==>> git elegant make-workflow show-work ahead common
33+
mkdir -p .workflows
34+
touch .workflows/show-work-ahead
35+
chmod +x .workflows/show-work-ahead
36+
vim .workflows/show-work-ahead
37+
\`\`\`
38+
MESSAGE
39+
}
40+
41+
--command() {
42+
command-text "$@"
43+
"$@"
44+
}
45+
46+
default() {
47+
_error-if-empty "${1}" "Please specify a name of Elegant Git command"
48+
_error-if-empty "${2}" "Please specify a type of the workflow"
49+
_error-if-empty "${3}" "Please specify a location of the workflow"
50+
local file=$(workflows-file ${3} ${1} ${2})
51+
if test -e ${file}; then
52+
error-text "The '${file}' file already exists."
53+
error-text "Please remove it manually and repeat the command if you need a new one."
54+
exit 43
55+
fi
56+
local directory=$(workflows-directory ${3})
57+
--command mkdir -p ${directory}
58+
--command touch ${file}
59+
echo "#!/usr/bin/env sh -e" >> ${file}
60+
echo "# This script invokes ${2} of the '${1}' execution." >> ${file}
61+
echo "" >> ${file}
62+
--command chmod +x ${file}
63+
--command $(git config core.editor) ${file}
64+
}

libexec/plugins/workflows

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@
66
fi
77
}
88

9-
personal-workflows() {
10-
# usage: personal-workflows <command>
11-
echo "$(--prefix ${1}).git/.workflows"
9+
workflows-directory() {
10+
# usage: workflows-directory <location>
11+
if [[ ${1} == personal ]]; then
12+
echo "$(--prefix ${1}).git/.workflows"
13+
elif [[ ${1} == common ]]; then
14+
echo "$(--prefix ${1}).workflows"
15+
else
16+
error-text "Unknown workflows location: " ${1}
17+
exit 1
18+
fi
1219
}
1320

14-
common-workflows() {
15-
# usage: common-workflows <command>
16-
echo "$(--prefix ${1}).workflows"
21+
workflows-file() {
22+
# usage: workflows-file <location> <command> <type>
23+
echo "$(workflows-directory ${1})/${2}-${3}"
1724
}
1825

1926
personal-workflows-file() {
2027
# usage: personal-workflows-file <comand> <type>
21-
echo "$(personal-workflows ${1})/${1}-${2}"
28+
workflows-file personal ${1} ${2}
2229
}
2330

2431
common-workflows-file() {
2532
# usage: common-workflows-file <comand> <type>
26-
echo "$(common-workflows ${1})/${1}-${2}"
33+
workflows-file common ${1} ${2}
2734
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bats
2+
3+
load addons-common
4+
load addons-repo
5+
6+
setup() {
7+
repo-new
8+
git config --local core.editor cat
9+
}
10+
11+
teardown(){
12+
repo-clean
13+
}
14+
15+
@test "'make-workflow': creates a new workflow file" {
16+
check git-elegant make-workflow show-work ahead common
17+
[[ ${status} -eq 0 ]]
18+
[[ ${lines[0]} =~ "mkdir -p .workflows" ]]
19+
[[ ${lines[1]} =~ "touch .workflows/show-work-ahead" ]]
20+
[[ ${lines[2]} =~ "chmod +x .workflows/show-work-ahead" ]]
21+
[[ ${lines[3]} =~ "cat .workflows/show-work-ahead" ]]
22+
[[ ${lines[4]} = "#!/usr/bin/env sh -e" ]]
23+
[[ ${lines[5]} = "# This script invokes ahead of the 'show-work' execution." ]]
24+
}
25+
26+
@test "'make-workflow': stops with exit code 45 if a command argument is not set" {
27+
check git-elegant make-workflow
28+
[[ ${status} -eq 45 ]]
29+
[[ ${lines[0]} = "Please specify a name of Elegant Git command" ]]
30+
}
31+
32+
@test "'make-workflow': stops with exit code 45 if a type argument is not set" {
33+
check git-elegant make-workflow show-work
34+
[[ ${status} -eq 45 ]]
35+
[[ ${lines[0]} = "Please specify a type of the workflow" ]]
36+
}
37+
38+
@test "'make-workflow': stops with exit code 45 if a location argument is not set" {
39+
check git-elegant make-workflow show-work ahead
40+
[[ ${status} -eq 45 ]]
41+
[[ ${lines[0]} = "Please specify a location of the workflow" ]]
42+
}
43+
44+
@test "'make-workflow': stops with exit code 43 if the workflow aready exists" {
45+
git-elegant make-workflow show-work ahead common
46+
check git-elegant make-workflow show-work ahead common
47+
[[ ${status} -eq 43 ]]
48+
[[ ${lines[0]} = "The '.workflows/show-work-ahead' file already exists." ]]
49+
[[ ${lines[1]} = "Please remove it manually and repeat the command if you need a new one." ]]
50+
}

tests/git-elegant-show-commands.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ teardown() {
3030
"release-work"
3131
"polish-work"
3232
"show-workflows"
33+
"make-workflow"
3334
)
3435
check git-elegant show-commands
3536
[[ ${#lines[@]} -eq ${#COMMANDS[@]} ]]

0 commit comments

Comments
 (0)