-
Notifications
You must be signed in to change notification settings - Fork 16
/
Checkouts.swift
31 lines (28 loc) · 1023 Bytes
/
Checkouts.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// Checkouts.swift
// SwiftPackageListCore
//
// Created by Felix Herrmann on 26.12.23.
//
import Foundation
struct Checkouts: Directory {
let url: URL
}
extension Checkouts {
func license(checkoutURL: URL) throws -> String? {
let checkoutName = checkoutURL.packageIdentity
let checkoutPath = url.appendingPathComponent(checkoutName)
let packageFiles = try FileManager.default.contentsOfDirectory(
at: checkoutPath,
includingPropertiesForKeys: [.isRegularFileKey, .localizedNameKey],
options: .skipsHiddenFiles
)
let licenseFileURL = packageFiles.first { packageFile in
let fileName = packageFile.deletingPathExtension().lastPathComponent.lowercased()
let allowedFileNames = ["license", "licence", "copying"]
return allowedFileNames.contains(fileName)
}
guard let licenseFileURL else { return nil }
return try String(contentsOf: licenseFileURL, encoding: .utf8)
}
}