Skip to content

Commit

Permalink
vk-399-api-stability: refactored to use SwiftCLI for controlling comm…
Browse files Browse the repository at this point in the history
…and line input.
  • Loading branch information
Udumft committed Nov 13, 2020
1 parent a553cfe commit 636a931
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 67 deletions.
3 changes: 2 additions & 1 deletion scripts/APIDiffReport/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/jakeheis/SwiftCLI", from: "6.0.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "APIDiffReport",
dependencies: []
dependencies: ["SwiftCLI"]
),
]
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@

import Foundation
import SwiftCLI

struct ApiDiff {
class DiffCommand: Command {
var name = "diff"
var shortDescription: String = "Runs a comparison between 2 JSON API logs and prints detected breaking changes."

static func runApiDiff(oldApiPath: URL, newApiPath: URL) throws -> Bool {
@Param var oldProjectPath: String
@Param var newProjectPath: String

func execute() throws {
guard try runApiDiff(oldApiPath: absURL(oldProjectPath),
newApiPath: absURL(newProjectPath)) else {
exit(1)
}
}

private func runApiDiff(oldApiPath: URL, newApiPath: URL) throws -> Bool {
let oldApi = try readJson(at: oldApiPath)
let newApi = try readJson(at: newApiPath)
let report = try diffreport(oldApi: oldApi, newApi: newApi)
Expand All @@ -21,7 +34,7 @@ struct ApiDiff {
}
}

static private func readJson(at path: URL) throws -> Any {
private func readJson(at path: URL) throws -> Any {
let data = try Data(contentsOf: path)

if !data.isEmpty {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@

import Foundation
import SwiftCLI

struct ApiLog {
class LogCommand: Command {
var name = "log"
var shortDescription: String = "Parses provided project and logs it's API structure in JSON format."

static func runApiLog(apiFolder: String, args: [String]) throws -> String? {
@Param var projectPath: String
@Param var outputPath: String
@CollectedParam var sourcekittenArgs: [String]

func execute() throws {
guard let log = try runApiLog(apiFolder: projectPath,
args: sourcekittenArgs) else {
print("Decoding 'sourcekitten' output failed.")
exit(1)
}
let outputURL = absURL(outputPath)
try FileManager.default.createDirectory(at: outputURL.deletingLastPathComponent(),
withIntermediateDirectories: true)
try log.write(to: outputURL,
atomically: true,
encoding: .utf8)
}

private func runApiLog(apiFolder: String, args: [String]) throws -> String? {
print("Running API Logging... ")
guard let APIDoc = runSourcekitten(apiFolder: apiFolder,
args: args) else {
Expand All @@ -13,7 +34,7 @@ struct ApiLog {
return String(data: APIDoc, encoding: .utf8)
}

static private func runSourcekitten(apiFolder: String, args: [String]) -> Data? {
private func runSourcekitten(apiFolder: String, args: [String]) -> Data? {
var result = Data()
let task = Process()
task.launchPath = "/usr/local/bin/sourcekitten"
Expand Down
65 changes: 5 additions & 60 deletions scripts/APIDiffReport/Sources/APIDiffReport/main.swift
Original file line number Diff line number Diff line change
@@ -1,65 +1,10 @@
import Foundation


func printUsage() {
print("Usage:")
print(" log <api project folder> <output file path> [forwarding sourcekitten args]")
print(" - Parses provided project and logs it's API structure in JSON format.")
print(" diff <old api log file path> <new api log file>")
print(" - Runs a comparison between 2 JSON API logs and prints detected breaking changes.")
}
import SwiftCLI

func absURL ( _ path: String ) -> URL {
// some methods cannot correctly expand '~' in a path, so we'll do it manually
let homeDirectory = URL(fileURLWithPath: NSHomeDirectory())
guard path != "~" else {
return homeDirectory
}
guard path.hasPrefix("~/") else { return URL(fileURLWithPath: path) }

var relativePath = path
relativePath.removeFirst(2)
return URL(string: relativePath,
relativeTo: homeDirectory) ?? homeDirectory
return URL(fileURLWithPath: (path as NSString).expandingTildeInPath)
}

guard ProcessInfo.processInfo.arguments.count > 2 else {
printUsage()
exit(1)
}

switch ProcessInfo.processInfo.arguments[1] {
case "log":
guard ProcessInfo.processInfo.arguments.count > 3 else {
printUsage()
exit(1)
}

let apiFolder = ProcessInfo.processInfo.arguments[2]
let outputFile = ProcessInfo.processInfo.arguments[3]
var sourcekittenArgs = Array<String>()
if ProcessInfo.processInfo.arguments.count > 4 {
sourcekittenArgs = Array(ProcessInfo.processInfo.arguments.suffix(from: 4))
}

guard let log = try ApiLog.runApiLog(apiFolder: apiFolder, args: sourcekittenArgs) else {
print("Decoding 'sourcekitten' output failed.")
exit(1)
}
let outputURL = absURL(outputFile)
try FileManager.default.createDirectory(at: outputURL.deletingLastPathComponent(),
withIntermediateDirectories: true)
try log.write(to: outputURL,
atomically: true,
encoding: .utf8)
case "diff":
let oldApi = ProcessInfo.processInfo.arguments[2]
let newApi = ProcessInfo.processInfo.arguments[3]

guard try ApiDiff.runApiDiff(oldApiPath: absURL(oldApi), newApiPath: absURL(newApi)) else {
exit(1)
}
default:
printUsage()
exit(1)
}
CLI(name: "APIDiffReport",
description: "A tool to detect Public API breaking changes",
commands: [LogCommand(), DiffCommand()]).goAndExit()

0 comments on commit 636a931

Please sign in to comment.