#!/usr/bin/env bash # Make sure the current directory is the location of this script to simplify matters cd "$(dirname $(readlink -f $0))"; ################ ### Settings ### ################ # The name of this project project_name="RhinoReminds"; # The path to the lantern build engine git submodule lantern_path="./lantern-build-engine"; ### # Custom Settings ### # Put any custom settings here. build_output_folder="./dist"; deploy_ssh_user="ci"; deploy_ssh_host="starbeamrainbowlabs.com"; deploy_ssh_port="2403"; deploy_target_dir="RhinoReminds"; ############################################################################### # Check out the lantern git submodule if needed if [ ! -f "${lantern_path}/lantern.sh" ]; then echo "Checking out lantern"; git submodule update --init "${lantern_path}"; fi source "${lantern_path}/lantern.sh"; if [[ "$#" -lt 1 ]]; then echo -e "${FBLE}${project_name}${RS} build script"; echo -e " by Starbeamrainbowlabs"; echo -e "${LC}Powered by the lantern build engine, v${version}${RS}"; echo -e ""; echo -e "${CSECTION}Usage${RS}"; echo -e " ./build ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ..."; echo -e ""; echo -e "${CSECTION}Available actions${RS}"; echo -e " ${CACTION}setup${RS} - Perform initial setup"; echo -e " ${CACTION}build${RS} - Restore nuget packages & compile project"; echo -e " ${CACTION}ci${RS} - Perform CI tasks"; echo -e " ${CACTION}package${RS} - Pack latest build for package managers"; echo -e " ${CACTION}archive${RS} - Construct binary archives"; echo -e ""; exit 1; fi ############################################################################### task_setup() { task_begin "Checking environment"; check_command git true; check_command mono true; check_command msbuild true; check_command nuget true; check_command mktemp true; task_end 0; } task_build() { task_begin "Restoring nuget packages"; nuget restore; task_end $?; task_begin "Building"; debug_logfile="$(mktemp --suffix ".rhinoreminds.debug.log")"; release_logfile="$(mktemp --suffix ".rhinoreminds.release.log")"; ( execute msbuild /consoleloggerparameters:ForceConsoleColor >"${debug_logfile}" 2>&1 release_exit_code=$!; ) & ( execute msbuild /consoleloggerparameters:ForceConsoleColor /p:Configuration=Release >"${release_logfile}" 2>&1 debug_exit_code=$!; ) & wait # FUTURE: Grab the stage_begin "Debug compilation output"; cat "${debug_logfile}"; stage_end "${release_exit_code}"; stage_begin "Release compilation output"; cat "${release_logfile}"; stage_end "${debug_exit_code}"; echo ""; rm "${debug_logfile}" "${release_logfile}"; task_end $?; } task_archive() { task_begin "Deleting extra files"; execute find RhinoReminds/bin -iname "*.xml" -delete; task_end $?; task_begin "Setting permissions"; find RhinoReminds/bin -type f -print0 | xargs -0 -P4 chmod -c -x; find RhinoReminds/bin -type f -iname "*.exe" -print0 | xargs -0 -P4 chmod -c +x; task_end $?; task_begin "Creating Archives"; execute cp -ral RhinoReminds/bin/Debug RhinoReminds-Debug; execute cp -ral RhinoReminds/bin/Release RhinoReminds-Release; execute cp -al README.md RhinoReminds-Debug/README.md; execute cp -al README.md RhinoReminds-Release/README.md; execute tar -caf RhinoReminds-Debug.tar.gz RhinoReminds-Debug; execute tar -caf RhinoReminds-Release.tar.gz RhinoReminds-Release; execute rm -r RhinoReminds-{Debug,Release}; task_end $?; if [ "${ARCHIVE}" != "" ]; then task_begin "CI environment detected - moving archives to specified CI archive directory"; execute mv RhinoReminds-{Debug,Release}.tar.gz "${ARCHIVE}"; task_end $?; fi } task_package() { task_begin "Preparing for packaging"; check_command fpm true; package_dir="$(mktemp -d --suffix "-rhinoreminds-ci-fpm-package")"; echo "Error: Not implemented yet :-/"; exit 1; task_end $?; } task_deploy() { stage_begin "Deploying to ${deploy_ssh_host}...."; if [ "${SSH_KEY_PATH}" == "" ]; then stage_end 1 "Error: Can't find the SSH key as the environment variable SSH_KEY_PATH isn't set."; fi task_begin "Preparing upload"; subtask_begin "Creating temporary directory"; temp_dir="$(mktemp -d --suffix "-rhinoreminds-upload")"; subtask_end $? "Error: Failed to create temporary directory"; subtask_begin "Unpacking release files"; execute tar -xf "${ARCHIVE}/RhinoReminds-Release.tar.gz" -C "${temp_dir}"; subtask_end $? "Failed to unpack release files"; # Define the directory whose contents we want to upload source_upload_dir="${temp_dir}/RhinoReminds-Release"; task_end $?; task_begin "Uploading release"; # TODO: If we experience issues, we need to somehow figure out how to recursively delete the contents of the directory before uploading. We may have to install lftp and use that instead sftp -i "${SSH_KEY_PATH}" -P "${deploy_ssh_port}" -o PasswordAuthentication=no "${deploy_ssh_user}@${deploy_ssh_host}" << SFTPCOMMANDS rm ${deploy_target_dir}/* put -r ${source_upload_dir}/* ${deploy_target_dir} bye SFTPCOMMANDS task_end $? "Failed to upload release"; task_begin "Cleaning up"; execute rm -r "${temp_dir}"; task_end $?; stage_end $? "Failed to deploy to ${deploy_ssh_host}."; } task_ci() { tasks_run setup; task_begin "Environment Information"; execute uname -a; execute git --version; execute mono --version; execute nuget help | head -n1; task_end 0; # FUTURE: We'll (eventually) want to call the package task here too tasks_run build archive; if [[ "$(git rev-parse --abbrev-ref HEAD)" == "master" ]]; then tasks_run deploy; else echo "Not deploying, as this build wasn't on the master branch."; fi } ############################################################################### tasks_run $@;