-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.saintMethods
139 lines (121 loc) · 4.25 KB
/
.saintMethods
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
#!/bin/bash
#Templates for creating new projects
# Returns the list of current projects
saint_project_list() {
# Count the number of projects
local projectCount=0
local projects=("${SAINT_PROJECTS_FOLDER}/"*)
for project in "${projects[@]}"; do
if [ -f "${project}/.${projName}" ]; then
let projectCount+=1
fi
done
# Display message based on the number of projects
if [ "$projectCount" -eq 1 ]; then
saint_msg "This is the project you have now:"
else
saint_msg "These are the projects you have now:"
fi
# ${newProjName} over existing projects and list them
for project in "${projects[@]}"; do
projName=$(basename "$project")
if [ -f "${project}/.${projName}" ]; then
echo "${projName}"
fi
done
}
saint_get_project_list_from_folders_array() {
local projects=("$@")
local project_list=()
for project in "${projects[@]}"; do
local projName=$(basename "$project")
if [ -f "${project}/.${projName}" ]; then
project_list+=("$projName")
fi
done
echo "${project_list[@]}"
}
saint_add_command() {
local project_name=$1
local command="\ \ \ \ \ \ \ \ ${project_name}) ${project_name}_main_commands \"\${projectCommand} \$1\" \${@:2};;"
if ! grep -q "${command}" ${SAINT_METHODS_MAIN_COMMANDS_FILE}; then
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "/#BEGIN-MAIN-COMMANDS/a\\
${command}
" ${SAINT_METHODS_MAIN_COMMANDS_FILE}
else
sed -i "/#BEGIN-MAIN-COMMANDS/a ${command}" ${SAINT_METHODS_MAIN_COMMANDS_FILE}
fi
if [ $? -ne 0 ]; then
saint_msg "${RED}ERROR${NC} :: Failed to add command for project ${project_name}."
return 1
fi
fi
}
saint_remove_command() {
local project_name=$1
local command="\ \ \ \ \ \ \ \ ${project_name}) ${project_name}_main_commands \"\${projectCommand} \$1\" \${@:2};;"
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "\|${command}|d" ${SAINT_METHODS_MAIN_COMMANDS_FILE}
else
sed -i "\|${command}|d" ${SAINT_METHODS_MAIN_COMMANDS_FILE}
fi
}
saint_validate_project_files() {
local project_dir=$1
local project_name=$2
local mandatory_files=(".alias" ".${project_name}" ".mainCommands" ".variables")
for file in "${mandatory_files[@]}"; do
if [ ! -f "${project_dir}/${file}" ]; then
saint_msg "${RED}ERROR${NC} :: Missing mandatory file ${file} in project ${project_name}."
return 1
fi
done
return 0
}
saint_load_project_commands() {
local project_name
# Check if the SAINT_PROJECTS_FOLDER exists, if not, create it
if [ ! -d "${SAINT_PROJECTS_FOLDER}" ]; then
mkdir -p "${SAINT_PROJECTS_FOLDER}"
fi
# Check if the projects directory exists and contains folders
if [ -d "${SAINT_PROJECTS_FOLDER}" ] && [ "$(find ${SAINT_PROJECTS_FOLDER} -mindepth 1 -type d | head -n 1)" ]; then
for project_dir in "${SAINT_PROJECTS_FOLDER}/"*; do
if [ -d "$project_dir" ]; then
project_name=$(basename "$project_dir")
if saint_validate_project_files "$project_dir" "$project_name"; then
saint_add_command "$project_name"
fi
fi
done
else
saint_msg "No projects found in ${SAINT_PROJECTS_FOLDER}."
return 1
fi
}
saint_update_project_commands() {
# Clear commands between BEGIN and END
sed -i '/#BEGIN-MAINCOMMANDS/,/#END-MAINCOMMANDS/{//!d;}' ${SAINT_METHODS_MAIN_COMMANDS_FILE}
saint_load_project_commands
saint_msg "${GREEN}Project commands updated successfully.${NC}"
}
saint_initialize_main_commands() {
if [ ! -d "${SAINT_TEMP_FOLDER}" ]; then
mkdir "${SAINT_TEMP_FOLDER}"
fi;
if [ ! -f ${SAINT_METHODS_MAIN_COMMANDS_FILE} ] || [ ! -d $SAINT_TEMP_FOLDER ]; then
local templatePath="${PROJECT_PATH}/templates/saint_temps/.saintMainCommands.txt"
local fileNameWithPath="${SAINT_METHODS_MAIN_COMMANDS_FILE}"
# Reading template content
local templateContent=$(<"$templatePath")
# Writing writing new contents on the path
echo "$templateContent" > "$fileNameWithPath"
saint_update_project_commands && saint_rebuild_help || return 1
else
return 1
fi
}
source "${PROJECT_PATH}/features/methods/saint_help_manager/.saintHelpManager"
source "${PROJECT_PATH}/features/methods/saint_template_manager/.saintTemplateManager"
source "${PROJECT_PATH}/features/methods/saint_project_manager/.saintProjectManager"