From d3575d537da9652431a4ac8a7770d6c2d9aecc80 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 19 Feb 2019 21:21:34 +0000 Subject: [PATCH] dev.sh fixups (#2114) * dev.sh: use dash instead of slash for msbuild Instead of switching between windows and non-windows to determine how to handle slashes for msbuild, use dashes instead of slashes to simplify the calling. * dev.sh: stop on errors Stop on errors, instead of continuing. This prevents us from failing to move through the directory space with `cd` / `pushd` / `popd` but still running commands. This is particularly dangerous when running commands like `rm`. * dev.sh: quote all filepaths Since directories may have a space in them, quote them to treat them as a single entity instead of wordsplitting on a space. Otherwise, if `FOO="a b c"` then `rm -rf $FOO` will remove files or folders named `a`, b`, and `c` instead of removing the single entity named `a b c`. * dev.sh: remove files carefully When expanding variables to pass to `rm`, make sure that the path variable is set and fail if it is not. This prevents an `rm` from accidentally expanding to `rm ""/*` when the variable is unset: https://github.com/ValveSoftware/steam-for-linux/issues/3671 Using `${FOO:?}` syntax will fail when `FOO` is unset. * dev.sh: quote the `$` in `$LastExitCode` `$LastExitCode` is not a bash variable; to pass that string along to PowerShell, it needs to be quoted. * dev.sh: use $(cmd) syntax instead of backticks The $(cmd) execution syntax is preferred over the legacy backtick syntax. * dev.sh: quote variables * externals.sh: quote variables to cope with spaces Quote the variables for the directories so that we can properly work with directories with spaces in their names. --- src/Misc/externals.sh | 10 +++--- src/dev.sh | 80 +++++++++++++++++++------------------------ 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/src/Misc/externals.sh b/src/Misc/externals.sh index 62ba9126a9..e4f7f10da8 100755 --- a/src/Misc/externals.sh +++ b/src/Misc/externals.sh @@ -12,8 +12,8 @@ get_abs_path() { echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" } -LAYOUT_DIR=$(get_abs_path `dirname $0`/../../_layout) -DOWNLOAD_DIR="$(get_abs_path `dirname $0`/../../_downloads)/netcore2x" +LAYOUT_DIR=$(get_abs_path "$(dirname $0)/../../_layout") +DOWNLOAD_DIR="$(get_abs_path "$(dirname $0)/../../_downloads")/netcore2x" function failed() { local error=${1:-Undefined error} @@ -41,8 +41,8 @@ function acquireExternalTool() { # Check if the download already exists. local download_target="$DOWNLOAD_DIR/$relative_url" - local download_basename="$(basename $download_target)" - local download_dir="$(dirname $download_target)" + local download_basename="$(basename "$download_target")" + local download_dir="$(dirname "$download_target")" if [[ "$PRECACHE" != "" ]]; then if [ -f "$download_target" ]; then @@ -57,7 +57,7 @@ function acquireExternalTool() { # Download from source to the partial file. echo "Downloading $download_source" - mkdir -p "$(dirname $download_target)" || checkRC 'mkdir' + mkdir -p "$(dirname "$download_target")" || checkRC 'mkdir' # curl -f Fail silently (no output at all) on HTTP errors (H) # -k Allow connections to SSL sites without certs (H) # -S Show error. With -s, make curl show errors when they occur diff --git a/src/dev.sh b/src/dev.sh index 2c09641cbd..a34966194c 100755 --- a/src/dev.sh +++ b/src/dev.sh @@ -6,6 +6,8 @@ # ############################################################################### +set -e + DEV_CMD=$1 DEV_CONFIG=$2 @@ -16,9 +18,9 @@ PACKAGE_DIR="$SCRIPT_DIR/../_package" DOTNETSDK_ROOT="$SCRIPT_DIR/../_dotnetsdk" DOTNETSDK_VERSION="2.1.403" DOTNETSDK_INSTALLDIR="$DOTNETSDK_ROOT/$DOTNETSDK_VERSION" -AGENT_VERSION=`cat agentversion` +AGENT_VERSION=$(cat agentversion) -pushd $SCRIPT_DIR +pushd "$SCRIPT_DIR" BUILD_CONFIG="Debug" if [[ "$DEV_CONFIG" == "Release" ]]; then @@ -26,8 +28,8 @@ if [[ "$DEV_CONFIG" == "Release" ]]; then fi CURRENT_PLATFORM="windows" -if [[ (`uname` == "Linux") || (`uname` == "Darwin") ]]; then - CURRENT_PLATFORM=`echo \`uname\` | awk '{print tolower($0)}'` +if [[ ($(uname) == "Linux") || ($(uname) == "Darwin") ]]; then + CURRENT_PLATFORM=$(uname | awk '{print tolower($0)}') fi if [[ "$CURRENT_PLATFORM" == 'windows' ]]; then @@ -102,44 +104,34 @@ function heading() { echo echo - echo ----------------------------------------- - echo ${1} - echo ----------------------------------------- + echo "-----------------------------------------" + echo " ${1}" + echo "-----------------------------------------" } function build () { heading "Building ..." + dotnet msbuild -t:Build -p:PackageRuntime="${RUNTIME_ID}" -p:BUILDCONFIG="${BUILD_CONFIG}" -p:Version="${AGENT_VERSION}" || failed build - if [[ ("$CURRENT_PLATFORM" == "windows") ]]; then - dotnet msbuild //t:Build //p:PackageRuntime=${RUNTIME_ID} //p:BUILDCONFIG=${BUILD_CONFIG} //p:Version=${AGENT_VERSION} || failed build - else - dotnet msbuild /t:Build /p:PackageRuntime=${RUNTIME_ID} /p:BUILDCONFIG=${BUILD_CONFIG} /p:Version=${AGENT_VERSION} || failed build - fi - - mkdir -p ${LAYOUT_DIR}/bin/en-US - grep --invert-match '^ *"CLI-WIDTH-' ./Misc/layoutbin/en-US/strings.json > ${LAYOUT_DIR}/bin/en-US/strings.json + mkdir -p "${LAYOUT_DIR}/bin/en-US" + grep --invert-match '^ *"CLI-WIDTH-' ./Misc/layoutbin/en-US/strings.json > "${LAYOUT_DIR}/bin/en-US/strings.json" } function layout () { heading "Create layout ..." + dotnet msbuild -t:layout -p:PackageRuntime="${RUNTIME_ID}" -p:BUILDCONFIG="${BUILD_CONFIG}" -p:Version="${AGENT_VERSION}" || failed build - if [[ ("$CURRENT_PLATFORM" == "windows") ]]; then - dotnet msbuild //t:layout //p:PackageRuntime=${RUNTIME_ID} //p:BUILDCONFIG=${BUILD_CONFIG} //p:Version=${AGENT_VERSION} || failed build - else - dotnet msbuild /t:layout /p:PackageRuntime=${RUNTIME_ID} /p:BUILDCONFIG=${BUILD_CONFIG} /p:Version=${AGENT_VERSION} || failed build - fi - - mkdir -p ${LAYOUT_DIR}/bin/en-US - grep --invert-match '^ *"CLI-WIDTH-' ./Misc/layoutbin/en-US/strings.json > ${LAYOUT_DIR}/bin/en-US/strings.json + mkdir -p "${LAYOUT_DIR}/bin/en-US" + grep --invert-match '^ *"CLI-WIDTH-' ./Misc/layoutbin/en-US/strings.json > "${LAYOUT_DIR}/bin/en-US/strings.json" #change execution flag to allow running with sudo if [[ ("$CURRENT_PLATFORM" == "linux") || ("$CURRENT_PLATFORM" == "darwin") ]]; then - chmod +x ${LAYOUT_DIR}/bin/Agent.Listener - chmod +x ${LAYOUT_DIR}/bin/Agent.Worker - chmod +x ${LAYOUT_DIR}/bin/Agent.PluginHost - chmod +x ${LAYOUT_DIR}/bin/installdependencies.sh + chmod +x "${LAYOUT_DIR}/bin/Agent.Listener" + chmod +x "${LAYOUT_DIR}/bin/Agent.Worker" + chmod +x "${LAYOUT_DIR}/bin/Agent.PluginHost" + chmod +x "${LAYOUT_DIR}/bin/installdependencies.sh" fi heading "Setup externals folder for $RUNTIME_ID agent's layout" @@ -156,11 +148,7 @@ function runtest () export VSTS_AGENT_SRC_DIR=${SCRIPT_DIR} - if [[ ("$CURRENT_PLATFORM" == "windows") ]]; then - dotnet msbuild //t:test //p:PackageRuntime=${RUNTIME_ID} //p:BUILDCONFIG=${BUILD_CONFIG} //p:Version=${AGENT_VERSION} || failed "failed tests" - else - dotnet msbuild /t:test /p:PackageRuntime=${RUNTIME_ID} /p:BUILDCONFIG=${BUILD_CONFIG} /p:Version=${AGENT_VERSION} || failed "failed tests" - fi + dotnet msbuild -t:test -p:PackageRuntime="${RUNTIME_ID}" -p:BUILDCONFIG="${BUILD_CONFIG}" -p:Version="${AGENT_VERSION}" || failed "failed tests" } function package () @@ -169,21 +157,23 @@ function package () echo "You must build first. Expecting to find ${LAYOUT_DIR}/bin" fi - agent_ver=`${LAYOUT_DIR}/bin/Agent.Listener --version` || failed "version" + agent_ver=$("${LAYOUT_DIR}/bin/Agent.Listener" --version) || failed "version" agent_pkg_name="vsts-agent-${RUNTIME_ID}-${agent_ver}" heading "Packaging ${agent_pkg_name}" - rm -Rf ${LAYOUT_DIR}/_diag - find ${LAYOUT_DIR}/bin -type f -name '*.pdb' -delete - mkdir -p $PACKAGE_DIR - pushd $PACKAGE_DIR > /dev/null - rm -Rf * + rm -Rf "${LAYOUT_DIR:?}/_diag" + find "${LAYOUT_DIR}/bin" -type f -name '*.pdb' -delete + + mkdir -p "$PACKAGE_DIR" + rm -Rf "${PACKAGE_DIR:?}/*" + + pushd "$PACKAGE_DIR" > /dev/null if [[ ("$CURRENT_PLATFORM" == "linux") || ("$CURRENT_PLATFORM" == "darwin") ]]; then tar_name="${agent_pkg_name}.tar.gz" echo "Creating $tar_name in ${LAYOUT_DIR}" - tar -czf "${tar_name}" -C ${LAYOUT_DIR} . + tar -czf "${tar_name}" -C "${LAYOUT_DIR}" . elif [[ ("$CURRENT_PLATFORM" == "windows") ]]; then zip_name="${agent_pkg_name}.zip" echo "Convert ${LAYOUT_DIR} to Windows style path" @@ -206,19 +196,19 @@ if [[ (! -d "${DOTNETSDK_INSTALLDIR}") || (! -e "${DOTNETSDK_INSTALLDIR}/.${DOTN # \dotnet # \.1.0.x echo "Download dotnetsdk into ${DOTNETSDK_INSTALLDIR}" - rm -Rf ${DOTNETSDK_DIR} + rm -Rf "${DOTNETSDK_DIR}" # run dotnet-install.ps1 on windows, dotnet-install.sh on linux if [[ ("$CURRENT_PLATFORM" == "windows") ]]; then echo "Convert ${DOTNETSDK_INSTALLDIR} to Windows style path" sdkinstallwindow_path=${DOTNETSDK_INSTALLDIR:1} sdkinstallwindow_path=${sdkinstallwindow_path:0:1}:${sdkinstallwindow_path:1} - powershell -NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command "& \"./Misc/dotnet-install.ps1\" -Version ${DOTNETSDK_VERSION} -InstallDir \"${sdkinstallwindow_path}\" -NoPath; exit $LastExitCode;" || checkRC dotnet-install.ps1 + powershell -NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command "& \"./Misc/dotnet-install.ps1\" -Version ${DOTNETSDK_VERSION} -InstallDir \"${sdkinstallwindow_path}\" -NoPath; exit \$LastExitCode;" || checkRC dotnet-install.ps1 else - bash ./Misc/dotnet-install.sh --version ${DOTNETSDK_VERSION} --install-dir ${DOTNETSDK_INSTALLDIR} --no-path || checkRC dotnet-install.sh + bash ./Misc/dotnet-install.sh --version ${DOTNETSDK_VERSION} --install-dir "${DOTNETSDK_INSTALLDIR}" --no-path || checkRC dotnet-install.sh fi - echo "${DOTNETSDK_VERSION}" > ${DOTNETSDK_INSTALLDIR}/.${DOTNETSDK_VERSION} + echo "${DOTNETSDK_VERSION}" > "${DOTNETSDK_INSTALLDIR}/.${DOTNETSDK_VERSION}" fi echo "Prepend ${DOTNETSDK_INSTALLDIR} to %PATH%" @@ -231,8 +221,8 @@ heading "Pre-cache external resources for $RUNTIME_ID package ..." bash ./Misc/externals.sh $RUNTIME_ID "Pre-Cache" || checkRC "externals.sh Pre-Cache" if [[ "$CURRENT_PLATFORM" == 'windows' ]]; then - vswhere=`find $DOWNLOAD_DIR -name vswhere.exe | head -1` - vs_location=`$vswhere -latest -property installationPath` + vswhere=$(find "$DOWNLOAD_DIR" -name vswhere.exe | head -1) + vs_location=$("$vswhere" -latest -property installationPath) msbuild_location="$vs_location""\MSBuild\15.0\Bin\msbuild.exe" if [[ ! -e "${msbuild_location}" ]]; then