Skip to content

Commit 070a47d

Browse files
committed
refactored most usages of unsafeToX
1 parent 5cc4028 commit 070a47d

File tree

7 files changed

+47
-50
lines changed

7 files changed

+47
-50
lines changed

src/main/scala/org/polystat/cli/EO.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ object EO:
5050
results,
5151
).json.toString
5252
cfg.output.dirs.traverse_(out =>
53-
val outPath =
54-
codePath
55-
.mount(
56-
to = (out / "sarif").unsafeToDirectory,
57-
relativelyTo = cfg.input,
58-
)
59-
.replaceExt(newExt = ".sarif.json")
6053
for
54+
sarifDir <- (out / "sarif").createDirIfDoesntExist
55+
outPath =
56+
codePath
57+
.mount(
58+
to = sarifDir,
59+
relativelyTo = cfg.input,
60+
)
61+
.replaceExt(newExt = ".sarif.json")
6162
_ <- IO.println(s"Writing results to $outPath...")
6263
_ <- writeOutputTo(outPath)(sarifJson)
6364
yield ()

src/main/scala/org/polystat/cli/Far.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ object Far:
5151
): IO[OdinAnalysisResult] =
5252
val codeFileNameNoExt: String = pathToCode.filenameNoExt
5353
val createPathToXml: IO[JPath] =
54-
(pathToTmpDir / "xmir").unsafeToDirectory.createDirIfDoesntExist.map(
54+
(pathToTmpDir / "xmir").createDirIfDoesntExist.map(
5555
tmp =>
5656
pathToCode
5757
.mount(to = tmp, relativelyTo = pathToSrcRoot)

src/main/scala/org/polystat/cli/Java.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ object Java:
9191
cfg: ProcessedConfig,
9292
): IO[Unit] =
9393
for
94-
dirForEO <- (cfg.tempDir / "eo").unsafeToDirectory.createDirIfDoesntExist
94+
dirForEO <- (cfg.tempDir / "eo").createDirIfDoesntExist
9595
_ <- runJ2EO(j2eoVersion, j2eo, input = cfg.input, outputDir = dirForEO)
9696
// J2EO deletes the tmp directory when there are no files to analyze
9797
// This causes the subsequent call to EO.analyze to fail, because there is no temp directory.

src/main/scala/org/polystat/cli/Main.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ object Main extends IOApp:
141141
case Input.FromFile(file) =>
142142
for
143143
singleFileTmpDir <-
144-
(tempDir / "singleFile").unsafeToDirectory.createDirIfDoesntExist
144+
(tempDir / "singleFile").createDirIfDoesntExist
145145
singleFileTmpPath =
146-
(singleFileTmpDir / (file.filenameNoExt + ext)).unsafeToFile
147-
_ <- singleFileTmpPath.unsafeToFile.createFileIfDoesntExist
146+
singleFileTmpDir / (file.filenameNoExt + ext)
147+
_ <- singleFileTmpPath.createFileIfDoesntExist
148148
_ <- readCodeFromFile(ext, file)
149149
.map(_._2)
150150
.through(fs2.text.utf8.encode)
@@ -155,9 +155,8 @@ object Main extends IOApp:
155155
case Input.FromStdin =>
156156
for
157157
stdinTmpDir <-
158-
(tempDir / "stdin").unsafeToDirectory.createDirIfDoesntExist
159-
stdinTmpFilePath =
160-
(stdinTmpDir / ("stdin" + ext)).unsafeToFile
158+
(tempDir / "stdin").createDirIfDoesntExist
159+
stdinTmpFilePath = stdinTmpDir / ("stdin" + ext)
161160
_ <- stdinTmpFilePath.createFileIfDoesntExist
162161
_ <-
163162
readCodeFromStdin

src/main/scala/org/polystat/cli/Python.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import org.polystat.py2eo.transpiler.Transpile
1111
object Python:
1212

1313
def analyze(cfg: ProcessedConfig): IO[Unit] =
14-
val dirForEO = (cfg.tempDir / "eo").unsafeToDirectory
1514
for
15+
dirForEO <- (cfg.tempDir / "eo").createDirIfDoesntExist
1616
_ <- readCodeFromDir(".py", cfg.input)
1717
.evalMap { case (path, code) =>
1818
for

src/main/scala/org/polystat/cli/util/FileTypes.scala

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ object FileTypes:
99
private[FileTypes] val underlying: Path
1010
):
1111
override def toString = underlying.toString
12+
def toPath: Path = underlying
1213

1314
given Conversion[Directory, Path] = _.underlying
1415
object Directory:
@@ -30,25 +31,17 @@ object FileTypes:
3031
def fromPathUnsafe(path: Path): Directory = Directory(path)
3132

3233
extension (dir: Directory)
33-
def createDirIfDoesntExist: IO[Directory] =
34-
Files[IO]
35-
.exists(dir)
36-
.ifM(
37-
ifTrue = IO.pure(dir),
38-
ifFalse = Files[IO].createDirectories(dir).as(dir),
39-
)
40-
4134
def clean: IO[Directory] =
4235
for
43-
_ <- Files[IO].deleteRecursively(dir)
44-
_ <- Files[IO].createDirectory(dir)
36+
_ <- Files[IO].deleteRecursively(dir.toPath)
37+
_ <- Files[IO].createDirectory(dir.toPath)
4538
yield dir
4639

4740
class File private[FileTypes] (private[FileTypes] val underlying: Path):
4841
override def toString(): String = underlying.toString
42+
def toPath = underlying
4943

5044
given Conversion[File, Path] = _.underlying
51-
5245
object File:
5346
def fromPath(path: Path): IO[Option[File]] =
5447
Files[IO]
@@ -68,15 +61,8 @@ object FileTypes:
6861
def fromPathUnsafe(path: Path): File = File(path)
6962

7063
extension (file: File)
71-
def createFileIfDoesntExist: IO[File] =
72-
file.parent match
73-
case Some(parent) =>
74-
(Files[IO].createDirectories(parent) *> Files[IO].createFile(file))
75-
.as(file)
76-
case None => Files[IO].createFile(file).as(file)
77-
7864
def filenameNoExt: String =
79-
val fileName = file.fileName.toString
65+
val fileName = file.toPath.fileName.toString
8066
fileName.splitAt(fileName.indexOf("."))._1
8167

8268
def replaceExt(newExt: String): File =
@@ -90,5 +76,6 @@ object FileTypes:
9076

9177
def mount(to: Directory, relativelyTo: Directory): File =
9278
val relativePath =
93-
relativelyTo.absolute.normalize.relativize(file.absolute.normalize)
94-
File.fromPathUnsafe(to.normalize / relativePath)
79+
relativelyTo.toPath.absolute.normalize
80+
.relativize(file.toPath.absolute.normalize)
81+
File.fromPathUnsafe(to.toPath.normalize / relativePath)

src/main/scala/org/polystat/cli/util/InputUtils.scala

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ import java.io.FileNotFoundException
1616
object InputUtils:
1717

1818
extension (path: Path)
19+
def createDirIfDoesntExist: IO[Directory] =
20+
Directory.fromPath(path).flatMap {
21+
case Some(dir) => IO.pure(dir)
22+
case None =>
23+
Files[IO].createDirectories(path).as(path.unsafeToDirectory)
24+
}
25+
26+
def createFileIfDoesntExist: IO[File] =
27+
File.fromPath(path).flatMap {
28+
case Some(file) => IO.pure(file)
29+
case None =>
30+
path.parent match
31+
case Some(parent) =>
32+
(Files[IO].createDirectories(parent) *> Files[IO]
33+
.createFile(path))
34+
.as(path.unsafeToFile)
35+
case None => Files[IO].createFile(path).as(path.unsafeToFile)
36+
}
37+
1938
def toInput: IO[Input] =
2039
Directory.fromPath(path).flatMap {
2140
case Some(dir) => IO.pure(Input.FromDirectory(dir))
@@ -49,27 +68,18 @@ object InputUtils:
4968
def readCodeFromDir(ext: String, dir: Directory): Stream[IO, (File, String)] =
5069
Files[IO]
5170
.walk(dir)
52-
.filter(_.extName.endsWith(ext))
71+
.evalMapFilter(path =>
72+
File.fromPath(path).map(_.filter(_.extName.endsWith(ext)))
73+
)
5374
.flatMap(path =>
5475
Files[IO]
5576
.readAll(path)
5677
.through(utf8.decode)
57-
.map(code => (path.unsafeToFile, code))
78+
.map(code => (path, code))
5879
)
5980

6081
def readCodeFromStdin: Stream[IO, String] = stdinUtf8[IO](4096).bufferAll
6182

62-
def readCodeFromInput(ext: String, input: Input): Stream[IO, (File, String)] =
63-
input match
64-
case Input.FromFile(path) =>
65-
readCodeFromFile(ext = ext, file = path)
66-
case Input.FromDirectory(path) =>
67-
readCodeFromDir(ext = ext, dir = path)
68-
case Input.FromStdin =>
69-
readCodeFromStdin.map(code =>
70-
(Path("stdin" + ext).unsafeToFile, "\n" + code + "\n")
71-
)
72-
7383
def readConfigFromFile(path: File): IO[PolystatUsage.Analyze] =
7484
HoconConfig(path).config.load
7585

0 commit comments

Comments
 (0)