Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run rubocop with bundle exec #618

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix tool
  • Loading branch information
DMarinhoCodacy committed Jul 1, 2024
commit e215d9b8f74f5407780901bf1941ba54e5991a40
76 changes: 35 additions & 41 deletions src/main/scala/codacy/rubocop/Rubocop.scala
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ import com.codacy.tools.scala.seed.utils.CommandRunner
import play.api.libs.json._

import scala.io.Source
import scala.util.{Failure, Properties, Success, Try}
import scala.util.{Failure, Properties, Try}

object Rubocop extends Tool {
private val plugins: List[String] =
@@ -39,49 +39,43 @@ object Rubocop extends Tool {
private val filesToIgnore: Set[String] =
Set("Gemfile.lock").map(_.toLowerCase())

override def apply(
source: api.Source.Directory,
configuration: Option[List[Pattern.Definition]],
files: Option[Set[api.Source.File]],
options: Map[Options.Key, Options.Value]
)(implicit specification: Tool.Specification): Try[List[Result]] = {
val cmd = getCommandFor(Paths.get(source.path), configuration, files, specification, resultFilePath)
CommandRunner.exec(cmd, Some(new File(source.path))) match {

case Right(resultFromTool) =>
parseResult(resultFilePath.toFile) match {
case s @ Success(_) => s
case Failure(e) =>
val msg =
s"""
|Rubocop exited with code ${resultFromTool.exitCode}
|message: ${e.getMessage}
|stdout: ${resultFromTool.stdout.mkString(Properties.lineSeparator)}
|stderr: ${resultFromTool.stderr.mkString(Properties.lineSeparator)}
""".stripMargin
Failure(new Exception(msg))
}

case Left(e) =>
Failure(e)
}
override def apply(
source: api.Source.Directory,
configuration: Option[List[Pattern.Definition]],
files: Option[Set[api.Source.File]],
options: Map[Options.Key, Options.Value]
)(implicit specification: Tool.Specification): Try[List[Result]] = {
val cmd = getCommandFor(Paths.get(source.path), configuration, files, specification, resultFilePath)
CommandRunner.exec(cmd, Some(new File(source.path))) match {
case Right(resultFromTool) =>
Try(parseResult(resultFilePath.toFile)).recover {
case e =>
val msg =
s"""
|Rubocop exited with code ${resultFromTool.exitCode}
|message: ${e.getMessage}
|stdout: ${resultFromTool.stdout.mkString(Properties.lineSeparator)}
|stderr: ${resultFromTool.stderr.mkString(Properties.lineSeparator)}
""".stripMargin
println(msg+resultFilePath)
List.empty[Result]
}

case Left(e) =>
Failure(e)
}
}

private[this] def parseResult(filename: File): Try[List[Result]] = {
Try {
val resultFromTool = Source.fromFile(filename).getLines().mkString
Json.parse(resultFromTool)
}.flatMap { json =>
json.validate[RubocopResult] match {
case JsSuccess(rubocopResult, _) =>
Success(rubocopResult.files.getOrElse(List.empty).flatMap { file =>
ruboFileToResult(file)
})
case JsError(err) =>
Failure(new Throwable(Json.stringify(JsError.toJson(err))))
}
}
private[this] def parseResult(filename: File): List[Result] = {
val resultFromTool = Source.fromFile(filename).getLines().mkString
val json = Json.parse(resultFromTool)
json.validate[RubocopResult] match {
case JsSuccess(rubocopResult, _) =>
rubocopResult.files.getOrElse(List.empty).flatMap(ruboFileToResult)
case JsError(_) =>
List.empty[Result] // Return an empty list in case of parsing errors
}
}

private[this] def ruboFileToResult(rubocopFiles: RubocopFiles): List[Result] = {
rubocopFiles.offenses.getOrElse(List.empty).map { offense =>