From 41b93b5ede887e170c014a9b296ec8a9b91463fd Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Wed, 8 Nov 2023 13:39:55 -0500 Subject: [PATCH] feat: backup old files, write new one --- Sources/CapacitorPluginPackage.swift | 2 +- .../GeneratePackageFile.swift | 47 +++++++++++++++++++ Sources/cap2spm.swift | 22 ++++++++- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 Sources/PackageSwiftGenerator/GeneratePackageFile.swift diff --git a/Sources/CapacitorPluginPackage.swift b/Sources/CapacitorPluginPackage.swift index 7a0a168..edfda11 100644 --- a/Sources/CapacitorPluginPackage.swift +++ b/Sources/CapacitorPluginPackage.swift @@ -67,7 +67,7 @@ class CapacitorPluginPackage { return url } - func fundObjCHeaderFile() throws -> URL { + func findObjCHeaderFile() throws -> URL { if let objcHeaderFilename { return URL(filePath: objcHeaderFilename, directoryHint: .notDirectory, relativeTo: basePathURL) } diff --git a/Sources/PackageSwiftGenerator/GeneratePackageFile.swift b/Sources/PackageSwiftGenerator/GeneratePackageFile.swift new file mode 100644 index 0000000..4be5b50 --- /dev/null +++ b/Sources/PackageSwiftGenerator/GeneratePackageFile.swift @@ -0,0 +1,47 @@ +import Foundation +import SwiftSyntax + + +class GeneratePackageFile { + let defaultIndent = 4 + + func create() { + let identiferPattern = IdentifierPatternSyntax(leadingTrivia: .space, + identifier: .identifier("package")) + + let packageExpression = DeclReferenceExprSyntax(baseName: .identifier("Package")) + + let stringSegmentList = StringLiteralSegmentListSyntax(arrayLiteral: + .stringSegment(StringSegmentSyntax(content: .stringSegment("MyPluginName")))) + + let stringLiteralExpr = StringLiteralExprSyntax(openingQuote: .stringQuoteToken(), + segments: stringSegmentList, + closingQuote: .stringQuoteToken()) + + + let firstArg = LabeledExprSyntax(label: .identifier("name"), expression: stringLiteralExpr) + let arguments = LabeledExprListSyntax(arrayLiteral: firstArg) + + let packageFunctionCall = FunctionCallExprSyntax(calledExpression: packageExpression, leftParen: .leftParenToken(), arguments: arguments, rightParen: .rightParenToken(), trailingTrivia: .space) + + let initalizerClause = InitializerClauseSyntax(equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space), + value: packageFunctionCall) + + let patternBinding = PatternBindingSyntax(pattern: identiferPattern, initializer: initalizerClause) + + let indentifierDecl = VariableDeclSyntax(leadingTrivia: .spaces(defaultIndent), + bindingSpecifier: .keyword(.let), + bindings: PatternBindingListSyntax(arrayLiteral: patternBinding), + trailingTrivia: .space) + + let defDecl = DeclSyntax(fromProtocol: indentifierDecl) + + let codeBlock = CodeBlockItemSyntax(item: .decl(defDecl)) + + let codeBlockList = CodeBlockItemListSyntax(arrayLiteral: codeBlock) + + let source = SourceFileSyntax(statements: codeBlockList) + + print(source.description) + } +} diff --git a/Sources/cap2spm.swift b/Sources/cap2spm.swift index e5815b3..762e49b 100644 --- a/Sources/cap2spm.swift +++ b/Sources/cap2spm.swift @@ -1,6 +1,6 @@ // The Swift Programming Language // https://docs.swift.org/swift-book -// +// // Swift Argument Parser // https://swiftpackageindex.com/apple/swift-argument-parser/documentation @@ -26,9 +26,16 @@ struct Cap2SPM: ParsableCommand { mutating func run() throws { let mFileURL: URL let swiftFileURL: URL + let hFileURL: URL let capacitorPluginPackage = try CapacitorPluginPackage(directoryName: pluginDirectory) + if let objcHeader { + hFileURL = URL(filePath: objcHeader, directoryHint: .notDirectory) + } else { + hFileURL = try capacitorPluginPackage.findObjCHeaderFile() + } + if let objcFile { mFileURL = URL(filePath: objcFile, directoryHint: .notDirectory) } else { @@ -47,7 +54,18 @@ struct Cap2SPM: ParsableCommand { guard let capPlugin = capacitorPluginPackage.oldPlugin?.capacitorPlugin else { return } let incremented = AddPluginToClass(with: capPlugin).visit(sourceFile) - print(incremented) + + let mFileBackup = mFileURL.appendingPathExtension("old") + let hFileBackup = hFileURL.appendingPathExtension("old") + let swiftFileBackup = swiftFileURL.appendingPathExtension("old") + + try FileManager.default.moveItem(at: mFileURL, to: mFileBackup) + try FileManager.default.moveItem(at: hFileURL, to: hFileBackup) + try FileManager.default.moveItem(at: swiftFileURL, to: swiftFileBackup) + + var outputString: String = "" + incremented.write(to: &outputString) + try outputString.write(to: swiftFileURL, atomically: true, encoding: .utf8) } }