Class: Gem::Commands::WorldCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/commands/world_command.rb

Instance Method Summary collapse

Constructor Details

#initializeWorldCommand

Returns a new instance of WorldCommand.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/commands/world_command.rb', line 12

def initialize
  super 'world', 'Manage gems that in your world'

  add_option '--init' do |v, opts|
    opts[:init] = v
  end

  add_option '--depclean' do |v, opts|
    opts[:depclean] = v
  end

  add_option '-e', '--edit' do |v, opts|
    opts[:edit] = v
  end
end

Instance Method Details

#add(name, version) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/commands/world_command.rb', line 105

def add(name, version)
  open(world_path, 'r+') do |f|
    f << YAML.load(f).tap {|world|
      (world[name] ||= []).tap {|vs|
        vs << version.to_s
      }.uniq!

      f.rewind
    }.to_yaml
  end
end

#depcleanObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/commands/world_command.rb', line 59

def depclean
  terminate_if_world_is_missing

  gems = world_gems.inject(Set.new) {|acc, gem|
    acc + collect_dependencies(gem)
  }

  targets = (Gem.source_index.map(&:last) - gems.to_a).sort_by(&:name)

  if targets.empty?
    say 'Your world is already clean.'
    terminate_interaction
  end

  targets.each do |t|
    say "#{t.name} (#{t.version})"
  end

  terminate_interaction unless ask_yes_no 'Would you like to uninstall these gems?'

  targets.each do |gem|
    Gem::Uninstaller.new(gem.name, :version => gem.version, :user_install => true, :ignore => true).uninstall
  end
end

#editObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/commands/world_command.rb', line 84

def edit
  terminate_if_world_is_missing

  unless editor = ENV['VISUAL'] || ENV['EDITOR']
    alert_error 'Please set VISUAL or EDITOR variable.'
    terminate_interaction
  end

  system editor, world_path
end

#executeObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/commands/world_command.rb', line 28

def execute
  if options[:init]
    init
  elsif options[:depclean]
    depclean
  elsif options[:edit]
    edit
  else
    list
  end
end

#initObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/commands/world_command.rb', line 40

def init
  if File.exist?(world_path)
    terminate_interaction unless ask_yes_no "'#{world_path}' is already exists. overwrite?"
  end

  open(world_path, 'w') do |f|
    f << Gem.source_index.map(&:last).select {|spec|
      spec.dependent_gems.empty?
    }.group_by(&:name).inject({}) {|h, (name, specs)|
      versions = specs.map(&:version).sort
      versions[-1] = Gem::Requirement.default

      h.merge(name => versions.map(&:to_s))
    }.to_yaml
  end

  say "'#{world_path}' was successfully initialized."
end

#listObject



95
96
97
98
99
100
101
102
103
# File 'lib/commands/world_command.rb', line 95

def list
  terminate_if_world_is_missing

  YAML.load_file(world_path).sort_by {|name, versions|
    name.downcase
  }.each do |name, versions|
    say "#{name} (#{versions.join(', ')})"
  end
end

#world_pathObject



8
9
10
# File 'lib/commands/world_command.rb', line 8

def world_path
  File.join(Gem.user_dir, 'world')
end