This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
skeleton.rb
executable file
·117 lines (92 loc) · 2.24 KB
/
skeleton.rb
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env ruby
require 'yaml'
require 'optparse'
require 'fileutils'
class YMLConfig
attr_reader :filters
def initialize(path)
@filters = YAML.safe_load(File.read(path))
end
end
class Skeleton
attr_reader :config
attr_reader :output_dir
attr_reader :ignored
def initialize(config, output_dir)
@config = config
@output_dir = output_dir
@ignored = %w[skeleton.rb skeleton.yml]
end
def build
clean_output_dir
tree.each do |file|
next if ignore?(file)
output_path = save(file, output(file))
show_filter(file, output_path) if filters?(file)
show_saved(output_path)
end
puts "Build available in #{output_dir}"
end
private
def show_saved(output_path)
puts "Saved #{output_path}"
end
def show_filter(original_path, output_path)
separator = '-' * 30
puts "Filtered #{original_path}"
puts separator
puts `diff #{original_path} #{output_path}`
puts separator
end
def tree
`git ls-tree -r --name-only head`.split("\n")
end
def output(path)
return filtered(path) if filters?(path)
File.read(path)
end
def filtered(path)
lines = File.readlines(path)
filters = config.filters[path]
filtered_lines = (1..lines.count).select do |line|
filters.none? { |from, to| (from..to).cover? line }
end
filtered_lines.map do |line|
lines[line - 1]
end.join
end
def filters?(path)
config.filters.include? path
end
def save(original_path, output)
output_path = File.join(output_dir, original_path)
output_dir = File.dirname(output_path)
`mkdir -p #{output_dir}`
File.open(output_path, 'w') { |f| f.write(output) }
output_path
end
def ignore?(file)
ignored.include? file
end
def clean_output_dir
Dir["#{output_dir}/*"].each do |node|
FileUtils.rmtree(node)
puts "Removed #{node}"
end
end
end
def parse_options
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: skeleton.rb [options]'
opts.on('--output OUTPUT_DIR') { |o| options[:output] = o }
end.parse!
options
end
def main
options = parse_options
config = YMLConfig.new('skeleton.yml')
skeleton = Skeleton.new(config, options.fetch(:output, 'skeleton'))
skeleton.build
end
main