Class: MultiRepo::AddCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/multirepo/commands/add-command.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#ensure_in_work_tree, #ensure_multirepo_enabled, #ensure_multirepo_tracked, #install_hooks, #multirepo_enabled_dependencies, report_error, #uninstall_hooks, #update_gitconfig

Constructor Details

#initialize(argv) ⇒ AddCommand

Returns a new instance of AddCommand.



13
14
15
16
# File 'lib/multirepo/commands/add-command.rb', line 13

def initialize(argv)
  @path = with_trailing_slash(argv.shift_argument)
  super
end

Class Method Details

.optionsObject



9
10
11
# File 'lib/multirepo/commands/add-command.rb', line 9

def self.options
  [['<path>', 'The relative path to the new dependency (e.g. ../MyNewDependency)']].concat(super)
end

Instance Method Details

#ensure_repo_validObject



39
40
41
42
43
# File 'lib/multirepo/commands/add-command.rb', line 39

def ensure_repo_valid
  fail MultiRepoException, "The provided path is not a direct sibling of the main repository" unless validate_is_sibling_repo(@path)
  fail MultiRepoException, "There is no folder at path '#{@path}'" unless Dir.exist?(@path)
  fail MultiRepoException, "'#{@path}' is not a repository" unless Repo.new(@path).exists?
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/multirepo/commands/add-command.rb', line 23

def run
  ensure_in_work_tree
  ensure_multirepo_enabled
  ensure_repo_valid
  
  config_file = ConfigFile.new(".")
  entry = ConfigEntry.new(Repo.new(@path))
  
  if config_file.entry_exists?(entry)
    Console.log_info("There is already an entry for '#{@path}' in the .multirepo file")
  else
    config_file.add_entry(entry)
    Console.log_step("Added '#{@path}' to the .multirepo file")
  end
end

#validate!Object



18
19
20
21
# File 'lib/multirepo/commands/add-command.rb', line 18

def validate!
  super
  help! "You must specify a repository to add as a dependency" unless @path
end

#validate_is_sibling_repo(path) ⇒ Object



45
46
47
48
49
# File 'lib/multirepo/commands/add-command.rb', line 45

def validate_is_sibling_repo(path)
  parent_dir = File.expand_path("..")
  path = File.expand_path("..", path)
  return parent_dir == path
end

#with_trailing_slash(path) ⇒ Object



51
52
53
# File 'lib/multirepo/commands/add-command.rb', line 51

def with_trailing_slash(path)
  if path.end_with?("/") then path else path + "/" end
end