class MapTool::Dependency

Usage

Attributes

dir_array[RW]
ignore_frameworks[RW]
output[RW]

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# 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
options() click to toggle source
Calls superclass method MapTool::Command::options
# File lib/maptool/dependency/dependency.rb, line 23
def self.options
    [['--dir', '库路径,多个库用’,‘号分割'],
    ['--output', '日志存放路径,默认当前路径']] + super
end

Public Instance Methods

parsing_class(products) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
run() click to toggle source
# 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_files() click to toggle source
# 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!() click to toggle source
Calls superclass method
# 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