Skip to content

A platform for community discussion. Free, open, simple.

DEV: rails generator to create plugin skeleton (#6332)

Joffrey JAFFEUXef36fdfb64a0
diff --git a/lib/generators/plugin/plugin_generator.rb b/lib/generators/plugin/plugin_generator.rb
new file mode 100644
index 00000000000..7da8209e13d
--- /dev/null
+++ b/lib/generators/plugin/plugin_generator.rb
@@ -0,0 +1,63 @@
+require 'rails/generators/named_base'
+
+class PluginGenerator < Rails::Generators::NamedBase
+ desc 'This generator creates a Discourse plugin skeleton'
+
+ source_root File.expand_path('templates', __dir__)
+
+ class_option :author, type: :string, desc: "Plugin author", required: true
+ class_option :stylesheet, type: :boolean, desc: "Generate Stylesheet", default: true
+ class_option :javascript, type: :boolean, desc: "Generate Javascript initializer", default: true
+ class_option :scheduled_job, type: :boolean, desc: "Generate Stylesheet", default: false
+
+ def create_scheduled_job_file
+ return unless @options['scheduled_job']
+
+ path = File.join('plugins', dasherized_name, 'jobs/scheduled', "check_#{underscored_name}.rb")
+ template 'scheduled_job.rb.erb', path
+ end
+
+ def create_readme_file
+ template 'README.md.erb', File.join('plugins', dasherized_name, "README.md")
+ end
+
+ def create_license_file
+ template 'LICENSE.erb', File.join('plugins', dasherized_name, "LICENSE")
+ end
+
+ def create_plugin_file
+ template 'plugin.rb.erb', File.join('plugins', dasherized_name, "plugin.rb")
+ end
+
+ def create_stylesheet_file
+ return unless @options['stylesheet']
+
+ template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/common', "#{dasherized_name}.scss")
+ end
+
+ def create_javascript_file
+ return unless @options['javascript']
+
+ template 'javascript.es6.erb', File.join('plugins', dasherized_name, 'assets/javascripts/initializers', "#{dasherized_name}.es6")
+ end
+
+ def create_gitignore_entry
+ plugin_entry = "!/plugins/#{dasherized_name}"
+
+ unless File.readlines(".gitignore").grep(/#{plugin_entry}/).size > 0
+ open('.gitignore', 'a') { |f| f.puts "\n#{plugin_entry}" }
+ end
+ end
+
+ def underscored_name
+ name.underscore
+ end
+
+ def dasherized_name
+ underscored_name.dasherize
+ end
+
+ def classified_name
+ name.tableize.classify
+ end
+end
diff --git a/lib/generators/plugin/templates/LICENSE.erb b/lib/generators/plugin/templates/LICENSE.erb
new file mode 100644
index 00000000000..9d4652c4be4
--- /dev/null
+++ b/lib/generators/plugin/templates/LICENSE.erb
@@ -0,0 +1,20 @@
+The MIT License (MIT)
+
+Copyright (c) 2018 <%= @options['author'] %>.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/lib/generators/plugin/templates/README.md.erb b/lib/generators/plugin/templates/README.md.erb
new file mode 100644
index 00000000000..323f2b6f271
--- /dev/null
+++ b/lib/generators/plugin/templates/README.md.erb
@@ -0,0 +1,16 @@
+# <%= name %>
+
+<%= name %> is a plugin for ...
+
+## Installation
+
+Follow [Install a Plugin](https://meta.discourse.org/t/install-a-plugin/19157)
+how-to from the official Discourse Meta, using `git clone https://github.com/<%= @options['author'] %>/<%= dasherized_name %>.git`
+as the plugin command.
+
+## Usage
+
+## Feedback
+
+If you have issues or suggestions for the plugin, please bring them up on
+[Discourse Meta](https://meta.discourse.org).
diff --git a/lib/generators/plugin/templates/javascript.es6.erb b/lib/generators/plugin/templates/javascript.es6.erb
new file mode 100644
index 00000000000..566b1ede916
--- /dev/null
+++ b/lib/generators/plugin/templates/javascript.es6.erb
@@ -0,0 +1,12 @@
+import { withPluginApi } from "discourse/lib/plugin-api";
+
+function initialize<%= classified_name %>(api) {
+}
+
+export default {
+ name: "<%= dasherized_name %>",
+
+ initialize() {
+ withPluginApi("0.8.24", initialize<%= classified_name %>);
+ }
+};
diff --git a/lib/generators/plugin/templates/plugin.rb.erb b/lib/generators/plugin/templates/plugin.rb.erb
new file mode 100644
index 00000000000..e0278a0c932
--- /dev/null
+++ b/lib/generators/plugin/templates/plugin.rb.erb
@@ -0,0 +1,26 @@
+# name: <%= name %>
+# about: AWESOME_PLUGIN
+# version: 0.1
+# authors: <%= @options['author'] %>
+# url: https://github.com/<%= @options['author'] %>
+
+<% if @options["stylesheet"] %>
+register_asset "stylesheets/common/<%= dasherized_name %>.scss"
+<% end %>
+
+enabled_site_setting :<%= underscored_name %>_enabled
+
+PLUGIN_NAME ||= "<%= name %>".freeze
+
+after_initialize do
+ module ::<%= classified_name %>
+ class Engine < ::Rails::Engine
+ engine_name PLUGIN_NAME
+ isolate_namespace <%= classified_name %>
+ end
+ end
+
+ <% if @options["scheduled_job"] %>
+ require File.expand_path("../jobs/scheduled/check_<%= underscored_name %>.rb", __FILE__)
+ <% end %>
+end
diff --git a/lib/generators/plugin/templates/scheduled_job.rb.erb b/lib/generators/plugin/templates/scheduled_job.rb.erb
new file mode 100644
index 00000000000..21d9e17b77d
--- /dev/null
+++ b/lib/generators/plugin/templates/scheduled_job.rb.erb
@@ -0,0 +1,8 @@
+module Jobs
+ class ::<%= classified_name %>::Check < Jobs::Scheduled
+ every 1.day
+
+ def execute(args = nil)
+ end
+ end
+end
diff --git a/lib/generators/plugin/templates/stylesheet.scss.erb b/lib/generators/plugin/templates/stylesheet.scss.erb
new file mode 100644
index 00000000000..ef54f605be4
--- /dev/null
+++ b/lib/generators/plugin/templates/stylesheet.scss.erb
@@ -0,0 +1,2 @@
+.<%= dasherized_name %> {
+}