From 35eca54e01ae92c92450315d48aaf94f52fd2e45 Mon Sep 17 00:00:00 2001
From: Takashi Kato <tohosaku@users.osdn.me>
Date: Mon, 27 Mar 2023 12:07:18 +0000
Subject: [PATCH 2/4] Add redmine_plugin_gem generator

---
 lib/generators/redmine_plugin_gem/USAGE       | 29 +++++++++
 .../redmine_plugin_gem_generator.rb           | 60 +++++++++++++++++++
 .../redmine_plugin_gem/templates/Gemfile.erb  |  7 +++
 .../redmine_plugin_gem/templates/README.rdoc  |  3 +
 .../redmine_plugin_gem/templates/Rakefile     |  4 ++
 .../templates/en_rails_i18n.yml               |  3 +
 .../redmine_plugin_gem/templates/gemspec.erb  | 24 ++++++++
 .../redmine_plugin_gem/templates/init.rb.erb  | 13 ++++
 .../redmine_plugin_gem/templates/routes.rb    |  2 +
 .../templates/test_helper.rb.erb              |  2 +
 10 files changed, 147 insertions(+)
 create mode 100644 lib/generators/redmine_plugin_gem/USAGE
 create mode 100644 lib/generators/redmine_plugin_gem/redmine_plugin_gem_generator.rb
 create mode 100644 lib/generators/redmine_plugin_gem/templates/Gemfile.erb
 create mode 100644 lib/generators/redmine_plugin_gem/templates/README.rdoc
 create mode 100644 lib/generators/redmine_plugin_gem/templates/Rakefile
 create mode 100644 lib/generators/redmine_plugin_gem/templates/en_rails_i18n.yml
 create mode 100644 lib/generators/redmine_plugin_gem/templates/gemspec.erb
 create mode 100644 lib/generators/redmine_plugin_gem/templates/init.rb.erb
 create mode 100644 lib/generators/redmine_plugin_gem/templates/routes.rb
 create mode 100644 lib/generators/redmine_plugin_gem/templates/test_helper.rb.erb

diff --git a/lib/generators/redmine_plugin_gem/USAGE b/lib/generators/redmine_plugin_gem/USAGE
new file mode 100644
index 000000000..cea4ccff3
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/USAGE
@@ -0,0 +1,29 @@
+Description:
+    The plugin generator creates stubs for a new Redmine plugin.
+
+Example:
+    bundle exec rails generate redmine_plugin_gem meetings
+      create  gems/meetings/app
+      create  gems/meetings/app/controllers
+      create  gems/meetings/app/helpers
+      create  gems/meetings/app/models
+      create  gems/meetings/app/views
+      create  gems/meetings/db/migrate
+      create  gems/meetings/lib/tasks
+      create  gems/meetings/assets/images
+      create  gems/meetings/assets/javascripts
+      create  gems/meetings/assets/stylesheets
+      create  gems/meetings/config/locales
+      create  gems/meetings/test
+      create  gems/meetings/test/fixtures
+      create  gems/meetings/test/unit
+      create  gems/meetings/test/functional
+      create  gems/meetings/test/integration
+      create  gems/meetings/Gemfile
+      create  gems/meetings/Rakefile
+      create  gems/meetings/README.rdoc
+      create  gems/meetings/init.rb
+      create  gems/meetings/meetings.gemspec
+      create  gems/meetings/config/routes.rb
+      create  gems/meetings/config/locales/en.yml
+      create  gems/meetings/test/test_helper.rb
diff --git a/lib/generators/redmine_plugin_gem/redmine_plugin_gem_generator.rb b/lib/generators/redmine_plugin_gem/redmine_plugin_gem_generator.rb
new file mode 100644
index 000000000..2b355c622
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/redmine_plugin_gem_generator.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+# Redmine - project management software
+# Copyright (C) 2006-2023  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+class RedminePluginGemGenerator < Rails::Generators::NamedBase
+  source_root File.expand_path('templates', __dir__)
+
+  attr_reader :gem_path, :plugin_name, :plugin_pretty_name
+
+  def initialize(*args)
+    super
+    @plugin_name = file_name.underscore
+    @plugin_pretty_name = plugin_name.titleize
+    @gem_path = Rails.root.join('gems', plugin_name)
+  end
+
+  def copy_templates
+    empty_directory "#{gem_path}/app"
+    empty_directory "#{gem_path}/app/controllers"
+    empty_directory "#{gem_path}/app/helpers"
+    empty_directory "#{gem_path}/app/models"
+    empty_directory "#{gem_path}/app/views"
+    empty_directory "#{gem_path}/db/migrate"
+    empty_directory "#{gem_path}/lib/tasks"
+    empty_directory "#{gem_path}/assets/images"
+    empty_directory "#{gem_path}/assets/javascripts"
+    empty_directory "#{gem_path}/assets/stylesheets"
+    empty_directory "#{gem_path}/config/locales"
+    empty_directory "#{gem_path}/test"
+    empty_directory "#{gem_path}/test/fixtures"
+    empty_directory "#{gem_path}/test/unit"
+    empty_directory "#{gem_path}/test/functional"
+    empty_directory "#{gem_path}/test/integration"
+    empty_directory "#{gem_path}/test/system"
+
+    template 'README.rdoc',    "#{gem_path}/README.rdoc"
+    template 'Gemfile.erb',    "#{gem_path}/Gemfile"
+    template 'Rakefile',    "#{gem_path}/Rakefile"
+    template 'init.rb.erb',   "#{gem_path}/init.rb"
+    template 'routes.rb',    "#{gem_path}/config/routes.rb"
+    template 'en_rails_i18n.yml',    "#{gem_path}/config/locales/en.yml"
+    template 'test_helper.rb.erb',    "#{gem_path}/test/test_helper.rb"
+    template 'gemspec.erb', "#{gem_path}/#{plugin_name}.gemspec"
+  end
+end
diff --git a/lib/generators/redmine_plugin_gem/templates/Gemfile.erb b/lib/generators/redmine_plugin_gem/templates/Gemfile.erb
new file mode 100644
index 000000000..d6245cdc5
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/Gemfile.erb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+source "https://rubygems.org"
+
+# Specify your gem's dependencies in <%= plugin_name %>.gemspec
+gemspec
+
diff --git a/lib/generators/redmine_plugin_gem/templates/README.rdoc b/lib/generators/redmine_plugin_gem/templates/README.rdoc
new file mode 100644
index 000000000..7a31a890e
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/README.rdoc
@@ -0,0 +1,3 @@
+= <%= file_name %>
+
+Description goes here
diff --git a/lib/generators/redmine_plugin_gem/templates/Rakefile b/lib/generators/redmine_plugin_gem/templates/Rakefile
new file mode 100644
index 000000000..b5ce21be1
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/Rakefile
@@ -0,0 +1,4 @@
+# frozen_string_literal: true
+
+require "bundler/gem_tasks"
+task default: []
diff --git a/lib/generators/redmine_plugin_gem/templates/en_rails_i18n.yml b/lib/generators/redmine_plugin_gem/templates/en_rails_i18n.yml
new file mode 100644
index 000000000..642b07f82
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/en_rails_i18n.yml
@@ -0,0 +1,3 @@
+# English strings go here for Rails i18n
+en:
+  # my_label: "My label"
diff --git a/lib/generators/redmine_plugin_gem/templates/gemspec.erb b/lib/generators/redmine_plugin_gem/templates/gemspec.erb
new file mode 100644
index 000000000..1182cddc1
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/gemspec.erb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+Gem::Specification.new do |spec|
+
+  # Do not use constants or variables from the gem's own code in this block, as is normally
+  # done with gems. (e.g. Foo::VERSION)
+  # Specify the version of redmine or dependencies between plugins in the init.rb file.
+
+  spec.name = "<%= plugin_name %>"
+  spec.version = "0.0.1"
+  spec.authors = ["johndoe"]
+  spec.email = ["johndoe@example.org"]
+
+  spec.summary = "<%= plugin_pretty_name %> plugin"
+  spec.description = "This is a plugin for Redmine"
+  spec.homepage = "https://example.org"
+  spec.required_ruby_version = ">= 2.7.0"
+
+  spec.metadata["author_url"] = spec.homepage
+  spec.files = Dir["{app,lib,config,assets,db}/**/*", "init.rb", "Gemfile", "README.rdoc"]
+
+  # DO NOT DELETE this attribute
+  spec.metadata["redmine_plugin_id"] = "<%= plugin_name %>"
+end
diff --git a/lib/generators/redmine_plugin_gem/templates/init.rb.erb b/lib/generators/redmine_plugin_gem/templates/init.rb.erb
new file mode 100644
index 000000000..20c7cbc0f
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/init.rb.erb
@@ -0,0 +1,13 @@
+Redmine::Plugin.register :<%= plugin_name %> do
+  # For gemmed plugins, the attributes of the plugin are described in the gemspec.
+  # The correspondence between plugin attributes and gemspec is as follows
+
+  # plugin name        => gemspec summary
+  # plugin author      => gemspec authors
+  # plugin description => gemspec description
+  # plugin version     => gemspec version
+  # plugin url         => gemspec homepage
+  # plugin author_url  => gemspec metadata['author_url']
+
+  # Other attributes should continue to be added to this block
+end
diff --git a/lib/generators/redmine_plugin_gem/templates/routes.rb b/lib/generators/redmine_plugin_gem/templates/routes.rb
new file mode 100644
index 000000000..1803173d0
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/routes.rb
@@ -0,0 +1,2 @@
+# Plugin's routes
+# See: http://guides.rubyonrails.org/routing.html
diff --git a/lib/generators/redmine_plugin_gem/templates/test_helper.rb.erb b/lib/generators/redmine_plugin_gem/templates/test_helper.rb.erb
new file mode 100644
index 000000000..55b016f6c
--- /dev/null
+++ b/lib/generators/redmine_plugin_gem/templates/test_helper.rb.erb
@@ -0,0 +1,2 @@
+# Load the Redmine helper
+require_relative '../../../test/test_helper'
-- 
2.39.2

