Class: BB::SwiftlintFilesHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-bb-PodAssistant/helpers/swiftlint_path_helper.rb

Constant Summary collapse

@@subspecs_info =

类变量存储所有subspecs信息

{}

Class Method Summary collapse

Class Method Details

.extract_subspec_real_paths(name, lib_path) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_path_helper.rb', line 108

def self.extract_subspec_real_paths(name, lib_path)

  return nil if @@subspecs_info.nil?

  path_list = @@subspecs_info[name]
  realPaths = []
  unless path_list.nil?
    path_list.each do | path |
      realPath = sanitize_path(path, lib_path)
      if realPath.nil? && path != nil && path.to_s.length > 0
        paths = extract_subspec_real_paths(path, lib_path)
        realPaths = realPaths | paths
      else
        # 不存在则添加
        unless realPaths.include?(realPath)
          realPaths << realPath
        end
      end
    end
  end
  return realPaths
end

.extract_subspecs(spec, parent_name = nil, lib_name = nil) ⇒ Object

获取组件subspecs路径



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
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_path_helper.rb', line 10

def self.extract_subspecs(spec, parent_name = nil, lib_name = nil)
  subspecs_info = {}

  full_name = parent_name ? "#{parent_name}/#{spec.name.split('/').last}" : spec.name
  source_files = spec.attributes_hash['source_files']

  info_list = subspecs_info[full_name]
  if info_list.nil?
    info_list = []
  end

  # 处理source_files路径
  if source_files.is_a?(String)
    info_list << source_files
  elsif source_files.is_a?(Array)
    info_list = info_list | source_files
  end

  # 处理dependency内部引用路径
  spec.dependencies.each do | dependency |
    dep_str = dependency.to_s
    # 检查依赖项是否匹配指定的库名
    if dep_str.start_with?(lib_name)
      info_list << dep_str
    end
  end

  unless info_list.empty?
    subspecs_info[full_name] = info_list if info_list
  end

  spec.subspecs.each do |subspec|
    subspecs_info.merge!(extract_subspecs(subspec, full_name, lib_name))
  end

  return subspecs_info
end

.find_first_with_suffix(directory, suffix) ⇒ Object

类方法:根据后缀递归查找文件或目录



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_path_helper.rb', line 155

def self.find_first_with_suffix(directory, suffix)
  # 检查目录是否存在
  unless Dir.exist?(directory)
    puts "目录 #{directory} 不存在"
    return nil
  end
  
  # 遍历目录下的所有文件和子目录
  Find.find(directory) do |path|
    # 如果是文件或目录且以给定后缀结尾,则返回其完整路径
    if (File.file?(path) || File.directory?(path)) && File.basename(path).end_with?(suffix)
      return path
    end
  end
  
  # 如果没有找到匹配项
  puts "未找到以 '#{suffix}' 结尾的文件或目录"
  nil
end

.parse_podspec(file_path) ⇒ Object



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
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_path_helper.rb', line 48

def self.parse_podspec(file_path)
  # Load the podspec file
  spec = Pod::Specification.from_file(file_path)
  spec_name = spec.attributes_hash['name']

  # Extract subspecs information
  subspecs_info = extract_subspecs(spec, nil, spec_name)

  default_subspecs = spec.attributes_hash['default_subspecs']

  # 处理默认引用
  if default_subspecs.is_a?(String)
    default_name = File.join(spec_name, default_subspecs)
    info_list = subspecs_info[default_name]
    subspecs_info[spec_name] = info_list if info_list
  elsif default_subspecs.is_a?(Array)
    default_list = []
    default_subspecs.each do | name |
      default_name = File.join(spec_name, name)
      info_list = subspecs_info[default_name]
      if info_list.is_a?(Array) && !info_list.empty
        default_list = default_list | info_list
      end
    end
    unless default_list.empty
      subspecs_info[spec_name] = default_list if default_list
    end
  end

  return subspecs_info
end

.query_file_path(name, lib_path) ⇒ Object

查询路径



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_path_helper.rb', line 132

def self.query_file_path(name, lib_path)

  return nil if name.nil? || lib_path.nil?

  if @@subspecs_info[name].nil?
    podspec_path = find_first_with_suffix(lib_path, ".podspec")
    unless podspec_path.nil?
      info = parse_podspec(podspec_path)
      if @@subspecs_info.nil?
        @@subspecs_info = info
      else
        @@subspecs_info.merge!(info) { |key, oldval, newval| 
          # 如果键对应的值也是哈希,则可以递归合并
          oldval.is_a?(Hash) && newval.is_a?(Hash) ? oldval.merge(newval) : newval 
        }
      end
    end
  end

  return extract_subspec_real_paths(name, lib_path)
end

.sanitize_path(path, lib_path) ⇒ Object

获取真正路径



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_path_helper.rb', line 81

def self.sanitize_path(path, lib_path)

  return nil if path.nil? || lib_path.nil?

  real_path = path.dup # 创建副本以避免修改原始数据
  # 去除 *.{...} 或 **/*.{...} 等文件匹配模式
  real_path.gsub!(%r{\*\.(\{\w+(,\w+)*\}|)}, "") # 匹配 *.{ext} 或 *.{}
  real_path.gsub!(%r{/\*\*/}, "")                # 匹配 /**/
  real_path.gsub!(%r{\*}, "")                    # 匹配单独的 *

  # 如果路径不以斜杠结尾,则添加斜杠
  real_path += '/' unless real_path.end_with?('/')

  # 去除多余的斜杠(如果有)
  real_path.squeeze!('/')

  # 去除末尾的多余斜杠
  real_path.chomp!('/') if real_path != '/'

  file_path = File.join(lib_path, real_path)
  if File.exist?(file_path)
    return file_path
  end

  return nil
end