-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshp.sh
executable file
·466 lines (390 loc) · 14.4 KB
/
shp.sh
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
version=0.1
function help {
cat <<EOF
░██████╗██╗░░██╗██████╗░
██╔════╝██║░░██║██╔══██╗
╚█████╗░███████║██████╔╝
░╚═══██╗██╔══██║██╔═══╝░
██████╔╝██║░░██║██║░░░░░
╚═════╝░╚═╝░░╚═╝╚═╝░░░░░ (simple-haskell-project)
Usage: shp.sh run
-h, --help display this help message and exit
-v, --version display version information and exit the program
run run the program
EOF
}
function version {
cat <<EOF
simple-haskell-project $version
EOF
}
# If the first argument is -v or --version then display the version information and exit the program.
if [ "$1" = "-v" ] || [ "$1" = "--version" ]; then
version
exit 0
fi
# If the first argument is -h or --help then display the help message and exit the program.
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
help
exit 0
fi
# If the first argument is not -v, --version, -h, --help, or run then display the help message and exit the program.
if [ "$1" != "-v" ] && [ "$1" != "--version" ] && [ "$1" != "-h" ] && [ "$1" != "--help" ] && [ "$1" != "run" ]; then
help
exit 0
fi
function usage {
# Ask for name, default is user name of the current user.
read -p "Enter your name [$(whoami)]: " name
name=${name:-$(whoami)}
# Ask for email, default is the variable name with @email.com appended.
read -p "Enter your email [$(whoami)@email.com]: " email
email=${email:-${name}@email.com}
# Ask for the project name, default is simple-haskell-project.
# remove whitespaces and replace with hyphens and convert to lowercase.
read -p "Enter the project name [simple-haskell-project]: " project
project=${project:-simple-haskell-project}
project=$(echo $project | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -d '[:space:]')
# If project name contains any non-alphanumeric characters (with the exception of hyphens)
# or if project name is the same as the name of a directory in the current directory then
# ask for a new project name until a valid project name is entered.
while [[ ! $project =~ ^[a-z0-9-]+$ ]] || [ -d $project ]; do
if [[ ! $project =~ ^[a-z0-9-]+$ ]]; then
echo "Project name must only contain alphanumeric characters and hyphens."
fi
if [ -d $project ]; then
echo "Project name must not be the same as the name of a directory in the current directory."
fi
read -p "Enter the project name [simple-haskell-project]: " project
project=${project:-simple-haskell-project}
project=$(echo $project | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -d '[:space:]')
done
# Ask for the project description, default is A simple Haskell project.
read -p "Enter the project description [A simple Haskell project]: " description
description=${description:-A simple Haskell project}
# Ask the user what the source directory should be called, default is src.
read -p "Enter the source directory name [src]: " srcdir
srcdir=${srcdir:-src}
# if the source directory name contains any non-alphanumeric characters (with the exception of hyphens)
# then ask for a new source directory name until a valid source directory name is entered.
while [[ ! $srcdir =~ ^[a-z0-9-]+$ ]]; do
echo "Source directory name must only contain alphanumeric characters and hyphens."
read -p "Enter the source directory name [src]: " srcdir
srcdir=${srcdir:-src}
done
mkdir $project
pushd $project > /dev/null
# Create src directory.
mkdir $srcdir
# Create Main.hs file.
cat <<EOF > $srcdir/Main.hs
module Main where
main :: IO ()
main = putStrLn "Hello, World!"
EOF
# Ask if the user wants to include a test suite, default is no.
read -p "Include a test suite? [y/N] " testsuite
testsuite=${testsuite:-n}
# if test suite is not yes or no then ask for a new test suite option until a valid test suite option is entered.
while [[ ! $testsuite =~ ^[yYnN]$ ]]; do
echo "Test suite option must be y or n."
read -p "Include a test suite? [y/N] " testsuite
testsuite=${testsuite:-n}
done
# If test suite is yes then create a test suite.
if [ $testsuite = "y" ] || [ $testsuite = "Y" ]; then
mkdir test
cat <<EOF > test/Spec.hs
import Test.Tasty
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "Tests" []
EOF
fi
# Ask if the user wants to include a benchmark suite, default is no.
read -p "Include a benchmark suite? [y/N] " benchmarksuite
benchmarksuite=${benchmarksuite:-n}
# If benchmark suite is not yes or no then ask for a new benchmark suite option until a valid benchmark suite option is entered.
while [[ ! $benchmarksuite =~ ^[yYnN]$ ]]; do
echo "Benchmark suite option must be y or n."
read -p "Include a benchmark suite? [y/N] " benchmarksuite
benchmarksuite=${benchmarksuite:-n}
done
# If benchmark suite is yes then create a benchmark suite.
if [ $benchmarksuite = "y" ] || [ $benchmarksuite = "Y" ]; then
mkdir bench
cat <<EOF > bench/Main.hs
import Criterion.Main
main :: IO ()
main = defaultMain []
EOF
fi
# Ask if the user wants to include a readme, default is no.
read -p "Include a readme? [y/N] " readme
readme=${readme:-n}
# If readme is not yes or no then ask for a new readme option until a valid readme option is entered.
while [[ ! $readme =~ ^[yYnN]$ ]]; do
echo "Readme option must be y or n."
read -p "Include a readme? [y/N] " readme
readme=${readme:-n}
done
# If readme is yes then create a readme file.
if [ $readme = "y" ] || [ $readme = "Y" ]; then
echo "$description" > README.md
fi
# Ask if the user wants to include a license, default is no.
read -p "Include a license? [y/N] " license
license=${license:-n}
# If license is not yes or no then ask for a new license option until a valid license option is entered.
while [[ ! $license =~ ^[yYnN]$ ]]; do
echo "License option must be y or n."
read -p "Include a license? [y/N] " license
license=${license:-n}
done
# If license is yes then ask which license to use.
if [ $license = "y" ] || [ $license = "Y" ]; then
# Fetch license list from https://api.github.com/licenses and display a list of licenses.
licenses=$(curl -s https://api.github.com/licenses | jq -r '.[].spdx_id')
licensecount=$(echo "$licenses" | wc -l)
echo "$licenses" | nl
# Ask the user to enter the number of the license they want to use. Default is MIT.
read -p "Enter the number of the license you want to use [11]: " licensenumber
licensenumber=${licensenumber:-11}
# If the user enters a number that is not in the list then ask for a new license number until a valid license number is entered.
while [[ ! $licensenumber =~ ^[0-9]+$ ]] || [ $licensenumber -gt $licensecount ]; do
echo "License number must be a number between 1 and $licensecount."
read -p "Enter the number of the license you want to use [11]: " licensenumber
licensenumber=${licensenumber:-11}
done
licensespdx_id=$(echo "$licenses" | sed -n "$licensenumber p")
licenseurl=$(curl -s https://api.github.com/licenses | jq -r ".[$((licensenumber - 1))].url")
licensetext=$(curl -s $licenseurl | jq -r '.body')
echo "$licensetext" > LICENSE
fi
# Ask if the user wants to include a changelog, default is no.
read -p "Include a changelog? [y/N] " changelog
changelog=${changelog:-n}
# If changelog is not yes or no then ask for a new changelog option until a valid changelog option is entered.
while [[ ! $changelog =~ ^[yYnN]$ ]]; do
echo "Changelog option must be y or n."
read -p "Include a changelog? [y/N] " changelog
changelog=${changelog:-n}
done
# If changelog is yes then create a changelog file.
if [ $changelog = "y" ] || [ $changelog = "Y" ]; then
echo "# Changelog" > CHANGELOG.md
fi
# Create cabal file.
cat <<EOF > $project.cabal
cabal-version: 2.4
name: $project
version: 0
tested-with: GHC ==8.6.3 || ==8.8.3 || ==8.10.5
description: $description
author: $name
maintainer: $name - $email
copyright: $(date '+%Y-%m-%d') $name
build-type: Simple
extra-doc-files:
README.md
EOF
# If changelog is yes then add the changelog to the cabal file generated above.
if [ $changelog = "y" ] || [ $changelog = "Y" ]; then
cat <<EOF >> $project.cabal
CHANGELOG.md
EOF
fi
# If license is yes then add the license to the cabal file generated above.
if [ $license = "y" ] || [ $license = "Y" ]; then
cat <<EOF >> $project.cabal
license: $licensespdx_id
license-file: LICENSE
EOF
fi
# If license is no then add NONE to the license field in the cabal file generated above.
if [ $license = "n" ] || [ $license = "N" ]; then
cat <<EOF >> $project.cabal
license: NONE
EOF
fi
cat <<EOF >> $project.cabal
common common-options
build-depends: base >=4.9 && <5
default-language: Haskell2010
default-extensions:
ghc-options:
executable $project-exe
import: common-options
hs-source-dirs: $srcdir
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
EOF
# If neither test suite or benchmark suite is yes then add the following to the cabal file generated above.
if [ $testsuite != "y" ] && [ $testsuite != "Y" ] && [ $benchmarksuite != "y" ] && [ $benchmarksuite != "Y" ]; then
cat <<EOF >> $project.cabal
build-depends:
EOF
else
cat <<EOF >> $project.cabal
build-depends: $project
library
import: common-options
hs-source-dirs: lib
exposed-modules: Lib
build-depends:
EOF
mkdir lib
pushd lib > /dev/null
cat <<EOF > Lib.hs
module Lib where
EOF
popd > /dev/null
fi
# If test suite is yes then add the test suite to the cabal file generated above.
if [ $testsuite = "y" ] || [ $testsuite = "Y" ]; then
cat <<EOF >> $project.cabal
test-suite $project-test
import: common-options
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends:
, $project
, hspec
, HUnit
, tasty
, QuickCheck
ghc-options: -threaded -rtsopts -with-rtsopts=-N
EOF
fi
# If benchmark suite is yes then add the benchmark suite to the cabal file generated above.
if [ $benchmarksuite = "y" ] || [ $benchmarksuite = "Y" ]; then
cat <<EOF >> $project.cabal
benchmark $project-bench
import: common-options
type: exitcode-stdio-1.0
hs-source-dirs: bench
main-is: Main.hs
build-depends:
, $project
, criterion
ghc-options: -threaded -rtsopts -with-rtsopts=-N
EOF
fi
# Create cabal.project file.
cat <<EOF > cabal.project
packages: ./
EOF
# If test suite is yes then add the test suite to the cabal.project file generated above.
if [ $testsuite = "y" ] || [ $testsuite = "Y" ]; then
cat <<EOF >> cabal.project
package $project
tests: true
EOF
fi
# Create hie.yaml file.
cat <<EOF > hie.yaml
cradle:
multi:
# - path: "ignore/"
# config: {cradle: {none: }}
- path: "$srcdir"
config: {cradle: {cabal: {component: "$project:$project-exe"}}}
EOF
# if either test suite or benchmark suite is yes then add the following to the hie.yaml file generated above.
if [ $testsuite = "y" ] || [ $testsuite = "Y" ] || [ $benchmarksuite = "y" ] || [ $benchmarksuite = "Y" ]; then
cat <<EOF >> hie.yaml
- path: "lib"
config: {cradle: {cabal: {component: "lib:$project"}}}
EOF
fi
# If test suite is yes then add the test suite to the hie.yaml file generated above.
if [ $testsuite = "y" ] || [ $testsuite = "Y" ]; then
cat <<EOF >> hie.yaml
- path: "test"
config: {cradle: {cabal: {component: "$project:$project-test"}}}
EOF
fi
# If benchmark suite is yes then add the benchmark suite to the hie.yaml file generated above.
if [ $benchmarksuite = "y" ] || [ $benchmarksuite = "Y" ]; then
cat <<EOF >> hie.yaml
- path: "bench"
config: {cradle: {cabal: {component: "$project:$project-bench"}}}
EOF
fi
# Create .hlint.yaml file.
cat <<EOF > .hlint.yaml
# Example hlint file
# Generalise map to fmap, ++ to <>. Off by default
- group:
name: generalise
enabled: true
- ignore:
name: Use section
- ignore:
name: Use infix
EOF
# Create flake.nix
cat <<EOF > flake.nix
{
description = "$description";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = inputs:
let
overlay = final: prev: {
haskell = prev.haskell // {
packageOverrides = hfinal: hprev:
prev.haskell.packageOverrides hfinal hprev // {
$project = hfinal.callCabal2nix "$project" ./. { };
};
};
$project = final.haskell.lib.compose.justStaticExecutables final.haskellPackages.$project;
};
perSystem = system:
let
pkgs = import inputs.nixpkgs { inherit system; overlays = [ overlay ]; };
hspkgs = pkgs.haskellPackages;
in
{
devShell = hspkgs.shellFor {
withHoogle = true;
packages = p: [ p.$project ];
buildInputs = [
hspkgs.cabal-install
hspkgs.haskell-language-server
hspkgs.hlint
hspkgs.ormolu
pkgs.bashInteractive
];
};
defaultPackage = pkgs.$project;
};
in
{ inherit overlay; } //
inputs.flake-utils.lib.eachDefaultSystem perSystem;
}
EOF
# Ask if the user wants to create a git repository. Default is no.
read -p "Do you want to initialize a git repository? [y/N] " gitrepo
gitrepo=${gitrepo:-n}
# If gitrepo is not yes or no then ask for a new changelog option until a valid changelog option is entered.
while [[ ! $gitrepo =~ ^[yYnN]$ ]]; do
echo "Initialize git repo option must be y or n."
read -p "Do you want to initialize a git repository? [y/N] " gitrepo
gitrepo=${gitrepo:-n}
done
# If gitrepo is yes then initialize a git repository.
if [ $gitrepo = "y" ] || [ $gitrepo = "Y" ]; then
git init
git add .
git commit -m "Initial commit"
fi
popd > /dev/null
}
# If the first argument is -r or --run then run the program.
if [ "$1" = "run" ]; then
usage
exit 0
fi