Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Sources/Container-Compose/Commands/ComposeDown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public struct ComposeDown: AsyncParsableCommand {
"Note: The 'name' field currently only affects container naming (e.g., '\(name)-serviceName'). Full project-level isolation for other resources (networks, implicit volumes) is not implemented by this tool."
)
} else {
projectName = URL(fileURLWithPath: cwd).lastPathComponent // Default to directory name
projectName = deriveProjectName(cwd: cwd)
print("Info: No 'name' field found in docker-compose.yml. Using directory name as project name: \(projectName ?? "")")
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Container-Compose/Commands/ComposeUp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public struct ComposeUp: AsyncParsableCommand, @unchecked Sendable {
"Note: The 'name' field currently only affects container naming (e.g., '\(name)-serviceName'). Full project-level isolation for other resources (networks, implicit volumes) is not implemented by this tool."
)
} else {
projectName = URL(fileURLWithPath: cwd).lastPathComponent // Default to directory name
projectName = deriveProjectName(cwd: cwd)
print("Info: No 'name' field found in docker-compose.yml. Using directory name as project name: \(projectName ?? "")")
}

Expand Down
11 changes: 11 additions & 0 deletions Sources/Container-Compose/Helper Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ public func resolveVariable(_ value: String, with envVars: [String: String]) ->
return resolvedValue
}

/// Derives a project name from the current working directory. It replaces any '.' characters with
/// '_' to ensure compatibility with container naming conventions.
///
/// - Parameter cwd: The current working directory path.
/// - Returns: A sanitized project name suitable for container naming.
public func deriveProjectName(cwd: String) -> String {
// We need to replace '.' with _ because it is not supported in the container name
let projectName = URL(fileURLWithPath: cwd).lastPathComponent.replacingOccurrences(of: ".", with: "_")
return projectName
}

extension String: @retroactive Error {}

/// A structure representing the result of a command-line process execution.
Expand Down
35 changes: 35 additions & 0 deletions Tests/Container-Compose-StaticTests/HelperFunctionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
// Copyright © 2025 Morris Richman and the Container-Compose 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 Testing
import Foundation
@testable import ContainerComposeCore

@Suite("Helper Functions Tests")
struct HelperFunctionsTests {

@Test("Derive project name from current working directory - contains dot")
func testDeriveProjectName() throws {
var cwd = "/Users/user/Projects/My.Project"
var projectName = deriveProjectName(cwd: cwd)
#expect(projectName == "My_Project")

cwd = ".devcontainers"
projectName = deriveProjectName(cwd: cwd)
#expect(projectName == "_devcontainers")
}

}