Skip to content

Commit

Permalink
Fix: Windows support (#148)
Browse files Browse the repository at this point in the history
* fix: Fix path separators for Windows

---------

Co-authored-by: Rafael Cortês <mrfyda@gmail.com>
  • Loading branch information
codacy-vrhpires and mrfyda authored Dec 13, 2023
1 parent d858082 commit 4d88d40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BinaryDockerHelper extends DockerHelper {

override def readDescription(docker: IDocker): Option[Set[PatternDescription]] = {
val cmd =
s"${DockerConstants.dockerRunCmd} --entrypoint=cat ${docker.dockerImage} ${cachedDescriptionsPathPrefix.resolve("description.json")}"
s"${DockerConstants.dockerRunCmd} --entrypoint=cat ${docker.dockerImage} $descriptionsFile"
.split(" ")
.toList

Expand All @@ -20,9 +20,10 @@ class BinaryDockerHelper extends DockerHelper {

override def readRaw(docker: IDocker, path: Path): Option[String] =
Try {
// path: hack for Windows because it compiles with dev OS fileSeparator
val shCmd =
s"""if [ -f $path ]; then
| cat $path;
s"""if [ -f ${path.toString().replace('\\', '/')} ]; then
| cat ${path.toString().replace('\\', '/')};
| if [ $$? -ne 0 ]; then
| exit 2;
| fi
Expand All @@ -37,6 +38,7 @@ class BinaryDockerHelper extends DockerHelper {
case Right(CommandResult(0, stdout, stderr @ _)) =>
Some(stdout.mkString(Properties.lineSeparator))
case Right(CommandResult(1, stdout @ _, stderr @ _)) => // file doesn't exist
println(errorMsg)
None
case Right(CommandResult(code @ _, stdout, stderr)) =>
Log.error(s"""$errorMsg
Expand Down
22 changes: 13 additions & 9 deletions src/main/scala/com/codacy/plugins/utils/DockerHelper.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.codacy.plugins.utils

import com.codacy.plugins.api.PatternDescription
import com.codacy.plugins.api.results.Tool
import com.codacy.plugins.api.results.{Pattern, Tool}
import com.codacy.plugins.runners.IDocker
import play.api.libs.json.{Format, Json}

import java.io.File
import java.nio.file.{Path, Paths}
import scala.util.Try

Expand All @@ -16,9 +17,12 @@ trait IDockerHelper {
}

abstract class DockerHelper extends IDockerHelper {

protected val docsRoot: Path = Paths.get("/docs")
protected val cachedDescriptionsPathPrefix = docsRoot.resolve("description")
protected val docsRoot: Path = Paths.get("docs")
protected val patternsFile = Paths.get(s"$docsRoot${File.separator}patterns.json")
protected val toolDescriptionFile = Paths.get(s"$docsRoot${File.separator}tool-description.md")
protected val descriptionsFile = Paths.get(s"$docsRoot${File.separator}description${File.separator}description.json")
protected def patternExplanationFile(id: Pattern.Id) =
Paths.get(s"$docsRoot${File.separator}description${File.separator}$id.md")

/**
* Reads the content of a file in a docker image. The file must be inside of `docsRoot`
Expand All @@ -32,14 +36,14 @@ abstract class DockerHelper extends IDockerHelper {
readDescription(docker)

override def loadPatterns(docker: IDocker): Option[Tool.Specification] =
readJsonDoc[Tool.Specification](docker, docsRoot.resolve("patterns.json"))
readJsonDoc[Tool.Specification](docker, patternsFile)

override def loadVersion(docker: IDocker): Option[String] =
readRaw(docker, docsRoot.resolve("patterns.json"))
readRaw(docker, patternsFile)
.flatMap(content => (Json.parse(content) \ "version").asOpt[String])

override def loadToolDescription(docker: IDocker): Option[String] =
readRaw(docker, docsRoot.resolve("tool-description.md"))
readRaw(docker, toolDescriptionFile)

protected def parseJsonDescriptions(content: String): Option[Set[PatternDescription]] = {
Try(Json.parse(content).asOpt[Set[PatternDescription]]).toOption.flatten
Expand All @@ -51,11 +55,11 @@ abstract class DockerHelper extends IDockerHelper {
*/
private[utils] def readDescription(docker: IDocker): Option[Set[PatternDescription]] = {
for {
content <- readRaw(docker, cachedDescriptionsPathPrefix.resolve("description.json"))
content <- readRaw(docker, descriptionsFile)
descriptions <- parseJsonDescriptions(content)
} yield {
descriptions.map { pattern =>
val descPath = cachedDescriptionsPathPrefix.resolve(s"${pattern.patternId}.md")
val descPath = patternExplanationFile(pattern.patternId)
val fileContents = readRaw(docker, descPath)
pattern.copy(explanation = fileContents)
}
Expand Down

0 comments on commit 4d88d40

Please sign in to comment.