Class: Frank::Compile

Inherits:
Base
  • Object
show all
Extended by:
Render
Defined in:
lib/frank/compile.rb

Constant Summary

Constants included from Render

Render::LAYOUT_EXTS, Render::TMPL_EXTS

Class Method Summary collapse

Methods included from Render

ext_from_handler, layout_ext_or_first, layout_for, render, setup_page, tilt, to_file_path

Methods inherited from Base

#call, #call!

Methods included from TemplateHelpers

#capture, #content_for, #content_for?, #lorem, #refresh, #render_partial

Methods included from Rescue

#render_404, #render_500

Class Method Details

.ask_nicelyObject

ask the user if they want to overwrite the folder get the input and return it



49
50
51
52
# File 'lib/frank/compile.rb', line 49

def ask_nicely
  print "\033[31mA folder named `#{Frank.export.path}' already exists, overwrite it?\033[0m [y/n] "
  STDIN.gets.chomp.downcase
end

.compile_templatesObject

compile the templates if production and template isn’t index and is html name a folder based on the template and compile to index.html otherwise compile as is



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/frank/compile.rb', line 11

def compile_templates
  dir = File.join(Frank.root, Frank.dynamic_folder)

  Dir[File.join(dir, '**{,/*/**}/*')].each do |path|
    if File.file?(path) && !File.basename(path).match(/^(\.|_)/)
      path    = path[ (dir.size + 1)..-1 ]
      ext     = File.extname(path)
      new_ext = ext_from_handler(ext)
      name    = File.basename(path, ext)

      if Frank.production? && "#{name}.#{new_ext}" != 'index.html' && new_ext == 'html'
        new_file = File.join(Frank.export.path, path.sub(/(\/?[\w-]+)\.[\w-]+$/, "\\1/index.#{new_ext}"))
      else
        new_file = File.join(Frank.export.path, path.sub(/\.[\w-]+$/, ".#{new_ext}"))
      end

      create_dirs(new_file)
      File.open(new_file, 'w') {|f| f.write render(path) }
      puts " - \033[32mCreating\033[0m '#{new_file}'" unless Frank.silent_export?
    end
  end
end

.copy_staticObject

copies over static content



41
42
43
44
45
# File 'lib/frank/compile.rb', line 41

def copy_static
  puts " - \033[32mCopying\033[0m static content" unless Frank.silent_export?
  static_folder = File.join(Frank.root, Frank.static_folder)
  FileUtils.cp_r(File.join(static_folder, '/.'), Frank.export.path)
end

.create_dirs(path) ⇒ Object

use path to determine folder name and create the required folders if they don’t exist



36
37
38
# File 'lib/frank/compile.rb', line 36

def create_dirs(path)
  FileUtils.makedirs path.split('/').reverse[1..-1].reverse.join('/')
end

.export!Object

TODO verbose everywhere is lame create the dump dir, compile templates, copy over static assets



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/frank/compile.rb', line 68

def export!
  folder_exists = File.exist?(Frank.export.path)

  if folder_exists && Frank.export.force
    FileUtils.rm_rf(Frank.export.path)
  elsif folder_exists
    verify_overwriting
  end

  FileUtils.mkdir(Frank.export.path)

  unless Frank.silent_export?
    puts "\nFrank is..."
    puts " - \033[32mCreating\033[0m '#{Frank.export.path}'"
  end

  compile_templates
  copy_static

  puts "\n \033[32mCongratulations, project dumped to '#{Frank.export.path}' successfully!\033[0m" unless Frank.silent_export?
end

.verify_overwritingObject

verify that the user wants to overwrite the folder remove it if so, exit if not



56
57
58
59
60
61
62
63
64
# File 'lib/frank/compile.rb', line 56

def verify_overwriting
  overwrite = ask_nicely

  while overwrite.empty?
    overwrite = ask_nicely
  end

  overwrite == 'y' ? FileUtils.rm_rf(Frank.export.path) : exit
end