Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#download(url, dest) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/croc.rb', line 107

def download(url, dest)
  File.open(dest, "w") do |f|
    url = URI.parse(url)
    http = Net::HTTP.new(url.host, url.port)
    http.request_get(url.path) do |resp|
      resp.read_body do |s|
        f.write(s)
      end
    end
  end
end

#find_gem_homeObject

Figure out where rdocs are installed.



8
9
10
11
12
# File 'lib/croc.rb', line 8

def find_gem_home
  if File.exists?("/Library/Ruby/Gems/1.8/gems")
    return "/Library/Ruby/Gems/1.8/gems"
  end
end

#get_specsObject

Collect specs for most recent version of all installed gems.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/croc.rb', line 42

def get_specs
  specs = {}
  Gem.source_index.search(Gem::Dependency.new("", nil)).each do |spec|
    next unless spec.has_rdoc?
    if specs[spec.name]
      specs[spec.name] = spec if (spec.version <=> specs[spec.name].version) > 0
    else
      specs[spec.name] = spec
    end
  end
  specs.values
end

#index_rdoc(name, rdoc_dir) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
# File 'lib/croc.rb', line 55

def index_rdoc(name, rdoc_dir)
  unless File.exists?(rdoc_dir)
    puts "WARN Couldn't find rdoc dir #{rdoc_dir}"
    return
  end

  methods_file = File.join(rdoc_dir, "fr_method_index.html")
  unless File.exists?(methods_file)
    puts "ERROR Couldn't find #{methods_file}"
  end

  classes_file = File.join(rdoc_dir, "fr_class_index.html")
  unless File.exists?(classes_file)
    puts "ERROR Couldn't find #{classes_file}"
  end

  # collect gem
  gem = {:name => name, :dir => rdoc_dir, :classes => {}}
  $gems << gem

  # collect classes
  doc = Hpricot(open(classes_file))
  (doc/"#index-entries a").each do |el|
    klass = el.inner_html
  
    if $classes[klass]
      puts "Already have class #{klass}"
    else
      gem[:classes][klass] = {:name => klass, :url => el["href"], :methods => []}
    end
  end

  # collect methods
  doc = Hpricot(open(methods_file))
  (doc/"#index-entries a").each do |el|
    if el.inner_html =~ /(\S+)\s+\((\S+)\)/
      method = $1
      if klass = gem[:classes][$2]
        el["href"] =~ /#(.*)$/
        url = $1
        klass[:methods] << {:name => method, :class => klass, :url => url}
      else
        # puts %Q(Couldn't find class "#{$2} for method #{method}")
      end
    else
      # puts %Q(Couldn't get method and class from "#{el.inner_html}")
    end
  end

  puts "Indexed #{name}"
end

#install_assetsObject

Creates ~/.croc and installs assets from public directory.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/croc.rb', line 20

def install_assets
  croc_dir = File.join(user_home_dir, ".croc")
  unless File.exists?(croc_dir)
    Dir.mkdir(croc_dir)
    Dir.mkdir(File.join(croc_dir, "rdoc"))
  end

  public_dir = File.join(File.dirname(__FILE__), "..", "public")
  Dir.new(public_dir).each do |f|
    next if f == "." || f == ".."
    File.copy(File.join(public_dir, f), File.join(croc_dir, f))
  end
end

#install_rdocs(url, src_dir_name, dest_dir_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/croc.rb', line 119

def install_rdocs(url, src_dir_name, dest_dir_name)
  croc_rdoc_dir = File.join($croc_home, "rdoc")
  Dir.chdir(croc_rdoc_dir)
  puts "Installing #{url}"
  tgz_file = File.join(croc_rdoc_dir, "dest.tgz")
  src_file = File.join(croc_rdoc_dir, src_dir_name)
  dest_file = File.join(croc_rdoc_dir, dest_dir_name)
  download(url, tgz_file)
  `tar xzf #{tgz_file}`
  File.rename(src_file, dest_file)
  File.delete(tgz_file)
end

Print installed gems and whether rdocs are available and indeed installed.



35
36
37
38
39
# File 'lib/croc.rb', line 35

def print_gems
  Gem.source_index.search(Gem::Dependency.new("", nil)).each do |spec|
    puts "#{spec.name} #{spec.version} #{spec.has_rdoc? ? "has rdoc" : ""} #{File.exists?(rdoc_dir) ? "and it exists" : "but it doesn't exist"}"
  end
end

#user_home_dirObject

Returns the user’s home directory as a string.



15
16
17
# File 'lib/croc.rb', line 15

def user_home_dir
  ENV["HOME"]
end