rubygems_snapshot 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile ADDED
@@ -0,0 +1,33 @@
1
+ h1. rubygems_snapshot
2
+
3
+ Adds snapshot command to gem. This command allow import/export of gems.
4
+
5
+ h2. Description
6
+
7
+ This gem is a plugin for rubygems.
8
+ Based on ["Import/Export Patch":http://rubyforge.org/tracker/?atid=577&group_id=126&func=browse] from ["Peer Allan":http://rubyforge.org/users/pallan].
9
+
10
+ h2. License
11
+
12
+ (The MIT License)
13
+
14
+ Copyright (c) 2008 FIX
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining
17
+ a copy of this software and associated documentation files (the
18
+ 'Software'), to deal in the Software without restriction, including
19
+ without limitation the rights to use, copy, modify, merge, publish,
20
+ distribute, sublicense, and/or sell copies of the Software, and to
21
+ permit persons to whom the Software is furnished to do so, subject to
22
+ the following conditions:
23
+
24
+ The above copyright notice and this permission notice shall be
25
+ included in all copies or substantial portions of the Software.
26
+
27
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
28
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
31
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
32
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
33
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,150 @@
1
+ require 'rubygems/command'
2
+ require 'yaml'
3
+
4
+ class Gem::Commands::SnapshotCommand < Gem::Command
5
+
6
+ def initialize
7
+ super 'snapshot', 'Export/Import your installed gems'
8
+ end
9
+
10
+ def arguments # :nodoc:
11
+ args = <<-EOF
12
+ export export your installed gems to yml file
13
+ import install allgems from yml file
14
+ filename yml file used to export/import actions
15
+ EOF
16
+ return args.gsub(/^\s+/, '')
17
+ end
18
+
19
+ def description # :nodoc:
20
+ <<-EOF
21
+ Describe here what snapshot does.
22
+ EOF
23
+ end
24
+
25
+ def usage # :nodoc:
26
+ "#{program_name} action(export|import) filename"
27
+ end
28
+
29
+ def execute
30
+ action, filename = get_and_check_arguments(options[:args])
31
+
32
+ if action == "export"
33
+ export(action, filename)
34
+ else
35
+ import(action, filename)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def import(action, filename)
42
+ #TODO: check if is a valid yml file
43
+
44
+ require 'rubygems/dependency_installer'
45
+
46
+ main_hash = nil
47
+ File.open(filename, "r") do |file|
48
+ main_hash = YAML.load(file)
49
+ end
50
+
51
+ main_hash['sources'].each do |source|
52
+ Gem.sources << source unless Gem.sources.include?(source)
53
+ end
54
+
55
+ options = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({
56
+ :generate_rdoc => true,
57
+ :generate_ri => true,
58
+ :format_executable => false,
59
+ :test => false,
60
+ :version => Gem::Requirement.default,
61
+ #aditional default parameters
62
+ :ignore_dependencies => true,
63
+ :verbose => true
64
+ })
65
+
66
+ main_hash['gems'].each do |hash_gem|
67
+ gem_name = hash_gem['name']
68
+ hash_gem['versions'].each do |version|
69
+
70
+ say "Going to install #{gem_name} -v#{version} ... wish me luck!"
71
+ begin
72
+ gem_install(options, gem_name, version)
73
+
74
+ rescue Gem::InstallError => e
75
+ alert_error("Error installing #{gem_name}:\n\t#{e.message}")
76
+ rescue Gem::GemNotFoundException => e
77
+ alert_error(e.message)
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ def gem_install(options, gem_name, version)
86
+ inst = Gem::DependencyInstaller.new(options)
87
+ inst.install(gem_name, version)
88
+
89
+ inst.installed_gems.each do |spec|
90
+ say "Successfully installed #{spec.full_name}"
91
+ end
92
+ end
93
+
94
+ def list_installed_gems
95
+ name = /^/i #get all local gems
96
+ dep = Gem::Dependency.new(name, Gem::Requirement.default)
97
+ specs = Gem.source_index.search(dep)
98
+ end
99
+
100
+ def export(action, filename)
101
+ say "Say CHEESE to snapshot! :P"
102
+
103
+ specs = list_installed_gems
104
+
105
+ hash_specs = {}
106
+ specs.each do |spec|
107
+ versions = hash_specs[spec.name.to_s] || []
108
+ versions << spec.version.to_s
109
+ hash_specs[spec.name.to_s] = versions
110
+ end
111
+
112
+ gems = []
113
+ hash_specs.each do |spec_name, versions|
114
+ gems << {'name' => spec_name, 'versions' => versions}
115
+ end
116
+
117
+ main_hash = {'gems' => gems, 'sources' => Gem.sources}
118
+ #say main_hash.to_yaml.to_s #for debug only :P
119
+
120
+ File.open(filename, "w") do |file|
121
+ file.puts(main_hash.to_yaml)
122
+ end
123
+ say "Gems exported to #{filename} successfully."
124
+ end
125
+
126
+ def get_and_check_arguments(args)
127
+ action = args[0]
128
+ raise Gem::CommandLineError, "Snapshot needs an action argument.\nUsage:\n#{usage}" if action.nil? or action.empty?
129
+
130
+ action = action.downcase
131
+ unless %w(export import).include?(action)
132
+ raise Gem::CommandLineError, "invalid action \"#{action}\" argument.\nUsage:\n#{usage}"
133
+ end
134
+
135
+ filename = nil
136
+ if action == "export"
137
+ filename = args[1] || "gems_#{Time.now.strftime("%Y%m%d_%H%M")}.yml"
138
+ filename = filename.downcase
139
+ filename.concat(".yml") unless filename.end_with?(".yml")
140
+ else
141
+ filename = args[1]
142
+ raise Gem::CommandLineError, "Snapshot needs an filename argument for import action.\nUsage:\n#{usage}" if filename.nil? or filename.empty?
143
+ raise Gem::Exception, "File not found. :( \nUsage:\n#{usage}" unless File.exist?(filename)
144
+ end
145
+
146
+ [action, filename]
147
+ end
148
+
149
+ end
150
+
@@ -0,0 +1,6 @@
1
+ #$:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
2
+
3
+ require 'rubygems/command_manager'
4
+ require 'commands/snapshot_command'
5
+ Gem::CommandManager.instance.register_command :snapshot
6
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubygems_snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Leite
@@ -21,10 +21,12 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files: []
23
23
 
24
- files: []
25
-
24
+ files:
25
+ - lib/commands/snapshot_command.rb
26
+ - lib/rubygems_plugin.rb
27
+ - README.textile
26
28
  has_rdoc: true
27
- homepage: ""
29
+ homepage: http://github.com/rogerleite/rubygems_snapshot
28
30
  licenses: []
29
31
 
30
32
  post_install_message: |+