logaling-command 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ require 'thor'
4
4
  require "logaling/glossary"
5
5
 
6
6
  class Logaling::Command < Thor
7
- VERSION = "0.0.3"
7
+ VERSION = "0.0.4"
8
8
  LOGALING_CONFIG = '.logaling'
9
9
 
10
10
  map '-c' => :create,
@@ -13,7 +13,8 @@ class Logaling::Command < Thor
13
13
  '-u' => :update,
14
14
  '-l' => :lookup,
15
15
  '-n' => :new,
16
- '-L' => :link
16
+ '-r' => :register,
17
+ '-U' => :unregister
17
18
 
18
19
  class_option "glossary", type: :string, aliases: "-g"
19
20
  class_option "source-language", type: :string, aliases: "-S"
@@ -21,6 +22,7 @@ class Logaling::Command < Thor
21
22
  class_option "logaling-home", type: :string, required: false, aliases: "-h"
22
23
 
23
24
  desc 'new [PROJECT NAME] [SOURCE LANGUAGE] [TARGET LANGUAGE(optional)]', 'Create .logaling'
25
+ method_option "no-register", type: :boolean, default: false
24
26
  def new(project_name, source_language, target_language=nil)
25
27
  unless File.exist?(LOGALING_CONFIG)
26
28
  FileUtils.mkdir_p(File.join(LOGALING_CONFIG, "glossary"))
@@ -29,32 +31,49 @@ class Logaling::Command < Thor
29
31
  config.puts "--source-language #{source_language}"
30
32
  config.puts "--target-language #{target_language}" if target_language
31
33
  end
34
+ register unless options["no-register"]
32
35
  say "Successfully created #{LOGALING_CONFIG}"
33
36
  else
34
- say "#{LOGALING_CONFIG} is already exists."
37
+ say "#{LOGALING_CONFIG} already exists."
35
38
  end
36
39
  end
37
40
 
38
- desc 'link', 'Link .logaling'
39
- def link
41
+ desc 'register', 'Register .logaling'
42
+ def register
40
43
  logaling_path = find_dotfile
41
44
  if logaling_path
42
- logaling_projects_path = File.join(LOGALING_HOME, "projects")
43
45
  FileUtils.mkdir_p(logaling_projects_path) unless File.exist?(logaling_projects_path)
44
46
 
45
47
  config = load_config
46
48
  symlink_path = File.join(logaling_projects_path, config["glossary"])
47
49
  unless File.exists?(symlink_path)
48
50
  FileUtils.ln_s(logaling_path, symlink_path)
49
- say "Your project is now linked to #{symlink_path}."
51
+ say "Your project is now registered to #{symlink_path}."
50
52
  else
51
- say "#{options["glossary"]} is already linked."
53
+ say "#{options["glossary"]} is already registered."
52
54
  end
53
55
  else
54
56
  say "Try 'loga new' first."
55
57
  end
56
58
  end
57
59
 
60
+ desc 'unregister', 'Unregister .logaling'
61
+ def unregister
62
+ logaling_path = find_dotfile
63
+ if logaling_path
64
+ config = load_config
65
+ symlink_path = File.join(logaling_projects_path, config["glossary"])
66
+ if File.exists?(symlink_path)
67
+ FileUtils.remove_entry_secure(symlink_path, true)
68
+ say "Your project is now unregistered."
69
+ else
70
+ say ".logaling is not yet registered."
71
+ end
72
+ else
73
+ say ".logaling can't be found."
74
+ end
75
+ end
76
+
58
77
  desc 'create', 'Create glossary.'
59
78
  def create
60
79
  glossary.create
@@ -114,6 +133,10 @@ class Logaling::Command < Thor
114
133
  Logaling::Glossary.new(glossary, source_language, target_language)
115
134
  end
116
135
 
136
+ def logaling_projects_path
137
+ File.join(LOGALING_HOME, "projects")
138
+ end
139
+
117
140
  def error(msg)
118
141
  STDERR.puts(msg)
119
142
  exit 1
@@ -73,7 +73,7 @@ module Logaling
73
73
  check_glossary_exists
74
74
 
75
75
  glossarydb = Logaling::GlossaryDB.new
76
- glossarydb.open(File.join(LOGALING_HOME, "db"), "utf8") do |db|
76
+ glossarydb.open(logaling_db_home, "utf8") do |db|
77
77
  glossaries = db.lookup(source_term)
78
78
  glossaries.reject! do |term|
79
79
  term[:source_language] != @source_language || term[:target_language] != @target_language
@@ -98,10 +98,9 @@ module Logaling
98
98
 
99
99
  def index
100
100
  projects = Dir.glob(File.join(LOGALING_HOME, "projects", "*"))
101
- db_home = File.join(LOGALING_HOME, "db")
102
101
  glossarydb = Logaling::GlossaryDB.new
103
- glossarydb.open(db_home, "utf8") do |db|
104
- db.recreate_table(db_home)
102
+ glossarydb.open(logaling_db_home, "utf8") do |db|
103
+ db.recreate_table(logaling_db_home)
105
104
  projects.each do |project|
106
105
  db.load_glossaries(File.join(project, "glossary"))
107
106
  end
@@ -47,14 +47,6 @@ module Logaling
47
47
  @database.nil? or @database.closed?
48
48
  end
49
49
 
50
- def get_file_list(path, types)
51
- glob_list = []
52
- types.each do |type|
53
- glob_list << File.join(path, "*.#{type}")
54
- end
55
- Dir.glob(glob_list)
56
- end
57
-
58
50
  def load_glossary(file)
59
51
  case extname = File.extname(file)
60
52
  when ".tsv", ".csv"
@@ -153,5 +145,11 @@ module Logaling
153
145
  schema.remove_table("terms")
154
146
  end
155
147
  end
148
+
149
+ def get_file_list(path, types)
150
+ glob_list = types.map{|type| File.join(path, "*.#{type}") }
151
+ Dir.glob(glob_list)
152
+ end
153
+
156
154
  end
157
155
  end
@@ -4,27 +4,176 @@ require File.join(File.dirname(__FILE__), "..", "spec_helper")
4
4
  describe Logaling::Command do
5
5
  let(:base_options) { {"glossary"=>"spec", "source-language"=>"en", "target-language"=>"ja"} }
6
6
  let(:command) { Logaling::Command.new([], base_options) }
7
- let(:project) { "spec" }
8
- let(:glossary_path) { Logaling::Glossary.build_path(project, 'en', 'ja') }
7
+ let(:glossary_path) { Logaling::Glossary.build_path('spec', 'en', 'ja') }
8
+ let(:target_project_path) { File.join(LOGALING_HOME, "projects", "spec") }
9
9
 
10
- before do
11
- FileUtils.remove_entry_secure(File.join(LOGALING_HOME, 'projects', 'spec'), true)
12
- FileUtils.mkdir_p(File.dirname(glossary_path))
10
+ describe '#new' do
11
+ before do
12
+ FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true)
13
+ FileUtils.remove_file(target_project_path) if File.exist?(target_project_path)
14
+ @project_counts = Dir[File.join(LOGALING_HOME, "projects", "*")].size
15
+ end
16
+
17
+ context 'when .logaling already exists' do
18
+ before do
19
+ FileUtils.mkdir_p(Logaling::Command::LOGALING_CONFIG)
20
+ @stdout = capture(:stdout) { command.new('spec', 'en', 'ja') }
21
+ end
22
+
23
+ it 'print message \"<.logaling path> already exists.\"' do
24
+ @stdout.should == "#{Logaling::Command::LOGALING_CONFIG} already exists.\n"
25
+ end
26
+ end
27
+
28
+ context 'when .logaling does not exist' do
29
+ context 'and called with no special option' do
30
+ before do
31
+ command.new('spec', 'en', 'ja')
32
+ end
33
+
34
+ it 'should create .logaling' do
35
+ File.exist?(Logaling::Command::LOGALING_CONFIG).should be_true
36
+ end
37
+
38
+ it 'should register .logaling as project' do
39
+ File.exist?(target_project_path).should be_true
40
+ Dir[File.join(LOGALING_HOME, "projects", "*")].size.should == @project_counts + 1
41
+ end
42
+ end
43
+
44
+ context "and called with '--no-register=true'" do
45
+ before do
46
+ command.options = base_options.merge("no-register" => true)
47
+ command.new('spec', 'en', 'ja')
48
+ end
49
+
50
+ it 'should create .logaling' do
51
+ File.exist?(Logaling::Command::LOGALING_CONFIG).should be_true
52
+ end
53
+
54
+ it 'should not register .logaling as project' do
55
+ File.exist?(target_project_path).should be_false
56
+ Dir[File.join(LOGALING_HOME, "projects", "*")].size.should == @project_counts
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#register' do
63
+ before do
64
+ FileUtils.remove_file(target_project_path) if File.exist?(target_project_path)
65
+ @project_counts = Dir[File.join(LOGALING_HOME, "projects", "*")].size
66
+ end
67
+
68
+ context "when can not find .logaling" do
69
+ before(:all) do
70
+ FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true)
71
+ @stdout = capture(:stdout) {command.register}
72
+ end
73
+
74
+ it 'register nothing' do
75
+ Dir[File.join(LOGALING_HOME, "projects", "*")].size.should == @project_counts
76
+ end
77
+
78
+ it "print message \"Try 'loga new' first.\"" do
79
+ @stdout.should == "Try 'loga new' first.\n"
80
+ end
81
+ end
82
+
83
+ context 'when find .logaling' do
84
+ before do
85
+ command.new('spec', 'en', 'ja')
86
+ command.register
87
+ end
88
+
89
+ it 'register .logaling as project' do
90
+ File.exist?(target_project_path).should be_true
91
+ Dir[File.join(LOGALING_HOME, "projects", "*")].size.should == @project_counts + 1
92
+ end
93
+
94
+ after do
95
+ FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '#unregister' do
101
+ before do
102
+ @project_counts = Dir[File.join(LOGALING_HOME, "projects", "*")].size
103
+ end
104
+
105
+ context "when can not find .logaling" do
106
+ before(:all) do
107
+ FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true)
108
+ @stdout = capture(:stdout) {command.unregister}
109
+ end
110
+
111
+ it 'unregister nothing' do
112
+ Dir[File.join(LOGALING_HOME, "projects", "*")].size.should == @project_counts
113
+ end
114
+
115
+ it "print message \".logaling can't be found.\"" do
116
+ @stdout.should == ".logaling can't be found.\n"
117
+ end
118
+ end
119
+
120
+ context 'when find .logaling' do
121
+ before do
122
+ command.new('spec', 'en', 'ja')
123
+ end
124
+
125
+ context 'and .logaling registered' do
126
+ before do
127
+ command.register
128
+ command.unregister
129
+ end
130
+
131
+ it 'unregister .logaling' do
132
+ File.exist?(target_project_path).should be_false
133
+ Dir[File.join(LOGALING_HOME, "projects", "*")].size.should == @project_counts
134
+ end
135
+ end
136
+
137
+ context "and .logaling is not registered" do
138
+ before do
139
+ @stdout = capture(:stdout) {command.unregister}
140
+ end
141
+
142
+ it "print message \".logaling is not yet registered.\"" do
143
+ @stdout.should == ".logaling is not yet registered.\n"
144
+ end
145
+ end
146
+ end
13
147
  end
14
148
 
15
149
  describe '#create' do
16
150
  before do
17
- FileUtils.touch(glossary_path)
151
+ FileUtils.remove_entry_secure(File.join(LOGALING_HOME, 'projects', 'spec'), true)
18
152
  end
19
153
 
20
154
  context 'with arguments show non-existent glossary' do
155
+ before do
156
+ command.new('spec', 'en', 'ja')
157
+ command.register
158
+ command.create
159
+ end
160
+
21
161
  it "glossary yaml should be newly created" do
22
162
  File.exists?(glossary_path).should be_true
23
163
  end
24
164
  end
165
+
166
+ after do
167
+ FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true)
168
+ end
25
169
  end
26
170
 
27
171
  describe '#add' do
172
+ before do
173
+ FileUtils.remove_entry_secure(File.join(LOGALING_HOME, 'projects', 'spec'), true)
174
+ FileUtils.mkdir_p(File.dirname(glossary_path))
175
+ end
176
+
28
177
  context 'with arguments have only bilingual pair' do
29
178
  before do
30
179
  command.add("spec", "テスト")
@@ -60,6 +209,8 @@ describe Logaling::Command do
60
209
 
61
210
  describe "#update" do
62
211
  before do
212
+ FileUtils.remove_entry_secure(File.join(LOGALING_HOME, 'projects', 'spec'), true)
213
+ FileUtils.mkdir_p(File.dirname(glossary_path))
63
214
  command.add("spec", "テスト", "備考")
64
215
  end
65
216
 
@@ -94,6 +245,8 @@ describe Logaling::Command do
94
245
 
95
246
  describe '#index' do
96
247
  before do
248
+ FileUtils.remove_entry_secure(File.join(LOGALING_HOME, 'projects', 'spec'), true)
249
+ FileUtils.mkdir_p(File.dirname(glossary_path))
97
250
  command.add("spec", "スペック", "備考")
98
251
  command.index
99
252
  end
@@ -113,8 +266,12 @@ describe Logaling::Command do
113
266
  describe '#lookup' do
114
267
  let(:base_options2) { {"glossary"=>"spec2", "source-language"=>"en", "target-language"=>"ja"} }
115
268
  let(:command2) { Logaling::Command.new([], base_options2) }
116
- let(:project2) { "spec2" }
117
- let(:glossary_path2) { Logaling::Glossary.build_path(project2, 'en', 'ja') }
269
+ let(:glossary_path2) { Logaling::Glossary.build_path('spec2', 'en', 'ja') }
270
+
271
+ before do
272
+ FileUtils.remove_entry_secure(File.join(LOGALING_HOME, 'projects', 'spec'), true)
273
+ FileUtils.mkdir_p(File.dirname(glossary_path))
274
+ end
118
275
 
119
276
  context 'with arguments exist term' do
120
277
  before do
@@ -12,34 +12,6 @@ describe Logaling::GlossaryDB do
12
12
  FileUtils.mkdir_p(File.dirname(glossary_path))
13
13
  end
14
14
 
15
- describe '#get_file_list' do
16
- context 'when specified file not exist' do
17
- subject { glossary_db.get_file_list(File.dirname(glossary_path), ['yml', 'csv', 'tsv']) }
18
- it 'should return empty array' do
19
- should == []
20
- end
21
- end
22
-
23
- context 'when specified file exists' do
24
- before do
25
- FileUtils.touch(glossary_path)
26
- FileUtils.touch(tsv_path)
27
- FileUtils.touch(csv_path)
28
- end
29
-
30
- subject { glossary_db.get_file_list(File.dirname(glossary_path), ['yml', 'csv', 'tsv']) }
31
- it 'should return file list' do
32
- should == [glossary_path, csv_path, tsv_path]
33
- end
34
-
35
- after do
36
- FileUtils.remove_entry_secure(glossary_path, true)
37
- FileUtils.remove_entry_secure(tsv_path, true)
38
- FileUtils.remove_entry_secure(csv_path, true)
39
- end
40
- end
41
- end
42
-
43
15
  describe '#load_glossary' do
44
16
  context 'with argument yml' do
45
17
  before do
@@ -191,7 +191,7 @@ lookup word : user
191
191
  subject { logaling_db.open(db_home, "utf8"){|db| logaling_db.lookup("spec")} }
192
192
 
193
193
  it 'glossaries should be indexed' do
194
- subject.should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"spec", :target_term=>"スペック", :note=>"備考"}]
194
+ should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"spec", :target_term=>"スペック", :note=>"備考"}]
195
195
  end
196
196
 
197
197
  after do
@@ -210,7 +210,7 @@ lookup word : user
210
210
  subject { logaling_db.open(db_home, "utf8"){|db| logaling_db.lookup("user")} }
211
211
 
212
212
  it 'glossaries should be indexed' do
213
- subject.should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"user", :target_term=>"ユーザ", :note=>nil}]
213
+ should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"user", :target_term=>"ユーザ", :note=>nil}]
214
214
  end
215
215
 
216
216
  after do
@@ -229,7 +229,7 @@ lookup word : user
229
229
  subject { logaling_db.open(db_home, "utf8"){|db| logaling_db.lookup("test")} }
230
230
 
231
231
  it 'glossaries should be indexed' do
232
- subject.should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"test", :target_term=>"テスト", :note=>nil}]
232
+ should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"test", :target_term=>"テスト", :note=>nil}]
233
233
  end
234
234
 
235
235
  after do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logaling-command
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,11 +13,11 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2011-11-22 00:00:00.000000000Z
16
+ date: 2011-11-28 00:00:00.000000000Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: thor
20
- requirement: &2154516840 !ruby/object:Gem::Requirement
20
+ requirement: &2152368760 !ruby/object:Gem::Requirement
21
21
  none: false
22
22
  requirements:
23
23
  - - ! '>='
@@ -25,10 +25,10 @@ dependencies:
25
25
  version: 0.14.6
26
26
  type: :runtime
27
27
  prerelease: false
28
- version_requirements: *2154516840
28
+ version_requirements: *2152368760
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
- requirement: &2154515940 !ruby/object:Gem::Requirement
31
+ requirement: &2152368000 !ruby/object:Gem::Requirement
32
32
  none: false
33
33
  requirements:
34
34
  - - ! '>='
@@ -36,10 +36,10 @@ dependencies:
36
36
  version: '1.0'
37
37
  type: :runtime
38
38
  prerelease: false
39
- version_requirements: *2154515940
39
+ version_requirements: *2152368000
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rake
42
- requirement: &2154515340 !ruby/object:Gem::Requirement
42
+ requirement: &2152367420 !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
45
45
  - - ! '>='
@@ -47,10 +47,10 @@ dependencies:
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
- version_requirements: *2154515340
50
+ version_requirements: *2152367420
51
51
  - !ruby/object:Gem::Dependency
52
52
  name: rspec
53
- requirement: &2154514760 !ruby/object:Gem::Requirement
53
+ requirement: &2152366640 !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
56
56
  - - ! '>='
@@ -58,7 +58,7 @@ dependencies:
58
58
  version: '0'
59
59
  type: :development
60
60
  prerelease: false
61
- version_requirements: *2154514760
61
+ version_requirements: *2152366640
62
62
  description: A command line interface for logaling.
63
63
  email:
64
64
  - koji.shimada@enishi-tech.com