class FluentPluginGenerator
Constants
- LICENSE_REGISTRY
- SUPPORTED_TYPES
Attributes
license_name[R]
name[R]
type[R]
Public Class Methods
lookup_license(license)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 355 def self.lookup_license(license) LICENSE_REGISTRY.lookup(license) end
new(argv = ARGV)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 32 def initialize(argv = ARGV) @argv = argv @parser = prepare_parser @license_name = "Apache-2.0" @overwrite_all = false end
register_license(license, klass)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 351 def self.register_license(license, klass) LICENSE_REGISTRY.register(license, klass) end
Public Instance Methods
call()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 40 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 Instance Methods
bundler_version()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 184 def bundler_version if File.exist?(lock_file_path) d = Bundler::Definition.build(gem_file_path, lock_file_path, false) d.locked_gems.bundler_version.version else # fallback even though Fluentd is installed without bundler Gem::Specification.find_by_name("bundler").version.version end end
capitalized_name()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 233 def capitalized_name @capitalized_name ||= name.split(/[-_]/).map(&:capitalize).join end
class_name()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 194 def class_name "#{capitalized_name}#{type.capitalize}" end
copy_license()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 249 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
create_label(dest, contents)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 262 def create_label(dest, contents) if dest.exist? if dest.read == contents "identical" else "conflict" end else "create" end end
dash_name()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 241 def dash_name @dash_name ||= name.tr("_", "-") end
dest_filename(path)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 220 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
file(source, dest)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 89 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
gem_file_path()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 154 def gem_file_path File.expand_path(File.join(File.dirname(__FILE__), "../../../", "Gemfile")) end
gem_name()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 146 def gem_name "fluent-plugin-#{dash_name}" end
lock_file_path()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 160 def lock_file_path File.expand_path(File.join(File.dirname(__FILE__), "../../../", "Gemfile.lock")) end
locked_gem_version(gem_name)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 166 def locked_gem_version(gem_name) if File.exist?(lock_file_path) d = Bundler::Definition.build(gem_file_path, lock_file_path, false) d.locked_gems.dependencies[gem_name].requirement.requirements.first.last.version else # fallback even though Fluentd is installed without bundler Gem::Specification.find_by_name(gem_name).version.version end end
overwrite?(dest)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 274 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
parse_options!()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 119 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
plugin_filename()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 198 def plugin_filename case type when "input" "in_#{underscore_name}.rb" when "output" "out_#{underscore_name}.rb" else "#{type}_#{underscore_name}.rb" end end
plugin_name()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 150 def plugin_name underscore_name end
preamble()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 245 def preamble @license.preamble(user_name) end
prepare_parser()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 98 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
rake_version()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 176 def rake_version locked_gem_version("rake") end
template(source, dest)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 73 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
template_directory()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 65 def template_directory (Pathname(__dir__) + "../../../templates/new_gem").realpath end
template_file(filename)
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 69 def template_file(filename) template_directory + filename end
test_filename()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 209 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
test_unit_version()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 180 def test_unit_version locked_gem_version("test-unit") end
underscore_name()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 237 def underscore_name @underscore_name ||= name.tr("-", "_") end
usage(message = "")
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 129 def usage(message = "") puts message puts puts @parser.help exit(false) end
user_email()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 141 def user_email v = `git config --get user.email`.chomp v.empty? ? "TODO: Write your email" : v end
user_name()
click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 136 def user_name v = `git config --get user.name`.chomp v.empty? ? "TODO: Write your name" : v end