2020-02-06 19:20:50 +08:00
# frozen_string_literal: true
2021-12-25 12:30:10 +08:00
require 'rake'
require 'fileutils'
require 'English'
2012-01-25 14:13:33 +08:00
2019-07-30 11:50:46 +08:00
def macos?
2021-12-25 12:30:10 +08:00
RUBY_PLATFORM . downcase . include? ( 'darwin' )
2019-07-30 11:50:46 +08:00
end
def linux?
2021-12-25 12:30:10 +08:00
RUBY_PLATFORM . downcase . include? ( 'linux' )
2019-07-30 11:50:46 +08:00
end
2017-07-06 10:50:01 +08:00
# this has all the runcoms from this directory.
2017-04-13 12:38:48 +08:00
task :link_files do
2021-12-25 12:30:10 +08:00
install_files ( Dir . glob ( 'git/*' ) ) if want_to_install? ( 'git configs (color, aliases)' )
install_files ( Dir . glob ( 'irb/*' ) ) if want_to_install? ( 'irb/pry configs (more colorful)' )
install_files ( Dir . glob ( 'ruby/*' ) ) if want_to_install? ( 'rubygems config (faster/no docs)' )
install_files ( Dir . glob ( 'ctags/*' ) ) if want_to_install? ( 'ctags config (better js/ruby support)' )
install_files ( Dir . glob ( 'tmux/*' ) ) if want_to_install? ( 'tmux config' )
install_files ( Dir . glob ( 'vimify/*' ) ) if want_to_install? ( 'vimification of command line tools' )
2020-02-06 19:20:50 +08:00
run %(
2022-08-30 10:21:07 +08:00
rm - rf ~ / .tmux / plugins / tpm
2017-07-06 11:00:31 +08:00
git clone https : / / github . com / tmux - plugins / tpm ~ / .tmux / plugins / tpm
2020-02-06 19:20:50 +08:00
)
2017-04-13 12:38:48 +08:00
end
2021-12-25 12:30:10 +08:00
desc 'Hook our dotfiles into system-standard positions.'
2020-02-06 19:20:50 +08:00
task install : % i [ submodule_init submodules ] do
2012-03-23 03:53:25 +08:00
puts
2021-12-25 12:30:10 +08:00
puts '======================================================'
puts 'Welcome to YADR Installation.'
puts '======================================================'
2012-03-23 03:53:25 +08:00
puts
2012-09-29 01:38:59 +08:00
2019-07-30 11:50:46 +08:00
install_homebrew if macos?
2012-10-03 13:45:26 +08:00
install_rvm_binstubs
2012-09-29 01:38:59 +08:00
2021-12-25 12:30:10 +08:00
Rake :: Task [ 'link_files' ] . execute
Rake :: Task [ 'install_prezto' ] . execute
Rake :: Task [ 'install_spacevim' ] . execute
2021-12-25 16:23:07 +08:00
Rake :: Task [ 'install_asdf' ] . execute
2012-01-25 14:13:33 +08:00
2014-07-17 15:11:56 +08:00
install_fonts
2012-09-29 01:38:59 +08:00
2019-07-30 11:50:46 +08:00
install_term_theme if macos?
2013-03-14 19:56:59 +08:00
2014-02-04 00:30:23 +08:00
run_bundle_config
2021-12-25 12:30:10 +08:00
success_msg ( 'installed' )
2012-08-10 02:47:45 +08:00
end
2012-10-25 22:52:18 +08:00
task :install_prezto do
2021-12-25 12:30:10 +08:00
install_prezto if want_to_install? ( 'zsh enhancements & prezto' )
2017-03-12 15:58:58 +08:00
end
2021-12-25 12:30:10 +08:00
desc 'install spacevim and related config files'
2020-02-10 10:56:29 +08:00
task :install_spacevim do
2023-08-29 17:11:19 +08:00
run 'curl -sLf https://spacevim.org/install.sh | sed "s;github.com/SpaceVim;git.bitnet.fun:Gaze;" | bash'
2020-02-10 10:56:29 +08:00
end
2021-12-25 12:30:10 +08:00
desc 'Update spacevim'
2021-03-09 13:50:37 +08:00
task :update_spacevim do
run %(
cd ~ / .SpaceVim
2023-08-29 17:11:19 +08:00
git remote set - url origin git @git . bitnet . fun :Gaze / SpaceVim . git
2021-03-09 13:50:37 +08:00
git pull - - rebase
)
end
2021-12-25 16:23:07 +08:00
desc 'install asdf'
task :install_asdf do
run 'git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.1'
install_files ( Dir . glob ( 'asdf/*' ) ) if want_to_install? ( 'tool version manager asdf' )
run %(
. $HOME / . asdf / asdf . sh
asdf plugin - add kubetail https : / / github . com / janpieper / asdf - kubetail . git
asdf install
)
end
2021-12-25 12:30:10 +08:00
desc 'Prepare necessary components for spacevim/typescript'
2021-07-29 00:24:28 +08:00
task :prepare_spacevim_typescript do
run %(
pip install neovim
npm install - g neovim typescript
)
end
2013-03-16 22:47:54 +08:00
task :update do
2021-12-25 12:30:10 +08:00
Rake :: Task [ 'vundle_migration' ] . execute if needs_migration_to_vundle?
Rake :: Task [ 'install' ] . execute
2020-02-06 19:20:50 +08:00
# TODO: for now, we do the same as install. But it would be nice
# not to clobber zsh files
2012-10-09 12:38:21 +08:00
end
2017-03-12 03:04:40 +08:00
task :sync do
2021-12-25 12:30:10 +08:00
vundle_path = File . join ( 'vim' , 'bundle' , 'vundle' )
2020-02-06 19:20:50 +08:00
unless File . exist? ( vundle_path )
run %(
2017-03-12 03:04:40 +08:00
cd $HOME / . yadr
git clone https : / / github . com / gmarik / vundle . git #{vundle_path}
2020-02-06 19:20:50 +08:00
)
2017-03-12 03:04:40 +08:00
end
end
2012-10-10 14:02:36 +08:00
task :submodule_init do
2021-12-25 12:30:10 +08:00
run %( git submodule update --init --recursive ) unless ENV [ 'SKIP_SUBMODULES' ]
2012-10-10 14:02:36 +08:00
end
2021-12-25 12:30:10 +08:00
desc 'Init and update submodules.'
2012-08-10 02:47:45 +08:00
task :submodules do
2021-12-25 12:30:10 +08:00
unless ENV [ 'SKIP_SUBMODULES' ]
puts '======================================================'
puts 'Downloading YADR submodules...please wait'
puts '======================================================'
2012-10-25 23:09:24 +08:00
2020-02-06 19:20:50 +08:00
run %(
2012-10-25 23:09:24 +08:00
cd $HOME / . yadr
2013-06-07 07:09:18 +08:00
git submodule update - - recursive
2013-04-02 21:12:24 +08:00
git clean - df
2020-02-06 19:20:50 +08:00
)
2012-10-25 23:09:24 +08:00
puts
end
2012-08-10 02:47:45 +08:00
end
2021-12-25 12:30:10 +08:00
task default : 'install'
2012-08-10 02:47:45 +08:00
private
2021-07-29 00:24:28 +08:00
2012-08-10 02:47:45 +08:00
def run ( cmd )
2012-10-09 12:38:21 +08:00
puts " [Running] #{ cmd } "
2021-12-25 12:30:10 +08:00
` #{ cmd } ` unless ENV [ 'DEBUG' ]
2012-08-10 02:47:45 +08:00
end
2014-02-04 00:30:23 +08:00
def number_of_cores
2020-02-06 19:20:50 +08:00
cores = if macos?
2021-12-25 12:30:10 +08:00
run %( sysctl -n hw.ncpu )
else
run %( nproc )
end
2014-02-04 00:30:23 +08:00
puts
cores . to_i
end
def run_bundle_config
2021-12-25 12:30:10 +08:00
return unless system ( 'which bundle' )
2014-03-02 09:40:33 +08:00
2014-02-04 00:30:23 +08:00
bundler_jobs = number_of_cores - 1
2021-12-25 12:30:10 +08:00
puts '======================================================'
puts 'Configuring Bundlers for parallel gem installation'
puts '======================================================'
2020-02-06 19:20:50 +08:00
run %( bundle config --global jobs #{ bundler_jobs } )
2014-02-04 00:30:23 +08:00
puts
end
2012-10-03 13:45:26 +08:00
def install_rvm_binstubs
2021-12-25 12:30:10 +08:00
puts '======================================================'
puts 'Installing RVM Bundler support. Never have to type'
puts 'bundle exec again! Please use bundle --binstubs and RVM'
2012-10-03 13:45:26 +08:00
puts " will automatically use those bins after cd'ing into dir. "
2021-12-25 12:30:10 +08:00
puts '======================================================'
2020-02-06 19:20:50 +08:00
run %( chmod +x $rvm_path/hooks/after_cd_bundler )
2012-10-03 13:45:26 +08:00
puts
end
2012-09-29 01:38:59 +08:00
def install_homebrew
2020-02-06 19:20:50 +08:00
run %( which brew )
unless $CHILD_STATUS . success?
2021-12-25 12:30:10 +08:00
puts '======================================================'
2013-01-27 03:06:01 +08:00
puts " Installing Homebrew, the OSX package manager...If it's "
2021-12-25 12:30:10 +08:00
puts 'already installed, this will do nothing.'
puts '======================================================'
2014-10-19 01:23:16 +08:00
run %{ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" }
2013-01-27 03:06:01 +08:00
end
2013-06-12 21:21:08 +08:00
puts
puts
2021-12-25 12:30:10 +08:00
puts '======================================================'
puts 'Updating Homebrew.'
puts '======================================================'
2020-02-06 19:20:50 +08:00
run %( brew update )
2012-09-29 01:38:59 +08:00
puts
puts
2021-12-25 12:30:10 +08:00
puts '======================================================'
puts 'Installing Homebrew packages...There may be some warnings.'
puts '======================================================'
2020-02-06 19:20:50 +08:00
run %( brew install zsh ctags git hub tmux reattach-to-user-namespace ripgrep ghi )
run %( brew install macvim --with-override-system-vim --with-lua --with-luajit )
2012-09-29 01:38:59 +08:00
puts
puts
end
def install_fonts
2021-12-25 12:30:10 +08:00
puts '======================================================'
puts 'Installing patched fonts for Powerline/Lightline.'
puts '======================================================'
2020-02-06 19:20:50 +08:00
run %( cp -f $HOME/.yadr/fonts/* $HOME/Library/Fonts ) if macos?
2021-12-25 12:30:10 +08:00
run %( mkdir -p ~/.fonts && cp ~/.yadr/fonts/* ~/.fonts && fc-cache -vf ~/.fonts ) if linux?
2012-09-29 01:38:59 +08:00
puts
end
2013-03-14 19:56:59 +08:00
def install_term_theme
2021-12-25 12:30:10 +08:00
puts '======================================================'
puts 'Installing iTerm2 solarized theme.'
puts '======================================================'
2020-02-06 19:20:50 +08:00
run %( /usr/libexec/PlistBuddy -c "Add :'Custom Color Presets':'Solarized Light' dict" ~/Library/Preferences/com.googlecode.iterm2.plist )
run %( /usr/libexec/PlistBuddy -c "Merge 'iTerm2/Solarized Light.itermcolors' :'Custom Color Presets':'Solarized Light'" ~/Library/Preferences/com.googlecode.iterm2.plist )
run %( /usr/libexec/PlistBuddy -c "Add :'Custom Color Presets':'Solarized Dark' dict" ~/Library/Preferences/com.googlecode.iterm2.plist )
run %( /usr/libexec/PlistBuddy -c "Merge 'iTerm2/Solarized Dark.itermcolors' :'Custom Color Presets':'Solarized Dark'" ~/Library/Preferences/com.googlecode.iterm2.plist )
2013-03-14 19:56:59 +08:00
2013-04-07 23:35:32 +08:00
# If iTerm2 is not installed or has never run, we can't autoinstall the profile since the plist is not there
2021-12-25 12:30:10 +08:00
unless File . exist? ( File . join ( ENV [ 'HOME' ] , '/Library/Preferences/com.googlecode.iterm2.plist' ) )
puts '======================================================'
puts 'To make sure your profile is using the solarized theme'
puts 'Please check your settings under:'
puts 'Preferences> Profiles> [your profile]> Colors> Load Preset..'
puts '======================================================'
2013-04-07 23:35:32 +08:00
return
end
# Ask the user which theme he wants to install
2021-12-25 12:30:10 +08:00
message = 'Which theme would you like to apply to your iTerm2 profile?'
2013-04-07 23:35:32 +08:00
color_scheme = ask message , iTerm_available_themes
2014-09-22 13:51:58 +08:00
2021-12-25 12:30:10 +08:00
return if color_scheme == 'None'
2014-09-22 13:51:58 +08:00
2021-12-25 12:30:10 +08:00
color_scheme_file = File . join ( 'iTerm2' , " #{ color_scheme } .itermcolors " )
2013-04-07 23:35:32 +08:00
# Ask the user on which profile he wants to install the theme
profiles = iTerm_profile_list
2021-12-25 12:30:10 +08:00
message = " I've found #{ profiles . size } #{ profiles . size > 1 ? 'profiles' : 'profile' } on your iTerm2 configuration, which one would you like to apply the Solarized theme to? "
profiles << 'All'
2013-04-07 23:35:32 +08:00
selected = ask message , profiles
2013-12-16 06:18:36 +08:00
2021-12-25 12:30:10 +08:00
if selected == 'All'
2020-02-06 19:20:50 +08:00
( profiles . size - 1 ) . times { | idx | apply_theme_to_iterm_profile_idx idx , color_scheme_file }
2013-04-07 23:35:32 +08:00
else
apply_theme_to_iterm_profile_idx profiles . index ( selected ) , color_scheme_file
end
end
def iTerm_available_themes
2021-12-25 12:30:10 +08:00
Dir [ 'iTerm2/*.itermcolors' ] . map { | value | File . basename ( value , '.itermcolors' ) } << 'None'
2013-04-07 23:35:32 +08:00
end
def iTerm_profile_list
2020-02-06 19:20:50 +08:00
profiles = [ ]
2013-04-07 23:35:32 +08:00
begin
2020-02-06 19:20:50 +08:00
profiles << ` /usr/libexec/PlistBuddy -c "Print :'New Bookmarks': #{ profiles . size } :Name" ~/Library/Preferences/com.googlecode.iterm2.plist 2>/dev/null `
end while $CHILD_STATUS . exitstatus == 0
2013-04-07 23:35:32 +08:00
profiles . pop
profiles
end
def ask ( message , values )
puts message
while true
2020-02-06 19:20:50 +08:00
values . each_with_index { | val , idx | puts " #{ idx + 1 } . #{ val } " }
2013-04-07 23:35:32 +08:00
selection = STDIN . gets . chomp
2020-02-06 19:20:50 +08:00
if ( begin
2021-07-29 00:24:28 +08:00
Float ( selection ) . nil?
rescue StandardError
true
end ) || selection . to_i < 0 || selection . to_i > values . size + 1
2013-04-07 23:35:32 +08:00
puts " ERROR: Invalid selection. \n \n "
else
break
end
2013-12-16 06:18:36 +08:00
end
2020-02-06 19:20:50 +08:00
selection = selection . to_i - 1
2013-04-07 23:35:32 +08:00
values [ selection ]
2013-03-14 19:56:59 +08:00
end
2012-09-27 01:23:39 +08:00
def install_prezto
2012-10-25 23:09:24 +08:00
puts
2021-12-25 12:30:10 +08:00
puts 'Installing Prezto (ZSH Enhancements)...'
2012-09-27 01:23:39 +08:00
2020-02-06 19:20:50 +08:00
run %( ln -nfs "$HOME/.yadr/zsh/prezto" "${ZDOTDIR:-$HOME}/.zprezto" )
2012-09-27 01:23:39 +08:00
2014-03-14 10:30:47 +08:00
# The prezto runcoms are only going to be installed if zprezto has never been installed
2021-12-25 12:30:10 +08:00
install_files ( Dir . glob ( 'zsh/prezto/runcoms/z*' ) , :symlink )
2012-09-27 01:23:39 +08:00
2012-10-25 23:09:24 +08:00
puts
2012-10-25 22:52:18 +08:00
puts " Overriding prezto ~/.zpreztorc with YADR's zpreztorc to enable additional modules... "
2021-12-25 12:30:10 +08:00
install_files ( Dir . glob ( 'zsh/prezto-override/z*' ) , :symlink )
2012-10-25 22:52:18 +08:00
2012-10-25 23:09:24 +08:00
puts
2021-12-25 12:30:10 +08:00
puts 'Creating directories for your customizations'
2020-02-06 19:20:50 +08:00
run %( mkdir -p $HOME/.zsh.before )
run %( mkdir -p $HOME/.zsh.after )
run %( mkdir -p $HOME/.zsh.prompts )
2012-11-26 23:41:35 +08:00
2021-12-25 12:30:10 +08:00
if ( ENV [ 'SHELL' ] ) . to_s . include? 'zsh'
puts 'Zsh is already configured as your shell of choice. Restart your session to load the new settings'
2013-03-21 21:02:25 +08:00
else
2021-12-25 12:30:10 +08:00
puts 'Setting zsh as your default shell'
if File . exist? ( '/usr/local/bin/zsh' )
if File . readlines ( '/private/etc/shells' ) . grep ( '/usr/local/bin/zsh' ) . empty?
puts 'Adding zsh to standard shell list'
2020-02-06 19:20:50 +08:00
run %( echo "/usr/local/bin/zsh" | sudo tee -a /private/etc/shells )
2013-12-20 05:00:57 +08:00
end
2020-02-06 19:20:50 +08:00
run %( chsh -s /usr/local/bin/zsh )
2013-12-17 07:27:06 +08:00
else
2020-02-06 19:20:50 +08:00
run %( chsh -s /bin/zsh )
2013-12-17 07:27:06 +08:00
end
2013-03-21 21:02:25 +08:00
end
2012-09-27 01:23:39 +08:00
end
2020-02-06 19:20:50 +08:00
def want_to_install? ( section )
2021-12-25 12:30:10 +08:00
if ENV [ 'ASK' ] == 'true'
2012-10-25 23:09:24 +08:00
puts " Would you like to install configuration files for: #{ section } ? [y]es, [n]o "
2021-12-25 12:30:10 +08:00
STDIN . gets . chomp == 'y'
2012-10-25 23:09:24 +08:00
else
true
end
2012-08-10 02:47:45 +08:00
end
2015-07-27 10:27:11 +08:00
def install_files ( files , method = :symlink )
2012-08-10 02:47:45 +08:00
files . each do | f |
2021-12-25 12:30:10 +08:00
file = f . split ( '/' ) . last
source = " #{ ENV [ 'PWD' ] } / #{ f } "
target = " #{ ENV [ 'HOME' ] } /. #{ file } "
2012-01-25 14:13:33 +08:00
2012-10-25 23:09:24 +08:00
puts " ====================== #{ file } ============================== "
puts " Source: #{ source } "
puts " Target: #{ target } "
2012-01-25 14:13:33 +08:00
2020-02-06 19:20:50 +08:00
if File . exist? ( target ) && ( ! File . symlink? ( target ) || ( File . symlink? ( target ) && File . readlink ( target ) != source ) )
2012-10-25 23:09:24 +08:00
puts " [Overwriting] #{ target } ...leaving original at #{ target } .backup... "
2020-02-06 19:20:50 +08:00
run %( mv "$HOME/. #{ file } " "$HOME/. #{ file } .backup" )
2012-01-25 14:13:33 +08:00
end
2012-08-10 02:47:45 +08:00
if method == :symlink
2020-02-06 19:20:50 +08:00
run %( ln -nfs " #{ source } " " #{ target } " )
2012-08-10 02:47:45 +08:00
else
2020-02-06 19:20:50 +08:00
run %( cp -f " #{ source } " " #{ target } " )
2012-08-10 02:47:45 +08:00
end
2012-01-25 14:13:33 +08:00
2012-08-10 02:47:45 +08:00
# Temporary solution until we find a way to allow customization
2012-09-27 01:23:39 +08:00
# This modifies zshrc to load all of yadr's zsh extensions.
# Eventually yadr's zsh extensions should be ported to prezto modules.
2021-12-25 12:30:10 +08:00
source_config_code = 'for config_file ($HOME/.yadr/zsh/*.zsh) source $config_file'
if file == 'zshrc'
File . open ( target , 'a+' ) do | zshrc |
zshrc . puts ( source_config_code ) if zshrc . readlines . grep ( / #{ Regexp . escape ( source_config_code ) } / ) . empty?
2012-08-10 02:47:45 +08:00
end
end
2012-01-25 14:13:33 +08:00
2021-12-25 12:30:10 +08:00
puts '=========================================================='
2012-10-25 23:09:24 +08:00
puts
2012-08-10 02:47:45 +08:00
end
2012-01-25 14:13:33 +08:00
end
2013-03-16 22:47:54 +08:00
def needs_migration_to_vundle?
2021-12-25 12:30:10 +08:00
File . exist? File . join ( 'vim' , 'bundle' , 'tpope-vim-pathogen' )
2013-03-16 22:47:54 +08:00
end
2013-04-07 23:35:32 +08:00
def apply_theme_to_iterm_profile_idx ( index , color_scheme_path )
2020-02-06 19:20:50 +08:00
values = [ ]
2013-04-07 23:35:32 +08:00
16 . times { | i | values << " Ansi #{ i } Color " }
2021-12-25 12:30:10 +08:00
values << [ 'Background Color' , 'Bold Color' , 'Cursor Color' , 'Cursor Text Color' , 'Foreground Color' ,
'Selected Text Color' , 'Selection Color' ]
values . flatten . each do | entry |
run %( /usr/libexec/PlistBuddy -c "Delete :'New Bookmarks': #{ index } :' #{ entry } '" ~/Library/Preferences/com.googlecode.iterm2.plist )
end
2013-04-07 23:35:32 +08:00
2020-02-06 19:20:50 +08:00
run %( /usr/libexec/PlistBuddy -c "Merge ' #{ color_scheme_path } ' :'New Bookmarks': #{ index } " ~/Library/Preferences/com.googlecode.iterm2.plist )
run %( defaults read com.googlecode.iterm2 )
2013-04-07 23:35:32 +08:00
end
2013-03-16 22:47:54 +08:00
2012-01-25 14:13:33 +08:00
def success_msg ( action )
2021-12-25 12:30:10 +08:00
puts ''
puts ' _ _ _ '
puts ' | | | | | | '
puts ' | |___| |_____ __| | ____ '
puts ' |_____ (____ |/ _ |/ ___) '
puts ' _____| / ___ ( (_| | | '
2012-01-25 14:13:33 +08:00
puts " (_______ \ _____| \ ____|_| "
2021-12-25 12:30:10 +08:00
puts ''
2012-01-25 14:13:33 +08:00
puts " YADR has been #{ action } . Please restart your terminal and vim. "
end