š¦āš¦ Store and retrieve files on the Atmosphere
1#!/usr/bin/env bash
2# atfile-devel=ignore-build
3
4function atfile.build() {
5 # shellcheck disable=SC2154
6 [[ $_os != "linux" ]] && atfile.die "Only available on Linux (GNU)\nā³ Detected OS: $_os"
7
8 function atfile.build.get_devel_value() {
9 local file="$1"
10 local value="$2"
11 local found_line
12
13 found_line="$(grep '^# atfile-devel=' "$file" | head -n1)"
14 if [[ -n "$found_line" ]]; then
15 local devel_values="${found_line#*=}"
16 IFS=',' read -ra arr <<< "$devel_values"
17 for v in "${arr[@]}"; do
18 if [[ "$v" == "$value" ]]; then
19 echo 1
20 fi
21 done
22 fi
23 }
24
25 function atfile.build.replace_template_var() {
26 string="$1"
27 key="$2"
28 value="$3"
29
30 sed -s "s|{:$key:}|$value|g" <<< "$string"
31 }
32
33 function atfile.build.pad_emoji() {
34 emoji="$1"
35 padding="$2"
36
37 # shellcheck disable=SC2154
38 if [[ $_ci == "tangled" ]]; then
39 atfile.say.inline "$emoji"
40 else
41 atfile.say.inline "$emoji$padding"
42 fi
43 }
44
45 atfile.util.check_prog "git"
46 atfile.util.check_prog "grep"
47 atfile.util.check_prog "hostname"
48 atfile.util.check_prog "md5sum"
49 atfile.util.check_prog "sed"
50 atfile.util.check_prog "shellcheck"
51
52 id="$(atfile.util.get_random 13)"
53 commit_author="$(git config user.name) <$(git config user.email)>"
54 commit_hash="$(git rev-parse HEAD)"
55 commit_date="$(git show --no-patch --format=%ci "$commit_hash")"
56 # shellcheck disable=SC2154
57 dist_file="$(echo "$_prog" | cut -d "." -f 1)-${_version}.sh"
58 # shellcheck disable=SC2154
59 dist_dir="$_prog_dir/bin"
60 dist_path="$dist_dir/$dist_file"
61 dist_path_relative="$(realpath --relative-to="$(pwd)" "$dist_path" 2>/dev/null)"
62 parsed_version="$(atfile.util.parse_version "$_version")"
63 version_record_id="atfile-$parsed_version"
64
65 test_error_count=0
66 test_info_count=0
67 test_style_count=0
68 test_warning_count=0
69 test_ignore_count=0
70
71 if [[ -z "$dist_path_relative" ]]; then
72 dist_path_relative="$dist_path"
73 else
74 dist_path_relative="./$dist_path_relative"
75 fi
76
77 atfile.say "$(atfile.build.pad_emoji "āļø" " ") Building..."
78
79 echo "ā³ Creating '$dist_file'..."
80 mkdir -p "$dist_dir"
81 echo "#!/usr/bin/env bash" > "$dist_path"
82
83 echo "ā³ Generating header..."
84 # shellcheck disable=SC2154
85 echo -e "\n# $_name <${ATFILE_FORCE_META_REPO}>
86# ---
87# Version: $_version
88# Commit: $commit_hash
89# Author: $commit_author
90# Build: $id ($(hostname):$(atfile.util.get_os))
91# ---
92# Psst! You can \`source atfile\` in your own Bash scripts!
93" >> "$dist_path"
94
95 for s in "${ATFILE_DEVEL_INCLUDES[@]}"
96 do
97 if [[ -f "$s" ]]; then
98 if [[ $(atfile.build.get_devel_value "$s" "ignore-build" == 1 ) ]]; then
99 echo "ā³ Ignoring: $s"
100 else
101 echo "ā³ Compiling: $s"
102
103 while IFS="" read -r line
104 do
105 include_line=1
106
107 if [[ $line == "#"* ]] ||\
108 [[ $line == *" #"* ]] ||\
109 [[ $line == " " ]] ||\
110 [[ $line == "" ]]; then
111 include_line=0
112 fi
113
114 if [[ $line == *"# shellcheck disable"* ]]; then
115 if [[ $line == *"=SC2154"* ]]; then
116 include_line=0
117 else
118 include_line=1
119 (( test_ignore_count++ ))
120 fi
121 fi
122
123 if [[ $include_line == 1 ]]; then
124 if [[ $line == *"{:"* && $line == *":}"* ]]; then
125 # NOTE: Not using atfile.util.get_envvar() here, as confusion can arise from config file
126 line="$(atfile.build.replace_template_var "$line" "meta_author" "$ATFILE_FORCE_META_AUTHOR")"
127 line="$(atfile.build.replace_template_var "$line" "meta_did" "$ATFILE_FORCE_META_DID")"
128 line="$(atfile.build.replace_template_var "$line" "meta_repo" "$ATFILE_FORCE_META_REPO")"
129 line="$(atfile.build.replace_template_var "$line" "meta_year" "$ATFILE_FORCE_META_YEAR")"
130 line="$(atfile.build.replace_template_var "$line" "version" "$ATFILE_FORCE_VERSION")"
131 fi
132
133 echo "$line" >> "$dist_path"
134 fi
135 done < "$s"
136 fi
137 fi
138 done
139
140 echo -e "\n# \"Four million lines of BASIC\"\n# - Kif Kroker (3003)" >> "$dist_path"
141
142 checksum="$(atfile.util.get_md5 "$dist_path" | cut -d "|" -f 1)"
143
144 echo "š§Ŗ Testing..."
145 shellcheck_output="$(shellcheck --format=json "$dist_path" 2>&1)"
146
147 while read -r item; do
148 code="$(echo "$item" | jq -r '.code')"
149 col="$(echo "$item" | jq -r '.column')"
150 line="$(echo "$item" | jq -r '.line')"
151 level="$(echo "$item" | jq -r '.level')"
152 message="$(echo "$item" | jq -r '.message')"
153
154 case "$level" in
155 "error") level="$(atfile.build.pad_emoji "š" "") Error"; (( test_error_count++ )) ;;
156 "info") level="$(atfile.build.pad_emoji "ā¹ļø" " ") Info"; (( test_info_count++ )) ;;
157 "style") level="$(atfile.build.pad_emoji "šØ" "") Style"; (( test_style_count++ )) ;;
158 "warning") level="$(atfile.build.pad_emoji "ā ļø" " ") Warning"; (( test_warning_count++ )) ;;
159 esac
160
161 echo "ā³ $level ($line:$col): [SC$code] $message"
162 done <<< "$(echo "$shellcheck_output" | jq -c '.[]')"
163
164 test_total_count=$(( test_error_count + test_info_count + test_style_count + test_warning_count ))
165
166 end_message_suffix_string="ā³ Issues: $(atfile.util.fmt_int "$test_total_count")
167 ā³ Error: $(atfile.util.fmt_int "$test_error_count")
168 ā³ Warning: $(atfile.util.fmt_int "$test_warning_count")
169 ā³ Info: $(atfile.util.fmt_int "$test_info_count")
170 ā³ Style: $(atfile.util.fmt_int "$test_style_count")
171 ā³ Ignored: $(atfile.util.fmt_int "$test_ignore_count")
172ā³ ID: $id"
173
174 atfile.say "---"
175
176 if [[ $test_error_count -gt 0 ]]; then
177 atfile.say "$(atfile.build.pad_emoji "ā" "") Failed: $_version
178$end_message_suffix_string" "" 31 31 1
179 rm -f "$dist_path"
180 exit 255
181 else
182 echo -e "$(atfile.build.pad_emoji "ā
" "") Built: $_version
183ā³ Path: $dist_path_relative
184 ā³ Check: $checksum
185 ā³ Size: $(atfile.util.get_file_size_pretty "$(stat -c %s "$dist_path")")
186 ā³ Lines: $(atfile.util.fmt_int "$(wc -l < "$dist_path")")
187$end_message_suffix_string"
188 fi
189
190 chmod +x "$dist_path"
191
192 # shellcheck disable=SC2154
193 if [[ $_devel_enable_publish == 1 ]]; then
194 atfile.say "---\n$(atfile.build.pad_emoji "āØ" "") Updating..."
195 atfile.auth "$_devel_dist_username" "$_devel_dist_password"
196 [[ $_version == *"+"* ]] && atfile.die "Cannot publish a Git version ($_version)"
197
198 atfile.say "ā³ Uploading '$dist_path'..."
199 atfile.upload "$dist_path" "" "$version_record_id"
200 # shellcheck disable=SC2181
201 [[ $? != 0 ]] && atfile.die "Unable to upload '$dist_path'"
202
203 latest_release_record="{
204 \"version\": \"$_version\",
205 \"releasedAt\": \"$(atfile.util.get_date "$commit_date")\",
206 \"commit\": \"$commit_hash\",
207 \"id\": \"$id\",
208 \"checksum\": \"$checksum\"
209}"
210
211 atfile.say "---\n$(atfile.build.pad_emoji "ā¬ļø" " ") Bumping..."
212 # shellcheck disable=SC2154
213 atfile.record update "at://$_devel_dist_username/self.atfile.latest/self" "$latest_release_record"
214 fi
215}