Class: Skeletor::Skeletons::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/skeletor/skeletons/loader.rb

Overview

Loader is a wrapper class to handle loading in the template file from the various possible load paths.

While accessible externally this is generally called internally.

Constant Summary collapse

TEMPLATE_PATH =

TEMPLATE_PATH specifies the internal directory for any included project templates.

File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), "templates"))
USER_TEMPLATE_PATH =

USER_TEMPLATE_PATH specifies the path to the users personal template directory

File.expand_path('~/.skeletor/templates')

Class Method Summary collapse

Class Method Details

.load_template(template) ⇒ Object

Searches for the specified template and loads it into a variable

Also adds the path where it was found to the returned Hash



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/skeletor/skeletons/loader.rb', line 22

def self.load_template(template)
  
  puts 'Loading Template - ' + template
  
  #if File.exists?(template) && !File.directory?(template)
  #  skeleton = YAML.load_file(template)
  #  path = File.dirname(template)
  #elsif File.exists?(File.join(template,File.basename(template) + '.yml'))
  #  skeleton = YAML.load_file(File.join(template,File.basename(template) + '.yml'))
  #  path = template
  #elsif File.exists?(File.join(USER_TEMPLATE_PATH,template,template+'.yml'))
  #  skeleton = YAML.load_file(File.join(USER_TEMPLATE_PATH,template,template+'.yml'))
  #  path = File.join(USER_TEMPLATE_PATH,template)
  #elsif File.exists?(File.join(TEMPLATE_PATH,template,template+'.yml'))
  #  skeleton = YAML.load_file(File.join(TEMPLATE_PATH,template,template+'.yml'))
  #  path = File.join(TEMPLATE_PATH,template)
  #else
  #  raise LoadError, 'Error: Template File ' + File.basename(template) + ' Could Not Be Found'
  #end
  
  if File.exists?(template) && !File.directory?(template)
    result = template
  else
    trail = Hike::Trail.new "/"
    trail.append_extensions ".yml", ".json" 
    
    if File.directory?(template)
      trail.append_paths template
      result = trail.find File.basename(template)
    else
      trail.append_paths USER_TEMPLATE_PATH, TEMPLATE_PATH
      result = trail.find File.basename(template) + "/" + File.basename(template)
    end
          
  end
  
  if result
    begin
      file = File.basename(result)
      path = File.dirname(result)
      skeleton = Grayskull::DataFile.load(result)
      skeleton['file'] = file
      skeleton['path'] = path
      puts 'Template ' + file + ' loaded from ' + path
   rescue Exception => e
    puts e.message
    exit
   end
  end
  
  puts 'Template ' + file + ' loaded from ' + path
          
  return skeleton
  
end