• 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

Gemを作るのが面倒になってきたので、githubから直接requireできたら楽になるかもしれないと思い、試してみました。

   1  def git(uri, sha1, options = {})
   2    require "tmpdir"
   3    basename = File.basename(uri)
   4    outdir = File.join(Dir.tmpdir, basename, sha1)
   5    unless File.exist?(outdir)
   6      sh = proc{|command| IO.popen("#{command} 2>&1"){|io| io.read}}
   7      sh["git clone #{uri} #{outdir}"]
   8      sh["cd #{outdir}; git checkout #{sha1}"]
   9    end
  10    $:.unshift File.join(outdir, 'lib')
  11    require options[:require_as] || basename.split(/\.git$/)[0]
  12  end

Dir.tmpdirを使ってOSのテンポラリディレクトリの下にリポジトリをクローンしてきて、指定したリビジョンをcheckoutし、LOAD_PATHに"lib"を加えて、リポジトリ名のファイルをrequireしています。

意外と普通にgemの代わりに使える感じです。

posted by Png genki on Thu 16 Jul 2009 at 17:40

Gist はちょっとしたコードの切れ端を貼付けておくのに便利です。 先日紹介したEndless RubyのコードもGist上でのみ公開されていました。 Gemを作るまでもないコードをGistに貼付けて、そのままrequireして使えると便利そうだと思ったので、試してみました。

   1  module Gist
   2  module_function
   3    def require(id, sha1 = "HEAD")
   4      Kernel.require "open-uri"
   5      gist = "http://gist.github.com"
   6      eval(if sha1 == "HEAD"
   7        open("#{gist}/#{id}.txt").read
   8      else
   9        Kernel.require "tmpdir"
  10        cache = File.join(Dir.tmpdir, "gist-#{id}-#{sha1}")
  11        if File.exist?(cache)
  12          open(cache).read
  13        else
  14          open("#{gist}/raw/#{id}/#{sha1}") do |i|
  15            code = i.read
  16            open(cache, "w"){|o| o.write code}
  17            code
  18          end
  19        end
  20      end)
  21    end
  22  end
  23  
  24  Gist.require("148479", "a59ea9ec3e865bafd1e4413b43b6ccb7a38d76fc")
  25  #=> Hello, world!

Hash値で内容の変更が検出可能なので、md5のチェックサムを信用している人にとっては同程度の安全性で利用できそうです。

posted by Png genki on Thu 16 Jul 2009 at 17:31
Contents
Gemの代わりにGitHubから直接requireする方法
Gistからrequireする方法
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
瀧内元気: おお、チェックしてみます。thx! '11-12
overisland: Reeder for iPhone もこの UI を実装していますね。 '11-12
瀧内元気: その情報は見たのですが、以下のサイトによると、現在はまた必要になってるっぽいんですよね。 ... '11-12
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ