diff --git a/SwiftMetaModelGenerator.xcodeproj/project.pbxproj b/SwiftMetaModelGenerator.xcodeproj/project.pbxproj index 904cd5e7738541020260f7411189977da3596abe..3e4882cca1ed15cb04066e91f4a64e5681dd4e35 100644 --- a/SwiftMetaModelGenerator.xcodeproj/project.pbxproj +++ b/SwiftMetaModelGenerator.xcodeproj/project.pbxproj @@ -10,10 +10,11 @@ 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 */; }; + F6752A6B2683600A00CC9A19 /* Stencil in Frameworks */ = {isa = PBXBuildFile; productRef = F6752A6A2683600A00CC9A19 /* Stencil */; }; + F6BC6BA72683649000E4F37D /* lib_InternalSwiftSyntaxParser.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = F6BC6BA62683648800E4F37D /* lib_InternalSwiftSyntaxParser.dylib */; }; + F6BC6BA82683649000E4F37D /* lib_InternalSwiftSyntaxParser.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = F6BC6BA62683648800E4F37D /* lib_InternalSwiftSyntaxParser.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -32,7 +33,7 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - F6752A62268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Embed Libraries */, + F6BC6BA82683649000E4F37D /* lib_InternalSwiftSyntaxParser.dylib in Embed Libraries */, ); name = "Embed Libraries"; runOnlyForDeploymentPostprocessing = 0; @@ -42,8 +43,9 @@ /* 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>"; }; + F6752A6C2683602800CC9A19 /* meta_model.stencil */ = {isa = PBXFileReference; lastKnownFileType = text; path = meta_model.stencil; sourceTree = "<group>"; }; + F6BC6BA62683648800E4F37D /* 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; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -52,7 +54,8 @@ buildActionMask = 2147483647; files = ( F6752A5C268354B900CC9A19 /* PathKit in Frameworks */, - F6752A61268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Frameworks */, + F6752A6B2683600A00CC9A19 /* Stencil in Frameworks */, + F6BC6BA72683649000E4F37D /* lib_InternalSwiftSyntaxParser.dylib in Frameworks */, F6752A592683534800CC9A19 /* ArgumentParser in Frameworks */, F6752A662683599500CC9A19 /* SwiftSyntax in Frameworks */, ); @@ -81,7 +84,7 @@ F6752A4C2683522200CC9A19 /* SwiftMetaModelGenerator */ = { isa = PBXGroup; children = ( - F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */, + F6752A6D2683606A00CC9A19 /* templates */, F6752A4D2683522200CC9A19 /* main.swift */, F6752A6726835A2F00CC9A19 /* DeclarationCollector.swift */, ); @@ -91,10 +94,19 @@ F6752A5E268357A200CC9A19 /* Frameworks */ = { isa = PBXGroup; children = ( + F6BC6BA62683648800E4F37D /* lib_InternalSwiftSyntaxParser.dylib */, ); name = Frameworks; sourceTree = "<group>"; }; + F6752A6D2683606A00CC9A19 /* templates */ = { + isa = PBXGroup; + children = ( + F6752A6C2683602800CC9A19 /* meta_model.stencil */, + ); + path = templates; + sourceTree = "<group>"; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -116,6 +128,7 @@ F6752A582683534800CC9A19 /* ArgumentParser */, F6752A5B268354B900CC9A19 /* PathKit */, F6752A652683599500CC9A19 /* SwiftSyntax */, + F6752A6A2683600A00CC9A19 /* Stencil */, ); productName = SwiftMetaModelGenerator; productReference = F6752A4A2683522200CC9A19 /* SwiftMetaModelGenerator */; @@ -148,6 +161,7 @@ F6752A572683534800CC9A19 /* XCRemoteSwiftPackageReference "swift-argument-parser" */, F6752A5A268354B900CC9A19 /* XCRemoteSwiftPackageReference "PathKit" */, F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */, + F6752A692683600A00CC9A19 /* XCRemoteSwiftPackageReference "Stencil" */, ); productRefGroup = F6752A4B2683522200CC9A19 /* Products */; projectDirPath = ""; @@ -356,6 +370,14 @@ minimumVersion = 0.50400.0; }; }; + F6752A692683600A00CC9A19 /* XCRemoteSwiftPackageReference "Stencil" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/kylef/Stencil.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 0.14.1; + }; + }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -374,6 +396,11 @@ package = F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */; productName = SwiftSyntax; }; + F6752A6A2683600A00CC9A19 /* Stencil */ = { + isa = XCSwiftPackageProductDependency; + package = F6752A692683600A00CC9A19 /* XCRemoteSwiftPackageReference "Stencil" */; + productName = Stencil; + }; /* 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 index ead400c894cd827fc60b6916e2c47c1a2f6bc00b..f1ac865bfc4989cde68774d9dae7797f914681bb 100644 --- a/SwiftMetaModelGenerator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/SwiftMetaModelGenerator.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -19,6 +19,15 @@ "version": "0.9.2" } }, + { + "package": "Stencil", + "repositoryURL": "https://github.com/kylef/Stencil.git", + "state": { + "branch": null, + "revision": "973e190edf5d09274e4a6bc2e636c86899ed84c3", + "version": "0.14.1" + } + }, { "package": "swift-argument-parser", "repositoryURL": "https://github.com/apple/swift-argument-parser", diff --git a/SwiftMetaModelGenerator.xcodeproj/xcshareddata/xcschemes/SwiftMetaModelGenerator.xcscheme b/SwiftMetaModelGenerator.xcodeproj/xcshareddata/xcschemes/SwiftMetaModelGenerator.xcscheme index 615bf9c650e0c39921ef97a34ebe9debb1e88ded..6f7901cace2b4cf4486a591430719aed67992e6d 100644 --- a/SwiftMetaModelGenerator.xcodeproj/xcshareddata/xcschemes/SwiftMetaModelGenerator.xcscheme +++ b/SwiftMetaModelGenerator.xcodeproj/xcshareddata/xcschemes/SwiftMetaModelGenerator.xcscheme @@ -56,7 +56,15 @@ isEnabled = "YES"> </CommandLineArgument> <CommandLineArgument - argument = "/Users/tobias/Documents/Programmieren/Projects/WolfManagement/WolfManagementMobile/WolfManagementMobile/Data/ValueObjects/Protocol/Accident/VOAnimalAccidentProtocol.swift" + argument = "/Users/tobias/Documents/Programmieren/Projects/WolfManagement/WolfManagementMobile/WolfManagementMobile/Data/ValueObjects" + isEnabled = "YES"> + </CommandLineArgument> + <CommandLineArgument + argument = "--output" + isEnabled = "YES"> + </CommandLineArgument> + <CommandLineArgument + argument = "/Users/tobias/Documents/Programmieren/Projects/WolfManagement/WolfManagementMobile/WolfManagementMobile/Generated/Metamodel.swift" isEnabled = "YES"> </CommandLineArgument> </CommandLineArguments> diff --git a/SwiftMetaModelGenerator/main.swift b/SwiftMetaModelGenerator/main.swift index 87436c8ae2efc9d48979fd04221f53be54cdcc09..5ab921ba8347d1f3ec6700b80c79c1e885068001 100644 --- a/SwiftMetaModelGenerator/main.swift +++ b/SwiftMetaModelGenerator/main.swift @@ -8,6 +8,7 @@ import Foundation import ArgumentParser import SwiftSyntax +import Stencil import PathKit struct SwiftMetaModelGenerator: ParsableCommand @@ -15,31 +16,40 @@ struct SwiftMetaModelGenerator: ParsableCommand @Option(name: .shortAndLong, help: "Input file or folder") var input: String + @Option(name: .shortAndLong, help: "Output file") + var output: String + mutating func run() throws { let inputPath = Path(input) + let classes: [ClassDeclaration] if inputPath.isDirectory { - inputPath.iterateChildren().forEach( { processFile(file: $0) }) + classes = try inputPath.iterateChildren().flatMap( { try processFile(file: $0) }) } else { - processFile(file: inputPath) + classes = try processFile(file: inputPath) } + + let context = [ + "classes": classes + ] + + let environment = Environment(loader: FileSystemLoader(paths: ["/Users/tobias/Documents/Programmieren/Projects/SwiftMetaModelGenerator/SwiftMetaModelGenerator/templates/"])) + let rendered = try environment.renderTemplate(name: "meta_model.stencil", context: context) + + try Path(output).write(rendered) } - func processFile(file: Path) { + func processFile(file: Path) throws -> [ClassDeclaration] { if !(file.extension?.lowercased().hasSuffix("swift") ?? false) { - return + 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) - } + let content: String = try file.read() + + let collector = DeclarationCollector() + let tree = try SyntaxParser.parse(source: content) + collector.walk(tree) + + return collector.classes } } diff --git a/SwiftMetaModelGenerator/templates/meta_model.stencil b/SwiftMetaModelGenerator/templates/meta_model.stencil new file mode 100644 index 0000000000000000000000000000000000000000..6fd4078b97dfc895a17c50c86848ed25e271f056 --- /dev/null +++ b/SwiftMetaModelGenerator/templates/meta_model.stencil @@ -0,0 +1,17 @@ +// swiftlint:disable all +// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen + +// swiftlint:disable superfluous_disable_command implicit_return +// swiftlint:disable sorted_imports +import Foundation + +// swiftlint:disable attributes file_length vertical_whitespace_closing_braces +// swiftlint:disable identifier_name line_length type_body_length +{% for class in classes %} +class {{class.name}}_ { +{% for variable in class.variables %} + static let {{ variable }}: String = "{{ variable }}" +{% endfor %} +} +{% endfor %} +// swiftlint:enable identifier_name line_length type_body_length