Sha256: ca4c1d86f179f24c6b1bd76c07e2601c1021ca209b087005dcc4fe5b7e35b975

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

require "digest"
require "fileutils"
require "open-uri"

module Linner
  class Bundler
    VENDOR = Pathname(".").expand_path.join "vendor"
    REPOSITORY = File.expand_path "~/.linner/bundles"

    Bundle = Struct.new(:name, :version, :url) do
      def path
        File.join(REPOSITORY, name, version, File.basename(url))
      end
    end

    def initialize(bundles)
      @bundles = []
      bundles.each do |name, props|
        @bundles << Bundle.new(name, props["version"], props["url"])
      end
    end

    def check
      return [false, "Bundles didn't exsit!"] unless File.exists? REPOSITORY
      @bundles.each do |bundle|
        unless File.exists?(bundle.path) and File.exists?(File.join(VENDOR, bundle.name))
          return [false, "Bundle #{bundle.name} v#{bundle.version} didn't match!"]
        end
      end
      return [true, "Perfect bundled, ready to go!"]
    end

    def install
      unless File.exists? REPOSITORY
        FileUtils.mkdir_p(REPOSITORY)
      end
      @bundles.each do |bundle|
        if bundle.version != "master"
          next if File.exists?(bundle.path) and File.exists?(File.join(VENDOR, bundle.name))
        end
        puts "Installing #{bundle.name} #{bundle.version}..."
        install_to_repository bundle.url, bundle.path
        link_to_vendor bundle.path, File.join(VENDOR, bundle.name)
      end
    end

    def perform
      check and install
    end

    private
    def install_to_repository(url, path)
      FileUtils.mkdir_p File.dirname(path)
      File.open(path, "w") do |dist|
        open(url, "r:UTF-8") {|file| dist.write file.read}
      end
    end

    def link_to_vendor(path, dist)
      if !File.exist?(dist) or Digest::MD5.file(path).hexdigest != Digest::MD5.file(dist).hexdigest
        FileUtils.mkdir_p File.dirname(dist)
        FileUtils.cp path, dist
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
linner-0.6.1 lib/linner/bundler.rb
linner-0.6.0 lib/linner/bundler.rb
linner-0.5.1 lib/linner/bundler.rb
linner-0.5.0 lib/linner/bundler.rb