Use {install-root}/libexec/container-plugins for plugins. (#341)

- Use a directory that's separate from user data, as user-installed
plugins have a distinct lifecycle.
- Closes #340.
This commit is contained in:
J Logan
2025-07-15 18:18:38 -07:00
committed by GitHub
parent 78d4422a43
commit 1707e1b530
5 changed files with 87 additions and 16 deletions
+16 -5
View File
@@ -129,18 +129,28 @@ struct APIServer: AsyncParsableCommand {
}
private func initializePluginLoader(log: Logger) throws -> PluginLoader {
// create user-installed plugins directory if it doesn't exist
let pluginsURL = PluginLoader.userPluginsDir(root: Self.appRoot)
try FileManager.default.createDirectory(at: pluginsURL, withIntermediateDirectories: true)
let installRoot = CommandLine.executablePathUrl
.deletingLastPathComponent()
.appendingPathComponent("..")
.standardized
let pluginsURL = PluginLoader.userPluginsDir(root: installRoot)
var directoryExists: ObjCBool = false
_ = FileManager.default.fileExists(atPath: pluginsURL.path, isDirectory: &directoryExists)
let userPluginsURL = directoryExists.boolValue ? pluginsURL : nil
// plugins built into the application installed as a macOS app bundle
let appBundlePluginsURL = Bundle.main.resourceURL?.appending(path: "plugins")
// plugins built into the application installed as a Unix-like application
let installRootPluginsURL = CommandLine.executableDirectoryUrl.appendingPathComponent("../libexec/container/plugins")
let installRootPluginsURL =
installRoot
.appendingPathComponent("libexec")
.appendingPathComponent("container")
.appendingPathComponent("plugins")
.standardized
let pluginDirectories = [
pluginsURL,
userPluginsURL,
appBundlePluginsURL,
installRootPluginsURL,
].compactMap { $0 }
@@ -150,6 +160,7 @@ struct APIServer: AsyncParsableCommand {
AppBundlePluginFactory(),
]
log.info("PLUGINS: \(pluginDirectories)")
let statePath = PluginLoader.defaultPluginResourcePath(root: Self.appRoot)
try FileManager.default.createDirectory(at: statePath, withIntermediateDirectories: true)
return PluginLoader(pluginDirectories: pluginDirectories, pluginFactories: pluginFactories, defaultResourcePath: statePath, log: log)
+25 -5
View File
@@ -94,12 +94,32 @@ struct Application: AsyncParsableCommand {
}()
static let pluginLoader: PluginLoader = {
// create user-installed plugins directory if it doesn't exist
let pluginsURL = PluginLoader.userPluginsDir(root: Self.appRoot)
try! FileManager.default.createDirectory(at: pluginsURL, withIntermediateDirectories: true)
let installRoot = CommandLine.executablePathUrl
.deletingLastPathComponent()
.appendingPathComponent("..")
.standardized
let pluginsURL = PluginLoader.userPluginsDir(root: installRoot)
var directoryExists: ObjCBool = false
_ = FileManager.default.fileExists(atPath: pluginsURL.path, isDirectory: &directoryExists)
let userPluginsURL = directoryExists.boolValue ? pluginsURL : nil
// plugins built into the application installed as a macOS app bundle
let appBundlePluginsURL = Bundle.main.resourceURL?.appending(path: "plugins")
// plugins built into the application installed as a Unix-like application
let installRootPluginsURL =
installRoot
.appendingPathComponent("libexec")
.appendingPathComponent("container")
.appendingPathComponent("plugins")
.standardized
let pluginDirectories = [
pluginsURL
]
userPluginsURL,
appBundlePluginsURL,
installRootPluginsURL,
].compactMap { $0 }
let pluginFactories = [
DefaultPluginFactory()
]
@@ -17,10 +17,20 @@
import Foundation
extension CommandLine {
public static var executableDirectoryUrl: URL {
let executablePath = Self.arguments[0]
let executableUrl = URL(filePath: executablePath)
let executableDirectoryUrl = executableUrl.deletingLastPathComponent()
return executableDirectoryUrl.standardized
public static var executablePathUrl: URL {
/// _NSGetExecutablePath with a zero-length buffer returns the needed buffer length
var bufferSize: Int32 = 0
var buffer = [CChar](repeating: 0, count: Int(bufferSize))
_ = _NSGetExecutablePath(&buffer, &bufferSize)
/// Create the buffer and get the path
buffer = [CChar](repeating: 0, count: Int(bufferSize))
guard _NSGetExecutablePath(&buffer, &bufferSize) == 0 else {
fatalError("UNEXPECTED: failed to get executable path")
}
/// Return the path with the executable file component removed the last component and
let executablePath = String(cString: &buffer)
return URL(filePath: executablePath)
}
}
+4 -1
View File
@@ -44,7 +44,10 @@ public struct PluginLoader: Sendable {
}
static public func userPluginsDir(root: URL) -> URL {
root.appending(path: "user-plugins")
root
.appending(path: "libexec")
.appending(path: "container-plugins")
.resolvingSymlinksInPath()
}
}
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the container project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import Foundation
import Testing
@testable import ContainerPlugin
struct CommandLineExecutableTest {
@Test
func testCLIPluginConfigLoad() async throws {
#expect(CommandLine.executablePathUrl.lastPathComponent == "swiftpm-testing-helper")
}
}