class Vorx::Store

Attributes

base_path[RW]
stderr[RW]

Public Class Methods

new(base_path = '~/.vorx/store', stderr: $stderr, store_file: 'vorx_store.yml', repository_prefix: '') click to toggle source
# File lib/vorx/store.rb, line 7
def initialize(base_path = '~/.vorx/store', stderr: $stderr, store_file: 'vorx_store.yml', repository_prefix: '')
  @base_path = Pathname.new(base_path.to_s)
  @stderr = stderr
  @repository_prefix = repository_prefix

  @base_path.mkpath

  @store = YAML::Store.new(@base_path.join(store_file).to_s)
  @store.transaction { @store[:repositories] ||= Set.new }
end

Public Instance Methods

add(git_reference) click to toggle source
# File lib/vorx/store.rb, line 32
def add(git_reference)
  git_repository = resolve_git_reference(git_reference)

  @store.transaction { @store[:repositories] << git_repository }

  git_repository
end
all() click to toggle source
# File lib/vorx/store.rb, line 62
def all
  @store.transaction { @store[:repositories] }
end
delete(git_reference) click to toggle source
# File lib/vorx/store.rb, line 48
def delete(git_reference)
  git_repository = resolve_git_reference(git_reference)

  `rm -rf #{git_folder(git_repository)}`

  @store.transaction { @store[:repositories].delete(git_repository) }
end
delete_all() click to toggle source
# File lib/vorx/store.rb, line 56
def delete_all
  all.each do |repo|
    delete(repo)
  end
end
fetch(git_reference) click to toggle source
# File lib/vorx/store.rb, line 18
def fetch(git_reference)
  git_repository = find(git_reference) || add(git_reference)

  git_fetch_references(git_repository)

  update_repository(git_repository, cloned: true)
end
fetch_all() click to toggle source
# File lib/vorx/store.rb, line 26
def fetch_all
  all.each do |repo|
    fetch(repo)
  end
end
find(git_reference) click to toggle source
# File lib/vorx/store.rb, line 40
def find(git_reference)
  git_repository = resolve_git_reference(git_reference)

  git_repositories.detect do |gr|
    gr == git_repository
  end
end
reload() click to toggle source
# File lib/vorx/store.rb, line 66
def reload
  @store.transaction do
    repos = @store[:repositories]

    repos.select { |r| git_folder(r).exist? }.each do |repo|
      repos.delete(repo)
      repos << repo.update(cloned: true)
    end
  end
end

Private Instance Methods

git_clone(git_repository) click to toggle source
# File lib/vorx/store.rb, line 107
def git_clone(git_repository)
  Git.clone(git_repository.git.to_s, git_folder(git_repository), branch: git_repository.version)
end
git_fetch(git_repository) click to toggle source
# File lib/vorx/store.rb, line 111
def git_fetch(git_repository)
  git_repo = Git.open(git_folder(git_repository))
  git_repo.fetch
end
git_fetch_references(git_repository) click to toggle source
# File lib/vorx/store.rb, line 100
def git_fetch_references(git_repository)
  return git_clone(git_repository) unless git_repository.cloned?

  git_fetch(git_repository)
  git_pull(git_repository)
end
git_folder(git_repository) click to toggle source
# File lib/vorx/store.rb, line 122
def git_folder(git_repository)
  @base_path.join(git_repository.folder_name)
end
git_pull(git_repository) click to toggle source
# File lib/vorx/store.rb, line 116
def git_pull(git_repository)
  git_repo = Git.open(git_folder(git_repository))

  git_repo.pull
end
git_repositories() click to toggle source
# File lib/vorx/store.rb, line 96
def git_repositories
  @store.transaction { @store[:repositories] }
end
resolve_git_reference(git_reference) click to toggle source
# File lib/vorx/store.rb, line 79
def resolve_git_reference(git_reference)
  return git_reference if git_reference.is_a?(GitRepository)

  GitReference.resolve(git_reference, prefix: @repository_prefix)
end
update_repository(git_repository, **params) click to toggle source
# File lib/vorx/store.rb, line 85
def update_repository(git_repository, **params)
  @store.transaction do
    repos = @store[:repositories]

    repos.delete(git_repository)
    repos << git_repository.update(**params)
  end
end