Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prepare-env for macOS #13

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands/prepare-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ steps:
PARAM_PROJECT_PATH: << parameters.project-path >>
SCRIPT_PREPARE_ENV_LINUX: << include(scripts/linux/prepare-env.sh) >>
SCRIPT_PREPARE_ENV_WINDOWS: << include(scripts/windows/prepare-env.sh) >>
SCRIPT_PREPARE_ENV_MACOS: << include(scripts/macos/prepare-env.sh) >>
command: << include(scripts/prepare-env.sh) >>
34 changes: 34 additions & 0 deletions src/executors/macos-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
description: |
Drop-in executor to build projects in a macOS runner.

parameters:
target_platform:
description: |
Pick the target platform for the GameCI image.
Available options can be found at https://game.ci/docs/docker/versions.
The 2018 release doesn't support the "linux-il2cpp" target platform.
type: enum
enum: [ "appletv", "base", "universal-windows-platform", "windows-il2cpp" ]
editor_version:
description: |
Pick the editor version for the GameCI image.
Make sure to pick the same major version as the Unity version you generated the license for.
Available options can be found at https://game.ci/docs/docker/versions.
type: string
resource_class:
description: |
Enter your Runner resource class.
For information on how to setup a Runner agent, see: https://circleci.com/docs/2.0/runner-overview/.
type: string
working_directory:
description: |
Enter the working directory of your runner agent.
type: string
default: "/var/opt/circleci/workdir"

machine: true
resource_class: << parameters.resource_class >>
working_directory: << parameters.working_directory >>
environment:
- UNITY_EDITOR_VERSION=<< parameters.editor_version >>
- UNITY_TARGET_PLATFORM=<< parameters.target_platform >>
30 changes: 30 additions & 0 deletions src/executors/macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
description: |
Drop-in executor to build project in macOS.

parameters:
editor_version:
description: |
Pick the Unity Editor version.
Make sure to pick the same major version as the Unity version you generated the license for.
Available options can be found at https://unity3d.com/get-unity/download/archive.
type: string
resource_class:
description: |
Resource class used for the executor.
If you are receiving a "CircleCI received exit code 137" from the build job, you may need to increase the resource class.
For details on resource class availability, see https://circleci.com/docs/using-macos#available-resource-classes.
type: enum
default: medium
enum: [ medium, macos.x86.medium.gen2, large, macos.x86.metal.gen1 ]
xcode_version:
description: |
Pick the Xcode version.
Available options can be found at https://circleci.com/docs/using-macos#supported-xcode-versions.
type: string
default: 13.4.1

macos:
xcode: << parameters.xcode_version >>
resource_class: << parameters.resource_class >>
environment:
- UNITY_EDITOR_VERSION=<< parameters.editor_version >>
147 changes: 147 additions & 0 deletions src/scripts/macos/prepare-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/bin/false
# shellcheck shell=bash
# shellcheck disable=SC2154

readonly unity_hub_path="/Applications/Unity Hub.app/Contents/MacOS/Unity Hub"
readonly unity_editor_path="/Applications/Unity/Hub/Editor/$UNITY_EDITOR_VERSION/Unity.app/Contents/MacOS/Unity"

printf '%s\n' "export UNITY_HUB_PATH=$unity_hub_path" >> "$BASH_ENV"
printf '%s\n' "export UNITY_EDITOR_PATH=$unity_editor_path" >> "$BASH_ENV"

check_and_install_unity_hub() {
if [ ! -f "$unity_hub_path" ]; then
printf '%s\n' "Could not find Unity Hub at \"$unity_hub_path\"."
printf '%s\n' "Installing it with brew..."

brew install --cask unity-hub

if [ -f "$unity_hub_path" ]; then
printf '%s\n' "Unity Hub installed successfully."

else
printf '%s\n' "Could not install the Unity Hub."
printf '%s\n' "Please try again or open an issue."
return 1
fi
fi

return 0
}

check_and_install_unity_editor() {
if [ ! -f "$unity_editor_path" ]; then
printf '%s\n' "Could not find the Unity Editor at \"$unity_editor_path\"."
printf '%s\n' "Installing it with the Unity Hub..."

if check_and_install_unity_hub; then

if ! command -v npm &> /dev/null; then
printf '%s\n' "npm is required to fetch the Unity Editor changeset."
printf '%s\n' "Please install it and try again."
return 1
fi

changeset="$(npx unity-changeset "$UNITY_EDITOR_VERSION")"

set -x
"$unity_hub_path" -- --headless install --version "$UNITY_EDITOR_VERSION" --changeset "$changeset" --module mac-il2cpp --childModules
set +x

if [ -f "$unity_editor_path" ]; then
printf '%s\n' "Unity Editor installed successfully."

else
printf '%s\n' "Could not install the Unity Editor."
printf '%s\n' "Please try again or open an issue."
return 1
fi
else
printf '%s\n' "Could not install the Editor because Unity Hub is not installed."
return 1
fi
fi

return 0
}

resolve_unity_serial() {
if [ -n "$unity_username" ] && [ -n "$unity_password" ]; then
# Serial provided.
if [ -n "$unity_serial" ]; then
printf '%s\n' "Detected Unity serial."
readonly resolved_unity_serial="$unity_serial"

# License provided.
elif [ -n "$unity_encoded_license" ]; then
printf '%s\n' "No serial detected. Extracting it from the encoded license."

if ! extract_serial_from_license; then
printf '%s\n' "Failed to parse the serial from the Unity license."
printf '%s\n' "Please try again or open an issue."
return 1

else
readonly resolved_unity_serial="$decoded_unity_serial"
fi

# Nothing provided.
else
printf '%s\n' "No serial or encoded license found."
printf '%s\n' "Please run the script again with a serial or encoded license file."
return 1
fi
fi

return 0
}

extract_serial_from_license() {
# Fix locale setting in PERL.
# https://stackoverflow.com/a/7413863
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

local unity_license
local developer_data
local encoded_serial

unity_license="$(base64 --decode <<< "$unity_encoded_license")"
developer_data="$(perl -nle 'print $& while m{<DeveloperData Value\="\K.*?(?="/>)}g' <<< "$unity_license")"
encoded_serial="$(cut -c 5- <<< "$developer_data")"

readonly decoded_unity_serial="$(base64 --decode <<< "$encoded_serial")"

if [ -n "$decoded_unity_serial" ]; then return 0; else return 1; fi
}

# Install the Editor if not already installed.
if ! check_and_install_unity_editor; then
printf '%s\n' "Something went wrong."
printf '%s\n' "Please try again or open an issue."
exit 1
fi

# Check if serial or encoded license was provided.
# If the latter, extract the serial from the license.
if ! resolve_unity_serial; then
printf '%s\n' "Failed to find the serial or parse it from the Unity license."
printf '%s\n' "Please try again or open an issue."
exit 1
fi

# If it doesn't exist, create folder for the Unity License File.
readonly unity_license_file_path="/Library/Application Support/Unity"
sudo mkdir -p "$unity_license_file_path"
sudo chmod -R 777 "$unity_license_file_path"

# Activate the Unity Editor.
set -x
"$unity_editor_path" \
-batchmode \
-quit \
-nographics \
-username "$unity_username" \
-password "$unity_password" \
-serial "$resolved_unity_serial" \
-logfile /dev/stdout
set +x
1 change: 1 addition & 0 deletions src/scripts/prepare-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if [ "$PLATFORM" = "linux" ]; then

elif [ "$PLATFORM" = "macos" ]; then
printf '%s\n' "Detected OS: macOS."
printf '%s\n' "$SCRIPT_PREPARE_ENV_MACOS" > "$base_dir/prepare-env.sh"

elif [ "$PLATFORM" = "windows" ]; then
printf '%s\n' "$SCRIPT_PREPARE_ENV_WINDOWS" > "$base_dir/prepare-env.sh"
Expand Down