Skip to content

Commit

Permalink
feat: backup old files, write new one
Browse files Browse the repository at this point in the history
  • Loading branch information
markemer committed Nov 8, 2023
1 parent 4eac723 commit 41b93b5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/CapacitorPluginPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
47 changes: 47 additions & 0 deletions Sources/PackageSwiftGenerator/GeneratePackageFile.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
22 changes: 20 additions & 2 deletions Sources/cap2spm.swift
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 41b93b5

Please sign in to comment.