Class: Fastlane::Helper::CommonHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/fastci/helper/common_helper.rb

Class Method Summary collapse

Class Method Details

.cache_git_commitObject

缓存最新 git



51
52
53
54
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 51

def self.cache_git_commit 
  latest_commit = sh("git rev-parse HEAD").strip
  write_cached_txt(COMMIT_HASH_FILE, latest_commit)
end

.extract_index_store_path(log_file) ⇒ Object

从构建日志中提取 index store 路径的辅助方法



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 57

def self.extract_index_store_path(log_file)
  begin
    log_content = File.read(log_file)

    # 在日志中查找 DerivedData 路径
    derived_data_match = log_content.match(/DerivedData\/([^\/]+)-([^\/]+)/)
    if derived_data_match
      project_name = derived_data_match[1]
      hash_suffix = derived_data_match[2]
      
      # 构建可能的 index store 路径
      derived_data_base = File.expand_path("~/Library/Developer/Xcode/DerivedData")
      project_dir = "#{derived_data_base}/#{project_name}-#{hash_suffix}"
      
      # Xcode 14+ 使用 Index.noindex
      index_paths = [
        "#{project_dir}/Index.noindex/DataStore",
        "#{project_dir}/Index/DataStore"
      ]
      
      # 返回第一个存在的路径
      index_paths.each do |path|
        if File.exist?(path)
          puts "*************| 找到 index store: #{path} |*************"
          return path
        end
      end
    end
    
    puts "*************| 在构建日志中未找到有效的 index store 路径 |*************"
    return nil
  rescue => e
    puts "*************| 解析构建日志失败: #{e.message} |*************"
    return nil
  end
end

.generate_and_open_html(title, python_file, origin_file, html_file) ⇒ Object

生成并打开 html



95
96
97
98
99
100
101
102
103
104
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 95

def self.generate_and_open_html(title, python_file, origin_file, html_file)
  python_path = File.expand_path("../../python/#{python_file}", __dir__)
  origin_file_path = File.expand_path(origin_file)
  html_file_path = File.expand_path(html_file)

  system("python3 \"#{python_path}\" \"#{title}\" \"#{origin_file_path}\" \"#{html_file_path}\"")
  system("open \"#{html_file_path}\"")

  File.delete(origin_file_path)
end

.get_cached_build_numberObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 23

def self.get_cached_build_number
  cached_build = read_cached_txt(Constants.BUILD_NUMBER_FILE)
  currentTime = Time.new.strftime("%Y%m%d")
  
  if cached_build.nil? || cached_build.empty?
    # 初始化 build
    return "#{currentTime}.00"
  else
    return cached_build
  end
end

.get_git_modified_swift_files(commit_hash) ⇒ Object

获取当前 commit_hash 到最新 commit 下所有变更的 swift 文件



44
45
46
47
48
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 44

def self.get_git_modified_swift_files(commit_hash)
  modified_files = sh("git diff --name-only --diff-filter=d #{commit_hash}..origin/develop").split("\n")
  swift_files = modified_files.select { |file| file.end_with?('.swift') }
  return swift_files
end

.is_validate_string(variable) ⇒ Object

判断字段是否有值



36
37
38
39
40
41
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 36

def self.is_validate_string(variable)
  if variable.nil? || variable.empty?
    return false
  end
  return true
end

.read_cached_txt(file_name) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 8

def self.read_cached_txt(file_name)
  if File.exist?(file_name)
    File.read(file_name).strip
  else
    nil
  end
end

.write_cached_txt(file_name, content) ⇒ Object



16
17
18
19
20
21
# File 'lib/fastlane/plugin/fastci/helper/common_helper.rb', line 16

def self.write_cached_txt(file_name, content)
  dir_name = File.dirname(file_name)
  FileUtils.mkdir_p(dir_name) unless Dir.exist?(dir_name)

  File.open(file_name, "w") { |file| file.write(content) }
end