Class: EPC::Command::AttachLibraryCommand

Inherits:
BaseCommand show all
Defined in:
lib/epc/command/attach_library_command.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::GIVEUP_TICKS, BaseCommand::SLEEP_TIME, BaseCommand::TICKER_TICKS

Instance Attribute Summary

Attributes inherited from BaseCommand

#client, #options

Instance Method Summary collapse

Methods inherited from BaseCommand

#go, inherited, #initialize, required_options

Methods included from PersistentAttributes

#auth_token, #caller_id, #target_url

Constructor Details

This class inherits a constructor from EPC::Command::BaseCommand

Instance Method Details

#execute(name = nil, version = nil, group = nil) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/epc/command/attach_library_command.rb', line 6

def execute(name = nil, version = nil, group = nil)
  path = File.expand_path(".")
  
  library_ids = []

  if @options[:file].present?
    libs = EPC::Config.read_content_as_json(@options[:file])
    library_ids = libs.map{|h| h.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}}
    library_ids = retrieve_libraries(library_ids.compact)
  elsif !@options[:pom].nil?
    library_ids = parse_pom(@options[:pom])
  else
    raise FatalError, "You must specify the library to be attached" if name.nil?
  end

  solution_id, solution_name = infer_solution_context(@options[:solution_name], path) 
  project_id, project_name = infer_project_context(@options[:project_name], path, solution_id)

  raise FatalError, "Project could not be determined" if project_id.nil?

  if library_ids.empty?
    if numeric?(name)
      library_ids << name.to_i
    else
      library_ids = retrieve_libraries([{:name => name, :library_version => version, :group => group}])
    end
  end

  raise InputError, "Library incorrectly specified" if library_ids.nil?

  status = nil

  library_ids.each do |library_id|
    status, response, headers = client.post(EPC::Config::PROJECTS_PATH + "/#{project_id}/attach_library/#{library_id}")

    if status.successful?
      say("Library [#{library_id}] attached to project.")
    else
      say("Failed to attach library [#{library_id}]: [#{response[:message]}]")
    end
  end

  return status

end

#parse_pom(pom_file) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/epc/command/attach_library_command.rb', line 52

def parse_pom(pom_file)
  begin
    pom_file = File.read(File.expand_path(pom_file))
    xml = REXML::Document.new(pom_file)
  rescue REXML::ParseException => ex
    raise FatalError, "Invalid xml supplied. Line: #{ex.line.to_s}. Context: #{ex.context.to_s}"
  rescue Exception => ex
    raise FatalError, ex.to_s
  end

  libraries = []
  xml.elements[1].elements.each('dependencies/dependency') do |element|
    lib = {}
    element.each_child do |child|
      next if child.is_a? REXML::Text
      lib[child.name] = child.text
    end
    libraries << lib
  end
  libraries

  libraries.each do |lib|
    lib[:name] = lib['artifactId']
    lib[:library_version] = lib['version']
    lib[:group] = lib['groupId']
  end
  retrieve_libraries(libraries)
end