Class: Dit
- Inherits:
-
Object
- Object
- Dit
- Defined in:
- lib/dit.rb
Overview
This is the class where all the dit work is done. The thor class is basically a very thin layer on top of this that just calls its methods directly. This is because the hooks are not running through the Thor object, but also referencing these methods.
Class Method Summary collapse
- .clean_home ⇒ Object
- .detect_hook(hook) ⇒ Object
- .exit_if_windows ⇒ Object
- .get_roots(list) ⇒ Object
- .hook ⇒ Object
- .init ⇒ Object
- .make_dit ⇒ Object
- .make_ruby_enforcer ⇒ Object
- .prompt_for_overwrite(a, b) ⇒ Object
- .prompt_for_symlink_all ⇒ Object
- .symlink(a, b) ⇒ Object
- .symlink_all ⇒ Object
- .symlink_list(list) ⇒ Object
- .symlink_unlinked ⇒ Object
- .version ⇒ Object
- .write_hook(hook_file, do_append) ⇒ Object
Class Method Details
.clean_home ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/dit.rb', line 182 def self.clean_home Dir.chdir(Dir.home) do existing_dotfiles = Dir.glob('.*') existing_dotfiles.each do |f| next if f == '.' || f == '..' if File.symlink?(f) f_abs = File.readlink(f) File.delete(f) unless File.exist?(f_abs) end end end end |
.detect_hook(hook) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/dit.rb', line 123 def self.detect_hook(hook) return [false, false] unless File.exist?(hook) cannot_hook, append_to_hook = false if `cat #{hook}`.include?('./.git/hooks/dit') puts 'Dit hook already installed.' cannot_hook = true elsif `cat #{hook}`.include?('#!/usr/bin/env bash') puts "You have #{hook} hooks already that use bash, so we'll " \ 'append ourselves to the file.' append_to_hook = true else puts "You have #{hook} hooks that use some foreign language, " \ "so we won't interfere, but we can't hook in there." cannot_hook = true end [append_to_hook, cannot_hook] end |
.exit_if_windows ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/dit.rb', line 28 def self.exit_if_windows if OS.windows? puts 'This is a windows system, and dit does not support windows.' puts 'See vulpino/dit issue #1 if you have a potential solution.' exit 1 end end |
.get_roots(list) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dit.rb', line 71 def self.get_roots(list) root_list = Set[] list.each do |f| f.strip! root = f.split('/')[0] root ||= f root_list |= Set[root] end root_list.delete?('') %w(.gitignore README.md README).each { |i| root_list.delete(i) } root_list end |
.hook ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dit.rb', line 44 def self.hook Dir.chdir(File.join('.git', 'hooks')) do # The following check for the existence of post-commit or post-merge hooks # and will not interfere with them if they exist and do not use bash. append_to_post_commit, cannot_post_commit = detect_hook 'post-commit' append_to_post_merge, cannot_post_merge = detect_hook 'post-merge' write_hook('post-commit', append_to_post_commit) unless cannot_post_commit write_hook('post-merge', append_to_post_merge) unless cannot_post_merge make_dit make_ruby_enforcer # Make sure they're executable FileUtils.chmod '+x', %w(post-commit post-merge dit force-ruby) end end |
.init ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dit.rb', line 13 def self.init exit_if_windows if Dir.exist?('.git') symlink_all if prompt_for_symlink_all else Git.init(Dir.getwd) puts "Initialized empty Git repository in #{File.join(Dir.getwd, '.git')}" end hook puts 'Dit was successfully hooked into .git/hooks.' end |
.make_dit ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/dit.rb', line 151 def self.make_dit File.open('dit', 'a') do |f| f.puts '#!/usr/bin/env ./.git/hooks/force-ruby' f.puts "require 'dit'" f.puts 'Dit.symlink_unlinked' end end |
.make_ruby_enforcer ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dit.rb', line 159 def self.make_ruby_enforcer # The following lines are because git hooks do this weird thing # where they prepend /usr/bin to the path and a bunch of other stuff # meaning git hooks will use /usr/bin/ruby instead of any ruby # from rbenv or rvm or chruby, so we make a script forcing the hook # to use our ruby ruby_path = `which ruby` if ruby_path != '/usr/bin/ruby' ruby_folder = File.dirname(ruby_path) File.open('force-ruby', 'a') do |f| f.puts '#!/usr/bin/env bash' f.puts 'set -e' f.puts 'PATH=#{ruby_folder}:$PATH' f.puts "exec ruby \"$@\"" end else File.open('force-ruby', 'a') do |f| f.puts '#!/usr/bin/env bash' f.puts "exec ruby \"$@\"" end end end |
.prompt_for_overwrite(a, b) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/dit.rb', line 103 def self.prompt_for_overwrite(a, b) return false if @never (FileUtils.rm(b); return true) if @always # just this once; puts "#{b} conflicts with #{a}. Remove #{b}? [y/n/a/s]" puts "To always overwrite, type \"A\". To never overwrite, type \"S\"" response = STDIN.gets.upcase case response when 'Y' FileUtils.rm(b) return true when 'A' @always = true FileUtils.rm(b) return true when 'S' @never = true end false end |
.prompt_for_symlink_all ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/dit.rb', line 36 def self.prompt_for_symlink_all puts 'Dit has detected an existing git repo, and will initialize it to ' \ 'populate your ~ directory with symlinks.' puts 'Please confirm this by typing y, or anything else to cancel.' response = STDIN.gets.chomp.upcase response == 'Y' end |
.symlink(a, b) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/dit.rb', line 93 def self.symlink(a, b) if File.exist?(b) return if File.symlink?(b) && File.readlink(b).include?(Dir.getwd) return unless prompt_for_overwrite a, b end File.symlink(a, b) rescue puts "Failed to symlink #{a} to #{b}" end |
.symlink_all ⇒ Object
88 89 90 91 |
# File 'lib/dit.rb', line 88 def self.symlink_all current_branch = `git rev-parse --abbrev-ref HEAD`.chomp symlink_list `git ls-tree -r #{current_branch} --name-only`.split("\n") end |
.symlink_list(list) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/dit.rb', line 62 def self.symlink_list(list) list = get_roots list list.each do |f| wd_f = File. f home_f = File.(f).gsub(Dir.getwd, Dir.home) symlink wd_f, home_f end end |
.symlink_unlinked ⇒ Object
84 85 86 |
# File 'lib/dit.rb', line 84 def self.symlink_unlinked symlink_list `git show --pretty="format:" --name-only HEAD`.split("\n") end |
.version ⇒ Object
195 196 197 |
# File 'lib/dit.rb', line 195 def self.version '0.4' end |
.write_hook(hook_file, do_append) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/dit.rb', line 144 def self.write_hook(hook_file, do_append) File.open(hook_file, 'a') do |f| f.puts '#!/usr/bin/env bash' unless do_append f.puts '( exec ./.git/hooks/dit )' end end |