Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cmd/protoc-gen-swift-oneof-helper/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// cmd/protoc-gen-swift-oneof-helper/main.go
package main

import (
"os"
"os/exec"
"path/filepath"
)

func main() {
// Get the directory of this executable
execPath, err := os.Executable()
if err != nil {
panic(err)
}

// Look for the Swift binary
swiftBinary := filepath.Join(filepath.Dir(execPath), "protoc-gen-swift-oneof-helper-swift")

// If Swift binary doesn't exist, try building it
if _, err := os.Stat(swiftBinary); os.IsNotExist(err) {
buildSwiftBinary(swiftBinary)
}

// Execute the Swift binary
cmd := exec.Command(swiftBinary)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
os.Exit(1)
}
}

func buildSwiftBinary(outputPath string) {
cmd := exec.Command("swift", "build", "-c", "release", "--product", "protoc-gen-swift-oneof-helper")
if err := cmd.Run(); err != nil {
panic("Failed to build Swift binary: " + err.Error())
}

// Copy the built binary to the expected location
builtBinary := ".build/release/protoc-gen-swift-oneof-helper"
cmd = exec.Command("cp", builtBinary, outputPath)
cmd.Run()
}
8 changes: 8 additions & 0 deletions swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
79 changes: 79 additions & 0 deletions swift/.swiftpm/xcode/xcshareddata/xcschemes/SwiftSebuf.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1640"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftSebuf"
BuildableName = "SwiftSebuf"
BlueprintName = "SwiftSebuf"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftSebufTests"
BuildableName = "SwiftSebufTests"
BlueprintName = "SwiftSebufTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</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">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftSebuf"
BuildableName = "SwiftSebuf"
BlueprintName = "SwiftSebuf"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1640"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftSebufTests"
BuildableName = "SwiftSebufTests"
BlueprintName = "SwiftSebufTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</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">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
24 changes: 24 additions & 0 deletions swift/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

@preconcurrency import PackageDescription

let package = Package(
name: .name,
platforms: [.macOS(.v14)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.swiftSebuf
],
dependencies: [
.swiftArgumentParser,
.swiftProtobuf
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.swiftSebuf,
.swiftSebufTest
]
)

private extension String {

// MARK: Package Name
static let name: Self = "SwiftSebuf"

// MARK: Modules
static let swiftSebuf: Self = "SwiftSebuf"

// MARK: Packages
static let swiftArgumentParser: Self = "ArgumentParser"
static let swiftArgumentParserPackage: Self = "swift-argument-parser"
static let swiftProtobuf: Self = "SwiftProtobuf"
static let swiftProtobufPluginLibrary: Self = "SwiftProtobufPluginLibrary"
static let swiftProtobufPackage: Self = "swift-protobuf"

var test: Self {
"\(self)Tests"
}
}

private extension Product {

static let swiftSebuf: Product = library(name: .swiftSebuf, targets: [.swiftSebuf])
}

private extension Target {

static let swiftSebuf: Target = target(
name: .swiftSebuf,
dependencies: [
.swiftArgumentParser,
.swiftProtobuf,
.swiftProtobufPluginLibrary
]
)
static let swiftSebufTest: Target = testTarget(name: .swiftSebuf.test, dependencies: [.swiftSebuf])
}

private extension Target.Dependency {

// MARK: Modules
static let swiftSebuf: Self = byName(name: .swiftSebuf)

// MARK: Packages
static let swiftArgumentParser: Self = product(
name: .swiftArgumentParser,
package: .swiftArgumentParserPackage
)
static let swiftProtobuf: Self = product(name: .swiftProtobuf, package: .swiftProtobufPackage)
static let swiftProtobufPluginLibrary: Self = product(
name: .swiftProtobufPluginLibrary,
package: .swiftProtobufPackage
)
}

private extension Package.Dependency {

static let swiftArgumentParser: Package.Dependency = package(
url: "https://github.com/apple/swift-argument-parser.git",
exact: "1.6.1"
)
static let swiftProtobuf: Package.Dependency = package(
url: "https://github.com/apple/swift-protobuf.git",
exact: "1.31.0"
)
}
18 changes: 18 additions & 0 deletions swift/Sources/SwiftSebuf/Generator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Generator.swift
// SwiftSebuf
//
// Created by Khaled Chehabeddine on 08/09/2025.
// Copyright © 2025 Sebuf. All rights reserved.
//

import Foundation
import SwiftProtobuf
import SwiftProtobufPluginLibrary

protocol Generator {

init(descriptorSet: DescriptorSet)

func generate() -> CodeGeneratorResponse
}
23 changes: 23 additions & 0 deletions swift/Sources/SwiftSebuf/OneofHelper/OneofHelperCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// OneofHelperCommand.swift
// SwiftSebuf
//
// Created by Khaled Chehabeddine on 04/09/2025.
// Copyright © 2025 Sebuf. All rights reserved.
//

import ArgumentParser
import Foundation
import SwiftProtobuf
import SwiftProtobufPluginLibrary

@main
struct OneofHelperCommand: SebufCommand {

typealias SebufCodeGenerator = OneofHelperGenerator

static let configuration: CommandConfiguration = .init(
commandName: "protoc-gen-swift-oneof-helper",
abstract: "Generate Swift oneof helper extensions for protobuf messages"
)
}
Loading