aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2017-02-15 01:37:53 -0500
committerChristopher Vogt <oss.nsp@cvogt.org>2017-02-15 03:47:49 -0500
commit7ee113962134e3c9c4659d6de9de3c7015174d3f (patch)
tree55f6158be8a53d3ad640f3cd038baeaf1a8f1f30
parent2c8257b0c119b38102aa0c3efecffc7fefc9d766 (diff)
downloadcbt-7ee113962134e3c9c4659d6de9de3c7015174d3f.tar.gz
cbt-7ee113962134e3c9c4659d6de9de3c7015174d3f.tar.bz2
cbt-7ee113962134e3c9c4659d6de9de3c7015174d3f.zip
add package to generated Main.scala to avoid classpath weirdnesses
caused by multiple root package Main classes from different subproject or test projects ending up on the same classpath
-rw-r--r--stage2/Lib.scala20
-rw-r--r--stage2/Scaffold.scala70
-rw-r--r--stage2/ToolsTasks.scala5
-rw-r--r--test/test.scala2
4 files changed, 84 insertions, 13 deletions
diff --git a/stage2/Lib.scala b/stage2/Lib.scala
index 0d382f2..fc53c4f 100644
--- a/stage2/Lib.scala
+++ b/stage2/Lib.scala
@@ -15,7 +15,7 @@ import scala.util._
case class Developer(id: String, name: String, timezone: String, url: URL)
/** Don't extend. Create your own libs :). */
-final class Lib(val logger: Logger) extends Stage1Lib(logger) with Scaffold{
+final class Lib(val logger: Logger) extends Stage1Lib(logger){
lib =>
val buildFileName = "build.scala"
@@ -28,16 +28,11 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger) with Scaffold{
This can either the Build itself, of if exists a BuildBuild or a BuildBuild for a BuildBuild and so on.
*/
def loadRoot(context: Context): BuildInterface = {
- def findStartDir(directory: File): File = {
- val buildDir = realpath( directory ++ "/build" )
- if(buildDir.exists) findStartDir(buildDir) else directory
- }
-
val directory = context.workingDirectory
context.logger.composition( context.logger.showInvocation("Build.loadRoot",directory) )
- val start = findStartDir(directory)
+ val start = lib.findInnerMostModuleDirectory(directory)
val useBasicBuild = directory == start && start.getName != buildDirectoryName
@@ -534,4 +529,15 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger) with Scaffold{
}
}
}
+
+ def findInnerMostModuleDirectory(directory: File): File = {
+ val buildDir = realpath( directory ++ ("/" ++ lib.buildDirectoryName) )
+ // do not appent buildFileName here, so that we detect empty build folders
+ if(buildDir.exists) findInnerMostModuleDirectory(buildDir) else directory
+ }
+ def findOuterMostModuleDirectory(directory: File): File = {
+ if(
+ ( directory.getParentFile ++ ("/" ++ lib.buildDirectoryName) ).exists
+ ) findOuterMostModuleDirectory(directory.getParentFile) else directory
+ }
}
diff --git a/stage2/Scaffold.scala b/stage2/Scaffold.scala
index 866e5da..b110258 100644
--- a/stage2/Scaffold.scala
+++ b/stage2/Scaffold.scala
@@ -2,8 +2,8 @@ package cbt
import java.io._
import java.nio.file._
import java.net._
-trait Scaffold{
- def logger: Logger
+class Scaffold( logger: Logger ){
+ val lib = new Lib(logger)
private def createFile( projectDirectory: File, fileName: String, code: String ){
val outputFile = projectDirectory ++ ("/" ++ fileName)
@@ -12,10 +12,42 @@ trait Scaffold{
println( GREEN ++ "Created " ++ fileName ++ RESET )
}
+ private[cbt] def packageName(name: String) = {
+ def stripNonAlPrefix = (_:String).dropWhile(
+ !(('a' to 'z') ++ ('A' to 'Z') ++ Seq('_')).contains(_)
+ )
+ def removeNonAlNumPlusSelected = "([^-a-zA-Z0-9_\\.\\\\/])".r.replaceAllIn(_:String, "")
+ def replaceSpecialWithUnderscore = "([-\\. ])".r.replaceAllIn(_:String, "_")
+ def removeRepeatedDots = "\\.+".r.replaceAllIn(_:String, ".")
+ val transform = (
+ (
+ stripNonAlPrefix
+ andThen
+ removeNonAlNumPlusSelected
+ andThen
+ replaceSpecialWithUnderscore
+ ).andThen(
+ (_:String).replace("/",".").replace("\\",".").toLowerCase
+ ) andThen removeRepeatedDots
+
+ )
+
+ transform( name )
+ }
+
+ private[cbt] def packageFromDirectory(directory: File) = {
+ packageName(
+ directory.getAbsolutePath.stripPrefix(
+ lib.findOuterMostModuleDirectory( directory ).getParentFile.getAbsolutePath
+ )
+ )
+ }
+
def createMain(
projectDirectory: File
- ): Unit = {
- createFile(projectDirectory, "Main.scala", s"""object Main{
+ ): Unit = {
+ createFile(projectDirectory, "Main.scala", s"""package ${packageFromDirectory(projectDirectory)}
+object Main{
def main( args: Array[String] ): Unit = {
println( Console.GREEN ++ "Hello World" ++ Console.RESET )
}
@@ -50,3 +82,33 @@ class Build(val context: Context) extends BaseBuild{
)
}
}
+object ScaffoldTest{
+ val scaffold = new Scaffold(new Logger(None,System.currentTimeMillis))
+ import scaffold._
+ def main(args: Array[String]): Unit = {
+ def assertEquals[T](left: T, right: T) = {
+ assert( left == right, left + " == " + right )
+ }
+ assertEquals(
+ packageName( "AsdfAsdfAsdf" ), "asdfasdfasdf"
+ )
+ assertEquals(
+ packageName( "_AsdfA4sdf" ), "_asdfa4sdf"
+ )
+ assertEquals(
+ packageName( "-AsdfAsdf" ), "asdfasdf"
+ )
+ assertEquals(
+ packageName( "asdf 4aSdf" ), "asdf4asdf"
+ )
+ assertEquals(
+ packageName( "&/(&%$&&/(asdf" ), "asdf"
+ )
+ assertEquals(
+ packageName( "AAA" ), "aaa"
+ )
+ assertEquals(
+ packageName( "/AAA/a_a/a.a" ), "aaa.a_a.a_a"
+ )
+ }
+}
diff --git a/stage2/ToolsTasks.scala b/stage2/ToolsTasks.scala
index 25156fb..943f096 100644
--- a/stage2/ToolsTasks.scala
+++ b/stage2/ToolsTasks.scala
@@ -15,8 +15,9 @@ class ToolsTasks(
implicit val logger: Logger = lib.logger
implicit val transientCache: java.util.Map[AnyRef,AnyRef] = new java.util.HashMap
private def Resolver( urls: URL* ) = MavenResolver(cbtLastModified,mavenCache,urls: _*)
- def createMain: Unit = lib.createMain( cwd )
- def createBuild: Unit = lib.createBuild( cwd )
+ val scaffold = new Scaffold(logger)
+ def createMain: Unit = scaffold.createMain( cwd )
+ def createBuild: Unit = scaffold.createBuild( cwd )
def gui = NailgunLauncher.main(Array(
"0.0",
(cbtHome / "tools" / "gui").getAbsolutePath,
diff --git a/test/test.scala b/test/test.scala
index 1665f13..778fcda 100644
--- a/test/test.scala
+++ b/test/test.scala
@@ -181,6 +181,8 @@ object Main{
path => assert(new File(path).exists, path)
}
+ ScaffoldTest.main(Array())
+
usage("nothing")
compile("nothing")
//clean("nothing")