Skip to content

Commit

Permalink
Update scala
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu committed Sep 22, 2024
1 parent 2b580cc commit a2ee137
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 157 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name := "chen"
ThisBuild / organization := "io.appthreat"
ThisBuild / version := "2.1.8"
ThisBuild / scalaVersion := "3.5.0"
ThisBuild / version := "2.1.9"
ThisBuild / scalaVersion := "3.5.1"

val cpgVersion = "1.0.0"

Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"downloadUrl": "https://github.com/AppThreat/chen",
"issueTracker": "https://github.com/AppThreat/chen/issues",
"name": "chen",
"version": "2.1.8",
"version": "2.1.9",
"description": "Code Hierarchy Exploration Net (chen) is an advanced exploration toolkit for your application source code and its dependency hierarchy.",
"applicationCategory": "code-analysis",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion console/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ libraryDependencies ++= Seq(
"dev.scalapy" %% "scalapy-core" % "0.5.3",
"org.scala-lang.modules" % "scala-asm" % "9.7.0-scala-2",
"org.scalatest" %% "scalatest" % Versions.scalatest % Test,
"org.scala-lang" %% "scala3-compiler" % "3.5.0"
"org.scala-lang" %% "scala3-compiler" % "3.5.1"
)


Expand Down
2 changes: 1 addition & 1 deletion meta.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set version = "2.1.8" %}
{% set version = "2.1.9" %}

package:
name: chen
Expand Down
83 changes: 0 additions & 83 deletions platform/frontends/jssrc2cpg/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ appProperties := {
applicationConf
}

lazy val astGenVersion = settingKey[String]("astgen version")
astGenVersion := appProperties.value.getString("jssrc2cpg.astgen_version")

libraryDependencies ++= Seq(
"io.appthreat" %% "cpg2" % Versions.cpg,
"com.lihaoyi" %% "upickle" % Versions.upickle,
Expand All @@ -32,86 +29,6 @@ Test / fork := false

enablePlugins(JavaAppPackaging, LauncherJarPlugin)

lazy val AstgenWin = "astgen-win.exe"
lazy val AstgenLinux = "astgen-linux"
lazy val AstgenMac = "astgen-macos"
lazy val AstgenMacArm = "astgen-macos-arm"

lazy val astGenDlUrl = settingKey[String]("astgen download url")
astGenDlUrl := s"https://github.com/joernio/astgen/releases/download/v${astGenVersion.value}/"

def hasCompatibleAstGenVersion(astGenVersion: String): Boolean = {
Try("astgen --version".!!).toOption.map(_.strip()) match {
case Some(installedVersion) if installedVersion != "unknown" =>
VersionHelper.compare(installedVersion, astGenVersion) >= 0
case _ => false
}
}

lazy val astGenBinaryNames = taskKey[Seq[String]]("astgen binary names")
astGenBinaryNames := {
if (hasCompatibleAstGenVersion(astGenVersion.value)) {
Seq.empty
} else if (sys.props.get("ALL_PLATFORMS").contains("TRUE")) {
Seq(AstgenWin, AstgenLinux, AstgenMac, AstgenMacArm)
} else {
Environment.operatingSystem match {
case Environment.OperatingSystemType.Windows =>
Seq(AstgenWin)
case Environment.OperatingSystemType.Linux =>
Seq(AstgenLinux)
case Environment.OperatingSystemType.Mac =>
Environment.architecture match {
case Environment.ArchitectureType.X86 => Seq(AstgenMac)
case Environment.ArchitectureType.ARM => Seq(AstgenMacArm)
}
case Environment.OperatingSystemType.Unknown =>
Seq(AstgenWin, AstgenLinux, AstgenMac, AstgenMacArm)
}
}
}

lazy val astGenDlTask = taskKey[Unit](s"Download astgen binaries")
astGenDlTask := {
val astGenDir = baseDirectory.value / "bin" / "astgen"
astGenDir.mkdirs()

astGenBinaryNames.value.foreach { fileName =>
val dest = astGenDir / fileName
if (!dest.exists) {
val url = s"${astGenDlUrl.value}$fileName"
val downloadedFile = SimpleCache.downloadMaybe(url)
IO.copyFile(downloadedFile, dest)
}
}

val distDir = (Universal / stagingDirectory).value / "bin" / "astgen"
distDir.mkdirs()
IO.copyDirectory(astGenDir, distDir)

// permissions are lost during the download; need to set them manually
astGenDir.listFiles().foreach(_.setExecutable(true, false))
distDir.listFiles().foreach(_.setExecutable(true, false))
}

Compile / compile := ((Compile / compile) dependsOn astGenDlTask).value

lazy val astGenSetAllPlatforms = taskKey[Unit](s"Set ALL_PLATFORMS")
astGenSetAllPlatforms := { System.setProperty("ALL_PLATFORMS", "TRUE") }

stage := Def
.sequential(astGenSetAllPlatforms, Universal / stage)
.andFinally(System.setProperty("ALL_PLATFORMS", "FALSE"))
.value

// Also remove astgen binaries with clean, e.g., to allow for updating them.
// Sadly, we can't define the bin/ folders globally,
// as .value can only be used within a task or setting macro
cleanFiles ++= Seq(
baseDirectory.value / "bin" / "astgen",
(Universal / stagingDirectory).value / "bin" / "astgen"
) ++ astGenBinaryNames.value.map(fileName => SimpleCache.encodeFile(s"${astGenDlUrl.value}$fileName"))

Universal / packageName := name.value
Universal / topLevelDirectory := None
githubOwner := "appthreat"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import scala.util.Try

object AstGenRunner:

private val logger = LoggerFactory.getLogger(getClass)

private val LineLengthThreshold: Int = 10000

private val TypeDefinitionFileExtensions = List(".t.ts", ".d.ts")
Expand Down Expand Up @@ -62,53 +60,7 @@ object AstGenRunner:
skippedFiles: List[(String, String)] = List.empty
)

lazy private val executableName = Environment.operatingSystem match
case Environment.OperatingSystemType.Windows => "astgen-win.exe"
case Environment.OperatingSystemType.Linux => "astgen-linux"
case Environment.OperatingSystemType.Mac =>
Environment.architecture match
case Environment.ArchitectureType.X86 => "astgen-macos"
case Environment.ArchitectureType.ARM => "astgen-macos-arm"
case Environment.OperatingSystemType.Unknown =>
logger.warn("Could not detect OS version! Defaulting to 'Linux'.")
"astgen-linux"

lazy private val executableDir: String =
val dir = getClass.getProtectionDomain.getCodeSource.getLocation.toString
val indexOfLib = dir.lastIndexOf("lib")
val fixedDir = if indexOfLib != -1 then
new java.io.File(dir.substring("file:".length, indexOfLib)).toString
else
val indexOfTarget = dir.lastIndexOf("target")
if indexOfTarget != -1 then
new java.io.File(dir.substring("file:".length, indexOfTarget)).toString
else
"."
Paths.get(fixedDir, "/bin/astgen").toAbsolutePath.toString

private def hasCompatibleAstGenVersion(astGenVersion: String): Boolean =
ExternalCommand.run("astgen --version", ".").toOption.map(_.mkString.strip()) match
case Some(installedVersion)
if installedVersion != "unknown" &&
Try(VersionHelper.compare(installedVersion, astGenVersion)).toOption.getOrElse(
-1
) >= 0 =>
logger.debug(s"Using local astgen v$installedVersion from systems PATH")
true
case Some(installedVersion) =>
logger.debug(
s"Found local astgen v$installedVersion in systems PATH but jssrc2cpg requires at least v$astGenVersion"
)
false
case _ => false

private lazy val astGenCommand =
val conf = ConfigFactory.load
val astGenVersion = conf.getString("jssrc2cpg.astgen_version")
if hasCompatibleAstGenVersion(astGenVersion) then
"astgen"
else
s"$executableDir/$executableName"
private lazy val astGenCommand = "astgen"
end AstGenRunner

class AstGenRunner(config: Config):
Expand All @@ -121,11 +73,8 @@ class AstGenRunner(config: Config):
val skipped = astGenOut.collect {
case out if !out.startsWith("Converted") && !out.startsWith("Retrieving") =>
val filename = out.substring(0, out.indexOf(" "))
val reason = out.substring(out.indexOf(" ") + 1)
logger.warn(s"\t- failed to parse '${in / filename}': '$reason'")
Option(filename)
case out =>
logger.debug(s"\t+ $out")
case _ =>
None
}
skipped.flatten
Expand All @@ -137,7 +86,6 @@ class AstGenRunner(config: Config):
}
lazy val isInIgnoredFileRegex = config.ignoredFilesRegex.matches(filePath)
if isInIgnoredFiles || isInIgnoredFileRegex then
logger.debug(s"'$filePath' ignored by user configuration")
true
else
false
Expand All @@ -149,9 +97,6 @@ class AstGenRunner(config: Config):
val linesOfCode = lines.size
val longestLineLength = if lines.isEmpty then 0 else lines.map(_.length).max
if longestLineLength >= LineLengthThreshold && linesOfCode <= 50 then
logger.debug(
s"'$filePath' seems to be a minified file (contains a line with length $longestLineLength)"
)
true
else false
case _ => false
Expand All @@ -161,7 +106,6 @@ class AstGenRunner(config: Config):
lazy val isIgnoredTest = IgnoredTestsRegex.exists(_.matches(filePath))
lazy val isMinified = isMinifiedFile(filePath)
if isIgnored || isIgnoredTest || isMinified then
logger.debug(s"'$filePath' ignored by default")
true
else
false
Expand All @@ -182,9 +126,6 @@ class AstGenRunner(config: Config):
false
) == file.nameWithoutExtension)
if isJsFile && hasSourceMap && hasFileWithSameName then
logger.debug(
s"'$filePath' ignored by default (seems to be the result of transpilation)"
)
true
else
false
Expand All @@ -193,9 +134,6 @@ class AstGenRunner(config: Config):
private def filterFiles(files: List[String], out: File): List[String] =
files.filter { file =>
file.stripSuffix(".json").replace(out.pathAsString, config.inputPath) match
// We are not interested in JS / TS type definition files at this stage.
// TODO: maybe we can enable that later on and use the type definitions there
// for enhancing the CPG with additional type information for functions
case filePath if TypeDefinitionFileExtensions.exists(filePath.endsWith) => false
case filePath if isIgnoredByUserConfig(filePath) => false
case filePath if isIgnoredByDefault(filePath) => false
Expand Down Expand Up @@ -274,13 +212,11 @@ class AstGenRunner(config: Config):

def execute(out: File): AstGenRunnerResult =
val in = File(config.inputPath)
logger.debug(s"Running astgen in '$in' ...")
runAstGenNative(in, out) match
case Success(result) =>
val parsed = filterFiles(SourceFiles.determine(out.toString(), Set(".json")), out)
val skipped = skippedFiles(in, result.toList)
AstGenRunnerResult(parsed.map((in.toString(), _)), skipped.map((in.toString(), _)))
case Failure(f) =>
logger.debug("\t- running astgen failed!", f)
case Failure(_) =>
AstGenRunnerResult()
end AstGenRunner
2 changes: 1 addition & 1 deletion platform/src/universal/schema-extender/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "schema-extender"

ThisBuild / scalaVersion := "3.5.0"
ThisBuild / scalaVersion := "3.5.1"

val cpgVersion = IO.read(file("cpg-version"))

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "appthreat-chen"
version = "2.1.8"
version = "2.1.9"
description = "Code Hierarchy Exploration Net (chen)"
authors = ["Team AppThreat <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit a2ee137

Please sign in to comment.