From f7bcb26b10cb273994184b048b8502a4c11908a2 Mon Sep 17 00:00:00 2001 From: Mykola Grymalyuk Date: Tue, 14 May 2024 21:16:25 -0600 Subject: [PATCH] AutoPkg: Modularize pre/postinstall scripts --- CHANGELOG.md | 4 ++ ci_tooling/autopkg/postinstall.sh | 73 +++++++++++++++++++++++++----- ci_tooling/autopkg/preinstall.sh | 74 ++++++++++++++++++++++++++----- 3 files changed, 129 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5a75093e..5c343ad5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ - Resolve unpatching Nvidia Web Drivers failing to clean up `/Library/Extensions` - Implement preflight code signature checks for macOS installer creation - Ensures validity of `createinstallmedia` binary before execution +- Modularize AutoPkg's pre/postinstall scripts + - Adjusted to use functions for better readability + - Implements ZSH shebang + - Removes OS logging ## 1.4.3 - Update non-Metal Binaries for macOS Sonoma: diff --git a/ci_tooling/autopkg/postinstall.sh b/ci_tooling/autopkg/postinstall.sh index 80c4f12aa..5f2325f1c 100755 --- a/ci_tooling/autopkg/postinstall.sh +++ b/ci_tooling/autopkg/postinstall.sh @@ -1,13 +1,64 @@ -#!/bin/sh +#!/bin/zsh --no-rcs +# ------------------------------------------------------ +# AutoPkg Assets Postinstall Script +# ------------------------------------------------------ +# Create alias for app, start patching and reboot. +# ------------------------------------------------------ -# Create alias for OpenCore-Patcher.app -if [ ! -d "/Applications/OpenCore-Patcher.app" ]; then - ln -s "/Library/Application Support/Dortania/OpenCore-Patcher.app" "/Applications/OpenCore-Patcher.app" -fi -# Start root patching -app_path="/Library/Application Support/Dortania/OpenCore-Patcher.app/Contents/MacOS/OpenCore-Patcher" -args="--patch_sys_vol" -"$app_path" "$args" &> "/Users/Shared/.OCLP-AutoPatcher-Log-$(date +"%Y_%m_%d_%I_%M_%p").txt" -log show --last boot > "/Users/Shared/.OCLP-System-Log-$(date +"%Y_%m_%d_%I_%M_%p").txt" -reboot \ No newline at end of file +# MARK: Variables +# --------------------------- + +mainAppPath="/Library/Application Support/Dortania/OpenCore-Patcher.app" +shimAppPath="/Applications/OpenCore-Patcher.app" +executablePath="$mainAppPath/Contents/MacOS/OpenCore-Patcher" + + +# MARK: Functions +# --------------------------- + +function _createAlias() { + local mainPath=$1 + local aliasPath=$2 + + # Check if alias path exists + if [[ -e $aliasPath ]]; then + # Check if alias path is a symbolic link + if [[ -L $aliasPath ]]; then + /bin/rm -f $aliasPath + else + /bin/rm -rf $aliasPath + fi + fi + + # Create symbolic link + /bin/ln -s $mainPath $aliasPath +} + +function _startPatching() { + local executable=$1 + local logPath=$(_logFile) + + # Start patching + "$executable" "--patch_sys_vol" &> $logPath +} + +function _logFile() { + echo "/Users/Shared/.OCLP-AutoPatcher-Log-$(/bin/date +"%Y_%m_%d_%I_%M_%p").txt" +} + +function _reboot() { + /sbin/reboot +} + +function _main() { + _createAlias "$mainAppPath" "$shimAppPath" + _startPatching "$executablePath" + _reboot +} + + +# MARK: Main +# --------------------------- + +_main \ No newline at end of file diff --git a/ci_tooling/autopkg/preinstall.sh b/ci_tooling/autopkg/preinstall.sh index bdb22cff0..ed2e77f62 100755 --- a/ci_tooling/autopkg/preinstall.sh +++ b/ci_tooling/autopkg/preinstall.sh @@ -1,12 +1,64 @@ -#!/bin/sh -app_path="/Library/Application Support/Dortania/OpenCore-Patcher.app" -launch_agent_path="/Library/LaunchAgents/com.dortania.opencore-legacy-patcher.auto-patch.plist" -if [ -d "$app_path" ]; then - echo "Found OpenCore-Patcher.app, removing..." - rm -rf "$app_path" -fi +#!/bin/zsh --no-rcs +# ------------------------------------------------------ +# AutoPkg Assets Preinstall Script +# ------------------------------------------------------ +# Remove old files, and prepare directories. +# ------------------------------------------------------ -if [ -f "$launch_agent_path" ]; then - echo "Found launch agent, removing..." - rm -f "$launch_agent_path" -fi + +# MARK: Variables +# --------------------------- + +filesToRemove=( + "/Applications/OpenCore-Patcher.app" + "/Library/Application Support/Dortania/Update.plist" + "/Library/Application Support/Dortania/OpenCore-Patcher.app" + "/Library/LaunchAgents/com.dortania.opencore-legacy-patcher.auto-patch.plist" +) + + +# MARK: Functions +# --------------------------- + +function _removeFile() { + local currentFile=$1 + + if [[ ! -e $currentFile ]]; then + # Check if file is a symbolic link + if [[ -L $currentFile ]]; then + /bin/rm -f $currentFile + fi + return + fi + + # Check if file is a directory + if [[ -d $currentFile ]]; then + /bin/rm -rf $currentFile + else + /bin/rm -f $currentFile + fi +} + +function _createParentDirectory() { + local currentFile=$1 + + local parentDirectory=$(/usr/bin/dirname $currentFile) + + # Check if parent directory exists + if [[ ! -d $parentDirectory ]]; then + /bin/mkdir -p $parentDirectory + fi +} + +function _main() { + for file in $filesToRemove; do + _removeFile $file + _createParentDirectory $file + done +} + + +# MARK: Main +# --------------------------- + +_main \ No newline at end of file