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



192
193
194
195
# File 'lib/rubygems/commands/specific_install_command.rb', line 192

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
63
64
65
# 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 /^https?(.*)$/
    install_http_repo
    # install_gem
  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



110
111
112
113
114
115
# File 'lib/rubygems/commands/specific_install_command.rb', line 110

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



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

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



90
91
92
# File 'lib/rubygems/commands/specific_install_command.rb', line 90

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

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



184
185
186
187
188
189
190
# File 'lib/rubygems/commands/specific_install_command.rb', line 184

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

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

Returns:



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

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

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



172
173
174
# File 'lib/rubygems/commands/specific_install_command.rb', line 172

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

#install_from_git(dir) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rubygems/commands/specific_install_command.rb', line 117

def install_from_git(dir)
  Dir.chdir dir do
    change_to_branch(@branch) if @branch
    system("git submodule update --init --recursive") # issue 25
    # reliable method
    if install_gemspec
      success_message
      exit 0
    end

    # legacy methods
    ['', '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



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubygems/commands/specific_install_command.rb', line 67

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



151
152
153
154
155
156
157
158
159
# File 'lib/rubygems/commands/specific_install_command.rb', line 151

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

#install_gitObject



94
95
96
97
98
99
100
# File 'lib/rubygems/commands/specific_install_command.rb', line 94

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_http_repoObject



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

def install_http_repo
  output.puts 'http installing from ' + @loc

  @loc = [@loc, '.git'].join unless @loc[/\.git$/]

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

#install_shorthandObject



102
103
104
105
106
107
108
# File 'lib/rubygems/commands/specific_install_command.rb', line 102

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



143
144
145
# File 'lib/rubygems/commands/specific_install_command.rb', line 143

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

#set_locationObject



139
140
141
# File 'lib/rubygems/commands/specific_install_command.rb', line 139

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

#success_messageObject



147
148
149
# File 'lib/rubygems/commands/specific_install_command.rb', line 147

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