add script to automate l10n extraction (#4027)

This commit is contained in:
William Durand 2021-11-09 09:27:16 +01:00 коммит произвёл GitHub
Родитель 5879309c4e
Коммит b16cdd2127
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 110 добавлений и 0 удалений

Просмотреть файл

@ -1,5 +1,6 @@
scripts/download-import-tag
scripts/list-firefox-tags
scripts/run-l10n-extraction
scripts/webext-test-functional
dist
coverage

Просмотреть файл

@ -4,6 +4,7 @@
# exclude these files
scripts/download-import-tag
scripts/list-firefox-tags
scripts/run-l10n-extraction
scripts/webext-test-functional
package-lock.json
LICENSE

Просмотреть файл

@ -188,6 +188,16 @@ We use [pino](https://github.com/pinojs/pino) for logging:
We use [Prettier](https://prettier.io/) to automatically format our JavaScript code and stop all the on-going debates over styles. As a developer, you have to run it (with `npm run prettier-dev`) before submitting a Pull Request.
### L10n extraction
The localization process is very similar to [how we do it for addons-frontend](https://addons-frontend.readthedocs.io/en/latest/i18n/#updating-locales): locales are always updated on the `master` branch, any PR that changes or introduces new localized strings should be merged on `master` first.
In order to update the locales (when new localized strings are added to the codebase), run the following script from the `master` branch. This script automates _all_ the steps described in the addons-frontend docs, without any confirmation step.
```
./scripts/run-l10n-extraction
```
## Architecture
In a nutshell the way the linter works is to take an add-on package, extract the metadata from the xpi (zip) format and then process the files it finds through various content scanners.

98
scripts/run-l10n-extraction Executable file
Просмотреть файл

@ -0,0 +1,98 @@
#!/usr/bin/env bash
# Exit immediately when a command fails.
set -e
# Make sure exit code are respected in a pipeline.
set -o pipefail
# Treat unset variables as an error and exit immediately.
set -u
INITIAL_GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
GIT_CHANGES=$(git status --porcelain)
DIFF_WITH_ONE_LINE_CHANGE="1 file changed, 1 insertion(+), 1 deletion(-)"
GIT_REPO_NAME=addons-linter
GIT_REMOTE="${GIT_REMOTE:-git@github.com:mozilla/$GIT_REPO_NAME.git}"
LOG_FILE="l10n-extraction.log"
info() {
local message="$1"
echo ""
echo "INFO: $message"
echo ""
}
error() {
local message="$1"
echo "ERROR: $message"
exit 1
}
run_l10n_extraction() {
local branch="$GIT_REPO_NAME-locales-$(date -u --iso-8601=date)"
info "Starting l10n extraction"
# Detect local (uncommitted) changes.
if [[ ! -z "$GIT_CHANGES" ]]; then
error "You have local changes, therefore this script cannot continue."
fi
# Switch to the `master` branch if we are not on it already.
if [[ "$INITIAL_GIT_BRANCH" != "master" ]]; then
git checkout master
fi
# Make sure the 'master' branch is up-to-date.
git pull "$GIT_REMOTE" master
# Update dependencies
npm i >> "$LOG_FILE" 2>&1
# Ensure the branch to extract the locales is clean.
if [[ $(git branch --list "$branch") ]]; then
info "Deleting branch '$branch' because it already exists"
git branch -D "$branch"
fi
info "Creating and switching to branch '$branch'"
git checkout -b "$branch"
info "Extracting locales (this can take several minutes)"
npm run extract-locales >> "$LOG_FILE" 2>&1
local git_diff_stat
git_diff_stat=$(git diff --shortstat)
if [[ -z "$git_diff_stat" ]] || [[ "$git_diff_stat" == *"$DIFF_WITH_ONE_LINE_CHANGE"* ]]; then
info "No locale changes, nothing to update, ending process"
git checkout -- .
git checkout "$INITIAL_GIT_BRANCH"
git branch -d "$branch"
return
fi
info "Merging locales"
./scripts/merge-locales >> "$LOG_FILE" 2>&1
git commit -a -m "Extract and merge locales ($(date -u --iso-8601=date))" --no-verify
git push "$GIT_REMOTE" "$branch"
git checkout "$INITIAL_GIT_BRANCH"
git branch -D "$branch"
echo ""
echo "----------------------------------------------------------------------"
echo ""
echo " You can now open the link below to submit your Pull Request:"
echo " https://github.com/mozilla/$GIT_REPO_NAME/pull/new/$branch"
echo ""
echo "----------------------------------------------------------------------"
}
# Clear log file
echo "" > "$LOG_FILE"
run_l10n_extraction