Skip to content

Commit 5d6129b

Browse files
committed
removed bad casts
1 parent bc4cea9 commit 5d6129b

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ object EO:
2929
a.analyze(
3030
tmpDir = cfg.tempDir,
3131
// TODO: make sure cfg input is always converted to directory
32-
pathToSrcRoot =
33-
cfg.input.asInstanceOf[Input.FromDirectory].path,
32+
pathToSrcRoot = cfg.input,
3433
pathToCode = codePath,
3534
code = code,
3635
).handleError(e =>
@@ -56,8 +55,7 @@ object EO:
5655
codePath
5756
.mount(
5857
to = (out / "sarif").unsafeToDirectory,
59-
relativelyTo =
60-
cfg.input.asInstanceOf[Input.FromDirectory].path,
58+
relativelyTo = cfg.input,
6159
)
6260
.replaceExt(newExt = ".sarif.json")
6361
for
@@ -81,7 +79,7 @@ object EO:
8179
}
8280

8381
for
84-
inputFiles <- readCodeFromInput(".eo", cfg.input).compile.toVector
82+
inputFiles <- readCodeFromDir(".eo", cfg.input).compile.toVector
8583
analyzed <- runAnalyzers(inputFiles)
8684
_ <- cfg.output.dirs.traverse_ { outDir =>
8785
for

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,7 @@ object Java:
9292
): IO[Unit] =
9393
for
9494
dirForEO <- (cfg.tempDir / "eo").unsafeToDirectory.createDirIfDoesntExist
95-
_ <- cfg.input match // writing EO files to tempDir
96-
case Input.FromStdin =>
97-
for
98-
code <- readCodeFromStdin.compile.string
99-
stdinTmp <- Files[IO].createTempDirectory.map(path =>
100-
(path / "stdin").unsafeToDirectory
101-
)
102-
_ <- writeOutputTo(stdinTmp)(code)
103-
_ <- runJ2EO(
104-
j2eoVersion,
105-
j2eo,
106-
input = stdinTmp,
107-
outputDir = dirForEO,
108-
)
109-
yield ()
110-
case Input.FromFile(path) =>
111-
runJ2EO(j2eoVersion, j2eo, input = path, outputDir = dirForEO)
112-
case Input.FromDirectory(path) =>
113-
runJ2EO(j2eoVersion, j2eo, input = path, outputDir = dirForEO)
95+
_ <- runJ2EO(j2eoVersion, j2eo, input = cfg.input, outputDir = dirForEO)
11496
// J2EO deletes the tmp directory when there are no files to analyze
11597
// This causes the subsequent call to EO.analyze to fail, because there is no temp directory.
11698
// The line below patches this issue by creating the temp directory if it was deleted by J2EO.
@@ -121,7 +103,7 @@ object Java:
121103
ifFalse = Files[IO].createDirectories(dirForEO),
122104
)
123105
_ <- EO.analyze(
124-
cfg.copy(input = Input.FromDirectory(dirForEO))
106+
cfg.copy(input = dirForEO)
125107
)
126108
yield ()
127109

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Python:
1313
def analyze(cfg: ProcessedConfig): IO[Unit] =
1414
val dirForEO = (cfg.tempDir / "eo").unsafeToDirectory
1515
for
16-
_ <- readCodeFromInput(".py", cfg.input)
16+
_ <- readCodeFromDir(".py", cfg.input)
1717
.evalMap { case (path, code) =>
1818
for
1919
maybeCode <- IO(
@@ -22,8 +22,7 @@ object Python:
2222
pathToEOCode = path
2323
.mount(
2424
to = dirForEO,
25-
// TODO: remove asInstanceOf
26-
relativelyTo = cfg.input.asInstanceOf[Input.FromDirectory].path,
25+
relativelyTo = cfg.input,
2726
)
2827
.replaceExt(newExt = ".eo")
2928
_ <- maybeCode match
@@ -35,7 +34,7 @@ object Python:
3534
}
3635
.compile
3736
.drain
38-
_ <- EO.analyze(cfg.copy(input = Input.FromDirectory(dirForEO)))
37+
_ <- EO.analyze(cfg.copy(input = dirForEO))
3938
yield ()
4039

4140
end Python

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ object FileTypes:
1212

1313
given Conversion[Directory, Path] = _.underlying
1414
object Directory:
15+
1516
def fromPath(path: Path): IO[Option[Directory]] =
1617
Files[IO]
1718
.isDirectory(path)
1819
.ifM(
1920
ifTrue = IO.pure(Some(Directory(path))),
2021
ifFalse = IO.pure(None),
2122
)
23+
2224
def fromPathFailFast(path: Path): IO[Directory] =
2325
fromPath(path).flatMap {
2426
case Some(dir) => IO.pure(dir)
@@ -55,17 +57,28 @@ object FileTypes:
5557
ifTrue = IO.pure(Some(File(path))),
5658
ifFalse = IO.pure(None),
5759
)
60+
5861
def fromPathFailFast(path: Path): IO[File] =
5962
fromPath(path).flatMap {
6063
case Some(file) => IO.pure(file)
6164
case None =>
6265
IO.raiseError(new Exception(s"$path must be a regular file!"))
6366
}
67+
6468
def fromPathUnsafe(path: Path): File = File(path)
69+
6570
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+
6678
def filenameNoExt: String =
6779
val fileName = file.fileName.toString
6880
fileName.splitAt(fileName.indexOf("."))._1
81+
6982
def replaceExt(newExt: String): File =
7083
File.fromPathUnsafe(
7184
Path(
@@ -74,6 +87,7 @@ object FileTypes:
7487
._1 + newExt
7588
)
7689
)
90+
7791
def mount(to: Directory, relativelyTo: Directory): File =
7892
val relativePath =
7993
relativelyTo.absolute.normalize.relativize(file.absolute.normalize)

0 commit comments

Comments
 (0)