-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
executable file
·291 lines (240 loc) · 7.03 KB
/
init.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
#!/bin/bash
######################################################################################
# SCRIPT: Allows to quickly generate a new Go project as a Go module #
# AUTHOR: github.com/maevadevs #
# CALL SIGN: bash init.sh -p <Project_Dir> -o <Out_File> -m <Module_Root_Path> #
# CALL EXAMPLE: bash init.sh -p hello_world -o hello -m github.com/go_projects #
######################################################################################
# Get Current Directory
CURRENT_DIR=$PWD;
# Get the project name and module root arguments from the user
while getopts p:o:m: flag
do
case "${flag}" in
p) PROJECT_NAME=${OPTARG};;
o) OUT_FILE=${OPTARG};;
m) MODULE_ROOT_PATH=${OPTARG};;
esac
done
# The new project's directory
PROJECT_DIR="$CURRENT_DIR/$PROJECT_NAME"
echo "";
echo "Project Name: $PROJECT_NAME";
echo "Module Root Path: $MODULE_ROOT_PATH";
echo "";
echo "Creating a new Go project: $PROJECT_DIR ..."
# Create directory for the project if it does not exist yet
if test -d $PROJECT_DIR; then
echo "The project directory already exist. Skipping creating a new directory.";
echo "";
else
echo "Creating a new project directory...";
echo "$PROJECT_DIR";
mkdir -p "$PROJECT_DIR";
echo "Done."
echo "";
fi
# Move into the project directory
cd $PROJECT_DIR;
# Create a new Go module if it does not exist yet
if test -f "$PROJECT_DIR/go.mod"; then
echo "The go.mod file already exist. Skipping creating a new go module.";
echo "";
else
echo "Creating a new Go module...";
go mod init "$MODULE_ROOT_PATH/$PROJECT_NAME";
echo "Done."
echo "";
fi
# Create a .gitignore
if test -f "$PROJECT_DIR/.gitignore"; then
echo "The .gitignore file already exist. Skipping creating a new .gitignore.";
echo "";
else
echo "Creating a new .gitignore file...";
touch ".gitignore";
# Add default placeholder contents
echo "\
# Project Specific
bin/
bin/*
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file (remove the comment below to include it)
# go.work
# VS Code
.vscode
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
.vsix
" >> .gitignore;
echo "Done."
echo "";
fi
# Create a readme.md with placeholder
if test -f "$PROJECT_DIR/readme.md"; then
echo "The readme.md file already exist. Skipping creating a new readme.md.";
echo "";
else
echo "Creating a new readme.md file...";
touch "readme.md";
# Add default placeholder contents
echo "\
# $PROJECT_NAME
---
" >> readme.md;
echo "Done."
echo "";
fi
# Create a makefile for the build process
if test -f "$PROJECT_DIR/makefile"; then
echo "The makefile already exist. Skipping creating a new makefile.";
echo "";
else
echo "Creating a new makefile...";
touch "makefile";
# Add default placeholder contents
echo "\
# NOTE: Make sure all indentations use actual tabs
# DEFAULT_GOAL specifies the default target
# This is run when no target is provided during the call
.DEFAULT_GOAL := try
# Target definitions
# .PHONY helps avoid possible name-collisions with other directory or file names on the computer
.PHONY: fmt vet build bld build-release bld-rls run run-release run-rls try-build-run tbr try-run try
# Target
fmt:
# Task: Format all source files
cd src
go fmt ./...
cd ..
# Target
vet: fmt
# Task: Verify any possible errors
cd src
go vet ./...
cd ..
# Target
build: vet
# Task: Build module
go build -o bin/debug/$OUT_FILE src/main.go
# Target: Alias to build
bld: vet
# Task: Build module
go build -o bin/debug/$OUT_FILE src/main.go
# Target
build-release: vet
# Task: Build module for release
go build -ldflags=\"-s -w\" -o bin/release/$OUT_FILE src/main.go
# Target: ALias to build-release
bld-rls: vet
# Task: Build module for release
go build -ldflags=\"-s -w\" -o bin/release/$OUT_FILE src/main.go
# Target
run: build
# Task: Build module then run
bin/debug/$OUT_FILE \$(ARGS)
# Target
run-release: build-release
# Task: Build module then run
bin/release/$OUT_FILE \$(ARGS)
# Target: ALias to run-release
run-rls: build-release
# Task: Build module then run
bin/release/$OUT_FILE \$(ARGS)
# Target
try-build-run: vet
# Task: Build module, run, then remove built binary
if test -f bin/debug/$OUT_FILE-Temp; then \\
rm -f bin/debug/$OUT_FILE-Temp; \\
fi
go build -o bin/debug/$OUT_FILE-Temp src/main.go
bin/debug/$OUT_FILE-Temp \$(ARGS)
rm -f bin/debug/$OUT_FILE-Temp
# Target: Alias to try-build-run
tbr: vet
# Task: Build module, run, then remove built binary
if test -f bin/debug/$OUT_FILE-Temp; then \\
rm -f bin/debug/$OUT_FILE-Temp; \\
fi
go build -o bin/debug/$OUT_FILE-Temp src/main.go
bin/debug/$OUT_FILE-Temp \$(ARGS)
rm -f bin/debug/$OUT_FILE-Temp
# Target
try-run: vet
# Task: Test-Run the module without building anything
go run src/main.go
# Target: Alias to try-run
try: vet
# Task: Test-Run the module without building anything
go run src/main.go
" >> makefile;
echo "Done."
echo "";
fi
# Create a new "src" folder if does not exist yet
if test -d "$PROJECT_DIR/src"; then
echo "The src directory exist. Skipping creating a new src directory.";
echo "";
else
echo "Creating a new src directory...";
mkdir -p "src";
echo "Done."
echo "";
fi
# Create a new "tests" folder if does not exist yet
if test -d "$PROJECT_DIR/tests"; then
echo "The tests directory exist. Skipping creating a new tests directory.";
echo "";
else
echo "Creating a new tests directory...";
mkdir -p "tests";
echo "Done."
echo "";
fi
# Create a new "main.go" in "src" if does not exist yet
if test -f "$PROJECT_DIR/src/main.go"; then
echo "The main.go file exist. Skipping creating a main.go file.";
echo "";
else
echo "Creating a new main.go file...";
touch "src/main.go";
# Add default placeholder contents
echo "\
package main
import \"fmt\"
// This is the main entry of the application.
func main() {
fmt.Println(\"Hello world!\")
}
// AVAILABLE COMMANDS
// ------------------
// make Default to \`make try\`
// make fmt Format all source files
// make vet Verify any possible errors
// make bld Build module
// make bld-rls Build module for release, strip symbols
// make run Build module then run
// make run-rls Build module for release then run
// make tbr Build module, run, then remove built binary
// make try Test-Run the module without building anything
" >> src/main.go;
echo "Done."
echo "";
fi
echo "All processes complete. Exiting."
exit