Class: AutoGemsets::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/auto-gemsets/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, input = $stdin, args = Array.try_convert(ARGV)) ⇒ Application

Returns a new instance of Application.



20
21
22
23
24
25
26
# File 'lib/auto-gemsets/application.rb', line 20

def initialize(output = $stdout, input = $stdin, args = Array.try_convert(ARGV))
  @output = output
  @input = input
  @args = args
  @command = @args.empty? ? :current : @args.shift.to_sym unless @args.first =~ /^-/
  parse_options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



16
17
18
# File 'lib/auto-gemsets/application.rb', line 16

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



16
17
18
# File 'lib/auto-gemsets/application.rb', line 16

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/auto-gemsets/application.rb', line 16

def options
  @options
end

Instance Method Details

#create(gemset) ⇒ Object



53
54
55
# File 'lib/auto-gemsets/application.rb', line 53

def create(gemset)
  touch(gemset)
end

#currentObject



28
29
30
31
32
# File 'lib/auto-gemsets/application.rb', line 28

def current
  gemset = "-> #{ENV['GEMSET']}";
  gemset = "#{gemset}*" if ENV['GEMSET'] == default_gemset
  @output.puts gemset
end

#editObject



114
115
116
117
# File 'lib/auto-gemsets/application.rb', line 114

def edit
  raise "You must set $EDITOR or $TERM_EDITOR to edit Gemfiles" unless ENV['EDITOR'] || ENV['TERM_EDITOR']
  %x{#{ENV['EDITOR'] || ENV['TERM_EDITOR']} #{ENV['GEMFILE']}} if File.exists ENV['GEMFILE']
end

#initObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/auto-gemsets/application.rb', line 119

def init
  if File.exists? script_file
    @output.puts "auto-gemsets is already installed!"
    confirm("Do you wish overwrite this installation?", {
      accept: -> {
        create_script
      },
      deny: -> {
        @output.puts "Existing installation preserved."
      }
    })
  else
    create_script
  end
end

#listObject



43
44
45
# File 'lib/auto-gemsets/application.rb', line 43

def list
  ls
end

#lsObject



38
39
40
41
# File 'lib/auto-gemsets/application.rb', line 38

def ls
  gemsets = Dir.glob(File.join(ENV['HOME'], '.gemsets', '*')).map { |d| display_gemset d }
  @output.puts gemsets.join "\n"
end

#mv(gemset, new_gemset) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/auto-gemsets/application.rb', line 57

def mv(gemset, new_gemset)
  if !File.exists?(gemset_path(new_gemset)) && FileUtils.mv(gemset_path(gemset), gemset_path(new_gemset))
    @output.puts "#{gemset} renamed to #{new_gemset}"
  else
    @output.puts "#{new_gemset} already exists!"
    confirm("Do you really wish to replace #{new_gemset} with #{gemset}?", {
      accept: -> {
        if FileUtils.rm_rf(gemset_path(new_gemset)) && FileUtils.mv(gemset_path(gemset), gemset_path(new_gemset))
          @output.puts "#{gemset} renamed to #{new_gemset}"
        end
      },
      deny: -> {
        @output.puts "No gemsets were harmed."
      }
    })
  end
end

#open(gemset = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/auto-gemsets/application.rb', line 96

def open(gemset = nil)
  command = case
  when AutoGemsets::on_OSX?
    'open'
  when AutoGemsets::on_linux?
    'xdg-open'
  when AutoGemsets::on_windows?
    'explorer'
  end

  if gemset
    %x{#{command} #{gemset_path(gemset)}} if File.exists?(gemset_path(gemset))
    @output.puts "No gemset named #{gemset}!" unless File.exists?(gemset_path(gemset))
  else
    %x{#{command} #{AutoGemsets::GEMSET_ROOT}}
  end
end

#remove(gemset) ⇒ Object



92
93
94
# File 'lib/auto-gemsets/application.rb', line 92

def remove(gemset)
  rm(gemset)
end

#rename(gemset, new_gemset) ⇒ Object



75
76
77
# File 'lib/auto-gemsets/application.rb', line 75

def rename(gemset, new_gemset)
  mv(gemset, new_gemset)
end

#rm(gemset) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/auto-gemsets/application.rb', line 79

def rm(gemset)
  confirm("Are you sure you wish to delete the #{gemset} gemset?", {
    accept: -> {
      if File.exists?(gemset_path(gemset)) && FileUtils.rm_rf(gemset_path(gemset))
        @output.puts "#{gemset} gemset removed!"
      end
    },
    deny: -> {
      @output.puts "No gemsets were harmed."
    }
  })
end

#runObject



34
35
36
# File 'lib/auto-gemsets/application.rb', line 34

def run
  self.send @command, *@args
end

#touch(gemset) ⇒ Object



47
48
49
50
51
# File 'lib/auto-gemsets/application.rb', line 47

def touch(gemset)
  if !File.exists?(gemset_path(gemset)) && FileUtils.mkdir_p(gemset_path(gemset))
    @output.puts "#{gemset} gemset created"
  end
end