From d0957b4444c28cf1721b8bdec2e9c81b22c3c573 Mon Sep 17 00:00:00 2001 From: Khemraj Rathore Date: Tue, 31 Dec 2024 16:11:08 +0530 Subject: [PATCH] add ast-gen in ruby --- joern-cli/frontends/rubysrc2cpg/build.sbt | 46 +++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/joern-cli/frontends/rubysrc2cpg/build.sbt b/joern-cli/frontends/rubysrc2cpg/build.sbt index 0fd0eb2aa7a9..1376a346fee1 100644 --- a/joern-cli/frontends/rubysrc2cpg/build.sbt +++ b/joern-cli/frontends/rubysrc2cpg/build.sbt @@ -28,19 +28,51 @@ libraryDependencies ++= Seq( "org.jruby" % "jruby-complete" % Versions.jRuby ) -enablePlugins(JavaAppPackaging, LauncherJarPlugin) - -libraryDependencies ++= Seq( - "io.shiftleft" %% "codepropertygraph" % Versions.cpg, - "org.scalatest" %% "scalatest" % Versions.scalatest % Test -) - enablePlugins(JavaAppPackaging, LauncherJarPlugin, Antlr4Plugin) Antlr4 / antlr4Version := Versions.antlr Antlr4 / antlr4GenVisitor := true Antlr4 / javaSource := (Compile / sourceManaged).value +lazy val astGenVersion = settingKey[String]("`ruby_ast_gen` version") +astGenVersion := appProperties.value.getString("rubysrc2cpg.ruby_ast_gen_version") + +lazy val astGenDlUrl = settingKey[String]("astgen download url") +astGenDlUrl := s"https://github.com/joernio/ruby_ast_gen/releases/download/v${astGenVersion.value}/" + +def hasCompatibleAstGenVersion(astGenBaseDir: File, astGenVersion: String): Boolean = { + val versionFile = astGenBaseDir / "lib" / "ruby_ast_gen" / "version.rb" + if (!versionFile.exists) return false + val versionPattern = "VERSION = \"([0-9]+\\.[0-9]+\\.[0-9]+)\"".r + versionPattern.findFirstIn(IO.read(versionFile)) match { + case Some(versionString) => + // Regex group matching doesn't appear to work in SBT + val version = versionString.stripPrefix("VERSION = \"").stripSuffix("\"") + version == astGenVersion + case _ => false + } +} + +lazy val astGenResourceTask = taskKey[Seq[File]](s"Download `ruby_ast_gen` and package this under `resources`") +astGenResourceTask := { + val targetDir = baseDirectory.value / "src" / "main" / "resources" + val gemName = s"ruby_ast_gen_v${astGenVersion.value}.zip" + val compressGemPath = targetDir / gemName + val unpackedGemFullPath = targetDir / gemName.stripSuffix(s"_v${astGenVersion.value}.zip") + if (!hasCompatibleAstGenVersion(unpackedGemFullPath, astGenVersion.value)) { + if (unpackedGemFullPath.exists()) IO.delete(unpackedGemFullPath) + val url = s"${astGenDlUrl.value}$gemName" + sbt.io.Using.urlInputStream(new URI(url).toURL) { inputStream => + sbt.IO.transfer(inputStream, compressGemPath) + } + IO.unzip(compressGemPath, unpackedGemFullPath) + IO.delete(compressGemPath) + } + (unpackedGemFullPath ** "*").get.filter(_.isFile) +} + +Compile / resourceGenerators += astGenResourceTask + lazy val joernTypeStubsDlUrl = settingKey[String]("joern_type_stubs download url") joernTypeStubsDlUrl := s"https://github.com/joernio/joern-type-stubs/releases/download/v${joernTypeStubsVersion.value}/"