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

Giter8 plugin #548

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions stage2/FakeContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cbt

import java.io.File
import java.util.concurrent.ConcurrentHashMap
import java.{lang, util}

class FakeContext(stage2Args: Stage2Args) extends Context {
override def cbtRootHome(): File = stage2Args.cbtHome

override def projectDirectory(): File = stage2Args.cbtHome

override def permanentKeys(): ConcurrentHashMap[String, AnyRef] = new ConcurrentHashMap

override def parentBuildOrNull(): BuildInterface = null

override def cbtLastModified(): Long = stage2Args.stage2LastModified

override def persistentCache(): util.Map[AnyRef, AnyRef] = stage2Args.persistentCache

override def start(): Long = 0

override def transientCache(): util.Map[AnyRef, AnyRef] = stage2Args.transientCache

override def argsArray(): Array[String] = Array.empty

override def cache(): File = stage2Args.cache

override def compatibilityTarget(): File = stage2Args.compatibilityTarget

override def startCompat(): lang.Long = new lang.Long(0)

override def enabledLoggersArray(): Array[String] = Array.empty

override def cbtHasChangedCompat(): lang.Boolean = false

override def cwd(): File = stage2Args.cwd

override def permanentClassLoaders(): ConcurrentHashMap[AnyRef, ClassLoader] = new ConcurrentHashMap()

override def scalaVersionOrNull(): String = null

override def cbtHome(): File = stage2Args.cbtHome
}

object FakeContext {
def apply(stage2Args: Stage2Args): FakeContext = new FakeContext(stage2Args)
}
2 changes: 1 addition & 1 deletion stage2/ToolsStage2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object ToolsStage2 extends Stage2Base{
def run( _args: Stage2Args ): ExitCode = {
val args = _args.args.dropWhile(Seq("tools") contains _)
val lib = new Lib(_args.logger)
val toolsTasks = new ToolsTasks(lib, args, _args.cwd, _args.cache, _args.cbtHome, _args.stage2LastModified)(_args.classLoaderCache)
val toolsTasks = new ToolsTasks(lib, args, _args)
lib.callReflective(toolsTasks, args.lift(0), null)
}
}
28 changes: 21 additions & 7 deletions stage2/ToolsTasks.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package cbt
import java.io.{Console => _, _}
import java.net._
import java.io.{Console=>_,_}
import java.nio.file._
class ToolsTasks(
lib: Lib,
args: Seq[String],
cwd: File,
cache: File,
cbtHome: File,
cbtLastModified: Long
)(implicit classLoaderCache: ClassLoaderCache){
stage2Args: Stage2Args
){
import stage2Args.{args => _, stage2LastModified => _, _}
private val cbtLastModified = stage2Args.stage2LastModified

def apply: String = "Available methods: " ++ lib.taskNames(getClass).mkString(" ")
override def toString = lib.usage(this.getClass, super.toString)
private val paths = CbtPaths(cbtHome, cache)
Expand All @@ -23,6 +22,21 @@ class ToolsTasks(
def `search-class` = java.awt.Desktop.getDesktop().browse(new URI(
"http://search.maven.org/#search%7Cga%7C1%7Cc%3A%22" ++ URLEncoder.encode(args(1),"UTF-8") ++ "%22"
))

def giter8 = {
val context: Context = FakeContext(stage2Args).copy(args = args.drop(1))
val giter = DirectoryDependency(cbtHome / "tools" / "giter8")(context).dependency
try{
lib.redirectOutToErr(lib.callReflective(giter, Some("createTemplate"), context))
ExitCode.Success
} catch {
case e: Throwable =>
System.err.println(e.getCause.getMessage)
ExitCode.Failure
}

}
def g8 = giter8
def gui = NailgunLauncher.main(Array(
"0.0",
(cbtHome / "tools" / "gui").getAbsolutePath,
Expand Down
1 change: 1 addition & 0 deletions stage2/plugins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class plugins( context: Context, scalaVersion: String ) {
),
None
)
final lazy val gitter = plugin( "giter8" )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't look like the plugin exists, no?

final lazy val googleJavaFormat = plugin( "google-java-format" )
final lazy val proguard = plugin( "proguard" )
final lazy val sbtLayout = plugin( "sbt_layout" )
Expand Down
17 changes: 17 additions & 0 deletions test/test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ object Main{
}
compile("../examples/uber-jar-example")

compile("../tools/giter8")

if( compat && fork ){ // FIXME: this should not be excluded in forking
val res = task("docJar","simple-fixed-cbt")
assert( res.out endsWith "simple-fixed-cbt_2.11-0.1-javadoc.jar\n", res.out )
Expand Down Expand Up @@ -506,6 +508,21 @@ object Main{
assert(res.exit0)
}

{
def deleteRecursively(file: File): Unit = {
if (file.isDirectory)
file.listFiles.foreach(deleteRecursively)
if (file.exists && !file.delete)
throw new Exception(s"Unable to delete ${file.getAbsolutePath}")
}
val templateDir = cbtHome / "test" / "temp"
val res = runCbt("../tools/giter8", Seq("createTemplate", "scala/scala-seed.g8", templateDir.getPath, "--name=template"))
assert(res.exit0)
println(res.out contains "Template applied")
assert((templateDir / "template").exists)
deleteRecursively(templateDir)
}

/*
// currently fails with
// java.lang.UnsupportedOperationException: scalafix.rewrite.ScalafixMirror.fromMirror $anon#typeSignature requires the semantic api
Expand Down
33 changes: 33 additions & 0 deletions tools/giter8/build/build.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.File

import cbt._
import giter8._
import scala.util._

class Build(val context: Context) extends BaseBuild {
def createTemplate = {
val helper = new JgitHelper(new Git(new JGitInteractor), G8TemplateRenderer)
(context.args.toList match {
case t :: Nil => Right(t, context.cwd, Seq.empty)
case t :: x :: Nil if x.startsWith("--") => Right(t, context.cwd, Seq(x))
case t :: x :: xs => Right(t, new File(x), xs)
case Nil => Left("Please, specify a template name")
})
.right.flatMap { case (template, dir, args) =>
Either.cond(args.forall(validateArgument),
(template, dir, args),
"""Incorrect arguments, it should be --<name>=<value>
|Example --name=my_cool_project
""".stripMargin)
}
.right.flatMap { case (template, dir, args) =>
val config = Config(template)
helper.run(config, args, dir)
}
.fold(f => Failure(new Exception(f)), s => Success(s))
.get
}

private def validateArgument(arg: String) =
arg.matches("""--[\w]+=[\w]+""")
}
9 changes: 9 additions & 0 deletions tools/giter8/build/build/build.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import cbt._

class Build(val context: Context) extends BuildBuild {
override def dependencies: Seq[Dependency] =
super.dependencies ++
Resolver(mavenCentral).bind(
ScalaDependency("org.foundweekends.giter8", "giter8-lib", "0.9.0")
)
}