diff options
author | Jan Christopher Vogt <oss.nsp@cvogt.org> | 2017-02-15 04:21:05 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-15 04:21:05 -0500 |
commit | 7ad65aeafc9d4fc9e0102c7d2ee8c9a31bd0be74 (patch) | |
tree | 12396906bde5f51cc8fc274c387d77e438bd2c23 /stage2/Scaffold.scala | |
parent | 2a1bf81eccf6b91e0dcdd2d7a05664da2049f3a1 (diff) | |
parent | 4f4c34f3f52b87057626682dd43a21cd83e2ff7a (diff) | |
download | cbt-7ad65aeafc9d4fc9e0102c7d2ee8c9a31bd0be74.tar.gz cbt-7ad65aeafc9d4fc9e0102c7d2ee8c9a31bd0be74.tar.bz2 cbt-7ad65aeafc9d4fc9e0102c7d2ee8c9a31bd0be74.zip |
Merge pull request #346 from cvogt/generate-packages
Generate packages
Diffstat (limited to 'stage2/Scaffold.scala')
-rw-r--r-- | stage2/Scaffold.scala | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/stage2/Scaffold.scala b/stage2/Scaffold.scala index 866e5da..68a966b 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 ) } @@ -27,7 +59,8 @@ trait Scaffold{ def createBuild( projectDirectory: File ): Unit = { - createFile(projectDirectory, "build/build.scala", s"""import cbt._ + createFile(projectDirectory, lib.buildDirectoryName++"/"++lib.buildFileName, s"""package cbt_build.${packageFromDirectory(projectDirectory)} +import cbt._ class Build(val context: Context) extends BaseBuild{ override def dependencies = super.dependencies ++ // don't forget super.dependencies here for scala-library, etc. @@ -50,3 +83,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" + ) + } +} |