Skip to content

Commit 20d3f51

Browse files
committed
Ignore non-source files from .gitignore, add root level files.
1 parent ef063a7 commit 20d3f51

File tree

6 files changed

+41
-11
lines changed

6 files changed

+41
-11
lines changed

Sources/SourceControl/GitRepository.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ public class GitRepository: Repository, WorkingCheckout {
391391
return localFileSystem.isDirectory(AbsolutePath(firstLine))
392392
}
393393

394+
public func getIgnoredFiles() -> [AbsolutePath] {
395+
return queue.sync {
396+
let result = try? Process.checkNonZeroExit(
397+
args: Git.tool, "-C", path.asString, "ls-files", "-o", "-i", "--exclude-standard").chomp()
398+
return result?.split(separator: "\n").map(String.init).map {
399+
return path.appending(RelativePath($0))
400+
} ?? []
401+
}
402+
}
403+
394404
// MARK: Git Operations
395405

396406
/// Resolve a "treeish" to a concrete hash.

Sources/SourceControl/InMemoryGitRepository.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ extension InMemoryGitRepository: WorkingCheckout {
268268
public func isAlternateObjectStoreValid() -> Bool {
269269
return true
270270
}
271+
272+
public func getIgnoredFiles() -> [AbsolutePath] {
273+
return []
274+
}
271275
}
272276

273277
/// This class implement provider for in memeory git repository.

Sources/SourceControl/Repository.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ public protocol WorkingCheckout {
203203

204204
/// Returns true if there is an alternative store in the checkout and it is valid.
205205
func isAlternateObjectStoreValid() -> Bool
206+
207+
/// Returns the `AbsolutePath` of the files ignored by git.
208+
func getIgnoredFiles() -> [AbsolutePath]
206209
}
207210

208211
/// A single repository revision.

Sources/Xcodeproj/generate().swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import POSIX
1313
import PackageGraph
1414
import PackageModel
1515
import PackageLoading
16+
import SourceControl
1617
import Utility
1718

1819
public struct XcodeprojOptions {
@@ -47,6 +48,7 @@ public func generate(
4748
outputDir: AbsolutePath,
4849
projectName: String,
4950
graph: PackageGraph,
51+
repositoryProvider: RepositoryProvider = GitRepositoryProvider(),
5052
options: XcodeprojOptions
5153
) throws -> AbsolutePath {
5254
// Note that the output directory might be completely separate from the
@@ -69,10 +71,21 @@ public func generate(
6971
// references in the project.
7072
let extraDirs = try findDirectoryReferences(path: srcroot)
7173

72-
// Find non-source files in the source directories that should be added
74+
// Find non-source files in the source directories and root that should be added
7375
// as a reference to the project.
74-
var extraFiles = try findNonSourceFiles(path: srcroot.appending(component: "Sources"))
75-
extraFiles.append(contentsOf: try findNonSourceFiles(path: srcroot.appending(component: "Tests")))
76+
var extraFiles = try findNonSourceFiles(path: srcroot)
77+
if let sourcesExtraFiles = try? findNonSourceFiles(path: srcroot.appending(component: "Sources"), recursively: true) {
78+
extraFiles.append(contentsOf: sourcesExtraFiles)
79+
}
80+
if let testsExtraFiles = try? findNonSourceFiles(path: srcroot.appending(component: "Tests"), recursively: true) {
81+
extraFiles.append(contentsOf: testsExtraFiles)
82+
}
83+
84+
// Get the ignored files and exclude them
85+
let ignoredFiles = try repositoryProvider.openCheckout(at: srcroot).getIgnoredFiles()
86+
extraFiles = extraFiles.filter {
87+
return !ignoredFiles.contains($0)
88+
}
7689

7790
/// Generate the contents of project.xcodeproj (inside the .xcodeproj).
7891
try open(xcodeprojPath.appending(component: "project.pbxproj")) { stream in
@@ -188,8 +201,8 @@ func findDirectoryReferences(path: AbsolutePath) throws -> [AbsolutePath] {
188201
}
189202

190203
/// Finds the non-source files from `path` recursively
191-
func findNonSourceFiles(path: AbsolutePath) throws -> [AbsolutePath] {
192-
let filesFromPath = try walk(path, recursively: true)
204+
func findNonSourceFiles(path: AbsolutePath, recursively: Bool = false) throws -> [AbsolutePath] {
205+
let filesFromPath = try walk(path, recursively: recursively)
193206

194207
return filesFromPath.filter {
195208
if !isFile($0) { return false }

Tests/XcodeprojTests/GenerateXcodeprojTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class GenerateXcodeprojTests: XCTestCase {
6767
let options = XcodeprojOptions(xcconfigOverrides: AbsolutePath("/doesntexist"))
6868
do {
6969
_ = try xcodeProject(xcodeprojPath: AbsolutePath.root.appending(component: "xcodeproj"),
70-
graph: graph, extraDirs: [], options: options, fileSystem: fileSystem)
70+
graph: graph, extraDirs: [], extraFiles: [], options: options, fileSystem: fileSystem)
7171
XCTFail("Project generation should have failed")
7272
} catch ProjectGenerationError.xcconfigOverrideNotFound(let path) {
7373
XCTAssertEqual(options.xcconfigOverrides, path)
@@ -85,7 +85,7 @@ class GenerateXcodeprojTests: XCTestCase {
8585
XCTAssertFalse(diagnostics.hasErrors)
8686

8787
_ = try xcodeProject(xcodeprojPath: AbsolutePath.root.appending(component: "xcodeproj"),
88-
graph: graph, extraDirs: [], options: XcodeprojOptions(), fileSystem: fileSystem,
88+
graph: graph, extraDirs: [], extraFiles: [], options: XcodeprojOptions(), fileSystem: fileSystem,
8989
warningStream: warningStream)
9090

9191
let warnings = warningStream.bytes.asReadableString

Tests/XcodeprojTests/PackageGraphTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PackageGraphTests: XCTestCase {
3939

4040
let options = XcodeprojOptions(xcconfigOverrides: AbsolutePath("/Overrides.xcconfig"))
4141

42-
let project = try xcodeProject(xcodeprojPath: AbsolutePath.root.appending(component: "xcodeproj"), graph: g, extraDirs: [], options: options, fileSystem: fs)
42+
let project = try xcodeProject(xcodeprojPath: AbsolutePath.root.appending(component: "xcodeproj"), graph: g, extraDirs: [], extraFiles: [], options: options, fileSystem: fs)
4343

4444
XcodeProjectTester(project) { result in
4545
result.check(projectDir: "Bar")
@@ -149,7 +149,7 @@ class PackageGraphTests: XCTestCase {
149149
products: [.library(name: "Bar", type: .dynamic, targets: ["Foo"])],
150150
targets: [.target(name: "Foo")]),
151151
], root: "/Foo", diagnostics: diagnostics, in: fs)
152-
let project = try xcodeProject(xcodeprojPath: AbsolutePath("/Foo/build").appending(component: "xcodeproj"), graph: g, extraDirs: [], options: XcodeprojOptions(), fileSystem: fs)
152+
let project = try xcodeProject(xcodeprojPath: AbsolutePath("/Foo/build").appending(component: "xcodeproj"), graph: g, extraDirs: [], extraFiles: [], options: XcodeprojOptions(), fileSystem: fs)
153153
XcodeProjectTester(project) { result in
154154
result.check(target: "Foo") { targetResult in
155155
targetResult.check(productType: .framework)
@@ -175,7 +175,7 @@ class PackageGraphTests: XCTestCase {
175175
let g = loadMockPackageGraph([
176176
"/Bar": Package(name: "Bar", targets: [Target(name: "swift", dependencies: ["Sea", "Sea2"])]),
177177
], root: "/Bar", diagnostics: diagnostics, in: fs)
178-
let project = try xcodeProject(xcodeprojPath: AbsolutePath("/Bar/build").appending(component: "xcodeproj"), graph: g, extraDirs: [], options: XcodeprojOptions(), fileSystem: fs)
178+
let project = try xcodeProject(xcodeprojPath: AbsolutePath("/Bar/build").appending(component: "xcodeproj"), graph: g, extraDirs: [], extraFiles: [], options: XcodeprojOptions(), fileSystem: fs)
179179

180180
XcodeProjectTester(project) { result in
181181
result.check(target: "swift") { targetResult in
@@ -209,7 +209,7 @@ class PackageGraphTests: XCTestCase {
209209
"/Pkg": Package(name: "Pkg", targets: [Target(name: "LibraryTests", dependencies: ["Library", "HelperTool"])]),
210210
], root: "/Pkg", diagnostics: diagnostics, in: fs)
211211

212-
let project = try xcodeProject(xcodeprojPath: AbsolutePath.root.appending(component: "xcodeproj"), graph: g, extraDirs: [], options: XcodeprojOptions(), fileSystem: fs)
212+
let project = try xcodeProject(xcodeprojPath: AbsolutePath.root.appending(component: "xcodeproj"), graph: g, extraDirs: [], extraFiles: [], options: XcodeprojOptions(), fileSystem: fs)
213213

214214
XcodeProjectTester(project) { result in
215215
result.check(projectDir: "Pkg")

0 commit comments

Comments
 (0)