You are here

build-gutenberg.sh in Gutenberg 8.2

#!/usr/bin/env bash

# Break on any error or undefined variable.
set -eu

SCRIPTS_DIR=$(cd "$(dirname "$0")"; pwd)
MODULE_DIR=$(realpath "${SCRIPTS_DIR}/..")
# Use the GUTENBERG_REPO_DIR environment variable as the default if available.
REPO_DIR=${GUTENBERG_REPO_DIR:-"$MODULE_DIR/.gutenberg"}
GIT_REPO="https://github.com/WordPress/gutenberg.git"
GIT_VERSION=master
GIT_FETCH_ALL=${GIT_FETCH_ALL:-0}
CHECKOUT_BRANCH="${GIT_VERSION}"
CLEAN_REPO=0
GIT_TAG=0
BUILD_MODE='production'

display_help() {
  script="$0"
  echo "Script for generating Gutenberg asset files..."
  echo "Usage: $script [repo_directory] [options...]" >&2;
  echo
  echo "   [repo_directory]               The directory to clone Gutenberg into. Defaults to GUTENBERG_REPO_DIR environment variable or [.gutenberg]"
  echo "                                  If using \`.gutenberg\`, you might want to add '.gutenberg' to your local \`\$settings['file_scan_ignore_directories']\`"
  echo
  echo "   --version {version}            The Gutenberg version to pull e.g. 'v7.8.1'. The 'v' prefix is important. Defaults to [master]"
  echo "   --clean                        Flag indicating that the repo should be cleaned. Useful when switching between versions and the node_modules need to be purged."
  echo "   --fetch-all                    Whether to fetch all branches and tags from the remote repository."
  echo "   --dev                          Flag indicating that a dev version of the assets should be generated. This typically enables source-map support."
  echo
  echo "   -h, --help                     This help text"
  echo
  echo "Example usage:"
  echo
  echo "  Install Gutenberg 7.8.1 in development mode:"
  echo
  echo "    $script --version v7.8.1 --dev"
  echo
}

POSITIONAL=()
while (( "$#" )); do
  case "$1" in
    --version|--version=*) value="${1##--version=}"
      if [[ "$value" != "$1" ]]; then shift; else value="$2"; shift 2; fi
      GIT_VERSION="$value";
      if [[ "${GIT_VERSION}" =~ ^v[0-9] ]]; then
        GIT_TAG=1
        CHECKOUT_BRANCH="branch-${GIT_VERSION}"
      else
        CHECKOUT_BRANCH="${GIT_VERSION}"
      fi
    ;;
    --fetch-all)
      GIT_FETCH_ALL=1; shift
    ;;
    --clean)
      CLEAN_REPO=1; shift
    ;;
    --dev)
      BUILD_MODE=dev; shift
    ;;

    --help | -h)
      display_help
      exit 0;
    ;;
    *)
      POSITIONAL+=( "$1" )
      shift
    ;;
  esac
done

set -- "${POSITIONAL[@]}" # restore unmatched positional parameters

if [[ "$#" -gt 0 ]]; then
  REPO_DIR="$1";
fi

mkdir -p "${REPO_DIR}";

if [[ -d "${REPO_DIR}/.git" ]]; then
  # Folder has already been initialized. We can do a reset and pull ourselves.
  echo "The Gutenberg repo already exists on disk ๐Ÿ˜Š..."
else
  echo "The Gutenberg repo does not exist on disk..."
  echo "Cloning repo (will take a while ๐Ÿ˜ฃ)..."
  git clone "${GIT_REPO}" "${REPO_DIR}"
fi

# Initialize the Gutenberg repo.
cd "${REPO_DIR}"

if [[ "$GIT_VERSION" = 'master' ]]; then
  # This is probably a mistake, as builds should be done against version tags.
  echo "๐Ÿšจ About to build Gutenberg for the $GIT_VERSION branch! Are you sure? ๐Ÿšจ"
  echo -n "[y]es/[N]o: "
  read answer
  if [[ ! "$answer" =~ ^([yY]) ]]; then
    exit
  fi
fi

if [[ "${GIT_FETCH_ALL}" = 1 ]]; then
  echo "Fetch all upstream branches and tags (might be really slow)"
  git fetch --all --tags
else
  if [[ "${GIT_TAG}" = 1 ]]; then
    echo "Fetching ${GIT_VERSION} tag from origin..."
    git fetch origin "refs/tags/${GIT_VERSION}"
  else
    echo "Fetching ${GIT_VERSION} branch from origin..."
    git fetch origin "${GIT_VERSION}"
  fi
fi

echo "Git reset hard"
git reset --hard HEAD

# Checkout or create the relevant branch.
if git rev-parse --verify --quiet "${CHECKOUT_BRANCH}"; then
  echo "Checking out existing branch."
  git checkout "${CHECKOUT_BRANCH}"
else
  if [[ "${GIT_TAG}" = 1 ]]; then
    # Checking out a version tag.
    echo "Checkout version tag: ${GIT_VERSION}"
    git checkout tags/"${GIT_VERSION}" -b "${CHECKOUT_BRANCH}"
  else
    echo "Checkout branch: ${CHECKOUT_BRANCH}"
    git checkout --track origin/"${CHECKOUT_BRANCH}" -b "${CHECKOUT_BRANCH}"
  fi
fi

git pull origin "${GIT_VERSION}"

if [[ "${CLEAN_REPO}" = 1 ]]; then
  to_clean=$(git clean -xdf --dry-run)
  if [[ ! -z "$to_clean" ]]; then
    echo "$to_clean"
    echo "๐Ÿšจ About to delete above files in the $REPO_DIR folder! Is this okay? ๐Ÿšจ"
    echo -n "[y]es/[N]o: "
    read answer
    #if [[ "$answer" =~ ^([yY]) ]]; then
    if [[ "$answer" != "${answer#[Yy]}" ]]; then
      # Remove ignored files to reset repository to pristine condition. Previous
      # test ensures that changed files abort the plugin build.
      echo "Cleaning working directory... ๐Ÿ›€"
      git clean -xdf
    fi
  fi
fi

# Run npm install.
# npm 6.9+ is required due to https://github.com/WordPress/gutenberg/pull/18048#discussion_r337402830
echo "Running npm install..."
npm install

echo "Building assets..."
rm "${REPO_DIR}/build" -rf
if [[ "${BUILD_MODE}" = 'production' ]]; then
  npm run build
else
  # Replicate 'npm run dev' without having to watch.
  npm run build:packages
  node "${REPO_DIR}/node_modules/webpack/bin/webpack.js"
fi

# Switch the working directory to the module.
cd "${MODULE_DIR}"

echo "Copying compiled assets..."

# Copy the built assets over to the vendor folder.
rm -rf "${MODULE_DIR}/vendor/gutenberg" &&  \
 cp -r "${REPO_DIR}/build" "${MODULE_DIR}/vendor/gutenberg"

# Regenerate the dependencies.
php "${SCRIPTS_DIR}/gutenberg-dependencies.php"

# Update the vendor scripts.
php "${SCRIPTS_DIR}/generate-vendor.php"

# Update the Gutenberg JS version constants in 'gutenberg.module'.
php "${SCRIPTS_DIR}/write-gutenberg-version.php" "${REPO_DIR}" --mode="$BUILD_MODE"

File

scripts/build-gutenberg.sh
View source
  1. #!/usr/bin/env bash
  2. # Break on any error or undefined variable.
  3. set -eu
  4. SCRIPTS_DIR=$(cd "$(dirname "$0")"; pwd)
  5. MODULE_DIR=$(realpath "${SCRIPTS_DIR}/..")
  6. # Use the GUTENBERG_REPO_DIR environment variable as the default if available.
  7. REPO_DIR=${GUTENBERG_REPO_DIR:-"$MODULE_DIR/.gutenberg"}
  8. GIT_REPO="https://github.com/WordPress/gutenberg.git"
  9. GIT_VERSION=master
  10. GIT_FETCH_ALL=${GIT_FETCH_ALL:-0}
  11. CHECKOUT_BRANCH="${GIT_VERSION}"
  12. CLEAN_REPO=0
  13. GIT_TAG=0
  14. BUILD_MODE='production'
  15. display_help() {
  16. script="$0"
  17. echo "Script for generating Gutenberg asset files..."
  18. echo "Usage: $script [repo_directory] [options...]" >&2;
  19. echo
  20. echo " [repo_directory] The directory to clone Gutenberg into. Defaults to GUTENBERG_REPO_DIR environment variable or [.gutenberg]"
  21. echo " If using \`.gutenberg\`, you might want to add '.gutenberg' to your local \`\$settings['file_scan_ignore_directories']\`"
  22. echo
  23. echo " --version {version} The Gutenberg version to pull e.g. 'v7.8.1'. The 'v' prefix is important. Defaults to [master]"
  24. echo " --clean Flag indicating that the repo should be cleaned. Useful when switching between versions and the node_modules need to be purged."
  25. echo " --fetch-all Whether to fetch all branches and tags from the remote repository."
  26. echo " --dev Flag indicating that a dev version of the assets should be generated. This typically enables source-map support."
  27. echo
  28. echo " -h, --help This help text"
  29. echo
  30. echo "Example usage:"
  31. echo
  32. echo " Install Gutenberg 7.8.1 in development mode:"
  33. echo
  34. echo " $script --version v7.8.1 --dev"
  35. echo
  36. }
  37. POSITIONAL=()
  38. while (( "$#" )); do
  39. case "$1" in
  40. --version|--version=*) value="${1##--version=}"
  41. if [[ "$value" != "$1" ]]; then shift; else value="$2"; shift 2; fi
  42. GIT_VERSION="$value";
  43. if [[ "${GIT_VERSION}" =~ ^v[0-9] ]]; then
  44. GIT_TAG=1
  45. CHECKOUT_BRANCH="branch-${GIT_VERSION}"
  46. else
  47. CHECKOUT_BRANCH="${GIT_VERSION}"
  48. fi
  49. ;;
  50. --fetch-all)
  51. GIT_FETCH_ALL=1; shift
  52. ;;
  53. --clean)
  54. CLEAN_REPO=1; shift
  55. ;;
  56. --dev)
  57. BUILD_MODE=dev; shift
  58. ;;
  59. --help | -h)
  60. display_help
  61. exit 0;
  62. ;;
  63. *)
  64. POSITIONAL+=( "$1" )
  65. shift
  66. ;;
  67. esac
  68. done
  69. set -- "${POSITIONAL[@]}" # restore unmatched positional parameters
  70. if [[ "$#" -gt 0 ]]; then
  71. REPO_DIR="$1";
  72. fi
  73. mkdir -p "${REPO_DIR}";
  74. if [[ -d "${REPO_DIR}/.git" ]]; then
  75. # Folder has already been initialized. We can do a reset and pull ourselves.
  76. echo "The Gutenberg repo already exists on disk ๐Ÿ˜Š..."
  77. else
  78. echo "The Gutenberg repo does not exist on disk..."
  79. echo "Cloning repo (will take a while ๐Ÿ˜ฃ)..."
  80. git clone "${GIT_REPO}" "${REPO_DIR}"
  81. fi
  82. # Initialize the Gutenberg repo.
  83. cd "${REPO_DIR}"
  84. if [[ "$GIT_VERSION" = 'master' ]]; then
  85. # This is probably a mistake, as builds should be done against version tags.
  86. echo "๐Ÿšจ About to build Gutenberg for the $GIT_VERSION branch! Are you sure? ๐Ÿšจ"
  87. echo -n "[y]es/[N]o: "
  88. read answer
  89. if [[ ! "$answer" =~ ^([yY]) ]]; then
  90. exit
  91. fi
  92. fi
  93. if [[ "${GIT_FETCH_ALL}" = 1 ]]; then
  94. echo "Fetch all upstream branches and tags (might be really slow)"
  95. git fetch --all --tags
  96. else
  97. if [[ "${GIT_TAG}" = 1 ]]; then
  98. echo "Fetching ${GIT_VERSION} tag from origin..."
  99. git fetch origin "refs/tags/${GIT_VERSION}"
  100. else
  101. echo "Fetching ${GIT_VERSION} branch from origin..."
  102. git fetch origin "${GIT_VERSION}"
  103. fi
  104. fi
  105. echo "Git reset hard"
  106. git reset --hard HEAD
  107. # Checkout or create the relevant branch.
  108. if git rev-parse --verify --quiet "${CHECKOUT_BRANCH}"; then
  109. echo "Checking out existing branch."
  110. git checkout "${CHECKOUT_BRANCH}"
  111. else
  112. if [[ "${GIT_TAG}" = 1 ]]; then
  113. # Checking out a version tag.
  114. echo "Checkout version tag: ${GIT_VERSION}"
  115. git checkout tags/"${GIT_VERSION}" -b "${CHECKOUT_BRANCH}"
  116. else
  117. echo "Checkout branch: ${CHECKOUT_BRANCH}"
  118. git checkout --track origin/"${CHECKOUT_BRANCH}" -b "${CHECKOUT_BRANCH}"
  119. fi
  120. fi
  121. git pull origin "${GIT_VERSION}"
  122. if [[ "${CLEAN_REPO}" = 1 ]]; then
  123. to_clean=$(git clean -xdf --dry-run)
  124. if [[ ! -z "$to_clean" ]]; then
  125. echo "$to_clean"
  126. echo "๐Ÿšจ About to delete above files in the $REPO_DIR folder! Is this okay? ๐Ÿšจ"
  127. echo -n "[y]es/[N]o: "
  128. read answer
  129. #if [[ "$answer" =~ ^([yY]) ]]; then
  130. if [[ "$answer" != "${answer#[Yy]}" ]]; then
  131. # Remove ignored files to reset repository to pristine condition. Previous
  132. # test ensures that changed files abort the plugin build.
  133. echo "Cleaning working directory... ๐Ÿ›€"
  134. git clean -xdf
  135. fi
  136. fi
  137. fi
  138. # Run npm install.
  139. # npm 6.9+ is required due to https://github.com/WordPress/gutenberg/pull/18048#discussion_r337402830
  140. echo "Running npm install..."
  141. npm install
  142. echo "Building assets..."
  143. rm "${REPO_DIR}/build" -rf
  144. if [[ "${BUILD_MODE}" = 'production' ]]; then
  145. npm run build
  146. else
  147. # Replicate 'npm run dev' without having to watch.
  148. npm run build:packages
  149. node "${REPO_DIR}/node_modules/webpack/bin/webpack.js"
  150. fi
  151. # Switch the working directory to the module.
  152. cd "${MODULE_DIR}"
  153. echo "Copying compiled assets..."
  154. # Copy the built assets over to the vendor folder.
  155. rm -rf "${MODULE_DIR}/vendor/gutenberg" && \
  156. cp -r "${REPO_DIR}/build" "${MODULE_DIR}/vendor/gutenberg"
  157. # Regenerate the dependencies.
  158. php "${SCRIPTS_DIR}/gutenberg-dependencies.php"
  159. # Update the vendor scripts.
  160. php "${SCRIPTS_DIR}/generate-vendor.php"
  161. # Update the Gutenberg JS version constants in 'gutenberg.module'.
  162. php "${SCRIPTS_DIR}/write-gutenberg-version.php" "${REPO_DIR}" --mode="$BUILD_MODE"