2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-29 19:32:41 +08:00
|
|
|
require 'rails/generators/named_base'
|
|
|
|
|
|
|
|
class PluginGenerator < Rails::Generators::NamedBase
|
2019-07-28 00:21:44 +08:00
|
|
|
attr_writer :about
|
2018-08-30 18:03:30 +08:00
|
|
|
attr_writer :github_username
|
|
|
|
|
2018-08-29 19:32:41 +08:00
|
|
|
desc 'This generator creates a Discourse plugin skeleton'
|
|
|
|
|
|
|
|
source_root File.expand_path('templates', __dir__)
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
class_option :no_license, type: :boolean, desc: "No license", default: false
|
2018-08-29 19:32:41 +08:00
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def create_plugin
|
|
|
|
@about ||= ask("What is the purpose of your plugin?")
|
|
|
|
@github_username ||= ask("Github username?")
|
2018-08-30 22:38:25 +08:00
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
readme_file
|
|
|
|
routes_file
|
|
|
|
engine_file
|
|
|
|
plugin_file
|
|
|
|
controller_file
|
|
|
|
license_file
|
|
|
|
stylesheet_file
|
|
|
|
javascript_file
|
|
|
|
settings_file
|
|
|
|
locales_file
|
2018-08-30 22:38:25 +08:00
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def controller_file
|
2019-10-21 15:24:14 +08:00
|
|
|
template 'plugin_controller.rb.erb', File.join('plugins', dasherized_name, "app/controllers/#{underscored_name}/#{underscored_name}_controller.rb")
|
2019-08-06 16:40:38 +08:00
|
|
|
template 'controller.rb.erb', File.join('plugins', dasherized_name, "app/controllers/#{underscored_name}/actions_controller.rb")
|
2018-08-30 22:38:25 +08:00
|
|
|
template 'controller_spec.rb.erb', File.join('plugins', dasherized_name, "spec/requests/actions_controller_spec.rb")
|
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def readme_file
|
2018-08-29 19:32:41 +08:00
|
|
|
template 'README.md.erb', File.join('plugins', dasherized_name, "README.md")
|
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def license_file
|
|
|
|
return if @options['no_license']
|
2018-08-30 18:03:30 +08:00
|
|
|
|
2018-08-29 19:32:41 +08:00
|
|
|
template 'LICENSE.erb', File.join('plugins', dasherized_name, "LICENSE")
|
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def engine_file
|
|
|
|
template 'engine.rb.erb', File.join('plugins', dasherized_name, "lib", dasherized_name, "engine.rb")
|
|
|
|
end
|
2018-08-30 18:03:30 +08:00
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
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")
|
2018-08-29 19:32:41 +08:00
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def plugin_file
|
|
|
|
template 'plugin.rb.erb', File.join('plugins', dasherized_name, "plugin.rb")
|
|
|
|
end
|
2018-08-29 19:32:41 +08:00
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def stylesheet_file
|
2018-08-29 19:32:41 +08:00
|
|
|
template 'stylesheet.scss.erb', File.join('plugins', dasherized_name, 'assets/stylesheets/common', "#{dasherized_name}.scss")
|
2019-07-28 00:21:44 +08:00
|
|
|
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")
|
2018-08-29 19:32:41 +08:00
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
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")
|
2018-08-29 19:32:41 +08:00
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
folder = 'assets/javascripts/discourse/adapters'
|
|
|
|
template "#{folder}/adapter.js.es6.erb", path(folder, "action.js.es6")
|
2018-08-29 19:32:41 +08:00
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def settings_file
|
2018-08-30 20:12:06 +08:00
|
|
|
template 'settings.yml.erb', File.join('plugins', dasherized_name, 'config', 'settings.yml')
|
|
|
|
end
|
|
|
|
|
2019-07-28 00:21:44 +08:00
|
|
|
def locales_file
|
|
|
|
template 'client.en.yml.erb', path('config/locales/client.en.yml')
|
2018-08-30 20:12:06 +08:00
|
|
|
template 'server.en.yml.erb', File.join('plugins', dasherized_name, 'config/locales', 'server.en.yml')
|
|
|
|
end
|
|
|
|
|
2018-08-29 19:32:41 +08:00
|
|
|
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
|
2019-07-28 00:21:44 +08:00
|
|
|
|
|
|
|
def path(*args)
|
|
|
|
File.join('plugins', dasherized_name, args)
|
|
|
|
end
|
2018-08-29 19:32:41 +08:00
|
|
|
end
|