Class: Rdm::Handlers::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rdm/handlers/template_handler.rb

Constant Summary collapse

REJECTED_TEMPLATE_FILES =
%W(.DS_Store)
NOT_HANDLED_TEMPLATES_EXT =
%W(.erb)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_name:, local_path:, current_path:, locals:, ignore_source_file:, stdout:, stdin:) ⇒ TemplateHandler

Returns a new instance of TemplateHandler.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rdm/handlers/template_handler.rb', line 26

def initialize(template_name:, local_path:, current_path:,  
               locals:, ignore_source_file:, stdout:, stdin:)

  @template_name      = template_name
  @local_path         = local_path
  @current_path       = current_path
  @ignore_source_file = ignore_source_file
  @missing_variables  = []
  @stdout             = stdout
  @stdin              = stdin

  default_locals     = { package_subdir_name: Rdm.settings.send(:package_subdir_name) }
  @locals            = default_locals.merge(locals)
end

Class Method Details

.generate(template_name:, local_path:, current_path:, locals: {}, ignore_source_file: false, stdout: STDOUT, stdin: STDIN) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rdm/handlers/template_handler.rb', line 11

def generate(template_name:, local_path:, current_path:, locals: {}, 
             ignore_source_file: false, stdout: STDOUT, stdin: STDIN)

  Rdm::Handlers::TemplateHandler.new(
    template_name:      template_name.to_s, 
    local_path:         local_path, 
    current_path:       current_path, 
    ignore_source_file: ignore_source_file,
    locals:             locals,
    stdout:             stdout,
    stdin:              stdin
  ).generate
end

Instance Method Details

#generateObject



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rdm/handlers/template_handler.rb', line 41

def generate
  project_path      = @ignore_source_file ? @current_path : File.dirname(Rdm::SourceLocator.locate(@current_path))
  template_detector = Rdm::Templates::TemplateDetector.new(project_path)

  render_helper_path = "#{project_path}/.rdm/helpers/render_helper.rb"
  require_relative File.expand_path(render_helper_path) if File.exist?(render_helper_path)

  @template_directory    = template_detector.detect_template_folder(@template_name)
  @destination_directory = File.join(project_path, @local_path)

  template_files_list = Dir
    .glob(File.join(@template_directory, '**', '*'), File::FNM_DOTMATCH)
    .reject { |path| REJECTED_TEMPLATE_FILES.include? File.basename(path)  }

  template_files_list.each do |path|
    @missing_variables.concat(
      Rdm::Templates::TemplateRenderer.get_undefined_variables(get_destination_path(path), @locals)
    ) 

    if handle_file_content?(path)
      @missing_variables.concat(
        Rdm::Templates::TemplateRenderer.get_undefined_variables(File.read(path), @locals)
      ) 
    end
  end

  if @missing_variables.any?
    @missing_variables.uniq!

    @stdout.puts "Undefined variables were found:"
    @missing_variables.size.times {|t| @stdout.puts "  #{t+1}. #{@missing_variables[t]}"}

    @missing_variables.each do |var|
      @stdout.print "Type value for '#{var}': "
      @locals[var] = @stdin.gets.chomp
    end
  end

  template_files_list.map! do |path|
    rendered_abs_path = Rdm::Templates::TemplateRenderer.handle(get_destination_path(path), @locals)
    rendered_rel_path = Pathname.new(rendered_abs_path).relative_path_from Pathname.new(project_path)

    if File.file?(rendered_abs_path) && File.exists?(rendered_abs_path)
      @stdout.puts "Warning! #{rendered_rel_path} already exists. Skipping file creation..."
      next
    end

    if File.directory?(path)
      FileUtils.mkdir_p rendered_abs_path
      next
    end

    rendered_file_content = handle_file_content?(path) ?
      Rdm::Templates::TemplateRenderer.handle(File.read(path), @locals) :
      File.read(path)
      
    FileUtils.mkdir_p(File.dirname(rendered_abs_path))
    File.open(rendered_abs_path, 'w') { |f| f.write rendered_file_content }

    rendered_rel_path
  end
  
  template_files_list.compact
end