File: generate_yaml_index.rb

package info (click to toggle)
jruby 1.5.6-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 45,088 kB
  • ctags: 77,093
  • sloc: ruby: 398,491; java: 170,202; yacc: 3,782; xml: 2,529; sh: 299; tcl: 40; makefile: 35; ansic: 23
file content (78 lines) | stat: -rwxr-xr-x 1,946 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env jruby

# Generate the yaml/yaml.Z index files for a gem server directory.
#
# Usage:  generate_yaml_index.rb --dir DIR [--verbose]

$:.unshift '~/rubygems' if File.exist? "~/rubygems"

require 'optparse'
require 'rubygems'
require 'zlib'

Gem.manage_gems

class Indexer

  def initialize(options)
    @options = options
    @directory = options[:directory]
  end

  def gem_file_list
    Dir.glob(File.join(@directory, "gems", "*.gem"))
  end

  def build_index
    build_uncompressed_index
    build_compressed_index
  end
  
  def build_uncompressed_index
    puts "Building yaml file" if @options[:verbose]
    File.open(File.join(@directory, "yaml"), "w") do |file|
      file.puts "--- !ruby/object:Gem::Cache"
      file.puts "gems:"
      gem_file_list.each do |gemfile|
        spec = Gem::Format.from_file_by_path(gemfile).spec
	puts "   ... adding #{spec.full_name}" if @options[:verbose]
        file.puts "  #{spec.full_name}: #{spec.to_yaml.gsub(/\n/, "\n    ")[4..-1]}"
      end
    end
  end

  def build_compressed_index
    puts "Building yaml.Z file" if @options[:verbose]
    File.open(File.join(@directory, "yaml.Z"), "w") do |file|
      file.write(Zlib::Deflate.deflate(File.read(File.join(@directory, "yaml"))))
    end
  end
end


options = {
  :directory => '.',
  :verbose => false,
}

ARGV.options do |opts|
  opts.on_tail("--help", "show this message") {puts opts; exit}
  opts.on('-d', '--dir=DIRNAME', "base directory containing gems subdirectory", String) do |value|
    options[:directory] = value
  end
  opts.on('-v', '--verbose', "show verbose output") do |value|
    options[:verbose] = value
  end
  opts.parse!
end

if options[:directory].nil?
  puts "Error, must specify directory name. Use --help"
  exit
elsif ! File.exist?(options[:directory]) ||
    ! File.directory?(options[:directory])
  puts "Error, unknown directory name #{directory}."
  exit
end

Indexer.new(options).build_index