@@ -16,7 +16,7 @@ import scala.util.matching.Regex
16
16
17
17
final case class Inputs (
18
18
elements : Seq [Inputs .Element ],
19
- mainClassElement : Option [Inputs .SingleElement ],
19
+ defaultMainClassElement : Option [Inputs .Script ],
20
20
workspace : os.Path ,
21
21
baseProjectName : String ,
22
22
mayAppendHash : Boolean ,
@@ -57,7 +57,7 @@ final case class Inputs(
57
57
58
58
private lazy val inputsHash : String =
59
59
Inputs .inputsHash(elements)
60
- lazy val projectName = {
60
+ lazy val projectName : String = {
61
61
val needsSuffix = mayAppendHash && (elements match {
62
62
case Seq (d : Inputs .Directory ) => d.path != workspace
63
63
case _ => true
@@ -87,19 +87,18 @@ final case class Inputs(
87
87
if (forbidden.exists(workspace.startsWith)) inHomeDir(directories)
88
88
else this
89
89
def checkAttributes (directories : Directories ): Inputs = {
90
+ @ tailrec
90
91
def existingParent (p : os.Path ): Option [os.Path ] =
91
92
if (os.exists(p)) Some (p)
92
93
else if (p.segmentCount <= 0 ) None
93
94
else existingParent(p / os.up)
94
95
def reallyOwnedByUser (p : os.Path ): Boolean =
95
96
if (Properties .isWin)
96
- p.toIO.canWrite() // Wondering if there's a better way to do that…
97
+ p.toIO.canWrite // Wondering if there's a better way to do that…
97
98
else
98
99
os.owner(p) == os.owner(os.home) &&
99
- p.toIO.canWrite()
100
- val canWrite = existingParent(workspace)
101
- .map(reallyOwnedByUser)
102
- .getOrElse(false )
100
+ p.toIO.canWrite
101
+ val canWrite = existingParent(workspace).exists(reallyOwnedByUser)
103
102
if (canWrite) this
104
103
else inHomeDir(directories)
105
104
}
@@ -123,7 +122,7 @@ final case class Inputs(
123
122
Iterator (v.content, bytes(" \n " ))
124
123
}
125
124
val md = MessageDigest .getInstance(" SHA-1" )
126
- it.foreach(md.update(_) )
125
+ it.foreach(md.update)
127
126
val digest = md.digest()
128
127
val calculatedSum = new BigInteger (1 , digest)
129
128
String .format(s " %040x " , calculatedSum)
@@ -188,15 +187,15 @@ object Inputs {
188
187
189
188
final case class Script (base : os.Path , subPath : os.SubPath )
190
189
extends OnDisk with SourceFile with AnyScalaFile with AnyScript {
191
- lazy val path = base / subPath
190
+ lazy val path : os. Path = base / subPath
192
191
}
193
192
final case class ScalaFile (base : os.Path , subPath : os.SubPath )
194
193
extends OnDisk with SourceFile with AnyScalaFile {
195
- lazy val path = base / subPath
194
+ lazy val path : os. Path = base / subPath
196
195
}
197
196
final case class JavaFile (base : os.Path , subPath : os.SubPath )
198
197
extends OnDisk with SourceFile with Compiled {
199
- lazy val path = base / subPath
198
+ lazy val path : os. Path = base / subPath
200
199
}
201
200
final case class Directory (path : os.Path ) extends OnDisk with Compiled
202
201
final case class ResourceDirectory (path : os.Path ) extends OnDisk
@@ -226,13 +225,13 @@ object Inputs {
226
225
Iterator (bytes(" virtual:" ), v.content, bytes(" \n " ))
227
226
}
228
227
val md = MessageDigest .getInstance(" SHA-1" )
229
- it.foreach(md.update(_) )
228
+ it.foreach(md.update)
230
229
val digest = md.digest()
231
230
val calculatedSum = new BigInteger (1 , digest)
232
231
String .format(s " %040x " , calculatedSum).take(10 )
233
232
}
234
233
235
- def homeWorkspace (elements : Seq [Element ], directories : Directories ) = {
234
+ def homeWorkspace (elements : Seq [Element ], directories : Directories ): os. Path = {
236
235
val hash0 = inputsHash(elements)
237
236
val dir = directories.virtualProjectsDir / hash0.take(2 ) / s " project- ${hash0.drop(2 )}"
238
237
os.makeDir.all(dir)
@@ -279,14 +278,11 @@ object Inputs {
279
278
case _ : ResourceDirectory => true
280
279
case _ : Virtual => true
281
280
}
282
- val mainClassElemOpt = validElems
283
- .collectFirst {
284
- case f : SourceFile => f
285
- case vsf : VirtualScalaFile => vsf
286
- }
281
+ // only on-disk scripts need a main class override
282
+ val defaultMainClassElemOpt = validElems.collectFirst { case script : Script => script }
287
283
Inputs (
288
284
updatedElems,
289
- mainClassElemOpt ,
285
+ defaultMainClassElemOpt ,
290
286
workspace,
291
287
baseProjectName,
292
288
mayAppendHash = needsHash,
@@ -295,23 +291,22 @@ object Inputs {
295
291
}
296
292
297
293
private val githubGistsArchiveRegex : Regex =
298
- s """ : \\ / \\ /gist \\ .github \\ .com \\ /[^ \\ /]*? \\ /[^ \\ /]* $$ """ .r
299
-
300
- private def resolve (path : String , content : Array [Byte ]): Element = {
301
- val wrapperPath =
302
- os.sub / path.split(" /" ).last
294
+ s """ ://gist \\ .github \\ .com/[^/]*?/[^/]* $$ """ .r
303
295
296
+ private def resolve (path : String , content : Array [Byte ]): Element =
304
297
if (path.endsWith(" .scala" )) VirtualScalaFile (content, path)
305
298
else if (path.endsWith(" .java" )) VirtualJavaFile (content, path)
306
- else if (path.endsWith(" .sc" )) VirtualScript (content, path, wrapperPath)
299
+ else if (path.endsWith(" .sc" )) {
300
+ val wrapperPath = os.sub / path.split(" /" ).last
301
+ VirtualScript (content, path, wrapperPath)
302
+ }
307
303
else VirtualData (content, path)
308
- }
309
304
310
305
private def resolveZipArchive (content : Array [Byte ]): Seq [Element ] = {
311
306
val zipInputStream = new ZipInputStream (new ByteArrayInputStream (content))
312
307
@ tailrec
313
308
def readArchive (acc : Seq [Element ]): Seq [Element ] =
314
- Option (zipInputStream.getNextEntry() ) match {
309
+ Option (zipInputStream.getNextEntry) match {
315
310
case Some (entry) if entry.isDirectory => readArchive(acc)
316
311
case Some (entry) =>
317
312
val content = zipInputStream.readAllBytes()
@@ -429,7 +424,7 @@ object Inputs {
429
424
def empty (workspace : os.Path ): Inputs =
430
425
Inputs (
431
426
elements = Nil ,
432
- mainClassElement = None ,
427
+ defaultMainClassElement = None ,
433
428
workspace = workspace,
434
429
baseProjectName = " project" ,
435
430
mayAppendHash = true ,
0 commit comments