xrt 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f297398d1b9757376b533d456353e069ad6c852
4
- data.tar.gz: a953d05e896c259e4c46fa2414cff70d66407969
3
+ metadata.gz: 4ebf55387c6ecfe4b9df158408b759f0b1ee8106
4
+ data.tar.gz: 82976c5d15a8b57a072272f727a913e60c1c3737
5
5
  SHA512:
6
- metadata.gz: d0ed3a59d292aa21d82f91d7c6277a19793d0f0fd8f9c7c1c7008a557f7408628d7e6355b922d0e5ff2fb4cd4642039ee2032aa8ffd77e88e20ca99f17f738fa
7
- data.tar.gz: 4a39005f378374d91efcda18bf016744873bb07778c5e1579b90f00e06516796e40b38a4484b9c71322c189653f0ae5d7dfce52dcc1710c0bdb0bc1cc9f80c52
6
+ metadata.gz: 1af7ab32ea83cdc03d6af6da7b10099efb26bdb7ecb1621477981e41b6de063b7795429eb845d15f9c6cc6313a292f908bba6014438560dc809b03e637eef0ab
7
+ data.tar.gz: 89609aba1c70bb9856b0c8ed65de38b9724c275e889acbd076275c6998dea00bfa11a689944a5f89d090e95beb853acdd6f10c532312d8beda8ed81ab8b45220
data/lib/xrt/cli.rb CHANGED
@@ -12,6 +12,9 @@ module XRT
12
12
  when 'extract'
13
13
  success = XRT::Command::Extract.new.execute(*args)
14
14
  exit success ? 0 : 1
15
+ when 'lcs'
16
+ success = XRT::Command::LCS.new.execute(*args)
17
+ exit success ? 0 : 1
15
18
  else
16
19
  warn "command not found"
17
20
  exit 1
@@ -0,0 +1,22 @@
1
+ require 'xrt/depth_checker'
2
+
3
+ module XRT
4
+ module Command
5
+ class Dump
6
+ def execute(files)
7
+ files.each{|file|
8
+ puts annotate_file file, STDOUT.tty?
9
+ }
10
+ end
11
+
12
+ def annotate_file target_file, enable_color=nil
13
+ checker = XRT::DepthChecker.new
14
+ parsed, annotated_source = checker.check open(target_file).read, enable_color
15
+ unless parsed
16
+ raise "Failed to parse #{target_file}"
17
+ end
18
+ annotated_source
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ require 'pathname'
2
+ require 'xrt/parser'
3
+ require 'xrt/transaction'
4
+
5
+ module XRT
6
+ module Command
7
+
8
+ class Extract
9
+ # xrt extract templates/blogs/index.html '[% IF pager' 'templates/' 'blogs/_pager.tt'
10
+ def execute(from_file, target_block, templates_directory, to_file_name)
11
+ transaction = as_transaction(from_file, target_block, templates_directory, to_file_name)
12
+ transaction.commit!
13
+ true
14
+ end
15
+
16
+ def as_transaction(from_file, target_block, templates_directory, to_file_name)
17
+ transaction = XRT::Transaction.new
18
+ from_source = open(from_file).read
19
+ parser = XRT::Parser.new(from_source)
20
+ from_doc = parser.document
21
+
22
+ found_blocks = from_doc.find_blocks.select{|block|
23
+ block.content.index(target_block) == 0
24
+ }
25
+
26
+ if found_blocks.length == 0
27
+ raise "target_block not found"
28
+ end
29
+
30
+ if found_blocks.length > 1
31
+ raise "ambiguous target_block"
32
+ end
33
+
34
+ found_block = found_blocks.first
35
+
36
+ replace_to_node = XRT::Parser.new(%Q{[% INCLUDE "#{to_file_name}" %]}).document
37
+
38
+ from_doc.replace_child(replace_to_node, found_block)
39
+
40
+ content_to_overwrite = from_doc.content
41
+ content_for_new_file = found_block.auto_indent + "\n"
42
+
43
+ transaction.edit from_file, content_to_overwrite
44
+
45
+ transaction.new_file transaction.full_path(templates_directory, to_file_name), content_for_new_file
46
+
47
+ transaction
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ require 'xrt/depth_checker'
2
+
3
+ module XRT
4
+ module Command
5
+ class LCS
6
+ def execute(*files)
7
+ lcs = collect(*files)
8
+ if lcs.length > 0
9
+ puts lcs.join("\n---\n")
10
+ true
11
+ else
12
+ false
13
+ end
14
+ end
15
+
16
+ def collect(*files)
17
+ products = statements(files.shift)
18
+ while files.length > 0
19
+ products = products.product(statements(files.shift))
20
+ end
21
+
22
+ products.select{|pairs|
23
+ pairs.flatten.uniq.length == 1
24
+ }.map{|pairs| pairs.first }.sort_by{|s| s.length}
25
+ end
26
+
27
+ def statements(file)
28
+ XRT::Parser.new(open(file).read).document.find_blocks.map{|s| s.auto_indent }
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/xrt/commands.rb CHANGED
@@ -1,70 +1,4 @@
1
- require 'pathname'
2
1
  require 'xrt/depth_checker'
3
-
4
- module XRT
5
- module Command
6
- class Dump
7
- def execute(files)
8
- files.each{|file|
9
- dump_file file
10
- }
11
- end
12
-
13
- def dump_file target_file
14
- warn "Dumping #{target_file}"
15
- checker = XRT::DepthChecker.new
16
- parsed, annotated_source = checker.check open(target_file).read
17
- puts annotated_source
18
- unless parsed
19
- raise "Failed to parser #{target_file} (#{index}/#{target_files.length})"
20
- end
21
- end
22
- end
23
-
24
- class Extract
25
- # xrt extract templates/blogs/index.html '[% IF pager' 'templates/' 'blogs/_pager.tt'
26
- def execute(from_file, target_block, templates_directory, to_file_name)
27
- from_source = open(from_file).read
28
- parser = XRT::Parser.new(from_source)
29
- from_doc = parser.document
30
-
31
- found_blocks = from_doc.find_blocks.select{|block|
32
- block.content.index(target_block) == 0
33
- }
34
-
35
- if found_blocks.length == 0
36
- raise "target_block not found"
37
- end
38
-
39
- if found_blocks.length > 1
40
- raise "ambiguous target_block"
41
- end
42
-
43
- found_block = found_blocks.first
44
-
45
- replace_to_node = XRT::Parser.new(%Q{[% INCLUDE "#{to_file_name}" %]}).document
46
-
47
- from_doc.replace_child(replace_to_node, found_block)
48
-
49
- content_to_overwrite = from_doc.content
50
- content_for_new_file = found_block.auto_indent
51
-
52
- open(from_file, 'w'){|f|
53
- f.write content_to_overwrite
54
- }
55
-
56
- new_file = Pathname(templates_directory).join(to_file_name)
57
-
58
- if new_file.exist?
59
- raise 'TO_FILE_NAME exists.'
60
- end
61
-
62
- open(new_file, 'w'){|f|
63
- f.puts content_for_new_file
64
- }
65
-
66
- true
67
- end
68
- end
69
- end
70
- end
2
+ require 'xrt/command/dump'
3
+ require 'xrt/command/extract'
4
+ require 'xrt/command/lcs'
@@ -3,7 +3,7 @@ require 'xrt/syntax'
3
3
 
4
4
  module XRT
5
5
  class DepthChecker
6
- def check source
6
+ def check source, enable_color=nil
7
7
  annotated_source = ''
8
8
  parser = XRT::Parser.new(source)
9
9
  syntax = XRT::Syntax.new
@@ -14,8 +14,8 @@ module XRT
14
14
  current_level += diff
15
15
  annotated_source += statement
16
16
 
17
- color = 44 + diff
18
- if STDOUT.tty?
17
+ if enable_color
18
+ color = 44 + diff
19
19
  annotated_source += "\e[#{color}m#{current_level}\e[0m"
20
20
  else
21
21
  annotated_source += current_level.to_s
data/lib/xrt/statement.rb CHANGED
@@ -45,6 +45,10 @@ module XRT
45
45
  nil
46
46
  end
47
47
 
48
+ def statements
49
+ children.concat(children.map{|child| child.children }.flatten)
50
+ end
51
+
48
52
  def find_blocks
49
53
  children.select{|child|
50
54
  child.kind_of? XRT::Statement::Block
@@ -0,0 +1,47 @@
1
+ module XRT
2
+ class Transaction
3
+ attr_reader :files
4
+ def initialize
5
+ @files = {}
6
+ end
7
+
8
+ def edit(path, content)
9
+ unless full_path(path).exist?
10
+ raise 'Editing new file'
11
+ end
12
+ if full_path(path).read == content
13
+ return
14
+ end
15
+ add(path, content)
16
+ end
17
+
18
+ def new_file(path, content)
19
+ if full_path(path).exist?
20
+ if full_path(path).read == content
21
+ # nothing will change
22
+ return
23
+ else
24
+ raise "File #{path} already exists"
25
+ end
26
+ end
27
+
28
+ add(path, content)
29
+ end
30
+
31
+ def full_path(*fragments)
32
+ Pathname(fragments.shift).join(*fragments)
33
+ end
34
+
35
+ def commit!
36
+ files.each_pair{|path, content|
37
+ full_path(path).open('w') {|f|
38
+ f.write content
39
+ }
40
+ }
41
+ end
42
+
43
+ def add(path, content)
44
+ @files[path.to_s] = content
45
+ end
46
+ end
47
+ end
data/lib/xrt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Xrt
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'test/unit'
2
+ require 'tmpdir'
3
+ require 'xrt/command/dump'
4
+
5
+ class TestParser < Test::Unit::TestCase
6
+ def test_annotate
7
+ Dir.mktmpdir{|dir|
8
+ Pathname(dir).join('if1.pm').open('w'){ |f| f.write %q{[% IF 1 %]nested[% END %]} }
9
+
10
+ command = XRT::Command::Dump.new
11
+ source = command.annotate_file Pathname(dir).join('if1.pm')
12
+ assert_equal %q{[% IF 1 %]1nested1[% END %]0}, source
13
+ }
14
+ end
15
+
16
+ def test_annotate_broken_template
17
+ Dir.mktmpdir{|dir|
18
+ Pathname(dir).join('if1.pm').open('w'){ |f| f.write %q{[% IF 1 %]nested[% END %} }
19
+
20
+ command = XRT::Command::Dump.new
21
+ assert_raise {
22
+ command.annotate_file Pathname(dir).join('if1.pm')
23
+ }
24
+ }
25
+ end
26
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'tmpdir'
3
+ require 'xrt/command/extract'
4
+
5
+ class TestCommandExtract < Test::Unit::TestCase
6
+ def test_extract
7
+ Dir.mktmpdir{|dir|
8
+ templates_dir = Pathname(dir).join('templates')
9
+ templates_dir.mkdir
10
+ templates_dir.join('if1.pm').open('w'){ |f| f.write %q{[% IF 1 %]nested[% END %]} }
11
+
12
+ command = XRT::Command::Extract.new
13
+ command.execute(templates_dir.join('if1.pm').to_s, %q{[% IF 1 %]}, templates_dir.to_s, '_if.tt')
14
+
15
+ assert_equal '[% INCLUDE "_if.tt" %]', templates_dir.join('if1.pm').open.read
16
+ assert_equal %Q{[% IF 1 %]nested[% END %]\n}, templates_dir.join('_if.tt').open.read
17
+ }
18
+ end
19
+
20
+ def test_as_transaction
21
+ Dir.mktmpdir{|dir|
22
+ templates_dir = Pathname(dir).join('templates')
23
+ templates_dir.mkdir
24
+ templates_dir.join('if1.pm').open('w'){ |f| f.write %q{[% IF 1 %]nested[% END %]} }
25
+
26
+ command = XRT::Command::Extract.new
27
+ transaction = command.as_transaction(templates_dir.join('if1.pm').to_s, %q{[% IF 1 %]}, templates_dir.to_s, '_if.tt')
28
+ assert_equal({
29
+ templates_dir.join('if1.pm').to_s => '[% INCLUDE "_if.tt" %]',
30
+ templates_dir.join('_if.tt').to_s => %Q{[% IF 1 %]nested[% END %]\n},
31
+ }, transaction.files)
32
+ }
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'tmpdir'
3
+ require 'xrt/command/lcs'
4
+
5
+ class TestCommandLCS < Test::Unit::TestCase
6
+ def test_annotate
7
+ Dir.mktmpdir{|dir|
8
+ Pathname(dir).join('if1.pm').open('w'){ |f| f.write %q{aaa[% IF 1 %]nested[% END %]bbb} }
9
+ Pathname(dir).join('if2.pm').open('w'){ |f| f.write %q{ccc[% IF 1 %]nested[% END %]ddd} }
10
+
11
+ command = XRT::Command::LCS.new
12
+ lcs = command.collect(Pathname(dir).join('if1.pm').to_s, Pathname(dir).join('if2.pm').to_s)
13
+ assert_equal [%q{[% IF 1 %]nested[% END %]}], lcs
14
+ }
15
+ end
16
+ end
@@ -114,6 +114,18 @@ HTML
114
114
  assert_equal nil, document.depth(not_child)
115
115
  end
116
116
 
117
+ def test_statements
118
+ document = XRT::Statement::Document.new
119
+ assert_equal [], document.find_blocks, 'when there is no block'
120
+
121
+ text_block = XRT::Statement::Text.new('1')
122
+ document << text_block
123
+ if_block = XRT::Statement::Block.new('[% IF a %]')
124
+ document << if_block
125
+
126
+ assert_equal [ text_block, if_block ], document.statements, 'returns statements'
127
+ end
128
+
117
129
  def test_find_blocks
118
130
  document = XRT::Statement::Document.new
119
131
  assert_equal [], document.find_blocks, 'when there is no block'
@@ -0,0 +1,97 @@
1
+ require 'test/unit'
2
+ require 'xrt/transaction'
3
+ require 'tmpdir'
4
+ require 'pathname'
5
+
6
+ class TestTransaction < Test::Unit::TestCase
7
+ def test_initialize
8
+ Dir.mktmpdir{|dir|
9
+ transaction = XRT::Transaction.new
10
+ assert transaction
11
+ assert_equal({}, transaction.files)
12
+ }
13
+ end
14
+
15
+ def test_full_path
16
+ transaction = XRT::Transaction.new
17
+ assert_equal Pathname('/tmp/xrt/a.txt'), transaction.full_path('/tmp/xrt', 'a.txt')
18
+ assert_equal Pathname('/tmp/xrt/somedir/a.txt'), transaction.full_path('/tmp/xrt', 'somedir', 'a.txt')
19
+ end
20
+
21
+ def test_new_file_without_conflict
22
+ Dir.mktmpdir{|dir|
23
+ transaction = XRT::Transaction.new
24
+ transaction.new_file transaction.full_path(dir, 'hello.txt').to_s, 'Hello!'
25
+ transaction.commit!
26
+ assert_equal 'Hello!', transaction.full_path(dir, 'hello.txt').open.read
27
+ }
28
+ end
29
+
30
+ def test_new_file_throws_error_when_conflict
31
+ Dir.mktmpdir{|dir|
32
+ Pathname(dir).join('hello.txt').open('w'){ |f| f.write 'existing content' }
33
+ transaction = XRT::Transaction.new
34
+
35
+ # when content doesn't match existing content
36
+ assert_raise {
37
+ transaction.new_file transaction.full_path(dir, 'hello.txt').to_s, 'Hello!'
38
+ }
39
+
40
+ # when content matches existing content
41
+ assert_nothing_raised {
42
+ transaction.new_file transaction.full_path(dir, 'hello.txt').to_s, 'existing content'
43
+ }
44
+ assert_equal transaction.files, {}, 'nothing added'
45
+ }
46
+ end
47
+
48
+ def test_edit_when_editing_existing_file
49
+ Dir.mktmpdir{|dir|
50
+ Pathname(dir).join('hello.txt').open('w'){ |f| f.write 'existing content' }
51
+ transaction = XRT::Transaction.new
52
+ assert_nothing_raised {
53
+ transaction.edit transaction.full_path(dir, 'hello.txt').to_s, 'Hello!'
54
+ }
55
+ assert_equal({
56
+ transaction.full_path(dir, 'hello.txt').to_s => 'Hello!',
57
+ }, transaction.files)
58
+ }
59
+ end
60
+
61
+ def test_edit_when_content_is_same
62
+ Dir.mktmpdir{|dir|
63
+ Pathname(dir).join('hello.txt').open('w'){ |f| f.write 'existing content' }
64
+ transaction = XRT::Transaction.new
65
+ assert_nothing_raised {
66
+ transaction.edit transaction.full_path(dir, 'hello.txt').to_s, 'existing content'
67
+ }
68
+ assert_equal({}, transaction.files)
69
+ }
70
+ end
71
+
72
+ def test_edit_when_editing_new_file
73
+ Dir.mktmpdir{|dir|
74
+ transaction = XRT::Transaction.new
75
+ assert_raise {
76
+ transaction.edit transaction.full_path(dir, 'hello.txt').to_s, 'Hello!'
77
+ }
78
+ assert_equal({}, transaction.files)
79
+ }
80
+ end
81
+
82
+ def _test_add_commit
83
+ Dir.mktmpdir{|dir|
84
+ transaction = XRT::Transaction.new
85
+ transaction.add transaction.full_path(dir, 'hello.txt').to_s, 'Hello!'
86
+ assert_equal({
87
+ transaction.full_path(dir, 'hello.txt').to_s => 'Hello!',
88
+ }, transaction.files)
89
+ assert_equal false, transaction.full_path(dir, 'hello.txt').exist?, 'not exist yet'
90
+
91
+ transaction.commit!
92
+
93
+ assert_equal true, transaction.full_path(dir, 'hello.txt').exist?, 'now exists'
94
+ assert_equal 'Hello!', transaction.full_path(dir, 'hello.txt').open.read
95
+ }
96
+ end
97
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xrt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - hitode909
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-22 00:00:00.000000000 Z
11
+ date: 2016-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,11 +69,15 @@ files:
69
69
  - bin/xrt
70
70
  - lib/xrt.rb
71
71
  - lib/xrt/cli.rb
72
+ - lib/xrt/command/dump.rb
73
+ - lib/xrt/command/extract.rb
74
+ - lib/xrt/command/lcs.rb
72
75
  - lib/xrt/commands.rb
73
76
  - lib/xrt/depth_checker.rb
74
77
  - lib/xrt/parser.rb
75
78
  - lib/xrt/statement.rb
76
79
  - lib/xrt/syntax.rb
80
+ - lib/xrt/transaction.rb
77
81
  - lib/xrt/version.rb
78
82
  - sketch/dump_blocks.rb
79
83
  - sketch/dump_max_depth.rb
@@ -81,10 +85,14 @@ files:
81
85
  - sketch/find_block.rb
82
86
  - sketch/replace_block.rb
83
87
  - test/run-test.rb
88
+ - test/test-command-dump.rb
89
+ - test/test-command-extract.rb
90
+ - test/test-command-lcs.rb
84
91
  - test/test-depth_checker.rb
85
92
  - test/test-parser.rb
86
93
  - test/test-statement.rb
87
94
  - test/test-syntax.rb
95
+ - test/test-transaction.rb
88
96
  - xrt.gemspec
89
97
  homepage: https://github.com/hitode909/xrt
90
98
  licenses:
@@ -112,7 +120,11 @@ specification_version: 4
112
120
  summary: Refactoring Tool for Text::Xslate
113
121
  test_files:
114
122
  - test/run-test.rb
123
+ - test/test-command-dump.rb
124
+ - test/test-command-extract.rb
125
+ - test/test-command-lcs.rb
115
126
  - test/test-depth_checker.rb
116
127
  - test/test-parser.rb
117
128
  - test/test-statement.rb
118
129
  - test/test-syntax.rb
130
+ - test/test-transaction.rb