class FluentPluginGenerator

Constants

LICENSE_REGISTRY
SUPPORTED_TYPES

Attributes

license_name[R]
name[R]
type[R]

Public Class Methods

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

Private Class Methods

lookup_license(license) click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 309
def self.lookup_license(license)
  LICENSE_REGISTRY.lookup(license)
end
register_license(license, klass) click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 305
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

capitalized_name() click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 188
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 149
def class_name
  "#{capitalized_name}#{type.capitalize}"
end
copy_license() click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 204
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 217
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 196
def dash_name
  @dash_name ||= name.tr("_", "-")
end
dest_filename(path) click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 175
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 84
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_name() click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 141
def gem_name
  "fluent-plugin-#{dash_name}"
end
overwrite?(dest) click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 229
  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 114
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 153
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 145
def plugin_name
  underscore_name
end
preamble() click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 200
def preamble
  @license.preamble(user_name)
end
prepare_parser() click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 93
  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

Options:
BANNER

    @parser.on("--[no-]license=NAME", "Specify license name (default: Apache-2.0)") do |v|
      @license_name = v || "no-license"
    end
    @parser
  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 = ERB.new(source.read, nil, "-").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 164
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
underscore_name() click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 192
def underscore_name
  @underscore_name ||= name.tr("-", "_")
end
usage(message = "") click to toggle source
# File lib/fluent/command/plugin_generator.rb, line 124
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 136
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 131
def user_name
  v = `git config --get user.name`.chomp
  v.empty? ? "TODO: Write your name" : v
end