Class: Guard::I18njs

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/i18n-js.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ I18njs

Returns a new instance of I18njs.



13
14
15
16
17
# File 'lib/guard/i18n-js.rb', line 13

def initialize(options = {})
  @config_file = options.delete(:config_file)
  @require_file = options.delete(:require_file)
  super
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



11
12
13
# File 'lib/guard/i18n-js.rb', line 11

def config_file
  @config_file
end

#current_threadObject (readonly)

Returns the value of attribute current_thread.



11
12
13
# File 'lib/guard/i18n-js.rb', line 11

def current_thread
  @current_thread
end

#require_fileObject (readonly)

Returns the value of attribute require_file.



11
12
13
# File 'lib/guard/i18n-js.rb', line 11

def require_file
  @require_file
end

Instance Method Details

#captureObject



70
71
72
73
74
75
76
77
78
# File 'lib/guard/i18n-js.rb', line 70

def capture
  original = $stdout
  $stdout = StringIO.new
  yield
rescue StandardError
  # noop
ensure
  $stdout = original
end

#error(message) ⇒ Object



87
88
89
# File 'lib/guard/i18n-js.rb', line 87

def error(message)
  ::Guard::UI.error "[i18n-js] #{message}"
end

#export_files(changes = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/guard/i18n-js.rb', line 47

def export_files(changes = nil)
  return unless validate_file(:config_file, config_file)
  return unless validate_file(:require_file, require_file)

  current_thread&.exit

  info("Changes detected: #{changes.join(', ')}") if changes

  @current_thread = Thread.new do
    capture do
      system "i18n",
             "export",
             "--config",
             config_file.to_s,
             "--require",
             require_file.to_s,
             "--quiet"
    end
  end

  current_thread.join
end

#info(message) ⇒ Object



91
92
93
# File 'lib/guard/i18n-js.rb', line 91

def info(message)
  ::Guard::UI.info "[i18n-js] #{message}"
end

#reloadObject



27
28
29
# File 'lib/guard/i18n-js.rb', line 27

def reload
  export_files
end

#run_allObject



31
32
33
# File 'lib/guard/i18n-js.rb', line 31

def run_all
  export_files
end

#run_on_additions(paths) ⇒ Object



35
36
37
# File 'lib/guard/i18n-js.rb', line 35

def run_on_additions(paths)
  export_files(paths)
end

#run_on_modifications(paths) ⇒ Object



39
40
41
# File 'lib/guard/i18n-js.rb', line 39

def run_on_modifications(paths)
  export_files(paths)
end

#run_on_removals(paths) ⇒ Object



43
44
45
# File 'lib/guard/i18n-js.rb', line 43

def run_on_removals(paths)
  export_files(paths)
end

#startObject



19
20
21
# File 'lib/guard/i18n-js.rb', line 19

def start
  export_files
end

#stopObject



23
24
25
# File 'lib/guard/i18n-js.rb', line 23

def stop
  current_thread&.exit
end

#validate_file(key, file) ⇒ Object



80
81
82
83
84
85
# File 'lib/guard/i18n-js.rb', line 80

def validate_file(key, file)
  return true if file && File.file?(file)

  error("#{key.inspect} must be a file")
  false
end