From 9fc99a5b8224bd7eeb94a23b3d671f51b5adb0b4 Mon Sep 17 00:00:00 2001 From: nikololiahim Date: Mon, 16 May 2022 12:07:25 +0300 Subject: [PATCH] j2eo is a dependency to polystat-cli --- .polystat.conf | 1 - README.md | 5 +- build.sbt | 2 +- src/main/scala/org/polystat/HoconConfig.scala | 17 ++--- src/main/scala/org/polystat/J2EO.scala | 68 +++---------------- src/main/scala/org/polystat/Main.scala | 14 ++-- .../scala/org/polystat/PolystatConfig.scala | 3 +- .../scala/org/polystat/PolystatOpts.scala | 24 +------ 8 files changed, 25 insertions(+), 109 deletions(-) diff --git a/.polystat.conf b/.polystat.conf index f523d49..3d8fc61 100644 --- a/.polystat.conf +++ b/.polystat.conf @@ -4,5 +4,4 @@ polystat { outputTo = . tempDir = tmp outputFormats = [sarif] - # excludeRules = [e, c, dialect] } diff --git a/README.md b/README.md index b07d738..eda81bf 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,7 @@ Write the [SARIF](https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.ht The description follows [this guide](https://en.wikipedia.org/wiki/Command-line_interface#Command_description_syntax). > Note: {a | b | c} means a set of _mutually-exclusive_ items. ``` -polystat {eo | python} [--tmp ] [--in ] [{--include | --exclude }] [--sarif] [--files [path]] -polystat java [--j2eo ] [--tmp ] [--in ] [{--include | --exclude }] [--sarif] [--files ] +polystat {eo | java | python} [--tmp ] [--in ] [{--include | --exclude }] [--sarif] [--files [path]] polystat [--version] [--help] [--config ] polystat list [--config | -c] ``` @@ -34,8 +33,6 @@ polystat list [--config | -c] ## Configuration options * `--include` and `--exclude` respectively define which rules should be included/excluded from the analysis run. These options are mutually exclusive, so specifying both should not be valid. If neither option is specified, all the available analyzers will be run. The list of available rule specifiers can be found via `polystat list` command. -* `--j2eo` options allows users to specify the path to the j2eo executable jar. If it's not specified, it looks for one in the current working diretory. -If it's not present in the current working directory, download one from Maven Central (for now, the version is hardcoded to be 0.4.0). ## Output configuration * `--sarif` option means that the command will produce the output in the [SARIF](https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html) format in addition to output in other formats (if any). diff --git a/build.sbt b/build.sbt index 07774eb..3e17f59 100644 --- a/build.sbt +++ b/build.sbt @@ -13,7 +13,6 @@ libraryDependencies ++= Seq( "org.polystat.odin" %% "analysis" % "0.3.3", "is.cir" %% "ciris" % "2.3.2", "lt.dvim.ciris-hocon" %% "ciris-hocon" % "1.0.1", - "org.http4s" %% "http4s-ember-client" % "1.0.0-M32", ).map(_.cross(CrossVersion.for3Use2_13)) excludeDependencies ++= Seq( @@ -26,6 +25,7 @@ libraryDependencies ++= Seq( "io.circe" %% "circe-core" % "0.14.1", "org.scalameta" %% "munit" % "1.0.0-M3" % Test, "org.slf4j" % "slf4j-nop" % "1.7.36", + "org.polystat" % "j2eo" % "0.4.0", ) assembly / assemblyJarName := "polystat.jar" diff --git a/src/main/scala/org/polystat/HoconConfig.scala b/src/main/scala/org/polystat/HoconConfig.scala index 47c7cbd..de23021 100644 --- a/src/main/scala/org/polystat/HoconConfig.scala +++ b/src/main/scala/org/polystat/HoconConfig.scala @@ -33,7 +33,6 @@ case class HoconConfig(path: Path): end hocon private val lang = hocon(keys.inputLanguage).as[SupportedLanguage] - private val j2eo = hocon(keys.j2eo).as[Path].option private val input = hocon(keys.input).as[Path].option.evalMap { case Some(path) => path.toInput case None => IO.pure(Input.FromStdin) @@ -53,13 +52,10 @@ case class HoconConfig(path: Path): .option val config: ConfigValue[IO, PolystatUsage.Analyze] = - (j2eo, inex, input, tmp, outputTo, outputFormats, lang).parMapN { - case (j2eo, inex, input, tmp, outputTo, outputFormats, lang) => + (inex, input, tmp, outputTo, outputFormats, lang).parMapN { + case (inex, input, tmp, outputTo, outputFormats, lang) => PolystatUsage.Analyze( - language = lang match - case Java(_) => Java(j2eo) - case other => other - , + language = lang, config = AnalyzerConfig( inex = inex, input = input, @@ -82,7 +78,6 @@ object HoconConfig: val outputFormats = "outputFormats" val includeRules = "includeRules" val excludeRules = "excludeRules" - val j2eo = "j2eo" val explanation = s""" |$toplevel.$inputLanguage | The type of input files which will be analyzed. This key must be present. @@ -90,10 +85,6 @@ object HoconConfig: | "java" - only ".java" files will be analyzed. | "eo" - only ".eo" files will be analyzed. | "python" - only ".py" files will be analyzed. - |$toplevel.$j2eo - | Specifies the path to the J2EO executable. - | If not specified, defaults to looking for j2eo.jar in the current working directory. - | If it's not found, downloads it from maven central. The download only happens when this key is NOT provided. |$toplevel.$input | How the files are supplied to the analyzer. | Can be either a path to a directory, path to a file, or absent. If absent, the code is read from standard input. @@ -124,7 +115,7 @@ object HoconConfig: extension (s: String) def asSupportedLang: Option[SupportedLanguage] = s match case "eo" => Some(EO) - case "java" => Some(Java(None)) + case "java" => Some(Java) case "python" => Some(Python) case _ => None diff --git a/src/main/scala/org/polystat/J2EO.scala b/src/main/scala/org/polystat/J2EO.scala index e72b603..896e554 100644 --- a/src/main/scala/org/polystat/J2EO.scala +++ b/src/main/scala/org/polystat/J2EO.scala @@ -1,72 +1,22 @@ package org.polystat import cats.effect.* import cats.syntax.all.* -import fs2.io.net.* -import org.http4s.ember.client.EmberClientBuilder -import org.http4s.ember.core.h2.* -import org.http4s.implicits.* -import org.http4s.Method.GET import fs2.text.utf8 -import org.http4s.Request -import org.http4s.client.middleware.FollowRedirect - import fs2.io.file.{Path, Files} -import org.http4s.Uri -import sys.process.* - -object J2EO: - private val DEFAULT_J2EO_PATH = Path("j2eo.jar") - private val J2EO_URL = - "https://search.maven.org/remotecontent?filepath=org/polystat/j2eo/0.4.0/j2eo-0.4.0.jar" +// entrypoint for J2EO +import main.Main2 - private def defaultJ2EO: IO[Path] = - Files[IO] - .exists(DEFAULT_J2EO_PATH) - .ifM( - ifTrue = IO.pure(DEFAULT_J2EO_PATH), - ifFalse = downloadJ2EO.as(DEFAULT_J2EO_PATH), - ) - - private def downloadJ2EO: IO[Unit] = - EmberClientBuilder - .default[IO] - .build - .map(client => FollowRedirect(2, _ => true)(client)) - .use[Unit] { client => - client - .run( - Request[IO]( - GET, - uri = Uri.unsafeFromString(J2EO_URL), - ) - ) - .use(resp => - IO.println( - "j2eo.jar was not found in the current working directory. Downloading..." - ) *> - resp.body - .through(Files[IO].writeAll(DEFAULT_J2EO_PATH)) - .compile - .drain - ) - } - end downloadJ2EO +object J2EO: - def run(j2eo: Option[Path], inputDir: Path, outputDir: Path): IO[Unit] = + def run(inputDir: Path, outputDir: Path): IO[Unit] = val command = - s"java -jar ${j2eo.getOrElse(DEFAULT_J2EO_PATH)} -o $outputDir $inputDir" + IO.blocking( + Main2.main(Array("-o", outputDir.toString, inputDir.toString)) + ) for - j2eo <- j2eo.map(IO.pure).getOrElse(defaultJ2EO) - _ <- Files[IO] - .exists(j2eo) - .ifM( - ifTrue = for - _ <- IO.println(s"""Running "$command"...""") - _ <- IO.blocking(command.!).void - yield (), - ifFalse = IO.println(s"""J2EO executable "$j2eo" doesn't exist!"""), - ) + _ <- IO.println(s"""Running "$command"...""") + _ <- command.void yield () end for end run diff --git a/src/main/scala/org/polystat/Main.scala b/src/main/scala/org/polystat/Main.scala index fac1215..15af7b1 100644 --- a/src/main/scala/org/polystat/Main.scala +++ b/src/main/scala/org/polystat/Main.scala @@ -125,9 +125,9 @@ object Main extends IOApp: case Some(path) => IO.pure(path) case None => Files[IO].createTempDirectory val inputExt: String = lang match - case SupportedLanguage.EO => ".eo" - case SupportedLanguage.Java(_) => ".java" - case SupportedLanguage.Python => ".py" + case SupportedLanguage.EO => ".eo" + case SupportedLanguage.Java => ".java" + case SupportedLanguage.Python => ".py" val analysisResults: IO[Unit] = lang match @@ -139,7 +139,7 @@ object Main extends IOApp: out = out, filteredAnalyzers = filteredAnalyzers, ) - case SupportedLanguage.Java(j2eo) => + case SupportedLanguage.Java => for tmp <- tempDir _ <- input match // writing EO files to tempDir @@ -150,12 +150,12 @@ object Main extends IOApp: path / "stdin.eo" ) _ <- writeOutputTo(stdinTmp)(code) - _ <- J2EO.run(j2eo, inputDir = stdinTmp, outputDir = tmp) + _ <- J2EO.run(inputDir = stdinTmp, outputDir = tmp) yield () case Input.FromFile(path) => - J2EO.run(j2eo, inputDir = path, outputDir = tmp) + J2EO.run(inputDir = path, outputDir = tmp) case Input.FromDirectory(path) => - J2EO.run(j2eo, inputDir = path, outputDir = tmp) + J2EO.run(inputDir = path, outputDir = tmp) inputFiles = readCodeFromInput( ".eo", Input.FromDirectory(tmp), diff --git a/src/main/scala/org/polystat/PolystatConfig.scala b/src/main/scala/org/polystat/PolystatConfig.scala index 15a23bb..9fa970f 100644 --- a/src/main/scala/org/polystat/PolystatConfig.scala +++ b/src/main/scala/org/polystat/PolystatConfig.scala @@ -18,8 +18,7 @@ object PolystatConfig: ) enum SupportedLanguage: - case EO, Python - case Java(j2eo: Option[Path]) + case EO, Python, Java end SupportedLanguage enum PolystatUsage: diff --git a/src/main/scala/org/polystat/PolystatOpts.scala b/src/main/scala/org/polystat/PolystatOpts.scala index 4b399fe..b3aa5cf 100644 --- a/src/main/scala/org/polystat/PolystatOpts.scala +++ b/src/main/scala/org/polystat/PolystatOpts.scala @@ -24,16 +24,6 @@ object PolystatOpts extends IOApp.Simple: "--config", "aboba", "--version", - - // "c++", - // "--in", - // "tmp", - // "--include", - // "1", - // "--include", - // "2", - // "--files", - // "sandbox" ) polystat @@ -67,21 +57,11 @@ object PolystatOpts extends IOApp.Simple: name = "java", help = "Analyze Java files", ) { - (analyzerConfig, j2eo).mapN((conf, j2eo) => - conf.map(conf => - PolystatUsage.Analyze(SupportedLanguage.Java(j2eo), conf) - ) + analyzerConfig.map(conf => + conf.map(conf => PolystatUsage.Analyze(SupportedLanguage.Java, conf)) ) } - def j2eo: Opts[Option[Path]] = Opts - .option[JPath]( - long = "j2eo", - help = "Path to a j2eo executable.", - ) - .map(Path.fromNioPath) - .orNone - def analyzePython: Opts[IO[PolystatUsage.Analyze]] = Opts.subcommand( name = "py", help = "Analyze Python files",