Skip to content

Commit

Permalink
Some fixes for end-to-end mac first time install, local, dockerless, …
Browse files Browse the repository at this point in the history
…single user:

Non-interactive brew install
Allow write permission for non-root mac user in sdkman and nvm install
dirs
Added brew rc snippet for bash and zsh
Source config vars after adjusted_id is set
Downgrade some code to support the default macos Bash version 3.2,
removed bash declare associative arrays and global attributes
Added a new check to set rc file ownership to macos user & group
  • Loading branch information
ridhwaans committed May 8, 2024
1 parent 29fa012 commit 2c7626b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/base/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ fi

start_time=$(date +%s)

source $(dirname $0)/modules/_config.sh
source $(dirname $0)/modules/_helper.sh

if [ $(uname) = Darwin ]; then
export ADJUSTED_ID="mac"
elif [ $(uname) = Linux ]; then
Expand All @@ -27,6 +24,9 @@ elif [ $(uname) = Linux ]; then
fi
fi

source $(dirname $0)/modules/_config.sh
source $(dirname $0)/modules/_helper.sh

# If in automatic mode, determine if a user already exists, if not use vscode
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
Expand Down
40 changes: 28 additions & 12 deletions src/base/modules/_helper.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
#!/usr/bin/env bash

updaterc() {
declare -A rc_paths=(
[vim]="$(eval echo "~$USERNAME")/.vimrc"
[bash]="$(eval echo "~$USERNAME")/.profile"
[zsh]="$(eval echo "~$USERNAME")/.zshrc"
)
rc_paths=("vim" "$(eval echo "~$USERNAME")/.vimrc"
"bash" "$(eval echo "~$USERNAME")/.profile"
"zsh" "$(eval echo "~$USERNAME")/.zshrc")

get_value_by_key() {
local key="$1"
local index
for ((index = 0; index < ${#rc_paths[@]}; index+=2)); do
if [[ "${rc_paths[index]}" == "$key" ]]; then
echo "${rc_paths[index+1]}"
return 0
fi
done
return 1
}

local rc_key=$1
local rc_content=$2

rc_path="${rc_paths[$rc_key]}"
rc_path=$(get_value_by_key "$rc_key")
rc_dir=$(dirname "$rc_path")
[ ! -d "$rc_dir" ] && mkdir -p "$rc_dir"
[ ! -f "$rc_path" ] && touch "$rc_path"
[ "$(stat -c '%U:%G' "$rc_path")" != "$USERNAME:$USERNAME" ] && chown $USERNAME:$USERNAME $rc_path

if [ "$ADJUSTED_ID" != "mac" ]; then
[ "$(stat -c '%U:%G' "$rc_path")" != "$USERNAME:$USERNAME" ] && chown $USERNAME:$USERNAME $rc_path
else
[ "$(stat -f '%Su:%Sg' "$rc_path")" != "$USERNAME:staff" ] && chown $USERNAME:staff $rc_path
fi
if [[ "$(cat $rc_path)" = *"$rc_content"* ]]; then
echo "Content already exists in $rc_path"
else
Expand All @@ -25,7 +39,7 @@ updaterc() {

run_brew_command_as_target_user() {
# workaround for issue running brew as root
sudo -u $USERNAME brew "$@"
eval "$(/opt/homebrew/bin/brew shellenv)" && sudo -u $USERNAME brew "$@"
}

conditional_grep() {
Expand All @@ -38,7 +52,7 @@ conditional_grep() {
}

# Figure out correct version of a three part version number is not passed
# Requires /bin/bash to run; ./ and /bin/zsh do not support indirect variable reference and extended pattern substitution syntax (//).
# Requires Bash. Zsh does not support indirect variable reference and extended pattern substitution syntax (//).
find_version_from_git_tags() {
local variable_name=$1
local requested_version=${!variable_name}
Expand All @@ -58,10 +72,12 @@ find_version_from_git_tags() {
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
local version_list="$(git ls-remote --tags ${repository} | conditional_grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
local latest_version="$(echo "${version_list}" | head -n 1)"
eval "${variable_name}='${latest_version}'"
else
set +e
declare -g ${variable_name}="$(echo "${version_list}" | conditional_grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
local matching_version="$(echo "${version_list}" | conditional_grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
eval "${variable_name}='${matching_version}'"
set -e
fi
fi
Expand Down
23 changes: 16 additions & 7 deletions src/base/modules/common-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ fi
install_mac_packages() {
if ! command -v xcode-select &>/dev/null; then
echo "Installing Command Line Tools..."
xcode-select --install
xcode-select --install && sudo xcodebuild -license accept
fi

if ! command -v brew &> /dev/null; then
echo "Installing Homebrew..."
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
yes '' | sudo -u $USERNAME bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

# Make sure we’re using the latest Homebrew
Expand All @@ -26,6 +26,7 @@ install_mac_packages() {
run_brew_command_as_target_user upgrade

packages=(
bash
dockutil
fontconfig
git
Expand All @@ -37,16 +38,13 @@ install_mac_packages() {
)
run_brew_command_as_target_user install "${packages[@]}"

# Install Caskroom
run_brew_command_as_target_user tap homebrew/cask-versions

apps=(
beekeeper-studio
docker
discord
dropbox
figma
iterm2-nightly
iterm2@nightly
kap
mounty
notion
Expand Down Expand Up @@ -106,7 +104,6 @@ install_mac_packages() {
install_debian_packages() {
# Ensure apt is in non-interactive to avoid prompts
export DEBIAN_FRONTEND=noninteractive

packages=(
acl
ca-certificates
Expand Down Expand Up @@ -260,6 +257,18 @@ if [ "${UPDATE_RC}" = "true" ]; then
updaterc "zsh" "${zsh_rc_snippet}"
fi

brew_rc_snippet=$(cat <<EOF
if [ \$(uname) = Darwin ]; then
eval \$(/opt/homebrew/bin/brew shellenv)
fi
EOF
)

if [ "${UPDATE_RC}" = "true" ]; then
updaterc "bash" "${brew_rc_snippet}"
updaterc "zsh" "${brew_rc_snippet}"
fi

vim_rc_snippet=$(cat <<EOF
let g:vim_plug_home="$VIMPLUG_PATH"
Expand Down
5 changes: 5 additions & 0 deletions src/base/modules/java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sdk_install() {
su ${USERNAME} -c "umask 0002 && . ${SDKMAN_DIR}/bin/sdkman-init.sh && sdk install ${install_type} ${requested_version} && sdk flush archives && sdk flush temp"
}


if [ "$ADJUSTED_ID" != "mac" ]; then
# Install sdkman if not installed
if [ ! -d "${SDKMAN_DIR}" ]; then
Expand All @@ -63,6 +64,10 @@ if [ "$ADJUSTED_ID" != "mac" ]; then
chown -R "root:sdkman" ${SDKMAN_DIR}
chmod -R g+rws "${SDKMAN_DIR}"
fi
else
# Install SDKMAN
curl -sSL "https://get.sdkman.io?rcupdate=false" | bash
chown -R $USERNAME ${SDKMAN_DIR}
fi

sdkman_rc_snippet=$(cat <<EOF
Expand Down
3 changes: 3 additions & 0 deletions src/base/modules/node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ if [ "$ADJUSTED_ID" != "mac" ]; then
[ ! -d ${NVM_DIR} ] && git clone https://github.com/nvm-sh/nvm.git ${NVM_DIR}
chown -R "root:nvm" "${NVM_DIR}"
chmod -R g+rws "${NVM_DIR}"
else
[ ! -d ${NVM_DIR} ] && git clone https://github.com/nvm-sh/nvm.git ${NVM_DIR}
chown -R $USERNAME ${NVM_DIR}
fi

# Adjust node version if required
Expand Down

0 comments on commit 2c7626b

Please sign in to comment.