From 1fe7a442c18a7b76d91ce8cd400225eb3e5d88b2 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 28 Jun 2015 13:55:02 +0100 Subject: [PATCH] Fix plugins support #153 --- Rakefile | 2 +- lib/i18n/tasks/cli.rb | 8 +++++++- lib/i18n/tasks/command/commander.rb | 10 +++++----- spec/fixtures/config/i18n-tasks.yml | 3 +++ spec/fixtures/lib/test_i18n_plugin.rb | 12 ++++++++++++ spec/i18n_tasks_spec.rb | 28 +++++++++++++++------------ 6 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 spec/fixtures/lib/test_i18n_plugin.rb diff --git a/Rakefile b/Rakefile index 0b8e7585..12067436 100644 --- a/Rakefile +++ b/Rakefile @@ -7,5 +7,5 @@ task :irb do # $: << File.expand_path('lib', __FILE__) require 'i18n/tasks' require 'i18n/tasks/commands' - ::I18n::Tasks::Commands.new.irb + ::I18n::Tasks::Commands.new(::I18n::Tasks::BaseTask.new).irb end diff --git a/lib/i18n/tasks/cli.rb b/lib/i18n/tasks/cli.rb index fdae685c..e4e10fb5 100644 --- a/lib/i18n/tasks/cli.rb +++ b/lib/i18n/tasks/cli.rb @@ -40,10 +40,12 @@ def run(argv) end def context - @context ||= ::I18n::Tasks::Commands.new.tap(&:set_internal_locale!) + @context ||= ::I18n::Tasks::Commands.new(base_task).tap(&:set_internal_locale!) end def commands + # load base task to initialize plugins + base_task @commands ||= ::I18n::Tasks::Commands.cmds.dup.tap do |cmds| # Hash#transform_keys is only available since activesupport v4.0.0 cmds.keys.each { |k| cmds[k.to_s.tr('_', '-')] = cmds.delete(k) } @@ -52,6 +54,10 @@ def commands private + def base_task + @base_task ||= I18n::Tasks::BaseTask.new + end + def parse!(argv) command = parse_command! argv options = optparse! command, argv diff --git a/lib/i18n/tasks/command/commander.rb b/lib/i18n/tasks/command/commander.rb index dbba92bf..6e9c1107 100644 --- a/lib/i18n/tasks/command/commander.rb +++ b/lib/i18n/tasks/command/commander.rb @@ -8,7 +8,11 @@ module Command class Commander include ::I18n::Tasks::Logging - def initialize(i18n = nil) + attr_reader :i18n + + + # @param [I18n::Tasks::BaseTask] i18n + def initialize(i18n) @i18n = i18n end @@ -37,10 +41,6 @@ def spreadsheet_report @spreadsheet_report ||= I18n::Tasks::Reports::Spreadsheet.new(i18n) end - def i18n - @i18n ||= I18n::Tasks::BaseTask.new - end - delegate :base_locale, :locales, :t, to: :i18n end end diff --git a/spec/fixtures/config/i18n-tasks.yml b/spec/fixtures/config/i18n-tasks.yml index 41bb0ce3..e9db2861 100644 --- a/spec/fixtures/config/i18n-tasks.yml +++ b/spec/fixtures/config/i18n-tasks.yml @@ -1,3 +1,6 @@ +<% require './lib/test_i18n_plugin' + ::I18n::Tasks::Commands.send :include, TestI18nPlugin %> + base_locale: en locales: [es] diff --git a/spec/fixtures/lib/test_i18n_plugin.rb b/spec/fixtures/lib/test_i18n_plugin.rb new file mode 100644 index 00000000..42423ae0 --- /dev/null +++ b/spec/fixtures/lib/test_i18n_plugin.rb @@ -0,0 +1,12 @@ +# An i18n-tasks plugin to test that the plugin system works. +module TestI18nPlugin + include ::I18n::Tasks::Command::Collection + + cmd :greet, + desc: 'print "Hello, %{name}"', + args: [['-n', '--name NAME', 'name']] + + def greet(opts = {}) + puts "Hello, #{opts[:name]}" + end +end diff --git a/spec/i18n_tasks_spec.rb b/spec/i18n_tasks_spec.rb index 77c7eeb6..02d5e968 100644 --- a/spec/i18n_tasks_spec.rb +++ b/spec/i18n_tasks_spec.rb @@ -10,18 +10,22 @@ describe 'bin/i18n-tasks' do it 'shows help when invoked with no arguments, shows version on --version' do # These bin/i18n-tasks tests are executed in parallel for performance - [ - proc { - out, err, status = Open3.capture3('bin/i18n-tasks') - expect(status).to be_success - expect(out).to be_empty - expect(err).to start_with('Usage: i18n-tasks [command] [options]') - expect(err).to include('Available commands', 'add-missing') - }, - proc { - expect(%x[bin/i18n-tasks --version].chomp).to eq(I18n::Tasks::VERSION) - } - ].map { |test| Thread.start(&test) }.each(&:join) + in_test_app_dir do + [ + proc { + out, err, status = Open3.capture3('../../bin/i18n-tasks') + expect(status).to be_success + expect(out).to be_empty + expect(err).to start_with('Usage: i18n-tasks [command] [options]') + expect(err).to include('Available commands', 'add-missing') + # a task from a plugin + expect(err).to include('greet') + }, + proc { + expect(%x[../../bin/i18n-tasks --version].chomp).to eq(I18n::Tasks::VERSION) + } + ].map { |test| Thread.start(&test) }.each(&:join) + end end end