DEV: improves rails plugin generator (#7949)

DEV: improves rails plugin generator (#7949)

Fixes bugs, simplifies code, more default files. General idea, more is more here as it’s easier to just delete things than reading and passing all the options.

diff --git a/lib/generators/plugin/plugin_generator.rb b/lib/generators/plugin/plugin_generator.rb
index a1926ab..2064f61 100644
--- a/lib/generators/plugin/plugin_generator.rb
+++ b/lib/generators/plugin/plugin_generator.rb
@@ -3,75 +3,99 @@
 require 'rails/generators/named_base'
 
 class PluginGenerator < Rails::Generators::NamedBase
+  attr_writer :about
   attr_writer :github_username
 
   desc 'This generator creates a Discourse plugin skeleton'
 
   source_root File.expand_path('templates', __dir__)
 
-  class_option :controller, type: :boolean, desc: "Generate controller", default: true
-  class_option :spec, type: :boolean, desc: "Generate spec", default: true
-  class_option :acceptance, type: :boolean, desc: "Generate acceptance test", default: 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 scheduled job", default: false
-  class_option :help, type: :boolean, desc: "Adds help comments in generated files", default: true
+  class_option :no_license, type: :boolean, desc: "No license", default: false
 
-  def create_acceptance_file
-    return unless @options['acceptance']
+  def create_plugin
+    @about ||= ask("What is the purpose of your plugin?")
+    @github_username ||= ask("Github username?")
 
-    template 'acceptance-test.js.es6.erb', File.join('plugins', dasherized_name, "test/javascripts/acceptance", "#{dasherized_name}-test.js.es6")
+    readme_file
+    routes_file
+    engine_file
+    plugin_file
+    controller_file
+    license_file
+    stylesheet_file
+    javascript_file
+    settings_file
+    locales_file
   end
 
-  def create_spec_file
-    return if !@options['spec'] || !@options['controller']
-
+  def controller_file
+    template 'plugin_controller.rb.erb', File.join('plugins', dasherized_name, "app/controllers/#{dasherized_name}/#{underscored_name}_controller.rb")
+    template 'controller.rb.erb', File.join('plugins', dasherized_name, "app/controllers/#{dasherized_name}/actions_controller.rb")
     template 'controller_spec.rb.erb', File.join('plugins', dasherized_name, "spec/requests/actions_controller_spec.rb")
   end
 
-  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
-    ensure_github_username
-
+  def readme_file
     template 'README.md.erb', File.join('plugins', dasherized_name, "README.md")
   end
 
-  def create_license_file
-    ensure_github_username
+  def license_file
+    return if @options['no_license']
 
     template 'LICENSE.erb', File.join('plugins', dasherized_name, "LICENSE")
   end
 
-  def create_plugin_file
-    ensure_github_username
+  def engine_file
+    template 'engine.rb.erb', File.join('plugins', dasherized_name, "lib", dasherized_name, "engine.rb")
+  end
 
-    template 'plugin.rb.erb', File.join('plugins', dasherized_name, "plugin.rb")
+  def routes_file
+    template 'routes.rb.erb', File.join('plugins', dasherized_name, "config", "routes.rb")
+    template 'route_constraint.rb.erb', File.join('plugins', dasherized_name, "lib", "#{underscored_name}_constraint.rb")
   end
 
-  def create_stylesheet_file
-    return unless @options['stylesheet']
+  def plugin_file
+    template 'plugin.rb.erb', File.join('plugins', dasherized_name, "plugin.rb")
+  end
 
+  def stylesheet_file
     template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/common', "#{dasherized_name}.scss")
+    template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/desktop', "#{dasherized_name}.scss")
+    template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/mobile', "#{dasherized_name}.scss")
   end
 
-  def create_javascript_file
-    return unless @options['javascript']
+  def javascript_file
+    template 'acceptance-test.js.es6.erb', File.join('plugins', dasherized_name, "test/javascripts/acceptance", "#{dasherized_name}-test.js.es6")
+    template 'javascript.js.es6.erb', File.join('plugins', dasherized_name, 'assets/javascripts/initializers', "#{dasherized_name}.js.es6")
+    template 'route-map.js.es6.erb', File.join('plugins', dasherized_name, 'assets/javascripts/discourse', "#{dasherized_name}-route-map.js.es6")
+
+    folder = 'assets/javascripts/discourse/templates'
+    template "#{folder}/template.hbs.erb", path(folder, "actions.hbs")
+    template "#{folder}/template-show.hbs.erb", path(folder, "actions-show.hbs")
+    template "#{folder}/template-index.hbs.erb", path(folder, "actions-index.hbs")
+
+    folder = 'assets/javascripts/discourse/routes'
+    template "#{folder}/route.js.es6.erb", path(folder, "#{dasherized_name}-actions.js.es6")
+    template "#{folder}/route-show.js.es6.erb", path(folder, "#{dasherized_name}-actions-show.js.es6")
+    template "#{folder}/route-index.js.es6.erb", path(folder, "#{dasherized_name}-actions-index.js.es6")
+
+    folder = 'assets/javascripts/discourse/controllers'
+    template "#{folder}/controller.js.es6.erb", path(folder, "actions.js.es6")
+    template "#{folder}/controller-show.js.es6.erb", path(folder, "actions-show.js.es6")
+    template "#{folder}/controller-index.js.es6.erb", path(folder, "actions-index.js.es6")
+
+    folder = 'assets/javascripts/discourse/models'
+    template "#{folder}/model.js.es6.erb", path(folder, "action.js.es6")
 
-    template 'javascript.es6.erb', File.join('plugins', dasherized_name, 'assets/javascripts/initializers', "#{dasherized_name}.es6")
+    folder = 'assets/javascripts/discourse/adapters'
+    template "#{folder}/adapter.js.es6.erb", path(folder, "action.js.es6")
   end
 
-  def create_settings_file
+  def settings_file
     template 'settings.yml.erb', File.join('plugins', dasherized_name, 'config', 'settings.yml')
   end
 
-  def create_locales_file
-    template 'client.en.yml.erb', File.join('plugins', dasherized_name, 'config/locales', 'client.en.yml')
+  def locales_file
+    template 'client.en.yml.erb', path('config/locales/client.en.yml')
     template 'server.en.yml.erb', File.join('plugins', dasherized_name, 'config/locales', 'server.en.yml')
   end
 
@@ -83,10 +107,6 @@ class PluginGenerator < Rails::Generators::NamedBase
     end
   end
 
-  def ensure_github_username
-    @github_username ||= ask("Github username?")
-  end
-
   def underscored_name
     name.underscore
   end
@@ -98,4 +118,8 @@ class PluginGenerator < Rails::Generators::NamedBase
   def classified_name
     name.tableize.classify
   end
+
+  def path(*args)
+    File.join('plugins', dasherized_name, args)
+  end
 end
diff --git a/lib/generators/plugin/templates/assets/javascripts/discourse/adapters/adapter.js.es6.erb b/lib/generators/plugin/templates/assets/javascripts/discourse/adapters/adapter.js.es6.erb
new file mode 100644
index 0000000..f952121
--- /dev/null
+++ b/lib/generators/plugin/templates/assets/javascripts/discourse/adapters/adapter.js.es6.erb
@@ -0,0 +1,7 @@
+import RestAdapter from "discourse/adapters/rest";
+
+export default RestAdapter.extend({
+  basePath() {
+    return "/<%= dasherized_name %>/";
+  }
+});
diff --git a/lib/generators/plugin/templates/assets/javascripts/discourse/controllers/controller-index.js.es6.erb b/lib/generators/plugin/templates/assets/javascripts/discourse/controllers/controller-index.js.es6.erb
new file mode 100644
index 0000000..044750a
--- /dev/null
+++ b/lib/generators/plugin/templates/assets/javascripts/discourse/controllers/controller-index.js.es6.erb
@@ -0,0 +1,4 @@
+export default Ember.Controller.extend({
+  actions: {
+  }
+});

[... diff too long, it was truncated ...]

GitHub sha: d83f99fc

2 Likes

This commit has been mentioned on Discourse Meta. There might be relevant details there: