cocoapods-bb-PodAssistant 0.3.8.0 → 0.3.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 591f0737b7cec4e7779d193eb7fa0b9822e1a7017b281580048c577383193bd2
4
- data.tar.gz: 3cf7f1112f2a31fef0fd2d2816d2a2e5b23932988a24722b8273b9dd4c47dd33
3
+ metadata.gz: fd0ad4c8d3ffd50eeacacccf5b14c1e06d90285e758cbe01582c03bdee76bccc
4
+ data.tar.gz: c1af2d87d63817d54a848d172126489009be2e43b946688c65a77341ee6f7fcf
5
5
  SHA512:
6
- metadata.gz: 296ee33991c46c0a91c888dc0452055d6462e92cf60fac5d5a06d1a3bb8e3f8d6cdf7aa046b3d7f9a274421c632695f5724be9b4fe1af435ef71e140f2d0f4de
7
- data.tar.gz: efaddedb930bdc695bd1b3e80b8b56e8e66d49d654187cb075f367423763939c2b65945ca50074abc1252ef9ded85955f7f025ca9b99a403584e97b9f39529f0
6
+ metadata.gz: 24724856a947cd524403bcf618286125d18655f01f5f5c219402ed03bd4e5afc0f3518f74be39937a42332cee0fbf4b1f59a8b0588ad66719c01c80828f76216
7
+ data.tar.gz: 8de6194b110fc57f7cf9c6d96c04ef7c53a0bc4bf78f399ad37e04ca1139243504a5396d5db0fa086c9db6b1d42e6932b32a5300eb9dabf4e5eaa1fd3a830b82
data/README.md CHANGED
@@ -57,4 +57,22 @@ pod stable update [组件名称] --sync
57
57
  * Build the component itself with a static framework
58
58
  ```
59
59
  pod 'xxx', :linkage => :static
60
- ```
60
+ ```
61
+
62
+ ## 可执行文件
63
+
64
+ * Tools工具集使用
65
+
66
+ ```
67
+ bb_tools --help
68
+ ```
69
+
70
+ * pp文件加密
71
+ ```
72
+ match_encrypt [password] [input_path] [output_path]
73
+ ```
74
+
75
+ * pp文件解密
76
+ ```
77
+ match_decrypt [password] [input_path] [output_path]
78
+ ```
data/bin/bb_tools ADDED
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gli'
3
+ require 'colored2'
4
+ require 'cocoapods-bb-PodAssistant'
5
+
6
+ include GLI::App
7
+
8
+ program_desc '内部命令行工具, 为iOS开发者提供的工具集合'
9
+ version CocoapodsBbPodassistant::VERSION
10
+
11
+ def usage
12
+ puts "USAGE: bb_tools command [args]".yellow
13
+ run(ARGV)
14
+ exit(-1)
15
+ end
16
+
17
+ desc "解析linkmap"
18
+ arg 'LinkMap-normal-arm64.txt'
19
+ command :parse do |c|
20
+ c.action do |global_options, options, args|
21
+ if args.size == 0
22
+ usage
23
+ elsif args.size == 1
24
+ BBItools::LinkMap.parser(args[0])
25
+ else
26
+ BBItools::LinkMap.parser_by_folder(args)
27
+ end
28
+ end
29
+ end
30
+
31
+ desc "在文件夹或文件中查找字符串(或者字符串组)"
32
+ arg 'xxx.txt'
33
+ command :search do |c|
34
+ c.action do |global_options, options, args|
35
+ BBItools::StringSearcher.search_result(args[0],args[1])
36
+ end
37
+ end
38
+ # 查找工程中没有用到的图片
39
+ desc "查找无用图片"
40
+ arg 'xxx.txt'
41
+ command :find do |c|
42
+ c.action do |global_options, options, args|
43
+ BBItools::ImgFinder.find(args[0])
44
+ end
45
+ end
46
+ # 查找Xcode工程中没有用到的类
47
+ desc "查找无用类"
48
+ arg 'xxx.txt'
49
+ command :search_unuse_class do |c|
50
+ c.action do |global_options, options, args|
51
+ BBItools::ClassFinder.search_unuse_class(args)
52
+ end
53
+ end
54
+
55
+ # 计算占用内存大小
56
+ desc "计算文件或者文件夹占用内存大小"
57
+ arg 'xxx.txt'
58
+ command :size_for do |c|
59
+ c.action do |global_options, options, args|
60
+ BBItools::Memory.sizeFor(args)
61
+ end
62
+ end
63
+
64
+ # 查找文件
65
+ desc "查找文件"
66
+ arg 'file name'
67
+ command :search_file do |c|
68
+ c.action do |global_options, options, args|
69
+ BBItools::FileSearcher.searchFile(args)
70
+ end
71
+ end
72
+
73
+ # 统计代码行数
74
+ desc "统计代码行数"
75
+ arg 'file name or folder'
76
+ command :count_code_line do |c|
77
+ c.action do |global_options, options, args|
78
+ BBItools::CodeCouner.count_line(args)
79
+ end
80
+ end
81
+
82
+ #本地commit规范化
83
+ desc "hook本地commit,然后进行规commit lint范化"
84
+ arg 'git项目根目录 or 可空'
85
+ command :lint do |c|
86
+ c.action do |global_options,options, args|
87
+ BBItools::GitSets.commit_msg_init(args)
88
+ end
89
+ end
90
+
91
+ #podfile.lock库平铺
92
+ desc "可以将podfile.lock中所有的依赖平铺至Podifle中"
93
+ arg 'podfile.lock'
94
+ command :podfile_tiled do |c|
95
+ c.action do |global_options,options, args|
96
+ BBItools::PodfileTiled.podfile_tiled(args)
97
+ end
98
+ end
99
+ exit run(ARGV)
@@ -8,4 +8,6 @@ require 'cocoapods-bb-PodAssistant/babybus/stable/podfile-linkline'
8
8
  require 'cocoapods-bb-PodAssistant/command/linkline'
9
9
  require 'cocoapods-bb-PodAssistant/babybus/linkline/target-linkline'
10
10
  require 'cocoapods-bb-PodAssistant/babybus/linkline/targetdefinition-linkline'
11
- require 'cocoapods-bb-PodAssistant/babybus/linkline/targetValidator-linkline'
11
+ require 'cocoapods-bb-PodAssistant/babybus/linkline/targetValidator-linkline'
12
+ # tool
13
+ require 'cocoapods-bb-PodAssistant/tools'
@@ -1,3 +1,3 @@
1
1
  module CocoapodsBbPodassistant
2
- VERSION = "0.3.8.0"
2
+ VERSION = "0.3.9.2"
3
3
  end
@@ -356,8 +356,20 @@ module BB
356
356
  stable_specs.push(data)
357
357
  end
358
358
  elsif pod.is_a? String
359
- # puts "===git===标签==> { names: [\"#{name}\"], version: #{pod}, method: REMOTE_TAG }".green
360
- stable_specs.push({ names: [name], version: pod, method: REMOTE_TAG })
359
+ # 这种情况pod 为版本, 需要从podfile_hash 查询额外信息
360
+ podInfo = podfile_hash.fetch(name, {})
361
+ linkages = podInfo.fetch(:linkages, "") # 如果 :linkages 不存在,返回空字符串
362
+ linkage = podInfo.fetch(:linkage, "") # 同上
363
+ if !linkages.empty?
364
+ # puts "===git===标签==> { podInfo: [\"#{podInfo}\"], linkages: #{linkages} }".green
365
+ stable_specs.push({ names: [name], version: pod, method: REMOTE_TAG, linkages: linkages})
366
+ elsif !linkage.empty?
367
+ # puts "===git===标签==> { podInfo: [\"#{podInfo}\"], linkage: #{linkage} }".green
368
+ stable_specs.push({ names: [name], version: pod, method: REMOTE_TAG, linkage: linkage})
369
+ else
370
+ # puts "===git===标签==> { podInfo: [\"#{podInfo}\"] }".green
371
+ stable_specs.push({ names: [name], version: pod, method: REMOTE_TAG })
372
+ end
361
373
  else
362
374
  puts "unknow type [#{name}] data:#{pod}".red
363
375
  end
@@ -0,0 +1,129 @@
1
+ require 'find'
2
+ module BBItools
3
+ class ClassFinder
4
+ attr_accessor :search_path, :classes, :search_in_files
5
+ def initialize(temp_search_path)
6
+ @search_path = temp_search_path
7
+ @classes = []
8
+ @search_in_files = []
9
+ end
10
+ def search
11
+ # 找到所有的.h以及所有要查找的文件
12
+ Find.find(@search_path) do |path|
13
+ if File.file?(path)
14
+ if !get_not_contain_file_ext.include?(File.extname(path))
15
+ @search_in_files << path
16
+ end
17
+
18
+ if File.extname(path).eql?(".h")
19
+ ff_result = ClassFindResult.new(File.basename(path,".h"),path)
20
+ @classes << ff_result
21
+ end
22
+ end
23
+ end
24
+
25
+ # 删除使用的文件
26
+ use_idxs = Set.new
27
+ @search_in_files.each{|s_file|
28
+ s_containet = ""
29
+ File.read(s_file).each_line do |line|
30
+ s_containet << line
31
+ s_containet << ","
32
+ end
33
+ # 查找所有文件在单个文件中是否被引用
34
+ @classes.each_with_index{|f_result,idx|
35
+ search_file_no_ext = get_no_ext_path(s_file)
36
+ check_file_no_ext = get_no_ext_path(f_result.fr_path)
37
+ # 判断是否是同一个文件或者是通文件的.m/.h,不是同一个文件才查找
38
+ if !check_file_no_ext.eql?(search_file_no_ext)
39
+ inheritance_str = ": #{f_result.fr_name}"
40
+ contain_str = '@"' + f_result.fr_name + '"'
41
+ reference_str = "#{f_result.fr_name}.h"
42
+
43
+ # if s_containet.match(/: #{f_result.fr_name}|@"#{f_result.fr_name}"|#{f_result.fr_name}.h/) != nil
44
+ if s_containet.include?(inheritance_str) or s_containet.include?(contain_str) or s_containet.include?(reference_str)
45
+ use_idxs << f_result
46
+ puts "#{f_result.fr_name}已使用,剩余查找文件数#{@classes.size - use_idxs.size}..."
47
+ end
48
+ end
49
+ }
50
+
51
+ }
52
+ final_result = []
53
+
54
+ temp_final_result_str = ''
55
+ use_idxs.to_a.each {|u_x|
56
+ temp_final_result_str << u_x.fr_name
57
+ temp_final_result_str << ","
58
+ }
59
+ @classes.delete_if {|find_result| temp_final_result_str.include?(find_result.fr_name) }
60
+ puts "\033[32m查找结束,共同无用文件#{@classes.size}个,如下:\033[0m"
61
+ Spreadsheet.client_encoding = 'utf-8'
62
+ book = Spreadsheet::Workbook.new
63
+ sheet1 = book.create_worksheet
64
+ sheet1.row(0)[0] = "序号"
65
+ sheet1.row(0)[1] = "文件名"
66
+ sheet1.row(0)[2] = "文件路径"
67
+ sheet1.row(0)[3] = "文件占用内存大小"
68
+ total_size = 0
69
+ @classes.each_with_index{|r_item,f_index|
70
+ # if !r_item.fr_name.include?("+")
71
+ total_size = total_size + File.size(r_item.fr_path)
72
+ # end
73
+ puts r_item.fr_name
74
+ sheet1.row(f_index+1)[0] = f_index + 1
75
+ sheet1.row(f_index+1)[1] = r_item.fr_name
76
+ sheet1.row(f_index+1)[2] = r_item.fr_path
77
+ sheet1.row(f_index+1).height = 20
78
+ }
79
+ sheet1.column(0).width = 4
80
+ sheet1.column(1).width = 45
81
+ sheet1.column(2).width = 100
82
+ book.write "#{@search_path}/search_unuseclass_result.xls"
83
+ puts "\033[32m文件已经保存到#{@search_path}/search_unuseclass_result.xls,无用文件#{@classes.size}个,预计可减少内存占用#{handleSize(total_size)}\033[0m"
84
+ end
85
+ # 大小格式化
86
+ def handleSize(size)
87
+ if size > 1024 * 1024
88
+ return format("%.2f",(size.to_f/(1024*1024))) + "MB"
89
+ elsif size > 1024
90
+ return format("%.2f",(size.to_f/1024)) + "KB"
91
+ else
92
+ return size.to_s + "B"
93
+ end
94
+ end
95
+ # 不包含后缀的路径
96
+ def get_no_ext_path(item)
97
+ return File.dirname(item) + "/" + File.basename(item,".*")
98
+ end
99
+ # 不需要查找的类
100
+ def get_not_contain_file_ext
101
+ nc_ext = [".jpg",".png",".md",".xls",".xcworkspace",".DS_Store",""]
102
+ return nc_ext
103
+ end
104
+ # 对外暴露
105
+ def self.search_unuse_class(args)
106
+ folder_path = args[0]
107
+ if folder_path.nil?
108
+ puts "\033[31m传入的参数不能为空\033[0m"
109
+ return
110
+ end
111
+ if !File::directory?(folder_path)
112
+ puts "\033[31m参数不是文件夹\033[0m"
113
+ return
114
+ end
115
+ class_finder = ClassFinder.new(folder_path)
116
+ class_finder.search
117
+ end
118
+ end
119
+
120
+ # ------------------------查找结果类------------------------
121
+
122
+ class ClassFindResult
123
+ attr_accessor :fr_name, :fr_path
124
+ def initialize(temp_name,temp_path)
125
+ @fr_name = temp_name
126
+ @fr_path = temp_path
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,55 @@
1
+ # 设置默认编码
2
+ Encoding.default_external = Encoding::UTF_8
3
+ Encoding.default_internal = Encoding::UTF_8
4
+
5
+ require 'find'
6
+ module BBItools
7
+ class CodeCouner
8
+ attr_accessor :file_path, :line_number
9
+ def initialize(path)
10
+ @file_path = path
11
+ @line_number = 0
12
+ end
13
+ # 统计行数
14
+ def calculate_line_number
15
+ puts "\033[33m正在统计#{@file_path} 代码行数,请稍后...\033[0m"
16
+ if File.file?(@file_path)
17
+ File.read(@file_path).each_line do |line|
18
+ if line.match(/^\/\/|^$/) == nil #去掉单行注释和空行
19
+ @line_number = @line_number + 1
20
+ end
21
+ end
22
+
23
+ return
24
+ end
25
+ if File::directory?(@file_path)
26
+ Find.find(@file_path) do |file|
27
+ if File.file?(file) #判断是否是文件
28
+ #只统计.h/.m/.mm/.cpp/.swift几个文件
29
+ # if File.extname(file).match(/^.[hm]m?$|.cpp|.swift/)
30
+ if File.extname(file).match(/\.(h|hpp|m|mm|cpp|swift)$/)
31
+ File.read(file).each_line do |line|
32
+ if line.match(/^\/\/|^$/) == nil #去掉单行注释和空行
33
+ @line_number = @line_number + 1
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ return
40
+ end
41
+ puts "\033[31m找不到指定路径的文件或者文件夹,请重新输入路径\033[0m"
42
+ end
43
+
44
+ def self.count_line(args)
45
+ file = args[0]
46
+ if file.nil?
47
+ puts "\033[31m参数异常,请传入一个参数(项目目录/要统计的文件目录/要统计的文件)\033[0m"
48
+ return
49
+ end
50
+ counter = CodeCouner.new(file)
51
+ counter.calculate_line_number
52
+ puts "\033[32m统计#{counter.file_path}结束,共#{counter.line_number}行\033[0m"
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,78 @@
1
+ require 'find'
2
+ require 'spreadsheet'
3
+ module BBItools
4
+ class FileResult
5
+ attr_accessor :keyword, :file_path, :file_name
6
+ def initialize(temp_path,temp_name)
7
+ @file_path = temp_path
8
+ @file_name = temp_name
9
+ end
10
+ end
11
+ class FileSearcher
12
+ # path:搜索的路径,files要搜索的文件,支持数组用逗号隔开即可。支持模糊搜索
13
+ attr_accessor :path ,:files, :search_result
14
+ def initialize(temp_path,temp_files)
15
+ @path = temp_path
16
+ @files = temp_files
17
+ @search_result = []
18
+ end
19
+ def search
20
+ puts "\033[32m开始查找...\033[0m"
21
+ if File::directory?(@path)
22
+ Find.find(@path) do |file|
23
+ if File.file?(file)
24
+ file_name = File.basename(file)
25
+ if file_name.include?(@files)
26
+ fr = FileResult.new(file,file_name)
27
+ @search_result << fr
28
+ end
29
+ else
30
+ # puts "查找#{file}..."
31
+ end
32
+ end
33
+ else
34
+ puts "\033[31m文件夹有误,请输入文件夹路径作为第一个参数\033[0m"
35
+ end
36
+ end
37
+ # 对外暴露方法
38
+ def self.searchFile(args)
39
+ path = args[0]
40
+ files = args[1]
41
+ if path.nil? || files.nil?
42
+ puts "\033[31m参数异常,请传入两个参数,第一个为路径,第二个为要搜索的文件名\033[0m"
43
+ return
44
+ end
45
+ # temp_files = files.split(",")
46
+ file_searcher = FileSearcher.new(path,files)
47
+ file_searcher.search
48
+ if file_searcher.search_result.size == 0
49
+ puts "\033[32m没有找到符合条件的文件\033[0m"
50
+ return
51
+ end
52
+ # 输出
53
+ # 输出搜索的内容
54
+
55
+ Spreadsheet.client_encoding = 'utf-8'
56
+ book = Spreadsheet::Workbook.new
57
+ sheet1 = book.create_worksheet
58
+ sheet1.row(0)[0] = "序号"
59
+ sheet1.row(0)[1] = "文件名"
60
+ sheet1.row(0)[2] = "文件所在路径"
61
+
62
+
63
+ puts "\033[32m找到共#{file_searcher.search_result.size}个文件结果如下;\033[0m"
64
+ file_searcher.search_result.each_with_index {|item,i|
65
+ puts item.file_name
66
+ sheet1.row(i+1)[0] = i + 1
67
+ sheet1.row(i+1)[1] = item.file_name
68
+ sheet1.row(i+1)[2] = item.file_path
69
+ sheet1.row(i+1).height = 20
70
+ }
71
+ sheet1.column(0).width = 4
72
+ sheet1.column(1).width = 45
73
+ sheet1.column(2).width = 100
74
+ book.write "#{File.dirname(path)}/search_#{files}_result.xls"
75
+ puts "\033[32m查找成功,共#{file_searcher.search_result.size}个文件,内容已经保存到#{File.dirname(path)}/search_#{files}_result.xls,请点击查看\033[0m"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,132 @@
1
+ require 'find'
2
+ require 'spreadsheet'
3
+ module BBItools
4
+ class FindResult
5
+ attr_accessor :name , :path
6
+ def initialize(name,path)
7
+ @name = name
8
+ @path = path
9
+ end
10
+
11
+ end
12
+ # --------------------------------------------
13
+ class ImgFinder
14
+ #
15
+ attr_accessor :image_count, :images, :unuse_images,:find_path
16
+ attr_accessor :search_files
17
+ def initialize
18
+ @image_count = 0
19
+ @images = []
20
+ @search_files = []
21
+ end
22
+ # 得到所有图片名称字符
23
+ def get_img_name_strs
24
+ result_arr = []
25
+ @images.each {|item|
26
+ item_name = Image.get_image_name(File.basename(item.name, ".*"))
27
+ result_arr << item_name
28
+ }
29
+ return result_arr
30
+ end
31
+ def get_image_path(image)
32
+ @images.each {|item|
33
+ if item.name.eql?(image)
34
+ return item.path
35
+ end
36
+ }
37
+ end
38
+ # 查找
39
+ def self.find(temp_find_dir)
40
+ imgFinder = ImgFinder.new
41
+ imgFinder.find_path = temp_find_dir
42
+ # 第一步:找到该文件夹下所有的图片文件
43
+ Find.find(temp_find_dir) do |filename|
44
+ if File.file?(filename) #如果是文件,则从文件中查找,忽略文件夹
45
+ if Image.is_image_format(File.extname(filename))
46
+ # p File.basename(filename)
47
+ # exit
48
+ imgFinder.image_count = imgFinder.image_count + 1
49
+ imageResult = FindResult.new(Image.get_image_name(File.basename(filename,".*")),filename)
50
+ imgFinder.images << imageResult
51
+ elsif File.extname(filename).eql?(".m") || File.extname(filename).eql?(".swift")
52
+ imgFinder.search_files << filename
53
+ end
54
+ end
55
+ end
56
+ if imgFinder.images.size == 0
57
+ puts "\033[32m查找成功,未发现图片\033[0m"
58
+ return
59
+ else
60
+ puts "\033[32m查找成功,共发现图片#{imgFinder.images.size}张\033[0m"
61
+ end
62
+ # 第二步:找到图片是否使用
63
+ imags = imgFinder.get_img_name_strs.uniq #要查找的图片名称数组
64
+
65
+ puts "\033[32m需要查找的图片有#{imags.size}张\033[0m"
66
+ # imgFinder.search_files #要查找的文件
67
+ imgFinder.search_files.each {|file|
68
+ File.read(file).each_line do |line|
69
+ haveStr = StringHandle.containsStr(line,imags)
70
+ if haveStr != -1
71
+ puts "#{imags[haveStr]}在使用...,剩余查找项#{imags.size-1}个"
72
+ imags.delete_at(haveStr)
73
+ end
74
+ end
75
+ }
76
+ puts "\033[32m无用图片#{imags.size}张,图片名称如下:\033[0m"
77
+ unuse_total_size = 0
78
+
79
+ Spreadsheet.client_encoding = 'utf-8'
80
+ book = Spreadsheet::Workbook.new
81
+ sheet1 = book.create_worksheet
82
+ sheet1.row(0)[0] = "文件名"
83
+ sheet1.row(0)[1] = "文件路径"
84
+ sheet1.row(0)[2] = "文件大小(B)"
85
+ imags.each_with_index {|item,idx|
86
+ sheet1.row(idx+1)[0] = item
87
+ path = imgFinder.get_image_path(item)
88
+ sheet1.row(idx+1)[1] = path
89
+ unuse_total_size = unuse_total_size + File.size(path)
90
+ sheet1.row(idx+1)[2] = File.size(path)
91
+ puts item
92
+ }
93
+ book.write "#{imgFinder.find_path}/search_result.xls"
94
+ puts "\033[32m文件已经保存到#{imgFinder.find_path}/search_result.xls,无用图片大小:#{unuse_total_size}B\033[0m"
95
+ puts "\033[32m内容仅供参考,具体还要自己通过结果查看一下\033[0m"
96
+ end
97
+ end
98
+ # 字符串操作类
99
+ class StringHandle
100
+ # originStr中是否包含targetStrs中的内容
101
+ def self.containsStr(originStr,targetStrs)
102
+ targetStrs.each_with_index {|item,idx|
103
+ if originStr.include?(item)
104
+ return idx
105
+ end
106
+ }
107
+ return -1
108
+ end
109
+ end
110
+ # ----------------------------
111
+ class Image
112
+
113
+ # 是否是图片格式,这里只判断了jpg、png和gif
114
+ def self.is_image_format(temp_ext_name)
115
+ if ['.jpg','.png','.gif'].include?(temp_ext_name)
116
+ return true
117
+ else
118
+ return false
119
+ end
120
+ end
121
+ def self.get_image_name(file)
122
+ return file.gsub(/@2x|@3x/,"")
123
+ end
124
+ end
125
+ # class ObjectiveC
126
+ # def self.is_h_file(temp_ext_name)
127
+ # if ['.h']
128
+
129
+ # end
130
+ # end
131
+ # end
132
+ end
@@ -0,0 +1,94 @@
1
+ require 'find'
2
+ module BBItools
3
+ class Memory
4
+ attr_accessor :pro
5
+ # 分发吹
6
+ def hand_cal_size(file,prop)
7
+ if prop.nil?
8
+ @pro = 1024
9
+ elsif prop == 0
10
+ @pro = 1024
11
+ else
12
+ @pro = prop
13
+ end
14
+ handle_method = ''
15
+ if File.file?(file)
16
+ puts "\033[32m开始计算文件的大小...\033[0m"
17
+ handle_method = 'cal_file'
18
+ elsif File::directory?(file)
19
+ handle_method = 'cal_folder'
20
+ puts "\033[32m开始计算文件夹的大小...\033[0m"
21
+ else
22
+ puts "\033[31m参数异常,请确保传入的第一个参数是文件路径或者文件夹路径\033[0m"
23
+ return
24
+ end
25
+ self.send(handle_method,file)
26
+ end
27
+ # 计算单个文件
28
+ def cal_file(file)
29
+ puts "\033[32m文件的大小为:#{get_show_size(File.size(file))}.\033[0m"
30
+ end
31
+ # 计算整个文件夹
32
+ def cal_folder(folder)
33
+ print "\033[32m请输入要查找文件后缀\033[0m(例如想文件夹中图片大小则输入:{png,jpg,gif},不输入则默认计算文件夹下所有文件大小之和):"
34
+ file_exts_string = STDIN.gets
35
+ file_exts_string.chomp! #过滤换行符
36
+ if file_exts_string.size == 0
37
+ file_exts = []
38
+ else
39
+ file_exts = file_exts_string.split(",")
40
+ end
41
+ sum = 0
42
+ file_count = 0
43
+ total_count = 0
44
+ total_size = 0
45
+ file_size = 0
46
+ Find.find(folder) do |filename|
47
+ if File.file?(filename)
48
+ total_count = total_count + 1
49
+ total_size = total_size + File.size(filename)
50
+ if file_exts.size == 0 #说明计算所有文件
51
+ sum = sum + File.size(filename)
52
+ file_count = file_count + 1
53
+ elsif file_exts.include?(File.extname(filename).delete(".")) #查找指定后缀的文件
54
+ sum = sum + File.size(filename)
55
+ file_count = file_count + 1
56
+ file_size = file_size + File.size(filename)
57
+ else
58
+ #不做任何处理
59
+ end
60
+ end
61
+ end
62
+ if file_exts.size == 0
63
+ puts "\033[32m文件夹中共#{total_count}个文件,共#{get_show_size(total_size)}(#{total_size})\033[0m"
64
+ else
65
+ puts "\033[32m文件夹中共#{total_count}个文件,共#{get_show_size(total_size)}(#{total_size});找到后缀为(#{file_exts_string})的文件#{file_count}个,共#{get_show_size(file_size)}(#{file_size}).\033[0m"
66
+ end
67
+
68
+ # puts `du -b #{folder} | awk '{print $1}'`.to_i
69
+ end
70
+ # get_show_size
71
+ def get_show_size(size)
72
+ if size > @pro * @pro * @pro
73
+ return format("%.2f",(size.to_f/(@pro*@pro*@pro))) + "GB"
74
+ elsif size > @pro * @pro
75
+ return format("%.2f",(size.to_f/(@pro*@pro))) + "MB"
76
+ elsif size > @pro
77
+ return format("%.2f",(size.to_f/@pro)) + "KB"
78
+ else
79
+ return size.to_s + "B"
80
+ end
81
+ end
82
+ # 对外暴露方法
83
+ def self.sizeFor(proport)
84
+ file = proport[0]
85
+ pro = proport[1].to_i
86
+ if file.nil?
87
+ puts "\033[31m参数异常,请传入一个参数\033[0m"
88
+ return
89
+ end
90
+ memory = Memory.new
91
+ memory.hand_cal_size(file,pro)
92
+ end
93
+ end
94
+ end