Sha256: dc2c75caae59395c6a85a1177da6b839952f18202a1d459831a3007ceca3e390

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

class Primo
  class Remote
    DIRECTORY = File.expand_path('~/.primo_remotes').freeze

    def self.list
      Primo::Config.instance.config[:remotes]
    end

    attr_accessor :name, :url

    def initialize name, url = nil
      @name = name
      @url = url || Primo::Config.instance.config[:remotes][name]
      ensure_saved
    end

    def templates
      template_names.map {|name| Primo::Template.new(name, self)}
    end

    def update
      ensure_cloned
      system "cd #{directory}; git fetch -q && git reset --hard origin/master -q"
    end

    def remove
      Primo::Config.instance.config[:remotes].delete(name)
      Primo::Config.instance.save
      FileUtils.rm_rf directory
    end

    def directory
      "#{DIRECTORY}/#{name}"
    end

    def ensure_cloned
      unless File.exists? "#{directory}/.git"
        system "git clone #{url} #{directory} -q"
      end
    rescue
      raise "Could not load templates from #{url}"
    end

    private

    def ensure_saved
      if !Primo::Config.instance.config[:remotes][name] && url
        Primo::Config.instance.config[:remotes][name] = url
        Primo::Config.instance.save
      end
    end

    def template_names
      Dir.entries(directory).select do |filename|
        filename =~ /\.rb$/
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
primo-0.1.0 lib/primo/remote.rb