Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
* upstream/master:
  add app/mailers to default search.relative_roots
  Bump to 0.8.5
  Fix plugins support glebm#153
  changelog
  Bump to 0.8.4
  Lock terminal-table to 1.4.5 due to tj/terminal-table#55
  changelog
  Use ActiveSupport::Inflector.constantize instead of const_get for ruby 1.9 support
  Reduce ActiveSupport dependencies, compat with 2.3
  Support ActionMailer
  • Loading branch information
bartimaeus committed Jun 29, 2015
2 parents 22ea682 + 9cd47aa commit 80bfe1a
Show file tree
Hide file tree
Showing 24 changed files with 133 additions and 40 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.8.5

* Fix regression: Plugin support [#153](https://github.com/glebm/i18n-tasks/issues/153).

## 0.8.4

* Support relative keys in mailers [#155](https://github.com/glebm/i18n-tasks/issues/155).

## 0.8.3

* Fix regression: ActiveSupport < 4 support [#143](https://github.com/glebm/i18n-tasks/issues/143).
Expand Down
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
2 changes: 1 addition & 1 deletion i18n-tasks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TEXT
s.add_dependency 'activesupport'
s.add_dependency 'easy_translate', '>= 0.5.0'
s.add_dependency 'term-ansicolor'
s.add_dependency 'terminal-table'
s.add_dependency 'terminal-table', '>= 1.5.1'
s.add_dependency 'highline'
s.add_dependency 'i18n'
s.add_development_dependency 'axlsx', '~> 2.0'
Expand Down
11 changes: 9 additions & 2 deletions lib/i18n/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ module Data
end


require 'active_support/inflector'
require 'active_support/core_ext/hash'
require 'active_support/core_ext/string'
require 'active_support/core_ext/array/access'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/object/blank'
begin
# activesupport >= 3
require 'active_support/core_ext/object/try'
rescue LoadError => _e
# activesupport ~> 2.3.2
require 'active_support/core_ext/try'
end
require 'term/ansicolor'
require 'erubis'

Expand Down
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
1 change: 1 addition & 0 deletions lib/i18n/tasks/command/collection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'i18n/tasks/command/dsl'
require 'i18n/tasks/command/options/common'
require 'i18n/tasks/command/options/locales'
require 'i18n/tasks/command/options/data'
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
2 changes: 2 additions & 0 deletions lib/i18n/tasks/command/commands/missing.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'i18n/tasks/command/collection'

module I18n::Tasks
module Command
module Commands
Expand Down
4 changes: 1 addition & 3 deletions lib/i18n/tasks/command/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ module Command
module DSL
def self.included(base)
base.module_eval do
@dsl = HashWithIndifferentAccess.new { |h, k|
h[k] = HashWithIndifferentAccess.new
}
@dsl = Hash.new { |h, k| h[k] = {} }
extend ClassMethods
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/tasks/command/options/common.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'i18n/tasks/command/dsl'

module I18n::Tasks
module Command
Expand Down
6 changes: 3 additions & 3 deletions lib/i18n/tasks/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def data
@data ||= begin
data_config = (config[:data] || {}).with_indifferent_access
data_config.merge!(base_locale: base_locale, locales: config[:locales])
adapter_class = data_config[:adapter].presence || data_config[:class].presence || :file_system
adapter_class = data_config[:adapter].presence || data_config[:class].presence || 'file_system'
adapter_class = adapter_class.to_s
adapter_class = "I18n::Tasks::Data::#{adapter_class.camelize}" if adapter_class !~ /[A-Z]/
adapter_class = 'I18n::Tasks::Data::FileSystem' if adapter_class == 'file_system'
data_config.except!(:adapter, :class)
adapter_class.constantize.new data_config
ActiveSupport::Inflector.constantize(adapter_class).new data_config
end
end

Expand Down
11 changes: 7 additions & 4 deletions lib/i18n/tasks/data/file_system_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,16 @@ def with_router(router)
self.router = router_was
end


ROUTER_NAME_ALIASES = {
'conservative_router' => 'I18n::Tasks::Data::Router::ConservativeRouter',
'pattern_router' => 'I18n::Tasks::Data::Router::PatternRouter'
}
def router
@router ||= begin
name = @config[:router].presence || 'conservative_router'
if name[0] != name[0].upcase
name = "I18n::Tasks::Data::Router::#{name.classify}"
end
name.constantize.new(self, @config.merge(base_locale: base_locale, locales: locales))
name = ROUTER_NAME_ALIASES[name] || name
ActiveSupport::Inflector.constantize(name).new(self, @config.merge(base_locale: base_locale, locales: locales))
end
end
attr_writer :router
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/data/tree/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def grep_keys(match, opts = {})
def set_each_value!(val_pattern, key_pattern = nil, &value_proc)
value_proc ||= proc { |node|
node_value = node.value
human_key = node.key.to_s.humanize
human_key = ActiveSupport::Inflector.humanize(node.key.to_s)
StringInterpolation.interpolate_soft(
val_pattern,
value: node_value,
Expand Down
4 changes: 3 additions & 1 deletion lib/i18n/tasks/scanners/base_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class BaseScanner

def initialize(config = {})
@config = config.dup.with_indifferent_access.tap do |conf|
conf[:relative_roots] = %w(app/views app/controllers app/helpers app/presenters) if conf[:relative_roots].blank?
if conf[:relative_roots].blank?
conf[:relative_roots] = %w(app/controllers app/helpers app/mailers app/presenters app/views)
end
conf[:paths] = %w(app/) if conf[:paths].blank?
conf[:include] = Array(conf[:include]) if conf[:include].present?
conf[:exclude] = Array(conf[:exclude]) + ALWAYS_EXCLUDE
Expand Down
6 changes: 5 additions & 1 deletion lib/i18n/tasks/scanners/pattern_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def match_to_key(match, path, location)

def absolute_key(key, path, location)
if key.start_with?('.')
if controller_file?(path)
if controller_file?(path) || mailer_file?(path)
absolutize_key(key, path, relative_roots, closest_method(location))
else
absolutize_key(key, path)
Expand All @@ -61,6 +61,10 @@ def controller_file?(path)
/controllers/.match(path)
end

def mailer_file?(path)
/mailers/.match(path)
end

def closest_method(location)
method = File.readlines(location[:src_path], encoding: 'UTF-8').first(location[:line_num] - 1).reverse_each.find { |x| x=~ /\bdef\b/ }
method &&= method.strip.sub(/^def\s*/, '').sub(/[\(\s;].*$/, '')
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/split_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def split_key(key, max = Float::INFINITY)
parts << part
pos += part.length + 1
if parts.length + 1 >= max
parts << key.from(pos) unless pos == key.length
parts << key[pos..-1] unless pos == key.length
break
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/used_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def scanner
@scanner ||= begin
search_config = (config[:search] || {}).with_indifferent_access
class_name = search_config[:scanner] || '::I18n::Tasks::Scanners::PatternWithScopeScanner'
class_name.constantize.new search_config
ActiveSupport::Inflector.constantize(class_name).new search_config
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
module I18n
module Tasks
VERSION = '0.8.3'
VERSION = '0.8.5'
end
end
4 changes: 4 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 Expand Up @@ -36,6 +39,7 @@ search:
relative_roots:
- app/views
- app/controllers
- app/mailers


# do not report these keys ever
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
30 changes: 17 additions & 13 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 Expand Up @@ -158,7 +162,7 @@
}
run_cmd 'add-missing', 'base'
in_test_app_dir {
expect(YAML.load_file('config/locales/en.yml')['en']['used_but_missing']['key']).to eq I18n.t('i18n_tasks.common.key')
expect(YAML.load_file('config/locales/en.yml')['en']['used_but_missing']['key']).to eq 'Key'
expect(YAML.load_file('config/locales/en.yml')['en']['present_in_es_but_not_en']['a']).to eq 'ES_TEXT'
}
end
Expand Down
39 changes: 39 additions & 0 deletions spec/relative_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,44 @@
end
end
end

context 'relative key in mailer' do
it 'works' do
key = scanner.absolutize_key(
'.subject',
'app/mailers/user_mailer.rb',
%w(app/mailers),
'welcome'
)

expect(key).to eq('user_mailer.welcome.subject')
end

context 'multiple words in mailer name' do
it 'works' do
key = scanner.absolutize_key(
'.subject',
'app/mailers/admin_user_mailer.rb',
%w(app/mailers),
'welcome'
)

expect(key).to eq('admin_user_mailer.welcome.subject')
end
end

context 'nested in module' do
it 'works' do
key = scanner.absolutize_key(
'.subject',
'app/mailers/nested/user_mailer.rb',
%w(app/mailers),
'welcome'
)

expect(key).to eq('nested.user_mailer.welcome.subject')
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/support/capture_std.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8
require 'active_support/core_ext/kernel/reporting'
module CaptureStd
def capture_stderr
err, $stderr = $stderr, StringIO.new
Expand Down
3 changes: 2 additions & 1 deletion templates/config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ search:

## Root directories for relative keys resolution.
# relative_roots:
# - app/views
# - app/controllers
# - app/helpers
# - app/mailers
# - app/presenters
# - app/views

## Files or `File.fnmatch` patterns to exclude from search. Some files are always excluded regardless of this setting:
## %w(*.jpg *.png *.gif *.svg *.ico *.eot *.otf *.ttf *.woff *.woff2 *.pdf *.css *.sass *.scss *.less *.yml *.json)
Expand Down

0 comments on commit 80bfe1a

Please sign in to comment.