Class: Hookup

Inherits:
Object
  • Object
show all
Defined in:
lib/hookup.rb

Defined Under Namespace

Classes: Error, Failure, PostCheckout

Constant Summary collapse

EMPTY_DIR =
'4b825dc642cb6eb9a060e54bf8d69288fbee4904'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(*argv) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/hookup.rb', line 11

def self.run(*argv)
  new.run(*argv)
rescue Failure => e
  puts e
  exit 1
rescue Error => e
  puts e
  exit
end

Instance Method Details

#bundler?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/hookup.rb', line 44

def bundler?
  !!ENV['BUNDLE_GEMFILE']
end

#git_dirObject



36
37
38
39
40
41
42
# File 'lib/hookup.rb', line 36

def git_dir
  unless @git_dir
    @git_dir = %x{git rev-parse --git-dir}.chomp
    raise Error unless $?.success?
  end
  @git_dir
end

#info_attributes_fileObject



56
57
58
# File 'lib/hookup.rb', line 56

def info_attributes_file
  File.join(git_dir, 'info', 'attributes')
end

#installObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hookup.rb', line 60

def install
  append(post_checkout_file, 0777) do |body, f|
    f.puts "#!/bin/bash" unless body
    f.puts make_command(%(hookup post-checkout "$@")) if body !~ /hookup/
  end

  append(info_attributes_file) do |body, f|
    map = 'db/schema.rb merge=railsschema'
    f.puts map unless body.to_s.include?(map)
  end

  system 'git', 'config', 'merge.railsschema.driver', make_command('hookup resolve-schema %A %O %B %L')

  puts "Hooked up!"
end

#make_command(command) ⇒ Object



48
49
50
# File 'lib/hookup.rb', line 48

def make_command(command)
  bundler? ? command.insert(0, "bundle exec ") : command
end

#post_checkout(*args) ⇒ Object



99
100
101
# File 'lib/hookup.rb', line 99

def post_checkout(*args)
  PostCheckout.new(ENV, *args).run
end

#post_checkout_fileObject



52
53
54
# File 'lib/hookup.rb', line 52

def post_checkout_file
  File.join(git_dir, 'hooks', 'post-checkout')
end

#removeObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hookup.rb', line 76

def remove
  body = IO.readlines(post_checkout_file)
  body.reject! { |item| item =~ /hookup/ }
  File.open(post_checkout_file, 'w') { |file| file.puts body.join }

  body = IO.readlines(info_attributes_file)
  body.reject! { |item| item =~ /railsschema/ }
  File.open(info_attributes_file, 'w') { |file| file.puts body.join }

  system 'git', 'config', '--unset', 'merge.railsschema.driver'

  puts "Hookup removed!"
end

#resolve_schema(a, o, b, marker_size = 7) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/hookup.rb', line 249

def resolve_schema(a, o, b, marker_size = 7)
  system 'git', 'merge-file', "--marker-size=#{marker_size}", a, o, b
  body = File.read(a)
  resolve_schema_version body, ":version =>"
  resolve_schema_version body, "version:"
  File.open(a, 'w') { |f| f.write(body) }
  if body.include?('<' * marker_size.to_i)
    raise Failure, 'Failed to automatically resolve schema conflict'
  end
end

#resolve_schema_version(body, version) ⇒ Object



260
261
262
263
264
265
# File 'lib/hookup.rb', line 260

def resolve_schema_version(body, version)
  asd = "ActiveRecord::Schema.define"
  body.sub!(/^<+ .*\n#{asd}\(#{version} ([0-9_]+)\) do\n=+\n#{asd}\(#{version} ([0-9_]+)\) do\n>+ .*/) do
    "#{asd}(#{version} #{[$1, $2].max}) do"
  end
end

#run(*argv) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hookup.rb', line 21

def run(*argv)
  if argv.empty?
    install
  else
    command = argv.shift
    begin
      send(command.tr('-', '_'), *argv)
    rescue NoMethodError
      raise Error, "Unknown command #{command}"
    rescue ArgumentError
      raise Error, "Invalid arguments for #{command}"
    end
  end
end