Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integration of SPM packages with slashes in their targets names #6260

Merged
merged 2 commits into from
May 15, 2024
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/TuistGenerator/Linter/TargetLinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TargetLinter: TargetLinting {
bundleIdentifier = bundleIdentifier.replacingOccurrences(of: "\\$\\(.+\\)", with: "", options: .regularExpression)

var allowed = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
allowed.formUnion(CharacterSet(charactersIn: "-."))
allowed.formUnion(CharacterSet(charactersIn: "-./"))

if !bundleIdentifier.unicodeScalars.allSatisfy({ allowed.contains($0) }) {
let reason =
Expand Down
3 changes: 2 additions & 1 deletion Sources/TuistGenerator/Mappers/ResourcesProjectMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class ResourcesProjectMapper: ProjectMapping { // swiftlint:disable:this
var additionalTargets: [Target] = []
var sideEffects: [SideEffectDescriptor] = []

let bundleName = "\(project.name)_\(target.name.replacingOccurrences(of: "-", with: "_"))"
let sanitizedTargetName = target.name.sanitizedModuleName
let bundleName = "\(project.name)_\(sanitizedTargetName)"
var modifiedTarget = target

if !target.supportsResources {
Expand Down
2 changes: 1 addition & 1 deletion Sources/TuistGraph/Models/Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public struct Target: Equatable, Hashable, Comparable, Codable {
self.product = product
self.destinations = destinations
self.bundleId = bundleId
self.productName = productName ?? name.replacingOccurrences(of: "-", with: "_")
self.productName = productName ?? name.sanitizedModuleName
self.deploymentTargets = deploymentTargets
self.infoPlist = infoPlist
self.entitlements = entitlements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ public final class PackageInfoMapper: PackageInfoMapping {

fileprivate class func sanitize(targetName: String) -> String {
targetName.replacingOccurrences(of: ".", with: "_")
.replacingOccurrences(of: "/", with: "_")
}

// swiftlint:disable:next function_body_length
Expand Down Expand Up @@ -596,7 +597,7 @@ public final class PackageInfoMapper: PackageInfoMapping {
.sanitize(targetName: target.name)
.replacingOccurrences(of: "-", with: "_"),
bundleId: target.name
.replacingOccurrences(of: "_", with: "."),
.replacingOccurrences(of: "_", with: ".").replacingOccurrences(of: "/", with: "."),
deploymentTargets: deploymentTargets,
infoPlist: .default,
sources: sources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public final class SwiftPackageManagerModuleMapGenerator: SwiftPackageManagerMod
moduleName: String,
publicHeadersPath: AbsolutePath
) throws -> ModuleMap {
let sanitizedModuleName = moduleName.replacingOccurrences(of: "-", with: "_")
let sanitizedModuleName = moduleName.sanitizedModuleName
let umbrellaHeaderPath = publicHeadersPath.appending(component: sanitizedModuleName + ".h")
let nestedUmbrellaHeaderPath = publicHeadersPath
.appending(components: sanitizedModuleName, moduleName + ".h")
.appending(components: sanitizedModuleName, sanitizedModuleName + ".h")
let customModuleMapPath = try Self.customModuleMapPath(publicHeadersPath: publicHeadersPath)
let generatedModuleMapPath: AbsolutePath

Expand All @@ -69,12 +69,12 @@ public final class SwiftPackageManagerModuleMapGenerator: SwiftPackageManagerMod
.parentDirectory
.appending(
components: Constants.DerivedDirectory.dependenciesDerivedDirectory,
moduleName,
"\(moduleName).modulemap"
sanitizedModuleName,
"\(sanitizedModuleName).modulemap"
)
} else {
generatedModuleMapPath = packageDirectory.appending(
components: Constants.DerivedDirectory.name, "\(moduleName).modulemap"
components: Constants.DerivedDirectory.name, "\(sanitizedModuleName).modulemap"
)
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/TuistSupport/Extensions/String+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ extension String {
return self
}
}

/// Formats the current string to a Uniform Type Identifier
public var sanitizedModuleName: String {
replacingOccurrences(of: "-", with: "_")
.replacingOccurrences(of: "/", with: "_")
}
}

extension Array where Element: CustomStringConvertible {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ final class ResourcesProjectMapperTests: TuistUnitTestCase {
// Given
let projectName = "sdk-with-dash"
let targetName = "target-with-dash"
let expectedBundleName = "\(projectName)_\(targetName.replacingOccurrences(of: "-", with: "_"))"
let expectedBundleName = "sdk-with-dash_target_with_dash"
let sources: [SourceFile] = ["/ViewController.m", "/ViewController2.swift"]
let resources: [ResourceFileElement] = [.file(path: "/AbsolutePath/Project/Resources/image.png")]
let target = Target.test(name: targetName, product: .staticLibrary, sources: sources, resources: .init(resources))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Buy
import KSCrash_Installations
import Pay
import SwiftUI

Expand All @@ -7,6 +8,8 @@ struct ContentView: View {
// Use Mobile Buy SDK
_ = Card.CreditCard(firstName: "", lastName: "", number: "", expiryMonth: "", expiryYear: "")
_ = PayAddress()
// Use KSCrash
_ = KSCrashInstallationStandard()
}

var body: some View {
Expand Down
2 changes: 2 additions & 0 deletions fixtures/ios_app_with_spm_dependencies/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let project = Project(
dependencies: [
.external(name: "Buy"),
.external(name: "Pay"),
.external(name: "KSCrash"),
.sdk(name: "c++", type: .library, status: .required),
]
),
.target(
Expand Down
11 changes: 10 additions & 1 deletion fixtures/ios_app_with_spm_dependencies/Tuist/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"originHash" : "6ed714a381b77d6163cdedad8446d73742573b78187635c59175fa22d0a3df2c",
"originHash" : "fc3374625da9a1a42edd765287e0715853d94414eda5959e9d420d041ec3b010",
"pins" : [
{
"identity" : "kscrash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/kstenerud/KSCrash",
"state" : {
"revision" : "0ad825211ec38404e03b6f677a2312deb728528a",
"version" : "1.17.1"
}
},
{
"identity" : "mobile-buy-sdk-ios",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 2 additions & 0 deletions fixtures/ios_app_with_spm_dependencies/Tuist/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ let package = Package(
dependencies: [
// Has space symbols in package name
.package(url: "https://github.com/Shopify/mobile-buy-sdk-ios", exact: "12.0.0"),
// Has targets with slash symbols in their names
.package(url: "https://github.com/kstenerud/KSCrash", exact: "1.17.1"),
]
)