nexus_artifact 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.sw?
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David McCullars
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # NexusArtifact
2
+
3
+ Simple Ruby gem to download/publish arbitrary binary file from/to Nexus server
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nexus_artifact'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nexus_artifact
18
+
19
+ ## Usage
20
+
21
+ Create a nexus.yml file specifying location of artifact in nexus server:
22
+
23
+ ---
24
+ :uri: http://my-maven-server:8081
25
+ :path: /nexus/content/repositories/some-repo/com/some-company/some_artifact
26
+ :artifact: "%v/any_name.%v.%e"
27
+ :user: someuser
28
+ :pass: somepass
29
+
30
+ In ruby code:
31
+
32
+ # Publish the file
33
+ NexusArtifact.instance.publish '/source_dir/my_file.iso', :ver => '3.5.2', :ext => 'iso'
34
+
35
+ # Download the file
36
+ NexusArtifact.instance.get '/save_dir/my_file.iso', :ver => '3.5.2', :ext => 'iso'
37
+
38
+ With the nexus.yml file above, this will publish two files:
39
+ 1. http://my-maven-server:8081/nexus/content/repositories/some-repo/com/some-company/some_artifact/3.5.2/any_name.3.5.2.iso
40
+ 2. http://my-maven-server:8081/nexus/content/repositories/some-repo/com/some-company/some_artifact/3.5.2/any_name.3.5.2.iso.sha1
41
+
42
+ In addition, you can find out what versions are available as well as what the next availble build number is:
43
+
44
+ NexusArtifact.instance.versions
45
+ # Will return e.g. ['3.4.1', '3.4.2', '3.5.1', '4.8.12']
46
+
47
+ NexusArtifact.instance.next_version('4.8')
48
+ # Will return e.g. '4.8.13'
49
+
50
+ If you do not wish to use a nexus.yml file, you can also create an instance of this class:
51
+
52
+ artifact = NexusArtifact.new 'http://my-maven-server:8081',
53
+ '/nexus/content/repositories/some-repo/com/some-company/some_artifact',
54
+ '%v/any_name.%v.%e'
55
+ artifact.auth 'someuser', 'somepass' # Only if needed
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class NexusArtifact
2
+ VERSION = "2.0.0"
3
+ end
@@ -0,0 +1,108 @@
1
+
2
+ require 'yaml'
3
+ require 'mechanize'
4
+ require 'digest/sha1'
5
+ require "nexus_artifact/version"
6
+
7
+ class NexusArtifact
8
+
9
+ PATTERN_MAP = {
10
+ '%v' => :ver,
11
+ '%e' => :ext,
12
+ }
13
+
14
+ def self.instance
15
+ @instance ||= begin
16
+ yml = YAML.load File.read('nexus.yml')
17
+ obj = new yml[:uri], yml[:path], yml[:artifact]
18
+ if yml[:user] && yml[:pass]
19
+ obj.auth yml[:user], yml[:pass]
20
+ end
21
+ obj
22
+ end
23
+ end
24
+
25
+ attr_reader :agent, :base_uri, :base_href, :pattern
26
+
27
+ def initialize(base_uri, base_path, pattern)
28
+ @base_uri = base_uri
29
+ @base_href = "#{base_uri}#{base_path}"
30
+ @pattern = pattern
31
+ @agent = Mechanize.new
32
+ end
33
+
34
+ def auth(user, pass)
35
+ agent.add_auth(base_uri, user, pass)
36
+ self
37
+ end
38
+
39
+ def versions
40
+ pg = call(:get, '') or return []
41
+ pg.links.map do |link|
42
+ link.text.gsub('/', '') if link.href.start_with? base_href
43
+ end.compact
44
+ end
45
+
46
+ def builds(base)
47
+ versions.map do |v|
48
+ $1.to_i if v.start_with?(base) && v[base.size .. -1] =~ /\A\.(\d+)\Z/
49
+ end.compact.sort
50
+ end
51
+
52
+ def next_version(base)
53
+ max = builds(base).max || -1
54
+ "#{base}.#{max + 1}"
55
+ end
56
+
57
+ def get(file, data)
58
+ path = path(data)
59
+ sha1_expected = call(:get_file, path + '.sha1').to_s.strip
60
+ download(path, file)
61
+ sha1_actual = Digest::SHA1.file(file).hexdigest
62
+ if sha1_expected != sha1_actual
63
+ raise "SHA1 mismatch for #{file}. Expected #{sha1_expected.inspect} but got #{sha1_actual.inspect}."
64
+ end
65
+ end
66
+
67
+ def publish(file, data)
68
+ path = path(data)
69
+ uploads = [
70
+ [path, File.read(file)],
71
+ [path + '.sha1', Digest::SHA1.file(file).hexdigest],
72
+ ([path + '.git', data[:git]] if data[:git]),
73
+ ]
74
+ uploads.each do |path, contents|
75
+ raise "#{path} already exists" if call(:head, path)
76
+ end
77
+ uploads.each do |path, contents|
78
+ call(:put, path, contents)
79
+ end
80
+ nil
81
+ end
82
+
83
+ private
84
+
85
+ def path(data)
86
+ "/#{pattern}".tap do |path|
87
+ PATTERN_MAP.each do |sym, key|
88
+ path.gsub! sym, data[key]
89
+ end
90
+ end
91
+ end
92
+
93
+ def call(method, path, *args)
94
+ agent.send method, "#{base_href}#{path}", *args
95
+ rescue Mechanize::ResponseCodeError => e
96
+ raise e unless '404' == e.response_code
97
+ end
98
+
99
+ def download(path, dest)
100
+ prior = agent.pluggable_parser.default
101
+ agent.pluggable_parser.default = Mechanize::Download
102
+ File.unlink(dest) # Delete or else it will be saved in #{file}.1
103
+ call(:get, path).save(dest)
104
+ ensure
105
+ agent.pluggable_parser.default = prior
106
+ end
107
+
108
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/nexus_artifact/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David McCullars"]
6
+ gem.email = ["david.mccullars@gmail.com"]
7
+ gem.description = %q{Simple Ruby gem to download/publish arbitrary binary file from/to Nexus server}
8
+ gem.summary = %q{Simple Ruby gem to download/publish arbitrary binary file from/to Nexus server}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "nexus_artifact"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = NexusArtifact::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nexus_artifact
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David McCullars
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple Ruby gem to download/publish arbitrary binary file from/to Nexus
15
+ server
16
+ email:
17
+ - david.mccullars@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/nexus_artifact.rb
28
+ - lib/nexus_artifact/version.rb
29
+ - nexus_artifact.gemspec
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Simple Ruby gem to download/publish arbitrary binary file from/to Nexus server
54
+ test_files: []