ld 0.1.9 → 0.1.10

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95e4523070a48dbd2dc6568e350e1c52a299b2f8
4
- data.tar.gz: 1f0612955ad5d9cf130146b9ab48a287fab6a1c8
3
+ metadata.gz: eef66e56fee081ece10f99a43b3b60d0bb8a6edc
4
+ data.tar.gz: e7d95cf09c210ad2a82f69cf75b42b3e1542147e
5
5
  SHA512:
6
- metadata.gz: 28fb5eeeaba95fb117dedab6e131571563c331eace945aca1c4b90604eb542acfede33f694b84b0d7b1318197baa3cdcd4596fd4001b19e43cf3467674e8506b
7
- data.tar.gz: e031936fa73380e1d80a0c063cfd4930fd37d36ea8250d40ef7c893a7498f14a382b51c82e8075dccc7b02069874e8c32df7249b80a1b5b500851eeb9d444fac
6
+ metadata.gz: 4cbd1a4069f61e0448a3f6b180f707cb07ef59188505082b5397a25f76b6a1f75d8d0ce09084310221e3a5e949094d30d8fc8baf7210b2f15305cf076f9fddfc
7
+ data.tar.gz: c74cfc450556980ea3e1270854717de4644b7fae4d1e3a972788f60b65d0844a7adae29f2f7fb979fdb25c161ee4c8f4a3f3768abe61d29017ddd32a0c9c5064
data/lib/ld/file.rb CHANGED
@@ -30,6 +30,40 @@ class Ld::File
30
30
  arr
31
31
  end
32
32
 
33
+ def search_files regexp
34
+ arr = []
35
+ iter_search_files regexp, arr
36
+ arr
37
+ end
38
+
39
+ def search_dirs
40
+ arr = []
41
+ iter_search_dir arr
42
+ arr
43
+ end
44
+
45
+ def iter_search_dir arr
46
+ children.each do |f|
47
+ if f.type == 1
48
+ arr << f
49
+ f.iter_search_dir arr
50
+ end
51
+ end
52
+ self
53
+ end
54
+
55
+ def iter_search_files regexp, arr
56
+ children.each do |f|
57
+ if f.type == 1
58
+ f.iter_search_files regexp, arr
59
+ end
60
+ if f.name.match(regexp)
61
+ arr << f
62
+ end
63
+ end
64
+ self
65
+ end
66
+
33
67
  def father
34
68
  arr = @path.split('/')
35
69
  arr.pop
@@ -54,40 +88,6 @@ class Ld::File
54
88
  File.open(@path).readlines
55
89
  end
56
90
 
57
- def where regexp
58
- arr = []
59
- iter_search regexp, arr
60
- arr
61
- end
62
-
63
- def search regexp
64
- arr = []
65
- iter_search regexp, arr
66
- arr
67
- end
68
-
69
- def iter_search regexp, arr
70
- children.each do |f|
71
- if f.type == 1
72
- f.iter_search regexp, arr
73
- end
74
- if f.name.match(regexp)
75
- arr << f
76
- end
77
- end
78
- self
79
- end
80
-
81
- def iter arr
82
- children.each do |f|
83
- if f.type == 1
84
- f.iter arr
85
- end
86
- arr << f
87
- end
88
- self
89
- end
90
-
91
91
  def size
92
92
  File.size path
93
93
  end
data/lib/ld/files.rb ADDED
@@ -0,0 +1,18 @@
1
+ class Ld::Files < Array
2
+
3
+ attr_accessor :results
4
+
5
+ def initialize files
6
+ @results = files
7
+ end
8
+
9
+ def find name
10
+ @results.each do |f|
11
+ if f.name == name
12
+ return f
13
+ end
14
+ end
15
+ nil
16
+ end
17
+
18
+ end
data/lib/ld/project.rb CHANGED
@@ -1,15 +1,130 @@
1
1
  class Ld::Project
2
2
 
3
- attr_accessor :root, :app, :name, :path, :models, :views_dir, :views, :controllers, :routes
3
+ attr_accessor :app, :name, :models, :views_dir, :views, :controllers, :routes
4
4
 
5
- def initialize path
6
- @root = Ld::File.new(path)
7
- @app = @root.app
8
- @models = @app.models.search(/.rb$/)
9
- @views_dir = @app.views.children
10
- @views = @app.views.search(/.html/)
11
- @controllers = @app.controllers.search(/_controller.rb$/)
12
- @routes = @root.config.find('routes.rb')
5
+ def initialize
6
+ @@root = Ld::File.new Rails.root.to_s
7
+ system "rake routes > #{@@root.config.path}/routes.txt"
8
+ Ld::Excel.create "#{@@root.config.path}/project.xls" do |excel|
9
+ excel.write_sheet 'routes' do |sheet|
10
+ sheet.set_format({color: :black, font_size: 14, font: '微软雅黑'})
11
+ sheet.set_headings ['控制器', 'action', '请求类型','URI','帮助方法']
12
+ sheet.set_point 'a1'
13
+ sheet.set_rows @@root.config.find('routes.txt').lines
14
+ .map{|line|
15
+ arr = line.split(' ')
16
+ arr.unshift(nil) if arr.size == 3
17
+ arr
18
+ }
19
+ .delete_if{|arr| arr.size == 5 }
20
+ .map{|row|
21
+ controller, action = row[3].split('#')
22
+ type = row[1]
23
+ help_method = row[0]
24
+ uri = row[2]
25
+ [controller, action, type, uri, help_method]
26
+ }
27
+ end
28
+
29
+ excel.write_sheet 'models' do |sheet|
30
+ sheet.set_format({color: :black, font_size: 14, font: '微软雅黑'})
31
+ sheet.set_point 'a1'
32
+
33
+ @models = @@root.app.models.search_files(/.rb$/)
34
+ @controllers = @@root.app.controllers.search_files(/_controller.rb$/)
35
+ @views = @@root.app.views.search_dirs
36
+
37
+ sheet.set_rows @models.map { |model_file|
38
+ model_name = model_file.name.split('.')[0]
39
+ model_lines = model_file.lines
40
+ actions_full_name = model_lines.map{|l| l.split('def ')[1] if l.match(/def /)}.compact
41
+ actions = actions_full_name.map{|action| action.split(' ')[0]}
42
+ model_instance = eval("#{model_name.camelize}.new")
43
+ fields = model_instance.attributes.keys
44
+
45
+
46
+ controller = find_controller model_name.pluralize
47
+ if controller
48
+ controller_lines = controller.lines
49
+ controller_methods = controller_lines.map{|l| l.split('def ')[1] if l.match(/def /)}.compact
50
+ end
51
+
52
+ view = find_view model_name.pluralize
53
+ if view
54
+ views = view.search_files(/.html/)
55
+ end
56
+
57
+ [
58
+ model_name, # 模型
59
+ model_name.camelize, # 类
60
+ model_lines.size, # 模型lines
61
+ model_file.path, # 模型文件
62
+ (controller.nil? ? '' : controller.path), # 控制器文件
63
+ (controller.nil? ? 0 : controller_lines.size),# 控制器lines
64
+ actions.size, # 模型方法size
65
+ actions.join(','), # 模型方法
66
+ fields.size, # 字段size
67
+ fields.join(','), # 字段
68
+ (views.nil? ? 0 : views.size), # 视图size
69
+ (views.nil? ? '' : views.map{|v| "#{v.name.split('.')[0]}-#{v.path}"}.join(',')), # 视图
70
+ (controller_methods.nil? ? 0 : controller_methods.size), # action-size
71
+ (controller_methods.nil? ? '' : controller_methods.join(',')) # actions
72
+ ]
73
+ }.sort{|a,b| b[2] <=> a[2]} # 按 模型文件行数 排序
74
+
75
+ sheet.set_row []
76
+ sheet.set_row []
77
+ sheet.set_headings ['模型','类',
78
+ '模型lines','模型文件',
79
+ '控制器文件','控制器lines',
80
+ '模型方法size','模型方法',
81
+ '字段size','字段',
82
+ '视图size', '视图',
83
+ 'action-size','actions']
84
+ end
85
+ end
86
+ end
87
+
88
+ def find_controller model_name
89
+ @controllers.each do |c|
90
+ if c.name.split('_controller.rb')[0] == model_name
91
+ return c
92
+ end
93
+ end
94
+ nil
95
+ end
96
+ def find_view model_name
97
+ @views.each do |v|
98
+ if v.name == model_name
99
+ return v
100
+ end
101
+ end
102
+ nil
103
+ end
104
+ def camelize name
105
+ name.camelize
106
+ end
107
+ def name
108
+ @@root.name
109
+ end
110
+ def path
111
+ @@root.path
112
+ end
113
+
114
+
115
+ def self.save_info
116
+ @@p ||= Ld::Project.new(Rails.root.to_s)
117
+
118
+ end
119
+
120
+ def self.get_routes
121
+ @@p ||= Ld::Project.new(Rails.root.to_s)
122
+ file = Ld::File.new @@p.config.path + '/routes.txt'
123
+ system "rake routes > #{file.path}"
124
+ file.lines.map{|line| arr = line.split(' '); arr.size == 3 ? arr.unshift(nil) : arr}
125
+ t.headings = ['controller', 'action', 'type']
126
+ arrs.map{|arr| controller,action = arr[3].split('#'); [controller, action, arr[1]]}
127
+ .each{|arr| t.add_row arr}
13
128
  end
14
129
 
15
130
  def self.p model = :all
@@ -18,7 +133,7 @@ class Ld::Project
18
133
  t = Terminal::Table.new
19
134
  case model.to_s
20
135
  when 'all'
21
- t.title = "project:#{@@p.root.name}"
136
+ t.title = "project:#{@@root.name}"
22
137
  t.headings = ['models', 'views', 'controllers', 'routes']
23
138
  t.add_row [@@p.models.size, @@p.views.size, @@p.controllers.size, @@p.routes.lines.size]
24
139
  when 'models'
@@ -39,9 +154,9 @@ class Ld::Project
39
154
  .sort{|a,b| b[1]-a[1]}
40
155
  .each{|arr| t.add_row arr}
41
156
  when 'routes'
42
- file = Ld::File.new @@p.root.path + '/routes.txt'
157
+ file = Ld::File.new @@root.path + '/routes.txt'
43
158
  if !file.exist?
44
- system "rake routes > #{@@p.root.path + '/routes.txt'}"
159
+ system "rake routes > #{@@root.path + '/routes.txt'}"
45
160
  end
46
161
  arrs = file.lines.map{|l| lines = l.split(' '); lines.size == 3 ? lines.unshift(nil) : lines}
47
162
  arrs.delete_at 0
@@ -0,0 +1,3 @@
1
+ module SearchModule
2
+
3
+ end
data/lib/ld/sheet.rb CHANGED
@@ -38,7 +38,7 @@ class Ld::Sheet
38
38
  end
39
39
  end
40
40
 
41
- def add_row row
41
+ def set_row row
42
42
  raise 'add_row 传入的必须是一个数组' if row.class != Array
43
43
  @rows << row
44
44
  end
data/lib/ld/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ld
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.10"
3
3
  end
data/lib/ld.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require "ld/version"
2
2
 
3
3
  module Ld
4
-
5
4
  end
6
5
 
6
+ require "ld/search_module"
7
+ require "ld/file"
8
+
7
9
  require "ld/excel"
8
10
  require "ld/sheet"
9
- require "ld/file"
10
- require "ld/tree"
11
11
  require "ld/table"
12
12
  require "ld/project"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ld
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Dong
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-28 00:00:00.000000000 Z
11
+ date: 2017-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: terminal-table
@@ -95,12 +95,11 @@ files:
95
95
  - lib/ld/dir.rb
96
96
  - lib/ld/excel.rb
97
97
  - lib/ld/file.rb
98
- - lib/ld/node.rb
99
- - lib/ld/nodes.rb
98
+ - lib/ld/files.rb
100
99
  - lib/ld/project.rb
100
+ - lib/ld/search_module.rb
101
101
  - lib/ld/sheet.rb
102
102
  - lib/ld/table.rb
103
- - lib/ld/tree.rb
104
103
  - lib/ld/version.rb
105
104
  homepage: https://github.com/18810625123/ld
106
105
  licenses:
data/lib/ld/node.rb DELETED
@@ -1,13 +0,0 @@
1
- class Ld::Node
2
- attr_accessor :id, :depth, :name, :path ,:type, :suffix
3
-
4
- def initialize id, depth, path
5
- @id = id
6
- @depth = depth
7
- @path = path
8
- @type = File.directory?(path) ? 1 : 0
9
- @name = File.basename path
10
- @suffix = @type == 1 ? nil : @name.split('.').last
11
- end
12
-
13
- end
data/lib/ld/nodes.rb DELETED
@@ -1,33 +0,0 @@
1
- class Ld::Nodes
2
- attr_accessor :nodes
3
-
4
- def initialize nodes = []
5
- @nodes = nodes
6
- end
7
-
8
- def << node
9
- @nodes << node
10
- end
11
-
12
- def where option
13
- sql = ""
14
- if option.instance_of?(String)
15
- sql = option
16
- elsif option.instance_of?(Hash)
17
- sql = option.map{|k,v| "node.#{k} == #{v.instance_of?(String) ? "'#{v}'" : "#{v}"}"}.join(" and ")
18
- end
19
- @nodes.map{|node| if(eval(sql));node;end}.compact
20
- end
21
-
22
- def find id
23
- @nodes.each{|node| return node if node.id == id}
24
- end
25
-
26
- def size
27
- @nodes.size
28
- end
29
-
30
- def sort_by_depth
31
- @nodes.sort{|a,b| a.depth - b.depth}
32
- end
33
- end
data/lib/ld/tree.rb DELETED
@@ -1,39 +0,0 @@
1
- class Ld::Tree
2
- attr_accessor :root_path, :root_name, :nodes, :depth, :id
3
-
4
- def initialize root_path
5
- @root_path = root_path
6
- @root_name = File.basename root_path
7
- @nodes = Ld::Nodes.new
8
- @numbers = 0
9
- @depth = 0
10
- @id = 0
11
- end
12
-
13
- def read_tree path = @root_path
14
- @depth+=1
15
- Dir.foreach(path)do |p|
16
- if !p.match(/^\./)
17
- node = Ld::Node.new(@id+=1, @depth, "#{path}/#{p}")
18
- @nodes << node
19
- if node.type == 1
20
- read_tree node.path
21
- end
22
- end
23
- end
24
- end
25
-
26
- def print_tree
27
- puts "#{@root_name}:"
28
- @nodes.sort_by_depth.each do |node|
29
- if node.type == 1
30
- print_tree
31
- else
32
- puts "\t"*node.id + "#{node.type}:#{node.name}"
33
- end
34
- end
35
- end
36
-
37
-
38
-
39
- end