Skip to content

Commit

Permalink
Update SwiftSyntax (#13)
Browse files Browse the repository at this point in the history
* Update SwiftSyntax

* update script

* fixes

* Restore script
  • Loading branch information
mackoj committed Apr 4, 2024
1 parent 4219e99 commit 265a648
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
DerivedData/
Package_generated.swift
package-generator-cli.artifactbundle/
xcuserdata/
xcuserdata/
.swiftpm/
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax",
"state" : {
"revision" : "75e60475d9d8fd5bbc16a12e0eaa2cb01b0c322e",
"version" : "0.50500.0"
"revision" : "fa8f95c2d536d6620cc2f504ebe8a6167c9fc2dd",
"version" : "510.0.1"
}
}
],
Expand Down
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ let package = Package(
],
products: [
.executable(name: "package-generator-cli", targets: ["PackageGeneratorCLI"]),
.library(name: "PackageGeneratorModels", targets: ["PackageGeneratorModels"])
],
dependencies: [
.package(url: "https://github.com/JohnSundell/Files.git", from: "4.2.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.1.4"),
.package(url: "https://github.com/apple/swift-syntax.git", exact: "0.50500.0"),
.package(url: "https://github.com/apple/swift-syntax", "509.0.0"..<"511.0.0"),
],
targets: [
.binaryTarget(
name: "lib_InternalSwiftSyntaxParser",
url: "https://github.com/keith/StaticInternalSwiftSyntaxParser/releases/download/5.5.2/lib_InternalSwiftSyntaxParser.xcframework.zip",
checksum: "96bbc9ab4679953eac9ee46778b498cb559b8a7d9ecc658e54d6679acfbb34b8"
.target(
name: "PackageGeneratorModels"
),
.executableTarget(
name: "PackageGeneratorCLI",
dependencies: [
"Files",
"lib_InternalSwiftSyntaxParser",
"PackageGeneratorModels",
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
Expand Down
22 changes: 13 additions & 9 deletions Sources/PackageGeneratorCLI/GetImportVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import SwiftSyntax
class GetImportVisitor: SyntaxRewriter {
var imports: [String] = []

override func visit(_ node: AccessPathSyntax) -> Syntax {
let identifier = node.tokens.compactMap { ts -> String? in
if case let .identifier(id) = ts.tokenKind, id.isEmpty == false {
let trimmed = id.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
return nil
}
imports.append(contentsOf: identifier)
func visit(_ node: ImportPathComponentSyntax) -> Syntax {
imports.append(node.name.text)
return node._syntaxNode
}
// override func visit(_ node: AccessPathSyntax) -> Syntax {
// let identifier = node.tokens.compactMap { ts -> String? in
// if case let .identifier(id) = ts.tokenKind, id.isEmpty == false {
// let trimmed = id.trimmingCharacters(in: .whitespacesAndNewlines)
// return trimmed.isEmpty ? nil : trimmed
// }
// return nil
// }
// imports.append(contentsOf: identifier)
// return node._syntaxNode
// }

func drain() -> [String] {
return imports
Expand Down
18 changes: 0 additions & 18 deletions Sources/PackageGeneratorCLI/ParsedPackage.swift

This file was deleted.

29 changes: 19 additions & 10 deletions Sources/PackageGeneratorCLI/cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Foundation
import Files
import ArgumentParser
import SwiftSyntax
import SwiftParser
import PackageGeneratorModels

@main
struct PackageGeneratorCLI: AsyncParsableCommand {
Expand Down Expand Up @@ -37,28 +39,28 @@ struct PackageGeneratorCLI: AsyncParsableCommand {
fatalError("Failed to create Data from \(inputFileURL.path)")
}

var lines: [String] = []
var lines: [PackageInformation] = []
do {
lines = try JSONDecoder().decode([String].self, from: fileData)
lines = try JSONDecoder().decode([PackageInformation].self, from: fileData)
} catch {
fatalError("Failed to JSONDecoder Data from \(inputFileURL.path) in [String].self")
fatalError("Failed to JSONDecoder Data from \(inputFileURL.path) in [PackageInformation].self")
}

for packagePath in lines {
if FileManager.default.fileExists(atPath: packagePath) == false {
if FileManager.default.fileExists(atPath: packagePath.target.path) == false {
print("\(packagePath) not found")
continue
}

var folder: Folder
do {
folder = try Folder(path: packagePath)
folder = try Folder(path: packagePath.target.path)
} catch {
fatalError("Failed to create Folder with \(packagePath)")
}

let (f, ti) = getImportsFromTarget(folder)
let parsedPackage = getTargetOutputFrom(f, ti, sourceCodeFolder)
let parsedPackage = getTargetOutputFrom(packagePath, f, ti, sourceCodeFolder)
parsedPackages.append(parsedPackage)
}
return parsedPackages
Expand All @@ -68,10 +70,11 @@ struct PackageGeneratorCLI: AsyncParsableCommand {
return folder.subfolders.recursive.filter { $0.name == "Resources" }.first
}

func getTargetOutputFrom(_ packageFolder: Folder, _ imports: [String], _ rootFolder : Folder) -> ParsedPackage {
func getTargetOutputFrom(_ packageInfo: PackageInformation, _ packageFolder: Folder, _ imports: [String], _ rootFolder : Folder) -> ParsedPackage {
let hasR = hasRessources(packageFolder)
return ParsedPackage(
name: packageFolder.name,
name: packageInfo.target.name,
test: packageInfo.test,
dependencies: imports,
path: packageFolder.path(relativeTo: rootFolder),
fullPath: packageFolder.path,
Expand All @@ -85,10 +88,16 @@ struct PackageGeneratorCLI: AsyncParsableCommand {

func getImportsFromFile(_ file: File) -> [String] {
do {
let syntaxTree = try SyntaxParser.parse(file.url, diagnosticEngine: .none)
let source = try String(contentsOf: file.url, encoding: .utf8)
let sourceFile = Parser.parse(source: source)
let visitor = GetImportVisitor()
_ = visitor.visit(syntaxTree)
_ = visitor.visit(sourceFile)
return visitor.drain()

// let syntaxTree = try SyntaxParser.parse(file.url, diagnosticEngine: .none)
// let visitor = GetImportVisitor()
// _ = visitor.visit(syntaxTree)
// return visitor.drain()
} catch {
print("💥 Failed to extract imports from \(file)")
}
Expand Down
25 changes: 25 additions & 0 deletions Sources/PackageGeneratorModels/PackageInformation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

public struct PackageInformation: Codable {
public struct PathInfo: Codable {
public let path: String
public let name: String
}
public let test: PathInfo?
public let target: PathInfo

public init(from decoder: any Decoder) throws {
// Legacy Parser
let stringContainer = try decoder.singleValueContainer()
if let pathString = try? stringContainer.decode(String.self) {
let path = URL(fileURLWithPath: pathString)
self.target = PathInfo(path: pathString, name: path.lastPathComponent)
self.test = nil
return
}

let container = try decoder.container(keyedBy: CodingKeys.self)
self.test = try container.decodeIfPresent(PackageInformation.PathInfo.self, forKey: .test)
self.target = try container.decode(PackageInformation.PathInfo.self, forKey: .target)
}
}
31 changes: 31 additions & 0 deletions Sources/PackageGeneratorModels/ParsedPackage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

public struct ParsedPackage: Codable, CustomStringConvertible {
public var name: String
public var test: PackageInformation.PathInfo?
public var dependencies: [String]
public var path: String
public var fullPath: String
public var resources: String?
public var localDependencies: Int = 0
public var hasBiggestNumberOfDependencies: Bool = false

public var hasResources: Bool {
resources != nil && resources!.isEmpty == false
}

public var description: String {
"[\(dependencies.count)|\(localDependencies)] \(name) \(hasResources == false ? "" : "/ hasResources")"
}

public init(name: String, test: PackageInformation.PathInfo? = nil, dependencies: [String], path: String, fullPath: String, resources: String? = nil, localDependencies: Int = 0, hasBiggestNumberOfDependencies: Bool = false) {
self.name = name
self.test = test
self.dependencies = dependencies
self.path = path
self.fullPath = fullPath
self.resources = resources
self.localDependencies = localDependencies
self.hasBiggestNumberOfDependencies = hasBiggestNumberOfDependencies
}
}
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fi

TMPRELEASEPROJECT=$TMPRELEASEFOLDER/PackageGeneratorCLI
TAG=$(curl -s "https://api.github.com/repos/$REPO/tags" | jq --compact-output --raw-output '.[0].name ')
#TAG="feat/moveToSwiftParser"
CLIBINPATH="$CLI.artifactbundle/$TRIPLE/bin"
ZIPOUTPUT=$CLI-$TRIPLE.artifactbundle.zip

Expand Down

0 comments on commit 265a648

Please sign in to comment.