-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
plugin_generator.rb
355 lines (302 loc) · 7.84 KB
/
plugin_generator.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
# Fluentd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "optparse"
require "pathname"
require "fileutils"
require "erb"
require "open-uri"
require "fluent/registry"
require 'fluent/version'
class FluentPluginGenerator
attr_reader :type, :name
attr_reader :license_name
SUPPORTED_TYPES = ["input", "output", "filter", "parser", "formatter"]
def initialize(argv = ARGV)
@argv = argv
@parser = prepare_parser
@license_name = "Apache-2.0"
@overwrite_all = false
end
def call
parse_options!
FileUtils.mkdir_p(gem_name)
Dir.chdir(gem_name) do
copy_license
template_directory.find do |path|
next if path.directory?
dest_dir = path.dirname.sub(/\A#{Regexp.quote(template_directory.to_s)}\/?/, "")
dest_file = dest_filename(path)
if path.extname == ".erb"
if path.fnmatch?("*/plugin/*")
next unless path.basename.fnmatch?("*#{type}*")
end
template(path, dest_dir + dest_file)
else
file(path, dest_dir + dest_file)
end
end
pid = spawn("git", "init", ".")
Process.wait(pid)
end
end
private
def template_directory
(Pathname(__dir__) + "../../../templates/new_gem").realpath
end
def template_file(filename)
template_directory + filename
end
def template(source, dest)
dest.dirname.mkpath
contents =
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
ERB.new(source.read, trim_mode: "-")
else
ERB.new(source.read, nil, "-")
end.result(binding)
label = create_label(dest, contents)
puts "\t#{label} #{dest}"
if label == "conflict"
return unless overwrite?(dest)
end
File.write(dest, contents)
end
def file(source, dest)
label = create_label(dest, source.read)
puts "\t#{label} #{dest}"
if label == "conflict"
return unless overwrite?(dest)
end
FileUtils.cp(source, dest)
end
def prepare_parser
@parser = OptionParser.new
@parser.version = Fluent::VERSION
@parser.banner = <<BANNER
Usage: fluent-plugin-generate [options] <type> <name>
Generate a project skeleton for creating a Fluentd plugin
Arguments:
\ttype: #{SUPPORTED_TYPES.join(",")}
\tname: Your plugin name (fluent-plugin- prefix will be added to <name>)
Options:
BANNER
@parser.on("--[no-]license=NAME", "Specify license name (default: Apache-2.0)") do |v|
@license_name = v || "no-license"
end
@parser
end
def parse_options!
@parser.parse!(@argv)
unless @argv.size == 2
raise ArgumentError, "Missing arguments"
end
@type, @name = @argv
rescue => e
usage("#{e.class}:#{e.message}")
end
def usage(message = "")
puts message
puts
puts @parser.help
exit(false)
end
def user_name
v = `git config --get user.name`.chomp
v.empty? ? "TODO: Write your name" : v
end
def user_email
v = `git config --get user.email`.chomp
v.empty? ? "TODO: Write your email" : v
end
def gem_name
"fluent-plugin-#{dash_name}"
end
def plugin_name
underscore_name
end
def gem_file_path
File.expand_path(File.join(File.dirname(__FILE__),
"../../../",
"Gemfile"))
end
def lock_file_path
File.expand_path(File.join(File.dirname(__FILE__),
"../../../",
"Gemfile.lock"))
end
def locked_gem_version(gem_name)
d = Bundler::Definition.build(gem_file_path, lock_file_path, false)
d.locked_gems.dependencies[gem_name].requirement.requirements.first.last.version
end
def rake_version
locked_gem_version("rake")
end
def test_unit_version
locked_gem_version("test-unit")
end
def bundler_version
d = Bundler::Definition.build(gem_file_path, lock_file_path, false)
d.locked_gems.bundler_version.version
end
def class_name
"#{capitalized_name}#{type.capitalize}"
end
def plugin_filename
case type
when "input"
"in_#{underscore_name}.rb"
when "output"
"out_#{underscore_name}.rb"
else
"#{type}_#{underscore_name}.rb"
end
end
def test_filename
case type
when "input"
"test_in_#{underscore_name}.rb"
when "output"
"test_out_#{underscore_name}.rb"
else
"test_#{type}_#{underscore_name}.rb"
end
end
def dest_filename(path)
case path.to_s
when %r!\.gemspec!
"#{gem_name}.gemspec"
when %r!lib/fluent/plugin!
plugin_filename
when %r!test/plugin!
test_filename
else
path.basename.sub_ext("")
end
end
def capitalized_name
@capitalized_name ||= name.split(/[-_]/).map(&:capitalize).join
end
def underscore_name
@underscore_name ||= name.tr("-", "_")
end
def dash_name
@dash_name ||= name.tr("_", "-")
end
def preamble
@license.preamble(user_name)
end
def copy_license
# in gem_name directory
return unless license_name
puts "License: #{license_name}"
license_class = self.class.lookup_license(license_name)
@license = license_class.new
Pathname("LICENSE").write(@license.text) unless @license.text.empty?
rescue Fluent::ConfigError
usage("Unknown license: #{license_name}")
rescue => ex
usage("#{ex.class}: #{ex.message}")
end
def create_label(dest, contents)
if dest.exist?
if dest.read == contents
"identical"
else
"conflict"
end
else
"create"
end
end
def overwrite?(dest)
return true if @overwrite_all
loop do
print "Overwrite #{dest}? (enter \"h\" for help) [Ynaqh]"
answer = $stdin.gets.chomp
return true if /\Ay\z/i =~ answer || answer.empty?
case answer
when "n"
return false
when "a"
@overwrite_all = true
return true
when "q"
exit
when "h"
puts <<HELP
\tY - yes, overwrite
\tn - no, do not overwrite
\ta - all, overwrite this and all others
\tq - quit, abort
\th - help, show this help
HELP
end
puts "Retrying..."
end
end
class NoLicense
attr_reader :name, :full_name, :text
def initialize
@name = ""
@full_name = ""
@text = ""
end
def preamble(usename)
""
end
end
class ApacheLicense
LICENSE_URL = "http://www.apache.org/licenses/LICENSE-2.0.txt"
attr_reader :text
def initialize
@text = ""
@preamble_source = ""
@preamble = nil
uri = URI.parse(LICENSE_URL)
uri.open do |io|
@text = io.read
end
@preamble_source = @text[/^(\s*Copyright.+)/m, 1]
end
def name
"Apache-2.0"
end
def full_name
"Apache License, Version 2.0"
end
def preamble(user_name)
@preamble ||= @preamble_source.dup.tap do |source|
source.gsub!(/\[yyyy\]/, "#{Date.today.year}-")
source.gsub!(/\[name of copyright owner\]/, user_name)
source.gsub!(/^ {2}|^$/, "#")
source.chomp!
end
end
end
LICENSE_REGISTRY = Fluent::Registry.new(:license, "")
def self.register_license(license, klass)
LICENSE_REGISTRY.register(license, klass)
end
def self.lookup_license(license)
LICENSE_REGISTRY.lookup(license)
end
{
"no-license" => NoLicense,
"Apache-2.0" => ApacheLicense
}.each do |license, klass|
register_license(license, klass)
end
end