Skip to content

Commit

Permalink
Parsing without file usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AllDmeat committed Feb 27, 2024
1 parent 2429567 commit 1d3e8dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 68 deletions.
52 changes: 0 additions & 52 deletions Sources/DBXCResultParser/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,4 @@ import Foundation

struct Constants {
private static let packageName = "DBXCResultParser"

private static var cachesDirectory: URL {
get throws {
guard let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
throw Error.noCachesDirectory
}

return cachesDirectory
}
}

private static var appCachesDirectory: URL {
get throws {
let appCachesDirectory = try cachesDirectory.appendingPathComponent(packageName, isDirectory: true)
if !FileManager.default.fileExists(atPath: appCachesDirectory.path) {
try FileManager.default.createDirectory(at: appCachesDirectory,
withIntermediateDirectories: true,
attributes: nil)
}
return appCachesDirectory
}
}

static var actionsInvocationRecord: URL {
get throws {
try appCachesDirectory
.appendingPathComponent("ActionsInvocationRecord", isDirectory: false)
.appendingPathExtension("json")
}
}

static var actionTestPlanRunSummaries: URL {
get throws {
try appCachesDirectory
.appendingPathComponent("ActionTestPlanRunSummaries", isDirectory: false)
.appendingPathExtension("json")
}
}

static var actionTestSummary: URL {
get throws {
try appCachesDirectory
.appendingPathComponent("ActionTestSummary", isDirectory: false)
.appendingPathExtension("json")
}
}
}

extension Constants {
enum Error: Swift.Error {
case noCachesDirectory
}
}
38 changes: 22 additions & 16 deletions Sources/DBXCResultParser/Models/DTO/DTO+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,48 @@ import Foundation

extension ActionsInvocationRecordDTO {
init(from xcresultPath: URL) throws {
let filePath = try Constants.actionsInvocationRecord
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json > \(filePath.relativePath)")
let data = try Data(contentsOf: filePath)
try FileManager.default.removeItem(atPath: filePath.relativePath)
let result = try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json")
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(ActionsInvocationRecordDTO.self, from: data)
}
}

extension ActionTestPlanRunSummariesDTO {
init(from xcresultPath: URL, refId: String? = nil) throws {
let refId = try (refId ?? ActionsInvocationRecordDTO(from: xcresultPath).testsRefId)
let filePath = try Constants.actionTestPlanRunSummaries
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId) > \(filePath.relativePath)")
let data = try Data(contentsOf: filePath)
try FileManager.default.removeItem(atPath: filePath.relativePath)
let result = try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId)")
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(ActionTestPlanRunSummariesDTO.self, from: data)
}
}

extension ActionTestSummaryDTO {
init(from xcresultPath: URL, refId: String? = nil) throws {
let refId = try (refId ?? ActionsInvocationRecordDTO(from: xcresultPath).testsRefId)
let filePath = try Constants.actionTestSummary
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId) > \(filePath.relativePath)")
let data = try Data(contentsOf: filePath)
try FileManager.default.removeItem(atPath: filePath.relativePath)
let result = try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId)")
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(ActionTestSummaryDTO.self, from: data)
}
}

extension Array where Element == CoverageDTO {
init(from xcresultPath: URL) throws {
let tempFilePath = try Constants.actionsInvocationRecord
try DBShell.execute("xcrun xccov view --report --only-targets --json \(xcresultPath.relativePath) > \(tempFilePath.relativePath)")
let data = try Data(contentsOf: tempFilePath)
try FileManager.default.removeItem(atPath: tempFilePath.relativePath)
let result = try DBShell.execute("xcrun xccov view --report --only-targets --json \(xcresultPath.relativePath)")
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(Array<CoverageDTO>.self, from: data)
}
}

infix operator ?!: NilCoalescingPrecedence

/// Throws the right hand side error if the left hand side optional is `nil`.
func ?!<T>(value: T?, error: @autoclosure () -> Error) throws -> T {
guard let value = value else {
throw error()
}
return value
}

enum UnwrapError: Swift.Error {
case valueIsNil
}

0 comments on commit 1d3e8dd

Please sign in to comment.