Module: IsoDoc::GemTasks

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

Constant Summary collapse

@@css_list =
[]

Class Method Summary collapse

Class Method Details

.comment_out_liquid(text) ⇒ Object



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

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/isodoc/gem_tasks.rb', line 112

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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/isodoc/gem_tasks.rb', line 132

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
  @@css_list << compiled_path
  CLEAN << compiled_path
end

.css_listObject



15
16
17
# File 'lib/isodoc/gem_tasks.rb', line 15

def self.css_list
  @@css_list
end

.fonts_placeholderObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/isodoc/gem_tasks.rb', line 100

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

.git_cache_compiled_filesObject



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

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

.git_rm_compiled_filesObject



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

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

.installObject



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
44
45
46
47
# File 'lib/isodoc/gem_tasks.rb', line 19

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_broken_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

.interactive?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/isodoc/gem_tasks.rb', line 49

def interactive?
  ENV["CI"] == nil
end

.notify_broken_compilation(error, current_task) ⇒ Object



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

def notify_broken_compilation(error, current_task)
  puts("Cannot compile #{current_task} because of #{error.message}")
  return unless interactive?

  puts("continue anyway[y|n]?")
  answer = $stdin.gets.strip
  exit(0) unless %w[y yes].include?(answer.strip.downcase)
end

.process_css_files(scss_files) ⇒ Object



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

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



93
94
95
96
97
98
# File 'lib/isodoc/gem_tasks.rb', line 93

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