-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkick.fish
265 lines (215 loc) · 7.99 KB
/
kick.fish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
begin
set languages 'python' 'go' 'rust' 'zig'
set python_gitignore 'https://github.com/github/gitignore/raw/main/Python.gitignore'
set go_gitignore 'https://github.com/github/gitignore/raw/main/Go.gitignore'
set zig_gitignore 'https://github.com/ziglang/zig/raw/master/.gitignore'
set default_name 'unnamed'
set python_main \
'def main(): ...\n\n\n' \
'if __name__ == "__main__":\n' \
' main()\n'
function _create_python -a name
set package 'app'
if contains -- '--lib' $argv
set package (string trim (string replace '-' '_' "$name"))
if not string match -aq -r '^[a-z][a-z0-9_]+$' "$package"
echo -s \
(set_color $fish_color_error) \
"$package does not comply with package naming; " \
"see https://peps.python.org/pep-0008/#package-and-module-names" \
(set_color normal)
return 1
end
# TODO: do we really believe in the src+package layout?
# Yes, Hynek, but also common sense. One needs to (un)intentionally
# design a library the way it’d behave differently as an installed
# package. So, it’s either a very specific choice or an error.
set package "src/$package"
end
touch pyproject.toml requirements.txt
if contains -- '--script' $argv
echo -se $python_main > $package.py
else
mkdir -p $package
echo -e '__version__ = "0.0.0"\n' > $package/__init__.py
echo -se $python_main > $package/main.py
end
if not contains -- '--no-tests' $argv
mkdir tests
touch tests/__init__.py tests/conftest.py
echo -e 'pytest >= 7.0\n' > requirements-test.txt
end
if not contains -- '--no-venv' $argv
if functions -q mise-venv
mise-venv
else if functions -q venv
venv
else
command python3 -m venv .venv
and source .venv/bin/activate.fish
and if command -q pip
pip install -U pip
end
end
end
echo "# $name" > README.md
if not contains -- '--no-git' $argv
_init_git $python_gitignore
end
end
function _create_go -a name
# Yeah, there is https://github.com/golang-standards/project-layout, but as
# Russ Cox (tech lead of the Go language project) explained in a relevant issue,
# there are no exact standards for a Go project layout.
# See https://github.com/golang-standards/project-layout/issues/117#issuecomment-828503689.
# Also, https://go.googlesource.com/example/.
set package 'app'
if contains -- '--lib' $argv
set package (string trim "$name")
end
go mod init $name
mkdir -p $package
echo "package main" > $package/main.go
echo "# $name" > README.md
if not contains -- '--no-git' $argv
_init_git $go_gitignore
end
end
function _create_rust -a name
set opts "--name=$name"
if contains -- '--lib' $argv
set -a opts '--lib'
else
set -a opts '--bin'
end
if contains -- '--no-git' $argv
set -a opts '--vcs none'
end
command cargo init $opts .
end
function _create_zig -a name
set cmd 'init-exe'
if contains -- '--lib' $argv
set cmd 'init-lib'
end
echo -se \
'.{\n' \
" .name = \"$name\",\n" \
' .version = "0.0.0",\n' \
' .dependencies = .{},\n' \
'}\n' \
> build.zig.zon
command zig $cmd
if not contains -- '--no-git' $argv
_init_git $zig_gitignore
end
end
function _init_git -a gitignore_url
curl -Lso .gitignore $gitignore_url
git init -q .
end
function kick -d 'Kickstarts a new software development project'
set -l opts (fish_opt -s h -l help)
set -a opts (fish_opt -s L -l lang --long-only --optional-val)
set -a opts (fish_opt -s n -l name --long-only --required-val)
set -a opts (fish_opt -s l -l lib --long-only)
set -a opts (fish_opt -s G -l no-git --long-only)
set -a opts (fish_opt -s R -l no-readme --long-only)
set -a opts (fish_opt -s V -l no-venv --long-only)
set -a opts (fish_opt -s T -l no-tests --long-only)
set -a opts (fish_opt -s S -l script --long-only)
argparse --ignore-unknown --stop-nonopt $opts -- $argv
or return
if test -n "$_flag_help"
echo 'Kickstart a new software development project.'
echo ''
echo "Usage: $_ [OPTS...] [TARGET]"
echo ''
echo 'Options:'
echo ' --lang=python Creates a Python project [default]'
echo ' --lang=go Creates a Go project'
echo ' --lang=rust Creates a Rust project'
echo ' --lang=zig Creates a Zig project'
echo ''
echo ' --name=<NAME> Specifies the project name [default: "unnamed"]'
echo ' --lib Specifies the project is a library'
echo ' --no-git Omits Git VCS initialization'
echo ' --no-readme Omits README.md creation'
echo ''
echo 'Python Options:'
echo ' --no-venv Omits Python virtual environment creation'
echo ' --no-tests Omits tests directory creation'
echo ' --script Specifies a single file project structure'
echo ''
echo 'Parameters:'
echo ' TARGET A target directory [default: "."]'
return
end
set lang $languages[1]
if test -n "$_flag_lang"
set lang (string trim "$_flag_lang")
end
if not contains "$lang" $languages
echo -s \
(set_color $fish_color_error) \
'error: invalid language name; must be either ' \
(string join ', ' $languages) \
(set_color normal)
return 1
end
set target '.'
if test -n "$argv[1]"
set target "$argv[1]"
end
set target (path resolve "$target")
if test "$target" != "$PWD"
echo "Creating $target"
mkdir -p $target
and cd $target
end
if test (ls -1A 2> /dev/null | wc -l | string trim) -gt 0
read -lun1 -P 'directory is not empty; continue anyway? [y|N] ' answer
if test "$answer" != 'y'
return 0
end
end
set name "$default_name"
if test -n "$_flag_name"
set name "$_flag_name"
end
set params $name $_flag_lib $_flag_no_git
switch "$lang"
case 'python'
_create_python $params $_flag_no_venv $_flag_no_tests $_flag_script
case 'go'
_create_go $params
case 'rust'
_create_rust $params
case 'zig'
_create_zig $params
end
if test $status -ne 0
echo -s \
(set_color $fish_color_error) \
'error: something went wrong' \
(set_color normal)
if test "$PWD" = "$target"
cd ..
end
if test -d "$target"
echo -s \
(set_color yellow) \
"consider removing $target" \
(set_color normal)
# rm -rf $target
end
return 1
end
if test -z "$_flag_no_readme"
echo "# $name" > README.md
end
if test -z "$_flag_no_git" -a -d .git
git add --all
end
end
end