0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 03:52:15 +02:00

Update JS library (jquery & mathjax) using GitHub action (#11454)

* update js library (jquery & mathjax)
* restore file mode, change paths in conf.js
* use variable for paths and replacement strings
* check if path exists or not for replacement
This commit is contained in:
Mani 2022-06-03 04:58:36 +05:30 committed by GitHub
parent 6d1019e52c
commit 29fe0055d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 0 deletions

58
.github/workflows/update_js_libs.yml vendored Normal file
View File

@ -0,0 +1,58 @@
name: Update JS Library
on:
workflow_dispatch:
jobs:
update_js_libs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 'Update js libs'
id: vars
run: |
# create a new branch and checkout on local
git branch update-js-libs
git checkout update-js-libs
# make executable
chmod +x ./tools/update-js-libs.sh
./tools/update-js-libs.sh
# commit changes
git config --global user.name github-actions
git config --global user.email github-actions@github.com
git add .
git commit -am "updated mathjax and jquery"
git push --set-upstream origin update-js-libs -f
# get installed jquery and mathjax version
cd ~/tmp/anki
jquery_ver=$(npm list jquery | grep -Po "jquery@(\d+\.)+\d+" | sort -u)
mathjax_ver=$(npm list mathjax | grep -Po "mathjax@(\d+\.)+\d+")
# setting an output parameter
echo ::set-output name=jquery_ver::"$jquery_ver"
echo ::set-output name=mathjax_ver::"$mathjax_ver"
- name: 'Create pull request'
uses: actions/github-script@v6
with:
script: |
try {
await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
head: 'update-js-libs',
base: 'main',
title: 'Update jquery and mathjax',
body: "### Updated\n ${{ steps.vars.outputs.jquery_ver }} \n ${{ steps.vars.outputs.mathjax_ver }}"
});
} catch(err) {
if (err.status === 422) {
console.log("PR already exists.")
} else {
throw err;
}
}

57
tools/update-js-libs.sh Normal file
View File

@ -0,0 +1,57 @@
# update mathjax and jquery js library with Anki Desktop
# path variable
ANKI_SRC=~/tmp/anki
ANKIDROID_SRC=/home/runner/work/Anki-Android/Anki-Android
# ensure some basic tools are installed
sudo apt install bash grep findutils curl gcc g++ git
# install bazelisk
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.10.1/bazelisk-linux-amd64 -o ./bazel
chmod +x bazel && sudo mv bazel /usr/local/bin/
# clone Anki
git clone https://github.com/ankitects/anki $ANKI_SRC
# build for mathjax and jquery
cd $ANKI_SRC/qt/aqt/data/web/js/vendor
bazel build vendor
cd $ANKI_SRC/ts
bazel build mathjax
# copy latest jquery to assets dir
cp $ANKI_SRC/.bazel/bin/qt/aqt/data/web/js/vendor/jquery.min.js $ANKIDROID_SRC/AnkiDroid/src/main/assets/jquery.min.js
# remove old mathjax file
rm -rf $ANKIDROID_SRC/AnkiDroid/src/main/assets/mathjax/*
# copy latest mathjax to assets dir
cp -r $ANKI_SRC/.bazel/bin/qt/aqt/data/web/js/vendor/mathjax $ANKIDROID_SRC/AnkiDroid/src/main/assets/
cp $ANKI_SRC/.bazel/bin/ts/mathjax/index.js $ANKIDROID_SRC/AnkiDroid/src/main/assets/mathjax/conf.js
# replace paths in conf.js for AnkiDroid
ANKI_MATHJAX_DIR="_anki/js/vendor/mathjax"
ANKIDROID_MATHJAX_DIR="android_asset/mathjax"
MATHJAX_CONF_JS=$ANKIDROID_SRC/AnkiDroid/src/main/assets/mathjax/conf.js
# check if the path exists in conf.js file or not, otherwise exit
if grep "$ANKI_MATHJAX_DIR" $MATHJAX_CONF_JS
then
sed -i "s|$ANKI_MATHJAX_DIR|$ANKIDROID_MATHJAX_DIR|" $MATHJAX_CONF_JS
else
echo "MathJax path does not exist in conf.js file, it is changed"
exit 1
fi
# cleanup
rm $ANKIDROID_SRC/AnkiDroid/src/main/assets/mathjax/mathjax-cp.sh
# restore mathjax.css file
cd $ANKIDROID_SRC
git restore AnkiDroid/src/main/assets/mathjax/mathjax.css
# change mode of files in assets dir
find $ANKIDROID_SRC/AnkiDroid/src/main/assets/mathjax/ -type f -print0 | xargs -0 chmod 644