Class: BB::SwiftlintManager
- Inherits:
-
Object
- Object
- BB::SwiftlintManager
- Defined in:
- lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb
Instance Method Summary collapse
-
#cocoapods_script_version(script_path) ⇒ Object
获取远端script脚本版本.
-
#find_xcodeproj_file ⇒ Object
查找.xcodeproj文件.
-
#get_script_version(script_content) ⇒ Object
获取script脚本版本.
-
#initialize ⇒ SwiftlintManager
constructor
A new instance of SwiftlintManager.
-
#swiftlint_from_module(path) ⇒ Object
写入路径到swiftlint文件.
-
#update_swiftlint_script ⇒ Object
更新script脚本.
Constructor Details
#initialize ⇒ SwiftlintManager
Returns a new instance of SwiftlintManager.
9 10 11 12 13 |
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 9 def initialize() update_swiftlint_file() update_swiftlint_script() update_swiftlint_gitignore() end |
Instance Method Details
#cocoapods_script_version(script_path) ⇒ Object
获取远端script脚本版本
275 276 277 278 279 280 281 282 |
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 275 def cocoapods_script_version(script_path) unless File.exist?(script_path) return nil end script_content = File.read(script_path) return get_script_version(script_content) end |
#find_xcodeproj_file ⇒ Object
查找.xcodeproj文件
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 160 def find_xcodeproj_file() file_extension = '.xcodeproj' dir_path = Pathname.pwd unless Dir.exist?(dir_path) # puts "目录 '#{dir_path}' 不存在" return nil end # 遍历当前目录下的所有文件和目录 found_file = nil Dir.foreach(dir_path) do |entry| # 跳过 '.' 和 '..' 目录 next if entry == '.' || entry == '..' # 构造完整路径 full_path = File.join(dir_path, entry) # 检查是否为文件以及是否以指定后缀结尾 if File.directory?(full_path) && entry.end_with?(file_extension) found_file = full_path break # 找到第一个匹配项后退出循环 end end return found_file end |
#get_script_version(script_content) ⇒ Object
获取script脚本版本
285 286 287 288 289 290 291 292 293 |
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 285 def get_script_version(script_content) # 使用正则表达式匹配本地脚本中的 SCRIPT_VERSION 字段 if script_content =~ /SCRIPT_VERSION=['"]([^'"]+)['"]/ local_script_version = $1 return local_script_version end return nil end |
#swiftlint_from_module(path) ⇒ Object
写入路径到swiftlint文件
16 17 18 |
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 16 def swiftlint_from_module(path) write_swiftlint_included(path) end |
#update_swiftlint_script ⇒ Object
更新script脚本
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 296 def update_swiftlint_script() project_path = find_xcodeproj_file() if project_path.nil? puts "#{swiftlint_tag} 未找到 .xcodeproj工程文件" return end cache_script_path = cocoapods_script_path() cache_script_version = cocoapods_script_version(cache_script_path) if cache_script_version.nil? # puts "远端#{sh_script_name}不存在: #{cache_script_path}" return end # puts "远端#{sh_script_name}版本号:#{cache_script_version}" project = Xcodeproj::Project.open(project_path) mainTarget = project.targets.first # 取出第一个target # 检查是否已经存在名为“Run SwiftLint”的Run Script existing_script = mainTarget.shell_script_build_phases.find do |phase| phase.name == xcode_script_name end if existing_script script_content = existing_script.shell_script local_script_version = get_script_version(script_content) if local_script_version.nil? || Gem::Version.new(local_script_version) < Gem::Version.new(cache_script_version) if local_script_version.nil? puts "#{swiftlint_tag} Xcode工程配置中的不存在#{xcode_script_name} 或 无版本,更新#{xcode_script_name}脚本".green else puts "#{swiftlint_tag} Xcode工程配置#{xcode_script_name}版本:#{local_script_version} 小于 远端版本:#{cache_script_version},更新#{xcode_script_name}脚本".green end existing_script.shell_script = File.read(cache_script_path) project.save end else puts "#{swiftlint_tag} Xcode工程配置中的不存在#{xcode_script_name}脚本,更新脚本".green new_script = mainTarget.new_shell_script_build_phase(xcode_script_name) new_script.shell_script = File.read(cache_script_path) project.save end end |