bukin 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.travis.yml +0 -1
  2. data/Gemfile +2 -0
  3. data/README.md +3 -3
  4. data/bukin.gemspec +2 -2
  5. data/docs/process.md +24 -0
  6. data/lib/bukin.rb +54 -2
  7. data/lib/bukin/bukfile.rb +46 -48
  8. data/lib/bukin/bukget.rb +63 -0
  9. data/lib/bukin/bukkit_dl.rb +45 -0
  10. data/lib/bukin/cli.rb +109 -83
  11. data/lib/bukin/download.rb +16 -0
  12. data/lib/bukin/file_match.rb +33 -0
  13. data/lib/bukin/installer.rb +65 -68
  14. data/lib/bukin/jenkins.rb +47 -0
  15. data/lib/bukin/lockfile.rb +23 -21
  16. data/lib/bukin/resource.rb +17 -0
  17. data/lib/bukin/version.rb +1 -1
  18. data/spec/bukfile_spec.rb +0 -16
  19. data/spec/bukget_spec.rb +56 -0
  20. data/spec/bukkit_dl_spec.rb +50 -0
  21. data/spec/cassettes/Bukin_Bukget/chooses_the_version_with_a_jar_file_when_there_are_multiple_versions.yml +61 -0
  22. data/spec/cassettes/Bukin_Bukget/installs_a_specific_version_of_a_resource.yml +61 -0
  23. data/spec/cassettes/Bukin_Bukget/installs_the_latest_version_of_a_resource.yml +49 -0
  24. data/spec/cassettes/Bukin_Bukget/returns_an_error_when_asked_for_a_file_that_does_not_exist.yml +61 -0
  25. data/spec/cassettes/Bukin_Bukget/returns_an_error_when_asked_for_a_resource_that_doese_not_exist.yml +43 -0
  26. data/spec/cassettes/Bukin_Bukget/returns_an_error_when_asked_for_a_version_that_doese_not_exist.yml +435 -0
  27. data/spec/cassettes/Bukin_BukkitDl/chooses_the_first_file_when_there_are_multiple_files.yml +59 -0
  28. data/spec/cassettes/Bukin_BukkitDl/installs_a_specific_version_of_a_resource.yml +59 -0
  29. data/spec/cassettes/Bukin_BukkitDl/installs_the_latest_version_of_a_resource.yml +59 -0
  30. data/spec/cassettes/Bukin_BukkitDl/returns_an_error_when_asked_for_a_file_that_does_not_exist.yml +59 -0
  31. data/spec/cassettes/Bukin_BukkitDl/returns_an_error_when_asked_for_a_resource_that_doese_not_exist.yml +128 -0
  32. data/spec/cassettes/Bukin_BukkitDl/returns_an_error_when_asked_for_a_version_that_doese_not_exist.yml +128 -0
  33. data/spec/cassettes/Bukin_Jenkins/chooses_the_first_file_when_there_are_multiple_files.yml +50 -0
  34. data/spec/cassettes/Bukin_Jenkins/installs_a_specific_version_of_a_resource.yml +50 -0
  35. data/spec/cassettes/Bukin_Jenkins/installs_the_latest_version_of_a_resource.yml +49 -0
  36. data/spec/cassettes/Bukin_Jenkins/returns_an_error_when_asked_for_a_file_that_does_not_exist.yml +50 -0
  37. data/spec/cassettes/Bukin_Jenkins/returns_an_error_when_asked_for_a_resource_that_doese_not_exist.yml +59 -0
  38. data/spec/cassettes/Bukin_Jenkins/returns_an_error_when_asked_for_a_version_that_does_not_exist.yml +59 -0
  39. data/spec/file_match_spec.rb +39 -0
  40. data/spec/installer_spec.rb +0 -1
  41. data/spec/jenkins_spec.rb +61 -0
  42. data/spec/lockfile_spec.rb +8 -8
  43. data/spec/spec_helper.rb +10 -0
  44. metadata +57 -26
  45. data/lib/bukin/exceptions.rb +0 -5
  46. data/lib/bukin/friendly_errors.rb +0 -22
  47. data/lib/bukin/providers/bukget.rb +0 -54
  48. data/lib/bukin/providers/bukkit_dl.rb +0 -29
  49. data/lib/bukin/providers/jenkins.rb +0 -40
@@ -0,0 +1,16 @@
1
+ module Bukin
2
+ # Straight file downloads
3
+ class Download
4
+ attr_accessor :url
5
+
6
+ def initialize(url)
7
+ @url = url
8
+ end
9
+
10
+ def find(data)
11
+ version = data[:version]
12
+
13
+ return version, @url
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ module Bukin
2
+ class FileMatch
3
+
4
+ def initialize(search)
5
+ @search = search
6
+ end
7
+
8
+ def match(file_name)
9
+ match_helper(@search, file_name)
10
+ end
11
+
12
+ alias_method :=~, :match
13
+
14
+ def self.any
15
+ FileMatch.new(true)
16
+ end
17
+
18
+ private
19
+ def match_helper(search, file_name)
20
+ if search == true || search == false
21
+ search
22
+ elsif search.is_a? ::String
23
+ search == file_name
24
+ elsif search.is_a? ::Regexp
25
+ search =~ file_name
26
+ elsif search.respond_to? :any?
27
+ search.any? {|item| match_helper(item, file_name)}
28
+ else
29
+ false
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,87 +1,84 @@
1
1
  require 'bukin/lockfile'
2
- require 'zip/zip'
2
+ require 'zip'
3
3
 
4
- class Bukin::Installer
5
- PATHS = { :server => '.', :plugin => 'plugins' }
4
+ module Bukin
5
+ class Installer
6
+ PATHS = { :server => '.', :plugin => 'plugins' }
6
7
 
7
- def initialize(path, use_lockfile = false)
8
- @lockfile = Bukin::Lockfile.new if use_lockfile
9
- end
10
-
11
- def install(data)
12
- path = PATHS[data[:type]]
13
- file_names = []
14
- dl_data, dl_name = download_file(data[:download])
15
-
16
- if File.extname(dl_name) == '.zip'
17
- match = self.get_match(data[:extract])
18
- file_names = extract_files(dl_data, path, match)
19
- raise Bukin::InstallError, "The resource #{data[:name]} (#{data[:version]}) has no jar files in it's download (zip file)." if file_names.empty?
20
- else
21
- self.save_download(dl_data, dl_name, path)
22
- file_names << dl_name
8
+ def initialize(path, use_lockfile = false)
9
+ @lockfile = Bukin::Lockfile.new if use_lockfile
23
10
  end
24
11
 
25
- if @lockfile
26
- data[:files] = file_names.map {|file_name| File.join(path, file_name)}
27
- @lockfile.add(data)
12
+ def install(resource)
13
+ path = PATHS[resource.type]
14
+ file_names = []
15
+ dl_data, dl_name = download_file(resource.download)
16
+
17
+ if File.extname(dl_name) == '.zip'
18
+ match = self.get_match(resource[:extract])
19
+ file_names = extract_files(dl_data, path, match)
20
+ raise Bukin::InstallError, "The resource #{resource.name} (#{resources.version}) has no jar files in it's download (zip file)." if file_names.empty?
21
+ else
22
+ save_download(dl_data, dl_name, path)
23
+ file_names << dl_name
24
+ end
28
25
  end
29
- end
30
26
 
31
- def extract_files(file_data, path, match)
32
- file_names = []
33
- tempfile = Tempfile.new('bukin')
34
- begin
35
- tempfile.write(file_data)
36
- tempfile.close
27
+ def extract_files(file_data, path, match)
28
+ file_names = []
29
+ tempfile = Tempfile.new('bukin')
30
+ begin
31
+ tempfile.write(file_data)
32
+ tempfile.close
37
33
 
38
- Zip::ZipFile.open(tempfile.path) do |zipfile|
39
- files = zipfile.find_all {|file| file.name =~ match}
40
- files.each do |file|
41
- file.extract(File.join(path, file.name)) { true }
42
- file_names << file.name
34
+ Zip::File.open(tempfile.path) do |zipfile|
35
+ files = zipfile.find_all {|file| file.name =~ match}
36
+ files.each do |file|
37
+ file.extract(File.join(path, file.name)) { true }
38
+ file_names << file.name
39
+ end
43
40
  end
41
+ ensure
42
+ tempfile.close
43
+ tempfile.unlink
44
44
  end
45
- ensure
46
- tempfile.close
47
- tempfile.unlink
45
+ file_names
48
46
  end
49
- file_names
50
- end
51
47
 
52
- def save_download(data, name, path)
53
- FileUtils.mkdir_p(path)
54
- open("#{path}/#{name}", "wb") do |file|
55
- file.print data
48
+ def save_download(data, name, path)
49
+ FileUtils.mkdir_p(path)
50
+ open("#{path}/#{name}", "wb") do |file|
51
+ file.print data
52
+ end
56
53
  end
57
- end
58
54
 
59
- def download_file(url, content_disposition = false)
60
- open(url, "User-Agent" => "Bukin #{Bukin::VERSION}") do |download|
61
- file_name = if download.meta['content-disposition']
62
- download.meta['content-disposition'].match(/filename=(\"?)(.+)\1/)[2]
63
- else
64
- File.basename(url)
65
- end
66
- file_name = file_name.force_encoding('UTF-8') if file_name.respond_to?(:force_encoding)
67
- data = download.read
68
- return data, file_name
55
+ def download_file(url, content_disposition = false)
56
+ open(url, "User-Agent" => "Bukin #{Bukin::VERSION}") do |download|
57
+ file_name = if download.meta['content-disposition']
58
+ download.meta['content-disposition'].match(/filename=(\"?)(.+)\1/)[2]
59
+ else
60
+ File.basename(url)
61
+ end
62
+ file_name = file_name.force_encoding('UTF-8') if file_name.respond_to?(:force_encoding)
63
+ data = download.read
64
+ return data, file_name
65
+ end
69
66
  end
70
- end
71
67
 
72
- def get_match(match)
73
- case match
74
- when Regexp
75
- match
76
- when String
77
- /^#{Regexp.quote(match)}$/
78
- when :all
79
- //
80
- when nil
81
- /\.jar$/
82
- else
83
- raise Bukin::InstallError, "The extract option #{match} is not valid. Please use a String, Regexp or :all"
68
+ def get_match(match)
69
+ case match
70
+ when Regexp
71
+ match
72
+ when String
73
+ /^#{Regexp.quote(match)}$/
74
+ when :all
75
+ //
76
+ when nil
77
+ /\.jar$/
78
+ else
79
+ raise Bukin::InstallError, "The extract option #{match} is not valid. Please use a String, Regexp or :all"
80
+ end
84
81
  end
85
- end
86
82
 
83
+ end
87
84
  end
@@ -0,0 +1,47 @@
1
+ require 'json'
2
+ require 'cgi'
3
+ require 'bukin/file_match'
4
+
5
+ module Bukin
6
+ # Api for downloading from jenkins
7
+ class Jenkins
8
+ attr_reader :url
9
+
10
+ VERSION = 'lastSuccessfulBuild'
11
+ GOOD_VERSIONS = "'build-125'"
12
+
13
+ def initialize(url)
14
+ @url = url
15
+ end
16
+
17
+ def find(data)
18
+ name = data[:name]
19
+ version = data[:version]
20
+ match = data[:file] ? FileMatch.new(data[:file]) : FileMatch.any
21
+
22
+ if version.nil? || version == VERSION
23
+ build = VERSION
24
+ elsif correct_version_format?(version)
25
+ build = version[/^build-([0-9]+)$/, 1]
26
+ else
27
+ raise VersionError.new(name, version, GOOD_VERSIONS)
28
+ end
29
+
30
+ base_path = "#{@url}/job/#{CGI.escape(name)}/#{CGI.escape(build)}"
31
+
32
+ info = Bukin.try_get_json("#{base_path}/api/json")
33
+ raise NoDownloadError.new(name, version) unless info
34
+
35
+ download_info = info['artifacts'].find{|file| match =~ file['fileName']}
36
+ raise NoDownloadError.new(name, version) unless download_info
37
+
38
+ download = "#{base_path}/artifact/#{download_info['relativePath']}"
39
+ return "build-#{info['number']}", download
40
+ end
41
+
42
+ private
43
+ def correct_version_format?(version)
44
+ version == VERSION || /^build-[0-9]+$/.match(version)
45
+ end
46
+ end
47
+ end
@@ -1,29 +1,31 @@
1
1
  require 'yaml'
2
2
 
3
- class Bukin::Lockfile
4
- FILE_NAME = 'Bukfile.lock'
5
- attr_reader :path
3
+ module Bukin
4
+ class Lockfile
5
+ FILE_NAME = 'Bukfile.lock'
6
+ attr_reader :path
6
7
 
7
- def initialize(path = nil)
8
- @path = path || File.join(Dir.pwd, FILE_NAME)
9
- @resources = File.exist?(@path) ? YAML::load_file(@path) : {}
10
- @resources = { 'resources' => {} } if @resources['resources'].nil?
11
- end
8
+ def initialize(path = nil)
9
+ @path = path || File.join(Dir.pwd, FILE_NAME)
10
+ @resources = File.exist?(@path) ? YAML::load_file(@path) : {}
11
+ @resources = { 'resources' => {} } if @resources['resources'].nil?
12
+ end
12
13
 
13
- def add(data)
14
- name = data[:name]
15
- resources[name] = {
16
- 'version' => data[:version],
17
- 'files' => data[:files]
18
- }
19
- save
20
- end
14
+ def add(data)
15
+ name = data[:name]
16
+ resources[name] = {
17
+ 'version' => data[:version],
18
+ 'files' => data[:files]
19
+ }
20
+ save
21
+ end
21
22
 
22
- def resources
23
- @resources['resources']
24
- end
23
+ def resources
24
+ @resources['resources']
25
+ end
25
26
 
26
- def save
27
- File.open(@path, "w") {|file| file.write @resources.to_yaml}
27
+ def save
28
+ File.open(@path, "w") {|file| file.write @resources.to_yaml}
29
+ end
28
30
  end
29
31
  end
@@ -0,0 +1,17 @@
1
+ module Bukin
2
+ class Resource
3
+ attr_reader :type, :name, :version, :download
4
+
5
+ def initialize(data, version, download)
6
+ @type = data[:type]
7
+ @name = data[:name]
8
+ @version = version
9
+ @download = download
10
+ @data = data
11
+ end
12
+
13
+ def [](key)
14
+ @data[key]
15
+ end
16
+ end
17
+ end
data/lib/bukin/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bukin
2
- VERSION = "0.6.0"
2
+ VERSION = '0.7.0'
3
3
  end
data/spec/bukfile_spec.rb CHANGED
@@ -70,22 +70,6 @@ describe Bukin::Bukfile do
70
70
  end
71
71
  end
72
72
 
73
- it "adds a server with the correct provider" do
74
- bukfile = Bukin::Bukfile.new do
75
- server 'resource_name'
76
- end
77
-
78
- bukfile.resources.first[:bukkit_dl].should == Bukin::BukkitDl::DEFAULT_URL
79
- end
80
-
81
- it "adds a plugin with the correct provider" do
82
- bukfile = Bukin::Bukfile.new do
83
- plugin 'resource_name'
84
- end
85
-
86
- bukfile.resources.first[:bukget].should == Bukin::Bukget::DEFAULT_URL
87
- end
88
-
89
73
  it 'adds both plugins and servers' do
90
74
  bukfile = Bukin::Bukfile.new do
91
75
  server 'craftbukkit'
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'bukin'
3
+
4
+ describe Bukin::Bukget, :vcr do
5
+ before do
6
+ @name = 'worldedit'
7
+ @version = '5.5.8'
8
+ @download = 'http://dev.bukkit.org/media/files/739/931/worldedit-5.5.8.jar'
9
+ @missing_name = 'missing-name'
10
+ @missing_version = '0.0.0'
11
+ @missing_file = 'missing-file.jar'
12
+ @latest_version = '5.5.8'
13
+ end
14
+
15
+ it 'installs the latest version of a resource' do
16
+ provider = Bukin::Bukget.new
17
+ version, download = provider.find(name: @name)
18
+
19
+ version.should == @latest_version
20
+ end
21
+
22
+ it 'installs a specific version of a resource' do
23
+ provider = Bukin::Bukget.new
24
+ version, download = provider.find(name: @name, version: @version)
25
+
26
+ version.should == @version
27
+ end
28
+
29
+ it 'returns an error when asked for a resource that doese not exist' do
30
+ provider = Bukin::Bukget.new
31
+ expect do
32
+ provider.find(name: @missing_name)
33
+ end.to raise_error(Bukin::NoDownloadError)
34
+ end
35
+
36
+ it 'returns an error when asked for a version that doese not exist' do
37
+ provider = Bukin::Bukget.new
38
+ expect do
39
+ provider.find(name: @name, version: @missing_version)
40
+ end.to raise_error(Bukin::NoDownloadError)
41
+ end
42
+
43
+ it 'returns an error when asked for a file that does not exist' do
44
+ provider = Bukin::Bukget.new
45
+ expect do
46
+ provider.find(name: @name, version: @version, file: @missing_file)
47
+ end.to raise_error(Bukin::NoDownloadError)
48
+ end
49
+
50
+ it 'chooses the version with a .jar file when there are multiple versions' do
51
+ provider = Bukin::Bukget.new
52
+ version, download = provider.find(name: @name, version: @version)
53
+
54
+ download.should == @download
55
+ end
56
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'bukin'
3
+
4
+ describe Bukin::BukkitDl, :vcr do
5
+ before do
6
+ @name = 'craftbukkit'
7
+ @version = 'build-2912'
8
+ @download = 'http://dl.bukkit.org/downloads/craftbukkit/get/'\
9
+ '02381_1.6.4-R1.0/craftbukkit.jar'
10
+ @missing_name = 'missing-name'
11
+ @missing_version = 'build-0000'
12
+ @missing_file = 'missing-file.jar'
13
+ @latest_version = 'build-2918'
14
+ end
15
+
16
+ it 'installs the latest version of a resource' do
17
+ provider = Bukin::BukkitDl.new
18
+ version, download = provider.find(name: @name)
19
+
20
+ version.should == @latest_version
21
+ end
22
+
23
+ it 'installs a specific version of a resource' do
24
+ provider = Bukin::BukkitDl.new
25
+ version, download = provider.find(name: @name, version: @version)
26
+
27
+ version.should == @version
28
+ end
29
+
30
+ it 'returns an error when asked for a resource that doese not exist' do
31
+ provider = Bukin::BukkitDl.new
32
+ expect do
33
+ provider.find(name: @missing_name)
34
+ end.to raise_error(Bukin::NoDownloadError)
35
+ end
36
+
37
+ it 'returns an error when asked for a version that doese not exist' do
38
+ provider = Bukin::BukkitDl.new
39
+ expect do
40
+ provider.find(name: @name, version: @missing_version)
41
+ end.to raise_error(Bukin::NoDownloadError)
42
+ end
43
+
44
+ it 'chooses the first file when there are multiple files' do
45
+ provider = Bukin::BukkitDl.new
46
+ version, download = provider.find(name: @name, version: @version)
47
+
48
+ download.should == @download
49
+ end
50
+ end