Skip to content

Commit

Permalink
Merge pull request #11 from voitau/master
Browse files Browse the repository at this point in the history
Add unit tests, add --yes option
  • Loading branch information
voitau authored Apr 2, 2020
2 parents ffcd587 + 8a0fd9c commit 8cc50ed
Show file tree
Hide file tree
Showing 30 changed files with 822 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: shellcheck
name: linting

on:
push:
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: unit-tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
bats:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install BATS
run: |
export BATS_DIR=$HOME/opt/bats
curl -L --fail -q -s https://github.com/bats-core/bats-core/archive/v1.1.0.tar.gz -o bats-core-release.tar.gz
tar zxvf bats-core-release.tar.gz
mkdir -p $BATS_DIR
cd bats-core-1.1.0/
./install.sh $BATS_DIR
- name: Execute unit tests
run: |
$HOME/opt/bats/bin/bats test
51 changes: 21 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[![shellcheck](https://github.com/markdown-localization/mdlm-sh/workflows/shellcheck/badge.svg)](https://github.com/markdown-localization/mdlm-sh/actions?query=workflow:shellcheck)
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/markdown-localization/mdlm-sh)
[![linting](https://github.com/markdown-localization/mdlm-sh/workflows/linting/badge.svg)](https://github.com/markdown-localization/mdlm-sh/actions?query=workflow:linting)
[![unit-tests](https://github.com/voitau/mdlm-sh/workflows/unit-tests/badge.svg)](https://github.com/markdown-localization/mdlm-sh/actions?query=workflow:unit-tests)
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/3814/badge)](https://bestpractices.coreinfrastructure.org/projects/3814)

# Markdown Localization Manager
Expand All @@ -9,10 +11,10 @@ Minimal reference implenentation of [Markdown Localization Specification](https:

To install or update mdlm, you should run the install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command:
```sh
curl -o- https://raw.githubusercontent.com/markdown-localization/mdlm-sh/v0.0.10/install.sh | bash
curl -o- https://raw.githubusercontent.com/markdown-localization/mdlm-sh/v0.0.11/install.sh | bash
```
```sh
wget -qO- https://raw.githubusercontent.com/markdown-localization/mdlm-sh/v0.0.10/install.sh | bash
wget -qO- https://raw.githubusercontent.com/markdown-localization/mdlm-sh/v0.0.11/install.sh | bash
```
Running either of the above commands downloads a script and runs it. The script copies mdlm script to `~/.mdlm`, and attempts to add the source lines from the snippet below to the correct profile file (`~/.bash_profile`, or `~/.bashrc`).

Expand All @@ -26,7 +28,7 @@ alias mdlm='$MDLM_DIR/mdlm.sh'
## List all supported locales

```
$ mdlm ls
$ mdlm search
aa (Afaraf) - Afar
ab (Аҧсуа) - Abkhazian
ae (Avesta) - Avestan
Expand All @@ -42,7 +44,7 @@ av (Авар) - Avaric
## List supported locales matching a specific pattern

```
$ mdlm ls uz
$ mdlm search uz
uz (Ўзбек) Uzbek
uz-Cyrl (Ўзбек) Uzbek in Cyrillic script
uz-Latn (O‘zbekcha) Uzbek in Latin script
Expand All @@ -52,52 +54,41 @@ uz-Latn (O‘zbekcha) Uzbek in Latin script

```
$ mdlm add eo
Creating new localization files.
Please confirm locale - "eo (Esperanto) Esperanto" (yes):
Localize example/README.md (example/README-eo.md)? (yes):
Created.
Localize README.md (README-eo.md)? (yes): n
Skipped.
Finished
Total files created: 1
Creating new localization files for locale - "eo (Esperanto) - Esperanto":
Creating example/README-eo.md. Confirm? [Y/n]
- Created.
Creating README-eo.md. Confirm? [Y/n]
- Created.
Finished. Total files created: 2
```

As a result, new README-eo.md file will be created. Localization switcher header will be added to original and localized files.

## Remove a locale

```
$ mdlm rm eo
WARNING: Deleting localization files.
Please confirm locale - "eo (Esperanto) Esperanto" (yes):
List of files to delete:
example/README-eo.md
Are you sure want to delete all files? (yes):
Localization files deleted.
$ mdlm rm eo--yes
Removing localization files for locale - "eo (Esperanto) - Esperanto":
Removing README-eo.md.
- Removed.
Removing example/README-eo.md.
- Removed.
Localization files removed.
```

As a result, README-eo.md will be removed. Localization switcher header will be updated, or removed, if no other localizations available.
As a result, README-eo.md will be removed. Localization switcher header will be updated, or removed, if no other localizations available. Optional `--yes` automatically sets yes to all prompts.

## Check synchronization status

```
$ mdlm status
Localization status for all locales.
example/README.md (English):
* example/README-ru.md (Русский) - synced.
* example/README-ar.md (العَرَبِيَّة) - synced.
* example/README-zh-Hans.md (简体中文) - synced.
* example/README-es.md (Español) - outdated.
* example/README-fr.md (Français) - synced.
README.md (English):
* README-ru.md (Русский) - synced.
```
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{ # this ensures the entire script is downloaded #

MDLM_VERSION="0.0.10"
MDLM_VERSION="0.0.11"
MDLM_REMOTE_FILE="https://raw.githubusercontent.com/markdown-localization/mdlm-sh/v${MDLM_VERSION}/mdlm.sh"

mdlm_echo() {
Expand Down
141 changes: 98 additions & 43 deletions mdlm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
# Functions:
# - mdlm_* - custom functions.
MDLM_VERSION="0.0.10"
MDLM_VERSION="0.0.11"

DEFAULT_LCM_LOCAL="English"

Expand Down Expand Up @@ -56,10 +56,10 @@ mdlm_validate_args_count() {
}

mdlm_version() {
mdlm_echo "Markdown Localization Manager - Bash (v${MDLM_VERSION})"
mdlm_echo "Markdown Localization Manager (Bash) ${MDLM_VERSION}"
}

mdlm_list_available_locales() {
mdlm_search_available_locales() {
local PATTERN="${1}"
local LCM_LIST
LCM_LIST="$(list_locales)"
Expand All @@ -74,7 +74,7 @@ mdlm_list_available_locales() {
}

mdlm_find_one_locale() {
mdlm_list_available_locales "^${1}[|]" | head -n 1
mdlm_search_available_locales "^${1}[|]" | head -n 1
}

mdlm_get_locale_suffix() {
Expand All @@ -93,15 +93,6 @@ mdlm_validate_locale() {
mdlm_echo "No matching locales found."
exit 1
fi

local LCM_CONFIRM
command echo -n "Please confirm locale - \"${blue}${LCM}${normal}\" (yes): "
read -r LCM_CONFIRM

if [ -n "$LCM_CONFIRM" ] && [ ! "$LCM_CONFIRM" = "yes" ] && [ ! "$LCM_CONFIRM" = "y" ]; then
mdlm_echo "Please choose another locale. Exiting."
exit 1
fi
}

mdlm_find_original_files() {
Expand Down Expand Up @@ -130,64 +121,81 @@ mdlm_copy_original_to_localized_file() {
}

mdlm_add_locale() {
mdlm_echo "Creating new localization files."
local YES_PROMPT
local LCM
local LCM_SUFFIX
local ORIG_FILE
local LCM_FILE
local COUNT=0

YES_PROMPT="${2}"

LCM="$(mdlm_find_one_locale "${1}")"
mdlm_validate_locale "${LCM}"

mdlm_echo "Creating new localization files for locale - \"${blue}${LCM}${normal}\":"
LCM_SUFFIX="$(mdlm_get_locale_suffix "${LCM}")"

for ORIG_FILE in $(mdlm_find_original_files); do
LCM_FILE=$(mdlm_get_localized_file_name "${ORIG_FILE}" "${LCM_SUFFIX}")

command echo -n "Localize ${yellow}${ORIG_FILE}${normal} (${yellow}${LCM_FILE}${normal})? (yes): "
read -r LCM_FILE_CONFIRM
if [ -n "${LCM_FILE_CONFIRM}" ] && [ ! "${LCM_FILE_CONFIRM}" = "yes" ] && [ ! "${LCM_FILE_CONFIRM}" = "y" ]; then
mdlm_echo "- Skipped."
command printf "Creating ${yellow}${LCM_FILE}${normal}."
if [ "${YES_PROMPT}" -eq 0 ]; then
command echo -n " Confirm? [Y/n] "
read -r LCM_FILE_CONFIRM
if [ -n "${LCM_FILE_CONFIRM}" ] && [ ! "${LCM_FILE_CONFIRM}" = "yes" ] && [ ! "${LCM_FILE_CONFIRM}" = "y" ]; then
mdlm_echo "- Skipped."
break
fi
else
mdlm_copy_original_to_localized_file "${ORIG_FILE}" "${LCM_FILE}"

COUNT=$((COUNT+1))
mdlm_echo "- Created."
mdlm_echo
fi

mdlm_copy_original_to_localized_file "${ORIG_FILE}" "${LCM_FILE}"

COUNT=$((COUNT+1))
mdlm_echo "- Created."
done

mdlm_update_all_headers
mdlm_echo "Finished. Total files created: ${COUNT}"
}

mdlm_rm_locale() {
mdlm_echo "Removing localization files."
local YES_PROMPT
local LCM
local LCM_SUFFIX
local RM_ALL_FILES
local RM_FILE


YES_PROMPT="${2}"

LCM="$(mdlm_find_one_locale "${1}")"
mdlm_validate_locale "${LCM}"

mdlm_echo "Removing localization files for locale - \"${blue}${LCM}${normal}\":"
LCM_SUFFIX="$(mdlm_get_locale_suffix "${LCM}")"

RM_ALL_FILES="$(find . -name "*-${LCM_SUFFIX}.md" -printf '%P\n')"

for RM_FILE in $RM_ALL_FILES; do
command echo -n "Delete ${yellow}${RM_FILE}${normal} (yes): "
read -r RM_CONFIRM
if [ -n "$RM_CONFIRM" ] && [ ! "$RM_CONFIRM" = "yes" ] && [ ! "$RM_CONFIRM" = "y" ]
then
mdlm_echo "- Skipped."
command echo -n "Removing ${yellow}${RM_FILE}${normal}."
if [ "${YES_PROMPT}" -eq 0 ]; then
command echo -n " Confirm? [Y/n] "
read -r RM_CONFIRM
if [ -n "$RM_CONFIRM" ] && [ ! "$RM_CONFIRM" = "yes" ] && [ ! "$RM_CONFIRM" = "y" ]; then
mdlm_echo "- Skipped."
break
fi
else
command rm "${RM_FILE}"
mdlm_echo "- Removed."
mdlm_echo
fi

command rm "${RM_FILE}"
mdlm_echo "- Removed."

done

mdlm_update_all_headers
mdlm_echo "Localization files deleted."
mdlm_echo "Localization files removed."
}

mdlm_create_header() {
Expand Down Expand Up @@ -281,7 +289,7 @@ mdlm_status() {
if [ -n "${LCM_PATTERN}" ]; then
LCM="$(mdlm_find_one_locale "${LCM_PATTERN}")"
if [ -n "${LCM}" ]; then
mdlm_echo "Localization status for: ${LCM}."
mdlm_echo "Localization status for - ${blue}${LCM}${normal}:"
else
mdlm_echo "No matching locale found. Localization status for all locales."
fi
Expand Down Expand Up @@ -343,13 +351,17 @@ mdlm_status() {

mdlm_help() {
mdlm_echo "$(mdlm_version)"
mdlm_echo
mdlm_echo "Usage:"
mdlm_echo " mdlm help Show this message"
mdlm_echo " mdlm version Print out the installed version of mdlm"
mdlm_echo " mdlm ls [<pattern>] List all available locales, matching a given <pattern> if provided"
mdlm_echo " mdlm search [<pattern>] List all available locales, matching a given <pattern> if provided"
mdlm_echo " mdlm add <locale> Create localization files for given <locale>"
mdlm_echo " --yes Automatic yes to prompts"
mdlm_echo " mdlm rm <locale> Remove all localization files for given <locale>"
mdlm_echo " --yes Automatic yes to prompts"
mdlm_echo " mdlm status [<locale>] Check synchronization status between original and localized versions, for a given <locale> if provided"
mdlm_echo " mdlm diff [<locale>] Show synchronization diff between original and localized versions, for a given <locale> if provided"
mdlm_echo " mdlm diff [<locale>] Show synchronization diff between original and localized versions, for a given <locale> if provided"
}

mdlm() {
Expand All @@ -367,19 +379,62 @@ mdlm() {
mdlm_help
;;
"version" | "--version")
mdlm_validate_args_count 0 0 $#
mdlm_version
;;
"ls" | "list")
"search")
mdlm_validate_args_count 0 1 $#
mdlm_list_available_locales "${1}"
mdlm_search_available_locales "${1}"
;;
"add")
mdlm_validate_args_count 1 1 $#
mdlm_add_locale "${1}"
local ARGS_COUNT
local YES_PROMPT
local LCM

ARGS_COUNT=$#
YES_PROMPT=0
while [ $# -ne 0 ]
do
case "$1" in
"-y" | "--yes")
YES_PROMPT=1
ARGS_COUNT=$((ARGS_COUNT-1))
shift
;;
*)
LCM="$1"
shift
;;
esac
done

mdlm_validate_args_count 1 1 ${ARGS_COUNT}
mdlm_add_locale "${LCM}" "${YES_PROMPT}"
;;
"rm")
mdlm_validate_args_count 1 1 $#
mdlm_rm_locale "${1}"
local ARGS_COUNT
local YES_PROMPT
local LCM

ARGS_COUNT=$#
YES_PROMPT=0
while [ $# -ne 0 ]
do
case "$1" in
"-y" | "--yes")
YES_PROMPT=1
ARGS_COUNT=$((ARGS_COUNT-1))
shift
;;
*)
LCM="$1"
shift
;;
esac
done

mdlm_validate_args_count 1 1 ${ARGS_COUNT}
mdlm_rm_locale "${LCM}" "${YES_PROMPT}"
;;
"status")
mdlm_validate_args_count 0 1 $#
Expand Down
Loading

0 comments on commit 8cc50ed

Please sign in to comment.