| #!/bin/bash |
| # |
| # Copyright (C) 2022-2024 The LeafOS Project |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| # |
| |
| usage() { |
| echo "Usage ${0} <branch>" |
| } |
| |
| # Verify argument count |
| if [ "$#" -ne 1 ]; then |
| usage |
| exit 1 |
| fi |
| |
| BRANCH="${1}" |
| |
| # Check to make sure this is being run from the top level repo dir |
| if [ ! -e "build/envsetup.sh" ]; then |
| echo "Must be run from the top level repo dir" |
| exit 1 |
| fi |
| |
| # Source build environment (needed for gettop) |
| . build/envsetup.sh |
| |
| TOP="$(gettop)" |
| MERGEDREPOS="${TOP}/merged_repos_lineage_translations.txt" |
| MANIFEST="${TOP}/.repo/manifests/snippets/leaf.xml" |
| LEAF_BRANCH=$(git -C ${TOP}/.repo/manifests.git config --get branch.default.merge | sed 's#refs/heads/##g') |
| |
| # Build list of LeafOS forked repos |
| PROJECTPATHS=$(repo forall -g default,-lineage -c '[ ! -z "$(find -name cm_strings.xml -or -name cm_plurals.xml)" ] && echo "$REPO_PATH "') |
| |
| echo "#### LineageOS branch = ${BRANCH} Branch = ${LEAF_BRANCH} ####" |
| |
| # Make sure manifest and forked repos are in a consistent state |
| echo "#### Verifying there are no uncommitted changes on projects containing LineageOS strings ####" |
| for PROJECTPATH in ${PROJECTPATHS} .repo/manifests; do |
| # Skip fully removed projects |
| [ ! -d "${TOP}/$PROJECTPATH" ] && continue; |
| cd "${TOP}/${PROJECTPATH}" |
| if [[ -n "$(git status --porcelain)" ]]; then |
| echo "Path ${PROJECTPATH} has uncommitted changes. Please fix." |
| exit 1 |
| fi |
| done |
| echo "#### Verification complete - no uncommitted changes found ####" |
| |
| # Remove any existing list of merged repos file |
| rm -f "${MERGEDREPOS}" |
| |
| # Iterate over each project |
| for PROJECTPATH in ${PROJECTPATHS}; do |
| # Skip fully removed projects |
| [ ! -d "${TOP}/$PROJECTPATH" ] && continue; |
| cd "${TOP}/${PROJECTPATH}" |
| echo "### Fetching LineageOS strings from ${BRANCH} into ${PROJECTPATH} ###" |
| |
| EXTRAREFS="" |
| EXTRAPATHREPLACE="" |
| # Upstream has some Settings strings in a dedicated repo |
| if [ "$PROJECTPATH" = "packages/apps/Settings" ]; then |
| git fetch https://github.com/LineageOS/android_packages_apps_LineageParts "${BRANCH}" |
| EXTRAREFS=$(git rev-parse FETCH_HEAD) |
| fi |
| # Upstream has some fwb strings in a dedicated repo |
| if [ "$PROJECTPATH" = "frameworks/base" ]; then |
| git fetch https://github.com/LineageOS/android_lineage-sdk "${BRANCH}" |
| EXTRAREFS=$(git rev-parse FETCH_HEAD) |
| EXTRAPATHREPLACE="^core|lineage" |
| EXTRAPATHREPLACE_REV="^lineage|core" |
| fi |
| |
| git fetch https://github.com/LineageOS/android_$(echo $PROJECTPATH | sed 's|/|_|g') "${BRANCH}" |
| |
| # Delete old translations |
| find -iregex '.*/values-.*/cm_\(strings\|plurals\).xml' -delete |
| for CM_STRINGS in $(find -iregex '.*/values/cm_\(strings\|plurals\).xml'); do |
| STRINGS_TO_FIND=$(grep -Po '<(string|plurals) name="\K[^"]*' "$CM_STRINGS") |
| PATTERN="$(dirname $CM_STRINGS | sed 's|^\./||g')-[^/]*/$(basename $CM_STRINGS)" |
| |
| for REF in "FETCH_HEAD" $EXTRAREFS; do |
| LOCAL_PATTERN="$PATTERN" |
| if [ "$REF" != "FETCH_HEAD" ]; then |
| if [ ! -z "$EXTRAPATHREPLACE" ]; then |
| LOCAL_PATTERN="$(echo $LOCAL_PATTERN | sed -e "s|$EXTRAPATHREPLACE|g")" |
| fi |
| |
| if [ "$(basename $CM_STRINGS)" = "cm_plurals.xml" ]; then |
| LOCAL_PATTERN="$(echo $LOCAL_PATTERN | sed -e 's/cm_plurals.xml$/plurals.xml/g')" |
| else |
| LOCAL_PATTERN="$(echo $LOCAL_PATTERN | sed -e 's/cm_strings.xml$/strings.xml/g')" |
| fi |
| fi |
| |
| for TRANSLATION in $(git ls-tree -r --name-only "$REF" | grep -P "$LOCAL_PATTERN"); do |
| FILENAME="$TRANSLATION" |
| if [ "$REF" != "FETCH_HEAD" ]; then |
| if [ ! -z "$EXTRAPATHREPLACE_REV" ]; then |
| FILENAME="$(echo $FILENAME | sed -e "s|$EXTRAPATHREPLACE_REV|g")" |
| fi |
| |
| if [ "$(basename $TRANSLATION)" = "plurals.xml" ]; then |
| FILENAME="$(echo $FILENAME | sed -e 's/plurals.xml$/cm_plurals.xml/g')" |
| else |
| FILENAME="$(echo $FILENAME | sed -e 's/strings.xml$/cm_strings.xml/g')" |
| fi |
| fi |
| |
| mkdir -p $(dirname "$FILENAME") |
| if [ ! -f "$FILENAME" ]; then |
| echo '<?xml version="1.0" encoding="utf-8"?>' > "$FILENAME" |
| GIT_PAGER="cat" git show "$REF":"$TRANSLATION" | grep -Pzo '<!--(\n( )*Copyright|\n/\*\*\n( )*\* Copyright)[\s\S]*?-->' | sed 's/\x0$/\n/g' >> "$FILENAME" |
| echo '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">' >> "$FILENAME" |
| fi |
| |
| for STRING in $STRINGS_TO_FIND; do |
| LINE=$(GIT_PAGER="cat" git show "$REF":"$TRANSLATION" | grep -Pzo " <(string|plurals) name=\"$STRING\">[\s\S]*?</(string|plurals)>" | sed 's/\x0$/\n/g') |
| |
| if [ ! -z "$LINE" ]; then |
| echo "$LINE" >> "$FILENAME" |
| fi |
| done |
| done |
| done |
| done |
| # Insert closing resources tag into all new files |
| for CM_STRINGS in $(find -iregex '.*/values-.*/cm_\(strings\|plurals\).xml'); do |
| echo '</resources>' >> $CM_STRINGS |
| done |
| |
| if [[ -n "$(git status --porcelain)" ]]; then |
| git add . |
| git commit -m "Import cm_strings translations from lineage" |
| echo -e "import\t\t${PROJECTPATH}" | tee -a "${MERGEDREPOS}" |
| else |
| echo -e "nochange\t\t${PROJECTPATH}" | tee -a "${MERGEDREPOS}" |
| fi |
| done |