forked from subscriptions-project/swg-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcuts.sh
281 lines (227 loc) · 6.17 KB
/
shortcuts.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
###############
## Constants ##
###############
AMPHTML_PATH=~/projects/amphtml
SWGJS_PATH=~/projects/swgjs
####################
## Public methods ##
####################
# Creates a new Swgjs branch with Git.
#
# You can call this before starting to code
# a new feature or bugfix.
function swgjs_create_branch() {
BRANCH_NAME=$1
if [[ -z "$BRANCH_NAME" ]]; then
echo "What is the branch named?"
read BRANCH_NAME
fi
cd $SWGJS_PATH
if [[ `git status --porcelain` ]]; then
git add .
git commit -m "Saving changes"
fi
git fetch team main
git checkout team/main
git branch -D $BRANCH_NAME
git switch -c $BRANCH_NAME
git push -f me $BRANCH_NAME
git branch --set-upstream-to=me/$BRANCH_NAME
}
# Starts the Swgjs server locally.
#
# You can call this when you're ready to
# manually test your new feature or bugfix.
function swgjs_start_server() {
SWG_JS=~/projects/swgjs
cd $SWG_JS
npx gulp
}
# Installs Swgjs.
#
# You can call this to install Swgjs in a standardized way.
# This helps the shortcuts in this file run smoothly, and also
# makes it easier to collaborate with other Swgjs engineers.
function swgjs_install() {
if ! __swgjs_has_nodejs; then return 1; fi
__swgjs_get_github_username
if ! test -f "$SWGJS_PATH/package.json"
then
echo "Installing Swgjs"
mkdir -p $SWGJS_PATH
git clone -o team https://github.com/subscriptions-project/swg-js $SWGJS_PATH
cd $SWGJS_PATH
git remote add me git@github.com:$GITHUB_USERNAME/swg-js.git
npm i
fi
}
# Publishes a Swgjs release, in pre-release mode.
#
# This publishes a Swgjs release, in pre-release mode.
# The output includes the release's version number (ex: 0.1.22.123).
# You will use this in a few places, like the "Release title" and "Version tag".
function swgjs_publish() {
swgjs_install
# Get the freshest branch possible.
# Also temporarily disable warnings
# about the branch being detached,
# since we don't plan on committing
# any changes to it.
cd $SWGJS_PATH
git fetch team
git -c advice.detachedHead=false checkout team/main
# Get changelog
npx gulp publish
# Return to previous branch
git checkout -
# Return to previous directory
cd -
}
# Adds Swgjs Shortcuts to the .bashrc file.
#
# You can run this once to make sure the
# Swgjs Shortcuts are always ready to go.
function swgjs_add_shortcuts_to_bashrc() {
local SHORTCUTS_CMD="source $SWGJS_PATH/shortcuts.sh"
# Add to .bashrc
if grep -Fxq "$SHORTCUTS_CMD" ~/.bashrc
then
echo "Swgjs Shortcuts are already installed. ✅"
else
echo "Swgjs Shortcuts are installing..."
echo "# Swgjs Shortcuts" >> ~/.bashrc
echo "$SHORTCUTS_CMD" >> ~/.bashrc
echo "" >> ~/.bashrc
echo "Swgjs Shortcuts are now installed. ✅"
fi
}
# Creates a Swgjs AMP Release PR.
function swgjs_create_amp_release_pr() {
if ! __swgjs_has_nodejs; then return 1; fi
echo "Swgjs: Create AMP Release PR"
echo ""
__swgjs_get_github_username
swgjs_install
__swgjs_install_amp
# Switch to clean AMP branch.
git fetch team
git checkout team/amp
if [[ `git status --porcelain` ]]; then
# Avoid building with local changes.
echo "ERROR: Local changes detected."
return 1
fi
# Build Swgjs for AMP.
SWG_VERSION=$(git rev-parse --short HEAD)
npx gulp build
npx gulp export-to-amp --swgVersion=$SWG_VERSION
# Create new AMP branch.
BRANCH_NAME="swg-release--$SWG_VERSION"
__swgjs_create_amp_branch $BRANCH_NAME
# Copy exports to AMP.
cp $SWGJS_PATH/dist/amp/* $AMPHTML_PATH/third_party/subscriptions-project/
# Push AMP branch.
git add .
git commit -m "SwG Release $SWG_VERSION"
git push -f -u me $BRANCH_NAME
# Wrap up.
echo "You're all set! Now just create a PR:"
echo "https://github.com/ampproject/amphtml/compare/main...$GITHUB_USERNAME:$BRANCH_NAME?expand=1"
}
# Deploys Swgjs Demos.
#
# You can run this after updating demos and merging
# your changes into the `main` branch.
function swgjs_deploy_demos() {
if ! command -v gcloud &> /dev/null
then
echo "Please install gcloud and select the 'swgjs-demos' project:"
echo "https://cloud.google.com/sdk/gcloud"
return
fi
cd $SWGJS_PATH/demos
git stash
git fetch team main
git checkout team/main
npm run deploy
git checkout -
git stash pop
cd -
}
#####################
## Private methods ##
#####################
# Creates a new AMP branch.
#
# @private
function __swgjs_create_amp_branch() {
BRANCH_NAME=$1
if [[ -z "$BRANCH_NAME" ]]; then
echo "What is the branch named?"
read BRANCH_NAME
fi
cd $AMPHTML_PATH
if [[ `git status --porcelain` ]]; then
git add .
git commit -m "Saving changes"
fi
git fetch team main
git checkout team/main
git branch -D $BRANCH_NAME
git switch -c $BRANCH_NAME
git push -f me $BRANCH_NAME
git branch --set-upstream-to=me/$BRANCH_NAME
}
# Installs AMP
#
# @private
function __swgjs_install_amp() {
__swgjs_get_github_username
if ! test -f "$AMPHTML_PATH/package.json"
then
echo "Installing AMP"
mkdir -p $AMPHTML_PATH
git clone -o team https://github.com/ampproject/amphtml $AMPHTML_PATH
cd $AMPHTML_PATH
git remote add me git@github.com:$GITHUB_USERNAME/amphtml.git
fi
}
# Asks you for your GitHub username,
# if it's not already available.
#
# @private
function __swgjs_get_github_username() {
# Check for GitHub env var
if [[ -z "${GITHUB_USERNAME}" ]]; then
# Ask for username
echo "What is your GitHub username?"
read GITHUB_USERNAME
# Optionally save username to .bashrc
echo "Save GitHub username ($GITHUB_USERNAME) to your .bashrc? [Y|n]"
read RESPONSE
if [ "$RESPONSE" == "n" ]; then
return 0
fi
# Save username
echo "# GitHub" >> ~/.bashrc
echo "export GITHUB_USERNAME=$GITHUB_USERNAME" >> ~/.bashrc
echo "" >> ~/.bashrc
echo "Saved!"
else
return 0
fi
}
# Checks if Nodejs is installed.
# Swgjs and AMP both depend on Nodejs.
#
# @private
function __swgjs_has_nodejs() {
if ! command -v npx &> /dev/null
then
echo "You need Nodejs to continue."
echo " For Linux and Mac, install NVM."
echo " For Windows, install Nodejs."
return 1
fi
return 0
}