Skip to content

Commit

Permalink
Merge pull request #2 from stephennancekivell/fix-idea-parsing-match-…
Browse files Browse the repository at this point in the history
…error

fix idea plugin parsing match error
  • Loading branch information
aiyanbo authored Feb 6, 2018
2 parents 80fdbc0 + 16d0c44 commit 9a46873
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
10 changes: 2 additions & 8 deletions src/main/scala/org/jmotor/sbt/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.nio.file.{ Files, Path, Paths }

import org.jmotor.sbt.model.ModuleStatus
import org.jmotor.sbt.service.ModuleUpdatesService
import org.jmotor.sbt.util.PluginParser
import sbt.CrossVersion._
import sbt.librarymanagement.Disabled
import sbt.{ ModuleID, ResolvedProject }
Expand All @@ -20,8 +21,6 @@ import scala.util.{ Failure, Success, Try }
*/
object Reporter {

private[this] val addSbtPluginRegex = """addSbtPlugin\("([\w\.-]+)" *%{1,2} *"([\w\.-]+)"\ *% *"([\w\.-]+)"\)""".r

def dependencyUpdates(dependencies: Seq[ModuleID], scalaVersion: String, scalaBinaryVersion: String): Seq[ModuleStatus] = {
val fullNameDependencies = dependencies.map { m
val remapVersion = m.crossVersion match {
Expand Down Expand Up @@ -52,12 +51,7 @@ object Reporter {
}
} match {
case Success(lines)
lines.filter { line
val trimLine = line.trim
trimLine.nonEmpty && trimLine.startsWith("addSbtPlugin")
} map {
case addSbtPluginRegex(org, n, v) ModuleID(org, n, v)
}
PluginParser.parseline(lines)
case Failure(_) Seq.empty[ModuleID]
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/org/jmotor/sbt/util/PluginParser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jmotor.sbt.util

import sbt.ModuleID

object PluginParser {
private[this] val addSbtPluginRegex = """addSbtPlugin\("([\w\.-]+)" *%{1,2} *"([\w\.-]+)"\ *% *"([\w\.-]+)"\)""".r

def parseline(lines: Seq[String]): Seq[ModuleID] = {
lines.map(_.trim).filter { line
line.nonEmpty && line.startsWith("addSbtPlugin")
}.flatMap {
case addSbtPluginRegex(org, n, v) Some(ModuleID(org, n, v))
case _ None
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class ModuleUpdatesServiceSpec extends FunSuite {

test("Resolve") {
val status = ModuleUpdatesService.resolve(Seq(
ModuleID("com.typesafe", "config", "1.3.0")
))
ModuleID("com.typesafe", "config", "1.3.0")))
assert(status.head.status == Status.Expired)
}

Expand Down
50 changes: 50 additions & 0 deletions src/test/scala/org/jmotor/sbt/util/PluginParserSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jmotor.sbt.util

import org.scalatest.{ FunSpec, Matchers }
import sbt.ModuleID

class PluginParserSpec extends FunSpec with Matchers {

describe("PluginParser") {
it("reads normal plugin") {
val lines =
"""
|addSbtPlugin("org.jmotor.sbt" % "sbt-dependency-updates" % "1.1.0")
""".stripMargin.split("\n")
val found = PluginParser.parseline(lines).toList
found shouldBe Seq(ModuleID("org.jmotor.sbt", "sbt-dependency-updates", "1.1.0"))
}

it("reads a few plugins with mixed content") {
val lines =
"""
| // comments
|
|addSbtPlugin("org.jmotor.sbt" % "sbt-dependency-updates" % "1.1.0")
|
| addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")
|
""".stripMargin.split("\n")

val found = PluginParser.parseline(lines).toList
found shouldBe Seq(
ModuleID("org.jmotor.sbt", "sbt-dependency-updates", "1.1.0"),
ModuleID("org.scalastyle", "scalastyle-sbt-plugin", "1.0.0"))
}

it("doesnt crash on the intellij plugin") {
val lines =
"""
|// Generated by IntelliJ-IDEA Scala plugin.
|// Add settings when starting sbt from IDEA.
|// Manual changes to this file will be lost.
|if (java.lang.System.getProperty("idea.runid", "false") == "2017.2") scala.collection.Seq(
|addSbtPlugin("org.jetbrains" % "sbt-structure-extractor" % "2017.2"),
|addSbtPlugin("org.jetbrains" % "sbt-idea-shell" % "2017.2")
|) else scala.collection.Seq.empty%
""".stripMargin.split("\n")

PluginParser.parseline(lines) shouldBe Seq(ModuleID("org.jetbrains", "sbt-idea-shell", "2017.2"))
}
}
}

0 comments on commit 9a46873

Please sign in to comment.