Method: Ld::Project#initialize

Defined in:
lib/ld/project.rb

#initializeProject

Returns a new instance of Project.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ld/project.rb', line 5

def initialize
  @@root = Ld::File.new Rails.root.to_s
  system "rake routes > #{@@root.config.path}/routes.txt"
  Ld::Excel.create "#{@@root.config.path}/project.xls" do |excel|
    excel.write_sheet 'routes' do |sheet|
      sheet.set_format({color: :black, font_size: 14, font: '微软雅黑'})
      sheet.set_headings ['控制器', 'action', '请求类型','URI','帮助方法']
      sheet.set_point 'a1'
      sheet.set_rows @@root.config.find('routes.txt').lines
                         .map{|line|
                       arr = line.split(' ')
                       arr.unshift(nil) if arr.size == 3
                       arr
                     }
                         .delete_if{|arr| arr.size == 5 }
                         .map{|row|
                       controller, action = row[3].split('#')
                       type        = row[1]
                       help_method = row[0]
                       uri         = row[2]
                       [controller, action, type, uri, help_method]
                     }
    end

    excel.write_sheet 'models' do |sheet|
      sheet.set_format({color: :black, font_size: 14, font: '微软雅黑'})
      sheet.set_point 'a1'

      @models = @@root.app.models.search_files(/.rb$/)
      @controllers = @@root.app.controllers.search_files(/_controller.rb$/)
      @views = @@root.app.views.search_dirs

      sheet.set_rows @models.map { |model_file|
        model_name    = model_file.name.split('.')[0]
        model_lines   = model_file.lines
        actions_full_name = model_lines.map{|l| l.split('def ')[1] if l.match(/def /)}.compact
        actions = actions_full_name.map{|action| action.split(' ')[0]}
        model_instance = eval("#{model_name.camelize}.new")
        fields = model_instance.attributes.keys


        controller = find_controller model_name.pluralize
        if controller
          controller_lines = controller.lines
          controller_methods = controller_lines.map{|l| l.split('def ')[1] if l.match(/def /)}.compact
        end

        view = find_view model_name.pluralize
        if view
          views = view.search_files(/.html/)
        end

        [
            model_name,             # 模型
            model_name.camelize,     # 类
            model_lines.size,       # 模型lines
            model_file.path,        # 模型文件
            (controller.nil? ? '' : controller.path),     # 控制器文件
            (controller.nil? ? 0 : controller_lines.size),# 控制器lines
            actions.size,           # 模型方法size
            actions.join(','),      # 模型方法
            fields.size,          # 字段size
            fields.join(','),   # 字段
            (views.nil? ? 0 : views.size), # 视图size
            (views.nil? ? '' : views.map{|v| "#{v.name.split('.')[0]}-#{v.path}"}.join(',')), # 视图
            (controller_methods.nil? ? 0 : controller_methods.size),  # action-size
            (controller_methods.nil? ? '' : controller_methods.join(',')) # actions
        ]
      }.sort{|a,b| b[2] <=> a[2]}  # 按 模型文件行数 排序

      sheet.set_row []
      sheet.set_row []
      sheet.set_headings ['模型','类',
                          '模型lines','模型文件',
                          '控制器文件','控制器lines',
                          '模型方法size','模型方法',
                          '字段size','字段',
                          '视图size', '视图',
                          'action-size','actions']
    end
  end
end