Skip to content

Commit

Permalink
Support PBXGroup build file elements eg localized variants (#35)
Browse files Browse the repository at this point in the history
* Add localized file to ExampleProject and associated test

* Refactor PBXBuildFile.path(projectFolder:) to leverage guard statements

* Refactor PBXBuildFile.path(projectFolder:) to return an array of paths

* Support PBXGroup build file elements eg localized variants
  • Loading branch information
alexdeem authored Oct 2, 2024
1 parent da2add4 commit aa3410f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
52 changes: 35 additions & 17 deletions Sources/DependencyCalculator/DependencyGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,41 @@ import Workspace
import XcodeProj

extension PBXBuildFile {
func path(projectFolder: Path) -> Path? {
if let path = file?.path {
var intermediatePath = Path()
func paths(projectFolder: Path) -> [Path] {
guard let file else {
Logger.warning("PBXBuildFile without file: self=\(self), \n self.product=\(String(describing: product))")
return []
}

let paths: [String] = switch file {
case let group as PBXGroup:
group.children.compactMap { $0.path }
default:
if let path = file.path {
[path]
} else {
[]
}
}

guard paths.count > 0 else {
Logger.warning("File without paths: self=\(self), \n self.file=\(String(describing: file)), \n self.product=\(String(describing: product))")
return []
}

var intermediatePath = Path()

var parent = file?.parent
var parent = file.parent

while parent?.path != nil {
if let parentPath = parent?.path {
intermediatePath = Path(parentPath) + intermediatePath
}
parent = parent?.parent
while parent?.path != nil {
if let parentPath = parent?.path {
intermediatePath = Path(parentPath) + intermediatePath
}
parent = parent?.parent
}

return projectFolder + intermediatePath + path
} else {
Logger.warning("File without path: self=\(self), \n self.file=\(String(describing: file)), \n self.product=\(String(describing: product))")
return nil
return paths.map {
projectFolder + intermediatePath + $0
}
}
}
Expand Down Expand Up @@ -284,13 +302,13 @@ extension WorkspaceInfo {
}

// Source Files
var filesPaths = try Set(target.sourcesBuildPhase()?.files?.compactMap { file in
file.path(projectFolder: path.parent())
var filesPaths = try Set(target.sourcesBuildPhase()?.files?.flatMap { file in
file.paths(projectFolder: path.parent())
} ?? [])

// Resources
filesPaths = try filesPaths.union(Set(target.resourcesBuildPhase()?.files?.compactMap { file in
file.path(projectFolder: path.parent())
filesPaths = try filesPaths.union(Set(target.resourcesBuildPhase()?.files?.flatMap { file in
file.paths(projectFolder: path.parent())
} ?? []))

// Establish dependencies based on linked frameworks build phase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
27F4680529B145A700A93E94 /* ExampleLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 27F4680429B145A700A93E94 /* ExampleLibrary.framework */; };
27F4680929B145F900A93E94 /* ExampleTargetLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F4680829B145F900A93E94 /* ExampleTargetLibrary.swift */; };
27F4680C29B1482E00A93E94 /* ExamplePackage in Frameworks */ = {isa = PBXBuildFile; productRef = 27F4680B29B1482E00A93E94 /* ExamplePackage */; };
B24BBDBD2CAD7228005E6DAC /* Example.xib in Resources */ = {isa = PBXBuildFile; fileRef = B24BBDBC2CAD7228005E6DAC /* Example.xib */; };
FDE924012A5C5AC300D61FD3 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDE924002A5C5AC300D61FD3 /* ContentView.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -88,6 +89,8 @@
27F4680429B145A700A93E94 /* ExampleLibrary.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = ExampleLibrary.framework; sourceTree = BUILT_PRODUCTS_DIR; };
27F4680829B145F900A93E94 /* ExampleTargetLibrary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleTargetLibrary.swift; sourceTree = "<group>"; };
27F4680A29B1469D00A93E94 /* ExamplePackage */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = ExamplePackage; sourceTree = "<group>"; };
B24BBDBB2CAD7228005E6DAC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/Example.xib; sourceTree = "<group>"; };
B24BBDBF2CAD7244005E6DAC /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Example.strings; sourceTree = "<group>"; };
FDE924002A5C5AC300D61FD3 /* ContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -167,6 +170,7 @@
FDE923FE2A5C5AC300D61FD3 /* Deep */,
276DB5C229B144C900E5C615 /* Assets.xcassets */,
276DB5C429B144C900E5C615 /* Preview Content */,
B24BBDBC2CAD7228005E6DAC /* Example.xib */,
);
path = ExampleProject;
sourceTree = "<group>";
Expand Down Expand Up @@ -405,6 +409,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B24BBDBD2CAD7228005E6DAC /* Example.xib in Resources */,
276DB5C629B144C900E5C615 /* Preview Assets.xcassets in Resources */,
276DB5C329B144C900E5C615 /* Assets.xcassets in Resources */,
);
Expand Down Expand Up @@ -509,6 +514,18 @@
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
B24BBDBC2CAD7228005E6DAC /* Example.xib */ = {
isa = PBXVariantGroup;
children = (
B24BBDBB2CAD7228005E6DAC /* Base */,
B24BBDBF2CAD7244005E6DAC /* en */,
);
name = Example.xib;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */

/* Begin XCBuildConfiguration section */
276DB5DD29B144CA00E5C615 /* Debug */ = {
isa = XCBuildConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13142" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12042"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
</objects>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 16 additions & 0 deletions Tests/SelectiveTestingTests/SelectiveTestingProjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ final class SelectiveTestingProjectTests: XCTestCase {
testTool.mainProjectUITests,
]))
}

func testProjectLocalizedPathChange() async throws {
// given
let tool = try testTool.createSUT(config: nil,
basePath: "ExampleProject.xcodeproj")
// when
try testTool.changeFile(at: testTool.projectPath + "ExampleProject/Base.lproj/Example.xib")

// then
let result = try await tool.run()
XCTAssertEqual(result, Set([
testTool.mainProjectMainTarget,
testTool.mainProjectTests,
testTool.mainProjectUITests,
]))
}
}

0 comments on commit aa3410f

Please sign in to comment.