@@ -38,20 +38,15 @@ enum MetalCompiler {
38
38
to: directory,
39
39
for: platform,
40
40
platformVersion: platformVersion
41
- ) . flatMap { _ in
42
- if keepSources {
43
- return . success( )
44
- }
45
-
46
- for source in shaderSources {
47
- do {
48
- try FileManager . default. removeItem ( at: source)
49
- } catch {
50
- return . failure( . failedToDeleteShaderSource( source, error) )
51
- }
41
+ )
42
+ . replacingSuccessValue ( with: ( ) )
43
+ . andThen ( if: !keepSources) { _ in
44
+ shaderSources. tryForEach { source in
45
+ FileManager . default. removeItem ( at: source)
46
+ . mapError { error in
47
+ . failedToDeleteShaderSource( source, error)
48
+ }
52
49
}
53
-
54
- return . success( )
55
50
}
56
51
}
57
52
@@ -61,7 +56,8 @@ enum MetalCompiler {
61
56
/// - destination: The directory to output `default.metallib` to.
62
57
/// - platform: The platform to compile for.
63
58
/// - platformVersion: The platform version to target during compilation.
64
- /// - Returns: Returns the location of the resulting `metallib`. If an error occurs, a failure is returned.
59
+ /// - Returns: Returns the location of the resulting `metallib`. If an error
60
+ /// occurs, a failure is returned.
65
61
static func compileMetalShaders(
66
62
_ sources: [ URL ] ,
67
63
to destination: URL ,
@@ -71,47 +67,37 @@ enum MetalCompiler {
71
67
// Create a temporary directory for compilation
72
68
let tempDirectory = FileManager . default. temporaryDirectory
73
69
. appendingPathComponent ( " metal_compilation- \( UUID ( ) . uuidString) " )
74
- do {
75
- try FileManager . default. createDirectory ( at: tempDirectory)
76
- } catch {
77
- return . failure( . failedToCreateTemporaryCompilationDirectory( tempDirectory, error) )
78
- }
70
+ let archive = tempDirectory. appendingPathComponent ( " default.metal-ar " )
79
71
80
- // Compile the shaders into `.air` files
81
- var airFiles : [ URL ] = [ ]
82
- for shaderSource in sources {
83
- let outputFileName = shaderSource. deletingPathExtension ( ) . appendingPathExtension ( " air " )
84
- . lastPathComponent
85
- let outputFile = tempDirectory. appendingPathComponent ( outputFileName)
86
-
87
- let compilationResult = compileShader (
88
- shaderSource,
89
- to: outputFile,
90
- for: platform,
91
- platformVersion: platformVersion
92
- )
93
-
94
- if case let . failure( error) = compilationResult {
95
- return . failure( error)
72
+ return FileManager . default. createDirectory (
73
+ at: tempDirectory,
74
+ onError: MetalCompilerError . failedToCreateTemporaryCompilationDirectory
75
+ )
76
+ . andThen { _ in
77
+ // Compile the shaders into `.air` files
78
+ sources. tryMap { shaderSource in
79
+ let outputFileName = shaderSource. deletingPathExtension ( )
80
+ . appendingPathExtension ( " air " ) . lastPathComponent
81
+ let outputFile = tempDirectory. appendingPathComponent ( outputFileName)
82
+
83
+ return compileShader (
84
+ shaderSource,
85
+ to: outputFile,
86
+ for: platform,
87
+ platformVersion: platformVersion
88
+ ) . replacingSuccessValue ( with: outputFile)
96
89
}
97
- airFiles. append ( outputFile)
98
90
}
99
-
100
- // Combine the compiled shaders into a `.metal-ar` archive
101
- let archive = tempDirectory. appendingPathComponent ( " default.metal-ar " )
102
- let archiveResult = createArchive ( at: archive, from: airFiles, for: platform)
103
- if case let . failure( error) = archiveResult {
104
- return . failure( error)
91
+ . andThen { airFiles in
92
+ // Combine the compiled shaders into a `.metal-ar` archive
93
+ createArchive ( at: archive, from: airFiles, for: platform)
105
94
}
106
-
107
- // Convert the `metal-ar` archive into a `metallib` library
108
- let library = destination. appendingPathComponent ( " default.metallib " )
109
- let libraryResult = createLibrary ( at: library, from: archive, for: platform)
110
- if case let . failure( error) = libraryResult {
111
- return . failure( error)
95
+ . andThen { _ in
96
+ // Convert the `metal-ar` archive into a `metallib` library
97
+ let library = destination. appendingPathComponent ( " default.metallib " )
98
+ return createLibrary ( at: library, from: archive, for: platform)
99
+ . replacingSuccessValue ( with: library)
112
100
}
113
-
114
- return . success( library)
115
101
}
116
102
117
103
/// Compiles a metal shader file into an `air` file.
0 commit comments