|
| 1 | +// |
| 2 | +// MintfileBuilder.swift |
| 3 | +// GitHubRestAPISwiftOpenAPI |
| 4 | +// |
| 5 | +// Created by zwc on 2025/5/11. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +struct MintfileBuilder { |
| 11 | + |
| 12 | + struct Dependency { |
| 13 | + let name: String |
| 14 | + let baseURL: String |
| 15 | + let path: String |
| 16 | + let version: String |
| 17 | + var urlString: String { "\(baseURL)/\(path)" } |
| 18 | + } |
| 19 | + |
| 20 | + let dependencies = [ |
| 21 | + Dependency( |
| 22 | + name: "Mint", |
| 23 | + baseURL: "https://github.com", |
| 24 | + path: "yonaskolb/Mint", |
| 25 | + version: "0.17.5" |
| 26 | + ), |
| 27 | + Dependency( |
| 28 | + name: "swift-openapi-generator", |
| 29 | + baseURL: "https://github.com", |
| 30 | + path: "apple/swift-openapi-generator", |
| 31 | + version: "1.7.2" |
| 32 | + ) |
| 33 | + ] |
| 34 | + |
| 35 | + func addVersionUpdatesManifests() { |
| 36 | + for dependency in dependencies { |
| 37 | + let manifestPath = ".github/dependabot-mintfile/manifest-\(dependency.name)" |
| 38 | + shell("mkdir -p \(manifestPath); swift package --package-path \(manifestPath) init --type empty") |
| 39 | + shell("mkdir -p \(manifestPath); swift package --package-path \(manifestPath) add-dependency \(dependency.urlString) --exact \(dependency.version)") |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + func write(to path: String = "Mintfile") { |
| 44 | + let lines = dependencies.map { "\($0.path)@\($0.version)" } |
| 45 | + let content = lines.joined(separator: "\n") + "\n" |
| 46 | + do { |
| 47 | + try content.write(toFile: path, atomically: true, encoding: .utf8) |
| 48 | + } catch { |
| 49 | + print("Failed to write Mintfile: \(error)") |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @discardableResult |
| 54 | + private func shell(_ command: String) -> Int32 { |
| 55 | + let task = Process() |
| 56 | + task.launchPath = "/bin/bash" |
| 57 | + task.arguments = ["-c", command] |
| 58 | + task.launch() |
| 59 | + task.waitUntilExit() |
| 60 | + return task.terminationStatus |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// MintfileBuilder().addVersionUpdatesManifests() |
| 65 | +MintfileBuilder().write() |
0 commit comments