Skip to content

Commit

Permalink
Fix plugins support #153
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Jun 28, 2015
1 parent fee8190 commit 1fe7a44
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion lib/i18n/tasks/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/i18n/tasks/command/commander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/config/i18n-tasks.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<% require './lib/test_i18n_plugin'
::I18n::Tasks::Commands.send :include, TestI18nPlugin %>

base_locale: en
locales: [es]

Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/lib/test_i18n_plugin.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 16 additions & 12 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1fe7a44

Please sign in to comment.