2018-08-29 07:32:41 -04:00
|
|
|
require 'rails/generators/named_base'
|
|
|
|
|
|
|
|
class PluginGenerator < Rails::Generators::NamedBase
|
2018-08-30 06:03:30 -04:00
|
|
|
attr_writer :github_username
|
|
|
|
|
2018-08-29 07:32:41 -04:00
|
|
|
desc 'This generator creates a Discourse plugin skeleton'
|
|
|
|
|
|
|
|
source_root File.expand_path('templates', __dir__)
|
|
|
|
|
2018-08-30 10:38:25 -04:00
|
|
|
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
|
2018-08-29 07:32:41 -04:00
|
|
|
class_option :stylesheet, type: :boolean, desc: "Generate Stylesheet", default: true
|
|
|
|
class_option :javascript, type: :boolean, desc: "Generate Javascript initializer", default: true
|
2018-08-29 07:51:16 -04:00
|
|
|
class_option :scheduled_job, type: :boolean, desc: "Generate scheduled job", default: false
|
2018-08-30 06:16:37 -04:00
|
|
|
class_option :help, type: :boolean, desc: "Adds help comments in generated files", default: true
|
2018-08-29 07:32:41 -04:00
|
|
|
|
2018-08-30 10:38:25 -04:00
|
|
|
def create_acceptance_file
|
|
|
|
return unless @options['acceptance']
|
|
|
|
|
|
|
|
template 'acceptance-test.js.es6.erb', File.join('plugins', dasherized_name, "test/javascripts/acceptance", "#{dasherized_name}-test.js.es6")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_spec_file
|
|
|
|
return if !@options['spec'] || !@options['controller']
|
|
|
|
|
|
|
|
template 'controller_spec.rb.erb', File.join('plugins', dasherized_name, "spec/requests/actions_controller_spec.rb")
|
|
|
|
end
|
|
|
|
|
2018-08-29 07:32:41 -04:00
|
|
|
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
|
2018-08-30 06:03:30 -04:00
|
|
|
ensure_github_username
|
|
|
|
|
2018-08-29 07:32:41 -04:00
|
|
|
template 'README.md.erb', File.join('plugins', dasherized_name, "README.md")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_license_file
|
2018-08-30 06:03:30 -04:00
|
|
|
ensure_github_username
|
|
|
|
|
2018-08-29 07:32:41 -04:00
|
|
|
template 'LICENSE.erb', File.join('plugins', dasherized_name, "LICENSE")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_plugin_file
|
2018-08-30 06:03:30 -04:00
|
|
|
ensure_github_username
|
|
|
|
|
2018-08-29 07:32:41 -04:00
|
|
|
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
|
|
|
|
|
2018-08-30 08:12:06 -04:00
|
|
|
def create_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')
|
|
|
|
template 'server.en.yml.erb', File.join('plugins', dasherized_name, 'config/locales', 'server.en.yml')
|
|
|
|
end
|
|
|
|
|
2018-08-29 07:32:41 -04: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
|
|
|
|
|
2018-08-30 06:03:30 -04:00
|
|
|
def ensure_github_username
|
|
|
|
@github_username ||= ask("Github username?")
|
|
|
|
end
|
|
|
|
|
2018-08-29 07:32:41 -04:00
|
|
|
def underscored_name
|
|
|
|
name.underscore
|
|
|
|
end
|
|
|
|
|
|
|
|
def dasherized_name
|
|
|
|
underscored_name.dasherize
|
|
|
|
end
|
|
|
|
|
|
|
|
def classified_name
|
|
|
|
name.tableize.classify
|
|
|
|
end
|
|
|
|
end
|