ditto_code 0.1.1 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/dittoc +13 -5
- data/lib/dittocode/parse.rb +64 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff2bc71ba0731750d144d1878e573f6bc2b85e98
|
4
|
+
data.tar.gz: b183b41ca6d755444711a1580b253234a08a0bb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c22c60d9860e6367dfc1d4ca1779596520e8951d297c59f09bf4f8fb61c340808603b26b28e1bceddae817b00d2487e3eb276165aedcd16b243b59ff49da534
|
7
|
+
data.tar.gz: 7ae8682cece826d4cb2df8d207eae3aac3a59d4808a55355938bbf28dec941830e5774ac923f7990c63fd12ad9899876336b527c6e31b0ea2fde5858d9b5f937
|
data/bin/dittoc
CHANGED
@@ -7,7 +7,7 @@ require 'ditto_code'
|
|
7
7
|
|
8
8
|
def usage(s)
|
9
9
|
$stderr.puts("Ditto say -> [Err] " + s)
|
10
|
-
$stderr.puts("Ditto say -> [Usage] #{File.basename($0)}: [-v <verbose>] [-f <folder>] [-r <recursive>] [-o <override files>] [--allow-views <search in views>] ENVIRONMENT file|folder")
|
10
|
+
$stderr.puts("Ditto say -> [Usage] #{File.basename($0)}: [-v <verbose>] [-f <folder>] [-r <recursive>] [-o <override files>] [--allow-views <search in views>] [-i <indent>] ENVIRONMENT file|folder")
|
11
11
|
exit(1)
|
12
12
|
end
|
13
13
|
|
@@ -64,6 +64,7 @@ $verbose = false;
|
|
64
64
|
$override = false;
|
65
65
|
@recursive = false
|
66
66
|
@allow_views = false
|
67
|
+
$indent = false
|
67
68
|
|
68
69
|
# ===============================================================
|
69
70
|
# INITIATION
|
@@ -76,14 +77,17 @@ while ARGV[0]
|
|
76
77
|
$isFolder = true
|
77
78
|
when '-v'
|
78
79
|
ARGV.shift
|
79
|
-
$verbose =
|
80
|
+
$verbose = true
|
80
81
|
when '-o'
|
81
82
|
ARGV.shift
|
82
83
|
$override = true
|
83
84
|
when '-r'
|
84
85
|
ARGV.shift
|
85
86
|
@recursive = true
|
86
|
-
|
87
|
+
when '-i'
|
88
|
+
ARGV.shift
|
89
|
+
$indent = true
|
90
|
+
when '--allow-views'
|
87
91
|
ARGV.shift
|
88
92
|
@allow_views = true
|
89
93
|
when /^-/
|
@@ -104,7 +108,7 @@ end
|
|
104
108
|
# ===============================================================
|
105
109
|
|
106
110
|
# Initiate ditto
|
107
|
-
@ditto = DittoCode::Parse.new($environment, $override)
|
111
|
+
@ditto = DittoCode::Parse.new($environment, $override, $verbose, $indent)
|
108
112
|
|
109
113
|
# Check if the user has introduce a folder/file
|
110
114
|
if $folder.empty?
|
@@ -121,7 +125,11 @@ else
|
|
121
125
|
iterateFolder($folder)
|
122
126
|
else
|
123
127
|
# Only a file
|
124
|
-
|
128
|
+
if $folder.end_with?('.rb')
|
129
|
+
@ditto.transformation($folder, false)
|
130
|
+
elsif $folder.end_with?('.erb') && @allow_views
|
131
|
+
@ditto.transformation($folder, true)
|
132
|
+
end
|
125
133
|
end
|
126
134
|
|
127
135
|
end
|
data/lib/dittocode/parse.rb
CHANGED
@@ -10,18 +10,25 @@ module DittoCode
|
|
10
10
|
end
|
11
11
|
|
12
12
|
# Initialize the environment
|
13
|
-
def initialize(environment, override)
|
14
|
-
@env = environment
|
15
|
-
@override = override
|
13
|
+
def initialize(environment, override, verbose, indent)
|
14
|
+
@env = environment
|
15
|
+
@override = override
|
16
|
+
@verbose = verbose
|
17
|
+
@indent = indent
|
16
18
|
end
|
17
19
|
|
18
20
|
# Transform the file based in the environment
|
19
|
-
def transformation(file_path,
|
21
|
+
def transformation(file_path, isView)
|
20
22
|
|
21
23
|
# Start to read the file
|
22
24
|
file = File.open(file_path)
|
23
|
-
|
25
|
+
if isView
|
26
|
+
@file_name = File.basename(file_path, ".erb")
|
27
|
+
else
|
28
|
+
@file_name = File.basename(file_path, ".rb")
|
29
|
+
end
|
24
30
|
@dir_name = File.dirname(file_path) + "/"
|
31
|
+
@isView = isView
|
25
32
|
|
26
33
|
# Generate the new archive
|
27
34
|
out_file = initiateFile(file_path)
|
@@ -32,10 +39,8 @@ module DittoCode
|
|
32
39
|
# Get the text
|
33
40
|
# Protect against nil or directory when it's a file
|
34
41
|
begin
|
35
|
-
text = file.read
|
36
|
-
|
37
42
|
# Each line........
|
38
|
-
|
43
|
+
file.each_line do | line |
|
39
44
|
|
40
45
|
if @isView
|
41
46
|
m = /[\s]*<%[\s]*DittoCode::Exec.if[\s]+['|"](?<environment>[a-zA-Z]+)['|"][\s]+do[\s]*%>/.match(line)
|
@@ -50,7 +55,13 @@ module DittoCode
|
|
50
55
|
else
|
51
56
|
|
52
57
|
if !actions[:atack]
|
53
|
-
|
58
|
+
if file.eof?
|
59
|
+
# Don't print a \n in the last line
|
60
|
+
out_file.print(line)
|
61
|
+
else
|
62
|
+
out_file.puts(line)
|
63
|
+
end
|
64
|
+
|
54
65
|
else
|
55
66
|
# He is transforming
|
56
67
|
dittos += 1
|
@@ -63,7 +74,7 @@ module DittoCode
|
|
63
74
|
actions[:atack] = false
|
64
75
|
elsif actions[:env] == @env
|
65
76
|
# Only how if we must mantain it
|
66
|
-
out_file
|
77
|
+
check_with_indent(out_file, line)
|
67
78
|
end
|
68
79
|
|
69
80
|
actions[:ends] -= 1
|
@@ -72,7 +83,7 @@ module DittoCode
|
|
72
83
|
|
73
84
|
# prints of coincide with the environment
|
74
85
|
if actions[:env] == @env
|
75
|
-
out_file
|
86
|
+
check_with_indent(out_file, line)
|
76
87
|
end
|
77
88
|
|
78
89
|
# Check if we need to add a new end
|
@@ -87,7 +98,10 @@ module DittoCode
|
|
87
98
|
|
88
99
|
end
|
89
100
|
|
90
|
-
|
101
|
+
if @verbose || dittos != 0
|
102
|
+
say "[Ok] #{dittos} lines ditted on #{@dir_name}#{@file_name}!"
|
103
|
+
end
|
104
|
+
|
91
105
|
closeFile(out_file)
|
92
106
|
|
93
107
|
rescue => e
|
@@ -98,20 +112,36 @@ module DittoCode
|
|
98
112
|
say "[Err] Oh no! I have an error :("
|
99
113
|
end
|
100
114
|
|
115
|
+
if @verbose
|
116
|
+
say e
|
117
|
+
end
|
118
|
+
|
101
119
|
end
|
102
120
|
end
|
103
121
|
|
104
122
|
private
|
105
123
|
|
124
|
+
# Send the line into the file. If indent is true
|
125
|
+
# The line will be indented
|
126
|
+
def check_with_indent(out_file, line)
|
127
|
+
if @indent
|
128
|
+
out_file.puts(line.indent(-1))
|
129
|
+
else
|
130
|
+
out_file.puts(line)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
106
134
|
# Detect an end line
|
107
135
|
def isEnd?(line)
|
108
136
|
|
109
137
|
# Coincide with an end!
|
110
|
-
if
|
111
|
-
|
138
|
+
if @isView && /[\s]+(end)[\s]*%>/.match(line)
|
139
|
+
true
|
140
|
+
elsif /^[\s]*end/.match(line)
|
141
|
+
true
|
142
|
+
else
|
143
|
+
false
|
112
144
|
end
|
113
|
-
|
114
|
-
return false
|
115
145
|
end
|
116
146
|
|
117
147
|
# Detect if we need to add a new end
|
@@ -120,7 +150,7 @@ module DittoCode
|
|
120
150
|
# Get the initializers and the ends of the blocks
|
121
151
|
if @isView
|
122
152
|
initializers = line.scan(/<%[\s]*(if|do|def)[\s]+/).size + line.scan(/<%[@=;\s\w\d]*(if|do|def)[\s]+/).size
|
123
|
-
finals = line.scan(/[\s]+(end)[\s]*%>/).size
|
153
|
+
finals = line.scan(/[\s]+(end)[\s]*%>/).size
|
124
154
|
else
|
125
155
|
initializers = line.scan(/^(if|do|def)[\s]+/).size + line.scan(/[\s]+(if|do|def)[\s]+/).size + line.scan(/[\s]+(if|do|def)$/).size
|
126
156
|
finals = line.scan(/[\s]+(end)[\s]+/).size + line.scan(/^(end)[\s]+/).size + line.scan(/[\s]+(end)$/).size
|
@@ -135,9 +165,17 @@ module DittoCode
|
|
135
165
|
def initiateFile(file_path)
|
136
166
|
|
137
167
|
if(@override)
|
138
|
-
|
168
|
+
if @isView
|
169
|
+
File.new("#{@dir_name}#{@file_name}_tmp.erb", "w")
|
170
|
+
else
|
171
|
+
File.new("#{@dir_name}#{@file_name}_tmp.rb", "w")
|
172
|
+
end
|
139
173
|
else
|
140
|
-
|
174
|
+
if @isView
|
175
|
+
File.new("#{@dir_name}#{@file_name}_#{@env}.erb", "w")
|
176
|
+
else
|
177
|
+
File.new("#{@dir_name}#{@file_name}_#{@env}.rb", "w")
|
178
|
+
end
|
141
179
|
end
|
142
180
|
|
143
181
|
end
|
@@ -153,8 +191,13 @@ module DittoCode
|
|
153
191
|
file.close;
|
154
192
|
|
155
193
|
if(@override)
|
156
|
-
|
157
|
-
|
194
|
+
if @isView
|
195
|
+
File.delete("#{@dir_name}#{@file_name}.erb")
|
196
|
+
File.rename(file, "#{@dir_name}#{@file_name}.erb")
|
197
|
+
else
|
198
|
+
File.delete("#{@dir_name}#{@file_name}.rb")
|
199
|
+
File.rename(file, "#{@dir_name}#{@file_name}.rb")
|
200
|
+
end
|
158
201
|
end
|
159
202
|
|
160
203
|
end
|