Skip to content

Instantly share code, notes, and snippets.

@koyachi
Created February 5, 2010 07:59
Show Gist options
  • Save koyachi/295626 to your computer and use it in GitHub Desktop.
Save koyachi/295626 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'fileutils'
require 'webrick'
def bundle(your_gem_dir=nil)
your_gem_dir ||='./gems'
FileUtils.mkdir_p File.expand_path(your_gem_dir)
Gem.path.map{|path| Dir.glob(path + '/cache/*.gem')}.flatten.map{|path| FileUtils.cp(path, "#{your_gem_dir}/#{path.split('/')[-1]}")}
system "gem generate_index"
end
def server(port=nil)
port ||= 8887
document_root = File.expand_path(File.dirname(__FILE__) + '/../')
generate_indexhtml
server = WEBrick::HTTPServer.new({:DocumentRoot => document_root, :BindAddress => 'localhost', :Port => port})
%w[INT TERM].each do |signal|
Signal.trap(signal){ server.shutdown }
end
server.start
end
def generate_indexhtml(your_gem_dir=nil)
your_gem_dir ||='./gems'
your_gem_dir = File.expand_path(your_gem_dir)
document_root ||= File.expand_path("#{your_gem_dir}/../")
gems = {}
Dir.glob(your_gem_dir + '/*.gem').map do |path|
name = path.split('/')[-1]
name_id = name.split('-')[0..-2].join('')
ver = name.split('-')[-1].split('.')[0..-2].join('.')
gems[name_id] ||= []
gems[name_id] << ver
end
html_gems = gems.keys.sort.map do |name|
["<div>#{name} ",
"[#{gems[name].map{|v| '<span class="hoge">' + v + '</span>'}.join(', ')}]",
"</div>"].flatten.join('')
end
html = <<"HTML"
<html>
<head>
<title>minigem server</title>
<style>
.hoge {
background: #e0e0e0;
color: #00C000;
}
</style>
</head>
<body>
<h1>available gems</h1>
<div>
#{html_gems}
</div>
</body>
</html>
HTML
open("#{document_root}/index.html", "w") do |f|
f.write html
end
end
def scp(dest)
cmd = "scp -r ../minigem #{dest}"
puts cmd
system cmd
end
case ARGV[0]
when 'bundle'
bundle ARGV[1]
when 'server'
server ARGV[1]
when 'scp'
scp ARGV[1]
else
puts "USAGE: minigem [bundle|server|scp]"
end
__END__
NAME:
minigem
DESCRIPTION:
ローカルマシンに入ってるgemを外部ネットにつながらないマシンにインストール可能な状態にしたい
USAGE:
minigem bundle
minigem scp [dest]
minigem server
1. ローカルマシンでminigem bundle
2. minigemディレクトリごと外部アクセスできないマシンがアクセスできるとこに置く
3. 置いた先でminigem server
4. 置いた先を参照するマシンでgem source -a http://置いた先:8887
DIRECTORY TREE:
./minigem
/bin/minigem.rb - this file
/gems/ - bundled gems here
/index.html - available gem list
AUTHOR:
2010-02-05 koyachi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment