Skip to content

Commit

Permalink
Close #191 - Add libs name param (--libs-name)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-lee committed Mar 7, 2021
1 parent 1721ab2 commit 19e5f0d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 51 deletions.
13 changes: 6 additions & 7 deletions cli/src/main/scala/maven2sbt/cli/Maven2SbtApp.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package maven2sbt.cli

import java.io.{BufferedWriter, File, FileWriter}
import cats.data._
import cats.syntax.all._
import cats.effect._
import cats.syntax.all._
import effectie.cats.ConsoleEffect
import effectie.cats.EitherTSupport._
import maven2sbt.core.{BuildSbt, Libs, Maven2Sbt, Maven2SbtError, ScalaBinaryVersion}
import maven2sbt.core.{BuildSbt, Maven2Sbt, Maven2SbtError, ScalaBinaryVersion}
import pirate._
import piratex._

import java.io.{BufferedWriter, File, FileWriter}


/**
* @author Kevin Lee
Expand All @@ -31,8 +32,7 @@ object Maven2SbtApp extends MainIo[Maven2SbtArgs] {
if (file.isAbsolute) file else file.getCanonicalFile

override def run(args: Maven2SbtArgs): IO[Either[Maven2SbtError, Unit]] = args match {
case Maven2SbtArgs.FileArgs(scalaVersion, propsName, out, overwrite, pomPath) =>
val libsName = Libs.LibsName("libs")
case Maven2SbtArgs.FileArgs(scalaVersion, propsName, libsName, out, overwrite, pomPath) =>
val scalaBinaryVersionName = ScalaBinaryVersion.Name("scalaBinaryVersion").some
for {
pom <- IO(toCanonicalFile(pomPath))
Expand Down Expand Up @@ -61,8 +61,7 @@ object Maven2SbtApp extends MainIo[Maven2SbtArgs] {
}
} yield result

case Maven2SbtArgs.PrintArgs(scalaVersion, propsName, pomPath) =>
val libsName = Libs.LibsName("libs")
case Maven2SbtArgs.PrintArgs(scalaVersion, propsName, libsName, pomPath) =>
val scalaBinaryVersionName = ScalaBinaryVersion.Name("scalaBinaryVersion").some

(for {
Expand Down
12 changes: 7 additions & 5 deletions cli/src/main/scala/maven2sbt/cli/Maven2SbtArgs.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package maven2sbt.cli

import java.io.File

import maven2sbt.core.ScalaVersion
import maven2sbt.core.Props
import maven2sbt.core.{Libs, Props, ScalaVersion}

/**
* @author Kevin Lee
Expand All @@ -16,6 +14,7 @@ object Maven2SbtArgs {
final case class FileArgs(
scalaVersion: ScalaVersion
, propsName: Props.PropsName
, libsName: Libs.LibsName
, out: File
, overwrite: Overwrite
, pomPath: File
Expand All @@ -24,23 +23,26 @@ object Maven2SbtArgs {
final case class PrintArgs(
scalaVersion: ScalaVersion
, propsName: Props.PropsName
, libsName: Libs.LibsName
, pomPath: File
) extends Maven2SbtArgs


def fileArgs(
scalaVersion: ScalaVersion
, propsName: Props.PropsName
, libsName: Libs.LibsName
, out: File
, overwrite: Overwrite
, pomPath: File
): Maven2SbtArgs = FileArgs(scalaVersion, propsName, out, overwrite, pomPath)
): Maven2SbtArgs = FileArgs(scalaVersion, propsName, libsName, out, overwrite, pomPath)

def printArgs(
scalaVersion: ScalaVersion
, propsName: Props.PropsName
, libsName: Libs.LibsName
, pomPath: File
): Maven2SbtArgs = PrintArgs(scalaVersion, propsName, pomPath)
): Maven2SbtArgs = PrintArgs(scalaVersion, propsName, libsName, pomPath)

}

Expand Down
9 changes: 8 additions & 1 deletion cli/src/main/scala/maven2sbt/cli/Maven2SbtArgsParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scalaz._
import Scalaz._
import pirate._
import Pirate._
import maven2sbt.core.{Props, ScalaVersion}
import maven2sbt.core.{Libs, Props, ScalaVersion}
import maven2sbt.info.Maven2SbtBuildInfo

/**
Expand All @@ -24,13 +24,19 @@ object Maven2SbtArgsParser {
, metavar("<props-name>") |+| description("properties object name (e.g. 'props' in `lazy val props = new {}`) (default: props)")
).default("props").map(Props.PropsName.apply)

private val libsNameArg: Parse[Libs.LibsName] = flag[String](
long("libs-name")
, metavar("<libs-name>") |+| description("The name of the object containing all the libraries to re-use (e.g. 'libs' in `lazy val libs = new {}`) (default: libs)")
).default("libs").map(Libs.LibsName.apply)

private val pomPathArg: Parse[File] = argument[String](
metavar("<pom-path>") |+| description("Path to the pom file.")
).map(new File(_))

def fileParser: Parse[Maven2SbtArgs] = Maven2SbtArgs.fileArgs _ |*| ((
scalaVersionArg
, paramsNameArg
, libsNameArg
, flag[String](
both('o', "out")
, metavar("<file>") |+| description("output sbt config file (default: build.sbt)")
Expand All @@ -45,6 +51,7 @@ object Maven2SbtArgsParser {
def printParse: Parse[Maven2SbtArgs] = Maven2SbtArgs.printArgs _ |*| ((
scalaVersionArg
, paramsNameArg
, libsNameArg
, pomPathArg
))

Expand Down
36 changes: 26 additions & 10 deletions core/src/main/scala/maven2sbt/core/BuildSbt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,45 @@ object BuildSbt {
}

object Settings {
def render(settings: Settings, prefix: Option[String], propsName: Props.PropsName, libs: Libs, delimiter: String, indentSize: Int): String =
def render(
settings: Settings,
prefix: Option[String],
propsName: Props.PropsName,
libsName: Libs.LibsName,
libs: Libs,
delimiter: String,
indentSize: Int
): String =
(
settings.groupId.map(groupId => toFieldValue(prefix, propsName, groupId)).toList ++
settings.version.map(version => toFieldValue(prefix, propsName, version)).toList ++
settings.scalaVersion.map(scalaVersion => toFieldValue(prefix, propsName, scalaVersion)).toList ++
settings.artifactId.map(artifactId => toFieldValue(prefix, propsName, artifactId)).toList ++
renderListOfFieldValue(prefix, settings.repositories, indentSize)(repo => Render[Repository].render(propsName, repo)).toList ++
renderListOfFieldValue(prefix, settings.dependencies, indentSize)(dependency => ReferencedRender[Dependency].render(propsName, libs, dependency)).toList
renderListOfFieldValue(
prefix, settings.dependencies, indentSize)(
dependency => ReferencedRender[Dependency].render(propsName, libsName, libs, dependency)
).toList
)
.stringsMkString(delimiter)
}

final case class ProjectSettings(projectSettings: Settings) extends AnyVal
object ProjectSettings {
def render(propsName: Props.PropsName, libs: Libs, projectSettings: ProjectSettings): String =
Settings.render(projectSettings.projectSettings, none[String], propsName, libs, s",\n${indent(4)}", 4)
def render(
propsName: Props.PropsName, libsName: Libs.LibsName, libs: Libs, projectSettings: ProjectSettings
): String =
Settings.render(projectSettings.projectSettings, none[String], propsName, libsName, libs, s",\n${indent(4)}", 4)

}
final case class ThisBuildSettings(thisBuildSettings: Settings) extends AnyVal
object ThisBuildSettings {
def render(propsName: Props.PropsName, thisBuildSettings: ThisBuildSettings): String =
def render(propsName: Props.PropsName, libsName: Libs.LibsName, thisBuildSettings: ThisBuildSettings): String =
Settings.render(
thisBuildSettings.thisBuildSettings,
"ThisBuild / ".some,
propsName,
libsName,
Libs(List.empty[(Libs.LibValName, Dependency)]),
"\n",
2
Expand All @@ -96,10 +110,12 @@ object BuildSbt {
)
)

def render(propsName: Props.PropsName, globalSettings: GlobalSettings): String =
def render(propsName: Props.PropsName, libsName: Libs.LibsName, globalSettings: GlobalSettings): String =
Settings.render(
globalSettings.globalSettings,
"Global / ".some, propsName,
"Global / ".some,
propsName,
libsName,
Libs(List.empty[(Libs.LibValName, Dependency)]),
"\n",
2
Expand All @@ -115,9 +131,9 @@ object BuildSbt {
, libs
) =>

val globalSettingsRendered = GlobalSettings.render(propsName, globalSettings)
val thisBuildSettingsRendered = ThisBuildSettings.render(propsName, thisBuildSettings)
val projectSettingsRendered = ProjectSettings.render(propsName, libs, projectSettings)
val globalSettingsRendered = GlobalSettings.render(propsName, libsName, globalSettings)
val thisBuildSettingsRendered = ThisBuildSettings.render(propsName, libsName, thisBuildSettings)
val projectSettingsRendered = ProjectSettings.render(propsName, libsName, libs, projectSettings)

s"""
|$globalSettingsRendered
Expand Down
17 changes: 9 additions & 8 deletions core/src/main/scala/maven2sbt/core/Dependency.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ object Dependency {
implicit val namedDependency: Named[Dependency] = Named.named("libraryDependencies")
implicit val renderDependency: ReferencedRender[Dependency] =
ReferencedRender.namedReferencedRender(
"dependency", (propsName, libs, dependency) => Dependency.render(propsName, libs, dependency)
"dependency",
(propsName, libsName, libs, dependency) => Dependency.render(propsName, libsName, libs, dependency)
)

def from(pom: Node, scalaBinaryVersionName: Option[ScalaBinaryVersion.Name]): Seq[Dependency] =
Expand Down Expand Up @@ -158,7 +159,7 @@ object Dependency {
}
}

def render(propsName: Props.PropsName, libs: Libs, dependency: Dependency): RenderedString = {
def render(propsName: Props.PropsName, libsName: Libs.LibsName, libs: Libs, dependency: Dependency): RenderedString = {

def renderWithoutLibs(propsName: Props.PropsName, dependency: Dependency): RenderedString =
dependency match {
Expand Down Expand Up @@ -194,18 +195,18 @@ object Dependency {
) if version.version.isBlank || libVersion === version =>
if ((scope === Scope.Compile || scope === Scope.Default)) {
if (libExclusions.length === exclusions.length && libExclusions.diff(exclusions).isEmpty)
RenderedString.noQuotesRequired(s"libs.${libValName.libValName}")
RenderedString.noQuotesRequired(s"${libsName.libsName}.${libValName.libValName}")
else if (libExclusions.isEmpty)
RenderedString.noQuotesRequired(s"libs.${libValName.libValName}${Render[List[Exclusion]].render(propsName, exclusions).toQuotedString}")
RenderedString.noQuotesRequired(s"${libsName.libsName}.${libValName.libValName}${Render[List[Exclusion]].render(propsName, exclusions).toQuotedString}")
else
renderWithoutLibs(propsName, dependency)
} else {
if (libExclusions.length === exclusions.length && libExclusions.diff(exclusions).isEmpty)
RenderedString.noQuotesRequired(
s"""libs.${libValName.libValName}${Scope.renderNonCompileWithPrefix(" % ", scope)}"""
s"""${libsName.libsName}.${libValName.libValName}${Scope.renderNonCompileWithPrefix(" % ", scope)}"""
)
else if (libExclusions.isEmpty)
RenderedString.noQuotesRequired(s"libs.${libValName.libValName}${Scope.renderNonCompileWithPrefix(" % ", scope)}${Render[List[Exclusion]].render(propsName, exclusions).toQuotedString}")
RenderedString.noQuotesRequired(s"${libsName.libsName}.${libValName.libValName}${Scope.renderNonCompileWithPrefix(" % ", scope)}${Render[List[Exclusion]].render(propsName, exclusions).toQuotedString}")
else
renderWithoutLibs(propsName, dependency)
}
Expand All @@ -216,10 +217,10 @@ object Dependency {
case ((_, _, libVersion, libScope, libExclusions), (_, _, version, scope, exclusions)) if version.version.isBlank || libVersion === version =>
if (libScope === scope) {
if (libExclusions.length === exclusions.length && libExclusions.diff(exclusions).isEmpty)
RenderedString.noQuotesRequired(s"libs.${libValName.libValName}")
RenderedString.noQuotesRequired(s"${libsName.libsName}.${libValName.libValName}")
else if (libExclusions.isEmpty)
RenderedString.noQuotesRequired(
s"""libs.${libValName.libValName}${Render[List[Exclusion]].render(propsName, exclusions).toQuotedString}"""
s"""${libsName.libsName}.${libValName.libValName}${Render[List[Exclusion]].render(propsName, exclusions).toQuotedString}"""
)
else
renderWithoutLibs(propsName, dependency)
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/maven2sbt/core/ReferencedRender.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ package maven2sbt.core
* @since 2020-03-14
*/
trait ReferencedRender[A] {
def render(propsName: Props.PropsName, libs: Libs, a: A): RenderedString
def render(propsName: Props.PropsName, libsName: Libs.LibsName, libs: Libs, a: A): RenderedString
}

object ReferencedRender {
def apply[A: ReferencedRender]: ReferencedRender[A] = implicitly[ReferencedRender[A]]

def namedReferencedRender[A](
name: String,
f: (Props.PropsName, Libs, A) => RenderedString
f: (Props.PropsName, Libs.LibsName, Libs, A) => RenderedString
): ReferencedRender[A] =
NamedReferencedRender(name, f)

final case class NamedReferencedRender[A](
name: String,
f: (Props.PropsName, Libs, A) => RenderedString
f: (Props.PropsName, Libs.LibsName, Libs, A) => RenderedString
) extends ReferencedRender[A] {
override def render(propsName: Props.PropsName, libs: Libs, a: A): RenderedString =
f(propsName, libs, a)
override def render(propsName: Props.PropsName, libsName: Libs.LibsName, libs: Libs, a: A): RenderedString =
f(propsName, libsName, libs, a)
}
}

15 changes: 10 additions & 5 deletions core/src/test/scala/maven2sbt/core/BuildSbtSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,14 @@ object BuildSbtSpec extends Properties {
n <- Gen.int(Range.linear(0, 10)).log("n")
} yield {
val propsName = Props.PropsName("testProps")
val libsName = Libs.LibsName("testLibs")
val libs = Libs(List.empty[(Libs.LibValName, Dependency)])
val expected = none[String]
val actual = BuildSbt.renderListOfFieldValue(
none[String],
List.empty[Dependency],
n
)(dep => ReferencedRender[Dependency].render(propsName, libs, dep))
)(dep => ReferencedRender[Dependency].render(propsName, libsName, libs, dep))
actual ==== expected
}

Expand All @@ -336,13 +337,14 @@ object BuildSbtSpec extends Properties {
dependency <- Gens.genDependency.log("dependency")
} yield {
val propsName = Props.PropsName("testProps")
val libsName = Libs.LibsName("testLibs")
val libs = Libs(List.empty[(Libs.LibValName, Dependency)])
val expected = s"libraryDependencies += ${Dependency.render(propsName, libs, dependency).toQuotedString}".some
val expected = s"libraryDependencies += ${Dependency.render(propsName, libsName, libs, dependency).toQuotedString}".some
val actual = BuildSbt.renderListOfFieldValue(
none[String],
List(dependency),
n
)(dep => ReferencedRender[Dependency].render(propsName, libs, dep))
)(dep => ReferencedRender[Dependency].render(propsName, libsName, libs, dep))
actual ==== expected
}

Expand All @@ -351,17 +353,20 @@ object BuildSbtSpec extends Properties {
libraryDependencies <- Gens.genDependency.list(Range.linear(2, 10)).log("libraryDependencies")
} yield {
val propsName = Props.PropsName("testProps")
val libsName = Libs.LibsName("testLibs")
val libs = Libs(List.empty[(Libs.LibValName, Dependency)])
val idt = StringUtils.indent(n)
val expected =
s"""libraryDependencies ++= List(
|$idt${libraryDependencies.map(dep => Dependency.render(propsName, libs, dep).toQuotedString).stringsMkString(" ", s",\n$idt ", "")}
|$idt${libraryDependencies
.map(dep => Dependency.render(propsName, libsName, libs, dep).toQuotedString)
.stringsMkString(" ", s",\n$idt ", "")}
|$idt)""".stripMargin.some
val actual = BuildSbt.renderListOfFieldValue(
none[String],
libraryDependencies,
n
)(dep => ReferencedRender[Dependency].render(propsName, libs, dep))
)(dep => ReferencedRender[Dependency].render(propsName, libsName, libs, dep))
actual ==== expected
}

Expand Down
Loading

0 comments on commit 19e5f0d

Please sign in to comment.