Sha256: 7fa165e9edc062e36da4a25ea01bb24a81e513e33b1e999b5824c97739c40cb3

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'active_support'
require 'active_support/core_ext'
require 'fileutils'
require 'pry'

require 'canpe/repository'

module Canpe
  class RepositoryStore
    attr_reader :path

    LOAD_PATHS = [Dir.pwd, ENV['HOME'], Gem::Specification.find_by_name('canpe').gem_dir]
    DEFAULT_REPOSITORY_DIR = '.canpe_repos'

    class << self
      def repository_stores
        stores = []
        LOAD_PATHS.each do |path|
          repository_store_path = File.join(path, DEFAULT_REPOSITORY_DIR)
          stores << new(path) if File.directory?(repository_store_path)
        end
        stores
      end

      def repository_list
        @_repository_list ||= repository_stores.map { |store| store.repository_list }.flatten
      end
    end

    def initialize(path)
      @path = path
    end

    def root_url
      path
    end

    def repository_dir
      DEFAULT_REPOSITORY_DIR
    end

    def repositories_root
      File.join(root_url, repository_dir)
    end

    def repository_list
      repository_list = []
      dir_list = Dir.glob(File.join(repositories_root, '*'))
      dir_list.each do |dir_path|
        repository_list << Repository.new(File.basename(dir_path), self) if File.directory? dir_path
      end
      repository_list
    end

    def name
      if path == ENV['HOME']
        'HOME'
      else
        Pathname.new(path).relative_path_from(Pathname.new(ENV['HOME']))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
canpe-0.1.1 lib/canpe/repository_store.rb
canpe-0.1.0 lib/canpe/repository_store.rb