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