Module: Multiruby

Defined in:
lib/multiruby.rb

Overview

multiruby_setup is a script to help you manage multiruby.

usage: multiruby_setup [-h|cmd|spec…]

cmds:

  -h, --help, help = show this help.
  build            = build and install everything. used internally.
  clean            = clean scm build dirs and remove non-scm build dirs.
  list             = print installed versions.
  rm:$version      = remove a particular version.
  rubygems:merge   = symlink all rubygem dirs to one dir.
  tags             = list all tags from svn.
  update           = update svn builds.
  update:rubygems  = update rubygems and nuke install dirs.

specs:

  the_usual              = alias for latest versions from tar + rubygems
  mri:svn:current        = alias for mri:svn:releases and mri:svn:branches.
  mri:svn:releases       = alias for supported releases of mri ruby.
  mri:svn:branches       = alias for active branches of mri ruby.
  mri:svn:branch:$branch = install a specific $branch of mri from svn.
  mri:svn:tag:$tag       = install a specific $tag of mri from svn.
  mri:tar:$version       = install a specific $version of mri from tarball.
  rbx:ln:$dir            = symlink your rbx $dir
  rbx:git:current        = install rbx from git
  jruby:git:current      = install jruby from git
  jruby-$version         = install a specific $version of jruby

environment variables:

  GEM_URL  = url for rubygems tarballs
  MRI_SVN  = url for MRI SVN
  RBX_GIT  = url for rubinius git
  RUBY_URL = url for MRI tarballs
  JRUBY_GIT = url for JRuby git
  JRUBY_URL = url for JRuby tarballs
  VERSIONS = what versions to install

  RUBYOPT is cleared on installs.

NOTES:

  • you can add a symlink to your rubinius build into ~/.multiruby/install

  • I need patches/maintainers for other implementations.

Constant Summary collapse

TAGS =
%w(    1_8_6 1_8_7 1_9_1)
BRANCHES =
%w(1_8 1_8_6 1_8_7 trunk)
VERSIONS =
env('VERSIONS', TAGS.join(":").gsub(/_/, '.')).split(/:/)
MRI_SVN =
env 'MRI_SVN',  'http://svn.ruby-lang.org/repos/ruby'
RBX_GIT =
env 'RBX_GIT',  'git://github.com/evanphx/rubinius.git'
RUBY_URL =
env 'RUBY_URL', 'http://ftp.ruby-lang.org/pub/ruby'
GEM_URL =
env 'GEM_URL',  'http://files.rubyforge.vm.bytemark.co.uk/rubygems'
JRUBY_GIT =
env 'JRUBY_GIT', 'git://github.com/jruby/jruby.git'
JRUBY_URL =
env 'JRUBY_URL', 'http://dist.codehaus.org/jruby'
HELP =
[]

Class Method Summary collapse

Class Method Details

.build_and_installObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/multiruby.rb', line 73

def self.build_and_install
  ENV.delete 'RUBYOPT'

  root_dir = self.root_dir
  versions = []

  Dir.chdir root_dir do
    self.setup_dirs

    rubygems = Dir["versions/rubygems*.tgz"]
    abort "You should delete all but one rubygem tarball" if rubygems.size > 1
    rubygem_tarball = File.expand_path rubygems.last rescue nil

    Dir.chdir "build" do
      Dir["../versions/*"].sort.each do |tarball|
        next if tarball =~ /rubygems/

        build_dir = File.basename tarball, ".tar.gz"
        build_dir.sub!(/-bin/,'') # e.g., jruby-bin-1.3.1.tar.gz
        version = build_dir.sub(/^ruby-?/, '')
        versions << version
        inst_dir = "#{root_dir}/install/#{version}"

        unless test ?d, inst_dir then
          unless test ?d, build_dir then
            if test ?d, tarball then
              dir = File.basename tarball
              FileUtils.ln_sf "../versions/#{dir}", "../build/#{dir}"
            else
              puts "creating #{inst_dir}"
              Dir.mkdir inst_dir
              run "tar zxf #{tarball}"
            end
          end
          Dir.chdir build_dir do
            puts "building and installing #{version}"
            if test ?f, "configure.in" then
              gnu_utils_build inst_dir
            elsif test ?f, "Rakefile" then
              rake_build inst_dir
            elsif version =~ /jruby/
              # git master should have built JRuby with 'Rakefile' above,
              # and there is no need to build expanded jruby-bin tarball
              FileUtils.ln_sf build_dir, inst_dir
            else
              raise "dunno how to build"
            end

            if rubygem_tarball and version !~ /1[._-]9|mri_trunk|rubinius/ then
              rubygems = File.basename rubygem_tarball, ".tgz"
              run "tar zxf #{rubygem_tarball}" unless test ?d, rubygems

              Dir.chdir rubygems do
                run "../ruby ./setup.rb --no-rdoc --no-ri", "../log.rubygems"
              end
            end
          end
        end
      end
    end
  end

  versions
end

.cleanObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/multiruby.rb', line 138

def self.clean
  self.each_scm_build_dir do |style|
    case style
    when :svn, :git then
      if File.exist? "Rakefile" then
        run "rake clean"
      elsif File.exist? "Makefile" then
        run "make clean"
      end
    else
      FileUtils.rm_rf Dir.pwd
    end
  end
end

.each_scm_build_dirObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/multiruby.rb', line 153

def self.each_scm_build_dir
  Multiruby.in_build_dir do
    Dir["*"].each do |dir|
      next unless File.directory? dir
      Dir.chdir dir do
        if File.exist?(".svn") || File.exist?(".git") then
          scm = File.exist?(".svn") ? :svn : :git
          yield scm
        else
          yield :none
        end
      end
    end
  end
end

.env(name, fallback) ⇒ Object

:nodoc:



53
# File 'lib/multiruby.rb', line 53

def self.env name, fallback; ENV[name] || fallback; end

.extract_latest_version(url, matching = nil) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/multiruby.rb', line 169

def self.extract_latest_version url, matching=nil
  file = URI.parse(url).read
  versions = file.scan(/href="(j?ruby.*tar.gz)"/).flatten.reject { |s|
    s =~ /preview|-rc\d|src/
  }.sort_by { |s|
    s.split(/\D+/).map { |i| i.to_i }
  }.flatten
  versions = versions.grep(/#{Regexp.escape(matching)}/) if matching
  versions.last
end

.fetch_tar(v, base_url = RUBY_URL) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/multiruby.rb', line 180

def self.fetch_tar v, base_url = RUBY_URL
  in_versions_dir do
    warn "  Determining latest version for #{v}"
    case base_url
    when RUBY_URL
      ver = v[/\d+\.\d+/]
    when JRUBY_URL
      ver = v[/\d+\.\d+\.\d+/]
    else
      warn "Unknown URL #{base_url}"
    end
    base = extract_latest_version("#{base_url}/#{ver}/", v)
    abort "Could not determine release for #{v}" unless base
    url = File.join base_url, ver, base
    unless File.file? base then
      warn "    Fetching #{base} via HTTP... this might take a while."
      open(url) do |f|
        File.open base, 'w' do |out|
          out.write f.read
        end
      end
    end
  end
end

.git_clone(url, dir) ⇒ Object



205
206
207
208
209
210
# File 'lib/multiruby.rb', line 205

def self.git_clone url, dir
  Multiruby.in_versions_dir do
    Multiruby.run "git clone #{url} #{dir}" unless File.directory? dir
    FileUtils.ln_sf "../versions/#{dir}", "../build/#{dir}"
  end
end

.git_co(repo, tag) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
# File 'lib/multiruby.rb', line 346

def self.git_co repo, tag
  Multiruby.in_versions_dir do
    Dir.chdir repo do
      Multiruby.run "git checkout #{tag}"
      unless File.directory? "../../versions/#{repo}-#{tag}"
        FileUtils.cp_r "../../versions/#{repo}", "../../versions/#{repo}-#{tag}"
        FileUtils.ln_sf "../../versions/#{repo}-#{tag}", "../../build/#{repo}-#{tag}"
      end
    end
  end
end

.git_co_master(repo) ⇒ Object



358
359
360
361
362
363
364
# File 'lib/multiruby.rb', line 358

def self.git_co_master repo
  Multiruby.in_versions_dir do
    Dir.chdir repo do
      Multiruby.run "git checkout master"
    end
  end
end

.gnu_utils_build(inst_dir) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/multiruby.rb', line 212

def self.gnu_utils_build inst_dir
  run "autoconf" unless test ?f, "configure"
  run "./configure --enable-shared --prefix #{inst_dir}", "log.configure" unless
    test ?f, "Makefile"
  run "(nice make -j4; nice make)", "log.build"
  run "make install", "log.install"
end

.helpObject



220
221
222
# File 'lib/multiruby.rb', line 220

def self.help
  puts HELP.join
end

.in_build_dirObject



224
225
226
227
228
# File 'lib/multiruby.rb', line 224

def self.in_build_dir
  Dir.chdir File.join(self.root_dir, "build") do
    yield
  end
end

.in_install_dirObject



230
231
232
233
234
# File 'lib/multiruby.rb', line 230

def self.in_install_dir
  Dir.chdir File.join(self.root_dir, "install") do
    yield
  end
end

.in_root_dirObject



236
237
238
239
240
# File 'lib/multiruby.rb', line 236

def self.in_root_dir
  Dir.chdir self.root_dir do
    yield
  end
end

.in_tmp_dirObject



242
243
244
245
246
# File 'lib/multiruby.rb', line 242

def self.in_tmp_dir
  Dir.chdir File.join(self.root_dir, "tmp") do
    yield
  end
end

.in_versions_dirObject



248
249
250
251
252
# File 'lib/multiruby.rb', line 248

def self.in_versions_dir
  Dir.chdir File.join(self.root_dir, "versions") do
    yield
  end
end

.listObject



254
255
256
257
258
259
260
261
# File 'lib/multiruby.rb', line 254

def self.list
  puts "Known versions:"
  in_install_dir do
    Dir["*"].sort.each do |d|
      puts "  #{d}"
    end
  end
end

.merge_rubygemsObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/multiruby.rb', line 263

def self.merge_rubygems
  in_install_dir do
    gems = Dir["*/lib/ruby/gems"]

    unless test ?d, "../gems" then
      FileUtils.mv gems.first, ".."
    end

    gems.each do |d|
      FileUtils.rm_rf d
      FileUtils.ln_sf "../../../../gems", d
    end
  end
end

.mri_latest_tag(v) ⇒ Object



278
279
280
# File 'lib/multiruby.rb', line 278

def self.mri_latest_tag v
  Multiruby.tags.grep(/#{v}/).last
end

.rake_build(inst_dir) ⇒ Object



282
283
284
285
# File 'lib/multiruby.rb', line 282

def self.rake_build inst_dir
  run "rake", "log.build"
  FileUtils.ln_sf "../build/#{File.basename Dir.pwd}", inst_dir
end

.rbx_ln(dir) ⇒ Object



287
288
289
290
291
292
293
# File 'lib/multiruby.rb', line 287

def self.rbx_ln dir
  dir = File.expand_path dir
  Multiruby.in_versions_dir do
    FileUtils.ln_sf dir, "rubinius"
    FileUtils.ln_sf "../versions/rubinius", "../install/rubinius"
  end
end

.rm(name) ⇒ Object



295
296
297
298
299
300
301
# File 'lib/multiruby.rb', line 295

def self.rm name
  Multiruby.in_root_dir do
    FileUtils.rm_rf Dir["*/#{name}"]
    f = "versions/ruby-#{name}.tar.gz"
    File.unlink f if test ?f, f
  end
end

.root_dirObject



303
304
305
306
307
308
309
310
311
312
313
# File 'lib/multiruby.rb', line 303

def self.root_dir
  root_dir = File.expand_path(ENV['MULTIRUBY'] ||
                              File.join(ENV['HOME'], ".multiruby"))

  unless test ?d, root_dir then
    puts "creating #{root_dir}"
    Dir.mkdir root_dir, 0700
  end

  root_dir
end

.run(base_cmd, log = nil) ⇒ Object



315
316
317
318
319
320
# File 'lib/multiruby.rb', line 315

def self.run base_cmd, log = nil
  cmd = base_cmd
  cmd += " > #{log} 2>&1" if log
  puts "Running command: #{cmd}"
  raise "ERROR: Command failed with exit code #{$?}" unless system cmd
end

.setup_dirs(download = true) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/multiruby.rb', line 322

def self.setup_dirs download = true
  %w(build install versions tmp).each do |dir|
    unless test ?d, dir then
      puts "creating #{dir}"
      Dir.mkdir dir
      if dir == "versions" && download then
        warn "  Downloading initial ruby tarballs to ~/.multiruby/versions:"
        VERSIONS.each do |v|
          self.fetch_tar v
        end
        warn "  ...done"
        warn "  Put other ruby tarballs in ~/.multiruby/versions to use them."
      end
    end
  end
end

.svn_co(url, dir) ⇒ Object



339
340
341
342
343
344
# File 'lib/multiruby.rb', line 339

def self.svn_co url, dir
  Multiruby.in_versions_dir do
    Multiruby.run "svn co #{url} #{dir}" unless File.directory? dir
    FileUtils.ln_sf "../versions/#{dir}", "../build/#{dir}"
  end
end

.tags(scm = "svn") ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/multiruby.rb', line 366

def self.tags(scm="svn")
  tags = nil
  Multiruby.in_tmp_dir do
    cache = "#{scm}.tag.cache"
    File.unlink cache if Time.now - File.mtime(cache) > 3600 rescue nil

    File.open cache, "w" do |f|
      case scm
      when "svn" then
        f.write `svn ls #{MRI_SVN}/tags/`
      when "git" then
        f.write `git tag`
      else
        warn "Unsupported SCM: #{scm}"
      end
    end unless File.exist? cache

    tags = File.read(cache).split(/\n/).grep(/^v/).reject {|s| s =~ /preview/}
  end

  tags = tags.sort_by { |s| s.scan(/\d+/).map { |s| s.to_i } }
end

.updateObject



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/multiruby.rb', line 389

def self.update
  # TODO:
  # update will look at the dir name and act accordingly rel_.* will
  # figure out latest tag on that name and svn sw to it trunk and
  # others will just svn update

  clean = []

  self.each_scm_build_dir do |style|
    dir = File.basename(Dir.pwd)
    warn dir

    case style
    when :svn then
      case dir
      when /mri_\d/ then
        system "svn cleanup" # just in case
        svn_up = `svn up`
        in_build_dir do
          if svn_up =~ /^[ADUCG] / then
            clean << dir
          else
            warn "  no update"
          end
          FileUtils.ln_sf "../build/#{dir}", "../versions/#{dir}"
        end
      when /mri_rel_(.+)/ then
        ver = $1
        url = `svn info`[/^URL: (.*)/, 1]
        latest = self.mri_latest_tag(ver).chomp('/')
        new_url = File.join(File.dirname(url), latest)
        if new_url != url then
          run "svn sw #{new_url}"
          clean << dir
        else
          warn "  no update"
        end
      else
        warn "  update in this svn dir not supported yet: #{dir}"
      end
    when :git then
      case dir
      when /rubinius|jruby/ then
        run "git pull", nil
        run "rake build" # minor cheat by building here
      else
        warn "  update in this git dir not supported yet: #{dir}"
      end
    else
      warn "  update in non-svn dir not supported yet: #{dir}"
    end
  end

  in_install_dir do
    clean.each do |dir|
      warn "removing install/#{dir}"
      FileUtils.rm_rf dir
    end
  end
end

.update_rubygemsObject



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/multiruby.rb', line 450

def self.update_rubygems
  warn "  Determining latest version for rubygems"
  html = URI.parse(GEM_URL).read

  versions = html.scan(/href="rubygems-update-(\d+(?:\.\d+)+).gem/i).flatten
  latest = versions.sort_by { |s| s.scan(/\d+/).map { |s| s.to_i } }.last

  Multiruby.in_versions_dir do
    file = "rubygems-#{latest}.tgz"
    unless File.file? file then
      warn "    Fetching rubygems-#{latest}.tgz via HTTP."
      File.unlink(*Dir["rubygems*"])
      File.open file, 'w' do |f|
        f.write URI.parse(GEM_URL+"/"+file).read
      end
    end
  end

  Multiruby.in_install_dir do
    FileUtils.rm_rf Dir["*"]
  end
end