@@ -21,11 +21,17 @@ ARTIFACTS_CHANGE_DETECTION_HASH_FILE=${ARTIFACTS_CHANGE_DETECTION_HASH_FILE:-"ar
21
21
CHANGE_DETECTION_HASH_FILE=${CHANGE_DETECTION_HASH_FILE:- " ${ARTIFACTS_CHANGE_DETECTION_HASH_FILE} " } # Name of the file that contains the hash code of the file list for change detection
22
22
CHANGE_DETECTION_HASH_FILE_PATH=${CHANGE_DETECTION_HASH_FILE_PATH:- " ./${ARTIFACTS_DIRECTORY} /${CHANGE_DETECTION_HASH_FILE} " } # Default path of the file that contains the hash code of the file list for change detection. Can be overridden by a command line option.
23
23
24
+ COLOR_INFO=' \033[0;30m' # dark grey
25
+ COLOR_ERROR=' \033[0;31m' # red
26
+ COLOR_DEFAULT=' \033[0m'
27
+
24
28
# Function to display script usage
25
29
usage () {
26
- echo " Usage: $0 [--readonly]"
27
- echo " [--paths <comma separated list of file and directory names> (default=artifacts)]"
28
- echo " [--hashfile <path to the file that contains the hash for change detection> (default=env var CHANGE_DETECTION_HASH_FILE_PATH)]"
30
+ echo -e " ${COLOR_ERROR} " >&2
31
+ echo " Usage: $0 [--readonly]" >&2
32
+ echo " [--paths <comma separated list of file and directory names> (default=artifacts)]" >&2
33
+ echo " [--hashfile <path to the file that contains the hash for change detection> (default=env var CHANGE_DETECTION_HASH_FILE_PATH)]" >&2
34
+ echo -e " ${COLOR_DEFAULT} " >&2
29
35
exit 1
30
36
}
31
37
@@ -52,24 +58,51 @@ while [[ $# -gt 0 ]]; do
52
58
shift
53
59
;;
54
60
* )
55
- echo " detectChangedFiles: Error: Unknown option: ${key} "
61
+ echo -e " ${COLOR_ERROR} detectChangedFiles: Error: Unknown option: ${key}${COLOR_DEFAULT} " >&2
56
62
usage
57
63
;;
58
64
esac
59
65
shift || true # ignore error when there are no more arguments
60
66
done
61
67
68
+ exit_failed () {
69
+ case " $0 " in
70
+ * /sh) return 1 ;; # Script is sourced
71
+ * ) exit 1 ;; # Script is executed directly
72
+ esac
73
+ }
74
+
75
+ exit_successful () {
76
+ case " $0 " in
77
+ * /sh) return 0 ;; # Script is sourced
78
+ * ) exit 0 ;; # Script is executed directly
79
+ esac
80
+ }
81
+
62
82
if ${readonlyMode} ; then
63
- echo " detectChangedFiles: Readonly mode activated. Change detection file won't be created." >&2
83
+ echo -e " ${COLOR_INFO} detectChangedFiles: Readonly mode activated. Change detection file won't be created.${COLOR_DEFAULT} " >&2
64
84
else
65
- echo " detectChangedFiles: ${hashFilePath} will be used as change detection file." >&2
85
+ echo -e " ${COLOR_INFO} detectChangedFiles: ${hashFilePath} will be used as change detection file.${COLOR_DEFAULT} " >&2
66
86
fi
67
87
68
88
# Check if the paths parameter exist
69
89
if [ -z " ${paths} " ] ; then
70
90
echo 0 # 0=No change detected. The path list is empty. There is nothing to compare. Therefore assume that there are no changes.
71
- exit 0
72
- fi
91
+ exit_successful
92
+ fi
93
+
94
+ # Check all paths if they are valid files or valid directories
95
+ for path in ${paths// ,/ } ; do
96
+ if [ -f " ${path} " ] ; then
97
+ continue # Valid file
98
+ fi
99
+ if [ -d " ${path} " ] ; then
100
+ continue # Valid directory
101
+ fi
102
+ # Neither a valid directory and file
103
+ echo -e " ${COLOR_ERROR} detectChangedFiles: Error: Invalid path: ${path}${COLOR_DEFAULT} " >&2
104
+ exit_failed
105
+ done
73
106
74
107
# Function to get file size
75
108
get_file_size () {
@@ -80,23 +113,46 @@ get_file_size() {
80
113
fi
81
114
}
82
115
116
+ isMacOS () {
117
+ [ " $( uname -s) " = " Darwin" ]
118
+ }
119
+
83
120
# Function to process a single path
84
121
file_names_and_sizes () {
85
122
if [ -d " $1 " ]; then
123
+ # TODO Remove after debugging
124
+ echo " detectChangedFiles: Checking directory $1 " >&2
125
+
86
126
# If it's a directory, list all files inside
87
127
# except for "node_modules", "target", "temp" and the change detection file itself
88
- find -L " $1 " \
89
- -type d -name " node_modules" -prune -o \
90
- -type d -name " target" -prune -o \
91
- -type d -name " temp" -prune -o \
92
- -type d -name " .reports" -prune -o \
93
- -not -path " ${hashFilePath} " \
94
- -type f \
95
- -exec stat -f " %N %z" {} + \
96
- | sort
128
+ if isMacOS; then
129
+ find -L " $1 " \
130
+ -type d -name " node_modules" -prune -o \
131
+ -type d -name " target" -prune -o \
132
+ -type d -name " temp" -prune -o \
133
+ -type d -name " .reports" -prune -o \
134
+ -not -path " ${hashFilePath} " \
135
+ -type f \
136
+ -exec stat -f " %N %z" {} + \
137
+ | sort
138
+ else
139
+ find -L " $1 " \
140
+ -type d -name " node_modules" -prune -o \
141
+ -type d -name " target" -prune -o \
142
+ -type d -name " temp" -prune -o \
143
+ -type d -name " .reports" -prune -o \
144
+ -not -path " ${hashFilePath} " \
145
+ -type f \
146
+ -exec stat --printf=" %n %s\n" {} + \
147
+ | sort
148
+ fi
97
149
elif [ -f " $1 " ]; then
98
- # If it's a file, just echo the file path
99
- stat -f " %N %z" < " $1 "
150
+ # The path is a file. Print its path and size.
151
+ if isMacOS; then
152
+ stat -f " %N %z" " $1 "
153
+ else
154
+ stat --printf=" %n %s\n" " $1 "
155
+ fi
100
156
fi
101
157
}
102
158
@@ -113,7 +169,7 @@ get_md5_checksum_of_all_file_names_and_sizes() {
113
169
local files_and_their_size; files_and_their_size=$( file_names_and_sizes " ${path} " )
114
170
all_files_and_sizes=" ${all_files_and_sizes}${files_and_their_size} "
115
171
processed_paths=$(( processed_paths + 1 ))
116
- echo -ne " detectChangedFiles: Calculate checksum progress: ($processed_paths /$total_paths )\r" >&2
172
+ echo -ne " ${COLOR_INFO} detectChangedFiles: Calculate checksum progress: ($processed_paths /$total_paths )\r${COLOR_DEFAULT} " >&2
117
173
done
118
174
echo " " >&2
119
175
echo " ${all_files_and_sizes} " | openssl md5 | awk ' {print $2}'
@@ -133,12 +189,12 @@ if [ ! -f "${hashFilePath}" ] ; then
133
189
mkdir -p " ${hash_file_directory} "
134
190
# Create the file containing the hash of the files list to a new file for the next call
135
191
echo " ${CURRENT_FILES_HASH} " > " ${hashFilePath} "
136
- echo " detectChangedFiles: Change detection file created" >&2
192
+ echo -e " ${COLOR_INFO} detectChangedFiles: Change detection file created. ${COLOR_DEFAULT} " >&2
137
193
else
138
- echo " detectChangedFiles: Skipping file creation with content (=hash) ${CURRENT_FILES_HASH} " >&2
194
+ echo -e " ${COLOR_INFO} detectChangedFiles: Skipping file creation with content (=hash) ${CURRENT_FILES_HASH}${COLOR_DEFAULT } " >&2
139
195
fi
140
196
echo 1 # 1=Change detected and change detection file created
141
- exit 0
197
+ exit_successful
142
198
fi
143
199
144
200
# Assume that there is no change if the saved hash is equal to the current one.
149
205
if ! ${readonlyMode} ; then
150
206
# Write the updated hash into the file containing the hash of the files list for the next call
151
207
echo " ${CURRENT_FILES_HASH} " > " ${hashFilePath} "
152
- echo " detectChangedFiles: Change detection file updated" >&2
208
+ echo -e " ${COLOR_INFO} detectChangedFiles: Change detection file updated. ${COLOR_DEFAULT} " >&2
153
209
else
154
- echo " detectChangedFiles: Skipping file update with content (=hash) ${CURRENT_FILES_HASH} " >&2
210
+ echo -e " ${COLOR_INFO} detectChangedFiles: Skipping file update with content (=hash) ${CURRENT_FILES_HASH} . ${COLOR_DEFAULT }" >&2
155
211
fi
156
212
echo 2 # 2=Change detected and change detection file updated
157
213
fi
0 commit comments