rept 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/rept ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'rept'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'rept'
8
+ end
9
+
10
+ rept
data/bin/rept4diff ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'rept4diff'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'rept4diff'
8
+ end
9
+
10
+ rept4diff
data/lib/addText.rept ADDED
@@ -0,0 +1,9 @@
1
+ Args:
2
+ �t�@�C����
3
+ �V���{����
4
+ �R�����g
5
+ Index: reptname1.yaml
6
+ --># ReptName3
7
+ -->ReptName2: ""
8
+ -->
9
+ # @insert
data/lib/fileinsert.rb ADDED
@@ -0,0 +1,90 @@
1
+ #------------------------------------------------------------------
2
+ # �t�@�C���w��ʒu�ւ̑}�����s��
3
+ #
4
+ # ���s�R�[�h�͑}����t�@�C���ɍ��킹��
5
+ #------------------------------------------------------------------
6
+
7
+ require 'tempfile'
8
+
9
+ def file_insert(path, start_line, data)
10
+ # �e���v�t�@�C��
11
+ temp = Tempfile.new("temp", File.dirname(path))
12
+ temp.binmode
13
+
14
+ # ���͎���\r\n�̉��s�R�[�h�œ��͂���Ă���”\�������邽�߁A
15
+ # ��x���s�R�[�h��\n�ɓ���
16
+ data = data.gsub("\r\n", "\n")
17
+
18
+ File.open(path, "rb"){|file|
19
+ # �s���̉��s�R�[�h�𒲂ׁA�������ރf�[�^�̉��s�R�[�h�����킹��
20
+ line = file.gets
21
+ data = data.gsub("\n", "\r\n") if (line.index("\r\n"))
22
+ file.rewind
23
+
24
+ # temp�t�@�C����start_line�܂ŏ�������
25
+ start_line.times{
26
+ if line = file.gets
27
+ temp.write(line)
28
+ end
29
+ }
30
+
31
+ # �}���f�[�^��lj�
32
+ temp.write(data)
33
+
34
+ # �c�����������
35
+ while line = file.gets
36
+ temp.write(line)
37
+ end
38
+ }
39
+
40
+ # �����ň�x�N���[�Y���Ȃ��ƃR�s�[�Ɏ��s����H
41
+ # false���w�肷��Ƃ����ɍ폜���ꂸ��GC������ɍ폜�����
42
+ temp.close(false)
43
+
44
+ # ���t�@�C���ƃX���b�v
45
+ mode = File.stat(path).mode
46
+ File.rename(temp.path, path)
47
+ File.chmod(mode, path)
48
+ end
49
+
50
+ def file_deleteline(path, start_line, num=1)
51
+ temp = Tempfile.new("temp", File.dirname(path))
52
+ temp.binmode
53
+
54
+ File.open(path, "rb") {|file|
55
+ # temp�t�@�C����start_line�܂ŏ�������
56
+ start_line.times{
57
+ if line = file.gets
58
+ temp.write(line)
59
+ end
60
+ }
61
+
62
+ # �폜�������s�������X�L�b�v
63
+ num.times{ file.gets }
64
+
65
+ # �c�����������
66
+ while line = file.gets
67
+ temp.write(line)
68
+ end
69
+ }
70
+
71
+ # �����ň�x�N���[�Y���Ȃ��ƃR�s�[�Ɏ��s����H
72
+ # false���w�肷��Ƃ����ɍ폜���ꂸ��GC������ɍ폜�����
73
+ temp.close(false)
74
+
75
+ # ���t�@�C���ƃX���b�v
76
+ mode = File.stat(path).mode
77
+ File.rename(temp.path, path)
78
+ File.chmod(mode, path)
79
+ end
80
+
81
+ def searchLineNo(path, match)
82
+ File.open(path) do |file|
83
+ file.each_with_index do |line, no|
84
+ return no if (line.index(match))
85
+ end
86
+ end
87
+
88
+ -1
89
+ end
90
+
data/lib/init.yaml ADDED
@@ -0,0 +1,4 @@
1
+ # �o�[�W�����ԍ�(�S�c�[���ŋ��ʂɂ��Ă���)
2
+ version: "0.1.0"
3
+
4
+ # @insert
data/lib/lib.rb ADDED
@@ -0,0 +1,34 @@
1
+ # ���ʃ��C�u����
2
+
3
+ class String
4
+ def downcaseHead
5
+ self[0, 1].downcase + self[1..-1]
6
+ end
7
+
8
+ def joinUnderbar
9
+ (self[0, 1] + self[1..-1].gsub(/([A-Z])/){|str| "_" + str }).downcase
10
+ end
11
+ end
12
+
13
+ class Dir
14
+ def Dir.isEmpty?(path)
15
+ Dir.entries(path) == [".", ".."]
16
+ end
17
+ end
18
+
19
+ # �R�}���h�v�����v�g�̕\�L��啶���ɓ��ꂵ�āAPathname�I�u�W�F�N�g��Ԃ�
20
+ def upcase_Pathname(path)
21
+ Pathname.new(path[0, 1].upcase + path[1..-1])
22
+ end
23
+
24
+ # windows�‹���'c:'��'C:'���A�啶���������̖�肪�����Ă���肭�������΃p�X�w��
25
+ def relative_path(p)
26
+ if (Pathname.new(p).absolute?)
27
+ base = upcase_Pathname(Pathname.pwd.to_s)
28
+ path = upcase_Pathname(p)
29
+ path.relative_path_from(base).to_s
30
+ else
31
+ Pathname.new(p)
32
+ end
33
+ end
34
+
data/lib/rept.rb ADDED
@@ -0,0 +1,418 @@
1
+ #! ruby -Ks
2
+
3
+ require 'optparse'
4
+ require 'fileutils'
5
+ require File.join(File.dirname(__FILE__), 'fileinsert')
6
+ require File.join(File.dirname(__FILE__), 'yamltext')
7
+ require 'readline'
8
+ require File.join(File.dirname(__FILE__), 'lib')
9
+
10
+ # ���b�Z�[�W�ꗗ
11
+ $init = YamlText.new(File.join(File.dirname(__FILE__), 'init.yaml'))
12
+ $text = YamlText.new(File.join(File.dirname(__FILE__), 'rept.yaml'))
13
+
14
+ # �o�[�W�����ԍ�
15
+ Version = $init.get('version')
16
+
17
+ class Parser
18
+ attr_reader :args
19
+ attr_reader :creator
20
+
21
+ def initialize(file_name)
22
+ @file_name = file_name
23
+ @args = nil
24
+ @creator = []
25
+ end
26
+
27
+ def parse
28
+ page = :none
29
+
30
+ open(@file_name) do |f|
31
+ f.each do |line|
32
+ if (line =~ /^Args:/)
33
+ page = :args
34
+ elsif (line =~ /^Index:/)
35
+ page = :index
36
+ @creator << Creator.new(line.strip.chomp.gsub(/Index:\s*/, ""))
37
+ else
38
+ parse_page(page, line)
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def parse_page(page, line)
45
+ case page
46
+ when :args
47
+ @args = Args.new if (@args.nil?)
48
+ @args.add(line)
49
+ when :index
50
+ @creator.last.add(line)
51
+ end
52
+ end
53
+ end
54
+
55
+ class Args
56
+ def initialize
57
+ @args = []
58
+ end
59
+
60
+ def add(comment)
61
+ @args << comment.strip.chomp
62
+ end
63
+
64
+ def description
65
+ @args.join(' ')
66
+ end
67
+
68
+ def num
69
+ @args.size
70
+ end
71
+ end
72
+
73
+ class Creator
74
+ attr_reader :file_name
75
+ attr_reader :text
76
+
77
+ def initialize(file_name)
78
+ @file_name = file_name
79
+ @text = []
80
+ @inputs = nil
81
+
82
+ # ���̃t���O��ON�ɂ���ƁA�r���ŕs�K�؂ȏ������������Ƃ��ɗ�O�𓊂���B
83
+ # �܂����ۂɃt�@�C�����������}�����邱�Ƃ͍s��Ȃ�
84
+ @testMode = false
85
+ end
86
+
87
+ def add(line)
88
+ @text << line
89
+ end
90
+
91
+ def setInputs(inputs)
92
+ @inputs = inputs
93
+ @conv_file_name = convertString(@file_name)
94
+ end
95
+
96
+ def createTest
97
+ @testMode = true
98
+ success = true
99
+
100
+ if (insert?)
101
+ if !FileTest.exist?(@conv_file_name)
102
+ puts $text.get('file_insert_error', @conv_file_name)
103
+ success = false
104
+ else
105
+ if (!createInsert)
106
+ success = false
107
+ end
108
+ end
109
+ else
110
+ puts $text.get('file_create_test', @conv_file_name)
111
+
112
+ if FileTest.exist?(@conv_file_name)
113
+ puts $text.get('file_create_error', @conv_file_name)
114
+ success = false
115
+ end
116
+ end
117
+
118
+ success
119
+ end
120
+
121
+ def create
122
+ @testMode = false
123
+
124
+ if (insert?)
125
+ createInsert
126
+ else
127
+ createNewFile
128
+ end
129
+ end
130
+
131
+ def undo
132
+ if (insert?)
133
+ deleteInsert
134
+ else
135
+ deleteFile
136
+ end
137
+ end
138
+
139
+ def createInsert
140
+ success = true
141
+
142
+ if (@testMode)
143
+ puts $text.get('insert_test', @conv_file_name)
144
+ else
145
+ puts $text.get('insert', @conv_file_name)
146
+ end
147
+
148
+ mode = :none
149
+ markerText = nil
150
+ insertTexts = []
151
+
152
+ @text.each_with_index do |data, no|
153
+ if (data =~ /\A\s*\z/)
154
+ if (!insertFromMode(mode, markerText, insertTexts))
155
+ success = false
156
+ end
157
+ mode = :none
158
+ elsif (data =~ /^-->/)
159
+ insertTexts << convertString(data.gsub(/^-->/, ""))
160
+ mode = :insert
161
+ else
162
+ markerText = convertString(data)
163
+ mode = :marker
164
+ end
165
+ end
166
+
167
+ if (mode != :none)
168
+ if (!insertFromMode(mode, markerText, insertTexts))
169
+ success = false
170
+ end
171
+ end
172
+
173
+ success
174
+ end
175
+
176
+ def insertFromMode(mode, markerText, insertTexts)
177
+ success = true
178
+
179
+ begin
180
+ if (mode == :insert)
181
+ insertText(markerText, insertTexts, 1)
182
+ elsif (mode == :marker)
183
+ insertText(markerText, insertTexts, 0)
184
+ end
185
+ rescue
186
+ success = false
187
+ ensure
188
+ insertTexts.clear
189
+ markerText = nil
190
+ end
191
+
192
+ success
193
+ end
194
+
195
+ def insertText(markerText, insertTexts, lineNoOffset)
196
+ lineNo = searchLineNo(@conv_file_name, markerText)
197
+
198
+ if (lineNo != -1)
199
+ if (!@testMode)
200
+ insertTexts.size.times do |index|
201
+ puts $text.get('real_insert', '"' + insertTexts[index].chomp + '"', '"' + markerText.chomp + '"')
202
+ end
203
+ file_insert(@conv_file_name, lineNo + lineNoOffset, insertTexts.to_s)
204
+ end
205
+ else
206
+ puts $text.get('insert_not_found', '"' + insertTexts[0].chomp + '"', '"' + markerText.chomp + '"')
207
+ raise
208
+ end
209
+ end
210
+
211
+ def deleteInsert
212
+ success = true
213
+
214
+ puts $text.get('delete_insert', @conv_file_name)
215
+
216
+ mode = :none
217
+ markerText = nil
218
+ insertTexts = []
219
+
220
+ @text.each_with_index do |data, no|
221
+ if (data =~ /\A\s*\z/)
222
+ if (!deleteInsertFromMode(mode, markerText, insertTexts))
223
+ success = false
224
+ end
225
+ mode = :none
226
+ elsif (data =~ /^-->/)
227
+ insertTexts << convertString(data.gsub(/^-->/, ""))
228
+ mode = :insert
229
+ else
230
+ markerText = convertString(data)
231
+ mode = :marker
232
+ end
233
+ end
234
+
235
+ if (mode != :none)
236
+ if (!deleteInsertFromMode(mode, markerText, insertTexts))
237
+ success = false
238
+ end
239
+ end
240
+
241
+ success
242
+ end
243
+
244
+ def deleteInsertFromMode(mode, markerText, insertTexts)
245
+ # �}���폜
246
+ if (mode == :insert)
247
+ deleteInsertText(markerText, insertTexts, 1)
248
+ elsif (mode == :marker)
249
+ deleteInsertText(markerText, insertTexts, -insertTexts.size)
250
+ end
251
+
252
+ # �㏈��
253
+ insertTexts.clear
254
+ markerText = nil
255
+
256
+ # ���̏��A���s�͖���
257
+ true
258
+ end
259
+
260
+ def deleteInsertText(markerText, insertTexts, lineNoOffset)
261
+ lineNo = searchLineNo(@conv_file_name, markerText)
262
+
263
+ if (lineNo != -1)
264
+ data = []
265
+ open(file_name) do |file|
266
+ file.each do |line|
267
+ data << line
268
+ end
269
+ end
270
+
271
+ startLineNo = lineNo + lineNoOffset
272
+ success = true
273
+
274
+ insertTexts.size.times do |index|
275
+ if (insertTexts[index] != data[startLineNo + index])
276
+ success = false
277
+ break
278
+ end
279
+ end
280
+
281
+ if (success)
282
+ insertTexts.size.times do |index|
283
+ puts $text.get('real_delete_insert', '"' + insertTexts[index].chomp + '"', '"' + markerText.chomp + '"')
284
+ end
285
+
286
+ file_deleteline(@conv_file_name, startLineNo, insertTexts.size)
287
+ end
288
+ end
289
+ end
290
+
291
+ def createNewFile
292
+ dirname = File::dirname(@conv_file_name)
293
+
294
+ if (!FileTest.exist? dirname)
295
+ puts $text.get('create_dir', dirname)
296
+ FileUtils.mkdir_p dirname
297
+ end
298
+
299
+ puts $text.get('create_file', @conv_file_name)
300
+ File.open(@conv_file_name, "wb") {|f| f.write convertString(@text.to_s)}
301
+ end
302
+
303
+ def deleteFile
304
+ if (FileTest.exist? @conv_file_name)
305
+ FileUtils.rm(@conv_file_name)
306
+ puts $text.get('delete_file', @conv_file_name)
307
+ else
308
+ puts $text.get('delete_file_fail', @conv_file_name)
309
+ end
310
+
311
+ # �f�B���N�g������ɂȂ�����폜����
312
+ dirname = File::dirname(@conv_file_name)
313
+ if (FileTest.exist?(dirname) && Dir.isEmpty?(dirname))
314
+ Dir.rmdir(dirname)
315
+ puts $text.get('delete_dir', dirname)
316
+ end
317
+ end
318
+
319
+ def convertString(string)
320
+ s = string.clone
321
+
322
+ @inputs.size.times do |index|
323
+ i = @inputs[index]
324
+ s.gsub!(/ReptName#{index + 1}/, i)
325
+ s.gsub!(/reptName#{index + 1}/, i.downcaseHead)
326
+ s.gsub!(/reptname#{index + 1}/, i.downcase)
327
+ s.gsub!(/REPTNAME#{index + 1}/, i.upcase)
328
+ s.gsub!(/rept_name#{index + 1}/, i.joinUnderbar)
329
+ end
330
+
331
+ s
332
+ end
333
+
334
+ def insert?
335
+ @text.to_s =~ /^-->/
336
+ end
337
+ end
338
+
339
+ def rept
340
+ # �I�v�V��������
341
+ undo = false
342
+ force = false
343
+
344
+ opt = OptionParser.new($text.get('command_line'))
345
+ opt.on('-u', '--undo', $text.get('undo')) {|v| undo = true}
346
+ opt.on('-f', '--force', $text.get('force')) {|v| force = true}
347
+ opt.parse!
348
+
349
+ # ���s�{��
350
+ if (ARGV.size > 0)
351
+ parser = Parser.new(ARGV[0])
352
+ parser.parse
353
+
354
+ args = parser.args
355
+ creator = parser.creator
356
+
357
+ if (ARGV.size == 1)
358
+ puts $text.get('rept_description', ARGV[0], args.description)
359
+ else
360
+ arg_list = ARGV[1..-1]
361
+
362
+ # �����̐����������Ȃ���ΏI��
363
+ abort $text.get('command_num_error', args.num) if arg_list.size % args.num != 0
364
+
365
+ inputs_list = []
366
+
367
+ # ���̓Z�b�g���쐬
368
+ while !arg_list.empty?
369
+ inputs = []
370
+ args.num.times {|| inputs << arg_list.shift }
371
+ inputs_list << inputs
372
+ end
373
+
374
+ # ���ɖ߂��ꍇ�͊m�F���s��
375
+ if (undo)
376
+ if (!force)
377
+ confirm = nil
378
+ while confirm != 'y' && confirm != 'n'
379
+ confirm = Readline.readline($text.get('is_undo'))
380
+ end
381
+
382
+ abort if (confirm == 'n')
383
+ end
384
+
385
+ # undo�̏ꍇ�͕����̃��X�g���^����ꂽ�ꍇ�A�t���ɓK�p������
386
+ # �������X�g�̑}���폜������ŏ�肭����
387
+ inputs_list.reverse!
388
+ end
389
+
390
+ while !inputs_list.empty?
391
+ inputs = inputs_list.shift
392
+
393
+ puts $text.get('rept_args', inputs.join(", "))
394
+
395
+ # �Z�b�g�A�b�v
396
+ creator.each {|c| c.setInputs(inputs)}
397
+
398
+ if (!undo)
399
+ # �������邩�H
400
+ isCreate = true
401
+
402
+ # �����”\���e�X�g
403
+ creator.each {|c| isCreate = false if !c.createTest}
404
+
405
+ # ����
406
+ if (isCreate)
407
+ creator.each {|c| c.create}
408
+ end
409
+ else
410
+ # ���ɖ߂�
411
+ creator.each {|c| c.undo}
412
+ end
413
+ end
414
+ end
415
+ else
416
+ puts opt.help
417
+ end
418
+ end
data/lib/rept.yaml ADDED
@@ -0,0 +1,66 @@
1
+ # �R�}���h���C���̐���
2
+ command_line: "rept [option] rept_file"
3
+
4
+ # -u�I�v�V�����̐���
5
+ undo: "���ɖ߂��܂�(���������ׂ��t�@�C���͍폜���A�}�����ꂽ�v�f�͏�����܂�)"
6
+
7
+ # ������rept�t�@�C�����n���ꂽ���̐���
8
+ # $1 ... rept�t�@�C��
9
+ # $2 ... �����̐���(rept�t�@�C���ɋL�q����Ă������)
10
+ rept_description: "rept $1 $2"
11
+
12
+ # �����̐�������Ȃ��������̐���
13
+ command_num_error: "�����̐���rept�t�@�C���̗v���ɂ����Ă��܂���($1�̔{���ł���K�v������܂�)"
14
+
15
+ # ���s�J�n���Ɉ������X�g��\������
16
+ rept_args: "���� : $1"
17
+
18
+ # �t�@�C���}���e�X�g�Ɏ��s����
19
+ file_insert_error: "�G���[: �}�������������̂ł����A$1�����݂��܂���"
20
+
21
+ # �t�@�C���쐬�e�X�g��
22
+ file_create_test: "�e�X�g: $1�͍쐬�”\�H"
23
+
24
+ # �t�@�C���쐬�e�X�g�Ɏ��s����
25
+ file_create_error: "�G���[: �V������肽�������̂ł����A$1�����łɑ��݂��Ă��܂�"
26
+
27
+ # �}���e�X�g
28
+ insert_test: "�e�X�g: $1�͑}���”\�H"
29
+
30
+ # �}��
31
+ insert: "���s : $1�ɑ}��"
32
+
33
+ # ���ۂɑ}���������ɕ\�������
34
+ real_insert: "���s : �}�� $1 --> $2"
35
+
36
+ # �}���悪���‚����Ȃ�����
37
+ insert_not_found: "�G���[: �}��������‚����܂���($1 --> $2)"
38
+
39
+ # �f�B���N�g���쐬
40
+ create_dir: "���s : �f�B���N�g���쐬 --> $1"
41
+
42
+ # �t�@�C���쐬
43
+ create_file: "���s : �t�@�C���쐬 --> $1"
44
+
45
+ # �t�@�C���폜
46
+ delete_file: "���s : �t�@�C�����폜 $1"
47
+
48
+ # �t�@�C���폜���悤�Ƃ����疳������
49
+ delete_file_fail: "���s : $1 �͂��łɑ��݂��Ă��܂���"
50
+
51
+ # �f�B���N�g���폜
52
+ delete_dir: "���s : ��ɂȂ����̂Ńf�B���N�g�����폜���܂��� $1"
53
+
54
+ # �{���Ɍ��ɖ߂����m�F
55
+ is_undo: "�m�F : �{���Ɍ��ɖ߂��Ă����ł����H(y/n) "
56
+
57
+ # �}���폜
58
+ delete_insert: "���s : $1����}���s���폜���܂�"
59
+
60
+ # �}���폜���s
61
+ real_delete_insert: "���s : �}���폜 $1 <-- $2"
62
+
63
+ # �Θb���s�킸�Ɏ��s���s��
64
+ force: "�Θb���s�킸�Ɏ��s���s��"
65
+
66
+ # @insert