Skip to content

Commit

Permalink
Introduce 'cleanup-browser-profiles.sh' (#17)
Browse files Browse the repository at this point in the history
Refactor 'clone_repo_into' to handle optional git branch to switch to.
Use string manipulation syntax instead of calling 'basename' for perf
  • Loading branch information
vraravam committed Jan 1, 2025
1 parent 10c2bd4 commit dd50eb5
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 31 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ As documented in the README's [adopting](README.md#how-to-adoptcustomize-the-scr

For those who follow this repo, here's the changelog for ease of adoption:

### 1.0-39

* Introduced [a new script](scripts/cleanup-browser-profiles.sh) to cleanup browser profiles folders.
* *[fresh-install-of-osx.sh]* Minor refactoring to enhance `clone_repo_into` to handle an optional target git branch which is also validated.

### 1.0-38

* *[.aliases]* Added extra checks for the `status_all_repos` and `count_all_repos` utility functions.
Expand Down Expand Up @@ -53,7 +58,7 @@ For those who follow this repo, here's the changelog for ease of adoption:

### 1.0-32

* *[fresh-install-of-osx]* Added date calculation in `fresh-install-of-osx.sh` to track total execution time.
* *[fresh-install-of-osx.sh]* Added date calculation in `fresh-install-of-osx.sh` to track total execution time.

### 1.0-31

Expand Down
4 changes: 4 additions & 0 deletions Extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Note:

This script is useful to capture the preferences of the known applications (both system-installed and custom-installed applications) using the `defaults read` command. It can be used to both export the preferences/settings (from the old system) or import them (into the new system)

## cleanup-browser-profiles.sh

This script is used to cleanup browser profiles folders (delete cache, session and other files that will anyways be recreated when you restart that browser). It can be safely invoked even if that browser is running (in which case it will skip processing after printing a warning to quit that application)

## osx-defaults.sh

This script is the erstwhile script to codify the macos setup. It can be used to setup some options, but it hasn't been maintained for newer versions of macos. Though the system will not get corrupted, there might be cruft introduced into the system preferences which might not be easy to identify and remove at a later point in time. Use caution and YMMV.
Expand Down
16 changes: 7 additions & 9 deletions files/--HOME--/.aliases
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ command_exists ncdu && alias du='ncdu --color dark -r -x --exclude .git --exclud

# git commands
# Note: Use a function so that it can accept parameters
_git_cmd() {
section_header "Processing ${1}"
git -C "${1}" "${2}"
}
st() {
local target="${1:-$(pwd)}"
section_header "Processing ${target}"
git -C "${target}" st
_git_cmd "${1:-$(pwd)}" st
}
cc() {
local target="${1:-$(pwd)}"
section_header "Processing ${target}"
git -C "${target}" cc
_git_cmd "${1:-$(pwd)}" cc
}
count() {
local target="${1:-$(pwd)}"
section_header "Processing ${target}"
git -C "${target}" count
_git_cmd "${1:-$(pwd)}" count
}

if command_exists 'run_all.sh'; then
Expand Down
30 changes: 29 additions & 1 deletion files/--HOME--/.shellrc
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,42 @@ blue() {
printf "${BLUE}${1}${NC}"
}

light_blue() {
printf "${LIGHT_BLUE}${1}${NC}"
}

purple() {
printf "${PURPLE}${1}${NC}"
}

light_purple() {
printf "${LIGHT_PURPLE}${1}${NC}"
}

cyan() {
printf "${CYAN}${1}${NC}"
}

light_cyan() {
printf "${LIGHT_CYAN}${1}${NC}"
}

green() {
printf "${GREEN}${1}${NC}"
}

light_green() {
printf "${LIGHT_GREEN}${1}${NC}"
}

red() {
printf "${RED}${1}${NC}"
}

light_red() {
printf "${LIGHT_RED}${1}${NC}"
}

yellow() {
printf "${YELLOW}${1}${NC}"
}
Expand All @@ -148,7 +172,7 @@ error() {
}

section_header() {
echo "$(blue "==> ${1} <==")"
echo "$(blue '==>') $(purple "${1}")"
}

is_non_zero_string() {
Expand Down Expand Up @@ -255,6 +279,10 @@ build_keybase_repo_url() {
echo "keybase://private/${KEYBASE_USERNAME}/${1}"
}

folder_size() {
echo $(cyan "$(\du -sh "${1}" | cut -f1)")
}

# if is_macos; then
# Uninstall and reinstall xcode (useful immediately after upgrade or if reinstalling the OS)
# TODO: Kept for reference purposes
Expand Down
3 changes: 1 addition & 2 deletions files/--HOME--/Brewfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# Upgrades and cleans up all regular outdated casks and libs (non-greedy)

# TODO: Need to find cask for:
# Ghostty
# Tinkertool
# ZoomHider

Expand Down Expand Up @@ -90,7 +89,6 @@ cask 'vscodium@insiders' # TODO: Doesn't handle 'Remote Containers' and GH-bas
cask 'zen-browser@twilight'
cask 'zoom'


brew 'mas'
mas 'PDFgear', id: 6469021132

Expand Down Expand Up @@ -182,6 +180,7 @@ end
# ---- casks
# cask 'cloudflare-warp'
# cask 'dbeaver-community'
# cask 'ghostty'
# cask 'git-credential-manager' # https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git
# cask 'intellij-idea-ce'
# cask 'kdiff3'
Expand Down
2 changes: 1 addition & 1 deletion files/--PERSONAL_PROFILES_DIR--/.envrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type is_non_zero_string &> /dev/null 2>&1 || source "${HOME}/.shellrc"

replace_dir_if_exists_and_is_not_symlinked() {
local target="${PERSONAL_PROFILES_DIR}/${2}"
! is_directory "${1}" && ! is_directory "${target}" && warn "skipping symlinking $(basename "${2}") since either source or target don't exist!!!" && return
! is_directory "${1}" && ! is_directory "${target}" && warn "skipping symlinking '${2}' since either source or target don't exist!!!" && return

mkdir -pv "$(dirname "${1}")" "${target}"

Expand Down
2 changes: 2 additions & 0 deletions scripts/capture-defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ensure_dir_exists "${target_dir}"
# PDFgear

app_array=(
'app.zen-browser.zen'
'ch.protonvpn.mac'
'com.0804Team.KeyClu'
'com.abhishek.Clocker'
Expand Down Expand Up @@ -158,6 +159,7 @@ app_array=(
'com.macpaw.site.theunarchiver'
'com.microsoft.rdc.macos'
'com.microsoft.VSCodeInsiders'
'com.mitchellh.ghostty'
'com.mowglii.ItsycalApp'
'com.piriform.ccleaner'
'com.raycast.macos'
Expand Down
88 changes: 88 additions & 0 deletions scripts/cleanup-browser-profiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env zsh

# vim:filetype=zsh syntax=zsh tabstop=2 shiftwidth=2 softtabstop=2 expandtab autoindent fileencoding=utf-8

# This script is used to cleanup browser profiles folders (delete cache, session and other files that will anyways be recreated when you restart that browser). It can be safely invoked even if that browser is running (in which case it will skip processing after printing a warning to quit that application)

type is_non_zero_string &> /dev/null 2>&1 || source "${HOME}/.shellrc"

check_app() {
if is_non_zero_string "$(pgrep "${1}")"; then
warn "Shutdown '${1}' first!; skipping processing of files for ${1}"
return 1
else
return 0
fi
}

find_and_destroy() {
find "${1}" -type "${2}" -iname "${3}" -delete
}

vacuum_browser_profile_folder() {
local profile_folder="${2}"
! is_directory "${profile_folder}" && warn "skipping processing of '${profile_folder}' since it doesn't exist" && return

section_header "Vacuuming '${1}' in '${profile_folder}'..."
echo "--> Size before: $(folder_size "${profile_folder}")"

command_exists sqlite && find "${profile_folder}" -type f -iname '*.sqlite' -exec sqlite3 -line {} "VACUUM; REINDEX;" \;

file_array=(
'.DS_Store'
'.localized'
'.parentlock'
'*.log'
'*.sqlite-shm'
'*.sqlite-wal'
'*.bak*'
'compatibility.ini'
'extensions.rdf'
'msgFilterRules.dat'
'popstate.dat'
'sessionstore.bak'
'signons*.txt'
'*healthreport*'
'global-messages-db.sqlite'
)
for file in "${file_array[@]}"; do
find_and_destroy "${profile_folder}" 'f' "${file}"
done

directory_array=(
'ABphotos'
'bookmarkbackups'
'sessionstore-backups'
'GoogleContacts'
"Local\ Folders"
'lnubackups'
'minidumps'
'SDThumbs'
"smart\ mailboxes"
'startupCache'
'thumbnails'
'*telemetry*'
'weave'
'crashes'
'*healthreport*'
)
for directory in "${directory_array[@]}"; do
find_and_destroy "${profile_folder}" 'd' "${directory}"
done

echo "--> Size after: $(folder_size "${profile_folder}")"
success "Successfully processed profile folder for '${1}'"
}

process_browser() {
check_app "${1}" && vacuum_browser_profile_folder "$@"
}

# TODO: Commented out since I am moving away from Arc and will delete that folder completely
# process_browser arc "${PERSONAL_PROFILES_DIR}/ArcProfile"
process_browser brave "${PERSONAL_PROFILES_DIR}/BraveProfile"
# TODO: Commented out since I haven't tested that this works without losing auto-recreated data
# process_browser chrome "${PERSONAL_PROFILES_DIR}/ChromeProfile"
process_browser firefox "${PERSONAL_PROFILES_DIR}/FirefoxProfile"
process_browser thunderbird "${PERSONAL_PROFILES_DIR}/ThunderbirdProfile"
process_browser zen "${PERSONAL_PROFILES_DIR}/ZenProfile"
34 changes: 19 additions & 15 deletions scripts/fresh-install-of-osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,30 @@ set_ssh_folder_permissions() {
}

clone_repo_into() {
ensure_dir_exists "${2}"
if ! is_git_repo "${2}"; then
local target_folder="${2}"
ensure_dir_exists "${target_folder}"
if ! is_git_repo "${target_folder}"; then
local tmp_folder="$(mktemp -d)"
git -C "${tmp_folder}" clone -q "${1}" .
mv "${tmp_folder}/.git" "${2}"
git -C "${2}" checkout .
git -C "${2}" submodule update --init --recursive --remote --rebase --force
mv "${tmp_folder}/.git" "${target_folder}"
git -C "${target_folder}" checkout .
git -C "${target_folder}" submodule update --init --recursive --remote --rebase --force
rm -rf "${tmp_folder}"
success "Successfully cloned '${1}' into '${2}'"
success "Successfully cloned '${1}' into '${target_folder}'"

local target_branch="${3}"
if is_non_zero_string "${target_branch}"; then
git -C "${target_folder}" switch "${target_branch}"
local checked_out_branch="$(git -C "${target_folder}" branch --show-current)"
[[ "${checked_out_branch}" != "${target_branch}" ]] && error "'${target_branch}' is not equal to the branch that was checked out: '${checked_out_branch}'; something is wrong. Please correct before retrying!"
fi
else
warn "Skipping cloning of '${1}' since '${2}' is already a git repo"
warn "Skipping cloning of '${1}' since '${target_folder}' is already a git repo"
fi
}

clone_omz_plugin_if_not_present() {
clone_repo_into "${1}" "${ZSH_CUSTOM}/plugins/$(basename "${1}")"
clone_repo_into "${1}" "${ZSH_CUSTOM}/plugins/${1##*/}"
}

replace_executable_if_exists_and_is_not_symlinked() {
Expand Down Expand Up @@ -189,11 +197,7 @@ if is_non_zero_string "${DOTFILES_DIR}" && ! is_git_repo "${DOTFILES_DIR}"; then
rm -rf "${ZDOTDIR}/.zshrc"

# Note: Cloning with https since the ssh keys will not be present at this time
clone_repo_into "https://github.com/${GH_USERNAME}/dotfiles" "${DOTFILES_DIR}"

git -C "${DOTFILES_DIR}" switch "${DOTFILES_BRANCH}"
local_branch="$(git -C "${DOTFILES_DIR}" branch --show-current)"
[[ "${local_branch}" != "${DOTFILES_BRANCH}" ]] && error "'DOTFILES_BRANCH' env var is not equal to the branch that was checked out: '${local_branch}'; something is wrong. Please correct before retrying!"
clone_repo_into "https://github.com/${GH_USERNAME}/dotfiles" "${DOTFILES_DIR}" "${DOTFILES_BRANCH}"

append_to_path_if_dir_exists "${DOTFILES_DIR}/scripts"

Expand Down Expand Up @@ -355,8 +359,8 @@ if is_non_zero_string "${KEYBASE_USERNAME}"; then
if is_non_zero_string "${KEYBASE_PROFILES_REPO_NAME}" && is_non_zero_string "${PERSONAL_PROFILES_DIR}"; then
clone_repo_into "$(build_keybase_repo_url "${KEYBASE_PROFILES_REPO_NAME}")" "${PERSONAL_PROFILES_DIR}"

# Clone the natsumi-browser repo into the ZenProfile/Profiles/chrome folder
is_directory "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/" && clone_repo_into "[email protected]:greeeen-dev/natsumi-browser" "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/chrome"
# Clone the natsumi-browser repo into the ZenProfile/Profiles/chrome folder and switch to the 'dev' branch
is_directory "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/" && clone_repo_into "[email protected]:vraravam/natsumi-browser" "${PERSONAL_PROFILES_DIR}/ZenProfile/Profiles/chrome" dev
else
warn "skipping cloning of profiles repo since either the 'KEYBASE_PROFILES_REPO_NAME' or the 'PERSONAL_PROFILES_DIR' env var hasn't been set"
fi
Expand Down
4 changes: 2 additions & 2 deletions scripts/recreate-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fi
! is_git_repo "${folder}" && error "'${folder}' is not a git repo. Please specify the root of a git repo to proceed. Aborting!!!"

# For the profiles repo alone, I don't care about retaining the history
[[ "$(basename "${folder}")" == "${KEYBASE_PROFILES_REPO_NAME}" ]] && force=Y
[[ "${folder##*/}" == "${KEYBASE_PROFILES_REPO_NAME}" ]] && force=Y

echo "$(yellow 'Processing folder'): '${folder}'"
echo "$(yellow "Squash commits (will lose history!)"): '${force}'"
Expand Down Expand Up @@ -86,7 +86,7 @@ eval "SKIP_SIZE_BEFORE=1 ${git_cmd} cc"

if [[ "${git_url}" =~ 'keybase' ]]; then
echo "$(blue 'Recreating') '$(yellow "${git_url}")'"
git_remote_repo_name="$(basename "${git_url}")"
local git_remote_repo_name="${git_url##*/}"
keybase git delete -f "${git_remote_repo_name}"
keybase git create "${git_remote_repo_name}"
fi
Expand Down

0 comments on commit dd50eb5

Please sign in to comment.