Module: IsoDoc::GemTasks

Extended by:
Rake::DSL
Defined in:
lib/isodoc/gem_tasks.rb

Class Method Summary collapse

Class Method Details

.comment_out_liquid(text) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/isodoc/gem_tasks.rb', line 77

def comment_out_liquid(text)
  text.split("\n").map do |line|
    if line.match?(/{({|%).+(}|%)}/)
      "/* LIQUID_COMMENT#{line}LIQUID_COMMENT */"
    else
      line
    end
  end
      .join("\n")
end

.compile_scss(filename) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/isodoc/gem_tasks.rb', line 103

def compile_scss(filename)
  require 'sassc'

  isodoc_path = if Gem.loaded_specs['isodoc']
                  File.join(Gem.loaded_specs['isodoc'].full_gem_path, 'lib', 'isodoc')
                else
                  File.join('lib', 'isodoc')
                end
  [isodoc_path,
   File.dirname(filename)].each do |name|
    SassC.load_paths << name
  end
  sheet_content = File.read(filename, encoding: 'UTF-8')
  SassC::Engine.new(fonts_placeholder + sheet_content,
                    syntax: :scss,
                    importer: SasscImporter)
               .render
end

.compile_scss_task(current_task) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/isodoc/gem_tasks.rb', line 122

def compile_scss_task(current_task)
  filename = current_task.source
  basename = File.basename(filename, '.*')
  compiled_path = File.join(File.dirname(filename), "#{basename}.css")
  content = uncomment_out_liquid(compile_scss(filename))
  File.open(compiled_path, 'w:UTF-8') do |f|
    f.write(content)
  end
  CLEAN << compiled_path
end

.fonts_placeholderObject



95
96
97
98
99
100
101
# File 'lib/isodoc/gem_tasks.rb', line 95

def fonts_placeholder
  <<~TEXT
    $bodyfont: '{{bodyfont}}';
    $headerfont: '{{headerfont}}';
    $monospacefont: '{{monospacefont}}';
  TEXT
end

.git_cache_compiled_filesObject



56
57
58
59
60
# File 'lib/isodoc/gem_tasks.rb', line 56

def git_cache_compiled_files
  CLEAN.each do |css_file|
    sh "git add #{css_file}"
  end
end

.git_rm_compiled_filesObject



62
63
64
65
66
# File 'lib/isodoc/gem_tasks.rb', line 62

def git_rm_compiled_files
  CLEAN.each do |css_file|
    sh "git rm --cached #{css_file}"
  end
end

.installObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/isodoc/gem_tasks.rb', line 13

def install
  rule '.css' => [proc { |tn| tn.sub(/\.css$/, '.scss') }] do |current_task|
    begin
      puts(current_task)
      compile_scss_task(current_task)
    rescue StandardError => e
      notify_borken_compilation(e, current_task)
    end
  end

  scss_files = Rake::FileList['lib/**/*.scss']
  source_files = scss_files.ext('.css')

  task :comment_out_liquid do
    process_css_files(scss_files) do |file_name|
      comment_out_liquid(File.read(file_name, encoding: 'UTF-8'))
    end
  end

  task build_scss: [:comment_out_liquid].push(*source_files) do
    process_css_files(scss_files) do |file_name|
      uncomment_out_liquid(File.read(file_name, encoding: 'UTF-8'))
    end
    git_cache_compiled_files && puts('Built scss!')
  end

  Rake::Task['build'].enhance [:build_scss] do
    git_rm_compiled_files
    Rake::Task[:clean].invoke
  end
end

.notify_borken_compilation(error, current_task) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/isodoc/gem_tasks.rb', line 45

def notify_borken_compilation(error, current_task)
  puts("Cannot compile #{current_task} because of #{error.message}")
  puts('continue anyway[y|n]?')
  answer = STDIN.gets.strip
  if %w[y yes].include?(answer.strip.downcase)
    puts("Cannot compile #{current_task} because of #{error.message}")
  else
    exit(0)
  end
end

.process_css_files(scss_files) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/isodoc/gem_tasks.rb', line 68

def process_css_files(scss_files)
  scss_files.each do |file_name|
    result = yield(file_name)
    File.open(file_name, 'w', encoding: 'UTF-8') do |file|
      file.puts(result)
    end
  end
end

.uncomment_out_liquid(text) ⇒ Object



88
89
90
91
92
93
# File 'lib/isodoc/gem_tasks.rb', line 88

def uncomment_out_liquid(text)
  text
    .gsub('/* LIQUID_COMMENT', '')
    .gsub('LIQUID_COMMENT */', '')
    .gsub('"{{', '{{').gsub('}}"', '}}')
end