@@ -29,20 +29,29 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
29
29
30
30
def pathToString (p : Path ): String = p.toString
31
31
32
- private def systemCmd [A ](cmd : String , args : List [String ], bldr : java.lang.ProcessBuilder )(fn : java.lang.Process => A ): IO [A ] = IO .blocking {
32
+ private def systemCmd [A ](
33
+ cmd : String ,
34
+ args : List [String ],
35
+ bldr : java.lang.ProcessBuilder
36
+ )(fn : java.lang.Process => A ): IO [A ] = IO .blocking {
33
37
// Start the process
34
38
val process = bldr.start()
35
39
val result = fn(process)
36
40
// Wait for the process to complete and check the exit value
37
41
val exitCode = process.waitFor()
38
42
if (exitCode != 0 ) {
39
- throw new RuntimeException (s " command $cmd ${args.mkString(" " )} failed with exit code: $exitCode" )
43
+ throw new RuntimeException (
44
+ s " command $cmd ${args.mkString(" " )} failed with exit code: $exitCode"
45
+ )
40
46
}
41
47
42
48
result
43
49
}
44
50
45
- private def processBldr (cmd : String , args : List [String ]): IO [java.lang.ProcessBuilder ] = IO {
51
+ private def processBldr (
52
+ cmd : String ,
53
+ args : List [String ]
54
+ ): IO [java.lang.ProcessBuilder ] = IO {
46
55
val processBuilder = new java.lang.ProcessBuilder ()
47
56
val command = new java.util.ArrayList [String ]()
48
57
(cmd :: args).foreach(command.add(_))
@@ -52,7 +61,6 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
52
61
53
62
def system (cmd : String , args : List [String ]): IO [Unit ] =
54
63
processBldr(cmd, args).flatMap { processBuilder =>
55
-
56
64
// Redirect output and error streams
57
65
processBuilder.redirectOutput(ProcessBuilder .Redirect .INHERIT )
58
66
processBuilder.redirectError(ProcessBuilder .Redirect .INHERIT )
@@ -62,13 +70,14 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
62
70
val gitShaHead : IO [String ] = {
63
71
val args = " rev-parse" :: " HEAD" :: Nil
64
72
processBldr(" git" , args).flatMap { processBuilder =>
65
-
66
73
// Combine stdout and stderr, and pipe the combined output for capturing
67
74
processBuilder.redirectErrorStream(true )
68
75
processBuilder.redirectOutput(ProcessBuilder .Redirect .PIPE )
69
- systemCmd(" git" , args, processBuilder) { process =>
76
+ systemCmd(" git" , args, processBuilder) { process =>
70
77
// Prepare to read the combined output
71
- val reader = new java.io.BufferedReader (new java.io.InputStreamReader (process.getInputStream))
78
+ val reader = new java.io.BufferedReader (
79
+ new java.io.InputStreamReader (process.getInputStream)
80
+ )
72
81
val output = new StringBuilder
73
82
var line : String = null
74
83
@@ -88,7 +97,8 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
88
97
override val parallelF : cats.Parallel [IO ] = IO .parallelForIO
89
98
90
99
private val parResource : Resource [IO , Par .EC ] =
91
- Resource .make(IO (Par .newService()))(es => IO (Par .shutdownService(es)))
100
+ Resource
101
+ .make(IO (Par .newService()))(es => IO (Par .shutdownService(es)))
92
102
.map(Par .ecFromService(_))
93
103
94
104
def withEC [A ](fn : Par .EC => IO [A ]): IO [A ] =
@@ -129,8 +139,11 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
129
139
}
130
140
131
141
def readHashed [A <: GeneratedMessage , H ](
132
- path : Path
133
- )(implicit gmc : GeneratedMessageCompanion [A ], algo : Algo [H ]): IO [Hashed [H , A ]] =
142
+ path : Path
143
+ )(implicit
144
+ gmc : GeneratedMessageCompanion [A ],
145
+ algo : Algo [H ]
146
+ ): IO [Hashed [H , A ]] =
134
147
IO .blocking {
135
148
val bytes = Files .readAllBytes(path)
136
149
val a = gmc.parseFrom(bytes)
@@ -155,13 +168,13 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
155
168
ifacePaths.traverse(read[proto.Interfaces ](_)),
156
169
packagePaths.traverse(read[proto.Packages ](_))
157
170
).flatMapN { (ifs, packs) =>
158
- IO .fromTry(
159
- ProtoConverter .packagesFromProto(
160
- ifs.flatMap(_.interfaces),
161
- packs.flatMap(_.packages)
162
- )
171
+ IO .fromTry(
172
+ ProtoConverter .packagesFromProto(
173
+ ifs.flatMap(_.interfaces),
174
+ packs.flatMap(_.packages)
163
175
)
164
- }
176
+ )
177
+ }
165
178
166
179
def readInterfaces (paths : List [Path ]): IO [List [Package .Interface ]] =
167
180
readInterfacesAndPackages(paths, Nil ).map(_._1)
@@ -172,7 +185,12 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
172
185
def readLibrary (path : Path ): IO [Hashed [Algo .Blake3 , proto.Library ]] =
173
186
readHashed[proto.Library , Algo .Blake3 ](path)
174
187
175
- def fetchHash [A ](algo : Algo [A ], hash : HashValue [A ], path : Path , uri : String ): F [Unit ] = {
188
+ def fetchHash [A ](
189
+ algo : Algo [A ],
190
+ hash : HashValue [A ],
191
+ path : Path ,
192
+ uri : String
193
+ ): F [Unit ] = {
176
194
// Create a Blaze client resource
177
195
import org .http4s ._
178
196
import fs2 .io .file .{Files , Path => Fs2Path , CopyFlags , CopyFlag }
@@ -201,41 +219,51 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
201
219
)
202
220
203
221
parent.traverse_(p => filesIO.createDirectories(Fs2Path .fromNioPath(p))) *>
204
- (clientResource,
205
- tempFileRes,
206
- Resource .eval(IO (Uri .unsafeFromString(uri)))
207
- ).tupled.use { case (client, tempPath, uri) =>
208
- // Create an HTTP GET request
209
- val request = Request [IO ](method = Method .GET , uri = uri)
210
-
211
- // Stream the response body and write it to the specified file path
212
- client.stream(request)
213
- .flatMap { response =>
214
- if (response.status.isSuccess) {
215
- response.body
216
- } else {
217
- fs2.Stream .raiseError[IO ](new Exception (s " Failed to download from $uri: ${response.status}" ))
218
- }
219
- }
220
- .broadcastThrough(
221
- Files [IO ].writeAll(tempPath),
222
- hashFile
223
- )
224
- .compile
225
- .lastOrError
226
- .flatMap { computedHash =>
227
- if (computedHash == hash) {
228
- // move it atomically to output
229
- filesIO.move(
230
- source = Fs2Path .fromNioPath(tempPath),
231
- target = Fs2Path .fromNioPath(path),
232
- CopyFlags (CopyFlag .AtomicMove ))
222
+ (
223
+ clientResource,
224
+ tempFileRes,
225
+ Resource .eval(IO (Uri .unsafeFromString(uri)))
226
+ ).tupled.use { case (client, tempPath, uri) =>
227
+ // Create an HTTP GET request
228
+ val request = Request [IO ](method = Method .GET , uri = uri)
229
+
230
+ // Stream the response body and write it to the specified file path
231
+ client
232
+ .stream(request)
233
+ .flatMap { response =>
234
+ if (response.status.isSuccess) {
235
+ response.body
236
+ } else {
237
+ fs2.Stream .raiseError[IO ](
238
+ new Exception (
239
+ s " Failed to download from $uri: ${response.status}"
240
+ )
241
+ )
242
+ }
233
243
}
234
- else {
235
- IO .raiseError(new Exception (s " from $uri expected hash to be ${hash.toIdent(algo)} but found ${computedHash.toIdent(algo)}" ))
244
+ .broadcastThrough(
245
+ Files [IO ].writeAll(tempPath),
246
+ hashFile
247
+ )
248
+ .compile
249
+ .lastOrError
250
+ .flatMap { computedHash =>
251
+ if (computedHash == hash) {
252
+ // move it atomically to output
253
+ filesIO.move(
254
+ source = Fs2Path .fromNioPath(tempPath),
255
+ target = Fs2Path .fromNioPath(path),
256
+ CopyFlags (CopyFlag .AtomicMove )
257
+ )
258
+ } else {
259
+ IO .raiseError(
260
+ new Exception (
261
+ s " from $uri expected hash to be ${hash.toIdent(algo)} but found ${computedHash.toIdent(algo)}"
262
+ )
263
+ )
264
+ }
236
265
}
237
- }
238
- }
266
+ }
239
267
}
240
268
241
269
def writeInterfaces (
@@ -312,4 +340,4 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
312
340
System .out.println(" " )
313
341
}
314
342
315
- }
343
+ }
0 commit comments