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

fix: ensure properties are correctly resolved #12

Merged
merged 2 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ object itest extends MillIntegrationTestModule {
T {
Seq(
PathRef(testBase / "minimal") -> Seq(
TestInvocation.Targets(Seq("g8.validate"), noServer = true)
TestInvocation.Targets(Seq("g8.validate"), noServer = true),
TestInvocation
.Targets(Seq("validatePackageStructure"), noServer = true)
),
PathRef(testBase / "no-mill") -> Seq(
TestInvocation.Targets(Seq("g8.validate"), noServer = true)
TestInvocation.Targets(Seq("g8.validate"), noServer = true),
TestInvocation.Targets(
Seq("validatePackageStructure"),
noServer = true
)
)
)
}
Expand Down
10 changes: 10 additions & 0 deletions itest/shared/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ import munit.Assertions._
object g8 extends G8Module {
override def validationTargets = Seq("__.compile")
}

/** Just tests to ensure that $package$ is actually correctly expanded and the
* strucutre looks as it should
*/
def validatePackageStructure = T {
val expectedFile =
T.workspace / "out" / "g8" / "generate.overridden" / "io" / "kipp" / "mill" / "giter8" / "G8Module" / "generate.dest" / "result" / "minimal" / "src" / "com" / "example" / "someproject" / "Main.scala"

assert(os.exists(expectedFile))
}
4 changes: 4 additions & 0 deletions itest/shared/src/main/g8/default.properties
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
name = some project
organization = com.example
package = $organization$.$name;format="norm,word"$

verbatim = mill
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package minimal
package $package$

@main def hello = println("hello")
33 changes: 16 additions & 17 deletions plugin/src/io/kipp/mill/giter8/G8Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import mill.define.TaskModule
import os.Path

import java.nio.file.attribute.PosixFilePermission
import scala.util.Failure
import scala.util.Success
import scala.util.Using

trait G8Module extends TaskModule {

Expand Down Expand Up @@ -166,27 +163,29 @@ trait G8Module extends TaskModule {
val output = T.dest / "result"

val rawProps: Either[String, G8.OrderedProperties] =
Using(os.read.inputStream(propsFile().path))(G8.readProps) match {
case Failure(_) if !os.exists(propsFile().path) =>
log.info(s"No default.properties file found so skipping...")
Right(List.empty)
case Failure(err) =>
Left(
s"""|Something went wrong when trying to read your default.properties.
|
|${err.getMessage()}""".stripMargin
)

case Success(value) => Right(value)
if (!os.exists(propsFile().path)) {
log.info(s"No default.properties file found so skipping...")
Right(List.empty)
} else {
val in = os.read.inputStream(propsFile().path)
// NOTE that readProps closes the stream
Right(G8.readProps(in))
}

val result = for {
raw <- rawProps
props <- G8.transformProps(raw)
mavenTransformed <- G8.transformProps(raw)
fullyTransformed =
mavenTransformed
.foldLeft(G8.ResolvedProperties.empty) { case (resolved, (k, v)) =>
resolved + (k -> G8.DefaultValueF(v)(resolved))
}
result <- G8.fromDirectory(
templateDirectory = templateBase().toIO,
workingDirectory = templateBase().toIO,
arguments = props.map { case (key, value) => s"--${key}=${value}" },
arguments = fullyTransformed.map { case (key, value) =>
s"--${key}=${value}"
}.toSeq,
forceOverwrite = true,
outputDirectory = Some(output.toIO)
)
Expand Down