1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package cbt
import java.io._
import java.nio.file._
import java.net._
class Scaffold( logger: Logger ){
val lib = new Lib(logger)
private def createFile( projectDirectory: File, fileName: String, code: String ){
val outputFile = projectDirectory ++ ("/" ++ fileName)
Stage0Lib.write( outputFile, code, StandardOpenOption.CREATE_NEW )
import scala.Console._
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"""package ${packageFromDirectory(projectDirectory)}
object Main{
def main( args: Array[String] ): Unit = {
println( Console.GREEN ++ "Hello World" ++ Console.RESET )
}
}
"""
)
}
def createBuild(
projectDirectory: File
): Unit = {
createFile(projectDirectory, "build/build.scala", s"""import cbt._
class Build(val context: Context) extends BaseBuild{
override def dependencies =
super.dependencies ++ // don't forget super.dependencies here for scala-library, etc.
Seq(
// source dependency
// DirectoryDependency( projectDirectory ++ "/subProject" )
) ++
// pick resolvers explicitly for individual dependencies (and their transitive dependencies)
Resolver( mavenCentral, sonatypeReleases ).bind(
// CBT-style Scala dependencies
// ScalaDependency( "com.lihaoyi", "ammonite-ops", "0.5.5" )
// MavenDependency( "com.lihaoyi", "ammonite-ops_2.11", "0.5.5" )
// SBT-style dependencies
// "com.lihaoyi" %% "ammonite-ops" % "0.5.5"
// "com.lihaoyi" % "ammonite-ops_2.11" % "0.5.5"
)
}
"""
)
}
}
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"
)
}
}
|