Class: Fontcustom::Generator::Template

Inherits:
Thor::Group
  • Object
show all
Includes:
Util, Thor::Actions
Defined in:
lib/fontcustom/generator/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#check_fontforge, #expand_path, #overwrite_file, #relative_to_root, #say_changed, #say_message, #symbolize_hash

Class Method Details

.source_rootObject

Required for Thor::Actions#template



18
19
20
# File 'lib/fontcustom/generator/template.rb', line 18

def self.source_root
  File.join Fontcustom.gem_lib, "templates"
end

Instance Method Details

#generateObject



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
# File 'lib/fontcustom/generator/template.rb', line 58

def generate
  @glyphs = @data[:glyphs]
  created = []
  packaged = %w|fontcustom-bootstrap-ie7.css fontcustom.css _fontcustom-bootstrap-ie7.scss _fontcustom-rails.scss
             fontcustom-bootstrap.css fontcustom-preview.html _fontcustom-bootstrap.scss _fontcustom.scss|
  css_exts = %w|.css .scss .sass .less .stylus|
  begin
    opts.templates.each do |source|
      name = File.basename source
      ext = File.extname source
      target = name.dup

      if packaged.include?(name) && opts.font_name != DEFAULT_OPTIONS[:font_name]
        target.sub! DEFAULT_OPTIONS[:font_name], opts.font_name
      end

      target = if opts.output.keys.include? name.to_sym
        File.join opts.output[name.to_sym], target
      elsif css_exts.include? ext
        File.join opts.output[:css], target
      elsif name == "fontcustom-preview.html"
        File.join opts.output[:preview], target
      else
        File.join opts.output[:fonts], target
      end

      template source, target, :verbose => false
      created << target
    end
  ensure
    say_changed :create, created
    @data[:templates] = (@data[:templates] + created).uniq
    json = JSON.pretty_generate @data
    overwrite_file opts.data_cache, json
  end
end

#get_dataObject



22
23
24
25
26
27
28
29
30
# File 'lib/fontcustom/generator/template.rb', line 22

def get_data
  if File.exists? opts.data_cache
    @data = JSON.parse File.read(opts.data_cache), :symbolize_names => true
  else
    raise Fontcustom::Error, "`#{relative_to_root(opts.data_cache)}` is missing. This file is required to generate templates."
  end
rescue
  raise Fontcustom::Error, "Couldn't parse `#{relative_to_root(opts.data_cache)}`. Delete it to start from scratch. Any previously generated files will need to be deleted manually."
end

#make_relative_pathsObject



48
49
50
51
52
53
54
55
56
# File 'lib/fontcustom/generator/template.rb', line 48

def make_relative_paths
  name = File.basename @data[:fonts].first, File.extname(@data[:fonts].first)
  fonts = Pathname.new opts.output[:fonts]
  css = Pathname.new opts.output[:css]
  preview = Pathname.new opts.output[:preview]
  @font_path = File.join fonts.relative_path_from(css).to_s, name
  @font_path_alt = opts.preprocessor_path.nil? ? @font_path : File.join(opts.preprocessor_path, name)
  @font_path_preview = File.join fonts.relative_path_from(preview).to_s, name
end

#reset_outputObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fontcustom/generator/template.rb', line 32

def reset_output
  return if @data[:templates].empty?
  begin
    deleted = []
    @data[:templates].each do |file|
      remove_file file, :verbose => false
      deleted << file
    end
  ensure
    @data[:templates] = @data[:templates] - deleted
    json = JSON.pretty_generate @data
    overwrite_file opts.data_cache, json
    say_changed :delete, deleted
  end
end