ld 0.3.6 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +104 -0
- data/lib/ld.rb +2 -0
- data/lib/ld/document/document.rb +27 -19
- data/lib/ld/error/parameter_error.rb +1 -1
- data/lib/ld/excel/excel.rb +6 -21
- data/lib/ld/excel/sheet.rb +15 -29
- data/lib/ld/file/file.rb +12 -15
- data/lib/ld/print/print.rb +14 -10
- data/lib/ld/project/controllers.rb +1 -1
- data/lib/ld/project/models.rb +1 -1
- data/lib/ld/project/project.rb +4 -1
- data/lib/ld/project/tables.rb +1 -1
- data/lib/ld/project/views.rb +2 -2
- data/lib/ld/version.rb +1 -1
- 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: 4aa24c26d705bf565bb481b9aceca58a56afa136
|
4
|
+
data.tar.gz: 4a94928e5dd958663f0702e5273bb1a25787866d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cd3d87c25b8eefd6d7b04944c42b21b7e87f4c5f4cd75e0f5bc00a87e1ff99188e97189e7bdb44953a88faad01a0b4d99f34217f64abb1281d6e28ebee6ff60
|
7
|
+
data.tar.gz: a491724ef9e82858a6971bd18900a9ffb8b822800314bf55ff2fb2027d5bab2c952d426b6a7a32d02d062d00e14ff6d1cd5a5f6000e12c2cd0a97fe4e3aa040e
|
data/README.md
CHANGED
@@ -118,6 +118,110 @@ Ld::Print.p users, 'id ,name, created_at'
|
|
118
118
|
|
119
119
|
## API
|
120
120
|
|
121
|
+
### Ld::Project
|
122
|
+
* `initialize table_hash = {}, project_root_path = Rails.root.to_s`
|
123
|
+
* 作用:解析一个项目的代码获得结构化的数据
|
124
|
+
|
125
|
+
* `print model_name, type = :relations`
|
126
|
+
* 作用:查看模型的相关信息(参数有:relations,fields,tables,routes,views,controllers)
|
127
|
+
|
128
|
+
* `to_xls path = {:file_path => "#{@root.path}/project.xls"}`
|
129
|
+
* 作用:将这个项目的代码分析结果保存到excel文件(默认在项目根目录下的project.xls)
|
130
|
+
|
131
|
+
### Ld::Document
|
132
|
+
* `initialize file`
|
133
|
+
* 作用:读一个rb文件生成api数据
|
134
|
+
|
135
|
+
### Ld::Excel
|
136
|
+
* `self.open path`
|
137
|
+
* 作用:打开一个xls文件,返回Ld::Excel实例
|
138
|
+
|
139
|
+
* `self.write path, &block`
|
140
|
+
* 作用:写excel(创建新的xls文件)
|
141
|
+
|
142
|
+
* `self.create path, &block`
|
143
|
+
* 作用:write的同名方法
|
144
|
+
|
145
|
+
* `read params, show_location = false`
|
146
|
+
* 示例:Ld::Excel.read "Sheet1?A1:B2"
|
147
|
+
* 作用:读xls文件中的内容,二维数组
|
148
|
+
|
149
|
+
* `read_with_location params`
|
150
|
+
* 作用:与read方法相同(但会多返回坐标数据)
|
151
|
+
|
152
|
+
### Ld::Sheet
|
153
|
+
* `set_headings headings`
|
154
|
+
* 作用:在当前sheet的主体内容顶上方添加一个表头(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
|
155
|
+
|
156
|
+
* `set_color color`
|
157
|
+
* 作用:设置当前sheet页的字体颜色
|
158
|
+
|
159
|
+
* `set_font_size size`
|
160
|
+
* 作用:设置当前sheet页的字体大小
|
161
|
+
|
162
|
+
* `set_font font`
|
163
|
+
* 作用:设置当前sheet页的字体
|
164
|
+
|
165
|
+
* `set_weight weight`
|
166
|
+
* 作用:设置当前sheet页的单元格宽度(暂时无效)
|
167
|
+
|
168
|
+
* `set_point point`
|
169
|
+
* 作用:设置当前sheet页的字体颜色
|
170
|
+
|
171
|
+
### Ld::File
|
172
|
+
* `self.open path`
|
173
|
+
* 作用:打开一个文件
|
174
|
+
|
175
|
+
* `children `
|
176
|
+
* 作用:返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错
|
177
|
+
|
178
|
+
* `self.current `
|
179
|
+
* 作用:返回当前所在目录(Dir.pwd)
|
180
|
+
|
181
|
+
* `dir? `
|
182
|
+
* 作用:判断这是目录吗
|
183
|
+
|
184
|
+
* `file? `
|
185
|
+
* 作用:判断这是文件吗
|
186
|
+
|
187
|
+
* `find name`
|
188
|
+
* 作用:查找文件或目录,返回一个一级目录或文件,如果不存在则返回nil
|
189
|
+
|
190
|
+
* `search name, type = :all`
|
191
|
+
* 作用:精确查找,返回所有匹配的目录和文件
|
192
|
+
|
193
|
+
* `search_regexp regexp, type = :all`
|
194
|
+
* 作用:模糊查找,返回所有匹配的目录和文件
|
195
|
+
|
196
|
+
* `lines `
|
197
|
+
* 作用:如果是一个文本文件,返回所有行
|
198
|
+
|
199
|
+
* `rename new_name`
|
200
|
+
* 作用:修改名称(目录或文件均可)
|
201
|
+
|
202
|
+
* `delete `
|
203
|
+
* 作用:删除当前文件(有gets确认)
|
204
|
+
|
205
|
+
* `files `
|
206
|
+
* 作用:返回所有文件
|
207
|
+
|
208
|
+
* `parent `
|
209
|
+
* 作用:返回父目录
|
210
|
+
|
211
|
+
* `siblings `
|
212
|
+
* 作用:返回所有兄弟
|
213
|
+
|
214
|
+
* `dirs `
|
215
|
+
* 作用:返回所有目录
|
216
|
+
|
217
|
+
* `ls `
|
218
|
+
* 作用:输出目录中所有条目
|
219
|
+
|
220
|
+
### Ld::Print
|
221
|
+
* `self.p models, fields`
|
222
|
+
* 作用:格式化打印模型数组
|
223
|
+
|
224
|
+
|
121
225
|
|
122
226
|
|
123
227
|
## Development
|
data/lib/ld.rb
CHANGED
data/lib/ld/document/document.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
class Ld::Document
|
2
2
|
|
3
|
-
attr_accessor :doc
|
3
|
+
attr_accessor :doc
|
4
4
|
|
5
|
-
#= 作用
|
5
|
+
#= 作用 读一个rb文件生成api数据
|
6
6
|
def initialize file
|
7
|
-
@class_name = file.split('.rb')[0].camelize
|
8
7
|
@doc = {}
|
9
|
-
lines = file.lines
|
10
|
-
lines.each_with_index do |line, i|
|
8
|
+
@lines = file.lines
|
9
|
+
@lines.each_with_index do |line, i|
|
11
10
|
arr = line.split(' ')
|
12
11
|
if arr.delete_at(0) == 'def'
|
13
|
-
notes = get_notes(lines, i)
|
12
|
+
notes = get_notes(@lines, i)
|
14
13
|
if notes.size > 0
|
15
|
-
|
14
|
+
method = arr.delete_at(0)
|
15
|
+
@doc[method] = {
|
16
16
|
params:arr.join(' '),
|
17
17
|
notes:notes
|
18
18
|
}
|
@@ -36,28 +36,36 @@ class Ld::Document
|
|
36
36
|
|
37
37
|
|
38
38
|
def class_name
|
39
|
-
|
39
|
+
@lines.each do |line|
|
40
|
+
if line.split(' ')[0] =='class'
|
41
|
+
return line.split(' ')[1]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
return nil
|
40
45
|
end
|
41
46
|
|
42
47
|
def self.write_readme readme_path = '/Users/liudong/ruby/my_gems/ld/README2.md'
|
43
48
|
docs = Ld::File.open('/Users/liudong/ruby/my_gems/ld/lib/ld').search_regexp(/.rb$/).map{|f| Ld::Document.new f}
|
44
|
-
arr = ["
|
45
|
-
docs.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
arr << "
|
49
|
+
arr = ["## API\n\n\n"]
|
50
|
+
docs.each do |doc|
|
51
|
+
if !doc.doc.empty?
|
52
|
+
arr << "### #{doc.class_name}"
|
53
|
+
# arr << "```ruby"
|
54
|
+
doc.doc.each do |k, v|
|
55
|
+
arr << "* `#{k} #{v[:params]}`"
|
56
|
+
v[:notes].each do |note|
|
57
|
+
arr << " * #{note[:title]}:#{note[:note]}"
|
58
|
+
end
|
59
|
+
arr << ""
|
51
60
|
end
|
52
|
-
arr << "
|
61
|
+
# arr << "```"
|
53
62
|
end
|
54
|
-
arr << "```"
|
55
63
|
end
|
56
64
|
File.open readme_path,'w' do |file|
|
57
65
|
arr.each do |line|
|
58
|
-
|
66
|
+
file.puts line
|
59
67
|
end
|
60
|
-
file.
|
68
|
+
file.close
|
61
69
|
end
|
62
70
|
end
|
63
71
|
|
data/lib/ld/excel/excel.rb
CHANGED
@@ -4,7 +4,6 @@ Spreadsheet.client_encoding = 'UTF-8'
|
|
4
4
|
class Ld::Excel
|
5
5
|
attr_accessor :excel, :path
|
6
6
|
|
7
|
-
#= 参数 path(可以为空)
|
8
7
|
def initialize path = nil
|
9
8
|
if path
|
10
9
|
if path.match(/.xls$/)
|
@@ -22,17 +21,12 @@ class Ld::Excel
|
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
|
-
#= 作用 打开一个xls
|
26
|
-
#= 参数 xls文件的全路径或相对路径的字符串
|
27
|
-
#= 示例 Ld::Excel.open 'project.xls'
|
28
|
-
#= 返回 Ld::Excel实例
|
24
|
+
#= 作用 打开一个xls文件,返回Ld::Excel实例
|
29
25
|
def self.open path
|
30
26
|
self.new path
|
31
27
|
end
|
32
28
|
|
33
|
-
#= 作用 写
|
34
|
-
#= 返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止)
|
35
|
-
#= 参数1 path 字符串,指定要写的文件路径(最好使用全路径)
|
29
|
+
#= 作用 写excel(创建新的xls文件)
|
36
30
|
def self.write path, &block
|
37
31
|
if path.class == Hash
|
38
32
|
path = path[:file_path]
|
@@ -42,16 +36,13 @@ class Ld::Excel
|
|
42
36
|
excel.save path
|
43
37
|
end
|
44
38
|
|
45
|
-
#= 作用 write
|
39
|
+
#= 作用 write的同名方法
|
46
40
|
def self.create path, &block
|
47
41
|
self.write path, &block
|
48
42
|
end
|
49
43
|
|
50
|
-
#= 作用 读xls
|
44
|
+
#= 作用 读xls文件中的内容,二维数组
|
51
45
|
#= 示例 Ld::Excel.read "Sheet1?A1:B2"
|
52
|
-
#= 示例 Ld::Excel.read "Sheet1?A1:B2+C,D"
|
53
|
-
#= 示例 Ld::Excel.read "Sheet1?A1:B2+C,D,1,2"
|
54
|
-
#= 返回 二维数组
|
55
46
|
def read params, show_location = false
|
56
47
|
case params.class.to_s
|
57
48
|
when 'String'
|
@@ -68,22 +59,16 @@ class Ld::Excel
|
|
68
59
|
end
|
69
60
|
|
70
61
|
#= 作用 与read方法相同(但会多返回坐标数据)
|
71
|
-
#= 返回 与read方法相同(但会多返回坐标数据)
|
72
|
-
#= 示例 与read方法相同(但会多返回坐标数据)
|
73
62
|
def read_with_location params
|
74
63
|
read params, true
|
75
64
|
end
|
76
65
|
|
77
|
-
|
78
|
-
#= 参数 无需参数
|
79
|
-
#= 返回 Ld::Excel实例
|
66
|
+
# 作用 如果xls文件内容有改变,可以刷新(会重新open一次,但这个方法不需要再传入参数了)
|
80
67
|
def flush
|
81
68
|
@excel = Ld::Excel.open @path
|
82
69
|
end
|
83
70
|
|
84
|
-
|
85
|
-
#= 返回 如果创建成功,返回Ld::Excel实例.如果创建失败会返回false(不会导致程序终止)
|
86
|
-
#= 参数1 path 字符串,指定要写的文件路径(最好使用全路径)
|
71
|
+
# 作用 保存(真正执行io写入操作)
|
87
72
|
def save path
|
88
73
|
puts "Covers a file: #{path}" if File.exist? path
|
89
74
|
@excel.write path
|
data/lib/ld/excel/sheet.rb
CHANGED
@@ -1,14 +1,6 @@
|
|
1
1
|
class Ld::Sheet
|
2
2
|
attr_accessor :excel, :sheet
|
3
3
|
|
4
|
-
# PARAMETER_ERROR ||= Ld::ParameterError.new self do |e|
|
5
|
-
# e.hint :scope => [
|
6
|
-
# "Example:",
|
7
|
-
# " read(String): read('abc?A1:B2')",
|
8
|
-
# " read(Hash): read({sheet:'abc',scope:'A1:B2',insert:'',delete:'',location:false})",
|
9
|
-
# ]
|
10
|
-
# end
|
11
|
-
|
12
4
|
ABSCISSA = {}
|
13
5
|
if ABSCISSA.empty?
|
14
6
|
zm = 'A'
|
@@ -16,9 +8,6 @@ class Ld::Sheet
|
|
16
8
|
19999.times{|i| ABSCISSA[zm.succ!] = i+1}
|
17
9
|
end
|
18
10
|
|
19
|
-
#= 参数1 Ld::Excel实例
|
20
|
-
#= 参数2 String:sheet名称
|
21
|
-
#= 参数3 String:有'new'与'open'两种选择
|
22
11
|
def initialize excel, name, type = 'new'
|
23
12
|
raise "sheet name is nil" if !name
|
24
13
|
@excel = excel
|
@@ -36,17 +25,14 @@ class Ld::Sheet
|
|
36
25
|
@format = @sheet.default_format
|
37
26
|
end
|
38
27
|
|
39
|
-
|
40
|
-
#= 参数1 String:指定要读取的范围
|
41
|
-
#= 参数2 Blooean:是否要返回坐标?
|
42
|
-
#= 返回 二维数组
|
28
|
+
# 作用 读sheet页数据,返回二维数组
|
43
29
|
def read scope, show_location = false
|
44
30
|
raise "scope params is nil" if !scope
|
45
31
|
map = read_scope_to_map scope
|
46
32
|
read_arrs map, show_location
|
47
33
|
end
|
48
34
|
|
49
|
-
|
35
|
+
# 作用 解析范围参数
|
50
36
|
def parse_string_scope scope
|
51
37
|
PARAMETER_ERROR.hint_and_raise :scope, "'+' or '-' 只能存在1个" if scope.split('+').size > 2 or scope.split('-').size > 2
|
52
38
|
hash = {}
|
@@ -68,7 +54,7 @@ class Ld::Sheet
|
|
68
54
|
hash
|
69
55
|
end
|
70
56
|
|
71
|
-
|
57
|
+
# 作用 使用范围参数构建maps(预读)
|
72
58
|
def read_scope_to_map scope
|
73
59
|
scope = parse_string_scope scope if scope.class == String
|
74
60
|
PARAMETER_ERROR.hint_and_raise :scope, "缺少scope参数,或':',或':'存在多个" if !scope[:scope] or !scope[:scope].match(/:/) or scope[:scope].split(':').size > 2
|
@@ -101,7 +87,7 @@ class Ld::Sheet
|
|
101
87
|
maps
|
102
88
|
end
|
103
89
|
|
104
|
-
|
90
|
+
# 作用 多读一些行或列
|
105
91
|
def insert_maps rows, cols, inserts
|
106
92
|
raise "inserts 参数只能是 String" if inserts.class != String
|
107
93
|
insert_arr = inserts.split(',').map do |insert|
|
@@ -122,7 +108,7 @@ class Ld::Sheet
|
|
122
108
|
end
|
123
109
|
end
|
124
110
|
|
125
|
-
|
111
|
+
# 作用 少读一些行或列
|
126
112
|
def delete_maps rows, cols, deletes
|
127
113
|
raise "deletes 参数只能是 String" if deletes.class != String
|
128
114
|
del_arr = deletes.split(',').map do |del|
|
@@ -143,7 +129,7 @@ class Ld::Sheet
|
|
143
129
|
end
|
144
130
|
end
|
145
131
|
|
146
|
-
|
132
|
+
# 作用 读二维数据(使用maps)
|
147
133
|
def read_arrs map_arrs, show_location
|
148
134
|
map_arrs.map do |map_arr|
|
149
135
|
map_arr.map do |map|
|
@@ -157,7 +143,7 @@ class Ld::Sheet
|
|
157
143
|
end
|
158
144
|
end
|
159
145
|
|
160
|
-
|
146
|
+
# 作用 通过x,y坐标获取一个单元格的内容
|
161
147
|
def read_unit_by_xy x, y, parse
|
162
148
|
# puts "x: #{x}\ty: #{y}"
|
163
149
|
unit = @sheet.row(y)[x]
|
@@ -169,7 +155,7 @@ class Ld::Sheet
|
|
169
155
|
return unit
|
170
156
|
end
|
171
157
|
|
172
|
-
|
158
|
+
# 作用 判断要添加或要移除的是一行还是一列
|
173
159
|
def is_row? row
|
174
160
|
if row.to_i.to_s == row.to_s
|
175
161
|
return true
|
@@ -177,17 +163,17 @@ class Ld::Sheet
|
|
177
163
|
false
|
178
164
|
end
|
179
165
|
|
180
|
-
|
166
|
+
# 作用 打开一个sheet
|
181
167
|
def self.open excel, name
|
182
168
|
self.new excel, name, 'open'
|
183
169
|
end
|
184
170
|
|
185
|
-
|
171
|
+
# 作用 创建一个sheet
|
186
172
|
def self.create excel, name
|
187
173
|
self.new excel, name, 'new'
|
188
174
|
end
|
189
175
|
|
190
|
-
|
176
|
+
# 作用 将数据写入sheet
|
191
177
|
def save
|
192
178
|
point = parse_point @point
|
193
179
|
raise '保存sheet必须要有内容,请 set_rows' if !@rows
|
@@ -204,7 +190,7 @@ class Ld::Sheet
|
|
204
190
|
self
|
205
191
|
end
|
206
192
|
|
207
|
-
|
193
|
+
# 作用 解析一个字符串坐标(如'A1')返回x,y坐标('A1'返回[0,0])
|
208
194
|
def parse_point point
|
209
195
|
raise "无法解析excel坐标,坐标需要是String,不能是#{point.class.to_s}" if point.class != String
|
210
196
|
point.upcase!
|
@@ -215,7 +201,7 @@ class Ld::Sheet
|
|
215
201
|
{:character => characters[0], :number => numbers[0].to_i}
|
216
202
|
end
|
217
203
|
|
218
|
-
|
204
|
+
# 作用 在当前sheet中添加主体数据(传入二维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
|
219
205
|
def set_rows rows
|
220
206
|
raise '必须是一个数组且是一个二维数组' if rows.class != Array && rows.first.class != Array
|
221
207
|
@rows = rows
|
@@ -231,13 +217,13 @@ class Ld::Sheet
|
|
231
217
|
end
|
232
218
|
end
|
233
219
|
|
234
|
-
|
220
|
+
# 作用 在当前sheet的主体内容末尾添加一行数据(传入一维数组),但不写入(只有调用Ld::Excel的实例方法save才会写入io)
|
235
221
|
def insert_row row
|
236
222
|
raise 'insert_row 传入的必须是一个数组' if row.class != Array
|
237
223
|
@rows << row
|
238
224
|
end
|
239
225
|
|
240
|
-
|
226
|
+
# 作用 通过x,y坐标往一个单元格中写入数据,但不写入(只有调用Ld::Excel的实例方法save才会写入io)
|
241
227
|
def write_unit_by_xy x, y, unit
|
242
228
|
if unit.class == Array
|
243
229
|
unit = unit.to_s
|
data/lib/ld/file/file.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
class Ld::File
|
2
2
|
|
3
|
-
attr_accessor :path, :name, :exist, :size, :mode, :stat
|
3
|
+
attr_accessor :path, :name, :exist, :size, :mode, :type, :stat
|
4
4
|
@@current_path = Dir.pwd
|
5
5
|
@@exclude = ['.', '..']
|
6
6
|
|
7
|
-
#= 作用 创建一个Ld::File实例(不初始化参数)
|
8
7
|
def initialize path
|
9
8
|
@path = path[0] == '/' ? path : "#{Dir.pwd}/#{path}"
|
10
9
|
@name = File.basename @path
|
@@ -20,12 +19,12 @@ class Ld::File
|
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
23
|
-
#= 作用
|
22
|
+
#= 作用 打开一个文件
|
24
23
|
def self.open path
|
25
24
|
self.new path
|
26
25
|
end
|
27
26
|
|
28
|
-
|
27
|
+
#= 作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错
|
29
28
|
def children
|
30
29
|
dir!
|
31
30
|
Dir.foreach(@path).map{|n| Ld::File.new("#{@path}/#{n}") if !is_exclude? n}.compact.sort{|a,b| a.type <=> b.type}
|
@@ -44,10 +43,12 @@ class Ld::File
|
|
44
43
|
raise "不存在的 #{path}" if !@exist
|
45
44
|
end
|
46
45
|
|
46
|
+
#= 作用 判断这是目录吗
|
47
47
|
def dir?
|
48
48
|
type == 'directory'
|
49
49
|
end
|
50
50
|
|
51
|
+
#= 作用 判断这是文件吗
|
51
52
|
def file?
|
52
53
|
type == 'file'
|
53
54
|
end
|
@@ -60,13 +61,13 @@ class Ld::File
|
|
60
61
|
raise "这不是一个文件,而是一个#{type}:#{path}" if type != 'file'
|
61
62
|
end
|
62
63
|
|
63
|
-
#= 作用
|
64
|
+
#= 作用 查找文件或目录,返回一个一级目录或文件,如果不存在则返回nil
|
64
65
|
def find name
|
65
66
|
dir!
|
66
67
|
Ld::File.new "#{path}/#{name.to_s}" if File.exist? "#{path}/#{name.to_s}"
|
67
68
|
end
|
68
69
|
|
69
|
-
#= 作用
|
70
|
+
#= 作用 精确查找,返回所有匹配的目录和文件
|
70
71
|
def search name, type = :all
|
71
72
|
dir!
|
72
73
|
results = []
|
@@ -81,7 +82,7 @@ class Ld::File
|
|
81
82
|
end
|
82
83
|
end
|
83
84
|
|
84
|
-
#= 作用
|
85
|
+
#= 作用 模糊查找,返回所有匹配的目录和文件
|
85
86
|
def search_regexp regexp, type = :all
|
86
87
|
dir!
|
87
88
|
results = []
|
@@ -118,7 +119,7 @@ class Ld::File
|
|
118
119
|
end
|
119
120
|
end
|
120
121
|
|
121
|
-
#= 作用
|
122
|
+
#= 作用 如果是一个文本文件,返回所有行
|
122
123
|
def lines
|
123
124
|
File.open(@path).readlines
|
124
125
|
end
|
@@ -127,7 +128,7 @@ class Ld::File
|
|
127
128
|
# find name
|
128
129
|
# end
|
129
130
|
|
130
|
-
#= 作用
|
131
|
+
#= 作用 修改名称(目录或文件均可)
|
131
132
|
def rename new_name
|
132
133
|
new_path = "#{dir.path}/#{new_name}"
|
133
134
|
if File.rename @path, new_path
|
@@ -135,7 +136,7 @@ class Ld::File
|
|
135
136
|
end
|
136
137
|
end
|
137
138
|
|
138
|
-
#= 作用 删除当前文件
|
139
|
+
#= 作用 删除当前文件(有gets确认)
|
139
140
|
def delete
|
140
141
|
puts "删除!:#{path}\n,确认请输入 delete_file,"
|
141
142
|
if gets.chomp == 'delete_file'
|
@@ -146,7 +147,7 @@ class Ld::File
|
|
146
147
|
end
|
147
148
|
end
|
148
149
|
|
149
|
-
#= 作用 返回所有文件
|
150
|
+
#= 作用 返回所有文件
|
150
151
|
def files
|
151
152
|
children.select{|f| f.type == 'file'}
|
152
153
|
end
|
@@ -175,10 +176,6 @@ class Ld::File
|
|
175
176
|
end
|
176
177
|
end
|
177
178
|
|
178
|
-
def save
|
179
|
-
|
180
|
-
end
|
181
|
-
|
182
179
|
def self.write path, arr
|
183
180
|
File.open(path)
|
184
181
|
end
|
data/lib/ld/print/print.rb
CHANGED
@@ -2,17 +2,21 @@ require 'terminal-table'
|
|
2
2
|
|
3
3
|
class Ld::Print
|
4
4
|
|
5
|
+
#= 作用 格式化打印模型数组
|
5
6
|
def self.p models, fields
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
t = Terminal::Table.new
|
8
|
+
t.title = models.first.class.to_s
|
9
|
+
fields = (fields.class == Array ? fields : fields.split(',')).map{|f| f.rstrip.lstrip}
|
10
|
+
t.headings = fields
|
11
|
+
models.map { |model|
|
12
|
+
fields.map { |field|
|
13
|
+
value = model.send field
|
14
|
+
value = value.strftime("%Y/%m/%d %H:%M:%S") if [Date, Time, DateTime, ActiveSupport::TimeWithZone].include? value.class
|
15
|
+
value
|
16
|
+
}
|
17
|
+
}#.sort{|a,b| a[2] <=> b[2]}
|
18
|
+
.each{|row| t.add_row row}
|
19
|
+
puts t
|
16
20
|
end
|
17
21
|
|
18
22
|
def self.print hash
|
@@ -9,7 +9,7 @@ class Ld::Controllers
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def parse
|
12
|
-
@rows = @root.app
|
12
|
+
@rows = @root.find('app/controllers').search_regexp(/_controller.rb$/).map { |c|
|
13
13
|
model_name = c.name.split('_controller')[0].singularize
|
14
14
|
model_name = @models.models.include?(model_name) ? model_name : nil
|
15
15
|
lines = c.lines
|
data/lib/ld/project/models.rb
CHANGED
@@ -10,7 +10,7 @@ class Ld::Models
|
|
10
10
|
|
11
11
|
def parse
|
12
12
|
rows = []
|
13
|
-
model_files = @root.app
|
13
|
+
model_files = @root.find('app/models').search_regexp(/.rb$/).map{|m|
|
14
14
|
m if @tables.tables.include?(m.name.split('.')[0].pluralize)
|
15
15
|
}.compact
|
16
16
|
@rows = model_files.map{ |file|
|
data/lib/ld/project/project.rb
CHANGED
@@ -2,10 +2,11 @@ class Ld::Project
|
|
2
2
|
|
3
3
|
attr_accessor :root, :tables, :models, :controllers, :views, :routes, :table_hash
|
4
4
|
|
5
|
+
#= 作用 解析一个项目的代码获得结构化的数据
|
5
6
|
def initialize table_hash = {}, project_root_path = Rails.root.to_s
|
6
7
|
@root = Ld::File.new project_root_path
|
7
8
|
@table_hash = table_hash
|
8
|
-
@schema = @root.
|
9
|
+
@schema = @root.find('db/schema.rb')
|
9
10
|
raise "schema.rb文件不存在\n请运行命令(rake db:schema:dump)或手动添加此文件" if @schema.nil?
|
10
11
|
raise "schema.rb文件是空的\n请运行命令(rake db:schema:dump)或手动添加此文件" if @schema.lines.size == 0
|
11
12
|
parse_project
|
@@ -20,6 +21,7 @@ class Ld::Project
|
|
20
21
|
@controllers = Ld::Controllers.new @root, @models
|
21
22
|
end
|
22
23
|
|
24
|
+
#= 作用 查看模型的相关信息(参数有:relations,fields,tables,routes,views,controllers)
|
23
25
|
def print model_name, type = :relations
|
24
26
|
model_name = model_name.to_s
|
25
27
|
if !@models.models.include? model_name
|
@@ -98,6 +100,7 @@ class Ld::Project
|
|
98
100
|
@controllers.rows.delete_at 0
|
99
101
|
end
|
100
102
|
|
103
|
+
#= 作用 将这个项目的代码分析结果保存到excel文件(默认在项目根目录下的project.xls)
|
101
104
|
def to_xls path = {:file_path => "#{@root.path}/project.xls"}
|
102
105
|
Ld::Excel.create path do |excel|
|
103
106
|
excel.write_sheet 'routes' do |sheet|
|
data/lib/ld/project/tables.rb
CHANGED
data/lib/ld/project/views.rb
CHANGED
@@ -8,8 +8,8 @@ class Ld::Views
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def parse
|
11
|
-
@rows = @root.app
|
12
|
-
dir_name = v.
|
11
|
+
@rows = @root.find('app/views').search_regexp(/.html/).map{|v|
|
12
|
+
dir_name = v.parent.name
|
13
13
|
model_name = @models.models.include?(dir_name.singularize) ? dir_name.singularize : nil
|
14
14
|
[model_name,v.lines.size,dir_name,v.name,v.path]
|
15
15
|
}.sort{|a,b| b[1] <=> a[1]}
|
data/lib/ld/version.rb
CHANGED