|
| 1 | +package codacy.dockerApi.traits |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.nio.file.Path |
| 5 | + |
| 6 | +import codacy.dockerApi.utils.CommandRunner |
| 7 | + |
| 8 | +import scala.util.{Failure, Success, Try} |
| 9 | + |
| 10 | +sealed trait Builder { |
| 11 | + |
| 12 | + val command: List[String] |
| 13 | + val pathComponents: Seq[String] |
| 14 | + |
| 15 | + def supported(path: Path): Boolean |
| 16 | + |
| 17 | + def targetOfDirectory(path: File): Option[String] |
| 18 | + |
| 19 | + private def buildWithCommand(command: List[String], path: Path): Try[Boolean] = { |
| 20 | + CommandRunner.exec(command, dir = Option(path.toFile)) match { |
| 21 | + case Left(failure) => Failure(failure) |
| 22 | + case Right(output) if output.exitCode != 0 => |
| 23 | + Failure(new Exception("Can't compile project.")) |
| 24 | + |
| 25 | + case Right(output) => Success(true) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + def build(path: Path): Try[Boolean] = { |
| 30 | + buildWithCommand(command, path) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +object MavenBuilder extends Builder { |
| 35 | + val command = List("mvn", "compile") |
| 36 | + val pathComponents = Seq("src", "main", "java") |
| 37 | + |
| 38 | + def supported(path: Path): Boolean = { |
| 39 | + path.toFile.list.contains("pom.xml") |
| 40 | + } |
| 41 | + |
| 42 | + def targetOfDirectory(path: File): Option[String] = { |
| 43 | + Some(Seq(path.getAbsolutePath, "target", "classes").mkString(File.separator)) |
| 44 | + } |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +object SBTBuilder extends Builder { |
| 49 | + |
| 50 | + val command = List("sbt", "compile") |
| 51 | + val pathComponents = Seq("src", "main", "scala") |
| 52 | + |
| 53 | + def supported(path: Path): Boolean = { |
| 54 | + path.toFile.list.contains("build.sbt") |
| 55 | + } |
| 56 | + |
| 57 | + def targetOfDirectory(path: File): Option[String] = { |
| 58 | + val target = new File(path.getAbsolutePath, "target") |
| 59 | + Option(target.exists).flatMap { |
| 60 | + case true => |
| 61 | + val potentialScalaDir = target.list.find { case filepath => filepath.startsWith("scala-") } |
| 62 | + potentialScalaDir.map { case scalaDirectory => |
| 63 | + target.toPath.resolve(scalaDirectory).resolve("classes").toString |
| 64 | + } |
| 65 | + case false => |
| 66 | + Option.empty |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments