forked from sunaku/glove80-keymaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
58 lines (46 loc) · 1.77 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'rake/clean'
require 'erb'
task :default => [:dtsi, :dot]
#-----------------------------------------------------------------------------
# ZMK configuration snippet (DTSI)
#-----------------------------------------------------------------------------
dtsi_files = FileList['*.dtsi.erb'].each do |erb|
dtsi = erb.pathmap('%X')
file dtsi => FileList[erb, '*.{yaml,json,zmk}', __FILE__]
CLEAN.include "#{erb}.tmp"
dtsi_min = "#{dtsi}.min"
file dtsi_min => dtsi
CLOBBER.include dtsi_min
end
task :dtsi => dtsi_files
rule '.dtsi' => '.dtsi.erb' do |t|
input = File.read(t.source)
# NOTE: this may shift line numbers, hence dump *.tmp below
.gsub(/\n(?= *<%(?!=))/, '') # remove leading newline
template = ERB.new(input, trim_mode: '<>')
template.filename = t.source + '.tmp'
File.write(template.filename, input) # for error line numbers
output = template.result()
.gsub(/ +$/, '') # remove trailing spaces
.gsub(/\n+(?= +#(?!define))/, "\n") # tighten #elif
File.write(t.name, output)
end
rule '.dtsi.min' => '.dtsi' do |t|
minified = File.read(t.source)
.gsub(%r{^\s*//(?! ==== ).*}, '') # remove comment lines
.gsub(%r{(?<=[^\*])//.*}, '') # remove trailing comments
.gsub(/^\s+/, '') # remove indentation
.squeeze("\n") # remove blank lines
.squeeze(' ') # remove extra spaces
File.write(t.name, minified)
end
#-----------------------------------------------------------------------------
# Graphviz DOT for diagrams
#-----------------------------------------------------------------------------
task :dot => 'define.svg'
file 'define.svg' => 'define.dot' do |t|
sh "dot -Tsvg #{t.prerequisites[0]} > #{t.name}"
end
file 'define.dot' => ['define.dot.erb', 'keymap.dtsi.min'] do |t|
sh "erb #{t.prerequisites[0]} > #{t.name}"
end