Class: Gem::Commands::SpecificInstallCommand

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

Direct Known Subclasses

GitInstallCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = STDOUT) ⇒ SpecificInstallCommand

Returns a new instance of SpecificInstallCommand.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubygems/commands/specific_install_command.rb', line 15

def initialize(output=STDOUT)
  super 'specific_install', description
  @output = output

  add_option('-l', '--location LOCATION', arguments) do |location, options|
    options[:location] = location
  end

  add_option('-b', '--branch LOCATION', arguments) do |branch, options|
    options[:branch] = branch
  end

end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



9
10
11
# File 'lib/rubygems/commands/specific_install_command.rb', line 9

def output
  @output
end

Instance Method Details

#argumentsObject



29
30
31
32
# File 'lib/rubygems/commands/specific_install_command.rb', line 29

def arguments
  "LOCATION like http://github.com/rdp/ruby_tutorials_core or git://github.com/rdp/ruby_tutorials_core.git or http://host/gem_name.gem\n" +
  "BRANCH (optional) like beta, or new-feature"
end

#change_to_branch(branch) ⇒ Object



178
179
180
181
# File 'lib/rubygems/commands/specific_install_command.rb', line 178

def change_to_branch(branch)
  system("git checkout #{branch}")
  system("git branch")
end

#descriptionObject



11
12
13
# File 'lib/rubygems/commands/specific_install_command.rb', line 11

def description
  "Allows you to install an 'edge' gem straight from its github repository or from a web site"
end

#determine_source_and_installObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubygems/commands/specific_install_command.rb', line 50

def determine_source_and_install
  case @loc
  when /^https?(.*)\.gem$/
    install_gem
  when /\.git$/
    install_git
  when %r(.*/.*)
    install_shorthand
  else
    warn 'Error: must end with .git to be a git repository' +
      'or be in shorthand form: rdp/specific_install'
  end
end

#download(full_url, output_name) ⇒ Object



97
98
99
100
101
102
# File 'lib/rubygems/commands/specific_install_command.rb', line 97

def download( full_url, output_name )
  File.open(output_name, "wb") do |output_file|
    uri = URI.parse(full_url)
    output_file.write(uri.read)
  end
end

#executeObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubygems/commands/specific_install_command.rb', line 38

def execute
  @loc ||= set_location
  @branch ||= set_branch if set_branch
  if @loc.nil?
    raise ArgumentError, "No location received. Use like `gem specific_install -l http://example.com/rdp/specific_install`"
  end
  Dir.mktmpdir do |dir|
    @dir = dir
    determine_source_and_install
  end
end

#find_or_build_gemObject



147
148
149
150
151
152
153
154
155
156
# File 'lib/rubygems/commands/specific_install_command.rb', line 147

def find_or_build_gem
  if gemspec_exists?
    gemspec = Gem::Specification.load(gemspec_file)
    Gem::Package.build gemspec
  elsif gemfile
    gemfile
  else
    nil
  end
end

#gem_nameObject



77
78
79
# File 'lib/rubygems/commands/specific_install_command.rb', line 77

def gem_name
  @gem_name ||= @loc.split("/").last
end

#gemfile(name = '**/*.gem') ⇒ Object



170
171
172
173
174
175
176
# File 'lib/rubygems/commands/specific_install_command.rb', line 170

def gemfile(name = '**/*.gem')
  if Dir[name].empty?
    false
  else
    Dir[name][0]
  end
end

#gemspec_exists?(name = "*.gemspec") ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
165
166
167
168
# File 'lib/rubygems/commands/specific_install_command.rb', line 162

def gemspec_exists?(name = "*.gemspec")
  if gemspec_file(name)
    File.exists?(gemspec_file(name))
  else
    false
  end
end

#gemspec_file(name = "*.gemspec") ⇒ Object



158
159
160
# File 'lib/rubygems/commands/specific_install_command.rb', line 158

def gemspec_file(name = "*.gemspec")
  Dir[name][0]
end

#install_from_git(dir) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rubygems/commands/specific_install_command.rb', line 104

def install_from_git(dir)
  Dir.chdir dir do
    change_to_branch(@branch) if @branch
    # reliable method
    if install_gemspec
      success_message
      exit 0
    end

    # legacy method
    ['', 'rake gemspec', 'rake gem', 'rake build', 'rake package'].each do |command|
      puts "attempting #{command}..."
      system command
      if install_gemspec
        success_message
        exit 0
      end
    end
  end
end

#install_gemObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubygems/commands/specific_install_command.rb', line 64

def install_gem
  Dir.chdir @dir do
    output.puts "downloading #{@loc}"
    download(@loc, gem_name)

    if install_gemspec
      success_message
    else
      output.puts "Failed"
    end
  end
end

#install_gemspecObject



137
138
139
140
141
142
143
144
145
# File 'lib/rubygems/commands/specific_install_command.rb', line 137

def install_gemspec
  gem = find_or_build_gem
  if gem
    inst = Gem::DependencyInstaller.new
    inst.install gem
  else
    nil
  end
end

#install_gitObject



81
82
83
84
85
86
87
# File 'lib/rubygems/commands/specific_install_command.rb', line 81

def install_git
  output.puts 'git installing from ' + @loc

  redirect_for_specs = ENV.fetch( "SPECIFIC_INSTALL_SPEC" ) { "" }
  system("git clone #{@loc} #{@dir} #{redirect_for_specs}")
  install_from_git(@dir)
end

#install_shorthandObject



89
90
91
92
93
94
95
# File 'lib/rubygems/commands/specific_install_command.rb', line 89

def install_shorthand
  output.puts "Installing from [email protected]:#{@loc}.git"

  redirect_for_specs = ENV.fetch( "SPECIFIC_INSTALL_SPEC" ) { "" }
  system("git clone [email protected]:#{@loc}.git #{@dir} #{redirect_for_specs}")
  install_from_git(@dir)
end

#set_branchObject



129
130
131
# File 'lib/rubygems/commands/specific_install_command.rb', line 129

def set_branch
  options[:branch] || options[:args][1]
end

#set_locationObject



125
126
127
# File 'lib/rubygems/commands/specific_install_command.rb', line 125

def set_location
  options[:location] || options[:args][0]
end

#success_messageObject



133
134
135
# File 'lib/rubygems/commands/specific_install_command.rb', line 133

def success_message
  output.puts 'Successfully installed'
end

#usageObject



34
35
36
# File 'lib/rubygems/commands/specific_install_command.rb', line 34

def usage
  "#{program_name} [LOCATION] [BRANCH] (or command line options for the same)"
end