Class: Gitcopier::CommandLine
- Inherits:
-
Object
- Object
- Gitcopier::CommandLine
- Defined in:
- lib/gitcopier/command_line.rb
Class Method Summary collapse
- .execute(options) ⇒ Object
- .exist?(path) ⇒ Boolean
- .generate_git_callback(from, to) ⇒ Object
- .integrate(options) ⇒ Object
- .integration_exist?(integrations, options) ⇒ Boolean
- .load_integration ⇒ Object
- .save_integrations(integrations) ⇒ Object
- .showall(options) ⇒ Object
- .under_git_control?(path) ⇒ Boolean
-
.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.
Class Method Details
.execute(options) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/gitcopier/command_line.rb', line 111 def self.execute() if [:command].nil? puts "No command specified.".colorize(:red) exit(0) end send([:command], ) end |
.exist?(path) ⇒ 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() integrations = load_integration return unless = validate() if integration_exist?(integrations, ) puts "You already have this integration.".colorize(:yellow) return end integrations << { source: [:from], des: [:to] } generate_git_callback( [:from], [:to]) ensure save_integrations(integrations) end |
.integration_exist?(integrations, options) ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/gitcopier/command_line.rb', line 35 def self.integration_exist?(integrations, ) integrations.find_index do |integration| (integration['source'] == [:from] && integration['des'] == [:to]) end != nil end |
.load_integration ⇒ Object
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() 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
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) = opt.clone if [:from].nil? || [:to].nil? puts "--from and --to options must be both specified".colorize(:red) return nil end if !under_git_control?([:from]) puts "--from option must be under git control".colorize(:red) return nil end if !exist?([:to]) puts "--to option must be an existing directory".colorize(:red) end [:from] = [:from][0..-2] if [:from][-1] == "/" [:to] = [:to][0..-2] if [:to][-1] == "/" return end |