1
0

build.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: MIT
  3. # This script is executed by GitHub Actions for every successful push (on any branch, PR or not).
  4. set -ex
  5. function initialize {
  6. if [[ -z $TLDRHOME ]]; then
  7. export TLDRHOME=${GITHUB_WORKSPACE:-$(pwd)}
  8. fi
  9. if [[ -z $TLDR_LANG_ARCHIVES_DIRECTORY ]]; then
  10. export TLDR_LANG_ARCHIVES_DIRECTORY="${GITHUB_WORKSPACE:-$(pwd)}/language_archives"
  11. fi
  12. export TLDR_ARCHIVE="tldr.zip"
  13. DIFF="$(git diff --name-only @^ @ | cut -d'/' -f1)"
  14. export DIFF
  15. }
  16. function build_index {
  17. if ! grep -xq "pages.*" <<< "$DIFF"; then
  18. return
  19. fi
  20. npm run build-index
  21. echo "Pages index successfully built."
  22. }
  23. function build_archive {
  24. if ! grep -xq "pages.*" <<< "$DIFF"; then
  25. return
  26. fi
  27. rm -f "$TLDR_ARCHIVE"
  28. cd "$TLDRHOME/"
  29. zip -q -r "$TLDR_ARCHIVE" pages* LICENSE.md index.json
  30. echo "Pages archive successfully built."
  31. }
  32. function build_translation_archives {
  33. local source_directory target_directory lang archive_name
  34. source_directory="$TLDRHOME"
  35. target_directory="$TLDR_LANG_ARCHIVES_DIRECTORY"
  36. mkdir -p "$target_directory"
  37. rm -f "$target_directory"/*
  38. for lang_dir in "$source_directory"/pages*; do
  39. lang=$(basename "$lang_dir")
  40. # Skip symlinks (pages.en), files outside pages* and directories that haven't been changed.
  41. if [[ ! -d $lang_dir || -h $lang_dir ]] || ! grep -Fxq "$lang" <<< "$DIFF"; then
  42. continue
  43. fi
  44. archive_name="tldr-$lang.zip"
  45. # Create the zip archive
  46. cd "$lang_dir"
  47. zip -q -r "$target_directory/$archive_name" .
  48. zip -q -j "$target_directory/$archive_name" "$source_directory/LICENSE.md"
  49. echo "Pages archive of $archive_name successfully created."
  50. done
  51. cd "$target_directory"
  52. if grep -Fxq pages <<< "$DIFF"; then
  53. cp tldr-pages.zip tldr-pages.en.zip
  54. fi
  55. }
  56. ###################################
  57. # MAIN
  58. ###################################
  59. initialize
  60. build_index
  61. build_archive
  62. build_translation_archives