sync.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. set -euo pipefail
  19. PR_DIR=$1
  20. PR_IMG_DIR="${PR_DIR}/docs/en/images"
  21. PR_DOC_DIR="${PR_DIR}/docs/en"
  22. PR_SIDEBAR_PATH="${PR_DIR}/docs/sidebars.js"
  23. WEBSITE_DIR=$2
  24. WEBSITE_IMG_DIR="${WEBSITE_DIR}/static/image_en"
  25. WEBSITE_DOC_DIR="${WEBSITE_DIR}/docs"
  26. DOCUSAURUS_DOC_SIDEBARS_FILE="${WEBSITE_DIR}/sidebars.js"
  27. ##############################################################
  28. #
  29. # Rebuild specific directory, if directory exists, will remove
  30. # it before create it, otherwise create it directly. It
  31. # supports one or more parameters.
  32. #
  33. # Arguments:
  34. #
  35. # <path...>: One or more directories want to rebuild
  36. #
  37. ##############################################################
  38. function rebuild_dirs() {
  39. for dir in "$@"; do
  40. echo " ---> Rebuild directory ${dir}"
  41. if [ -d "${dir}" ]; then
  42. rm -rf "${dir}"
  43. fi
  44. mkdir -p "${dir}"
  45. done
  46. }
  47. ##############################################################
  48. #
  49. # Remove specific exists file. It supports one or more
  50. # parameters.
  51. #
  52. # Arguments:
  53. #
  54. # <file...>: One or more files want to remove
  55. #
  56. ##############################################################
  57. function rm_exists_files() {
  58. for file in "$@"; do
  59. echo " ---> Remove exists ${file}"
  60. if [ -f "${file}" ]; then
  61. rm -rf "${file}"
  62. fi
  63. done
  64. }
  65. ##############################################################
  66. #
  67. # Replace images path in markdown documents, the source path
  68. # in repo `apache/seatunnel` is like `images/<name>.png`
  69. # and we should replace it to `images_en/<name>.png`
  70. #
  71. # Arguments:
  72. #
  73. # replace_dir: The directory to replace the img path
  74. #
  75. ##############################################################
  76. function replace_images_path(){
  77. replace_dir=$1
  78. for file_path in "${replace_dir}"/*; do
  79. if test -f "${file_path}"; then
  80. if [ "${file_path##*.}"x = "md"x ] || [ "${file_path##*.}"x = "mdx"x ]; then
  81. echo " ---> Replace images path to /doc/image_en in ${file_path}"
  82. if [[ "$OSTYPE" == "darwin"* ]]; then
  83. sed -E -i '' "s/(\.\.\/)*images/\/image_en/g" "${file_path}"
  84. else
  85. sed -E -i "s/(\.\.\/)*images/\/image_en/g" "${file_path}"
  86. fi
  87. fi
  88. else
  89. replace_images_path "${file_path}"
  90. fi
  91. done
  92. }
  93. ##############################################################
  94. # Try build the updated document in the PR
  95. ##############################################################
  96. function prepare_docs() {
  97. echo "===>>>: Start documents sync."
  98. echo "===>>>: Rebuild directory docs, static/image_en."
  99. rebuild_dirs "${WEBSITE_DOC_DIR}" "${WEBSITE_IMG_DIR}"
  100. echo "===>>>: Remove exists file sidebars.js."
  101. rm_exists_files "${DOCUSAURUS_DOC_SIDEBARS_FILE}"
  102. echo "===>>>: Rsync sidebars.js to ${DOCUSAURUS_DOC_SIDEBARS_FILE}"
  103. rsync -av "${PR_SIDEBAR_PATH}" "${DOCUSAURUS_DOC_SIDEBARS_FILE}"
  104. echo "===>>>: Rsync images to ${WEBSITE_IMG_DIR}"
  105. rsync -av "${PR_IMG_DIR}"/ "${WEBSITE_IMG_DIR}"
  106. echo "===>>>: Rsync documents exclude images to ${WEBSITE_DOC_DIR}"
  107. rsync -av --exclude images "${PR_DOC_DIR}"/ "${WEBSITE_DOC_DIR}"
  108. echo "===>>>: Replace images path in ${WEBSITE_DOC_DIR}"
  109. replace_images_path "${WEBSITE_DOC_DIR}"
  110. echo "===>>>: End documents sync"
  111. }
  112. prepare_docs