Skip to content

Commit

Permalink
Move installer scripts within Nudge.app
Browse files Browse the repository at this point in the history
  • Loading branch information
erikng committed Mar 4, 2024
1 parent 5a06353 commit 90dcb61
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 120 deletions.
24 changes: 24 additions & 0 deletions Nudge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
63D7D0F625C9E9A500236281 /* NudgeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63D7D0F525C9E9A500236281 /* NudgeTests.swift */; };
63D7D10125C9E9A500236281 /* NudgeUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63D7D10025C9E9A500236281 /* NudgeUITests.swift */; };
63D7D12725C9F1EE00236281 /* StandardMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63D7D12625C9F1EE00236281 /* StandardMode.swift */; };
63FD280F2B96846A004E0108 /* postinstall-launchagent in Resources */ = {isa = PBXBuildFile; fileRef = 63FD280B2B9682CD004E0108 /* postinstall-launchagent */; };
63FD28102B96846E004E0108 /* postinstall-logger in Resources */ = {isa = PBXBuildFile; fileRef = 63FD280C2B9682F9004E0108 /* postinstall-logger */; };
63FD28112B968472004E0108 /* postinstall-suite in Resources */ = {isa = PBXBuildFile; fileRef = 63FD280D2B968309004E0108 /* postinstall-suite */; };
63FD28122B968475004E0108 /* preinstall-app in Resources */ = {isa = PBXBuildFile; fileRef = 63FD280E2B96831F004E0108 /* preinstall-app */; };
73CC1D7829B81EE500FBF8E2 /* com.github.macadmins.Nudge.SMAppService.plist in Resources */ = {isa = PBXBuildFile; fileRef = 73CC1D7729B81EE500FBF8E2 /* com.github.macadmins.Nudge.SMAppService.plist */; };
73CC1D7A29B81F0600FBF8E2 /* com.github.macadmins.Nudge.SMAppService.plist in CopyFiles */ = {isa = PBXBuildFile; fileRef = 73CC1D7729B81EE500FBF8E2 /* com.github.macadmins.Nudge.SMAppService.plist */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -118,6 +122,10 @@
63D7D10025C9E9A500236281 /* NudgeUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NudgeUITests.swift; sourceTree = "<group>"; };
63D7D10225C9E9A500236281 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
63D7D12625C9F1EE00236281 /* StandardMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StandardMode.swift; sourceTree = "<group>"; };
63FD280B2B9682CD004E0108 /* postinstall-launchagent */ = {isa = PBXFileReference; lastKnownFileType = text; path = "postinstall-launchagent"; sourceTree = "<group>"; };
63FD280C2B9682F9004E0108 /* postinstall-logger */ = {isa = PBXFileReference; lastKnownFileType = text; path = "postinstall-logger"; sourceTree = "<group>"; };
63FD280D2B968309004E0108 /* postinstall-suite */ = {isa = PBXFileReference; lastKnownFileType = text; path = "postinstall-suite"; sourceTree = "<group>"; };
63FD280E2B96831F004E0108 /* preinstall-app */ = {isa = PBXFileReference; lastKnownFileType = text; path = "preinstall-app"; sourceTree = "<group>"; };
73CC1D7729B81EE500FBF8E2 /* com.github.macadmins.Nudge.SMAppService.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = com.github.macadmins.Nudge.SMAppService.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -255,6 +263,7 @@
639B6B5425DF374600E38EC1 /* UI */,
639B6B3925DF1FEB00E38EC1 /* Preferences */,
639B6B4725DF218900E38EC1 /* Utilities */,
63FD280A2B968290004E0108 /* Scripts */,
63D7D0E625C9E9A500236281 /* Assets.xcassets */,
63D7D0EB25C9E9A500236281 /* Info.plist */,
63D7D0EC25C9E9A500236281 /* Nudge.entitlements */,
Expand Down Expand Up @@ -291,6 +300,17 @@
path = NudgeUITests;
sourceTree = "<group>";
};
63FD280A2B968290004E0108 /* Scripts */ = {
isa = PBXGroup;
children = (
63FD280B2B9682CD004E0108 /* postinstall-launchagent */,
63FD280C2B9682F9004E0108 /* postinstall-logger */,
63FD280D2B968309004E0108 /* postinstall-suite */,
63FD280E2B96831F004E0108 /* preinstall-app */,
);
path = Scripts;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -412,13 +432,17 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
63FD280F2B96846A004E0108 /* postinstall-launchagent in Resources */,
63D7D0EA25C9E9A500236281 /* Preview Assets.xcassets in Resources */,
63FD28122B968475004E0108 /* preinstall-app in Resources */,
63D7D0E725C9E9A500236281 /* Assets.xcassets in Resources */,
639B6B0F25DC9ED300E38EC1 /* com.github.macadmins.Nudge.mobileconfig in Resources */,
73CC1D7829B81EE500FBF8E2 /* com.github.macadmins.Nudge.SMAppService.plist in Resources */,
63C6A08E2833FB6500D5264A /* com.github.macadmins.Nudge.tester.json in Resources */,
035C2AEC25D8ABC400429458 /* com.github.macadmins.Nudge.json in Resources */,
63FD28112B968472004E0108 /* postinstall-suite in Resources */,
6316F0E72832CA0700E1354D /* Schema in Resources */,
63FD28102B96846E004E0108 /* postinstall-logger in Resources */,
637CEBC12A30C9E700EFA3E9 /* Localizable.xcstrings in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
51 changes: 51 additions & 0 deletions Nudge/Scripts/postinstall-launchagent
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Copyright 2021-Present Erik Gomez.
#
# 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
#
# https://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 you change your agent file name, update the following line
launch_agent_plist_name='com.github.macadmins.Nudge.plist'

# Base paths
launch_agent_base_path='Library/LaunchAgents/'

# Load agent if installing to a running system
if [[ $3 == "/" ]] ; then
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_agent_base_path}${launch_agent_plist_name}" ]; then
echo "LaunchAgent missing, exiting"
exit 1
fi

# Current console user information
console_user=$(/usr/bin/stat -f "%Su" /dev/console)
console_user_uid=$(/usr/bin/id -u "$console_user")

# Only enable the LaunchAgent if there is a user logged in, otherwise rely on built in LaunchAgent behavior
if [[ -z "$console_user" ]]; then
echo "Did not detect user"
elif [[ "$console_user" == "loginwindow" ]]; then
echo "Detected Loginwindow Environment"
elif [[ "$console_user" == "_mbsetupuser" ]]; then
echo "Detect SetupAssistant Environment"
elif [[ "$console_user" == "root" ]]; then
echo "Detect root as currently logged-in user"
else
# Unload the agent so it can be triggered on re-install
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl unload -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
# Kill Nudge just in case (say someone manually opens it and not launched via launchagent
/usr/bin/killall Nudge
# Load the launch agent
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl load -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
fi
fi
34 changes: 34 additions & 0 deletions Nudge/Scripts/postinstall-logger
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Copyright 2021-Present Erik Gomez.
#
# 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
#
# https://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 you change your agent file name, update the following line
launch_daemon_plist_name='com.github.macadmins.Nudge.logger.plist'

# Base paths
launch_daemon_base_path='Library/LaunchDaemons/'

# Load agent if installing to a running system
if [[ $3 == "/" ]] ; then
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_daemon_base_path}${launch_daemon_plist_name}" ]; then
echo "LaunchDaemon missing, exiting"
exit 1
fi

# Unload the agent so it can be triggered on re-install
/bin/launchctl unload -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
# Load the launch agent
/bin/launchctl load -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
fi
66 changes: 66 additions & 0 deletions Nudge/Scripts/postinstall-suite
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# Copyright 2021-Present Erik Gomez.
#
# 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
#
# https://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 you change your agent/daemon files name, update the following lines
launch_agent_plist_name='com.github.macadmins.Nudge.plist'
launch_daemon_plist_name='com.github.macadmins.Nudge.logger.plist'

# Base paths
launch_agent_base_path='Library/LaunchAgents/'
launch_daemon_base_path='Library/LaunchDaemons/'

# Load agent and daemon if installing to a running system
if [[ $3 == "/" ]] ; then
## Agent
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_agent_base_path}${launch_agent_plist_name}" ]; then
echo "LaunchAgent missing, exiting"
exit 1
fi

# Current console user information
console_user=$(/usr/bin/stat -f "%Su" /dev/console)
console_user_uid=$(/usr/bin/id -u "$console_user")

# Only enable the LaunchAgent if there is a user logged in, otherwise rely on built in LaunchAgent behavior
if [[ -z "$console_user" ]]; then
echo "Did not detect user"
elif [[ "$console_user" == "loginwindow" ]]; then
echo "Detected Loginwindow Environment"
elif [[ "$console_user" == "_mbsetupuser" ]]; then
echo "Detect SetupAssistant Environment"
elif [[ "$console_user" == "root" ]]; then
echo "Detect root as currently logged-in user"
else
# Unload the agent so it can be triggered on re-install
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl unload -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
# Kill Nudge just in case (say someone manually opens it and not launched via launchagent
/usr/bin/killall Nudge
# Load the launch agent
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl load -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
fi

## Daemon Logger
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_daemon_base_path}${launch_daemon_plist_name}" ]; then
echo "LaunchDaemon missing, exiting"
exit 1
fi

# Unload the agent so it can be triggered on re-install
/bin/launchctl unload -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
# Load the launch agent
/bin/launchctl load -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
fi
34 changes: 34 additions & 0 deletions Nudge/Scripts/preinstall-app
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Copyright 2021-Present Erik Gomez.
#
# 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
#
# https://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.

# Only run if on a running system
if [[ $3 == "/" ]] ; then
# Current console user information
console_user=$(/usr/bin/stat -f "%Su" /dev/console)

# Only run if there is a user logged in, otherwise do nothing
if [[ -z "$console_user" ]]; then
echo "Did not detect user"
elif [[ "$console_user" == "loginwindow" ]]; then
echo "Detected Loginwindow Environment"
elif [[ "$console_user" == "_mbsetupuser" ]]; then
echo "Detect SetupAssistant Environment"
elif [[ "$console_user" == "root" ]]; then
echo "Detect root as currently logged-in user"
else
# Kill Nudge is running
/usr/bin/pgrep -i Nudge | /usr/bin/xargs kill
fi
fi
38 changes: 3 additions & 35 deletions build_assets/postinstall-launchagent
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/zsh
#!/bin/sh
#
# Copyright 2021-Present Erik Gomez.
#
Expand All @@ -14,39 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# If you change your agent file name, update the following line
launch_agent_plist_name='com.github.macadmins.Nudge.plist'

# Base paths
launch_agent_base_path='Library/LaunchAgents/'

# Load agent if installing to a running system
# Only run if on a running system
if [[ $3 == "/" ]] ; then
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_agent_base_path}${launch_agent_plist_name}" ]; then
echo "LaunchAgent missing, exiting"
exit 1
fi

# Current console user information
console_user=$(/usr/bin/stat -f "%Su" /dev/console)
console_user_uid=$(/usr/bin/id -u "$console_user")

# Only enable the LaunchAgent if there is a user logged in, otherwise rely on built in LaunchAgent behavior
if [[ -z "$console_user" ]]; then
echo "Did not detect user"
elif [[ "$console_user" == "loginwindow" ]]; then
echo "Detected Loginwindow Environment"
elif [[ "$console_user" == "_mbsetupuser" ]]; then
echo "Detect SetupAssistant Environment"
elif [[ "$console_user" == "root" ]]; then
echo "Detect root as currently logged-in user"
else
# Unload the agent so it can be triggered on re-install
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl unload -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
# Kill Nudge just in case (say someone manually opens it and not launched via launchagent
/usr/bin/killall Nudge
# Load the launch agent
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl load -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
fi
/bin/zsh --no-rcs -c '/Applications/Utilities/Nudge.app/Contents/Resources/postinstall-launchagent'
fi
21 changes: 3 additions & 18 deletions build_assets/postinstall-logger
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/zsh
#!/bin/sh
#
# Copyright 2021-Present Erik Gomez.
#
Expand All @@ -14,22 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# If you change your agent file name, update the following line
launch_daemon_plist_name='com.github.macadmins.Nudge.logger.plist'

# Base paths
launch_daemon_base_path='Library/LaunchDaemons/'

# Load agent if installing to a running system
# Only run if on a running system
if [[ $3 == "/" ]] ; then
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_daemon_base_path}${launch_daemon_plist_name}" ]; then
echo "LaunchDaemon missing, exiting"
exit 1
fi

# Unload the agent so it can be triggered on re-install
/bin/launchctl unload -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
# Load the launch agent
/bin/launchctl load -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
/bin/zsh --no-rcs -c '/Applications/Utilities/Nudge.app/Contents/Resources/postinstall-logger'
fi
53 changes: 3 additions & 50 deletions build_assets/postinstall-suite
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/zsh
#!/bin/sh
#
# Copyright 2021-Present Erik Gomez.
#
Expand All @@ -14,54 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# If you change your agent/daemon files name, update the following lines
launch_agent_plist_name='com.github.macadmins.Nudge.plist'
launch_daemon_plist_name='com.github.macadmins.Nudge.logger.plist'

# Base paths
launch_agent_base_path='Library/LaunchAgents/'
launch_daemon_base_path='Library/LaunchDaemons/'

# Load agent and daemon if installing to a running system
# Only run if on a running system
if [[ $3 == "/" ]] ; then
## Agent
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_agent_base_path}${launch_agent_plist_name}" ]; then
echo "LaunchAgent missing, exiting"
exit 1
fi

# Current console user information
console_user=$(/usr/bin/stat -f "%Su" /dev/console)
console_user_uid=$(/usr/bin/id -u "$console_user")

# Only enable the LaunchAgent if there is a user logged in, otherwise rely on built in LaunchAgent behavior
if [[ -z "$console_user" ]]; then
echo "Did not detect user"
elif [[ "$console_user" == "loginwindow" ]]; then
echo "Detected Loginwindow Environment"
elif [[ "$console_user" == "_mbsetupuser" ]]; then
echo "Detect SetupAssistant Environment"
elif [[ "$console_user" == "root" ]]; then
echo "Detect root as currently logged-in user"
else
# Unload the agent so it can be triggered on re-install
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl unload -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
# Kill Nudge just in case (say someone manually opens it and not launched via launchagent
/usr/bin/killall Nudge
# Load the launch agent
/bin/launchctl asuser "${console_user_uid}" /bin/launchctl load -w "$3${launch_agent_base_path}${launch_agent_plist_name}"
fi

## Daemon Logger
# Fail the install if the admin forgets to change their paths and they don't exist.
if [ ! -e "$3/${launch_daemon_base_path}${launch_daemon_plist_name}" ]; then
echo "LaunchDaemon missing, exiting"
exit 1
fi

# Unload the agent so it can be triggered on re-install
/bin/launchctl unload -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
# Load the launch agent
/bin/launchctl load -w "$3${launch_daemon_base_path}${launch_daemon_plist_name}"
/bin/zsh --no-rcs -c '/Applications/Utilities/Nudge.app/Contents/Resources/postinstall-suite'
fi
Loading

0 comments on commit 90dcb61

Please sign in to comment.