Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
martimlobao committed Nov 6, 2024
1 parent f956fa3 commit 8aa9062
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 101 deletions.
12 changes: 6 additions & 6 deletions bash_traceback.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash

# Bash traceback (from https://gist.github.com/Asher256/4c68119705ffa11adb7446f297a7beae)
set -o errexit # stop the script each time a command fails
set -o nounset # stop if you attempt to use an undef variable
set -o errexit # stop the script each time a command fails
set -o nounset # stop if you attempt to use an undef variable

function bash_traceback() {
local lasterr="$?"
Expand All @@ -13,10 +13,10 @@ function bash_traceback() {
if [ ${#FUNCNAME[@]} -gt 2 ]; then
# Print out the stack trace described by $function_stack
echo "Traceback of ${BASH_SOURCE[1]} (most recent call last):" >&2
for ((i=0; i < ${#FUNCNAME[@]} - 1; i++)); do
local funcname="${FUNCNAME[$i]}"
[ "$i" -eq "0" ] && funcname=$bash_command
echo -e " ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]}\\t$funcname" >&2
for ((i = 0; i < ${#FUNCNAME[@]} - 1; i++)); do
local funcname="${FUNCNAME[$i]}"
[ "$i" -eq "0" ] && funcname=$bash_command
echo -e " ${BASH_SOURCE[i + 1]}:${BASH_LINENO[$i]}\\t$funcname" >&2
done
fi
echo "Exiting with status ${code}" >&2
Expand Down
30 changes: 15 additions & 15 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

{ # Prevent script from running if partially downloaded

set -euo pipefail
set -euo pipefail

DOTPATH=$HOME/.dotfiles
DOTPATH=$HOME/.dotfiles

echo -e "\033[1;34m🥾 Bootstrapping dotfiles\033[0m"
echo -e "\033[1;34m🥾 Bootstrapping dotfiles\033[0m"

if [ ! -d "$DOTPATH" ]; then
git clone https://github.com/martimlobao/dotfiles.git "$DOTPATH"
echo -e "\033[1;32m✅ Cloned $DOTPATH\033[0m"
else
echo -e "\033[1;34m✅ Dotfiles already downloaded to $DOTPATH\033[0m"
fi
if [ ! -d "$DOTPATH" ]; then
git clone https://github.com/martimlobao/dotfiles.git "$DOTPATH"
echo -e "\033[1;32m✅ Cloned $DOTPATH\033[0m"
else
echo -e "\033[1;34m✅ Dotfiles already downloaded to $DOTPATH\033[0m"
fi

cd "$DOTPATH"
cd "$DOTPATH"

if [[ "${1:-}" == "--yes" ]] || [[ "${1:-}" == "-y" ]]; then
./run.sh -y
else
./run.sh
fi
if [[ ${1:-} == "--yes" ]] || [[ ${1:-} == "-y" ]]; then
./run.sh -y
else
./run.sh
fi

} # Prevent script from running if partially downloaded
10 changes: 4 additions & 6 deletions dock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,19 @@ function add_folder_to_dock {
sortby="1"
displayas="0"
viewcontentas="0"
while [[ "$#" -gt 0 ]]
do
while [[ $# -gt 0 ]]; do
case $1 in
-s|--sortby)
-s | --sortby)
if [[ $2 =~ ^[1-5]$ ]]; then
sortby="${2}"
fi
;;
-d|--displayas)
-d | --displayas)
if [[ $2 =~ ^[0-1]$ ]]; then
displayas="${2}"
fi
;;
-v|--viewcontentas)
-v | --viewcontentas)
if [[ $2 =~ ^[0-3]$ ]]; then
viewcontentas="${2}"
fi
Expand Down Expand Up @@ -113,7 +112,6 @@ function reset_launchpad {
defaults write com.apple.dock ResetLaunchPad -bool true
}


###############################################################################
# CONFIGURE MACOS DOCK #
###############################################################################
Expand Down
17 changes: 8 additions & 9 deletions dotsync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,36 @@ source "$root/bash_traceback.sh"
# UPDATE DOTFILES #
###############################################################################


function dotlink() {
find "linkme" -type d -mindepth 1 | sed "s|^linkme/||" | \
find "linkme" -type d -mindepth 1 | sed "s|^linkme/||" |
while read -r dir; do mkdir -p "$HOME/$dir"; done
find "linkme" -type f -not -name '.DS_Store' | sed "s|^linkme/||" | \
find "linkme" -type f -not -name '.DS_Store' | sed "s|^linkme/||" |
while read -r file; do
echo -e "\033[1;32m🔗 Linked $(pwd)/linkme/$file -> $HOME/$file\033[0m"
ln -fvns "$(pwd)/linkme/$file" "$HOME/$file" &> /dev/null;
ln -fvns "$(pwd)/linkme/$file" "$HOME/$file" &>/dev/null
done
}

function dotunlink() {
rsync -av --exclude='.DS_Store' linkme/ "$HOME" | \
grep -v "building file list ... done" | \
rsync -av --exclude='.DS_Store' linkme/ "$HOME" |
grep -v "building file list ... done" |
awk '/^$/ { exit } !/\/$/ { printf "\033[1;32m🔙 Restored %s\033[0m\n", $0; }'
}

# Copy all files from copyme/ to $HOME
if [ "${1:-}" == "unlink" ]; then
echo -e "\033[1;34m📋 Restoring dotfiles...\033[0m"
dotunlink;
dotunlink
else
echo -e "\033[1;34m🔗 Linking dotfiles...\033[0m"
if [[ "${1:-}" != "-y" ]] && [[ "${1:-}" != "--yes" ]]; then
if [[ ${1:-} != "-y" ]] && [[ ${1:-} != "--yes" ]]; then
read -rp $'\e[1;31mOverwrite existing dotfiles with symlinks to stored dotfiles? (y/n)\e[0m ' LINK
else
LINK="y"
fi

if [[ $LINK =~ ^[Yy]$ ]]; then
dotlink;
dotlink
fi
fi

Expand Down
92 changes: 46 additions & 46 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ echo -e "📲 \033[1;34mInstalling apps and packages...\033[0m"

# Check if the first argument is -y or --yes
auto_yes=false
if [[ "${1:-}" == "-y" ]] || [[ "${1:-}" == "--yes" ]]; then
if [[ ${1:-} == "-y" ]] || [[ ${1:-} == "--yes" ]]; then
auto_yes=true
fi

# Ensure yq is installed to parse the apps.toml file
if ! command -v yq &> /dev/null; then
if ! command -v yq &>/dev/null; then
echo -e "⬇️ \033[1;34mInstalling yq to parse apps.toml...\033[0m"
brew install yq
fi
Expand Down Expand Up @@ -57,45 +57,45 @@ in_array() {
shift
local item
for item; do
[[ "$item" == "$needle" ]] && return 0
[[ $item == "$needle" ]] && return 0
done
return 1
}

# Install function that uses the correct command based on the installation method
install () {
install() {
local app="$1"
local app_name="${app##*/}" # Extract the part after the last slash
local app_name="${app##*/}" # Extract the part after the last slash
local method="$2"
local cmd=""
local is_installed=false

case "$method" in
"cask")
cmd="brew install --cask"
in_array "$app_name" "${installed_casks[@]}" && is_installed=true
;;
"formula")
cmd="brew install --formula"
in_array "$app_name" "${installed_formulas[@]}" && is_installed=true
;;
"mas")
if mas list | grep -q "$app "; then
is_installed=true
app_name=$(mas list | grep "$app " | sed -E 's/.*[0-9]+[[:space:]]+(.*)[[:space:]]+\(.*/\1/' | sed -E 's/[[:space:]]*$//')
else
app_name=$(mas info "$app" | head -n 1 | sed -E 's/(.*)[[:space:]]+[0-9\.]+ \[.*\]/\1/')
cmd="mas install"
fi
;;
"uv")
cmd="uv tool install"
in_array "$app" "${installed_uv[@]}" && is_installed=true
;;
*)
echo -e "❌ \033[1;31mUnknown installation method: $method for $app_name\033[0m"
return 1
;;
"cask")
cmd="brew install --cask"
in_array "$app_name" "${installed_casks[@]}" && is_installed=true
;;
"formula")
cmd="brew install --formula"
in_array "$app_name" "${installed_formulas[@]}" && is_installed=true
;;
"mas")
if mas list | grep -q "$app "; then
is_installed=true
app_name=$(mas list | grep "$app " | sed -E 's/.*[0-9]+[[:space:]]+(.*)[[:space:]]+\(.*/\1/' | sed -E 's/[[:space:]]*$//')
else
app_name=$(mas info "$app" | head -n 1 | sed -E 's/(.*)[[:space:]]+[0-9\.]+ \[.*\]/\1/')
cmd="mas install"
fi
;;
"uv")
cmd="uv tool install"
in_array "$app" "${installed_uv[@]}" && is_installed=true
;;
*)
echo -e "❌ \033[1;31mUnknown installation method: $method for $app_name\033[0m"
return 1
;;
esac

if ! $is_installed; then
Expand All @@ -112,7 +112,7 @@ install () {
brew_sync() {
local toml_apps
toml_apps=$(yq eval 'to_entries | map(.value | to_entries | map(select(.value == "cask" or .value == "formula") | .key)) | flatten | .[]' "$apps_toml")
toml_apps=$(echo "$toml_apps" | sed -E 's|.*/||') # get name from tapped apps (slashes in name)
toml_apps=$(echo "$toml_apps" | sed -E 's|.*/||') # get name from tapped apps (slashes in name)

local missing_formulae
missing_formulae=$(comm -23 <(brew leaves | sort) <(echo "$toml_apps" | sort))
Expand All @@ -121,18 +121,18 @@ brew_sync() {
local missing_apps
missing_apps=$(echo -e "$missing_formulae\n$missing_casks" | sort -u)

if [[ -n "$missing_apps" ]]; then
if [[ -n $missing_apps ]]; then
echo -e "❗️ \033[1;31mThe following Homebrew-installed formulae and casks are missing from apps.toml:\033[0m"
# shellcheck disable=SC2001
echo "$missing_formulae" | sed 's/^/ /'
# shellcheck disable=SC2001
# shellcheck disable=SC2001
echo "$missing_casks" | sed 's/^/ /'
if [[ "$auto_yes" == false ]]; then
if [[ $auto_yes == false ]]; then
read -rp $'\e[1;31mDo you want to uninstall these apps? (y/n)\e[0m ' choice
else
choice="y"
fi
if [[ "$choice" == "y" ]]; then
if [[ $choice == "y" ]]; then
for app in $missing_apps; do
brew uninstall "$app"
echo -e "🚮 \033[1;35mUninstalled $app.\033[0m"
Expand All @@ -152,16 +152,16 @@ uv_sync() {
local missing_uv_apps
missing_uv_apps=$(comm -23 <(uv tool list | awk '{print $1}' | grep -v '^-*$' | sort) <(echo "$toml_apps" | sort))

if [[ -n "$missing_uv_apps" ]]; then
if [[ -n $missing_uv_apps ]]; then
echo -e "❗️ \033[1;31mThe following uv-installed apps are missing from apps.toml:\033[0m"
# shellcheck disable=SC2001
# shellcheck disable=SC2001
echo "$missing_uv_apps" | sed 's/^/ /'
if [[ "$auto_yes" == false ]]; then
if [[ $auto_yes == false ]]; then
read -rp $'\e[1;31mDo you want to uninstall these apps? (y/n)\e[0m ' choice
else
choice="y"
fi
if [[ "$choice" == "y" ]]; then
if [[ $choice == "y" ]]; then
for app in $missing_uv_apps; do
uv tool uninstall "$app"
echo -e "🚮 \033[1;35mUninstalled $app.\033[0m"
Expand All @@ -182,25 +182,25 @@ mas_sync() {
installed_mas_apps=$(mas list | sed -E 's/^([0-9]+)[[:space:]]+(.*)[[:space:]]+\(.*/\1 \2/' | sort)

# `-A` requires bash 4+, can't use Apple-provided bash which is 3.2
declare -A missing_mas_apps=() # Ensure it's initialized as an empty associative array
declare -A missing_mas_apps=() # Ensure it's initialized as an empty associative array

while read -r id name; do
if ! echo "$toml_apps" | grep -q "^$id$"; then
missing_mas_apps["$id"]="$name" # Store ID as key and app name as value
missing_mas_apps["$id"]="$name" # Store ID as key and app name as value
fi
done <<< "$installed_mas_apps"
done <<<"$installed_mas_apps"

if [[ ${#missing_mas_apps[@]} -gt 0 ]]; then
echo -e "❗️ \033[1;31mThe following Mac App Store apps are missing from apps.toml:\033[0m"
for id in "${!missing_mas_apps[@]}"; do
echo -e " ${missing_mas_apps[$id]} ($id)"
done
if [[ "$auto_yes" == false ]]; then
if [[ $auto_yes == false ]]; then
read -rp $'\e[1;31mDo you want to uninstall these apps? (y/n)\e[0m ' choice
else
choice="y"
fi
if [[ "$choice" == "y" ]]; then
if [[ $choice == "y" ]]; then
for id in "${!missing_mas_apps[@]}"; do
name="${missing_mas_apps[$id]}"
if ! mas uninstall "$id"; then
Expand All @@ -227,8 +227,8 @@ parsed_toml=$(yq e 'to_entries | .[] | .key as $category | .value | to_entries[]
# Install apps from each category in the apps.toml file
current_category=""
echo "$parsed_toml" | while IFS=$'\t' read -r category app method; do
if [[ "$category" != "$current_category" ]]; then
suffix=$([[ "$category" == *s ]] && echo "" || echo " apps")
if [[ $category != "$current_category" ]]; then
suffix=$([[ $category == *s ]] && echo "" || echo " apps")
echo -e "\n📦 \033[1;35mInstalling ${category}${suffix}...\033[0m"
current_category="$category"
fi
Expand Down
6 changes: 3 additions & 3 deletions local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ else
fi

# Copy all files from copyme/ to $HOME
if [[ "${1:-}" != "--yes" ]] && [[ "${1:-}" != "-y" ]]; then
if [[ ${1:-} != "--yes" ]] && [[ ${1:-} != "-y" ]]; then
read -rp $'\e[1;31mDo you want to copy and overwrite all files from copyme/ to $HOME? (y/n)\e[0m ' COPYME
else
COPYME="y"
fi
if [[ $COPYME =~ ^[Yy]$ ]]; then
rsync -av --exclude='.DS_Store' copyme/ "$HOME" | \
grep -v "building file list ... done" | \
rsync -av --exclude='.DS_Store' copyme/ "$HOME" |
grep -v "building file list ... done" |
awk '/^$/ { exit } !/\/$/ { printf "\033[1;32m📋 Copied %s\033[0m\n", $0; }'
fi

Expand Down
Loading

0 comments on commit 8aa9062

Please sign in to comment.