class Generator

Attributes

destination[RW]
name[RW]
relative_to_cwd[RW]
template_path[RW]

Public Instance Methods

generate() click to toggle source
# File lib/wp_scaffold/models/generator.rb, line 5
def generate
        # Path to our template folder
        source = TEMPLATE_BASE + self.template_path

        # For each file in the template source directory
        Dir.glob("#{source}/**/*").each do |file|
                unless File.directory?(file)

                        @generator = self

                        # template file, plus template directories
                        file_name = file.split(source)[1]

                        # full path to where the file will be written, including file name
                        # Should this be relative to the current working directory?
                        if @generator.relative_to_cwd == true
                                full_destination = USER_DIRECTORY + self.destination + "/" + file_name
                        else
                                full_destination = self.destination + "/" + file_name
                        end

                        # full path to where the file will be written, without file name
                        file_directory =  File.dirname(full_destination)

                        # Create the directory, if needed
                        unless File.directory?(file_directory)
                          FileUtils.mkdir_p(file_directory)
                        end

                        # complile the erb template, remove the .erb extention, and write to a new file
                        File.open(full_destination.gsub('.erb', ''), 'w') do |f|
                          f.write ERB.new(open(file).read).result(binding)
                        end

                end
        end # each
end