Class: Skeletor::Builder

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

Overview

The Builder class performs the main task of parsing the loaded Skeleton and generating a project structure from it.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, template, path) ⇒ Builder

Creates a new Builder instance



10
11
12
13
14
15
16
17
# File 'lib/skeletor/builder.rb', line 10

def initialize(project,template,path)
  
  @project = project
  @template_name = File.basename(template).gsub('.yml','')
  @template = Skeletons::Skeleton.new(template)
  @path = path
  
end

Class Method Details

.clean(path = @path) ⇒ Object

Cleans the directory of all files and folders



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/skeletor/builder.rb', line 79

def self.clean(path=@path)
  
  puts 'Cleaning directory of files and folders'
  start_dir = Dir.new(path)
  
  start_dir.each{
    |dir|
    
    if dir != '.' && dir != '..'
      FileUtils.rm_r File.join(path,dir), {:secure=>true}  
    end
    
  }
  puts 'Directory cleaned'
  
end

Instance Method Details

#buildObject

Builds the project skeleton

If the target directory does not exist it is created, then the directory structure is generated.

Once the folder structure is set up and all includes have been loaded/created, any tasks specified in the template file are run.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/skeletor/builder.rb', line 26

def build()
  
  #check dir exists, if not, make it
  if(!File.exists?(@path))
    Dir.mkdir(@path)
  end
  
  #build directory structure
  puts 'Building directory structure'
  build_skeleton(@template.directory_structure)
  
  #execute build tasks
  puts 'Running build tasks'
  execute_tasks(@template.tasks,@template.path)
  
  puts 'Skeleton built'
  
end

#build_skeleton(dirs, path = @path) ⇒ Object

Builds the directory structure



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/builder.rb', line 46

def build_skeleton(dirs,path=@path)
    
    dirs.each{
      |node|
      
      if node.kind_of?(Hash) && !node.empty?()
        node.each_pair{
          |dir,content| 
      
          dir = replace_tags(dir)
      
          puts 'Creating directory ' + File.join(path,dir)
          Dir.mkdir(File.join(path,dir))
    
          if content.kind_of?(Array) && !content.empty?()
            build_skeleton(content,File.join(path,dir))
          end
          
        }
      elsif node.kind_of?(Array) && !node.empty?()
        node.each{
          |file|
          
          write_file(file,path)
    
        }
      end
      
  }
  
end

#execute_tasks(tasks, template_path) ⇒ Object

Parses the task string and runs the task.

Will check Skeleton::Tasks module first before running tasks from the ‘tasks.rb` file in the template directory.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/skeletor/builder.rb', line 120

def execute_tasks(tasks,template_path)
  
  if File.exists?(File.expand_path(File.join(template_path,'tasks.rb')))
    load File.expand_path(File.join(template_path,'tasks.rb'))
  end
   
  tasks.each{
    |task|
    
    puts 'Running Task: ' + task
    
    task = replace_tags(task);
    
    options = task.split(', ')
    action = options.slice!(0)
    
    if(Tasks.respond_to?(action))
      Tasks.send action, options.join(', ')
    else
      send action, options.join(', ')
    end
    
  }
              
end

#replace_tags(str) ⇒ Object

Parses supplied string and replaces any instances of following tags with the relavant internal variable:

<skeleton_path>, <skeleton_project>, <skeleton_template>



152
153
154
155
156
157
# File 'lib/skeletor/builder.rb', line 152

def replace_tags(str)
 str = str.gsub('<skeleton_path>',@path)
 str = str.gsub('<skeleton_project>',@project)
 str = str.gsub('<skelton_template>',@template_name)
 return str
end

#write_file(file, path) ⇒ Object

Checks if file is listed in the includes list and if so copies it from the given location. If not it creates a blank file.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/skeletor/builder.rb', line 98

def write_file(file,path)
  #if a pre-existing file is specified in the includes list, copy that, if not write a blank file      
  if @template.includes.has_key?(file)
    begin
      content = Includes.copy_include(@template.includes[file],@template.path)
      file = replace_tags(file)
    rescue TypeError => e
      puts e.message
      exit
    end
  else
   file = replace_tags(file) 
    puts 'Creating blank file: ' + File.join(path,file)
    content=''
  end
  File.open(File.join(path,file),'w'){|f| f.write(replace_tags(content))}
end