From f6702eb532ef94f0b19b0b31ccc2dcbdf4664d43 Mon Sep 17 00:00:00 2001 From: Aidas Kasparas Date: Thu, 7 Dec 2017 14:28:18 +0200 Subject: [PATCH] Enable rails generators out of the box (#6043) * Enable rails generators out of the box Add bin/rails and Gemfile to generated plugin. This enables use of rails generators in the generated plugin right after including it into OP's Gemfile.plugins and setting OPENPROJECT_ROOT environment variable. [ci skip] --- .../open_project/plugin/plugin_generator.rb | 11 ++++++++ .../open_project/plugin/templates/Gemfile | 3 +++ .../plugin/templates/bin/rails.tt | 26 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 lib/generators/open_project/plugin/templates/Gemfile create mode 100755 lib/generators/open_project/plugin/templates/bin/rails.tt diff --git a/lib/generators/open_project/plugin/plugin_generator.rb b/lib/generators/open_project/plugin/plugin_generator.rb index f821188e96c..7886c65c8f6 100644 --- a/lib/generators/open_project/plugin/plugin_generator.rb +++ b/lib/generators/open_project/plugin/plugin_generator.rb @@ -36,6 +36,7 @@ class OpenProject::PluginGenerator < Rails::Generators::Base def generate_plugin plugin_dir lib_dir + bin_dir end def full_name @@ -70,4 +71,14 @@ class OpenProject::PluginGenerator < Rails::Generators::Base directory('lib', lib_path) end end + + def bin_path + "#{plugin_path}/bin" + end + + def bin_dir + @bin_dir ||= begin + directory('bin', bin_path) + end + end end diff --git a/lib/generators/open_project/plugin/templates/Gemfile b/lib/generators/open_project/plugin/templates/Gemfile new file mode 100644 index 00000000000..fa75df15632 --- /dev/null +++ b/lib/generators/open_project/plugin/templates/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/lib/generators/open_project/plugin/templates/bin/rails.tt b/lib/generators/open_project/plugin/templates/bin/rails.tt new file mode 100755 index 00000000000..8fcd60c45ac --- /dev/null +++ b/lib/generators/open_project/plugin/templates/bin/rails.tt @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby +# This command will automatically be run when you run "rails" with Rails gems +# installed from the root of your application. + +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/open_project/<%= plugin_name %>/engine', __FILE__) + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) + +## +# This is not ideal but better than nothing. +if ENV['OPENPROJECT_ROOT'] + # /Users/mkahl/dev/openproject/release/lib + path = Pathname(ENV["OPENPROJECT_ROOT"]).join("lib") + $LOAD_PATH.unshift(path.to_s) +else + puts + puts "Error: please define OPENPROJECT_ROOT pointing to your OpenProject core's root directory" + puts + Kernel.exit(1) +end + +require 'rails/all' +require 'rails/engine/commands'