Skip to content

Commit

Permalink
Correctly handle packages such as xcodeproj
Browse files Browse the repository at this point in the history
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
  • Loading branch information
henrik-dmg committed Jan 19, 2024
1 parent 2a7a83f commit c5303f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
28 changes: 15 additions & 13 deletions Sources/FileOrganiserKit/FileAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,43 @@ public struct FileAttributes {
// MARK: - Nested Types

enum FileAttributesError: LocalizedError {
case resourceValuesNotFound
case unknownCreationDate

var errorDescription: String? {
switch self {
case .resourceValuesNotFound:
return "One or more resource values was not found"
case .unknownCreationDate:
return "Could not read creation date of file"
}
}
}

// MARK: - Properties

public let fileSizeInBytes: Int
static let urlResourceKeys = Set<URLResourceKey>([.isRegularFileKey, .totalFileSizeKey, .creationDateKey, .isPackageKey, .fileSizeKey])

public let fileSizeInBytes: Int?
public let creationDate: Date
public let isPackage: Bool

// MARK: - Init

init?(values: URLResourceValues) throws {
guard
let isRegularFile = values.isRegularFile,
isRegularFile
else {
return nil
guard let isRegularFile = values.isRegularFile, let isPackage = values.isPackage, isRegularFile || isPackage else {
return nil // Not a file or not a package
}
guard let creationDate = values.creationDate, let fileSizeInBytes = values.totalFileSize else {
throw FileAttributesError.resourceValuesNotFound
guard let creationDate = values.creationDate else {
throw FileAttributesError.unknownCreationDate
}
self.fileSizeInBytes = fileSizeInBytes
self.fileSizeInBytes = values.fileSize ?? values.totalFileSize
self.creationDate = creationDate
self.isPackage = isPackage
}

#if DEBUG
init(fileSizeInBytes: Int, creationDate: Date) {
init(fileSizeInBytes: Int?, creationDate: Date, isPackage: Bool) {
self.fileSizeInBytes = fileSizeInBytes
self.creationDate = creationDate
self.isPackage = isPackage
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Sources/FileOrganiserKit/FileHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct FileHandler: FileHandlerProtocol {
}

public func resourceValues(of url: URL) throws -> FileAttributes? {
let resourceValues = try url.resourceValues(forKeys: [.isRegularFileKey, .totalFileSizeKey, .creationDateKey])
let resourceValues = try url.resourceValues(forKeys: FileAttributes.urlResourceKeys)
return try FileAttributes(values: resourceValues)
}

Expand Down
10 changes: 4 additions & 6 deletions Sources/FileOrganiserKit/Organiser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Organiser {

enum FileProcessingResult {
case skipped(reason: SkipReason)
case written(sourcePath: String, destinationPath: String, fileSize: Int)
case written(sourcePath: String, destinationPath: String, fileSize: Int?)
case notRegularFile
case dryRun(sourcePath: String, destinationPath: String)
}
Expand Down Expand Up @@ -38,8 +38,6 @@ public class Organiser {
private let fileHandler: FileHandlerProtocol
private let logger: LoggerProtocol

private let urlResourceKeys = Set<URLResourceKey>([.creationDateKey, .totalFileSizeKey, .isRegularFileKey])

// MARK: - Init

public init(fileHandler: FileHandlerProtocol, logger: LoggerProtocol) {
Expand Down Expand Up @@ -99,8 +97,8 @@ public class Organiser {

try fileHandler.contentsOfDirectory(
at: sourceURL,
includingPropertiesForKeys: Array(urlResourceKeys),
options: [.skipsHiddenFiles, .producesRelativePathURLs],
includingPropertiesForKeys: Array(FileAttributes.urlResourceKeys),
options: [.skipsHiddenFiles, .producesRelativePathURLs, .skipsPackageDescendants],
shouldSoftFail: shouldSoftFail
) { url in
if let glob, !glob.match(url.path) {
Expand All @@ -125,7 +123,7 @@ public class Organiser {
case .written(let sourcePath, let destinationPath, let fileSize):
logger.logFileWritten(sourcePath: sourcePath, destinationPath: destinationPath, fileStrategy: fileStrategy)
filesWritten += 1
bytesWritten += fileSize
bytesWritten += fileSize ?? 0
case .dryRun(let sourcePath, let destinationPath):
logger.logDryRun(sourcePath: sourcePath, destinationPath: destinationPath, fileStrategy: fileStrategy)
case .notRegularFile:
Expand Down
10 changes: 5 additions & 5 deletions Tests/FileOrganiserKitTests/OrganiserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class OrganiserTests: XCTestCase {
false
}
fakeFileHandler.resourceValuesHandler = { _ in
FileAttributes(fileSizeInBytes: 100, creationDate: .now)
FileAttributes(fileSizeInBytes: 100, creationDate: .now, isPackage: false)
}
fakeFileHandler.createDirectoryHandler = { url in
XCTAssertEqual(url.relativePath, fakeFileStructure.destinationURL.relativePath + "/" + fakeImageCreationDate.path(for: .year))
Expand Down Expand Up @@ -69,7 +69,7 @@ final class OrganiserTests: XCTestCase {
false
}
fakeFileHandler.resourceValuesHandler = { _ in
FileAttributes(fileSizeInBytes: 100, creationDate: .now)
FileAttributes(fileSizeInBytes: 100, creationDate: .now, isPackage: false)
}
fakeFileHandler.createDirectoryHandler = { url in
XCTAssertEqual(url.relativePath, fakeFileStructure.destinationURL.relativePath + "/" + fakeImageCreationDate.path(for: .year))
Expand Down Expand Up @@ -100,7 +100,7 @@ final class OrganiserTests: XCTestCase {
true
}
fakeFileHandler.resourceValuesHandler = { _ in
FileAttributes(fileSizeInBytes: 100, creationDate: .now)
FileAttributes(fileSizeInBytes: 100, creationDate: .now, isPackage: false)
}
fakeFileHandler.createDirectoryHandler = { url in
XCTFail("Should not attempt to create any directories")
Expand All @@ -127,7 +127,7 @@ final class OrganiserTests: XCTestCase {
fakeFileStructure.textFileURL,
]
fakeFileHandler.resourceValuesHandler = { _ in
FileAttributes(fileSizeInBytes: 100, creationDate: .now)
FileAttributes(fileSizeInBytes: 100, creationDate: .now, isPackage: false)
}

let result = try runOrganiser(
Expand Down Expand Up @@ -182,7 +182,7 @@ final class OrganiserTests: XCTestCase {
false
}
fakeFileHandler.resourceValuesHandler = { _ in
FileAttributes(fileSizeInBytes: 100, creationDate: .now)
FileAttributes(fileSizeInBytes: 100, creationDate: .now, isPackage: false)
}
fakeFileHandler.createDirectoryHandler = { url in
XCTAssertEqual(url.relativePath, fakeFileStructure.destinationURL.relativePath + "/" + fakeImageCreationDate.path(for: .year))
Expand Down

0 comments on commit c5303f8

Please sign in to comment.