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



75
76
77
78
79
80
81
82
83
# File 'lib/isodoc/gem_tasks.rb', line 75

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



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

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



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

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



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

def fonts_placeholder
  <<~TEXT
    $bodyfont: "{{bodyfont}}";
    $headerfont: "{{headerfont}}";
    $monospacefont: "{{monospacefont}}";
    $normalfontsize: "{{normalfontsize}}";
    $smallerfontsize: "{{smallerfontsize}}";
    $footnotefontsize: "{{footnotefontsize}}";
    $monospacefontsize: "{{monospacefontsize}}";
  TEXT
end

.git_cache_compiled_filesObject



54
55
56
57
58
# File 'lib/isodoc/gem_tasks.rb', line 54

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

.git_rm_compiled_filesObject



60
61
62
63
64
# File 'lib/isodoc/gem_tasks.rb', line 60

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
# File 'lib/isodoc/gem_tasks.rb', line 13

def install
  rule ".css" => [proc { |tn| tn.sub(/\.css$/, ".scss") }] do |current_task|
    puts(current_task)
    compile_scss_task(current_task)
  rescue StandardError => e
    notify_borken_compilation(e, current_task)
  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



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

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



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

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



85
86
87
88
89
90
# File 'lib/isodoc/gem_tasks.rb', line 85

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