Class: SlimKeyfy::Console::Commandline

Inherits:
Object
  • Object
show all
Defined in:
lib/slimkeyfy/console/command_line.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Commandline

Returns a new instance of Commandline.



4
5
6
7
8
# File 'lib/slimkeyfy/console/command_line.rb', line 4

def initialize(args)
  @options = { translator: {} }
  @args = args
  OptionParser.new(&method(:opt_scan)).parse!(@args)
end

Instance Method Details

#directory_translate(input) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/slimkeyfy/console/command_line.rb', line 76

def directory_translate(input)
  rec = @options.fetch(:recursive, false)
  SlimKeyfy::Slimutils::MFileUtils.walk(input, rec).each do |rec_input|
    if File.file?(rec_input) and SlimKeyfy::Slimutils::MFileUtils.is_valid_extension?(rec_input)
      @options[:input] = rec_input
      translate
    end
  end
end

#opt_scan(opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/slimkeyfy/console/command_line.rb', line 15

def opt_scan(opts)
  opts.banner = "
#{"Usage".white}: slimkeyfy #{"INPUT_FILENAME_OR_DIRECTORY".green} #{"LOCALE".red} #{"[YAML_FILE]".yellow} [Options]
e.g. slimkeyfy #{"app/views/users/".green} #{"en".red} #{"phrase/locales/en.yml".yellow} [Options]

#{"Description".white}: Extract plain Strings from .slim views and Rails controllers to
replace them with I18n's t() method. Keys with it's translations will be streamed
to a YAML file.

the following options are available:

"
  opts.on_tail('-t', '--translator-api-key [API_KEY]', 'API key for Yandex Translator') do |value|
    @options[:translator][:api] = value || ENV['YANDEX_TRANSLATOR_API']
  end

  #
  opts.on_tail('-l', '--keys-from-locale LOCALE', 'translate keys from locale') do |from_locale|
    @options[:translator][:from_locale] = from_locale
  end

  opts.on_tail('-h', '--help', 'Show this message') do
    puts opts
    exit
  end
  opts.on_tail('-v', '--version', 'Show current version') do
    puts "0.1"
    exit
  end
  opts.on_tail('-R', '--recursive', 'If a directory is given all subdirectories will be walked either.
                                    Without -r and a directory just the files within the first level are walked.
                                    ') do
    @options[:recursive] = true
  end
  opts.on_tail('-b', '--no-backup', 'No Backups - for safety reasons - are created. For minimum safety we
                                    recommend a version control like git. Backups will still be created for
                                    comparison but deleted right after you agree to the changes.
                              ') do
    @options[:no_backup] = true
  end
end

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/slimkeyfy/console/command_line.rb', line 57

def run
  @options[:input] = input = @args.shift
  @options[:locale] = locale = @args.shift
  @options[:yaml_output] = @args.shift

  SlimKeyfy::Slimutils::TranslationKeyGenerator.translator_options = @options[:translator]

  wrong_usage if (input.nil? or locale.nil?)

  if File.directory?(input)
    directory_translate(input)
  elsif File.file?(input)
    translate
  else
    raise "Please provide a file or directory!"
    exit
  end
end

#translateObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/slimkeyfy/console/command_line.rb', line 86

def translate
  unless SlimKeyfy::Slimutils::MFileUtils.is_valid_extension?(@options[:input])
    puts "invalid extension!"
    return
  end
  @options[:ext] = SlimKeyfy::Slimutils::MFileUtils.file_extension(@options[:input])

  puts "file=#{@options[:input]}"
  translate_slim = SlimKeyfy::Console::Translate.new(@options)
  begin
    translate_slim.stream_mode
  rescue Interrupt
    SlimKeyfy::Slimutils::MFileUtils.restore(translate_slim.bak_path, translate_slim.original_file_path)
  end
end

#wrong_usageObject



10
11
12
13
# File 'lib/slimkeyfy/console/command_line.rb', line 10

def wrong_usage
  puts "Please provide a path to .slims and a locale name. See -h for more information."
  exit
end