Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for disabling simulated location #28

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sources/coordinate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CoreLocation

func coordinate(from arguments: [String]) -> Result<CLLocationCoordinate2D> {
func coordinate(from arguments: [String]) -> Result<CLLocationCoordinate2D?> {
if arguments.count != 2 {
return .failure("Incorrect number of arguments, expected 2 got \(arguments.count)")
}
Expand Down
5 changes: 5 additions & 0 deletions sources/delete.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import CoreLocation

func delete(_: [String]) -> Result<CLLocationCoordinate2D?> {
return .success(nil)
}
11 changes: 8 additions & 3 deletions sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ guard let flag = arguments.popFirst() else {

let commands = [
"-c": coordinate,
"-d": delete,
"-q": findLocation,
]

Expand All @@ -18,20 +19,24 @@ guard let command = commands[flag] else {
}

switch command(Array(arguments)) {
case .success(let coordinate) where coordinate.isValid:
case .success(let coordinate) where coordinate?.isValid != false:
do {
let bootedSimulators = try getBootedSimulators()
let simulators =
try deviceUUID.map { try getSimulators(with: $0, from: bootedSimulators) }
?? deviceName.map { try getSimulators(named: $0, from: bootedSimulators) }
?? bootedSimulators
postNotification(for: coordinate, to: simulators.map { $0.udid.uuidString })
print("Setting location to \(coordinate.latitude) \(coordinate.longitude)")
if let coordinate = coordinate {
print("Setting location to \(coordinate.latitude) \(coordinate.longitude)")
} else {
print("Clearing location")
}
} catch let error as SimulatorFetchError {
exitWithUsage(error: error.description)
}
case .success(let coordinate):
exitWithUsage(error: "Coordinate: \(coordinate) is invalid")
exitWithUsage(error: "Coordinate: \(String(describing: coordinate)) is invalid")
case .failure(let error):
exitWithUsage(error: error)
}
11 changes: 7 additions & 4 deletions sources/notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import Foundation

private let kNotificationName = "com.apple.iphonesimulator.simulateLocation"

func postNotification(for coordinate: CLLocationCoordinate2D, to simulators: [String]) {
let userInfo: [AnyHashable: Any] = [
"simulateLocationLatitude": coordinate.latitude,
"simulateLocationLongitude": coordinate.longitude,
func postNotification(for coordinate: CLLocationCoordinate2D?, to simulators: [String]) {
var userInfo: [AnyHashable: Any] = [
"simulateLocationDevices": simulators,
]

if let coordinate = coordinate {
userInfo["simulateLocationLatitude"] = coordinate.latitude
userInfo["simulateLocationLongitude"] = coordinate.longitude
}

let notification = Notification(name: Notification.Name(rawValue: kNotificationName), object: nil,
userInfo: userInfo)

Expand Down
2 changes: 1 addition & 1 deletion sources/query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import CoreLocation
import Foundation
import MapKit

func findLocation(from arguments: [String]) -> Result<CLLocationCoordinate2D> {
func findLocation(from arguments: [String]) -> Result<CLLocationCoordinate2D?> {
if arguments.isEmpty {
return .failure("No arguments passed for location search")
}
Expand Down
2 changes: 1 addition & 1 deletion sources/stderr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func exitWithUsage(error: String? = nil) -> Never {
print(error, terminator: "\n\n", to: &stderrStream)
}

print("Usage: set-simulator-location [-c 0 0|-q San Francisco] [-s Simulator Name|-u Simulator udid]",
print("Usage: set-simulator-location [-c 0 0|-q San Francisco|-d] [-s Simulator Name|-u Simulator udid]",
to: &stderrStream)
exit(EXIT_FAILURE)
}