Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Try to automate some of the translation updates #87

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions update-translations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#! /usr/bin/env bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to use -eu to catch commands exiting non-zero, and undefined variables.


CWD=`pwd`
DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

cd $DIRECTORY

function log() {
if [[ -n $1 ]];then
printf "\033[0;36m\n$1\n\033[0m"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might be able to use tput to set the colours:

# Text color variables
txtund=$(tput sgr 0 1)    # Underline
txtbld=$(tput bold)       # Bold
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtylw=$(tput setaf 3)    # Yellow
txtblu=$(tput setaf 4)    # Blue
txtpur=$(tput setaf 5)    # Purple
txtcyn=$(tput setaf 6)    # Cyan
txtwht=$(tput setaf 7)    # White
txtrst=$(tput sgr0)       # Text reset

(from SO)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting...

fi
}

function quit() {
if [[ -n $1 ]];then
printf "\033[0;31m\n\n$1\n\n\033[0m"
fi
cd $CWD
exit
}

function repo_checkout_master() {
if [[ -n $1 ]];then
log "checking out master for '$1'"
cd $DIRECTORY
cd "workspace-zen/$1/"
DIRTY=`git status --porcelain`

if [[ -n $DIRTY ]]; then
quit "$1 has unstaged / uncommitted changes, please fix and try again"
fi
fi
}

# Update the translations repo if we are able
repo_checkout_master 'cp-translations'

TRANSLATIONS_VERSION=$(cat package.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[ ",^]//g')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is jq available..? I mean, you've written this now, so maybe it doesn't matter :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you could use npx project-version to get the version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, okay - so found out I can do jq '.dependencies."cp-translations"' and .dependencies."cp-translations" = "$TRANSLATIONS_VERSION" - will check that out, but means adding package.json here.

log "cp-translations is version $TRANSLATIONS_VERSION"

REPOS=("cp-events-service" "cp-dojos-service" "cp-users-service" "cp-zen-frontend")

for repo in ${REPOS[@]}; do
printf "\033[0;33m\n\nUpdating $repo\n\033[0m"

repo_checkout_master $repo

PACKAGE_VERSION=$(cat package.json \
| grep cp-translations \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[ ",^]//g')

log "$repo uses version $PACKAGE_VERSION"

#if it doesn't match, we need to create a branch, update version, commit and push
if [[ "$TRANSLATIONS_VERSION" != "$PACKAGE_VERSION" ]]; then
log "Updating cp-translations for cp-events-service"
escaped_lhs=$(printf '%s\n' "\"cp-translations\": \"^$PACKAGE_VERSION\"" | sed 's:[][\\/.^$*]:\\&:g')
escaped_rhs=$(printf '%s\n' "\"cp-translations\": \"^$TRANSLATIONS_VERSION\"" | sed 's:[\\/&]:\\&:g;$!s/$/\\/')

sed -i '' -e "s/$escaped_lhs/$escaped_rhs/g" package.json
Copy link
Member

@DanielBrierton DanielBrierton Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yarn.lock also needs to be updated. I think this will result in either failures when running yarn install or it will just install the old version. I don't think there's any reason why you couldn't just run yarn add cp-translations@$TRANSLATIONS_VERSION

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a possibility of running the update inside the Docker container, but outside would result in potential mismatches in yarn.lock to do with the platform right? Maybe will check / investigate, ta.


BRANCH="update-translations-$TRANSLATIONS_VERSION"
git add package.json
git checkout -b $BRANCH
git commit -m "Update cp-translations to $TRANSLATIONS_VERSION"
git push -u origin $BRANCH
git checkout master
# git push origin --delete $BRANCH
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably remove these commented out lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Yeah, was in there for testing :) ]

# git branch -D $BRANCH
fi
done

log "Finished"
exit