Modern Apps git

← xtool.git

DevNewCommand

Commit
8d17b71b3c0e11913715b0cea973ae4f580b8f08
Author
Kabir Oberai <[email protected]>
When
2024-12-19 11:29 UTC (1 year ago)
Parents
4a8cef9

Diff

Sources/SupersignCLISupport/DevCommand.swift
diff --git a/Sources/SupersignCLISupport/DevCommand.swift b/Sources/SupersignCLISupport/DevCommand.swiftindex e6a50b1..98d4b9c 100644--- a/Sources/SupersignCLISupport/DevCommand.swift+++ b/Sources/SupersignCLISupport/DevCommand.swift@@ -130,6 +130,7 @@ struct DevCommand: AsyncParsableCommand {         abstract: "Develop iOS apps with SwiftPM",         subcommands: [             DevSetupCommand.self,+            DevNewCommand.self,             DevSDKCommand.self,             DevBuildCommand.self,             DevRunCommand.self,
Sources/SupersignCLISupport/DevNewCommand.swift
diff --git a/Sources/SupersignCLISupport/DevNewCommand.swift b/Sources/SupersignCLISupport/DevNewCommand.swiftnew file mode 100644index 0000000..e29ab94--- /dev/null+++ b/Sources/SupersignCLISupport/DevNewCommand.swift@@ -0,0 +1,146 @@+import ArgumentParser+import Foundation++struct DevNewCommand: AsyncParsableCommand {+    static let configuration = CommandConfiguration(+        commandName: "new",+        abstract: "Create a new project"+    )++    @Argument var name: String?++    func run() async throws {+        let name = try await Console.promptRequired("Package name: ", existing: self.name)+        let baseURL = URL(fileURLWithPath: name)++        guard !baseURL.exists else {+            throw Console.Error("Cannot create \(name): a file already exists at that path.")+        }++        print("Creating package: \(name)")++        let files: [(String, String)] = [+            (+                "Package.swift",+                """+                // swift-tools-version: 6.0++                import PackageDescription++                let package = Package(+                    name: "\(name)",+                    platforms: [+                        .iOS(.v17),+                        .macOS(.v14),+                    ],+                    products: [+                        // A Swiftpack project should contain exactly one library target,+                        // representing the main app.+                        .library(+                            name: "\(name)",+                            targets: ["\(name)"]+                        ),+                    ],+                    targets: [+                        .target(+                            name: "\(name)"+                        ),+                    ]+                )+                """+            ),++            (+                "swiftpack.yml",+                """+                version: 1+                bundleID: com.example.\(name)+                """+            ),++            (+                ".gitignore",+                """+                .DS_Store+                /.build+                /Packages+                xcuserdata/+                DerivedData/+                .swiftpm/configuration/registries.json+                .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata+                .netrc+                +                /swiftpack+                /.sourcekit-lsp+                """+            ),++            (+                ".sourcekit-lsp/config.json",+                """+                {+                    "swiftPM": {+                        "swiftSDK": "arm64-apple-ios"+                    }+                }+                """+            ),++            (+                "Sources/\(name)/\(name)App.swift",+                """+                import SwiftUI+                +                @main+                struct \(name)App: App {+                    var body: some Scene {+                        WindowGroup {+                            ContentView()+                        }+                    }+                }+                """+            ),++            (+                "Sources/\(name)/ContentView.swift",+                """+                import SwiftUI+                +                struct ContentView: View {+                    var body: some View {+                        VStack {+                            Image(systemName: "globe")+                                .imageScale(.large)+                                .foregroundStyle(.tint)+                            Text("Hello, world!")+                        }+                        .padding()+                    }+                }+                """+            ),+        ]++        for (path, contents) in files {+            let url = baseURL.appendingPathComponent(path)+            try? FileManager.default.createDirectory(+                at: url.deletingLastPathComponent(),+                withIntermediateDirectories: true+            )+            print("Creating \(path)")+            try "\(contents)\n".write(to: url, atomically: true, encoding: .utf8)+        }++        let gitInit = Process()+        gitInit.executableURL = URL(fileURLWithPath: "/usr/bin/env")+        gitInit.arguments = ["git", "init"]+        gitInit.currentDirectoryURL = baseURL+        try gitInit.run()+        await gitInit.waitForExit()++        print("\nFinished generating project \(name). Next steps:")+        print("- Enter the directory with `cd \(name)`")+        print("- Build and run with `supersign dev`")+    }+}