-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathbuild.rb
More file actions
115 lines (90 loc) · 3.29 KB
/
build.rb
File metadata and controls
115 lines (90 loc) · 3.29 KB
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
#!/usr/bin/env ruby
# This script generates GitHub Actions for the currently supported
# set of Snyk images. The Actions all have the same interface.
require "erb"
require "fileutils"
class ActionGenerator
VARIANTS = File.readlines('variants').map(&:strip).reject(&:empty?).freeze
def initialize
@templates_dir = "_templates"
end
def generate_all
generate_base_readme
generate_variant_actions
generate_root_action
generate_test_workflows
end
private
def variant_deprecated?(variant)
variant.include?("DEPRECATED")
end
def clean_variant_name(variant)
variant.gsub(/\s*DEPRECATED\s*/, "").strip
end
def active_variants
VARIANTS.reject { |v| variant_deprecated?(v) }
end
def deprecated_variants
VARIANTS.select { |v| variant_deprecated?(v) }.map { |v| clean_variant_name(v) }
end
def generate_base_readme
puts "Generating base README.md"
render_template("BASE.md.erb", "README.md") do |erb|
erb.instance_variable_set(:@variants, active_variants)
erb.instance_variable_set(:@deprecated_variants, deprecated_variants)
end
end
def generate_variant_actions
VARIANTS.each do |variant|
puts "Generating Action for #{variant}"
generate_variant_action(variant)
end
end
def generate_variant_action(variant)
is_deprecated = variant_deprecated?(variant)
clean_variant = clean_variant_name(variant)
dirname = clean_variant.downcase
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
name, ident = clean_variant.split("-", 2)
%w[action.yml README.md].each do |filename|
template_name = "#{filename}.erb"
output_path = File.join(dirname, filename)
render_template(template_name, output_path) do |erb|
erb.instance_variable_set(:@variant, clean_variant)
erb.instance_variable_set(:@name, name)
erb.instance_variable_set(:@ident, ident)
erb.instance_variable_set(:@is_deprecated, is_deprecated)
end
end
end
def generate_root_action
puts "Generating root Action"
# Generate root action.yml using the Node variant but marked as root
render_template("action.yml.erb", "action.yml") do |erb|
erb.instance_variable_set(:@variant, "Node")
erb.instance_variable_set(:@name, "Node")
erb.instance_variable_set(:@ident, nil)
erb.instance_variable_set(:@is_root, true)
end
end
def generate_test_workflows
workflows_dir = ".github/workflows"
FileUtils.mkdir_p(workflows_dir) unless File.directory?(workflows_dir)
puts "Generating matrix test workflow for active (non-deprecated) actions"
template_name = "test-generated-actions.yml.erb"
output_path = File.join(workflows_dir, "test-generated-actions.yml")
render_template(template_name, output_path) do |erb|
erb.instance_variable_set(:@variants, active_variants)
end
end
def render_template(template_name, output_path)
template_path = File.join(@templates_dir, template_name)
template_content = File.read(template_path)
erb = ERB.new(template_content)
yield(erb) if block_given?
result = erb.result(erb.instance_eval { binding })
File.write(output_path, result)
end
end
# Run the generator if this file is executed directly
ActionGenerator.new.generate_all if __FILE__ == $PROGRAM_NAME