Skip to content

Commit

Permalink
Merge pull request #5 from freddi-kit/add-include-path
Browse files Browse the repository at this point in the history
Add include resource path option
  • Loading branch information
freddi-kit authored Aug 17, 2023
2 parents 7289d92 + d2fc6d0 commit 8e03c58
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
name: "ArtifactBundleGen",
platforms: [.macOS(.v12)],
products: [
.plugin(name: "ArtifactBundleGenCommand", targets: ["ArtifactBundleGenCommand"]),
.plugin(name: "ArtifactBundleGenPluginCommand", targets: ["ArtifactBundleGenPluginCommand"]),
.library(name: "ArtifactBundleGen", targets: ["ArtifactBundleGen"])
],
dependencies: [
Expand All @@ -20,13 +20,13 @@ let package = Package(
name: "ArtifactBundleGenTests",
dependencies: ["ArtifactBundleGen"]),
.plugin(
name: "ArtifactBundleGenCommand",
name: "ArtifactBundleGenPluginCommand",
capability: .command(
intent: .custom(verb: "generate-artifact-bundle",
description: "Generate Artifact Bundle"),
permissions: [.writeToPackageDirectory(reason: "Save Artifact Bundle information")]
),
path: "Plugins/ArtifactBundleGenCommand"
path: "Plugins/ArtifactBundleGenPluginCommand"
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
import OSLog

@main
struct ArtifactBundleGenCommand: CommandPlugin {
struct ArtifactBundleGenPluginCommand: CommandPlugin {

func performCommand(context: PluginContext, arguments: [String]) async throws {
var argumentExtractor = ArgumentExtractor(arguments)
Expand All @@ -12,6 +12,7 @@ struct ArtifactBundleGenCommand: CommandPlugin {
let executableNameOption = argumentExtractor.extractOption(named: "executable-name")
let buildFolderNameOption = argumentExtractor.extractOption(named: "build-folder")
let configOption = argumentExtractor.extractOption(named: "build-config")
let includeResourceOption = argumentExtractor.extractOption(named: "include-resource-path")

guard let name = executableNameOption.first else {
throw ArtifactBundleGenError.nameOptionMissing
Expand All @@ -25,7 +26,8 @@ struct ArtifactBundleGenCommand: CommandPlugin {
version: packageVersionOption.first ?? "1.0.0",
name: name,
buildFolderName: buildFolderNameOption.first ?? ".build",
config: config
config: config,
includeResourcePaths: includeResourceOption
)
try await artifactBundleGen.generate()
}
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ swift build -c debug --arch arm64 --arch x86_64
# .build is generated

# Call command
$ swift package generate-artifact-bundle --package-version 0.5.11 --executable-name some-awesome-tool --build-config debug --build-folder .build
$ swift package plugin generate-artifact-bundle --package-version 0.5.11 --executable-name some-awesome-tool --build-config debug --build-folder .build

$ ls
> some-awesome-tool.artifactbundle.zip
Expand Down Expand Up @@ -47,7 +47,7 @@ $ swift build -c debug --arch arm64 --arch x86_64
### 3. Call ArtifactBundleGen

```sh
$ swift package generate-artifact-bundle --package-version {version} --executable-name {executable-name} --build-config {config} --build-folder {folder}
$ swift package plugin generate-artifact-bundle --package-version {version} --executable-name {executable-name} --build-config {config} --build-folder {folder}
```

{tool_name}.artifactbundle will be generated!
Expand All @@ -69,6 +69,13 @@ let package = Package(
##### optionals
- --package-version: version of package (default is 1.0.0)
- --build-folder: version of package (default is .build)
- --include-resource-path: path to add as resources to artifact bundle (multiple is okay)

```
swift package plugin generate-artifact-bundle --package-version ... \
--include-resource-path some-awesome-bundle.bundle \
--include-resource-path some-awesome-image.jpg \
```


### 4. Complete!
Expand Down
34 changes: 24 additions & 10 deletions Sources/ArtifactBundleGen/ArtifactBundleGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public struct ArtifactBundleGen {
private let name: String
private let buildFolderName: String
private let config: Config
private let includeResourcePaths: [String]

private let folderCreator = FolderCreator()
private let lipoRunner = LipoRunnner()
Expand All @@ -30,7 +31,7 @@ public struct ArtifactBundleGen {
try folderCreator.createFolder(name: artifactBundleFolderName)
}

private func generateAppleUniversalUniversalBinaryArchIfExists() throws -> [Variant] {
private func generateAppleUniversalUniversalBinaryArchIfExists(with includeResourcePaths: [String]) throws -> [Variant] {
guard fileExistChecker.isExist(path: appleUniversalBinaryPath) else { return [] }

var variants: [Variant] = []
Expand All @@ -40,11 +41,17 @@ public struct ArtifactBundleGen {
let destinationUniversalBinaryFolderName = "\(artifactBundleFolderName)/\(appBundleUniversalBinaryFolderName)/bin"
try folderCreator.createFolder(name: destinationUniversalBinaryFolderName)

let destinationUniversalBinaryPath = "\(destinationUniversalBinaryFolderName)/\(name)"
let originExecutableURL = URL(fileURLWithPath: appleUniversalBinaryPath)
let destinationURL = URL(fileURLWithPath: destinationUniversalBinaryPath)
let destinationURL = URL(fileURLWithPath: "\(destinationUniversalBinaryFolderName)/\(name)")

try fileCopy.copy(from: originExecutableURL, to: destinationURL)
try includeResourcePaths.forEach {
let resourceFileURL = URL(fileURLWithPath: $0)
try fileCopy.copy(
from: resourceFileURL,
to: URL(fileURLWithPath: destinationUniversalBinaryFolderName).appendingPathComponent(resourceFileURL.lastPathComponent)
)
}

variants.append(
Variant(
Expand All @@ -58,7 +65,7 @@ public struct ArtifactBundleGen {
return variants
}

private func generateEachTriplesVariantsIfExists() throws -> [Variant] {
private func generateEachTriplesVariantsIfExists(with includeResourcePaths: [String]) throws -> [Variant] {
var variants: [Variant] = []

// check for all triples
Expand All @@ -69,11 +76,17 @@ public struct ArtifactBundleGen {
let artifactTripleDirectryPath = "\(artifactBundleFolderName)/\(triple)/bin"
try folderCreator.createFolder(name: artifactTripleDirectryPath)

let destinationPath = "\(artifactTripleDirectryPath)/\(name)"
let originExecutableURL = URL(fileURLWithPath: executablePath)
let destinationURL = URL(fileURLWithPath: destinationPath)
let destinationURL = URL(fileURLWithPath: "\(artifactTripleDirectryPath)/\(name)")

try fileCopy.copy(from: originExecutableURL, to: destinationURL)
try includeResourcePaths.forEach {
let resourceFileURL = URL(fileURLWithPath: $0)
try fileCopy.copy(
from: resourceFileURL,
to: URL(fileURLWithPath: artifactTripleDirectryPath).appendingPathComponent(resourceFileURL.lastPathComponent)
)
}

variants.append(
Variant(
Expand All @@ -87,7 +100,7 @@ public struct ArtifactBundleGen {
}

private func generateArtifactBundle(variants: [Variant]) -> ArtifactBundle {
let artifact = Artifact(
let artifact = Artifact(
version: version,
type: .executable,
variants: variants
Expand All @@ -111,22 +124,23 @@ public struct ArtifactBundleGen {
return string.replacingOccurrences(of: "\\", with: "")
}

public init(version: String, name: String, buildFolderName: String, config: Config) {
public init(version: String, name: String, buildFolderName: String, config: Config, includeResourcePaths: [String]) {
self.version = version
self.name = name
self.buildFolderName = buildFolderName
self.config = config
self.includeResourcePaths = includeResourcePaths
}

public func generate() async throws {
try prepareArtifactBundleFolder()

var variants: [Variant] = []

let generatedAppleUniversalUniversalBinaryVariants = try generateAppleUniversalUniversalBinaryArchIfExists()
let generatedAppleUniversalUniversalBinaryVariants = try generateAppleUniversalUniversalBinaryArchIfExists(with: includeResourcePaths)
variants.append(contentsOf: generatedAppleUniversalUniversalBinaryVariants)

let generatedEachTriplesVariants = try generateEachTriplesVariantsIfExists()
let generatedEachTriplesVariants = try generateEachTriplesVariantsIfExists(with: includeResourcePaths)
variants.append(contentsOf: generatedEachTriplesVariants)

let artifactBundle = generateArtifactBundle(variants: variants)
Expand Down

0 comments on commit 8e03c58

Please sign in to comment.