-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
201 lines (171 loc) · 6.24 KB
/
Copy pathRakefile
File metadata and controls
201 lines (171 loc) · 6.24 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require 'rake'
require 'json'
desc "run out of box testing using the local build"
task oobt: %i[check build demo]
desc "execute all the steps"
task default: %i[check dependency version build test oobt]
desc 'install project dependencies'
task :dependency do
sh 'swift package update'
end
desc 'build serpapi library'
task :build do
sh 'swift build'
end
desc 'run tests'
task :test do
ENV['DEVELOPER_DIR'] ||= '/Applications/Xcode.app/Contents/Developer'
sh 'swift test'
end
namespace :examples do
desc 'run demo tests'
task :demo do
ENV['DEVELOPER_DIR'] ||= '/Applications/Xcode.app/Contents/Developer'
puts "Running tests for Examples/Demo..."
sh 'swift test --package-path Examples/Demo'
end
desc 'run events demo tests'
task :events_demo do
ENV['DEVELOPER_DIR'] ||= '/Applications/Xcode.app/Contents/Developer'
puts "Running tests for Examples/EventsDemo..."
sh 'swift test --package-path Examples/EventsDemo'
end
end
desc 'run tests with coverage'
task :coverage do
# Use main Xcode installation to find XCTest
ENV['DEVELOPER_DIR'] ||= '/Applications/Xcode.app/Contents/Developer'
sh 'swift test --enable-code-coverage'
# Find the coverage report path
bin_path = `swift build --show-bin-path`.strip
# The coverage data is usually in .build/debug/codecov/default.profdata
# We need to process it with llvm-cov
# Try to find llvm-cov
llvm_cov = `xcrun -f llvm-cov 2>/dev/null`.strip
if llvm_cov.empty?
puts "llvm-cov not found. Please ensure Xcode command line tools are installed."
else
# Generate report
# Note: We need to target the test bundle executable
xctest_path = Dir.glob("#{bin_path}/*.xctest/Contents/MacOS/*").first || Dir.glob("#{bin_path}/*.xctest/*").first
if xctest_path
puts "Generating coverage report..."
sh "#{llvm_cov} report #{xctest_path} -instr-profile=.build/debug/codecov/default.profdata -ignore-filename-regex='.build|Tests'"
else
puts "Could not find xctest binary for coverage report"
end
end
end
desc 'run linting'
task :lint do
if which('swiftlint')
ENV['DEVELOPER_DIR'] ||= '/Applications/Xcode.app/Contents/Developer'
sh 'swiftlint'
else
puts 'swiftlint not found, skipping lint'
end
end
desc 'format swift code'
task :format do
if which('swiftformat')
sh 'swiftformat .'
else
puts 'swiftformat not found, skipping format'
end
end
desc 'generate documentation'
task :doc do
ENV['DEVELOPER_DIR'] ||= '/Applications/Xcode.app/Contents/Developer'
sh 'swift package generate-documentation'
end
desc 'run the CLI demo (Examples/Demo)'
task :demo do
sh 'swift run --package-path Examples/Demo'
end
desc 'run the EventsDemo GUI on macOS'
task :events do
sh 'swift run --package-path Examples/EventsDemo'
end
namespace :events do
desc 'run EventsDemo in iOS simulator'
task :ios do
# Ensure we use the full Xcode path for simulator tools
developer_dir = '/Applications/Xcode.app/Contents/Developer'
ENV['DEVELOPER_DIR'] = developer_dir if File.exist?(developer_dir)
# 1. Find a simulator
puts "Searching for available iOS simulator..."
devices = `xcrun simctl list devices available -j`
begin
data = JSON.parse(devices)
# Find first booted or first available iPhone
all_devices = data['devices'].values.flatten
device = all_devices.find { |d| d['state'] == 'Booted' && d['name'].include?('iPhone') }
device ||= all_devices.find { |d| d['name'].include?('iPhone') }
if device
device_id = device['udid']
puts "Using device: #{device['name']} (#{device_id})"
# 2. Boot if needed
if device['state'] != 'Booted'
puts "Booting simulator..."
sh "xcrun simctl boot #{device_id}"
sh "open -a Simulator"
end
# 3. Build for simulator
puts "Building EventsDemo for iOS Simulator..."
derived_data = ".build/derived_data"
sh "xcodebuild build -scheme EventsDemo -destination 'platform=iOS Simulator,id=#{device_id}' -derivedDataPath #{derived_data} -project Examples/EventsDemo/.build/EventsDemo.xcodeproj" rescue nil
# xcodebuild might not work directly on a folder without generating a project first or if it's a SPM package.
# For SPM packages in subfolders, we should use -project if we generated one, or just build from the folder.
sh "cd Examples/EventsDemo && xcodebuild build -scheme EventsDemo -destination 'platform=iOS Simulator,id=#{device_id}' -derivedDataPath ../../.build/derived_data"
# 4. Find the .app and Bundle ID
app_path = Dir.glob("#{derived_data}/Build/Products/*-iphonesimulator/EventsDemo.app").first
if app_path
bundle_id = `defaults read "#{File.expand_path(app_path)}/Info.plist" CFBundleIdentifier`.strip
puts "Installing #{bundle_id}..."
sh "xcrun simctl install #{device_id} '#{app_path}'"
puts "Launching..."
sh "xcrun simctl launch #{device_id} #{bundle_id}"
else
puts "Error: Could not find built .app in #{derived_data}"
end
else
puts "Error: No iPhone simulator found. Please create one in Xcode or use 'open -a Simulator' first."
end
rescue => e
puts "Error: #{e.message}"
puts "Falling back to 'swift run EventsDemo' (macOS version)"
sh "swift run EventsDemo"
end
end
end
desc 'alias for events:ios (run EventsDemo in iOS simulator)'
task ios: 'events:ios'
desc 'print current version'
task :version do
sh "grep 'static let version' Sources/SerpApi/Version.swift"
end
desc 'create a git tag'
task :tag do
version = `grep 'static let version' Sources/SerpApi/Version.swift | cut -d '"' -f 2`.strip
puts "create git tag #{version}"
sh "git tag #{version}"
puts "now publish the tag:\n$ git push origin #{version}"
end
task :check do
if ENV['SERPAPI_KEY']
puts 'check: found $SERPAPI_KEY'
else
puts 'check: SERPAPI_KEY must be defined'
exit 1
end
end
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end