dotfiles/tasks/osx.py

262 lines
6.9 KiB
Python
Raw Normal View History

import os
2016-07-09 01:47:56 -06:00
from invoke import task as ctask
2014-10-21 12:00:04 -06:00
from . import util
@ctask(default=True)
2014-11-11 16:52:25 -07:00
def setup(ctx):
brew(ctx)
2016-10-08 23:18:26 -06:00
brew_essential(ctx)
2014-11-11 16:52:25 -07:00
access_for_assistive_devices(ctx)
2016-08-15 19:09:22 -06:00
tccutil(ctx)
2015-04-28 14:43:26 -06:00
karabiner(ctx)
2014-11-11 16:52:25 -07:00
locate(ctx)
set_path_for_launchd(ctx)
2015-01-02 16:09:48 -07:00
rvm(ctx)
fonts(ctx)
fix_htop(ctx)
2015-01-21 19:42:18 -07:00
iTerm(ctx)
keyboard_settings(ctx)
2015-01-22 01:22:05 -07:00
custom_keyboard_shortcuts(ctx)
2016-10-08 23:18:26 -06:00
brew_additional(ctx)
brew_default(ctx)
2015-01-26 01:42:29 -07:00
@ctask
def setup_dbus(ctx):
ctx.run("ln -sfv /usr/local/opt/d-bus/*.plist ~/Library/LaunchAgents")
2015-01-26 01:42:29 -07:00
@ctask
2015-01-26 01:42:29 -07:00
def system_settings(ctx):
ctx.run('{0}'.format(
os.path.join(util.RESOURCES_DIRECTORY, 'osx_setup.sh')
2014-10-23 16:42:28 -06:00
), pty=True)
2014-10-21 12:00:04 -06:00
2014-11-10 01:13:30 -07:00
2016-10-08 23:18:26 -06:00
BREWFILE_DIRECTORY = os.path.join(util.TASKS_DIRECTORY, "brewfiles")
def make_brewfile_task(filename):
task_name = "brew_{}".format(filename)
@ctask(name=task_name)
def temp(ctx):
ctx.run('brew bundle --file="{}"'.format(
os.path.join(BREWFILE_DIRECTORY, filename)
))
globals()[task_name] = temp
for filename in os.listdir(BREWFILE_DIRECTORY):
make_brewfile_task(filename)
2014-10-31 18:07:28 -06:00
@ctask
2014-11-11 16:52:25 -07:00
def cider(ctx):
2014-11-10 01:13:30 -07:00
ctx.run('brew install caskroom/cask/brew-cask')
2014-10-31 19:34:49 -06:00
if not util.command_exists('cider'):
ctx.run('sudo pip install cider')
ctx.run('cider restore -i')
2014-10-31 18:07:28 -06:00
@ctask
2014-11-11 16:52:25 -07:00
def brew(ctx):
path = 'https://raw.githubusercontent.com/Homebrew/install/master/install)'
2014-10-21 12:00:04 -06:00
if not util.command_exists('brew'):
2014-11-11 16:52:25 -07:00
ctx.run('ruby -e "$(curl -fsSL {0}'.format(path))
2016-10-08 23:18:26 -06:00
ctx.run('brew tap Homebrew/bundle')
2014-11-10 01:13:30 -07:00
2014-10-20 21:15:11 -06:00
@ctask
2014-11-11 16:52:25 -07:00
def set_path_for_launchd(ctx):
2014-10-21 12:00:04 -06:00
launch_agent_dir = os.path.expanduser('~/Library/LaunchAgents/')
filename = 'set-path.plist'
source = os.path.join(util.RESOURCES_DIRECTORY, filename)
destination = os.path.join(launch_agent_dir, filename)
if os.path.exists(source) and not os.path.exists(destination):
util.ensure_path_exists(launch_agent_dir)
ctx.run('ln -s {0} {1}'.format(source, destination))
2014-10-20 21:15:11 -06:00
APPS_NEEDING_ASSISTIVE_DEVICE_ACCESS = ('Slate', 'Synergy', 'iTerm')
2015-01-26 01:42:29 -07:00
@ctask
2014-11-11 16:52:25 -07:00
def access_for_assistive_devices(ctx):
# TODO: refactor to use tccutil.py
for app in APPS_NEEDING_ASSISTIVE_DEVICE_ACCESS:
app_string = '/Applications/{0}.app'.format(app)
user_application = os.path.expanduser('~' + app_string)
2014-11-11 16:52:25 -07:00
access_if_exists(ctx, user_application)
access_if_exists(ctx, app_string)
2015-04-28 14:43:26 -06:00
access_if_exists(
ctx,
"/Applications/Karabiner.app/"
)
2014-11-11 16:52:25 -07:00
access_if_exists(
ctx,
2014-11-11 16:52:25 -07:00
"/Applications/Karabiner.app/Contents/Applications/"
"Karabiner_AXNotifier.app"
)
2014-10-24 07:19:23 -06:00
SYNERGY_BINARIES_PATH = '/Applications/Synergy.app/Contents/MacOS/'
SCRIPTS_NEEDING_ASSISTIVE_DEVICE_ACCESS = [
os.path.join(SYNERGY_BINARIES_PATH, 'synergyc'),
os.path.join(SYNERGY_BINARIES_PATH, 'synergys'),
"org.gnu.Emacs",
"com.googlecode.iterm2",
]
@ctask
def tccutil(ctx):
for script in SCRIPTS_NEEDING_ASSISTIVE_DEVICE_ACCESS:
ctx.run(
'sudo tccutil -i "{}"'.format(script)
)
ctx.run(
'sudo tccutil -e "{}"'.format(script)
)
2014-10-24 07:19:23 -06:00
2014-11-11 16:52:25 -07:00
def access_if_exists(ctx, app_string):
2014-10-24 07:19:23 -06:00
if os.path.exists(app_string):
2015-04-28 14:43:26 -06:00
print("enabling access for {0}", app_string)
2014-10-24 07:19:23 -06:00
ctx.run(
'zsh -c "source ~/.zshrc && '
'enable_access_for_assistive_devices \"{0}\""'.format(
app_string
)
2014-10-24 07:19:23 -06:00
)
2015-01-26 01:42:29 -07:00
2015-02-02 17:16:30 -07:00
@ctask(aliases=['hyper', 'fast_repeat'])
def karabiner(ctx):
2014-10-24 07:19:23 -06:00
source = '{0}/karabiner-hyper.xml'.format(util.RESOURCES_DIRECTORY)
2014-12-31 09:13:40 -07:00
destination_folder = os.path.join(
os.path.expanduser("~/Library"), "Application\\ Support", "Karabiner"
2014-11-11 16:52:25 -07:00
)
2014-12-31 09:13:40 -07:00
destination = os.path.join(destination_folder, "private.xml")
2014-10-24 07:19:23 -06:00
try:
ctx.run("rm {0}".format(destination))
except:
pass
2014-12-31 09:13:40 -07:00
util.ensure_path_exists(destination_folder)
2014-10-24 07:19:23 -06:00
ctx.run("ln -s {0} {1}".format(source, destination))
ctx.run("{0}/karabiner_config.sh".format(util.RESOURCES_DIRECTORY))
2014-11-10 03:00:46 -07:00
@ctask
2014-11-11 16:52:25 -07:00
def locate(ctx):
ctx.run('sudo launchctl load -w '
'/System/Library/LaunchDaemons/com.apple.locate.plist')
2014-11-10 03:00:46 -07:00
@ctask
2015-01-02 16:09:48 -07:00
def rvm(ctx):
2014-11-11 16:52:25 -07:00
ctx.run('\\curl -sSL https://get.rvm.io | bash -s stable')
2014-11-10 03:00:46 -07:00
@ctask
2015-01-02 16:09:48 -07:00
def fonts(ctx):
2014-11-11 16:52:25 -07:00
ctx.run('open {0}'.format(
2015-08-16 02:00:12 -06:00
os.path.join(util.RESOURCES_DIRECTORY, 'fonts', "*"))
2014-11-11 16:52:25 -07:00
)
2014-11-10 03:00:46 -07:00
@ctask
2014-11-11 16:52:25 -07:00
def cl_tools(ctx):
2014-11-10 03:00:46 -07:00
if not util.command_exists('gcc'):
ctx.run('xcode-select --install')
2014-12-20 02:23:42 -07:00
@ctask
def fix_htop(ctx):
2015-12-13 18:20:21 -07:00
real_htop_location = ctx.run(
2016-03-15 17:26:51 -06:00
"zsh -c 'greadlink -f $(brew --prefix htop-osx)'"
2015-12-13 18:20:21 -07:00
).stdout.strip() + "/bin/htop"
2014-12-31 09:13:27 -07:00
ctx.run("sudo chmod 6555 {0}".format(real_htop_location))
2014-12-20 02:23:42 -07:00
ctx.run("sudo chown root {0}".format(real_htop_location))
@ctask
def iTerm(ctx):
library_plist = os.path.join(os.path.expanduser("~"), "Library",
"Preferences", "com.googlecode.iterm2.plist")
ctx.run("defaults write {0} LoadPrefsFromCustomFolder -bool true".format(
library_plist
))
ctx.run("defaults write {0} PrefsCustomFolder -string {1}".format(
library_plist, util.RESOURCES_DIRECTORY
))
2014-12-22 11:08:12 -07:00
@ctask
def launch_agents(ctx, flags=''):
ctx.run('dotfiles -sn{1} -R {0}/resources/LaunchAgents/ '
'-H ~/Library/LaunchAgents'.format(util.REPO_DIRECTORY, flags))
@ctask
def keyboard_settings(ctx):
ctx.run("zsh -c 'refresh_config && set_modifier_keys_on_all_keyboards'")
2015-01-21 19:42:18 -07:00
2015-01-22 01:22:05 -07:00
settings_directory = os.path.join(util.RESOURCES_DIRECTORY, 'osx_settings')
all_save_settings = []
all_write_settings = []
all_diff_settings = []
2015-01-26 01:42:29 -07:00
2015-01-21 23:59:25 -07:00
def functions_for_filename(filename):
2015-01-22 01:22:05 -07:00
filepath = os.path.join(settings_directory, filename)
task_name = 'settings-write:' + filename.replace('.', '-')
2015-01-21 23:59:25 -07:00
@ctask(name=task_name)
def task(ctx):
ctx.run("defaults write {0} '$(cat {1})'".format(
filename, filepath
))
globals()[task_name] = task
2015-01-22 01:22:05 -07:00
all_write_settings.append(task)
2015-01-21 23:59:25 -07:00
2015-01-22 01:22:05 -07:00
task_name = 'settings-save:' + filename.replace('.', '-')
2015-01-21 23:59:25 -07:00
@ctask(name=task_name)
def task(ctx):
ctx.run("defaults read {0} > {1}".format(
filename, filepath
))
globals()[task_name] = task
2015-01-22 01:22:05 -07:00
all_save_settings.append(task)
2015-01-21 23:59:25 -07:00
2015-01-22 01:22:05 -07:00
task_name = 'settings-diff:' + filename.replace('.', '-')
2015-01-21 23:59:25 -07:00
@ctask(name=task_name)
def task(ctx):
2016-09-03 17:43:05 -06:00
print(filepath)
print(filename)
2015-01-21 23:59:25 -07:00
ctx.run("zsh -c 'icdiff <(defaults read {0}) {1}'".format(
filename, filepath
))
globals()[task_name] = task
2015-01-22 01:22:05 -07:00
all_diff_settings.append(task)
2015-01-26 01:42:29 -07:00
2015-01-22 01:22:05 -07:00
for _, _, filenames in os.walk(settings_directory):
2015-01-21 23:59:25 -07:00
for filename in filenames:
functions_for_filename(filename)
2015-01-22 00:00:52 -07:00
@ctask
2015-01-22 01:22:05 -07:00
def settings_write_all(ctx):
for function in all_write_settings:
2015-01-22 00:00:52 -07:00
function(ctx)
2015-01-22 01:22:05 -07:00
@ctask
def custom_keyboard_shortcuts(ctx):
2016-09-03 17:43:05 -06:00
command_string = b"""defaults write -globalDomain NSUserKeyEquivalents '{"Enter Full Screen" = "@\U21a9";"Exit Full Screen" = "@\U21a9";"Full Screen" = "@\U21a9";}'"""
2015-01-22 01:22:05 -07:00
ctx.run(command_string)