Class: NexusArtifact

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_artifact.rb,
lib/nexus_artifact/version.rb

Constant Summary collapse

PATTERN_MAP =
{
  '%v' => :ver,
  '%e' => :ext,
}
VERSION =
"2.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, base_path, pattern) ⇒ NexusArtifact

Returns a new instance of NexusArtifact.



27
28
29
30
31
32
# File 'lib/nexus_artifact.rb', line 27

def initialize(base_uri, base_path, pattern)
  @base_uri = base_uri
  @base_href = "#{base_uri}#{base_path}"
  @pattern = pattern
  @agent = Mechanize.new
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



25
26
27
# File 'lib/nexus_artifact.rb', line 25

def agent
  @agent
end

#base_hrefObject (readonly)

Returns the value of attribute base_href.



25
26
27
# File 'lib/nexus_artifact.rb', line 25

def base_href
  @base_href
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



25
26
27
# File 'lib/nexus_artifact.rb', line 25

def base_uri
  @base_uri
end

#patternObject (readonly)

Returns the value of attribute pattern.



25
26
27
# File 'lib/nexus_artifact.rb', line 25

def pattern
  @pattern
end

Class Method Details

.instanceObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/nexus_artifact.rb', line 14

def self.instance
  @instance ||= begin
    yml = YAML.load File.read('nexus.yml')
    obj = new yml[:uri], yml[:path], yml[:artifact]
    if yml[:user] && yml[:pass]
      obj.auth yml[:user], yml[:pass]
    end
    obj
  end
end

Instance Method Details

#auth(user, pass) ⇒ Object



34
35
36
37
# File 'lib/nexus_artifact.rb', line 34

def auth(user, pass)
  agent.add_auth(base_uri, user, pass)
  self
end

#builds(base) ⇒ Object



46
47
48
49
50
# File 'lib/nexus_artifact.rb', line 46

def builds(base)
  versions.map do |v|
    $1.to_i if v.start_with?(base) && v[base.size .. -1] =~ /\A\.(\d+)\Z/
  end.compact.sort
end

#get(file, data) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/nexus_artifact.rb', line 57

def get(file, data)
  path = path(data)
  sha1_expected = call(:get_file, path + '.sha1').to_s.strip
  download(path, file)
  sha1_actual = Digest::SHA1.file(file).hexdigest
  if sha1_expected != sha1_actual
    raise "SHA1 mismatch for #{file}.  Expected #{sha1_expected.inspect} but got #{sha1_actual.inspect}."
  end
end

#next_version(base) ⇒ Object



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

def next_version(base)
  max = builds(base).max || -1
  "#{base}.#{max + 1}"
end

#publish(file, data) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nexus_artifact.rb', line 67

def publish(file, data)
  path = path(data)
  uploads = [
    [path, File.read(file)],
    [path + '.sha1', Digest::SHA1.file(file).hexdigest],
    ([path + '.git', data[:git]] if data[:git]),
  ]
  uploads.each do |path, contents|
    raise "#{path} already exists" if call(:head, path)
  end
  uploads.each do |path, contents|
    call(:put, path, contents)
  end
  nil
end

#versionsObject



39
40
41
42
43
44
# File 'lib/nexus_artifact.rb', line 39

def versions
  pg = call(:get, '') or return []
  pg.links.map do |link|
    link.text.gsub('/', '') if link.href.start_with? base_href
  end.compact
end