Class: Logaling::Command::Application

Inherits:
Thor
  • Object
show all
Defined in:
lib/logaling/command/application.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Application

Returns a new instance of Application.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/logaling/command/application.rb', line 32

def initialize(*args)
  super
  @logaling_home = options["logaling-home"] ? options["logaling-home"] : LOGALING_HOME
  @repository = Logaling::Repository.new(@logaling_home)
  @config = Logaling::Config.load(@repository.config_path)

  @dotfile_path = options["logaling-config"] ? options["logaling-config"] : Logaling::Project.find_dotfile
  @project_config_path = File.join(@dotfile_path, 'config')
  @config.load(@project_config_path)
rescue Logaling::ProjectNotFound => e
  @project_config_path = nil
ensure
  @config.merge!(options)
end

Instance Method Details

#add(source_term, target_term, note = '') ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/logaling/command/application.rb', line 190

def add(source_term, target_term, note='')
  required_options = {
    "glossary" => "input glossary name '-g <glossary name>'",
    "source-language" => "input source-language code '-S <source-language code>'",
    "target-language" => "input target-language code '-T <target-language code>'"
  }
  @config.check_required_option(required_options)
  check_logaling_home_exists
  project = @repository.find_project(@config.glossary)
  raise Logaling::ProjectNotFound unless project
  raise Logaling::ProjectNotFound if project.imported?
  glossary = project.glossary(@config.source_language, @config.target_language)

  glossary.add(source_term, target_term, note)
rescue Logaling::CommandFailed => e
  say e.message
rescue Logaling::TermError => e
  say "term '#{source_term}: #{target_term}' already exists in '#{@config.glossary}'"
rescue Logaling::GlossaryNotFound => e
  say "Try 'loga new or register' first."
rescue Logaling::ProjectNotFound
  say "glossary <#{@config.glossary}> not found."
  say "Try 'loga list' and confirm glossary name."
end

#config(key, value) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/logaling/command/application.rb', line 171

def config(key, value)
  if options["global"]
    unless File.exist?(@logaling_home)
      FileUtils.mkdir_p(@logaling_home) rescue raise Logaling::CommandFailed, "Input existing directory as logaling-home."
    end
    config_path = File.join(@logaling_home, "config")
  else
    raise Logaling::CommandFailed, "Can't found .logaling" unless @project_config_path
    config_path = @project_config_path
  end
  config = Logaling::Config.load(config_path)
  config.add(key, value)
  config.save(config_path)
  say "Successfully set config."
rescue Logaling::CommandFailed => e
  say e.message
end

#copy(project_name, source_language, target_language, new_project_name, new_source_language, new_target_language) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/logaling/command/application.rb', line 373

def copy(project_name, source_language, target_language, new_project_name, new_source_language, new_target_language)
  check_logaling_home_exists

  src_glossary = @repository.find_glossary(project_name, source_language, target_language)
  unless src_glossary
    raise Logaling::GlossaryNotFound, "Can't found #{project_name}.#{source_language}.#{target_language}"
  end

  dest_project = @repository.create_personal_project(new_project_name, new_source_language, new_target_language)
  dest_glossary = dest_project.glossary(new_source_language, new_target_language)

  dest_glossary.merge!(src_glossary)
rescue Logaling::CommandFailed, Logaling::GlossaryAlreadyRegistered, Logaling::GlossaryNotFound => e
  say e.message
end

#delete(source_term, target_term = nil) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/logaling/command/application.rb', line 217

def delete(source_term, target_term=nil)
  required_options = {
    "glossary" => "input glossary name '-g <glossary name>'",
    "source-language" => "input source-language code '-S <source-language code>'",
    "target-language" => "input target-language code '-T <target-language code>'"
  }
  @config.check_required_option(required_options)
  check_logaling_home_exists
  project = @repository.find_project(@config.glossary)
  raise Logaling::ProjectNotFound unless project
  glossary = project.glossary(@config.source_language, @config.target_language)

  if target_term
    glossary.delete(source_term, target_term)
  else
    glossary.delete_all(source_term, options["force"])
  end
rescue Logaling::CommandFailed, Logaling::TermError => e
  say e.message
rescue Logaling::GlossaryNotFound => e
  say "Try 'loga new or register' first."
rescue Logaling::ProjectNotFound
  say "glossary <#{@config.glossary}> not found."
  say "Try 'loga list' and confirm glossary name."
end

#import(external_glossary = nil, *args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/logaling/command/application.rb', line 103

def import(external_glossary=nil, *args)
  require "logaling/external_glossary"
  Logaling::ExternalGlossary.load
  if options["list"]
    Logaling::ExternalGlossary.list.each {|glossary_source| say "#{glossary_source.name.bright} : #{glossary_source.description} (#{glossary_source.url})" }
  else
    case external_glossary
    when 'tmx'
      check_import_parameter(args)
      url = args[1]
      if url && !URI.parse(url).host
        url = File.expand_path(url)
      end
      glossary = Logaling::Glossary.new(args[0], args[2], args[3])
      @repository.import_tmx(Logaling::ExternalGlossary.get(external_glossary), glossary, url)
    else
      @repository.import(Logaling::ExternalGlossary.get(external_glossary))
    end
  end
rescue Logaling::CommandFailed => e
  say e.message
rescue Logaling::ExternalGlossaryNotFound
  say "'#{external_glossary}' can't find in import list."
  say "Try 'loga import --list' and confirm import list."
rescue Logaling::GlossaryNotFound => e
  say e.message
end

#indexObject



160
161
162
163
164
165
166
167
# File 'lib/logaling/command/application.rb', line 160

def index
  check_logaling_home_exists

  @repository.index
  say 'Complete index.'
rescue Logaling::CommandFailed => e
  say e.message
end

#listObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/logaling/command/application.rb', line 351

def list
  check_logaling_home_exists
  @repository.index
  projects = @repository.projects
  unless projects.empty?
    run_pager
    # 用語集の一覧といいつつプロジェクトの一覧を出していて、
    # かつ個人用のプロジェクトと .logaling によるプロジェクトで
    # プロジェクトとして表現しているスコープが異なっているために、
    # 重複した名前のプロジェクトが表示されるケースが存在する
    #TODO 表示する情報の単位を整理後に見直す
    projects.map(&:name).uniq.each do |project_name|
      printf("  %s\n", project_name)
    end
  else
    "There is no registered glossary."
  end
rescue Logaling::CommandFailed, Logaling::GlossaryDBNotFound => e
  say e.message
end

#lookup(source_term) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/logaling/command/application.rb', line 274

def lookup(source_term)
  check_logaling_home_exists
  if @config.glossary
    project = @repository.find_project(@config.glossary)
    raise Logaling::ProjectNotFound unless project
    glossary = project.glossary(@config.source_language, @config.target_language)
  else
    glossary = nil
  end
  terms = @repository.lookup(source_term, glossary, options)
  unless terms.empty?
    run_pager
    terms.each_with_index do |term, i|
      case options["output"]
      when "terminal"
        term_renderer = Logaling::Command::Renderers::TermDefaultRenderer.new(term, @repository, @config, options)
        term_renderer.set_max_source_term_width(terms)
        term_renderer.render($stdout)
      when "csv"
        term_renderer = Logaling::Command::Renderers::TermCsvRenderer.new(term, @repository, @config, options)
        term_renderer.render($stdout)
      when "json"
        term_renderer = Logaling::Command::Renderers::TermJsonRenderer.new(term, @repository, @config, options)
        term_renderer.index = i
        term_renderer.last_index = terms.length
        term_renderer.render($stdout)
      end
    end
  else
    "source-term <#{source_term}> not found"
  end
rescue Logaling::ProjectNotFound
  say "glossary <#{@config.glossary}> not found."
  say "Try 'loga list' and confirm glossary name."
rescue Logaling::CommandFailed, Logaling::TermError => e
  say e.message
end

#new(project_name, source_language, target_language = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/logaling/command/application.rb', line 71

def new(project_name, source_language, target_language=nil)
  unless options['personal']
    unless File.exist?(logaling_config_path)
      FileUtils.mkdir_p(File.join(logaling_config_path, "glossary"))

      config = Logaling::Config.new("glossary" => project_name, "source-language" => source_language)
      config.merge!("target-language" => target_language) if target_language
      config.save(File.join(logaling_config_path, "config"))

      unless options["no-register"]
        @dotfile_path = options["logaling-config"] || Logaling::Project.find_dotfile
        @project_config_path = File.join(@dotfile_path, 'config')
        @config.load(@project_config_path)
        @repository.register(@dotfile_path, @config.glossary)
      end
      say "Successfully created #{logaling_config_path}"
    else
      say "#{logaling_config_path} already exists."
    end
  else
    raise Logaling::CommandFailed, "[TARGET-LANGUAGE] is required when you use '--personal'" unless target_language
    personal_project = @repository.create_personal_project(project_name, source_language, target_language)
    say "Successfully created #{personal_project.absolute_path}"
  end
rescue Logaling::CommandFailed => e
  say e.message
rescue Logaling::GlossaryAlreadyRegistered => e
  say e.message
end

#registerObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/logaling/command/application.rb', line 132

def register
  raise Logaling::CommandFailed, "Can't use '-g <glossary>' option." if options["glossary"]
  @config.check_required_option("glossary" => "Do 'loga register' at project directory.")
  raise Logaling::CommandFailed, "Try 'loga new' first." unless File.exist?(@dotfile_path)

  @repository.register(@dotfile_path, @config.glossary)
  say "#{@config.glossary} is now registered to logaling."
rescue Logaling::CommandFailed => e
  say e.message
rescue Logaling::GlossaryAlreadyRegistered => e
  say "#{@config.glossary} is already registered."
end

#showObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/logaling/command/application.rb', line 320

def show
  required_options = {
    "glossary" => "input glossary name '-g <glossary name>'",
    "source-language" => "input source-language code '-S <source-language code>'",
    "target-language" => "input target-language code '-T <target-language code>'"
  }
  @config.check_required_option(required_options)
  check_logaling_home_exists
  glossary = @repository.find_glossary(@config.glossary, @config.source_language, @config.target_language)
  raise Logaling::GlossaryNotFound unless glossary
  terms = glossary.terms(options["annotation"])
  unless terms.empty?
    run_pager
    terms.each do |term|
      term_renderer = Logaling::Command::Renderers::TermDefaultRenderer.new(term, @repository, @config, options)
      term_renderer.set_max_source_term_width(terms)
      term_renderer.hide_glossary_name
      term_renderer.render($stdout)
    end
  else
    "glossary <#{@config.glossary}> not found"
  end
rescue Logaling::CommandFailed, Logaling::GlossaryDBNotFound => e
  say e.message
rescue Logaling::GlossaryNotFound
  say "glossary <#{@config.glossary}> not found."
  say "Try 'loga list' and confirm glossary name."
end

#unregisterObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/logaling/command/application.rb', line 146

def unregister
  raise Logaling::CommandFailed, "Can't use '-g <glossary>' option." if options["glossary"]
  @config.check_required_option("glossary" => "Do 'loga unregister' at project directory.")

  project = @repository.find_project(@config.glossary)
  @repository.unregister(project)
  say "#{@config.glossary} is now unregistered."
rescue Logaling::CommandFailed => e
  say e.message
rescue Logaling::ProjectNotFound => e
  say "#{@config.glossary} is not yet registered."
end

#update(source_term, target_term, new_target_term, note = '') ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/logaling/command/application.rb', line 244

def update(source_term, target_term, new_target_term, note='')
  required_options = {
    "glossary" => "input glossary name '-g <glossary name>'",
    "source-language" => "input source-language code '-S <source-language code>'",
    "target-language" => "input target-language code '-T <target-language code>'"
  }
  @config.check_required_option(required_options)
  check_logaling_home_exists
  project = @repository.find_project(@config.glossary)
  raise Logaling::ProjectNotFound unless project
  glossary = project.glossary(@config.source_language, @config.target_language)

  glossary.update(source_term, target_term, new_target_term, note)
rescue Logaling::CommandFailed => e
  say e.message
rescue Logaling::TermError => e
  say "term '#{source_term}: #{new_target_term}' already exists in '#{@config.glossary}'"
rescue Logaling::GlossaryNotFound => e
  say "Try 'loga new or register' first."
rescue Logaling::ProjectNotFound
  say "glossary <#{@config.glossary}> not found."
  say "Try 'loga list' and confirm glossary name."
end

#versionObject



313
314
315
# File 'lib/logaling/command/application.rb', line 313

def version
  say "logaling-command version #{Logaling::Command::VERSION}"
end