Sha256: 8ff68f8f5a4ade8314a6d78f3e5949f8f197257ca86753a212fabfee335ba833

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'fileutils'

module Automatiek
  class Gem
    def initialize(gem_name, &block)
      @gem_name = gem_name
      block.call(self) if block
    end

    def vendor!(version)
      update(version)
      namespace_files
      clean
    end

    def download=(opts = {}, &block)
      if block
        @download = block
      elsif github = opts.delete(:github)
        @download = lambda do |version|
          Dir.chdir File.dirname(vendor_lib) do
            `curl -L #{github}/archive/#{version}.tar.gz | tar -xz`
            puts `ls`
            unless $?.success?
              raise "Downloading & untarring #{gem_name} (#{version}) failed"
            end
            FileUtils.mv "#{github.split("/").last}-#{version.sub(/^v/, "")}", gem_name
          end
        end
      end
    end

    attr_accessor :gem_name
    attr_accessor :namespace
    attr_accessor :prefix
    attr_accessor :vendor_lib

    def update(version)
      FileUtils.rm_rf vendor_lib
      @download.call(version)
    end

    def files
      Dir.glob("#{vendor_lib}/**/*.rb")
    end

    def namespace_files
      require_target = vendor_lib.sub(%r{^(.+?/)?lib/}, "") << "/lib"
      process_files(namespace, "#{prefix}::#{namespace}")
      process_files(/require (["'])#{Regexp.escape gem_name}/, "require \\1#{require_target}/#{gem_name}")
      process_files(/(autoload\s+[:\w]+,\s+["'])(#{Regexp.escape gem_name}[\w\/]+["'])/, "\\1#{require_target}/\\2")
    end

    def clean
      files = Dir.glob("#{vendor_lib}/*", File::FNM_DOTMATCH).reject {|f| %(. .. lib).include? f.split("/").last }
      FileUtils.rm_r files
    end

    private

    def process_files(regex, replacement = "")
      files.each do |file|
        contents = File.read(file)
        contents.gsub!(regex, replacement)
        File.open(file, "w") {|f| f << contents }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
automatiek-0.1.0 lib/automatiek/gem.rb