From 28d2b2e391ab8973173eb344102dd5834c7388a3 Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Fri, 2 Feb 2024 17:50:19 -0500 Subject: [PATCH] chore(manager): cleanup and fix Manager build (#1497) --- .../electron_app/build.action.sh | 40 +++---- .../electron_app/package.action.sh | 103 ++++++++++++++++++ .../electron_app/start.action.sh | 2 +- .../scripts/fill_packaging_opts.sh | 57 ---------- .../scripts/finish_info_files.sh | 33 ------ .../scripts/get_electron_build_flags.mjs | 59 ---------- .../scripts/get_manager_release_channel.sh | 22 ---- src/server_manager/scripts/get_version.mjs | 36 ------ src/server_manager/web_app/build.action.sh | 2 +- 9 files changed, 121 insertions(+), 233 deletions(-) create mode 100755 src/server_manager/electron_app/package.action.sh delete mode 100755 src/server_manager/scripts/fill_packaging_opts.sh delete mode 100755 src/server_manager/scripts/finish_info_files.sh delete mode 100644 src/server_manager/scripts/get_electron_build_flags.mjs delete mode 100755 src/server_manager/scripts/get_manager_release_channel.sh delete mode 100644 src/server_manager/scripts/get_version.mjs diff --git a/src/server_manager/electron_app/build.action.sh b/src/server_manager/electron_app/build.action.sh index 837b62a8f..94e947d3f 100755 --- a/src/server_manager/electron_app/build.action.sh +++ b/src/server_manager/electron_app/build.action.sh @@ -13,10 +13,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -set -eux +set -eu + +# Electron app root folder +readonly STATIC_DIR="${BUILD_DIR}/server_manager/electron_app/static" +rm -rf "${STATIC_DIR}" +mkdir -p "${STATIC_DIR}" -PLATFORM=$1 -STAGING_PERCENTAGE=100 VERSION_NAME='0.0.0-debug' BUILD_MODE=debug for i in "$@"; do @@ -29,10 +32,6 @@ for i in "$@"; do VERSION_NAME="${i#*=}" shift ;; - --stagingPercentage=*) - STAGING_PERCENTAGE="${i#*=}" - shift - ;; --* | -*) echo "Unknown option: ${i}" exit 1 @@ -41,12 +40,14 @@ for i in "$@"; do esac done -readonly OUT_DIR="${BUILD_DIR}/server_manager/electron_app" -rm -rf "${OUT_DIR}" - -# Electron app root folder -readonly STATIC_DIR="${OUT_DIR}/static" -mkdir -p "${STATIC_DIR}" +if [[ -z "${WEBPACK_MODE:-}" ]]; then + case "${BUILD_MODE}" in + release) + export WEBPACK_MODE="production";; + *) + export WEBPACK_MODE="development";; + esac +fi # Build the Web App. run_action server_manager/web_app/build @@ -54,16 +55,13 @@ run_action server_manager/web_app/build # Compile the Electron main process and preload to the app root folder. # Since Node.js on Cygwin doesn't like absolute Unix-style paths, # we'll use relative paths here. -webpack --config=src/server_manager/electron_main.webpack.mjs ${BUILD_ENV:+--mode=${BUILD_ENV}} -webpack --config=src/server_manager/electron_preload.webpack.mjs ${BUILD_ENV:+--mode=${BUILD_ENV}} +webpack --config=src/server_manager/electron_main.webpack.mjs ${WEBPACK_MODE:+--mode=${WEBPACK_MODE}} +webpack --config=src/server_manager/electron_preload.webpack.mjs ${WEBPACK_MODE:+--mode=${WEBPACK_MODE}} # Assemble everything together. mkdir -p "${STATIC_DIR}/server_manager" cp -r "${BUILD_DIR}/server_manager/web_app/static" "${STATIC_DIR}/server_manager/web_app/" -# TODO(fortuna): Separate the build of Electron main and the Electron package. -# Building the package significantly delays the start action. - # Electron requires a package.json file for the app's name, etc. # We also need to install NPMs at this location for require() # in order for require() to work right in the renderer process, which @@ -76,9 +74,3 @@ npm ci --prod --ignore-scripts # Icons. cd "${ROOT_DIR}" electron-icon-maker --input=src/server_manager/images/launcher-icon.png --output=build/server_manager/electron_app/static - -# TODO(daniellacosse): refactor these scripts into node so we can call the electron builder there directly -# shellcheck disable=SC2046 -electron-builder $(node src/server_manager/scripts/get_electron_build_flags.mjs "${PLATFORM}" --buildMode "${BUILD_MODE}") - -src/server_manager/scripts/finish_info_files.sh "${PLATFORM}" "${STAGING_PERCENTAGE}" diff --git a/src/server_manager/electron_app/package.action.sh b/src/server_manager/electron_app/package.action.sh new file mode 100755 index 000000000..eac7d0e3a --- /dev/null +++ b/src/server_manager/electron_app/package.action.sh @@ -0,0 +1,103 @@ +#!/bin/bash -eu +# +# Copyright 2024 The Outline Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +set -eu + +SCRIPT_DIR="$(dirname "$0")" +PROJECT_DIR="${BUILD_DIR}/server_manager/electron_app/static" +BUILD_MODE=debug +PLATFORM= + +function package_electron() { + declare -a electron_builder_cmd=( + electron-builder + --projectDir="${PROJECT_DIR}" + --config="${SCRIPT_DIR}/electron_builder.json" + --publish=never + ) + + case "${PLATFORM}" in + linux) + electron_builder_cmd+=(--linux);; + windows) + electron_builder_cmd+=(--win --ia32);; + macos) + electron_builder_cmd+=(--mac);; + *) + echo "Unsupported platform ${PLATFORM}" >&2 && exit 1 + esac + + if [[ "${BUILD_MODE}" == "release" ]]; then + electron_builder_cmd+=( + --config.generateUpdatesFilesForAllChannels=true + --config.publish.provider=generic + --config.publish.url=https://s3.amazonaws.com/outline-releases/manager/ + ) + fi + + "${electron_builder_cmd[@]}" +} + +function finish_yaml_files() { + declare -r staging_percentage="${1?Staging percentage missing}" + + local release_channel + release_channel=$(node_modules/node-jq/bin/jq -r '.version' src/server_manager/package.json | cut -s -d'-' -f2) + # If this isn't an alpha or beta build, `cut -s` will return an empty string + if [[ -z "${release_channel}" ]]; then + release_channel=latest + fi + echo "stagingPercentage: ${staging_percentage}" >> "${PROJECT_DIR}/dist/${release_channel}${PLATFORM}.yml" + + # If we cut a staged mainline release, beta testers will take the update as well. + if [[ "${release_channel}" == "latest" ]]; then + echo "stagingPercentage: ${staging_percentage}" >> "${PROJECT_DIR}/dist/beta${PLATFORM}.yml" + fi + + # We don't support alpha releases + rm -f "${PROJECT_DIR}/dist/alpha${PLATFORM}.yml" +} + +function main() { + declare staging_percentage=100 + declare version_name='0.0.0-debug' + for i in "$@"; do + case "${i}" in + --buildMode=*) + BUILD_MODE="${i#*=}" + shift + ;; + --versionName=*) + version_name="${i#*=}" + shift + ;; + --stagingPercentage=*) + staging_percentage="${i#*=}" + shift + ;; + --* | -*) + echo "Unknown option: ${i}" + exit 1 + ;; + *) ;; + esac + done + PLATFORM="${1?Platform missing}" + run_action server_manager/electron_app/build --buildMode="${BUILD_MODE}" --versionName="${version_name}" + package_electron + finish_yaml_files "${staging_percentage}" +} + +main "$@" diff --git a/src/server_manager/electron_app/start.action.sh b/src/server_manager/electron_app/start.action.sh index 9db457399..bc9b63da6 100755 --- a/src/server_manager/electron_app/start.action.sh +++ b/src/server_manager/electron_app/start.action.sh @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -run_action server_manager/electron_app/build "$1" +run_action server_manager/electron_app/build "$@" cd "${BUILD_DIR}/server_manager/electron_app/static" diff --git a/src/server_manager/scripts/fill_packaging_opts.sh b/src/server_manager/scripts/fill_packaging_opts.sh deleted file mode 100755 index e14be8d11..000000000 --- a/src/server_manager/scripts/fill_packaging_opts.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash -eu -# -# Copyright 2020 The Outline Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Collects common packaging options. Meant to be called from the diffrent package_foo and -# release_foo scripts. -# Usage from a packaging script: -# source src/server_manager/electron_app/package_$PLATFORM $0 $@ -# -# Note that you MUST use "source" in order to run the script in the same process as the calling -# script, allowing fill_packaging_opts.sh to fill variables for the caller. - -# Input: "/absolute/path/src/server_manager/electron_app/something_action.sh" -# Output: "npm run action server_manager/electron_app/something" -readonly ELECTRON_PATH='server_manager/electron_app/' -readonly RELATIVE="${1#*/src/${ELECTRON_PATH}}" -readonly NPM_COMMAND="npm run action ${ELECTRON_PATH}${RELATIVE%.action.sh}" -shift - -function usage () { - echo "Usage:" 1>&2 - echo "${NPM_COMMAND} [-s stagingPercentage]" 1>&2 - echo " -s: The staged rollout percentage for this release. Must be in the interval (0, 100]. Defaults to 100" 1>&2 - echo " -h: this help message" 1>&2 - echo 1>&2 - echo "Examples:" 1>&2 - echo "Releases the beta of version 1.2.3 to 10% of users listening on the beta channel" 1>&2 - echo '$ '"jq -r '.version' src/server_manager/package.json'" 1>&2 - echo "1.2.3-beta" 1>&2 - echo '$ '"${YARN_COMMAND} -s 10" 1>&2 - exit 1 -} - -STAGING_PERCENTAGE=100 -while getopts s:? opt; do - case ${opt} in - s) STAGING_PERCENTAGE=${OPTARG} ;; - *) usage ;; - esac -done - -if ((STAGING_PERCENTAGE <= 0)) || ((STAGING_PERCENTAGE > 100)); then - echo "Staging percentage must be greater than 0 and no more than 100" 1>&2 - exit 1 -fi diff --git a/src/server_manager/scripts/finish_info_files.sh b/src/server_manager/scripts/finish_info_files.sh deleted file mode 100755 index 81491e69f..000000000 --- a/src/server_manager/scripts/finish_info_files.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -eu -# -# Copyright 2018 The Outline Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -PLATFORM="-$1" -if [[ "${PLATFORM}" == "-win" ]]; then - PLATFORM="" -fi -readonly STAGING_PERCENTAGE="$2" -readonly BUILD_DIR='build/server_manager/electron_app/static' - -INFO_FILE_CHANNEL=$(src/server_manager/scripts/get_manager_release_channel.sh) -echo "stagingPercentage: ${STAGING_PERCENTAGE}" >> "${BUILD_DIR}/dist/${INFO_FILE_CHANNEL}${PLATFORM}.yml" - -# If we cut a staged mainline release, beta testers will take the update as well. -if [[ "${INFO_FILE_CHANNEL}" == "latest" ]]; then - echo "stagingPercentage: ${STAGING_PERCENTAGE}" >> "${BUILD_DIR}/dist/beta${PLATFORM}.yml" -fi - -# We don't support alpha releases -rm -f "${BUILD_DIR}/dist/alpha${PLATFORM}.yml" diff --git a/src/server_manager/scripts/get_electron_build_flags.mjs b/src/server_manager/scripts/get_electron_build_flags.mjs deleted file mode 100644 index 42338781f..000000000 --- a/src/server_manager/scripts/get_electron_build_flags.mjs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2022 The Outline Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -import minimist from 'minimist'; -import url from 'url'; - -export async function getElectronBuildFlags(platform, buildMode) { - let buildFlags = [ - '--projectDir=build/server_manager/electron_app/static', - '--config=../../../../src/server_manager/electron_app/electron_builder.json', - '--publish=never', - ]; - - switch (platform) { - case 'linux': - buildFlags = ['--linux', ...buildFlags]; - break; - case 'windows': - buildFlags = ['--win', '--ia32', ...buildFlags]; - break; - case 'macos': - buildFlags = ['--mac', ...buildFlags]; - } - - if (buildMode === 'release') { - buildFlags = [ - ...buildFlags, - '--config.generateUpdatesFilesForAllChannels=true', - '--config.publish.provider=generic', - '--config.publish.url=https://s3.amazonaws.com/outline-releases/manager/', - ]; - } - - return buildFlags; -} - -async function main() { - const {_, buildMode} = minimist(process.argv); - - const platform = _[2]; - - console.log((await getElectronBuildFlags(platform, buildMode)).join(' ')); -} - -if (import.meta.url === url.pathToFileURL(process.argv[1]).href) { - (async function () { - return main(); - })(); -} diff --git a/src/server_manager/scripts/get_manager_release_channel.sh b/src/server_manager/scripts/get_manager_release_channel.sh deleted file mode 100755 index 750cb0f14..000000000 --- a/src/server_manager/scripts/get_manager_release_channel.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -eu -# -# Copyright 2020 The Outline Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# If this isn't an alpha or beta build, `cut -s` will return an empty string -INFO_FILE_CHANNEL=$(node_modules/node-jq/bin/jq -r '.version' src/server_manager/package.json | cut -s -d'-' -f2) -if [[ -z "${INFO_FILE_CHANNEL}" ]]; then - INFO_FILE_CHANNEL=latest -fi -echo "${INFO_FILE_CHANNEL}" diff --git a/src/server_manager/scripts/get_version.mjs b/src/server_manager/scripts/get_version.mjs deleted file mode 100644 index 4f4711819..000000000 --- a/src/server_manager/scripts/get_version.mjs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2022 The Outline Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import url from "url"; -import path from "path"; -import {readFile} from "fs/promises"; - -const __filename = url.fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - -export async function get_version() { - const {version} = JSON.parse(await readFile(path.resolve(__dirname, "../package.json"))); - - return version; -} - -async function main() { - console.log(await get_version()); -} - -if (import.meta.url === url.pathToFileURL(process.argv[1]).href) { - (async function() { - return main(); - })(); -} diff --git a/src/server_manager/web_app/build.action.sh b/src/server_manager/web_app/build.action.sh index 45f0fbf5e..d20a3591f 100755 --- a/src/server_manager/web_app/build.action.sh +++ b/src/server_manager/web_app/build.action.sh @@ -25,5 +25,5 @@ run_action server_manager/web_app/build_install_script # So, we use a relative path as input to webpack. pushd "${ROOT_DIR}" > /dev/null # Notice that we forward the build environment if defined. -webpack --config=src/server_manager/electron_renderer.webpack.js ${BUILD_ENV:+--mode=${BUILD_ENV}} +webpack --config=src/server_manager/electron_renderer.webpack.js ${WEBPACK_MODE:+--mode=${WEBPACK_MODE}} popd > /dev/null