-
Notifications
You must be signed in to change notification settings - Fork 61
/
setup-package.sh
executable file
·199 lines (174 loc) · 5.84 KB
/
setup-package.sh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
# Usage:
# setup-package.sh -p $package_path [-t $tag] [-s $sample1,$sample2,...]
#
# Creates a package of the unity files at the `$package_path` folder
# and will include the files form "./Assets/Samples" in the export
# under the path "Samples~" to follow unity convention.
#
# Will also preserve any git-lfs links for files to avoid
# duplicating assets in the repo.
#
# Arguments:
#
# -p $package_path - Required, path to package folder of project,
# Should be something like "Packages/com.companyname.packagename"
# [-t $tag] - Optional, tag version to checkout before building
# package. If provided, will create a new branch with
# the name pattern "release/$tag"
# [-s $sample1,$sample2,...] - Optional, comma separated list
# of samples to copy from the "./Assets/Samples" folder. If none
# is provided, then will select all the samples found in the Samples
# folder (if any).
sample_path="./Assets/Samples/"
current_branch=$(git rev-parse --abbrev-ref HEAD)
current_sha=$(git rev-parse --verify HEAD)
previous_githooks=$(git config core.hooksPath)
if [ $# -eq 0 ]
then
show_help
exit 0
fi
show_help () {
echo "Usage:"
echo " setup-package.sh -p \$package_path [-t \$tag] [-s \$sample1,\$sample2,...]"
echo ""
echo " Creates a package of the unity files at the `\$package_path` folder"
echo " and will include the files form \"$sample_path\" in the export"
echo " under the path \"Samples~\" to follow unity convention."
echo ""
echo " Will also preserve any git-lfs links for files to avoid"
echo " duplicating assets in the repo."
echo " Arguments:"
echo ""
echo " -p \$package_path - Required, path to package folder of project,"
echo " Should be something like \"Packages/com.companyname.packagename\""
echo " [-t \$tag] - Optional, tag version to checkout before building"
echo " package. If provided, will create a new branch with"
echo " the name pattern \"release/\$tag\""
echo " [-s \$sample1,\$sample2,...] - Optional, comma separated list"
echo " of samples to copy from the \"$sample_path\" folder. If none"
echo " is provided, then will select all the samples found in the Samples"
echo " folder (if any)."
}
while getopts "p:t:s:h" opt; do
case $opt in
p) package_path=$OPTARG ;;
t) selected_tag=$OPTARG ;;
s) selected_samples=$OPTARG ;;
h) show_help;exit 0 ;;
?)
echo "Invalid option: -${OPTARG}."
echo
show_help
exit 1
;;
esac
done
if [ -z "$package_path" ]
then
echo "Error: Did not provide package path (-p) argument" 1>&2
show_help
exit 1
fi
if [ ! -d "$package_path" ]
then
echo "Error: Did not find package at path: \"$package_path\"" 1>&2
fi
# Checkout specific tag if one is provided
if [ ! -z "$selected_tag" ]
then
echo "Attempting to make release for tag $selected_tag"
if git rev-parse "$selected_tag" >/dev/null 2>&1; then
# git config core.hooksPath .git/hooks
echo "Found tag $selected_tag, checking out changes"
# git checkout "$selected_tag"
else
echo "Error: Tag $selected_tag does not exist, aborting changes" 1>&2
exit 1
fi
fi
# Find the samples listed by the user
if [ ! -z "$selected_samples" ]
then
IFS=',' read -ra samples_array <<< "$selected_samples"
samples_array=("${samples_array[@]/#/$sample_path}")
for sample in "${samples_array[@]}"
do
if [ ! -d "$sample" ]
then
echo "Error: Did not find sample at path: \"$sample\"" 1>&2
exit 1
else
echo "Found provided sample at path: \"$sample\""
fi
done
else
echo "No samples selected, searching for samples in $sample_path"
samples_array=()
while IFS= read -r -d $'\0'; do
samples_array+=("$REPLY")
done < <(find $sample_path -maxdepth 1 -mindepth 1 -type d -print0)
echo "Found samples: '${samples_array[@]}'"
fi
# Check if there are changes
if [ ! -z "$(git status --porcelain)" ]; then
echo "Found unstaged changes:"
git status --porcelain
echo "Will not setup package if branch has changes" 1>&2
exit 1
fi
# Move to temporary branch
exists=`git show-ref refs/heads/temp-branch`
if [ -n "$exists" ]; then
git branch -D temp-branch
fi
git checkout -b temp-branch
user_email=$(git config --global user.email)
user_name=$(git config --global user.name)
if [ -z "$user_email" ]
then
git config --global user.email "github-actions[bot]@users.noreply.github.com"
fi
if [ -z "$user_name" ]
then
git config --global user.name "github-actions[bot]"
fi
git lfs install
# Sets up unity package samples
for sample in "${samples_array[@]}"
do
sample_name=${sample#"$sample_path"}
dest="$package_path/Samples~/$sample_name"
echo "Moving sample at path \"$sample\" to \"$dest\""
# Setup sample directory
mkdir -p "$(dirname $dest)"
git mv "$sample.meta" "$dest.meta"
git mv "$sample/" "$dest/"
done
echo "git commit -m \"Moved $sample_path to $package_path/Samples\""
git commit -m "Moved $sample_path to $package_path/Samples"
# Reset all other changes
git rm -rf .
git checkout HEAD -- "$package_path"
# Keep .gitattributes for lfs files
git checkout HEAD -- .gitattributes
git commit -m "Filtered for only package files"
# Move files from $package_path to root folder
git mv $package_path/* .
git commit -m "Moved files from \"$package_path/*\" to root"
# Push changes to repo if tag was provided
if [ ! -z "$selected_tag" ]
then
# Push changes to original repo
exists=`git show-ref refs/heads/release/$selected_tag`
if [ -n "$exists" ]; then
git branch -D "release/$selected_tag"
fi
git branch -m "release/$selected_tag"
git push --set-upstream origin "release/$selected_tag" --force
git config core.hooksPath "$previous_githooks"
# Cleanup any files in the repo we don't care about
git checkout . && git clean -xdf .
git checkout "$current_sha" && git checkout "$current_branch"
fi