Class: BB::SwiftlintManager

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

Instance Method Summary collapse

Constructor Details

#initializeSwiftlintManager

Returns a new instance of SwiftlintManager.



13
14
15
16
17
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 13

def initialize()
  remove_swiftlint_path()
  # 已安装过run swiftlint脚本将强制更新
  update_swiftlint_script(true)
end

Instance Method Details

#cocoapods_script_version(script_path) ⇒ Object

获取远端script脚本版本



321
322
323
324
325
326
327
328
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 321

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_fileObject

查找.xcodeproj文件



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 206

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脚本版本



331
332
333
334
335
336
337
338
339
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 331

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(name, lib_path) ⇒ Object

写入路径到swiftlint文件



30
31
32
33
34
35
36
37
38
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 30

def swiftlint_from_module(name, lib_path)
  # 查询真实路径
  path_list = SwiftlintFilesHelper.query_file_path(name, lib_path)
  unless path_list.empty?
    path_list.each do | path |
      write_swiftlint_included(path)
    end
  end
end

#update_swiftlint_configObject



19
20
21
22
23
24
25
26
27
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 19

def update_swiftlint_config()
  # 单次运行过程中只安装或更新一次
  if !@update_config
    @update_config = true
    update_swiftlint_file()
    update_swiftlint_script()
    update_swiftlint_gitignore()
  end
end

#update_swiftlint_script(force = false) ⇒ Object

更新script脚本



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/cocoapods-bb-PodAssistant/helpers/swiftlint_manager_helper.rb', line 342

def update_swiftlint_script(force = false)

  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
  elsif !force
    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