11#! /bin/bash
2+ # @file functions
3+ # @brief CI and local package release "automation" functions.
24
3- # USAGE: packageVersion "[PATH]/package.json"
5+ #
6+ # @description Parse and return a version value from a "package.json" file
7+ #
8+ # @see [shdoc](https://github.com/reconquest/shdoc)
9+ # @example
10+ # packageVersion "<PATH>/package.json"
411packageVersion () {
5- local PACKAGE_JSON_FILE =$1
6- VERSION =" "
12+ local package_json_file =$1
13+ local version =" "
714 while read a b ; do
8- [ " $a " = ' "version":' ] && { b=" ${b% \" * } " ; VERSION=" ${b# \" } " ; break ; }
9- done < $PACKAGE_JSON_FILE
10- echo $VERSION
15+ [ " $a " = ' "version":' ] && { b=" ${b% \" * } " ; version=" ${b# \" } " ; break ; }
16+ done < $package_json_file
17+ echo $version
18+ }
19+
20+ #
21+ # @description $PKG_ROOT environment variable check. Should be a string
22+ # with a root path of the package
23+ #
24+ # @see $PKG_ROOT
25+ checkPkgRoot () {
26+ if [ -z " $PKG_ROOT " ]
27+ then
28+ PKG_ROOT=$1
29+ if [ -z " $PKG_ROOT " ]
30+ then
31+ PKG_ROOT=" ."
32+ fi
33+ fi
34+ }
35+
36+ #
37+ # @description Replace .env (default) file content with $PKG_ROOT variable value
38+ #
39+ # @see $PKG_ROOT
40+ fixEnvFile () {
41+ # $PKG_ROOT environment variable check
42+ checkPkgRoot $1
43+
44+ # Fix env (default) file with correct value
45+ # for environment variables
46+ if [ -f " $PKG_ROOT /.env" ]
47+ then
48+ echo " [FIX .ENV] Replaced $( cat " $PKG_ROOT /.env" ) to => REPOSITORY_ROOT=."
49+ echo " REPOSITORY_ROOT=." > " $PKG_ROOT /.env"
50+ fi
1151}
1252
1353# @description Add github actions state and output variables to be handled on .yml workflow files
1454#
15- # @see [shdoc](https://github.com/reconquest/shdoc)
55+ # @see $GITHUB_OUTPUT
1656# @see [Deprecating save-state and set-output commands](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands)
57+ # @see [Github Actions: Output parameter](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter)
1758githubActionsOutputs () {
59+ # PS: CURRENT_TAG and COMMIT_MESSAGE are handled here as "global/environment" variables
1860 CURRENT_TAG=$( git describe --tags $( git rev-list --tags --max-count=1) )
1961 COMMIT_MESSAGE=$( git log -1 --pretty=%B)
62+
63+ echo " [GITHUB VARIABLES] Commit => $COMMIT_MESSAGE "
64+
2065 # Use the format {name}={value} instead of ::set-output
21- echo " {tag}={ $CURRENT_TAG } " >> $GITHUB_OUTPUT
22- echo " {commit_message}={ $COMMIT_MESSAGE } " >> $GITHUB_OUTPUT
66+ echo " TAG= $CURRENT_TAG " >> $GITHUB_OUTPUT
67+ echo " COMMIT_MESSAGE= $COMMIT_MESSAGE " >> $GITHUB_OUTPUT
2368}
2469
70+ # @description Copy all content of the package folder to the ROOT dir
71+ #
72+ # @see [shdoc](https://github.com/reconquest/shdoc)
73+ # @example
74+ # # From the root folder, with "Packages/<PACKAGE_NAME>"
75+ # copyPackagesContent
76+ # # result: Copy "Packages/<PACKAGE_NAME>/*.*" to ROOT/
2577copyPackagesContent () {
26- shopt -s extglob dotglob
27- cp -rvf " Packages/$PKG_NAME /." " $PKG_ROOT "
78+ # shopt -s extglob dotglob
79+
80+ # Preserve .env file from $PKG_ROOT
81+ # if [ -f "$PKG_ROOT/.env"]
82+ # then
83+ # mkdir "$PKG_ROOT/tmp"
84+ # mv "$PKG_ROOT/.env" "$PKG_ROOT/tmp"
85+ # fi
86+
87+ cp -rvf " Packages/$PKG_NAME /." " $PKG_ROOT /"
2888 rm -rf ./Packages
89+
90+ # Move .env to $PKG_ROOT
91+ # if [ -f "$PKG_ROOT/tmp/.env"]
92+ # then
93+ # mv -f "$PKG_ROOT/tmp/.env" "$PKG_ROOT/"
94+ # rm -rf "$PKG_ROOT/tmp"
95+ # fi
2996}
3097
98+ # TODO: Move this common function to another script file in order to reuse (e.g .github/scripts/common.sh)
99+ #
100+ # @description Rename invalid package directories to be "untracked" by game engine, adding a "~" suffix
101+ # (e.g "Samples" => "Samples~", "Documentation" => "Documentation~")
102+ #
103+ # @see $PKG_ROOT
104+ # @see [shdoc](https://github.com/reconquest/shdoc)
105+ renameInvalidDirs () {
106+ # $PKG_ROOT environment variable check
107+ checkPkgRoot $1
108+
109+ if [ $PKG_ROOT = " ./" ]
110+ then
111+ echo " [RENAME DIRECTORIES] [Error] The \$ PKG_ROOT => '$PKG_ROOT ' should be just '.' for current directory."
112+ return 1
113+ fi
114+
115+ echo " [RENAME DIRECTORIES] Package Root: $PKG_ROOT /"
116+
117+ # Rename UPM special directories with suffix "~"
118+ if [ -d " $PKG_ROOT /Samples" ] && [ ! -d " $PKG_ROOT /Samples~" ]
119+ then
120+ mv " $PKG_ROOT /Samples" " $PKG_ROOT /Samples~"
121+ rm -f " $PKG_ROOT /Samples.meta"
122+
123+ echo " [RENAMED] Samples => $PKG_ROOT /Samples~"
124+ fi
125+ if [ -d " $PKG_ROOT /Documentation" ] && [ ! -d " $PKG_ROOT /Documentation~" ]
126+ then
127+ mv " $PKG_ROOT /Documentation" " $PKG_ROOT /Documentation~"
128+ rm -f " $PKG_ROOT /Documentation.meta"
129+
130+ echo " [RENAMED] Documentation => $PKG_ROOT /Documentation~"
131+ fi
132+ }
133+
134+ #
135+ # @description Commit with a new version of the package and push the $PKG_BRANCH
136+ # new orphan branch (usually "upm" branch)
137+ #
138+ # @see $PKG_BRANCH
139+ # @see renameInvalidDirs()
31140commitAndPush () {
32141 # Incrementing LAST_RELEASE_TAG+1.
33142 # Keep here just to store the history, and if need this to the future/others repositories
34143 #
35144 # PS: Keep in mind that not always you would like to increment the git tag version (e.g rewriting with force an existent git tag)
36145 # [[ "$LAST_RELEASE_TAG" =~ (.*[^0-9])([0-9]+)$ ]] && LAST_RELEASE_TAG="${BASH_REMATCH[1]}$((${BASH_REMATCH[2]} + 1))";
37146
38- RELEASE_VERSION =$( packageVersion " ./package.json" )
147+ local release_version =$( packageVersion " ./package.json" )
39148
40- echo " New version: $RELEASE_VERSION "
149+ echo " [COMMIT AND PUSH] New version: $release_version "
150+
151+ renameInvalidDirs
41152
42- if [[ -d " Samples" ]]; then
43- mv Samples Samples~
44- rm -f Samples.meta
45- fi
46- if [[ -d " Documentation" ]]; then
47- mv Documentation Documentation~
48- rm -f Documentation.meta
49- fi
50153 git config --global user.name ' github-bot'
51154 git config --global user.email
' [email protected] ' 52155 git add .
53156 git commit --allow-empty -am " $COMMIT_MESSAGE "
54157
55- echo $RELEASE_VERSION > VERSION.md~
158+ echo $release_version > VERSION.md~
159+
160+ # .env (default) file path fix
161+ fixEnvFile
162+
56163 git add VERSION.md~
57- git commit -am " fix: Samples => Samples~ and commit a new version: $RELEASE_VERSION "
164+ git commit -am " fix: Samples => Samples~ and commit a new version: $release_version "
58165 git push -f -u origin " $PKG_BRANCH "
59166}
60167
168+ # TODO: Move this function to another script file (e.g .github/scripts/local.sh)
169+ #
170+ # @description Copy a list of files and dirs from the ROOT to the package folder
171+ #
172+ # @arg $1 string A path configured as "$repository_root" local variable to be used as an origin
173+ # path to copy content into package dir
174+ # @arg $2 string A path configured as "$PKG_ROOT" environment variable to be used as
175+ # root path of the package
176+ #
177+ # @see $PKG_ROOT
178+ # @see [Exit the Bash Script if a Certain Condition Occurs](https://itslinuxfoss.com/exit-bash-script-if-certain-condition-occurs)
179+ # @see [How to Check if a File or Directory Exists in Bash](https://linuxize.com/post/bash-check-if-file-exists)
180+ copyFilesForPublish () {
181+ local repository_root=$1
182+
183+ if [ -z " $repository_root " ]
184+ then
185+ echo " [COPY FILES] The parameter \$ 1 => \$ repository_root is required: $repository_root "
186+ return 1
187+ else
188+ if [[ " $repository_root " =~ \. $ ]]
189+ then
190+ repository_root=" $repository_root /"
191+ fi
192+ echo " [COPY FILES] \$ repository_root: $repository_root "
193+ fi
194+
195+ # $PKG_ROOT environment variable check
196+ checkPkgRoot $2
197+
198+ local pkg_root_full_path=$( realpath $PKG_ROOT )
199+ if [[ $repository_root == $pkg_root_full_path ]]
200+ then
201+ echo " [COPY FILES] Cannot copy a directory FROM: \$ repository_root => '$repository_root ' to \$ PKG_ROOT => '$pkg_root_full_path ', into itself"
202+ return 1
203+ fi
204+
205+ chmod -R 777 " $PKG_ROOT /"
206+
207+ local files_copy=(README.md README.md.meta LICENSE LICENSE.meta Images Images.meta)
208+ for file_name in " ${files_copy[@]} "
209+ do
210+ if [[ -f " $repository_root /$file_name " && ! -f " $PKG_ROOT /$file_name " ]] || [[ -d " $repository_root /$file_name " && ! -d " $PKG_ROOT /$file_name " ]]
211+ then
212+ cp -rf " $repository_root /$file_name " " $PKG_ROOT /$file_name "
213+ echo " [COPY FILES] Copied: $PKG_ROOT /$file_name "
214+ fi
215+ done
216+ }
217+
218+ # TODO: Move this function to another script file (e.g .github/scripts/local.sh)
219+ # TODO: Move common functions dependencies to another script file in order to reuse (e.g .github/scripts/common.sh)
220+ #
221+ # @description Automate all actions required before PUBLISH a package in a remote registry
222+ #
223+ # @arg $1 string A path configured as "$repository_root" local variable to be used as an origin
224+ # path to copy content into package dir
225+ # @arg $2 string A path configured as "$PKG_ROOT" environment variable to be used as
226+ # root path of the package
227+ #
228+ # @see $PKG_ROOT
229+ # @see renameInvalidDirs($PKG_ROOT)
230+ # @see copyFilesForPublish($1)
231+ localBeforePublish () {
232+ local repository_root=$1
233+
234+ if [ -d $repository_root ] && [[ $repository_root != " ./" && $repository_root != " ." ]]
235+ then
236+ # $PKG_ROOT environment variable check
237+ checkPkgRoot $2
238+
239+ renameInvalidDirs $PKG_ROOT
240+ copyFilesForPublish $repository_root
241+ else
242+ echo " [PRE PUBLISH] [Skip] Bypass package preparation because \$ 1 : \$ repository_root => '$repository_root ' is invalid"
243+ fi
244+ }
245+
246+ # TODO: Move this function to another script file (e.g .github/scripts/local.sh)
247+ #
248+ # @description PUBLISH a package in a remote registry. Usually used inside of a npm script
249+ #
250+ # @arg $1 string Overrides the $PKG_ROOT environment variable with a path to a package
251+ #
252+ # @see $PKG_ROOT
253+ localPublish () {
254+ # $PKG_ROOT environment variable check
255+ checkPkgRoot $1
256+
257+ cd $PKG_ROOT
258+ npm publish
259+ }
260+
61261run () {
62262 if [ $1 == " push" ]
63263 then
@@ -68,10 +268,25 @@ run() {
68268 elif [ $1 == " githubActionsVariables" ]
69269 then
70270 githubActionsOutputs
271+ elif [ $1 == " copyFilesForPublish" ]
272+ then
273+ copyFilesForPublish $2 $3
274+ elif [ $1 == " fixEnvFile" ]
275+ then
276+ fixEnvFile $2
277+ elif [ $1 == " renameInvalidDirs" ]
278+ then
279+ renameInvalidDirs $2
280+ elif [ $1 == " localBeforePublish" ]
281+ then
282+ localBeforePublish $2
283+ elif [ $1 == " localPublish" ]
284+ then
285+ localPublish $2
71286 else
72287 echo " [ERROR] INVALID SCRIPT OPERATION"
73288 exit 1
74289 fi
75290}
76291
77- run $1
292+ run $1 $2 $3
0 commit comments