All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.shiftleft.js2cpg.passes.ConfigPass.scala Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
package io.shiftleft.js2cpg.passes

import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.codepropertygraph.generated.nodes.NewConfigFile
import io.shiftleft.js2cpg.core.Report
import io.shiftleft.js2cpg.io.FileDefaults
import io.shiftleft.js2cpg.io.FileUtils
import io.shiftleft.js2cpg.utils.TimeUtils
import io.shiftleft.passes.ConcurrentWriterCpgPass
import io.shiftleft.utils.IOUtils
import org.slf4j.{Logger, LoggerFactory}

import java.nio.file.Path

class ConfigPass(filenames: List[(Path, Path)], cpg: Cpg, report: Report)
    extends ConcurrentWriterCpgPass[(Path, Path)](cpg) {

  private val logger: Logger = LoggerFactory.getLogger(getClass)

  protected def fileContent(filePath: Path): Seq[String] = IOUtils.readLinesInFile(filePath)

  override def generateParts(): Array[(Path, Path)] = filenames.toArray

  override def runOnPart(diffGraph: DiffGraphBuilder, file: (Path, Path)): Unit = {
    val (filePath, fileRootPath) = file
    val relativeFile             = fileRootPath.relativize(filePath)
    val fileStatistics           = FileUtils.fileStatistics(filePath)
    if (fileStatistics.linesOfCode > FileDefaults.NUM_LINES_THRESHOLD) {
      logger.info(
        s"Skip adding file '$relativeFile' as config file (more than ${FileDefaults.NUM_LINES_THRESHOLD} lines)"
      )
    } else if (fileStatistics.longestLineLength > FileDefaults.LINE_LENGTH_THRESHOLD) {
      logger.info(
        s"Skip adding file '$relativeFile' as config file (at least one line longer than ${FileDefaults.LINE_LENGTH_THRESHOLD} characters)"
      )
    } else {
      val fileName = relativeFile.toString
      val content  = fileContent(filePath)
      val (result, time) = TimeUtils.time {
        val localDiff = new DiffGraphBuilder
        logger.debug(s"Adding file '$relativeFile' as config file.")
        val configNode = NewConfigFile().name(fileName).content(content.mkString("\n"))
        report.addReportInfo(fileName, fileStatistics.linesOfCode, parsed = true, cpgGen = true, isConfig = true)
        localDiff.addNode(configNode)
        localDiff
      }
      diffGraph.absorb(result)
      report.updateReportDuration(fileName, time)
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy