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

Improve error messages #22

Merged
merged 7 commits into from
Oct 4, 2018
Merged
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
17 changes: 11 additions & 6 deletions Bridgecraft/GenerateCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import SourceKittenFramework
import XcodeEdit

extension GenerateCommand {
static func execute(assumeNonnull: Bool,
static func execute(verbose: Bool,
assumeNonnull: Bool,
sdkOverride: [String],
destOverride: [String],
outputPath: [String],
origProjectPath: String,
targetName: String) {
let cmd = GenerateCommand(assumeNonnull: assumeNonnull,
let cmd = GenerateCommand(verbose: verbose,
assumeNonnull: assumeNonnull,
sdkOverride: sdkOverride,
destOverride: destOverride,
outputPath: outputPath,
Expand All @@ -34,17 +36,20 @@ struct GenerateCommand {
private let preprocessedURL: URL
private let outputFileURL: URL?

private let verbose: Bool
private let assumeNonnull: Bool
private let sdkOverride: String?
private let destOverride: String?
private let targetName: String

init(assumeNonnull: Bool,
init(verbose: Bool,
assumeNonnull: Bool,
sdkOverride: [String],
destOverride: [String],
outputPath: [String],
origProjectPath: String,
targetName: String) {
self.verbose = verbose
self.assumeNonnull = assumeNonnull
self.sdkOverride = sdkOverride.first
self.destOverride = destOverride.first
Expand Down Expand Up @@ -130,7 +135,7 @@ struct GenerateCommand {
"-showBuildSettings",
"-project", tempProjectURL.path,
"-target", targetName
])
], verbose: verbose)
}
catch {
printError("cannot query build settings for \(tempProjectURL.path): \(error)")
Expand Down Expand Up @@ -245,7 +250,7 @@ struct GenerateCommand {

let output: String
do {
output = try shell("/usr/bin/xcodebuild", args: args)
output = try shell("/usr/bin/xcodebuild", args: args, verbose: verbose)
}
catch {
printError("cannot dry-run build for \(tempProjectURL.path)")
Expand Down Expand Up @@ -298,7 +303,7 @@ struct GenerateCommand {
"-x", "objective-c", "-C", "-fmodules", "-fimplicit-modules",
"-E", bridgingSourceURL.path,
"-o", preprocessedURL.path
] + compilerFlags)
] + compilerFlags, verbose: verbose)
}
catch {
printError("failed to preprocess file at \(bridgingSourceURL.path): \(error)")
Expand Down
35 changes: 28 additions & 7 deletions Bridgecraft/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,48 @@ enum BridgecraftError: Error {
}

@discardableResult
func shell(_ command: String, args: [String]) throws -> String {
func shell(_ command: String, args: [String], verbose: Bool = false) throws -> String {

let ps = Process()
ps.launchPath = command
ps.arguments = args

let pipe = Pipe()
ps.standardOutput = pipe
let outputPipe = Pipe()
let errorPipe = Pipe()
ps.standardOutput = outputPipe
ps.standardError = errorPipe

ps.launch()

var buffer = Data()
var outputPipeResult = Data()
var errorPipeResult = Data()

while ps.isRunning {
buffer.append(pipe.fileHandleForReading.readDataToEndOfFile())
outputPipeResult.append(outputPipe.fileHandleForReading.readDataToEndOfFile())
errorPipeResult.append(errorPipe.fileHandleForReading.readDataToEndOfFile())
}

guard
let output = String(data: outputPipeResult, encoding: String.Encoding.utf8),
let error = String(data: errorPipeResult, encoding: String.Encoding.utf8)
else {
printError("\(command) \(args.joined(separator: " "))")
printError("Error parsing the output of the previous command.")
throw BridgecraftError.unknown
}

guard ps.terminationStatus == 0 else {
printError("\(command) \(args.joined(separator: " "))")
printError("Terminated with the status \(ps.terminationStatus).")
printError(output)
printError(error)
throw BridgecraftError.unknown
}

guard let output = String(data: buffer, encoding: String.Encoding.utf8) else {
throw BridgecraftError.unknown
if verbose {
print("\(command) \(args.joined(separator: " "))")
print(output)
printError(error)
}

return output
Expand Down
1 change: 1 addition & 0 deletions Bridgecraft/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Foundation
let version = "0.3.0"

let generate = command(
Flag("verbose", description: "output's all terminal commands and their results"),
Flag("assume-nonnull", description: "assume that all headers have been audited for nullability"),
Options<String>("sdk", default: [], count: 1, description: "override the SDK used for the build (see xcodebuild -sdk)"),
Options<String>("destination", default: [], count: 1, description: "override the destination device used for the build (see xcodebuild -destination)"),
Expand Down