Emcee

Форк
0
/
ResourceLocation+SubprocessArgument.swift 
70 строк · 3.0 Кб
1
import Foundation
2
import PathLib
3
import ProcessController
4
import ResourceLocation
5

6
private class ResolvableResourceLocationArg: SubprocessArgument, CustomStringConvertible {
7
    private let resolvableResourceLocation: ResolvableResourceLocation
8
    private let implicitFilenameInArchive: String?
9
    
10
    enum ArgError: Error, CustomStringConvertible {
11
        case cannotResolve(ResolvableResourceLocation, containerPath: AbsolutePath)
12
        
13
        var description: String {
14
            switch self {
15
            case .cannotResolve(let resolvableResourceLocation, let containerPath):
16
                return "Unable to generate direct local path for resource location '\(resolvableResourceLocation)': "
17
                + "the location is not accessible directly, and it has been fetched into container path '\(containerPath)'. "
18
                + "But it is not possible to determine a file inside container as no file name was provided in the resource location. "
19
                + "You can provide a filename inside archive via URL fragment, e.g. http://example.com/archive.zip#filename"
20
            }
21
        }
22
    }
23

24
    public init(
25
        resolvableResourceLocation: ResolvableResourceLocation,
26
        implicitFilenameInArchive: String?
27
    ) {
28
        self.resolvableResourceLocation = resolvableResourceLocation
29
        self.implicitFilenameInArchive = implicitFilenameInArchive
30
    }
31
    
32
    public func stringValue() throws -> String {
33
        let result = try resolvableResourceLocation.resolve()
34
        switch result {
35
        case .directlyAccessibleFile(let path):
36
            return try path.stringValue()
37
        case .contentsOfArchive(let containerPath, let filenameInArchive):
38
            if let filenameInArchive = filenameInArchive {
39
                return try containerPath.appending(filenameInArchive).stringValue()
40
            } else if let implicitFilenameInArchive = implicitFilenameInArchive {
41
                return try containerPath.appending(implicitFilenameInArchive).stringValue()
42
            } else {
43
                throw ArgError.cannotResolve(resolvableResourceLocation, containerPath: containerPath)
44
            }
45
        }
46
    }
47
    
48
    public var description: String {
49
        switch resolvableResourceLocation.resourceLocation {
50
        case .localFilePath(let path):
51
            return path
52
        case .remoteUrl(let url, _):
53
            var items = ["url: \(url)"]
54
            if let implicitFilenameInArchive = implicitFilenameInArchive {
55
                items.append("file: \(implicitFilenameInArchive)")
56
            }
57
            return "<\(type(of: self)): " + items.joined(separator: " ") + ">"
58
        }
59
    }
60
}
61

62
public extension ResolvableResourceLocation {
63
    func asArgumentWith(implicitFilenameInArchive: String) -> SubprocessArgument {
64
        return ResolvableResourceLocationArg(resolvableResourceLocation: self, implicitFilenameInArchive: implicitFilenameInArchive)
65
    }
66
    
67
    func asArgument() -> SubprocessArgument {
68
        return ResolvableResourceLocationArg(resolvableResourceLocation: self, implicitFilenameInArchive: nil)
69
    }
70
}
71

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.