forked from microsoft/vscode-dev-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homebrew-debian.sh
executable file
·143 lines (131 loc) · 4.7 KB
/
homebrew-debian.sh
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------
#
# ** This script is community supported **
# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/homebrew.md
# Maintainer: @andreiborisov
#
# Syntax: ./homebrew-debian.sh [non-root user] [add Homebrew binaries to PATH] [whether to use shallow clone] [where to install Homebrew]
USERNAME=${1:-"automatic"}
UPDATE_RC=${2:-"true"}
USE_SHALLOW_CLONE=${3:-"false"}
BREW_PREFIX=${4:-"/home/linuxbrew/.linuxbrew"}
set -e
if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi
# Determine the appropriate non-root user
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in ${POSSIBLE_USERS[@]}; do
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
USERNAME=${CURRENT_USER}
break
fi
done
if [ "${USERNAME}" = "" ]; then
USERNAME=root
fi
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
USERNAME=root
fi
function updaterc() {
if [ "${UPDATE_RC}" = "true" ]; then
echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
echo -e "$1" >> /etc/bash.bashrc
fi
if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
echo -e "$1" >> /etc/zsh/zshrc
fi
fi
}
function updatefishconfig() {
if [ "${UPDATE_RC}" = "true" ]; then
echo "Updating /etc/fish/config.fish..."
if [ -f "/etc/fish/config.fish" ]; then
echo -e "$1" >> /etc/fish/config.fish
fi
fi
}
export DEBIAN_FRONTEND=noninteractive
ARCHITECTURE="$(uname -m)"
if [ "${ARCHITECTURE}" != "amd64" ] && [ "${ARCHITECTURE}" != "x86_64" ]; then
echo "(!) Architecture $ARCHITECTURE unsupported"
exit 1
fi
# Install dependencies if missing
if ! dpkg -s \
bzip2 \
ca-certificates \
curl \
file \
fonts-dejavu-core \
g++ \
git \
less \
libz-dev \
locales \
make \
netbase \
openssh-client \
patch \
sudo \
tzdata \
uuid-runtime \
> /dev/null 2>&1; then
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
apt-get update
fi
apt-get -y install --no-install-recommends \
bzip2 \
ca-certificates \
curl \
file \
fonts-dejavu-core \
g++ \
git \
less \
libz-dev \
locales \
make \
netbase \
openssh-client \
patch \
sudo \
tzdata \
uuid-runtime
fi
# Install Homebrew
mkdir -p "${BREW_PREFIX}"
if [ "${USE_SHALLOW_CLONE}" = "false" ]; then
echo "Installing Homebrew..."
git clone https://github.com/Homebrew/brew "${BREW_PREFIX}/Homebrew"
mkdir -p "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew"
git clone https://github.com/Homebrew/homebrew-core "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew/homebrew-core"
else
echo "Installing Homebrew with shallow clone..."
git clone --depth 1 https://github.com/Homebrew/brew "${BREW_PREFIX}/Homebrew"
mkdir -p "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew"
git clone --depth 1 https://github.com/Homebrew/homebrew-core "${BREW_PREFIX}/Homebrew/Library/Taps/homebrew/homebrew-core"
# Disable automatic updates as they are not allowed with shallow clone installation
updaterc "export HOMEBREW_NO_AUTO_UPDATE=1"
updatefishconfig "set -gx HOMEBREW_NO_AUTO_UPDATE 1"
fi
"${BREW_PREFIX}/Homebrew/bin/brew" config
mkdir "${BREW_PREFIX}/bin"
ln -s "${BREW_PREFIX}/Homebrew/bin/brew" "${BREW_PREFIX}/bin"
chown -R ${USERNAME} "${BREW_PREFIX}"
# Add Homebrew binaries into PATH in bashrc/zshrc/config.fish files (unless disabled)
updaterc "$(cat << EOF
if [[ "\${PATH}" != *"${BREW_PREFIX}/bin"* ]]; then export PATH="${BREW_PREFIX}/bin:\${PATH}"; fi
if [[ "\${PATH}" != *"${BREW_PREFIX}/sbin"* ]]; then export PATH="${BREW_PREFIX}/sbin:\${PATH}"; fi
EOF
)"
updatefishconfig "fish_add_path ${BREW_PREFIX}/bin ${BREW_PREFIX}/sbin"
echo "Done!"