-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathharness_coverage_hook.rb
More file actions
87 lines (73 loc) · 2.74 KB
/
harness_coverage_hook.rb
File metadata and controls
87 lines (73 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
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
47
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
79
80
81
82
83
84
85
86
87
module HarnessCoverageHook
def run_podfile_post_install_hooks
super
pods = resolve_coverage_pods
return if pods.empty?
Pod::UI.puts "[HarnessCoverage] Instrumenting pods for native coverage: #{pods.join(', ')}"
apply_coverage_flags_to_pods(pods)
enable_harness_coverage_pod
apply_linker_flags
end
private
def resolve_coverage_pods
project_dir = Dir.pwd
config_json = `node -e "
import('#{project_dir}/rn-harness.config.mjs')
.then(m => console.log(JSON.stringify(
m.default?.coverage?.native?.ios?.pods || []
)))
.catch(() => console.log('[]'))
"`.strip
JSON.parse(config_json)
rescue => e
Pod::UI.warn "[HarnessCoverage] Failed to read config: #{e.message}"
[]
end
def apply_coverage_flags_to_pods(pods)
pods_project.targets.each do |target|
next unless pods.include?(target.name)
target.build_configurations.each do |config|
swift_flags = config.build_settings['OTHER_SWIFT_FLAGS'] || '$(inherited)'
unless swift_flags.include?('-profile-generate')
config.build_settings['OTHER_SWIFT_FLAGS'] =
"#{swift_flags} -profile-generate -profile-coverage-mapping"
end
c_flags = config.build_settings['OTHER_CFLAGS'] || '$(inherited)'
unless c_flags.include?('-fprofile-instr-generate')
config.build_settings['OTHER_CFLAGS'] =
"#{c_flags} -fprofile-instr-generate -fcoverage-mapping"
end
end
Pod::UI.puts "[HarnessCoverage] -> #{target.name}"
end
end
def enable_harness_coverage_pod
pods_project.targets.each do |target|
next unless target.name == 'HarnessCoverage'
target.build_configurations.each do |config|
swift_conditions = config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] || '$(inherited)'
unless swift_conditions.include?('HARNESS_COVERAGE')
config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] =
"#{swift_conditions} HARNESS_COVERAGE"
end
gcc_defs = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] || '$(inherited)'
unless gcc_defs.include?('HARNESS_COVERAGE')
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] =
"#{gcc_defs} HARNESS_COVERAGE=1"
end
end
end
end
def apply_linker_flags
pods_project.targets.each do |target|
target.build_configurations.each do |config|
ldflags = config.build_settings['OTHER_LDFLAGS'] || '$(inherited)'
unless ldflags.include?('-fprofile-instr-generate')
config.build_settings['OTHER_LDFLAGS'] =
"#{ldflags} -fprofile-instr-generate"
end
end
end
end
end
Pod::Installer.prepend(HarnessCoverageHook)