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



11
12
13
14
15
16
# File 'lib/begin/repository.rb', line 11

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



39
40
41
42
# File 'lib/begin/repository.rb', line 39

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

#install(source_uri, name) ⇒ Object



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

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

#install_prerequisites(name) ⇒ Object



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

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



44
45
46
47
# File 'lib/begin/repository.rb', line 44

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

#template_from_path(path) ⇒ Object



63
64
65
66
# File 'lib/begin/repository.rb', line 63

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

#template_name(uri) ⇒ Object



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

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



59
60
61
# File 'lib/begin/repository.rb', line 59

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