comparison scripts/create_structure.sh @ 0:a17a4894f4bd draft default tip

Initial commit converting git to mercurial
author Alfred Burgess <aburgess@ucc.asn.au>
date Sun, 14 Apr 2024 19:35:23 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a17a4894f4bd
1 #!/bin/bash
2
3 # Script to create the Fam-tree project structure
4
5 # Set project name
6 project_name="fam-tree"
7 root_dir="."
8
9 # Set environment variables for your project
10 export PROJECT_ENV_VAR1="value1"
11 export PROJECT_ENV_VAR2="value2"
12
13 # Set logs directory
14 logs_directory="$root_dir/logs"
15
16 # Feature Flags
17 feature_git=0
18 feature_docker=0
19
20 # Error flag
21 error_flag=0
22
23 # Function to display messages to both console and log file
24 log_error() {
25 echo "E: $(date +"%Y-%m-%d %H:%M:%S") - $1"
26 }
27 log_message() {
28 echo "I: $(date +"%Y-%m-%d %H:%M:%S") - $1"
29 }
30
31 # Function to create a directory if it doesn't exist
32 create_directory() {
33 if [ ! -d "$1" ]; then
34 mkdir -p "$1"
35 if [ $? -ne 0 ]; then
36 log_error "Error: Failed to create directory '$1'" >&2
37 error_flag=1
38 else
39 log_message "Created directory: $1"
40 fi
41 else
42 log_message "Directory exists. Skipped: $1"
43 fi
44 }
45
46 # Function to create a file if it doesn't exist
47 create_file() {
48 if [ ! -f "$1" ]; then
49 touch "$1"
50 if [ $? -ne 0 ]; then
51 log_error "Error: Failed to create file '$1'" >&2
52 error_flag=1
53 else
54 log_message "Created file: $1"
55 fi
56 else
57 log_message "File exists. Skipped: $1"
58 fi
59 }
60
61 install_dependency() {
62 local dependency="$1"
63
64 if ! command -v "$dependency" &> /dev/null; then
65 echo "Installing $dependency..."
66 case "$dependency" in
67 "docker")
68 # Install Docker using the official scrip
69 curl -fsSL https://get.docker.com -o get-docker.sh
70 sudo sh get-docker.sh
71 rm get-docker.sh
72 ;;
73 *)
74 # For other dependencies, use your package manager
75 # Add more conditions for different package managers (e.g., apt, yum)
76 echo "Please install $dependency manually."
77 error_flag=1
78 ;;
79 esac
80 fi
81 }
82
83 check_tool_exists() {
84 local tool_name=$1
85 command -v "$tool_name" >/dev/null 2>&1
86 return $?
87 }
88
89 if check_tool_exists "git"; then
90 feature_git=1
91 fi
92
93 if check_tool_exists "docker"; then
94 feature_docker=1
95 fi
96
97 # Function to generate GitLab CI configuration
98 generate_gitlab_ci() {
99 local gitlab_ci_file=".gitlab-ci.yml"
100 cat <<EOL > "$gitlab_ci_file"
101 stages:
102 - build
103 - test
104 - deploy
105
106 variables:
107 DOCKER_COMPOSE_VERSION: "1.27.4"
108
109 before_script:
110 - docker --version
111 - docker-compose --version
112
113 services:
114 - docker:$DOCKER_COMPOSE_VERSION
115
116 build:
117 stage: build
118 script:
119 - docker-compose build
120
121 test:
122 stage: test
123 script:
124 - docker-compose run --rm backend /your/test/command
125
126 deploy:
127 stage: deploy
128 script:
129 - echo "Deploy your application here"
130 EOL
131
132 echo "GitLab CI file created: $gitlab_ci_file"
133 }
134
135
136 # Create logs directory
137 create_directory "$logs_directory"
138
139 # Create log file with timestamp
140 log_file="$logs_directory/$(date +"%Y%m%d%H%M%S").log"
141 touch "$log_file"
142
143 # Redirect all subsequent output to the log file
144 exec 3>&1 4>&2 1>>"$log_file" 2>&1
145
146
147 # Display welcome message in the log
148 log_message "Creating project structure for $project_name"
149
150 # Create backend directory
151 create_directory "$root_dir/backend"
152 create_file "$root_dir/backend/main.go"
153
154 # Create API directory
155 create_directory "$root_dir/backend/api"
156 create_file "$root_dir/backend/api/family-tree.proto"
157
158 # Create database directory
159 create_directory "$root_dir/backend/database"
160 create_directory "$root_dir/backend/database/migrations"
161
162 # Create cmd directory for server configurations
163 create_directory "$root_dir/backend/cmd"
164 create_directory "$root_dir/backend/cmd/server"
165 create_file "$root_dir/backend/cmd/server/config.yaml"
166 create_file "$root_dir/backend/cmd/server/start.sh"
167 create_file "$root_dir/backend/cmd/server/stop.sh"
168
169 # Create frontend directory
170 create_directory "$root_dir/frontend"
171 create_directory "$root_dir/frontend/src"
172 create_directory "$root_dir/frontend/src/components"
173 create_directory "$root_dir/frontend/src/services"
174
175 # Create proto directory for shared .proto definitions
176 create_directory "$root_dir/proto"
177 create_file "$root_dir/proto/family-tree.proto"
178
179 # Create nginx directory
180 create_directory "$root_dir/nginx"
181 create_file "$root_dir/nginx/nginx.conf"
182
183 # Create backend directory
184 create_directory "$root_dir/backend"
185 create_file "$root_dir/backend/dockerfile"
186
187 # Create frontend directory
188 create_directory "$root_dir/frontend"
189 create_file "$root_dir/frontend/dockerfile"
190
191 # Create docker-compose.yml for Docker Compose configuration
192 create_file "$root_dir/docker-compose.yml"
193
194 # Create scripts directory for setup, start, and stop scripts
195 create_directory "$root_dir/scripts"
196 create_file "$root_dir/scripts/setup.sh"
197 create_file "$root_dir/scripts/start.sh"
198 create_file "$root_dir/scripts/stop.sh"
199
200 # Create docs directory for documentation
201 create_directory "$root_dir/docs"
202 create_file "$root_dir/docs/api.md"
203 create_file "$root_dir/docs/database.md"
204
205 # Create cron directory for automated scheduled actions
206 create_directory "$root_dir/cron"
207 create_file "$root_dir/cron/cronjobs"
208 create_file "$root_dir/cron/tasks"
209
210 # Create tests directory for automated tests
211 create_directory "$root_dir/tests"
212
213 # Create ci-cd directory for GitLab CI/CD configuration
214 create_directory "$root_dir/ci-cd"
215 create_file "$root_dir/ci-cd/gitlab-ci.yml"
216
217 # Create config directory for application settings
218 create_directory "$root_dir/config"
219 create_file "$root_dir/config/config.yaml"
220
221 # Create README.md for project documentation
222 create_file "$root_dir/README.md"
223
224 # Create .gitignore for Git ignore file
225 create_file "$root_dir/.gitignore"
226 # Function to create a gitignore file
227 create_gitignore() {
228 cat <<EOL > .gitignore
229 # Ignore logs
230 logs/
231
232 # Ignore compiled binaries
233 *.exe
234 *.dll
235 *.out
236
237 # Ignore user-specific files
238 .idea/
239 .vscode/
240
241 # Ignore system files
242 .DS_Store
243 Thumbs.db
244
245 # Ignore node modules
246 node_modules/
247
248 # Ignore compiled TypeScript
249 *.js
250 *.js.map
251
252 # Ignore Go binary
253 *.go
254
255 # Ignore Docker artifacts
256 dockerfile.*
257 docker-compose.yml
258
259 # Ignore dependencies
260 vendor/
261
262 # Ignore compiled Python files
263 *.pyc
264 __pycache__/
265
266 # Ignore compiled Java files
267 *.class
268
269 # Ignore temporary files
270 *.tmp
271
272 # Ignore editor backup files
273 *~
274 *.swp
275 *.swo
276 *.swn
277 *.swm
278 *.swl
279
280 # Ignore environment variables file
281 .env
282
283 # Ignore build artifacts
284 build/
285 dist/
286
287 # Ignore database migrations
288 database/migrations/*.sql
289 EOL
290 }
291
292 initialize_git_repository() {
293 if [ "$feature_git" -ne 0 ]; then
294 if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1;
295 then
296 log_message "Git repo already enabled"
297 else
298 git init --initial-branch=main
299 if [ $? -ne 0 ]; then
300 log_error "Error: Failed to initialize Git repository" >&2
301 error_flag=1
302 else
303 git add -A
304 git commit -m "Initial commit for project: $project_name"
305 fi
306 fi
307 else
308 log_message "Git not enabled"
309 fi
310 }
311
312 # Function to create a pre-commit hook
313 create_pre_commit_hook() {
314 if [ "$feature_git" -ne 0 ]; then
315 local pre_commit_hook=".git/hooks/pre-commit"
316 local hook_script="#!/bin/bash\n\n# This script runs before every commit to perform linting, formatting, or other checks\n# Add your checks or commands here\n\n# Example: Run a linter\n# lint_command=\"your-linter-command\"\n# \$lint_command\n\n# Example: Run a formatter\n# format_command=\"your-formatter-command\"\n# \$format_command\n\n# If any command fails, the commit will be aborted\n"
317
318 create_file "$pre_commit_hook"
319 echo "$hook_script" > "$pre_commit_hook"
320 chmod +x "$pre_commit_hook"
321 fi
322 }
323
324 # Check for errors
325 if [ "$error_flag" -eq 1 ]; then
326 echo "Error: One or more errors occurred during script execution. See the log for details." >&2
327 exit 1
328 fi
329
330 # Function to create README with a basic structure
331 create_readme() {
332 local readme_file="README.md"
333 local readme_content="# $project_name\nThis is a basic README file for the project."
334 create_file "$readme_file"
335 echo "$readme_content" > "$readme_file"
336 }
337
338 dependencies=("git" "docker")
339 for dep in "${dependencies[@]}"; do
340 echo "$dep"
341 done
342
343 # Create gitignore file
344 create_gitignore
345 initialize_git_repository
346 create_readme
347 generate_gitlab_ci
348
349 # Display success message in the log
350 echo "Gitignore file created successfully"
351
352 # Display success message in the log
353 log_message "Project structure for $project_name created successfully"
354
355 # Restore default output and close the log file
356 exec 1>&3 2>&4
357 exec 3>&- 4>&-
358
359 # Display log file path
360 echo "Log file created at: $log_file"