]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Add a plugin skeleton generator
authorRaine Virta <rane@kapsi.fi>
Thu, 22 Apr 2010 20:47:36 +0000 (23:47 +0300)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 18 Aug 2010 08:41:22 +0000 (10:41 +0200)
`rake plugin`

Rakefile
tasks/plugin.rake [new file with mode: 0644]

index 2b51595c6c3f035b187fcbb6d557e029d8bad313..97b3e1f328bfd694c09947c221a3069549424fb5 100644 (file)
--- a/Rakefile
+++ b/Rakefile
@@ -195,4 +195,4 @@ desc 'Generate mo files'
 task :makemo =>
   FileList['po/*/*.po'].pathmap('%{^po,data/locale}d/LC_MESSAGES/%n.mo')
 
-
+Dir['tasks/**/*.rake'].each { |t| load t }
diff --git a/tasks/plugin.rake b/tasks/plugin.rake
new file mode 100644 (file)
index 0000000..f781481
--- /dev/null
@@ -0,0 +1,53 @@
+task :plugin, :name  do |t, args|
+  if args.to_a.empty?
+    raise <<-eos
+Usage:   rake #{t}[<plugin name>]
+Example: rake plugin[Greet]
+    eos
+  end
+  plugin_name = args.name.downcase
+  class_name  = "#{plugin_name.capitalize}Plugin"
+  plugin_template = <<-eot
+class #{class_name} < Plugin
+  def help(plugin, topic="")
+    topics = %w{hello}
+
+    case topic
+    when 'hello'
+      _("hello, this is an example topic of my new plugin! :)")
+    else
+      _("#{plugin_name} plugin — topics: %{list}") % {
+        :list => topics.join(", ")
+      }
+    end
+  end
+
+  def example(m, params)
+    m.reply "example action was triggered"
+  end
+end
+
+plugin = #{class_name}.new
+plugin.map "#{plugin_name} [:arg]", :action => 'example'
+  eot
+
+  plugins_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'data/rbot/plugins'))
+  file_path    = File.join(plugins_path, "#{plugin_name}.rb")
+
+  if File.exist?(file_path)
+    puts "File exists: #{file_path}"
+    print "Overwrite? "
+    input = STDIN.gets.chomp
+    puts
+
+    exit unless input =~ /y(es)?/
+  end
+
+  File.open(file_path, "w") do |f|
+    f << plugin_template
+  end
+
+  puts "Plugin skeleton for #{class_name} written!"
+  puts "Now issue `rescan` on the bot and use the command `help #{plugin_name}` to see that the plugin works."
+  puts
+end