-
Notifications
You must be signed in to change notification settings - Fork 4
Use fs2-io library for file and process handling #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5950911
Use fs2-io library for file and process handling
yhefamly 4900c39
Fix ProcessRunner exception handling
yhefamly 16d320d
Restore scala-cli check in BuildToolDetector
yhefamly 4a0c8f7
Merge branch 'main' into use-fs2-io
yhefamly 7432fa1
Small fixes after merge
yhefamly 9f61b95
Adjust Config loading to use fs2-io
yhefamly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,23 @@ | ||
| package cellar.build | ||
|
|
||
| import cats.effect.IO | ||
| import java.nio.file.{Files, Path} | ||
| import java.security.MessageDigest | ||
| import cats.syntax.all.* | ||
| import fs2.Chunk | ||
| import fs2.io.file.{Files, Path} | ||
| import fs2.hashing.* | ||
|
|
||
| import java.nio.charset.StandardCharsets | ||
|
|
||
| object BuildFingerprint: | ||
| def compute(files: List[Path], module: String): IO[String] = | ||
| IO.blocking { | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| digest.update(module.getBytes(java.nio.charset.StandardCharsets.UTF_8)) | ||
| files.sorted.foreach { path => | ||
| if Files.exists(path) then | ||
| digest.update(path.toString.getBytes(java.nio.charset.StandardCharsets.UTF_8)) | ||
| digest.update(Files.readAllBytes(path)) | ||
| } | ||
| digest.digest().map(b => f"$b%02x").mkString | ||
| Hashing[IO].hasher(HashAlgorithm.SHA256).use { hasher => | ||
| for | ||
| _ <- hasher.update(Chunk.array(module.getBytes(StandardCharsets.UTF_8))) | ||
| _ <- fs2.Stream.emits(files.sortBy(_.toNioPath)) | ||
| .evalFilter(Files[IO].exists) | ||
| .evalTap(path => hasher.update(Chunk.array(path.toString.getBytes(StandardCharsets.UTF_8)))) | ||
| .flatMap(path => Files[IO].readAll(path).through(hasher.update)) | ||
| .compile.drain | ||
| hash <- hasher.hash | ||
| yield hash.bytes.foldMap(b => f"$b%02x") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,28 @@ | ||
| package cellar.build | ||
|
|
||
| import cats.effect.IO | ||
| import java.nio.file.{Files, Path, StandardCopyOption} | ||
| import cats.syntax.all.* | ||
| import fs2.Stream | ||
| import fs2.io.file.{CopyFlag, CopyFlags, Files, Path} | ||
|
|
||
| class ClasspathCache(projectDir: Path): | ||
| private val cacheDir = projectDir.resolve(".cellar").resolve("cache") | ||
|
|
||
| def get(hash: String): IO[Option[List[Path]]] = | ||
| IO.blocking { | ||
| val file = cacheDir.resolve(s"$hash.txt") | ||
| if !Files.exists(file) then None | ||
| else | ||
| val paths = Files.readString(file).linesIterator | ||
| .filter(_.nonEmpty) | ||
| .map(Path.of(_)) | ||
| .toList | ||
| // Validate all paths still exist | ||
| if paths.forall(Files.exists(_)) then Some(paths) | ||
| else None | ||
| val file = cacheDir.resolve(s"$hash.txt") | ||
| Files[IO].exists(file).flatMap { | ||
| case true => | ||
| for | ||
| paths <- Files[IO].readUtf8Lines(file).filter(_.nonEmpty).map(Path(_)).compile.toList | ||
| allExist <- paths.forallM(Files[IO].exists) | ||
| yield Option.when(allExist)(paths) | ||
| case false => IO.pure(None) | ||
| } | ||
|
|
||
| def put(hash: String, paths: List[Path]): IO[Unit] = | ||
| IO.blocking { | ||
| Files.createDirectories(cacheDir) | ||
| val file = cacheDir.resolve(s"$hash.txt") | ||
| val tmp = cacheDir.resolve(s"$hash.tmp") | ||
| Files.writeString(tmp, paths.map(_.toString).mkString("\n") + "\n") | ||
| Files.move(tmp, file, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE) | ||
| } | ||
| def put(hash: String, paths: List[Path]): IO[Unit] = for { | ||
| _ <- Files[IO].createDirectories(cacheDir) | ||
| file = cacheDir.resolve(s"$hash.txt") | ||
| tmp = cacheDir.resolve(s"$hash.tmp") | ||
| _ <- Stream(paths.map(_.toString).mkString("\n") + "\n").through(Files[IO].writeUtf8(tmp)).compile.drain | ||
| _ <- Files[IO].move(tmp, file, CopyFlags(CopyFlag.ReplaceExisting, CopyFlag.AtomicMove)) | ||
| } yield () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets restore .scala-build detection. It is duplicated for now with the fallback but in the future if we want to add support for any other build tool, the fallback may no longer apply
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-added and refactored the code using
findMfor better readability.