Class: Begin::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/begin/repository.rb

Overview

Provides centralised access to the local repository of templates on the machine

Instance Method Summary collapse

Constructor Details

#initialize(name = '.begin', parent_dir = '~') ⇒ Repository

Returns a new instance of Repository.



13
14
15
16
17
18
# File 'lib/begin/repository.rb', line 13

def initialize(name = '.begin', parent_dir = '~')
  @parent_dir = Path.new(parent_dir, '.', 'Repository Parent')
  @parent_dir.ensure_dir_exists
  @repo_dir = Path.new(name, @parent_dir, 'Repository directory')
  @template_dir = Path.new('templates', @repo_dir, 'Templates directory')
end

Instance Method Details

#eachObject



41
42
43
44
# File 'lib/begin/repository.rb', line 41

def each
  templates = @template_dir.dir_contents
  templates.each { |x| yield template_name x }
end

#install(source_uri, name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/begin/repository.rb', line 20

def install(source_uri, name)
  path = install_prerequisites(name)
  begin
    GitTemplate.install source_uri, path
  rescue StandardError
    return SymlinkTemplate.install source_uri, path unless source_uri.include? '://'

    raise
  end
end

#install_prerequisites(name) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/begin/repository.rb', line 31

def install_prerequisites(name)
  @repo_dir.make_dir
  @template_dir.make_dir
  path = template_path name
  raise "A template is already installed at: #{path}" if path.exists?

  Output.info "Installing to '#{path}'"
  path
end

#template(name) ⇒ Object



46
47
48
49
# File 'lib/begin/repository.rb', line 46

def template(name)
  path = template_path name
  template_from_path path
end

#template_from_path(path) ⇒ Object



65
66
67
68
69
# File 'lib/begin/repository.rb', line 65

def template_from_path(path)
  return SymlinkTemplate.new(path) if File.symlink? path

  GitTemplate.new path
end

#template_name(uri) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/begin/repository.rb', line 51

def template_name(uri)
  uri = URI(uri)
  path_bits = uri.path.split '/'
  name = path_bits.last
  name.slice! 'begin-'
  name.slice! 'begin_'
  name.chomp! '.git'
  name
end

#template_path(template_name) ⇒ Object



61
62
63
# File 'lib/begin/repository.rb', line 61

def template_path(template_name)
  Path.new template_name, @template_dir, 'Template directory'
end