Class: Gitcopier::CommandLine

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

Class Method Summary collapse

Class Method Details

.execute(options) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/gitcopier/command_line.rb', line 111

def self.execute(options)
  if options[:command].nil?
    puts "No command specified.".colorize(:red)
    exit(0)
  end
  send(options[:command], options)
end

.exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/gitcopier/command_line.rb', line 88

def self.exist?(path)
  File.directory? path
end

.generate_git_callback(from, to) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitcopier/command_line.rb', line 42

def self.generate_git_callback(from, to)
  des = File.join from, ".git", "hooks", "post-merge"
  puts "Generating script to #{des}.".colorize(:green)
  script = <<-SCRIPT
#!/usr/bin/env ruby
#
# This script will ask to copy changed file after the merge
#
# User's decisions on coping the files will be recorded to not to ask next time
# User can reconfigure the decision later by modifing the record
# User can apply a decision on the whole directory
#

require 'gitcopier'

integrator = Gitcopier::Integrator.new(
  "#{to}", "#{from}")
integrator.integrate_all_changes
  SCRIPT
  File.write(des, script)
  FileUtils.chmod "+x", des
  puts "Done."
end

.integrate(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitcopier/command_line.rb', line 17

def self.integrate(options)
  integrations = load_integration
  return unless validated_options = validate(options)
  if integration_exist?(integrations, validated_options)
    puts "You already have this integration.".colorize(:yellow)
    return
  end
  integrations << {
    source: validated_options[:from],
    des: validated_options[:to]
  }
  generate_git_callback(
    validated_options[:from],
    validated_options[:to])
ensure
  save_integrations(integrations)
end

.integration_exist?(integrations, options) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/gitcopier/command_line.rb', line 35

def self.integration_exist?(integrations, options)
  integrations.find_index do |integration|
    (integration['source'] == options[:from] &&
     integration['des'] == options[:to])
  end != nil
end

.load_integrationObject



101
102
103
104
# File 'lib/gitcopier/command_line.rb', line 101

def self.load_integration
  file_path = File.join(Dir.home, Gitcopier::INTEGRATION_FILE_NAME)
  JSON.parse(File.read(file_path)) rescue []
end

.save_integrations(integrations) ⇒ Object



106
107
108
109
# File 'lib/gitcopier/command_line.rb', line 106

def self.save_integrations(integrations)
  file_path = File.join(Dir.home, Gitcopier::INTEGRATION_FILE_NAME)
  File.write(file_path, JSON.pretty_generate(integrations))
end

.showall(options) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/gitcopier/command_line.rb', line 7

def self.showall(options)
  integrations = load_integration
  puts "You have #{integrations.size} integration.".colorize(:yellow)
  integrations.each do |int|
    source = int['source'].colorize(:green)
    des = int['des'].colorize(:green)
    puts "Integration from #{source} to #{des}"
  end
end

.under_git_control?(path) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
# File 'lib/gitcopier/command_line.rb', line 92

def self.under_git_control?(path)
  return false unless self.exist?(path)
  pwd = Dir.pwd
  Dir.chdir(path)
  git = IO.popen(['git', 'rev-parse', '--is-inside-work-tree']).read.chomp
  Dir.chdir(pwd)
  return git == "true"
end

.validate(opt) ⇒ Object

–from and –to options must be both specified –from must be an existing directory and is under git control –to must be an exising directory remove trailing / from –from and –to



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitcopier/command_line.rb', line 70

def self.validate(opt)
  options = opt.clone
  if options[:from].nil? || options[:to].nil?
    puts "--from and --to options must be both specified".colorize(:red)
    return nil
  end
  if !under_git_control?(options[:from])
    puts "--from option must be under git control".colorize(:red)
    return nil
  end
  if !exist?(options[:to])
    puts "--to option must be an existing directory".colorize(:red)
  end
  options[:from] = options[:from][0..-2] if options[:from][-1] == "/"
  options[:to] = options[:to][0..-2] if options[:to][-1] == "/"
  return options
end