Skip to content

Commit eaf0b47

Browse files
committed
using the new function where mounting happens
1 parent b1bb149 commit eaf0b47

File tree

4 files changed

+77
-54
lines changed

4 files changed

+77
-54
lines changed

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
package org.polystat.cli
22

33
import cats.effect.IO
4-
import cats.syntax.foldable.*
5-
import cats.syntax.traverse.*
4+
import cats.syntax.all.*
5+
import fs2.io.file.Path
66
import io.circe.syntax.*
7+
import org.polystat.cli.util.InputUtils.*
78
import org.polystat.odin.analysis.ASTAnalyzer
89
import org.polystat.odin.analysis.EOOdinAnalyzer
10+
import org.polystat.odin.analysis.EOOdinAnalyzer.OdinAnalysisResult
911
import org.polystat.odin.parser.EoParser.sourceCodeEoParser
1012
import org.polystat.sarif.AggregatedSarifOutput
1113
import org.polystat.sarif.SarifOutput
12-
import fs2.io.file.Path
1314

1415
import PolystatConfig.*
15-
import InputUtils.*
16-
import org.polystat.odin.analysis.EOOdinAnalyzer.OdinAnalysisResult
1716

1817
object EO:
1918

2019
def analyze(cfg: ProcessedConfig): IO[Unit] =
21-
def runAnalyzers(inputFiles: Vector[(Path, String)]) = inputFiles
22-
.traverse { case (codePath, code) =>
23-
for
24-
_ <- IO.println(s"Analyzing $codePath...")
25-
analyzed <- cfg.filteredAnalyzers.traverse(a =>
26-
a.analyze(cfg.tempDir)(codePath)(code)
27-
.handleError(e => OdinAnalysisResult.AnalyzerFailure(a.ruleId, e))
28-
)
29-
yield (codePath, analyzed)
30-
}
20+
def runAnalyzers(
21+
inputFiles: Vector[(Path, String)]
22+
): IO[Vector[(Path, List[OdinAnalysisResult])]] =
23+
inputFiles
24+
.traverse { case (codePath, code) =>
25+
for
26+
_ <- IO.println(s"Analyzing $codePath...")
27+
analyzed <- cfg.filteredAnalyzers.traverse(a =>
28+
a.analyze(
29+
tmpDir = cfg.tempDir,
30+
// TODO: make sure cfg input is always converted to directory
31+
pathToSrcRoot =
32+
cfg.input.asInstanceOf[Input.FromDirectory].path,
33+
pathToCode = codePath,
34+
code = code,
35+
).handleError(e =>
36+
OdinAnalysisResult.AnalyzerFailure(a.ruleId, e)
37+
)
38+
)
39+
yield (codePath, analyzed)
40+
}
3141

3242
def writeToDirs(
3343
analyzed: Vector[(Path, List[OdinAnalysisResult])]
@@ -42,7 +52,13 @@ object EO:
4252
).json.toString
4353
cfg.output.dirs.traverse_(out =>
4454
val outPath =
45-
out / "sarif" / codePath.replaceExt(".sarif.json")
55+
codePath
56+
.mount(
57+
to = out / "sarif",
58+
relativelyTo =
59+
cfg.input.asInstanceOf[Input.FromDirectory].path,
60+
)
61+
.replaceExt(newExt = ".sarif.json")
4662
for
4763
_ <- IO.println(s"Writing results to $outPath...")
4864
_ <- writeOutputTo(outPath)(sarifJson)
@@ -63,17 +79,8 @@ object EO:
6379
}
6480
}
6581

66-
def pathToDisplay(relPath: Path) = cfg.input match
67-
case Input.FromDirectory(dir) => dir / relPath
68-
// TODO: account for other cases
69-
// This should be the same pass that is created when running readCodeFromX
70-
case _ => relPath
71-
7282
for
7383
inputFiles <- readCodeFromInput(".eo", cfg.input).compile.toVector
74-
.map(_.map { case (path, results) =>
75-
(pathToDisplay(path), results)
76-
})
7784
analyzed <- runAnalyzers(inputFiles)
7885
_ <- cfg.output.dirs.traverse_ { outDir =>
7986
for

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package org.polystat.cli
22

3-
import cats.effect.Sync
43
import cats.effect.IO
4+
import cats.effect.Sync
5+
import com.jcabi.xml.XML
6+
import com.jcabi.xml.XMLDocument
57
import fs2.io.file.Path
68
import higherkindness.droste.data.Fix
9+
import org.cactoos.Func
10+
import org.cactoos.io.InputOf
11+
import org.cactoos.io.OutputTo
12+
import org.eolang.parser.Spy
13+
import org.eolang.parser.Syntax
14+
import org.eolang.parser.Xsline
15+
import org.polystat.cli.util.InputUtils.*
716
import org.polystat.far.FaR
817
import org.polystat.odin.analysis.ASTAnalyzer
918
import org.polystat.odin.analysis.EOOdinAnalyzer.OdinAnalysisResult
1019
import org.polystat.odin.core.ast.EOExpr
1120
import org.polystat.odin.core.ast.EOProg
12-
import org.eolang.parser.Syntax
13-
import org.eolang.parser.Xsline
14-
import org.eolang.parser.Spy
15-
import org.cactoos.io.OutputTo
16-
import org.cactoos.io.InputOf
17-
import org.cactoos.Func
18-
import com.jcabi.xml.XMLDocument
19-
import com.jcabi.xml.XML
20-
import org.polystat.cli.InputUtils.replaceExt
21+
2122
import java.nio.file.Path as JPath
2223
import scala.jdk.CollectionConverters.*
2324

@@ -41,16 +42,23 @@ object Far:
4142
): IO[java.util.Collection[String]] =
4243
IO.delay(new FaR().errors(program(pathToXml), locator))
4344

44-
def analyze(ruleId: String)(pathToTmpDir: Path)(
45-
pathToCode: Path
45+
def analyze(
46+
ruleId: String,
47+
pathToSrcRoot: Path,
48+
pathToTmpDir: Path,
49+
pathToCode: Path,
4650
): IO[OdinAnalysisResult] =
47-
val codeFileName: Path = pathToCode.fileName
48-
val codeFileNameNoExt: String =
49-
codeFileName.toString.splitAt(codeFileName.toString.indexOf("."))._1
50-
val pathToXml: JPath =
51-
(pathToTmpDir / codeFileName.replaceExt(".xml")).toNioPath
51+
val codeFileNameNoExt: String = pathToCode.filenameNoExt
52+
val createPathToXml: IO[JPath] =
53+
(pathToTmpDir / "xmir").createDirIfDoesntExist.map(tmp =>
54+
pathToCode
55+
.mount(to = tmp, relativelyTo = pathToSrcRoot)
56+
.replaceExt(newExt = ".xml")
57+
.toNioPath
58+
)
5259

5360
for
61+
pathToXml <- createPathToXml
5462
// parse EO to XMIR
5563
_ <- IO.delay(
5664
new Syntax(

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import org.http4s.client.middleware.FollowRedirect
1212
import org.http4s.ember.client.EmberClientBuilder
1313
import org.http4s.ember.core.h2.*
1414
import org.http4s.implicits.*
15+
import org.polystat.cli.BuildInfo
16+
import org.polystat.cli.util.InputUtils.*
1517

1618
import sys.process.*
1719
import PolystatConfig.*
18-
import InputUtils.*
19-
import org.polystat.cli.BuildInfo
2020

2121
object Java:
2222

@@ -95,6 +95,7 @@ object Java:
9595
cfg: ProcessedConfig,
9696
): IO[Unit] =
9797
for
98+
dirForEO <- (cfg.tempDir / "eo").createDirIfDoesntExist
9899
_ <- cfg.input match // writing EO files to tempDir
99100
case Input.FromStdin =>
100101
for
@@ -107,24 +108,24 @@ object Java:
107108
j2eoVersion,
108109
j2eo,
109110
inputDir = stdinTmp,
110-
outputDir = cfg.tempDir,
111+
outputDir = dirForEO,
111112
)
112113
yield ()
113114
case Input.FromFile(path) =>
114-
runJ2EO(j2eoVersion, j2eo, inputDir = path, outputDir = cfg.tempDir)
115+
runJ2EO(j2eoVersion, j2eo, inputDir = path, outputDir = dirForEO)
115116
case Input.FromDirectory(path) =>
116-
runJ2EO(j2eoVersion, j2eo, inputDir = path, outputDir = cfg.tempDir)
117+
runJ2EO(j2eoVersion, j2eo, inputDir = path, outputDir = dirForEO)
117118
// J2EO deletes the tmp directory when there are no files to analyze
118119
// This causes the subsequent call to EO.analyze to fail, because there is no temp directory.
119120
// The line below patches this issue by creating the temp directory if it was deleted by J2EO.
120121
_ <- Files[IO]
121122
.exists(cfg.tempDir)
122123
.ifM(
123124
ifTrue = IO.unit,
124-
ifFalse = Files[IO].createDirectory(cfg.tempDir),
125+
ifFalse = Files[IO].createDirectories(dirForEO),
125126
)
126127
_ <- EO.analyze(
127-
cfg.copy(input = Input.FromDirectory(cfg.tempDir))
128+
cfg.copy(input = Input.FromDirectory(dirForEO))
128129
)
129130
yield ()
130131

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,38 @@ import cats.effect.IO
44
import cats.effect.IOApp
55
import fs2.io.file.Files
66
import fs2.io.file.Path
7-
import org.polystat.cli.InputUtils.*
87
import org.polystat.cli.PolystatConfig.*
8+
import org.polystat.cli.util.InputUtils.*
99
import org.polystat.py2eo.transpiler.Transpile
1010

1111
object Python:
1212

1313
def analyze(cfg: ProcessedConfig): IO[Unit] =
14+
val dirForEO = cfg.tempDir / "eo"
1415
for
1516
_ <- readCodeFromInput(".py", cfg.input)
1617
.evalMap { case (path, code) =>
17-
val fileName = path.fileName.toString
1818
for
1919
maybeCode <- IO(
20-
Transpile(fileName.splitAt(fileName.indexOf("."))._1, code)
20+
Transpile(path.filenameNoExt, code)
2121
)
22+
pathToEOCode = path
23+
.mount(
24+
to = dirForEO,
25+
// TODO: remove asInstanceOf
26+
relativelyTo = cfg.input.asInstanceOf[Input.FromDirectory].path,
27+
)
28+
.replaceExt(newExt = ".eo")
2229
_ <- maybeCode match
2330
case Some(code) =>
24-
writeOutputTo(cfg.tempDir / path.replaceExt(".eo"))(code)
31+
writeOutputTo(pathToEOCode)(code)
2532
case None => IO.println(s"Couldn't analyze $path...")
2633
yield ()
2734
end for
2835
}
2936
.compile
3037
.drain
31-
_ <- EO.analyze(cfg.copy(input = Input.FromDirectory(cfg.tempDir)))
38+
_ <- EO.analyze(cfg.copy(input = Input.FromDirectory(dirForEO)))
3239
yield ()
3340

3441
end Python

0 commit comments

Comments
 (0)