Configure GitHub workflow to update currency codes

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2020-05-20 14:31:30 +02:00
parent e0fbed32c9
commit bb9ff1dba0
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 61 additions and 34 deletions

View file

@ -0,0 +1,33 @@
name: Update currency codes
on:
schedule:
- cron: '0 0 * * 0'
jobs:
update-currency-codes:
name: Update currency codes
runs-on: ubuntu-latest
steps:
- name: Install xmlstarlet
run: sudo apt install xmlstarlet
- name: Checkout
uses: actions/checkout@v2
with:
ref: "2.0"
- name: Execute script
run: bin/update-currency-codes
- name: Create pull request
uses: peter-evans/create-pull-request@v2
with:
committer: The Respect Panda <therespectpanda@gmail.com>
author: The Respect Panda <therespectpanda@gmail.com>
commit-message: Update list of currency codes
title: Update list of currency codes
base: "2.0"
branch: "workflows/update-currency-codes"

View file

@ -1,45 +1,39 @@
#!/usr/bin/env bash
# Usage: {script} TLD_FILENAME
# Update list of TLD
# Usage: {script}
# Update the list of currency codes
set -euo pipefail
declare -r IFS=$'\n'
declare -r URL="https://www.currency-iso.org/dam/downloads/lists/list_one.xml"
declare -r RULE="${1}"
declare -r TEMPORARY_XML=$(mktemp)
download_list()
declare -r LIST_URL="https://www.currency-iso.org/dam/downloads/lists/list_one.xml"
declare -r LIST_FILENAME=$(mktemp)
declare -r RULE_FILENAME=$(dirname "${BASH_SOURCE}")/../library/Rules/CurrencyCode.php
declare -r RULE_FILENAME_TEMPORARY=$(mktemp)
echo "- Downloading list"
curl --silent --location "${LIST_URL}" --output "${LIST_FILENAME}"
declare -r CURRENCY_CODES_COUNT=$(grep "<CcyNtry>" "${LIST_FILENAME}" | wc --lines)
echo "- Creating temporary file"
{
echo "Downloading list from '${URL}'"
curl --silent --location "${1}" --output "${2}"
}
sed -n "/^</,/ return \[/p" "${RULE_FILENAME}"
for index in $(seq 1 ${CURRENCY_CODES_COUNT}); do
declare name=$(xmlstarlet sel --template --value-of "//CcyNtry[${index}]/CcyNm" < "${LIST_FILENAME}")
declare code=$(xmlstarlet sel --template --value-of "//CcyNtry[${index}]/Ccy" < "${LIST_FILENAME}")
update_currency_codes()
{
local -r filename_rule="${1}"
local -r filename_xml="${2}"
local -r number_of_items=$(grep "<CcyNtry>" "${filename_xml}" | wc --lines)
local -r temporary_rule=$(mktemp)
if [[ -z "${code}" ]]; then
continue
fi
echo "Updating list in '${filename_rule}'"
{
sed -n "/^</,/ return \[/p" "${filename_rule}"
for index in $(seq 1 ${number_of_items}); do
local name=$(xml sel --template --value-of "//CcyNtry[${index}]/CcyNm" < "${filename_xml}")
local code=$(xml sel --template --value-of "//CcyNtry[${index}]/Ccy" < "${filename_xml}")
echo " '${code}', //" $(sed --regexp-extended 's, +$,,' <<< "${name}")
done | sort --unique
sed --silent '/^ \]/,/^}/p' "${RULE_FILENAME}"
} > "${RULE_FILENAME_TEMPORARY}"
if [[ -z "${code}" ]]; then
continue
fi
echo "- Updating content of '$(basename ${RULE_FILENAME})'"
mv "${RULE_FILENAME_TEMPORARY}" "${RULE_FILENAME}"
echo " '${code}', //" $(sed --regexp-extended 's, +$,,' <<< "${name}")
done | sort --unique
sed --silent '/^ \]/,/^}/p' "${filename_rule}"
} > "${temporary_rule}"
mv "${temporary_rule}" "${filename_rule}"
}
download_list "${URL}" "${TEMPORARY_XML}"
update_currency_codes "${RULE}" "${TEMPORARY_XML}"
echo "Finished!"