Class: MotherBrain::Bootstrap::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/mb/bootstrap/template.rb

Class Method Summary collapse

Class Method Details

.find(name_or_path = nil) ⇒ String?

Find and validate a user-chosen template

Parameters:

  • name_or_path (String) (defaults to: nil)

    User’s choice of template

Returns:

  • (String)

    the actual path the the chosen template

  • (nil)

    if there is no template

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mb/bootstrap/template.rb', line 55

def find(name_or_path=nil)
  name_or_path ||= MB::Application.config.bootstrap.default_template
  return unless name_or_path
  installed = MB::FileSystem.templates.join("#{name_or_path}.erb").to_s
  if File.exists?(installed)
    return installed
  end
  if File.exists?(name_or_path)
    return name_or_path
  else
    raise MB::BootstrapTemplateNotFound
  end
end

.install(name, filename_or_url) ⇒ Object

Install a bootstrap template into the templates directory

Parameters:

  • name (String)

    the name of the template

  • filename_or_url (String)

    a local filename or URL to download

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mb/bootstrap/template.rb', line 13

def install(name, filename_or_url)
  template_path = MB::FileSystem.templates.join("#{name}.erb")
  if template_path.exist?
    raise MB::BootstrapTemplateNotFound, "Template named `#{name}` already installed"
  end
  MB.log.info "Installing bootstrap template `#{name}` from #{filename_or_url}"
  name += ".erb"
  if filename_or_url.match(URI.regexp)
    if filename_or_url.match(URI.regexp(['http','https']))
      uri = URI.parse(filename_or_url)
      begin
        conn = Faraday.new do |b|
          b.use Ridley::Middleware::FollowRedirects
          b.adapter :net_http
        end
        response = conn.get filename_or_url
        template_path.open("w+") do |file|
          file.write(response.body)
        end
      rescue Exception => ex
        raise MB::BootstrapTemplateNotFound, ex
      end
    else
      raise MB::BootstrapTemplateNotFound, "Only http/https URLs are supported"
    end
  elsif File.exists?(filename_or_url)
    FileUtils.copy(filename_or_url, template_path.to_s)
  else
    raise MB::BootstrapTemplateNotFound, "Couldn't find the template to install or the protocol given is not supported."
  end
end