Class: Command::Diff
Instance Attribute Summary
Attributes inherited from CommandBase
#stream_io
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from CommandBase
#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids
Constructor Details
#initialize ⇒ Diff
Returns a new instance of Diff.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/command/diff.rb', line 22
def initialize
super("[<target>] [options]")
@opt.separator "\n \u30FB\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u66F4\u65B0\u524D\u5F8C\u306E\u5909\u66F4\u70B9\u306E\u5DEE\u5206\u3092\u8868\u793A\u3057\u307E\u3059\u3002\n \u30FB\u5BFE\u8C61\u5C0F\u8AAC\u3092\u6307\u5B9A\u3057\u306A\u304B\u3063\u305F\u5834\u5408\u306F\u76F4\u524D\u306B\u66F4\u65B0\u3057\u305F\u5C0F\u8AAC\u306E\u5DEE\u5206\u3092\u8868\u793A\u3057\u307E\u3059\u3002\n \u30FB\u3082\u3057\u81EA\u5206\u306E\u597D\u304D\u306A\u5DEE\u5206\u8868\u793A\u30D7\u30ED\u30B0\u30E9\u30E0\u3092\u4F7F\u3044\u305F\u3044\u5834\u5408\u3001difftool\u3092\u8A2D\u5B9A\u3057\u3066\u5909\u66F4\u3059\u308B\u3053\u3068\u304C\u51FA\u6765\u307E\u3059(\u4E0B\u8A18\u53C2\u7167)\n\n Examples:\nnarou diff # \u76F4\u524D\u306B\u66F4\u65B0\u3057\u305F\u5C0F\u8AAC\u306E\u5DEE\u5206\u3092\u8868\u793A\nnarou diff 6\nnarou diff 6 -n 2 # \u6700\u65B0\u304B\u30892\u756A\u76EE\u306E\u5DEE\u5206\u3068\u306E\u6BD4\u8F03\nnarou diff 6 -2 # -n 2 \u306E\u7701\u7565\u3057\u305F\u8A18\u8FF0\u65B9\u6CD5\nnarou diff 6 [email protected] # \u5DEE\u5206\u3092\u76F4\u63A5\u6307\u5B9A\nnarou diff 6 -l # \u904E\u53BB\u306B\u3069\u306E\u8A71\u6570\u306E\u5DEE\u5206\u304C\u3042\u308B\u306E\u304B\u3092\u78BA\u8A8D\n\n# \u81EA\u5206\u306E\u597D\u304D\u306A\u5DEE\u5206\u8868\u793A\u30D7\u30ED\u30B0\u30E9\u30E0\u3092\u4F7F\u3046\u5834\u5408\nnarou s difftool=\"C:\\\\Program Files\\\\WinMerge\\\\WinMergeU.exe\"\nnarou s difftool=colordiff # \u30B3\u30DE\u30F3\u30C9\u30E9\u30A4\u30F3\u30C4\u30FC\u30EB\u3092\u6307\u5B9A\u3057\u305F\u308A\n# Narou.rb\u30AA\u30EA\u30B8\u30CA\u30EB\u306E\u5DEE\u5206\u8868\u793A\u306B\u623B\u3059\u5834\u5408\u306F\u8A2D\u5B9A\u3092\u524A\u9664\u3059\u308B\nnarou s difftool=\n\n# difftool\u306B\u6E21\u3059\u5F15\u6570(\u6307\u5B9A\u3057\u306A\u3051\u308C\u3070\u5358\u7D14\u306B\u65B0\u65E7\u30D5\u30A1\u30A4\u30EB\u3092\u5F15\u6570\u306B\u547C\u3073\u51FA\u3059)\n# \u7279\u6B8A\u306A\u5909\u6570 %NEW : \u6700\u65B0\u30C7\u30FC\u30BF\u306E\u5DEE\u5206\u7528\u30D5\u30A1\u30A4\u30EB\u30D1\u30B9\n# %OLD : \u53E4\u3044\u65B9\u306E\u5DEE\u5206\u7528\u30D5\u30A1\u30A4\u30EB\u30D1\u30B9\nnarou s difftool.arg='-e -x -ub -dl \"OLD\" -dr \"NEW\" %OLD %NEW'\nnarou s difftool.arg=\"-u %OLD %NEW\"\n\n Options:\n EOS\n\n @opt.on(\"-n NUM\", \"--number\", \"\u6BD4\u8F03\u3059\u308B\u5DEE\u5206\u3092\u9061\u3063\u3066\u6307\u5B9A\u3059\u308B\u3002\u6700\u65B0\u306E\u30A2\u30C3\u30D7\u30C7\u30FC\u30C8\u3068\u76F4\u524D\u306E\u30C7\u30FC\u30BF\u3092\u6BD4\u8F03\u3059\u308B\u306A\u3089-n 1\u30012\u500B\u524D\u306E\u30A2\u30C3\u30D7\u30C7\u30FC\u30C8\u306A\u3089-n 2\u3002(\u30C7\u30D5\u30A9\u30EB\u30C8\u306F-n 1)\", Integer) { |number|\n @options[\"number\"] = number if number > 1\n }\n @opt.on(\"-l\", \"--list\", \"\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u5DEE\u5206\u4E00\u89A7\u3092\u8868\u793A\u3059\u308B\") {\n @options[\"list\"] = true\n }\n @opt.on(\"-c\", \"--clean\", \"\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u5DEE\u5206\u30C7\u30FC\u30BF\u3092\u5168\u3066\u524A\u9664\u3059\u308B\") {\n @options[\"clean\"] = true\n }\n @opt.on(\"--all-clean\", \"\u51CD\u7D50\u6E08\u3092\u9664\u304F\u5168\u5C0F\u8AAC\u306E\u5DEE\u5206\u30C7\u30FC\u30BF\u3092\u524A\u9664\u3059\u308B\") {\n @options[\"all-clean\"] = true\n }\n @opt.on(\"--no-tool\", \"\u5916\u90E8\u5DEE\u5206\u30C4\u30FC\u30EB\u3092\u4F7F\u7528\u3057\u306A\u3044\") {\n @options[\"no-tool\"] = true\n }\nend\n"
|
Class Method Details
.oneline_help ⇒ Object
18
19
20
|
# File 'lib/command/diff.rb', line 18
def self.oneline_help
"更新された小説の差分を表示します"
end
|
Instance Method Details
#clean_all_diff ⇒ Object
295
296
297
298
299
300
301
302
303
|
# File 'lib/command/diff.rb', line 295
def clean_all_diff
Database.instance.each do |id, data|
next if Narou.novel_frozen?(id)
cache_root_dir = Downloader.get_cache_root_dir(id)
next unless File.exist?(cache_root_dir)
FileUtils.remove_entry_secure(cache_root_dir)
stream_io.puts "#{data["title"]} の差分を削除しました"
end
end
|
#clean_diff(id) ⇒ Object
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/command/diff.rb', line 137
def clean_diff(id)
cache_root_dir = Downloader.get_cache_root_dir(id)
stream_io.print @novel_data["title"] + " の"
unless File.exist?(cache_root_dir)
stream_io.puts "差分はひとつもありません"
return
end
FileUtils.remove_entry_secure(cache_root_dir)
stream_io.puts "差分を削除しました"
end
|
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/command/diff.rb', line 169
def create_difftool_command_string(temp_old_path, temp_new_path)
diff_arg = Inventory.load("global_setting", :global)["difftool.arg"]
diff_cmd = %!"#{@difftool}" !
if diff_arg
diff_arg.gsub!("%OLD", temp_old_path)
diff_arg.gsub!("%NEW", temp_new_path)
diff_cmd += diff_arg
else
diff_cmd += [temp_old_path, temp_new_path].join(" ")
end
diff_cmd
end
|
#create_temp_by_sections(temp_prefix, sections, novel_info) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/command/diff.rb', line 226
def create_temp_by_sections(temp_prefix, sections, novel_info)
html = HTML.new
html.strip_decoration_tag = true
sections.each do |section|
element = section["element"]
data_type = element.delete("data_type") || "text"
element.each do |text_type, elm_text|
if data_type == "html"
html.string = elm_text
element[text_type] = html.to_aozora
end
end
end
temp = Tempfile.open([temp_prefix, ".txt"])
temp.write(Template.get("diff.txt", binding, 1.0))
temp.close
temp
end
|
#create_temp_files(id) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/command/diff.rb', line 187
def create_temp_files(id)
if @options["view_diff_version"]
cache_root_dir = Downloader.get_cache_root_dir(id)
if cache_root_dir
cache_dir = File.join(cache_root_dir, @options["view_diff_version"])
cache_dir = nil unless File.exist?(cache_dir)
end
else
nth = @options["number"]
list = get_sorted_cache_list(id)
cache_dir = list ? list[nth - 1] : nil
end
unless cache_dir
stream_io.puts "#{@novel_data["title"]} の差分データがありません"
return nil
end
cache_section_list = Dir.glob("#{cache_dir}/*.yaml").sort_by { |path|
File.basename(path, ".yaml").split(" ", 2)[0].to_i
}
if cache_section_list.length == 0
stream_io.puts "#{@novel_data["title"]} は最新話のみのアップデートのようです"
return nil
end
novel_dir = Downloader.get_novel_section_save_dir(Downloader.get_novel_data_dir_by_target(id))
latest_novel_sections = []
cache_sections = []
cache_section_list.each do |path|
match_latest_path = File.join(novel_dir, File.basename(path))
latest_novel_sections << YAML.unsafe_load_file(match_latest_path) if File.exist?(match_latest_path)
cache_sections << YAML.unsafe_load_file(path)
end
novel_info = Database.instance[id]
temp_old = create_temp_by_sections("old", cache_sections, novel_info)
temp_new = create_temp_by_sections("new", latest_novel_sections, novel_info)
[temp_old, temp_new]
end
|
#display_diff_list(id) ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/command/diff.rb', line 276
def display_diff_list(id)
list = get_diff_list(id)
stream_io.print "#{list[:title]} の"
if list[:list].empty?
stream_io.puts "差分はひとつもありません"
return
end
stream_io.puts "差分一覧"
list[:list].each do |data|
stream_io.puts "<bold><yellow>#{data[:version_string]} -#{data[:number]}</yellow></bold>".termcolor
data[:objects].each do |object|
stream_io.puts " 第#{object[:index]}部分 #{object[:subtitle]}"
end
if data[:objects].empty?
stream_io.puts " (最新話のみのアップデート)"
end
end
end
|
#display_diff_on_oneself(id) ⇒ Object
308
309
310
311
312
313
314
315
|
# File 'lib/command/diff.rb', line 308
def display_diff_on_oneself(id)
require_relative "../diffviewer"
temp_paths = create_temp_files(id) or return
stream_io.puts "#{@novel_data["title"]} の差分を表示します"
stream_io.puts DiffViewer.new(*temp_paths).result
ensure
temp_paths.map(&:delete) if temp_paths
end
|
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/command/diff.rb', line 148
def exec_difftool(id)
temp_paths = create_temp_files(id) or return
diff_cmd = create_difftool_command_string(*temp_paths.map { |temp|
path = temp.path
if Helper.os_windows?
path = path.encode(Encoding::Windows_31J)
end
%!"#{path}"!
})
begin
res = Helper::AsyncCommand.exec(diff_cmd)
rescue Errno::ENOENT => e
error e.message
exit Narou::EXIT_ERROR_CODE
ensure
temp_paths.map(&:delete)
end
stream_io.puts res[0] unless res[0].empty?
error res[1] unless res[1].empty?
end
|
#execute(argv) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/command/diff.rb', line 81
def execute(argv)
disable_logging
short_number_option_parse(argv)
super
@options["number"] ||= 1
if argv.empty?
latest = Database.instance.sort_by("last_update").first
return unless latest
id = latest["id"]
@novel_data = latest
else
target = argv.shift
@novel_data = Downloader.get_data_by_target(target) || {}
id = @novel_data["id"]
end
unless id
error "#{target} は存在しません"
return
end
view_diff_version = argv.shift
if view_diff_version
if invalid_diff_version_string?(view_diff_version)
error "差分指定の書式が違います(正しい例:[email protected])"
return
end
@options["view_diff_version"] = view_diff_version
end
if @options["list"]
display_diff_list(id)
return
end
if @options["clean"]
clean_diff(id)
return
end
if @options["all-clean"]
clean_all_diff
return
end
@difftool = Inventory.load("global_setting", :global)["difftool"]
if @difftool.! || @options["no-tool"]
display_diff_on_oneself(id)
return
end
exec_difftool(id)
end
|
#get_diff_list(id) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/command/diff.rb', line 245
def get_diff_list(id)
@novel_data ||= Database.instance.get_data("id", id)
list = {
id: id,
title: @novel_data["title"],
list: []
}
cache_list = get_sorted_cache_list(id)
return list if cache_list.blank?
cache_list.each.with_index(1) do |cache_path, i|
objects = []
version_string = File.basename(cache_path)
data = {
number: i,
version_string: version_string,
time: version_string_to_time(version_string),
objects: objects
}
list[:list].push(data)
cache_section_list = Dir.glob(File.join(cache_path, "*.yaml"))
if cache_section_list.length > 0
cache_section_list.map { |section_path|
File.basename(section_path, ".yaml").split(" ", 2)
}.sort_by { |v| v[0].to_i }.each { |index, subtitle|
objects.push({ index: index, subtitle: subtitle })
}
end
end
list
end
|
#get_sorted_cache_list(id) ⇒ Object
182
183
184
185
|
# File 'lib/command/diff.rb', line 182
def get_sorted_cache_list(id)
list = Downloader.get_cache_list(id) or return nil
list.sort_by { |v| File.basename(v) }.reverse
end
|
#invalid_diff_version_string?(str) ⇒ Boolean
128
129
130
|
# File 'lib/command/diff.rb', line 128
def invalid_diff_version_string?(str)
str !~ /^\d{4}\.\d{2}\.\d{2}@\d{2}[;.]\d{2}[;.]\d{2}$/
end
|
#short_number_option_parse(argv) ⇒ Object
引数の中の -数字 オプション(-n 数字の省略形)を -n 数字 に変換する
71
72
73
74
75
76
77
78
79
|
# File 'lib/command/diff.rb', line 71
def short_number_option_parse(argv)
argv.map! { |arg|
if arg.to_s =~ /^-(\d+)$/
["-n", $1.to_s]
else
arg
end
}.flatten!
end
|
#version_string_to_time(string) ⇒ Object
132
133
134
135
|
# File 'lib/command/diff.rb', line 132
def version_string_to_time(string)
ymd, hms = string.split("@")
Helper.date_string_to_time("#{ymd} #{hms.tr(".", ":")}")
end
|