Sha256: 9d177c59707e985235a3ecd53ee2d3cdd1f370c029af12c5278ae0a6b654cfce

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

module ArchitectureJS
  class Generator

    attr_accessor :template_paths,
                  :templates,
                  :blueprint,
                  :project

    def initialize(blueprint)
      @project = blueprint
      @blueprint = @project.config
      @template_paths = @project.template_directories
      @templates = Hash.new
      load_templates
    end

    def find_templates(paths)
      [*paths].each do |path|
        Dir["#{path}/*"].each do |file|
          add_file_to_templates file
        end
      end
    end

    def load_templates
      find_templates @template_paths
    end

    def add_file_to_templates(file)
      ext = File.extname(file)
      template_name = File.basename(file, ext)
      @templates[template_name] = {
        erb: read_template(file),
        ext: ext
      }
    end

    def read_template(file)
      ERB.new(File.read(file), nil, '<>')
    end

    def generate(config)
      raise "You must provide a template to generate:\n  - architect generate <template>" unless config[:template]
      raise "There is no template named '#{template}' in the #{@blueprint[:name]} project" unless @templates[config[:template]]

      template = config[:template]
      options = config[:options]
      arguments = config[:arguments]
      filename = "#{config[:filename]}#{@templates[template][:ext]}"

      generate_file(filename, render_template(template, filename, arguments, options))
    end

    def generate_file(filename, template, path = nil)
      path ||= File.expand_path(Dir.getwd)
      File.open("#{path}/#{filename}", "w+") { |f| f.write template }
      puts ArchitectureJS::Notification.added "#{filename} generated" if File.exists?("#{path}/#{filename}")
    end

    def render_template(template, filename, arguments, options)
      blueprint = @blueprint
      project = @project
      @templates[template][:erb].result(binding)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
architecture-js-0.5.8 lib/architecture-js/generator.rb
architecture-js-0.5.7 lib/architecture-js/generator.rb
architecture-js-0.5.6 lib/architecture-js/generator.rb
architecture-js-0.5.5 lib/architecture-js/generator.rb
architecture-js-0.5.4 lib/architecture-js/generator.rb