-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
postinstall
executable file
·107 lines (93 loc) · 3.5 KB
/
postinstall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# $1 Full path to the installer (unused)
# $2 Location of the Homebrew installation we may need to move into place
# $3 Target install location (unused)
# $4 System root directory (unused)
set -euo pipefail
# disable analytics while installing
export HOMEBREW_NO_ANALYTICS_THIS_RUN=1
export HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT=1
# verify the installation exists
# default to /opt/homebrew to make development/testing easier
homebrew_directory="${2:-/opt/homebrew}"
if [[ ! -d "${homebrew_directory:?}" ]]
then
echo "No directory at ${homebrew_directory}!" >&2
exit 1
fi
# add Git to path
export PATH="/Library/Developer/CommandLineTools/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}"
# helpers for setting/unsetting Git's safe directory setting
set_git_safe_directory() {
if git config --global --get-all safe.directory | grep -q "${1}"
then
return
fi
SET_GIT_SAFE_DIRECTORY="${1}"
git config --global --add safe.directory "${1}"
}
unset_git_safe_directory() {
if [[ -z "${SET_GIT_SAFE_DIRECTORY-}" ]]
then
return
fi
git config --global --unset safe.directory "${1}" || git config --global --unset-all safe.directory
if [[ ${SET_GIT_SAFE_DIRECTORY-} == "${1}" ]]
then
unset SET_GIT_SAFE_DIRECTORY
fi
}
# reset Git repository
cd "${homebrew_directory}"
set_git_safe_directory "${homebrew_directory}"
git reset --hard
git checkout --force master
git branch | grep -v '\*' | xargs -n 1 git branch --delete --force || true
unset_git_safe_directory "${homebrew_directory}"
# move to /usr/local if on x86_64
if [[ $(uname -m) == "x86_64" ]]
then
if [[ -f "/usr/local/bin/brew" && -d "/usr/local/Homebrew" ]]
then
cp -pRL "${homebrew_directory}/.git" "/usr/local/Homebrew/"
mv "${homebrew_directory}/cache_api" "/usr/local/Homebrew/"
set_git_safe_directory /usr/local/Homebrew
git -C /usr/local/Homebrew reset --hard
git -C /usr/local/Homebrew checkout --force master
unset_git_safe_directory /usr/local/Homebrew
else
mkdir -vp /usr/local/bin
mv "${homebrew_directory}" "/usr/local/Homebrew/"
# create symlink to /usr/local/bin/brew
ln -svf "../Homebrew/bin/brew" "/usr/local/bin/brew"
fi
rm -rf "${homebrew_directory}"
homebrew_directory="/usr/local/Homebrew"
cd /usr/local
fi
# create missing directories
mkdir -vp Cellar Frameworks etc include lib opt sbin share var/homebrew/linked
# optionally define an install user at /var/tmp/.homebrew_pkg_user.plist
homebrew_pkg_user_plist="/var/tmp/.homebrew_pkg_user.plist"
if [[ -f "${homebrew_pkg_user_plist}" ]] && [[ -n $(defaults read "${homebrew_pkg_user_plist}" HOMEBREW_PKG_USER) ]]
then
homebrew_pkg_user=$(defaults read /var/tmp/.homebrew_pkg_user HOMEBREW_PKG_USER)
# otherwise, get valid logged in user
else
homebrew_pkg_user=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }')
fi
# set permissions
if [[ "${homebrew_directory}" == "/usr/local/Homebrew" ]]
then
chown -R "${homebrew_pkg_user}:admin" Cellar Frameworks Homebrew bin etc include lib sbin share var
else
chown -R "${homebrew_pkg_user}:admin" .
fi
# move API cache to ~/Library/Caches/Homebrew
user_home_dir=$(dscl . read /Users/"${homebrew_pkg_user}" NFSHomeDirectory | awk '{ print $2 }')
user_cache_dir="${user_home_dir}/Library/Caches/Homebrew"
user_api_cache_dir="${user_cache_dir}/api"
mkdir -vp "${user_api_cache_dir}"
mv -v "${homebrew_directory}/cache_api/"* "${user_api_cache_dir}"
chown -R "${homebrew_pkg_user}:staff" "${user_cache_dir}"
rm -vrf "${homebrew_directory}/cache_api"