diff --git a/SwiftMetaModelGenerator.xcodeproj/project.pbxproj b/SwiftMetaModelGenerator.xcodeproj/project.pbxproj index 6b3a7f40f41ceca550c703a86742db1bb0aae6e9..904cd5e7738541020260f7411189977da3596abe 100644 --- a/SwiftMetaModelGenerator.xcodeproj/project.pbxproj +++ b/SwiftMetaModelGenerator.xcodeproj/project.pbxproj @@ -3,11 +3,17 @@ archiveVersion = 1; classes = { }; - objectVersion = 50; + objectVersion = 52; objects = { /* Begin PBXBuildFile section */ F6752A4E2683522200CC9A19 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6752A4D2683522200CC9A19 /* main.swift */; }; + F6752A592683534800CC9A19 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = F6752A582683534800CC9A19 /* ArgumentParser */; }; + F6752A5C268354B900CC9A19 /* PathKit in Frameworks */ = {isa = PBXBuildFile; productRef = F6752A5B268354B900CC9A19 /* PathKit */; }; + F6752A61268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */; }; + F6752A62268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; + F6752A662683599500CC9A19 /* SwiftSyntax in Frameworks */ = {isa = PBXBuildFile; productRef = F6752A652683599500CC9A19 /* SwiftSyntax */; }; + F6752A6826835A2F00CC9A19 /* DeclarationCollector.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6752A6726835A2F00CC9A19 /* DeclarationCollector.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -20,11 +26,24 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + F6752A63268357D900CC9A19 /* Embed Libraries */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + F6752A62268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Embed Libraries */, + ); + name = "Embed Libraries"; + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ F6752A4A2683522200CC9A19 /* SwiftMetaModelGenerator */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwiftMetaModelGenerator; sourceTree = BUILT_PRODUCTS_DIR; }; F6752A4D2683522200CC9A19 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; }; + F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = lib_InternalSwiftSyntaxParser.dylib; path = Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/lib_InternalSwiftSyntaxParser.dylib; sourceTree = DEVELOPER_DIR; }; + F6752A6726835A2F00CC9A19 /* DeclarationCollector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeclarationCollector.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -32,6 +51,10 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + F6752A5C268354B900CC9A19 /* PathKit in Frameworks */, + F6752A61268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Frameworks */, + F6752A592683534800CC9A19 /* ArgumentParser in Frameworks */, + F6752A662683599500CC9A19 /* SwiftSyntax in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -43,6 +66,7 @@ children = ( F6752A4C2683522200CC9A19 /* SwiftMetaModelGenerator */, F6752A4B2683522200CC9A19 /* Products */, + F6752A5E268357A200CC9A19 /* Frameworks */, ); sourceTree = "<group>"; }; @@ -57,11 +81,20 @@ F6752A4C2683522200CC9A19 /* SwiftMetaModelGenerator */ = { isa = PBXGroup; children = ( + F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */, F6752A4D2683522200CC9A19 /* main.swift */, + F6752A6726835A2F00CC9A19 /* DeclarationCollector.swift */, ); path = SwiftMetaModelGenerator; sourceTree = "<group>"; }; + F6752A5E268357A200CC9A19 /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = "<group>"; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -72,12 +105,18 @@ F6752A462683522200CC9A19 /* Sources */, F6752A472683522200CC9A19 /* Frameworks */, F6752A482683522200CC9A19 /* CopyFiles */, + F6752A63268357D900CC9A19 /* Embed Libraries */, ); buildRules = ( ); dependencies = ( ); name = SwiftMetaModelGenerator; + packageProductDependencies = ( + F6752A582683534800CC9A19 /* ArgumentParser */, + F6752A5B268354B900CC9A19 /* PathKit */, + F6752A652683599500CC9A19 /* SwiftSyntax */, + ); productName = SwiftMetaModelGenerator; productReference = F6752A4A2683522200CC9A19 /* SwiftMetaModelGenerator */; productType = "com.apple.product-type.tool"; @@ -105,6 +144,11 @@ Base, ); mainGroup = F6752A412683522200CC9A19; + packageReferences = ( + F6752A572683534800CC9A19 /* XCRemoteSwiftPackageReference "swift-argument-parser" */, + F6752A5A268354B900CC9A19 /* XCRemoteSwiftPackageReference "PathKit" */, + F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */, + ); productRefGroup = F6752A4B2683522200CC9A19 /* Products */; projectDirPath = ""; projectRoot = ""; @@ -119,6 +163,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + F6752A6826835A2F00CC9A19 /* DeclarationCollector.swift in Sources */, F6752A4E2683522200CC9A19 /* main.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -285,6 +330,51 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + F6752A572683534800CC9A19 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/apple/swift-argument-parser"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 0.4.3; + }; + }; + F6752A5A268354B900CC9A19 /* XCRemoteSwiftPackageReference "PathKit" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/kylef/PathKit.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 1.0.0; + }; + }; + F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/apple/swift-syntax.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 0.50400.0; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + F6752A582683534800CC9A19 /* ArgumentParser */ = { + isa = XCSwiftPackageProductDependency; + package = F6752A572683534800CC9A19 /* XCRemoteSwiftPackageReference "swift-argument-parser" */; + productName = ArgumentParser; + }; + F6752A5B268354B900CC9A19 /* PathKit */ = { + isa = XCSwiftPackageProductDependency; + package = F6752A5A268354B900CC9A19 /* XCRemoteSwiftPackageReference "PathKit" */; + productName = PathKit; + }; + F6752A652683599500CC9A19 /* SwiftSyntax */ = { + isa = XCSwiftPackageProductDependency; + package = F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */; + productName = SwiftSyntax; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = F6752A422683522200CC9A19 /* Project object */; } diff --git a/SwiftMetaModelGenerator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/SwiftMetaModelGenerator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000000000000000000000000000000000000..ead400c894cd827fc60b6916e2c47c1a2f6bc00b --- /dev/null +++ b/SwiftMetaModelGenerator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,43 @@ +{ + "object": { + "pins": [ + { + "package": "PathKit", + "repositoryURL": "https://github.com/kylef/PathKit.git", + "state": { + "branch": null, + "revision": "73f8e9dca9b7a3078cb79128217dc8f2e585a511", + "version": "1.0.0" + } + }, + { + "package": "Spectre", + "repositoryURL": "https://github.com/kylef/Spectre.git", + "state": { + "branch": null, + "revision": "f79d4ecbf8bc4e1579fbd86c3e1d652fb6876c53", + "version": "0.9.2" + } + }, + { + "package": "swift-argument-parser", + "repositoryURL": "https://github.com/apple/swift-argument-parser", + "state": { + "branch": null, + "revision": "986d191f94cec88f6350056da59c2e59e83d1229", + "version": "0.4.3" + } + }, + { + "package": "SwiftSyntax", + "repositoryURL": "https://github.com/apple/swift-syntax.git", + "state": { + "branch": null, + "revision": "2fff9fc25cdc059379b6bd309377cfab45d8520c", + "version": "0.50400.0" + } + } + ] + }, + "version": 1 +} diff --git a/SwiftMetaModelGenerator.xcodeproj/xcshareddata/xcschemes/SwiftMetaModelGenerator.xcscheme b/SwiftMetaModelGenerator.xcodeproj/xcshareddata/xcschemes/SwiftMetaModelGenerator.xcscheme new file mode 100644 index 0000000000000000000000000000000000000000..615bf9c650e0c39921ef97a34ebe9debb1e88ded --- /dev/null +++ b/SwiftMetaModelGenerator.xcodeproj/xcshareddata/xcschemes/SwiftMetaModelGenerator.xcscheme @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Scheme + LastUpgradeVersion = "1250" + version = "1.3"> + <BuildAction + parallelizeBuildables = "YES" + buildImplicitDependencies = "YES"> + <BuildActionEntries> + <BuildActionEntry + buildForTesting = "YES" + buildForRunning = "YES" + buildForProfiling = "YES" + buildForArchiving = "YES" + buildForAnalyzing = "YES"> + <BuildableReference + BuildableIdentifier = "primary" + BlueprintIdentifier = "F6752A492683522200CC9A19" + BuildableName = "SwiftMetaModelGenerator" + BlueprintName = "SwiftMetaModelGenerator" + ReferencedContainer = "container:SwiftMetaModelGenerator.xcodeproj"> + </BuildableReference> + </BuildActionEntry> + </BuildActionEntries> + </BuildAction> + <TestAction + buildConfiguration = "Debug" + selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" + selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" + shouldUseLaunchSchemeArgsEnv = "YES"> + <Testables> + </Testables> + </TestAction> + <LaunchAction + buildConfiguration = "Debug" + selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" + selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" + launchStyle = "0" + useCustomWorkingDirectory = "NO" + ignoresPersistentStateOnLaunch = "NO" + debugDocumentVersioning = "YES" + debugServiceExtension = "internal" + allowLocationSimulation = "YES"> + <BuildableProductRunnable + runnableDebuggingMode = "0"> + <BuildableReference + BuildableIdentifier = "primary" + BlueprintIdentifier = "F6752A492683522200CC9A19" + BuildableName = "SwiftMetaModelGenerator" + BlueprintName = "SwiftMetaModelGenerator" + ReferencedContainer = "container:SwiftMetaModelGenerator.xcodeproj"> + </BuildableReference> + </BuildableProductRunnable> + <CommandLineArguments> + <CommandLineArgument + argument = "--input" + isEnabled = "YES"> + </CommandLineArgument> + <CommandLineArgument + argument = "/Users/tobias/Documents/Programmieren/Projects/WolfManagement/WolfManagementMobile/WolfManagementMobile/Data/ValueObjects/Protocol/Accident/VOAnimalAccidentProtocol.swift" + isEnabled = "YES"> + </CommandLineArgument> + </CommandLineArguments> + </LaunchAction> + <ProfileAction + buildConfiguration = "Release" + shouldUseLaunchSchemeArgsEnv = "YES" + savedToolIdentifier = "" + useCustomWorkingDirectory = "NO" + debugDocumentVersioning = "YES"> + <BuildableProductRunnable + runnableDebuggingMode = "0"> + <BuildableReference + BuildableIdentifier = "primary" + BlueprintIdentifier = "F6752A492683522200CC9A19" + BuildableName = "SwiftMetaModelGenerator" + BlueprintName = "SwiftMetaModelGenerator" + ReferencedContainer = "container:SwiftMetaModelGenerator.xcodeproj"> + </BuildableReference> + </BuildableProductRunnable> + </ProfileAction> + <AnalyzeAction + buildConfiguration = "Debug"> + </AnalyzeAction> + <ArchiveAction + buildConfiguration = "Release" + revealArchiveInOrganizer = "YES"> + </ArchiveAction> +</Scheme> diff --git a/SwiftMetaModelGenerator/DeclarationCollector.swift b/SwiftMetaModelGenerator/DeclarationCollector.swift new file mode 100644 index 0000000000000000000000000000000000000000..94c760f35ccb5c32be58fe94ca7a5ceb45da71b5 --- /dev/null +++ b/SwiftMetaModelGenerator/DeclarationCollector.swift @@ -0,0 +1,49 @@ +// +// DeclarationCollector.swift +// SwiftMetaModelGenerator +// +// Created by Tobias on 23.06.21. +// + +import Foundation +import SwiftSyntax + +struct ClassDeclaration { + let name: String + let variables: [String] +} + +class DeclarationCollector: SyntaxVisitor { + + private(set) var classes: [ClassDeclaration] = [] + private var currentVariables: [String] = [] + + override func visitPost(_ node: ClassDeclSyntax) { + let name = node.identifier.text.trimmingCharacters(in: .whitespacesAndNewlines) + + classes.append(ClassDeclaration(name: name, variables: currentVariables)) + currentVariables = [] + } + + override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind { + for child: PatternBindingSyntax in node.bindings { + if let parent = child.context as? VariableDeclSyntax, parent.context is ClassDeclSyntax { + let name = child.pattern.description.trimmingCharacters(in: .whitespacesAndNewlines) + currentVariables.append(name) + } + } + return .skipChildren + } +} + + +extension SyntaxProtocol { + var context: DeclSyntaxProtocol? { + for case let node? in sequence(first: parent, next: { $0?.parent }) { + guard let declaration = node.asProtocol(DeclSyntaxProtocol.self) else { continue } + return declaration + } + + return nil + } +} diff --git a/SwiftMetaModelGenerator/main.swift b/SwiftMetaModelGenerator/main.swift index 09dcde2e2350108240f6f0f26c5096105f84c8af..87436c8ae2efc9d48979fd04221f53be54cdcc09 100644 --- a/SwiftMetaModelGenerator/main.swift +++ b/SwiftMetaModelGenerator/main.swift @@ -6,6 +6,43 @@ // import Foundation +import ArgumentParser +import SwiftSyntax +import PathKit + +struct SwiftMetaModelGenerator: ParsableCommand +{ + @Option(name: .shortAndLong, help: "Input file or folder") + var input: String + + mutating func run() throws { + let inputPath = Path(input) + if inputPath.isDirectory { + inputPath.iterateChildren().forEach( { processFile(file: $0) }) + } else { + processFile(file: inputPath) + } + } + + func processFile(file: Path) { + if !(file.extension?.lowercased().hasSuffix("swift") ?? false) { + return + } + + do { + let content: String = try file.read() + + let collector = DeclarationCollector() + let tree = try SyntaxParser.parse(source: content) + collector.walk(tree) + + print(collector.classes) + } catch { + print(error) + } + } +} + +SwiftMetaModelGenerator.main() -print("Hello, World!")