Merge pull request #2008 from gaelicWizard/plugin-osx

Plugin/OSX: cleanup
pull/2007/head
Noah Gorny 2022-01-07 08:41:17 +02:00 committed by GitHub
commit 5d68fca7e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 148 additions and 179 deletions

View File

@ -104,6 +104,8 @@ plugins/available/jump.plugin.bash
plugins/available/less-pretty-cat.plugin.bash plugins/available/less-pretty-cat.plugin.bash
plugins/available/node.plugin.bash plugins/available/node.plugin.bash
plugins/available/nodenv.plugin.bash plugins/available/nodenv.plugin.bash
plugins/available/osx-timemachine.plugin.bash
plugins/available/osx.plugin.bash
plugins/available/percol.plugin.bash plugins/available/percol.plugin.bash
plugins/available/plenv.plugin.bash plugins/available/plenv.plugin.bash
plugins/available/pyenv.plugin.bash plugins/available/pyenv.plugin.bash

View File

@ -1,84 +1,95 @@
cite about-plugin # shellcheck shell=bash
about-plugin 'OS X Time Machine functions' about-plugin 'OS X Time Machine functions'
function time-machine-destination() { if [[ "${OSTYPE}" != 'darwin'* ]]; then
group "osx-timemachine" _log_warning "This plugin only works with Mac OS X"
about "Shows the OS X Time Machine destination/mount point" return 1
fi
echo $(tmutil destinationinfo | grep "Mount Point" | sed -e 's/Mount Point : \(.*\)/\1/g') function time-machine-destination() {
group "osx-timemachine"
about "Shows the OS X Time Machine destination/mount point"
tmutil destinationinfo | grep "Mount Point" | sed -e 's/Mount Point : \(.*\)/\1/g'
} }
function time-machine-list-machines() { function time-machine-list-machines() {
group "osx-timemachine" group "osx-timemachine"
about "Lists the OS X Time Machine machines on the backup volume" about "Lists the OS X Time Machine machines on the backup volume"
local tmdest="$(time-machine-destination)/Backups.backupdb" local tmdest
tmdest="$(time-machine-destination)/Backups.backupdb"
find "$tmdest" -maxdepth 1 -mindepth 1 -type d | grep -v "/\." | while read line ; do find "$tmdest" -maxdepth 1 -mindepth 1 -type d | grep -v "/\." | while read -r line; do
echo "${line##*/}" echo "${line##*/}"
done done
} }
function time-machine-list-all-backups() { function time-machine-list-all-backups() {
group "osx-timemachine" group "osx-timemachine"
about "Shows all of the backups for the specified machine" about "Shows all of the backups for the specified machine"
param "1: Machine name (optional)" param "1: Machine name (optional)"
example "time-machine-list-all-backups my-laptop" example "time-machine-list-all-backups my-laptop"
# Use the local hostname if none provided # Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)} local COMPUTERNAME BACKUP_LOCATION
local BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME" COMPUTERNAME=${1:-$(scutil --get ComputerName)}
BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"
find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d | while read line ; do find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d | while read -r line; do
echo "$line" echo "$line"
done done
} }
function time-machine-list-old-backups() { function time-machine-list-old-backups() {
group "osx-timemachine" group "osx-timemachine"
about "Shows all of the backups for the specified machine, except for the most recent backup" about "Shows all of the backups for the specified machine, except for the most recent backup"
param "1: Machine name (optional)" param "1: Machine name (optional)"
example "time-machine-list-old-backups my-laptop" example "time-machine-list-old-backups my-laptop"
# Use the local hostname if none provided # Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)} local COMPUTERNAME BACKUP_LOCATION
local BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME" COMPUTERNAME=${1:-$(scutil --get ComputerName)}
BACKUP_LOCATION="$(time-machine-destination)/Backups.backupdb/$COMPUTERNAME"
# List all but the most recent one # List all but the most recent one
find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d -name 2\* | sed \$d | while read line ; do find "$BACKUP_LOCATION" -maxdepth 1 -mindepth 1 -type d -name 2\* | sed \$d | while read -r line; do
echo "$line" echo "$line"
done done
} }
# Taken from here: http://stackoverflow.com/a/30547074/1228454 # Taken from here: http://stackoverflow.com/a/30547074/1228454
function _tm_startsudo() { function _tm_startsudo() {
sudo -v local -x SUDO_COMMAND="plugin/osx-timemachine: keep 'sudo' token alive during long-run 'tmutil' commands"
( while true; do sudo -v; sleep 50; done; ) & sudo "-${SUDO_ASKPASS:+A}v" # validate without running a command, using `ssh-askpass` if available.
SUDO_PID="$!" (while sudo "-${SUDO_ASKPASS:+A}v"; do
trap _tm_stopsudo SIGINT SIGTERM sleep 50
done) &
SUDO_PID="$!"
trap _tm_stopsudo SIGINT SIGTERM
} }
function _tm_stopsudo() { function _tm_stopsudo() {
kill "$SUDO_PID" kill "$SUDO_PID"
trap - SIGINT SIGTERM trap - SIGINT SIGTERM
sudo -k sudo -k
} }
function time-machine-delete-old-backups() { function time-machine-delete-old-backups() {
group "osx-timemachine" group "osx-timemachine"
about "Deletes all of the backups for the specified machine, with the exception of the most recent one" about "Deletes all of the backups for the specified machine, with the exception of the most recent one"
param "1: Machine name (optional)" param "1: Machine name (optional)"
example "time-machine-delete-old-backups my-laptop" example "time-machine-delete-old-backups my-laptop"
# Use the local hostname if none provided # Use the local hostname if none provided
local COMPUTERNAME=${1:-$(scutil --get ComputerName)} local COMPUTERNAME=${1:-$(scutil --get ComputerName)} _old_backup
# Ask for sudo credentials only once # Ask for sudo credentials only once
_tm_startsudo _tm_startsudo
echo "$(time-machine-list-old-backups "$COMPUTERNAME")" | while read i ; do while read -r _old_backup; do
# Delete the backup # Delete the backup
sudo tmutil delete "$i" sudo tmutil delete "$_old_backup"
done done <<< "$(time-machine-list-old-backups "$COMPUTERNAME")"
_tm_stopsudo _tm_stopsudo
} }

View File

@ -1,23 +1,21 @@
cite about-plugin # shellcheck shell=bash
about-plugin 'osx-specific functions' about-plugin 'osx-specific functions'
if [[ "${OSTYPE}" != 'darwin'* ]]; then
_log_warning "This plugin only works with Mac OS X."
return 1
fi
# OS X: Open new tabs in same directory # OS X: Open new tabs in same directory
if [[ $OSTYPE == 'darwin'* ]]; then if _is_function update_terminal_cwd; then
if type update_terminal_cwd > /dev/null 2>&1 ; then safe_append_prompt_command 'update_terminal_cwd'
if ! [[ $PROMPT_COMMAND =~ (^|;)update_terminal_cwd($|;) ]] ; then
PROMPT_COMMAND="${PROMPT_COMMAND%;};update_terminal_cwd"
declared="$(declare -p PROMPT_COMMAND)"
[[ "$declared" =~ \ -[aAilrtu]*x[aAilrtu]*\ ]] 2>/dev/null
[[ $? -eq 0 ]] && export PROMPT_COMMAND
fi
fi
fi fi
function tab() { function tab() {
about 'opens a new terminal tab' about 'opens a new terminal tab'
group 'osx' group 'osx'
osascript 2>/dev/null <<EOF osascript 2> /dev/null << EOF
tell application "System Events" tell application "System Events"
tell process "Terminal" to keystroke "t" using command down tell process "Terminal" to keystroke "t" using command down
end end
@ -30,147 +28,105 @@ EOF
# renames the current os x terminal tab title # renames the current os x terminal tab title
function tabname { function tabname {
printf "\e]1;$1\a" printf '%b' "\e]1;$1\a"
} }
# renames the current os x terminal window title # renames the current os x terminal window title
function winname { function winname {
printf "\e]2;$1\a" printf '%b' "\e]2;$1\a"
} }
# this one switches your os x dock between 2d and 3d function pman() {
# thanks to savier.zwetschge.org about 'view man documentation in Preview'
function dock-switch() { param '1: man page to view'
about 'switch dock between 2d and 3d' example '$ pman bash'
param '1: "2d" or "3d"' group 'osx'
example '$ dock-switch 2d' man -t "${1}" | open -fa 'Preview'
group 'osx'
if [[ "$OSTYPE" = 'darwin'* ]]; then
if [[ $1 = 3d ]] ; then
defaults write com.apple.dock no-glass -boolean NO
killall Dock
elif [[ $1 = 2d ]] ; then
defaults write com.apple.dock no-glass -boolean YES
killall Dock
else
echo "usage:"
echo "dock-switch 2d"
echo "dock-switch 3d."
fi
else
echo "Sorry, this only works on Mac OS X"
fi
} }
function pman () function pri() {
{ about 'display information about Ruby classes, modules, or methods, in Preview'
about 'view man documentation in Preview' param '1: Ruby method, module, or class'
param '1: man page to view' example '$ pri Array'
example '$ pman bash' group 'osx'
group 'osx' ri -T "${1}" | open -fa 'Preview'
man -t "${1}" | open -fa $PREVIEW
}
function pri ()
{
about 'display information about Ruby classes, modules, or methods, in Preview'
param '1: Ruby method, module, or class'
example '$ pri Array'
group 'osx'
ri -T "${1}" | open -fa $PREVIEW
} }
# Download a file and open it in Preview # Download a file and open it in Preview
function prevcurl() { function prevcurl() {
about 'download a file and open it in Preview' about 'download a file and open it in Preview'
param '1: url' param '1: url'
group 'osx' group 'osx'
if [[ ! $OSTYPE = 'darwin'* ]] curl "$*" | open -fa 'Preview'
then
echo "This function only works with Mac OS X"
return 1
fi
curl "$*" | open -fa $PREVIEW
} }
function refresh-launchpad() { function refresh-launchpad() {
about 'Reset launchpad layout in macOS' about 'Reset launchpad layout in macOS'
example '$ refresh-launchpad' example '$ refresh-launchpad'
group 'osx' group 'osx'
if [[ "$OSTYPE" = 'darwin'* ]];then defaults write com.apple.dock ResetLaunchPad -bool TRUE
defaults write com.apple.dock ResetLaunchPad -bool TRUE killall Dock
killall Dock
else
echo "Sorry, this only works on Mac OS X"
fi
} }
function list-jvms(){ function list-jvms() {
about 'List java virtual machines and their states in macOS' about 'List java virtual machines and their states in macOS'
example 'list-jvms' example 'list-jvms'
group 'osx' group 'osx'
JVMS_DIR="/Library/Java/JavaVirtualMachines" local JVMS_DIR="/Library/Java/JavaVirtualMachines"
JVMS=( $(ls ${JVMS_DIR}) ) # The following variables are intended to impact the enclosing scope, not local.
JVMS_STATES=() JVMS=("${JVMS_DIR}"/*)
JVMS_STATES=()
# Map state of JVM # Map state of JVM
for (( i = 0; i < ${#JVMS[@]}; i++ )); do for ((i = 0; i < ${#JVMS[@]}; i++)); do
if [[ -f "${JVMS_DIR}/${JVMS[$i]}/Contents/Info.plist" ]]; then if [[ -f "${JVMS[i]}/Contents/Info.plist" ]]; then
JVMS_STATES[${i}]=enabled JVMS_STATES[i]=enabled
else else
JVMS_STATES[${i}]=disabled JVMS_STATES[i]=disabled
fi fi
echo "${i} ${JVMS[$i]} ${JVMS_STATES[$i]}" printf '%s\t%s\t%s\n' "${i}" "${JVMS[i]##*/}" "${JVMS_STATES[i]}"
done done
} }
function pick-default-jvm(){ function pick-default-jvm() {
about 'Pick the default Java Virtual Machines in system-wide scope in macOS' about 'Pick the default Java Virtual Machines in system-wide scope in macOS'
example 'pick-default-jvm' example 'pick-default-jvm'
# Call function for listing # Declare variables
list-jvms local JVMS JVMS_STATES
local DEFAULT_JVM_DIR DEFAULT_JVM OPTION
# Declare variables # Call function for listing
local DEFAULT_JVM_DIR="" list-jvms
local DEFAULT_JVM=""
local OPTION=""
# OPTION for default jdk and set variables # OPTION for default jdk and set variables
while [[ ! "$OPTION" =~ ^[0-9]+$ || OPTION -ge "${#JVMS[@]}" ]]; do while [[ ! "$OPTION" =~ ^[0-9]+$ || OPTION -ge "${#JVMS[@]}" ]]; do
read -p "Enter Default JVM: " OPTION read -rp "Enter Default JVM: " OPTION
if [[ ! "$OPTION" =~ ^[0-9]+$ ]]; then if [[ ! "$OPTION" =~ ^[0-9]+$ ]]; then
echo "Please enter a number" echo "Please enter a number"
fi fi
if [[ OPTION -ge "${#JVMS[@]}" ]]; then if [[ OPTION -ge "${#JVMS[@]}" ]]; then
echo "Please select one of the displayed JVMs" echo "Please select one of the displayed JVMs"
fi fi
done done
DEFAULT_JVM_DIR="${JVMS_DIR}/${JVMS[$OPTION]}" DEFAULT_JVM_DIR="${JVMS[OPTION]}"
DEFAULT_JVM="${JVMS[$OPTION]}" DEFAULT_JVM="${JVMS[OPTION]##*/}"
# Disable all jdk # Disable all jdk
for (( i = 0; i < ${#JVMS[@]}; i++ )); do for ((i = 0; i < ${#JVMS[@]}; i++)); do
if [[ -f "${JVMS_DIR}/${JVMS[$i]}/Contents/Info.plist" ]]; then if [[ "${JVMS[i]}" != "${DEFAULT_JVM_DIR}" && -f "${JVMS[i]}/Contents/Info.plist" ]]; then
sudo mv "${JVMS_DIR}/${JVMS[$i]}/Contents/Info.plist" "${JVMS_DIR}/${JVMS[$i]}/Contents/Info.plist.disable" sudo mv "${JVMS[i]}/Contents/Info.plist" "${JVMS[i]}/Contents/Info.plist.disable"
fi fi
done done
# Enable default jdk # Enable default jdk
if [[ -f "${DEFAULT_JVM_DIR}/Contents/Info.plist.disable" ]]; then if [[ -f "${DEFAULT_JVM_DIR}/Contents/Info.plist.disable" ]]; then
sudo mv "${DEFAULT_JVM_DIR}/Contents/Info.plist.disable" "${DEFAULT_JVM_DIR}/Contents/Info.plist" sudo mv -vn "${DEFAULT_JVM_DIR}/Contents/Info.plist.disable" "${DEFAULT_JVM_DIR}/Contents/Info.plist" \
echo "Enabled ${DEFAULT_JVM} as default JVM" && echo "Enabled ${DEFAULT_JVM} as default JVM"
fi fi
} }
# Make this backwards compatible
alias pcurl='prevcurl'

View File

@ -11,7 +11,7 @@ if [ -z "${BASH_IT}" ]; then
fi fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
test_dirs=( ${test_directory}/{bash_it,completion,install,lib,plugins,themes} ) test_dirs=( "${test_directory}"/{bash_it,completion,install,lib,plugins,themes} )
else else
test_dirs=( "$1" ) test_dirs=( "$1" )
fi fi