Skip to content

Commit

Permalink
feat: Add testing and backup option
Browse files Browse the repository at this point in the history
  • Loading branch information
markemer committed Nov 8, 2023
1 parent fddccdf commit 50e7b4d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ let package = Package(
.product(name: "SwiftParser", package: "swift-syntax")
]
),
.testTarget(name: "CapacitorConverterTests",
dependencies: [
.target(name: "cap2spm"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax")
]
)
]
)
34 changes: 27 additions & 7 deletions Sources/cap2spm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import ArgumentParser

@main
struct Cap2SPM: ParsableCommand {
@Flag(name: .customLong("backup"), inversion: .prefixedNo, help: "Should we make a backup? Defaults to true")
var shouldBackup = true

@Option(help: "Objective-C header for file containing CAP_PLUGIN macro")
var objcHeader: String?

Expand Down Expand Up @@ -46,14 +49,27 @@ struct Cap2SPM: ParsableCommand {

try modifySwiftFile(at: swiftFileURL, plugin: capPlugin)

try fileBackup(at: mFileURL)
try fileBackup(at: hFileURL)
let fileList = [mFileURL, hFileURL]
if shouldBackup {
try fileBackup(of: fileList)
} else {
try fileDelete(of: fileList)
}
}

private func fileBackup(at fileURL: URL) throws {
let fileBackupURL = fileURL.appendingPathExtension("old")
print("Moving \(fileURL.path()) to \(fileBackupURL.path())")
try FileManager.default.moveItem(at: fileURL, to: fileBackupURL)
private func fileBackup(of fileURLs: [URL]) throws {
for fileURL in fileURLs {
let fileBackupURL = fileURL.appendingPathExtension("old")
print("Moving \(fileURL.path()) to \(fileBackupURL.path())...")
try FileManager.default.moveItem(at: fileURL, to: fileBackupURL)
}
}

private func fileDelete(of fileURLs: [URL]) throws {
for fileURL in fileURLs {
print("Deleting \(fileURL.path())...")
try FileManager.default.removeItem(at: fileURL)
}
}

private func modifySwiftFile(at fileURL: URL, plugin: CapacitorPluginSyntax) throws {
Expand All @@ -62,7 +78,11 @@ struct Cap2SPM: ParsableCommand {

let incremented = AddPluginToClass(with: plugin).visit(sourceFile)

try fileBackup(at: fileURL)
if shouldBackup {
try fileBackup(of: [fileURL])
} else {
try fileDelete(of: [fileURL])
}

var outputString: String = ""
incremented.write(to: &outputString)
Expand Down
11 changes: 11 additions & 0 deletions Tests/CapacitorPluginMethodTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testable import cap2spm
import XCTest

class CapacitorPluginMethodTests: XCTestCase {
func testCapacitorPluginMethod() {
let pluginMethod = CapacitorPluginMethod(methodName: "testMethod", returnType: .promise)
var methodOutput = ""
pluginMethod.functionCallExpr.write(to: &methodOutput)
XCTAssertEqual(methodOutput, "CAPPluginMethod(name: \"testMethod\", returnType: CAPPluginReturnPromise)")
}
}

0 comments on commit 50e7b4d

Please sign in to comment.