Class: MapTool::Dependency

Inherits:
Command
  • Object
show all
Defined in:
lib/maptool/dependency/dependency.rb

Overview

Usage

Constant Summary

Constants inherited from Command

Command::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Dependency

Returns a new instance of Dependency.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/maptool/dependency/dependency.rb', line 32

def initialize(argv)
    super
    self.dir_array = argv.option('dir')
    self.output = argv.option('output')
    if self.dir_array
        self.dir_array = self.dir_array.split(",").reject {|i|
            i.empty? || !Dir.exist?(i)
        }
    end
    self.output = Dir.pwd unless self.output
    self.output << "/MapToolDependency.log"
    self.ignore_frameworks = ["AppKit", "Foundation", "UIKit", "WatchKit"]
end

Instance Attribute Details

#dir_arrayObject

目录



28
29
30
# File 'lib/maptool/dependency/dependency.rb', line 28

def dir_array
  @dir_array
end

#ignore_frameworksObject

忽略的外部库



30
31
32
# File 'lib/maptool/dependency/dependency.rb', line 30

def ignore_frameworks
  @ignore_frameworks
end

#outputObject

输出地址



29
30
31
# File 'lib/maptool/dependency/dependency.rb', line 29

def output
  @output
end

Class Method Details

.optionsObject



23
24
25
26
# File 'lib/maptool/dependency/dependency.rb', line 23

def self.options
    [['--dir', '库路径,多个库用’,‘号分割'],
    ['--output', '日志存放路径,默认当前路径']] + super
end

Instance Method Details

#parsing_class(products) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/maptool/dependency/dependency.rb', line 104

def parsing_class(products)
    puts "\n分析文件内容...".green
    log = ""
    all_frameworks = []
    for project in products.values
        puts project.name
        templog = ""
        hlog = self.parsing_content(project.h_path, all_frameworks, products) if project.h_path
        templog << hlog if hlog
        mlog = self.parsing_content(project.m_path, all_frameworks, products) if project.m_path
        templog << mlog if mlog
        mmlog = self.parsing_content(project.mm_path, all_frameworks, products) if project.mm_path
        templog << mmlog if mmlog
        log << "\n#{project.name}\n#{templog}" unless templog.empty?
    end
    all_frameworks.uniq!
    all_frameworks.sort!
    return "外部依赖汇总:#{all_frameworks.size} #{all_frameworks.to_s}\n\n" + log
end

#parsing_content(file, all_frameworks, products) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/maptool/dependency/dependency.rb', line 124

def parsing_content(file, all_frameworks, products)
    frameworks = []
    warning = []
    error = []
    File.open(file, "r") { |file|
        while line = file.gets
            if line.include?("#import")
                if line.include?("<")
                    import = line[/<.*>/][/<.*\//]
                    if import
                        import[0] = ""
                        import[-1] = ""
                        frameworks << import unless self.ignore_frameworks.include?(import)
                    else
                         error << line.chomp
                    end
                elsif line.include?(".h\"")
                    import = line[/".*.h"/]
                    import[0] = ""
                    import[-1] = ""
                    warning << import unless products[import[0..import.size-3]]
                end
            elsif line.include?("@protocol") || line.include?("@interface") || line.include?("@implementation")
                break
            end
        end
    }
    all_frameworks.concat(frameworks)
    frameworks.uniq!
    frameworks.sort!
    if !frameworks.empty? || !warning.empty? || !error.empty?
        log = "#{file}\n"
        log << "外部依赖:#{frameworks.to_s}\n" unless frameworks.empty?
        log << "错误依赖:#{error.to_s}\n" unless error.empty?
        log << "不合理依赖:#{warning.to_s}\n" unless warning.empty?
        return log
    end
    return nil
end

#parsing_files(files) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/maptool/dependency/dependency.rb', line 81

def parsing_files(files)
    puts "\n分析文件...".green
    hash = {}
    for file in files
        puts file
        fileName = File.basename(file).split(".").first
        product_class = hash[fileName]
        unless product_class
            product_class = ProductClass.new(fileName)
            hash[fileName] = product_class
        end
        case File.extname(file)
        when ".h"
            product_class.h_path = file
        when ".m"
            product_class.m_path = file
        when ".mm"
            product_class.mm_path = file
        end
    end
    return hash
end

#runObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/maptool/dependency/dependency.rb', line 54

def run
    log = "MapTool 代码依赖分析\n"
    files = self.search_files
    log << "文件数:#{files.size}\n"
    products = self.parsing_files(files)
    log << "class数:#{products.size}\n"
    log << self.parsing_class(products)
    puts "\n分析完毕,打开日志:#{self.output}".green
    File.write(self.output, log)
    `open #{self.output}`
end

#search_filesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/maptool/dependency/dependency.rb', line 66

def search_files
    puts "\n搜索文件...".green
    files = []
    for dir in self.dir_array do
        Dir.chdir(dir) {
            puts dir
            for path in Dir["**/*.{h,m,mm}"] do
                files << "#{dir}/#{path}"
            end
        }
    end
    files.uniq!
    files.sort!
end

#validate!Object



46
47
48
49
50
51
52
# File 'lib/maptool/dependency/dependency.rb', line 46

def validate!
    super
    unless self.dir_array && !self.dir_array.empty?
        puts "库路径为空或不存在".red
        self.banner!
    end
end