5
5
package scalatronCLI .cmdline
6
6
7
7
import scalatronRemote .api .ScalatronRemote
8
- import scalatronRemote .api .ScalatronRemote .{ScalatronException , ConnectionConfig }
9
8
import java .io .{FileWriter , File }
10
9
import io .Source
11
10
import scalatronRemote .Version
12
11
import java .text .DateFormat
13
12
import java .util .Date
13
+ import scalatronRemote .api .ScalatronRemote .{SourceFileCollection , ScalatronException , ConnectionConfig }
14
14
15
15
16
16
/** A command line interface for interaction with a remote Scalatron server over the RESTful HTTP API.
@@ -73,6 +73,13 @@ object CommandLineProcessor {
73
73
println(" -sourceDir <path> the path of the local directory where the source files can be found" )
74
74
println(" -label <name> the label to apply to the versions (default: empty)" )
75
75
println(" " )
76
+ println(" getVersion retrieves the source code of the version with the given ID; as user only" )
77
+ println(" -targetDir <path> the path of the local directory where the source files should be stored" )
78
+ println(" -id <int> the version's ID" )
79
+ println(" " )
80
+ println(" deleteVersion deletes the version with the given ID; as user only" )
81
+ println(" -id <int> the version's ID" )
82
+ println(" " )
76
83
println(" benchmark runs standard isolated-bot benchmark on given source files; as user only" )
77
84
println(" -sourceDir <path> the path of the local directory where the source files can be found" )
78
85
println(" " )
@@ -86,6 +93,8 @@ object CommandLineProcessor {
86
93
println(" java -jar ScalatronCLI.jar -user Frankie -password a -cmd build" )
87
94
println(" java -jar ScalatronCLI.jar -user Frankie -password a -cmd versions" )
88
95
println(" java -jar ScalatronCLI.jar -user Frankie -password a -cmd createVersion -sourceDir /tempsrc -label \" updated\" " )
96
+ println(" java -jar ScalatronCLI.jar -user Frankie -password a -cmd getVersion -targetDir /tempsrc -id 1" )
97
+ println(" java -jar ScalatronCLI.jar -user Frankie -password a -cmd deleteVersion -id 1" )
89
98
println(" java -jar ScalatronCLI.jar -user Frankie -password a -cmd benchmark -sourceDir /tempsrc" )
90
99
System .exit(0 )
91
100
}
@@ -118,6 +127,8 @@ object CommandLineProcessor {
118
127
case " build" => cmd_buildSources(connectionConfig, argMap)
119
128
case " versions" => cmd_versions(connectionConfig, argMap)
120
129
case " createVersion" => cmd_createVersion(connectionConfig, argMap)
130
+ case " getVersion" => cmd_getVersion(connectionConfig, argMap)
131
+ case " deleteVersion" => cmd_deleteVersion(connectionConfig, argMap)
121
132
case " benchmark" => cmd_benchmark(connectionConfig, argMap)
122
133
case _ => System .err.println(" unknown command: " + command)
123
134
}
@@ -316,25 +327,11 @@ object CommandLineProcessor {
316
327
(scalatron : ScalatronRemote , loggedonUser : ScalatronRemote .User , users : ScalatronRemote .UserList ) => {
317
328
// get user source files (1 round-trip)
318
329
handleScalatronExceptionsFor {
319
- val targetDir = new File (targetDirPath)
320
- if (! targetDir.exists()) {
321
- if (! targetDir.mkdirs()) {
322
- System .err.println(" error: cannot create local directory '%s'" .format(targetDirPath))
323
- System .exit(- 1 )
324
- }
325
- }
326
-
327
- val sourceFiles = loggedonUser.getSourceFiles
328
- sourceFiles.foreach(sf => {
329
- val filename = sf.filename
330
- val filePath = targetDir.getAbsolutePath + " /" + filename
331
- val fileWriter = new FileWriter (filePath)
332
- fileWriter.append(sf.code)
333
- fileWriter.close()
334
- })
330
+ val sourceFileCollection = loggedonUser.sourceFiles
331
+ SourceFileCollection .writeTo(targetDirPath, sourceFileCollection, connectionConfig.verbose)
335
332
336
333
if (connectionConfig.verbose)
337
- println(" Wrote %d source files to '%s'" .format(sourceFiles .size, targetDirPath))
334
+ println(" Wrote %d source files to '%s'" .format(sourceFileCollection .size, targetDirPath))
338
335
}
339
336
}
340
337
)
@@ -358,12 +355,12 @@ object CommandLineProcessor {
358
355
(scalatron : ScalatronRemote , loggedonUser : ScalatronRemote .User , users : ScalatronRemote .UserList ) => {
359
356
// update user source files (1 round-trip)
360
357
handleScalatronExceptionsFor {
361
- val sourceFiles = readSourceFiles (sourceDirPath)
358
+ val sourceFileCollection = SourceFileCollection .loadFrom (sourceDirPath)
362
359
363
- loggedonUser.updateSourceFiles(sourceFiles )
360
+ loggedonUser.updateSourceFiles(sourceFileCollection )
364
361
365
362
if (connectionConfig.verbose)
366
- println(" Updated %d source files from '%s'" .format(sourceFiles .size, sourceDirPath))
363
+ println(" Updated %d source files from '%s'" .format(sourceFileCollection .size, sourceDirPath))
367
364
}
368
365
}
369
366
)
@@ -395,8 +392,7 @@ object CommandLineProcessor {
395
392
}
396
393
397
394
398
- /** -command sources gets a source files from a user workspace; user or Administrator
399
- * -targetDir path the path of the local directory where the source files should be stored
395
+ /** -command versions gets a list of versions for a specific user; as user only
400
396
*/
401
397
def cmd_versions (connectionConfig : ConnectionConfig , argMap : Map [String , String ]) {
402
398
doAsUser(
@@ -431,12 +427,91 @@ object CommandLineProcessor {
431
427
// update user source files (1 round-trip)
432
428
handleScalatronExceptionsFor {
433
429
val label = argMap.getOrElse(" -label" , " " )
434
- val sourceFiles = readSourceFiles (sourceDirPath)
430
+ val sourceFileCollection = SourceFileCollection .loadFrom (sourceDirPath)
435
431
436
- val version = loggedonUser.createVersion(label, sourceFiles )
432
+ val version = loggedonUser.createVersion(label, sourceFileCollection )
437
433
438
434
if (connectionConfig.verbose)
439
- println(" Create version #%d from %d source files from '%s'" .format(version.id, sourceFiles.size, sourceDirPath))
435
+ println(" Create version #%d from %d source files from '%s'" .format(version.id, sourceFileCollection.size, sourceDirPath))
436
+ }
437
+ }
438
+ )
439
+ }
440
+ }
441
+
442
+
443
+ /** -command getVersion retrieves the source code of the version with the given ID; as user only
444
+ * -targetDir path the path of the local directory where the source files should be stored
445
+ * -id int the version's ID
446
+ */
447
+ def cmd_getVersion (connectionConfig : ConnectionConfig , argMap : Map [String , String ]) {
448
+ argMap.get(" -targetDir" ) match {
449
+ case None =>
450
+ System .err.println(" error: command 'getVersion' requires option '-targetDir'" )
451
+ System .exit(- 1 )
452
+
453
+ case Some (targetDirPath) =>
454
+ argMap.get(" -id" ) match {
455
+ case None =>
456
+ System .err.println(" error: command 'getVersion' requires option '-id'" )
457
+ System .exit(- 1 )
458
+
459
+ case Some (versionIdStr) =>
460
+ val versionId = versionIdStr.toInt
461
+ doAsUser(
462
+ connectionConfig,
463
+ argMap,
464
+ (scalatron : ScalatronRemote , loggedonUser : ScalatronRemote .User , users : ScalatronRemote .UserList ) => {
465
+ // get user source files (1 round-trip)
466
+ handleScalatronExceptionsFor {
467
+ loggedonUser.version(versionId) match {
468
+ case None =>
469
+ System .err.println(" error: cannot locate version with id %d" .format(versionId))
470
+ System .exit(- 1 )
471
+
472
+ case Some (version) =>
473
+ val sourceFileCollection = version.sourceFiles
474
+ SourceFileCollection .writeTo(targetDirPath, sourceFileCollection, connectionConfig.verbose)
475
+
476
+ if (connectionConfig.verbose)
477
+ println(" Wrote %d source files to '%s'" .format(sourceFileCollection.size, targetDirPath))
478
+ }
479
+ }
480
+ }
481
+ )
482
+ }
483
+ }
484
+ }
485
+
486
+
487
+ /** -command deleteVersion deletes the version with the given ID; as user only
488
+ * -id int the version's ID
489
+ */
490
+ def cmd_deleteVersion (connectionConfig : ConnectionConfig , argMap : Map [String , String ]) {
491
+ argMap.get(" -id" ) match {
492
+ case None =>
493
+ System .err.println(" error: command 'deleteVersion' requires option '-id'" )
494
+ System .exit(- 1 )
495
+
496
+ case Some (versionIdStr) =>
497
+ val versionId = versionIdStr.toInt
498
+ doAsUser(
499
+ connectionConfig,
500
+ argMap,
501
+ (scalatron : ScalatronRemote , loggedonUser : ScalatronRemote .User , users : ScalatronRemote .UserList ) => {
502
+ // get user source files (1 round-trip)
503
+ handleScalatronExceptionsFor {
504
+ loggedonUser.version(versionId) match {
505
+ case None =>
506
+ System .err.println(" error: cannot locate version with id %d" .format(versionId))
507
+ System .exit(- 1 )
508
+
509
+ case Some (version) =>
510
+ version.delete()
511
+
512
+ if (connectionConfig.verbose)
513
+ println(" Deleted version with id %d" .format(versionId))
514
+ }
440
515
}
441
516
}
442
517
)
@@ -460,10 +535,10 @@ object CommandLineProcessor {
460
535
(scalatron : ScalatronRemote , loggedonUser : ScalatronRemote .User , users : ScalatronRemote .UserList ) => {
461
536
handleScalatronExceptionsFor {
462
537
// update user source files (1 round-trip)
463
- val sourceFiles = readSourceFiles (sourceDirPath)
464
- loggedonUser.updateSourceFiles(sourceFiles )
538
+ val sourceFileCollection = SourceFileCollection .loadFrom (sourceDirPath)
539
+ loggedonUser.updateSourceFiles(sourceFileCollection )
465
540
if (connectionConfig.verbose)
466
- println(" Updated %d source files from '%s'" .format(sourceFiles .size, sourceDirPath))
541
+ println(" Updated %d source files from '%s'" .format(sourceFileCollection .size, sourceDirPath))
467
542
468
543
// build uploaded user source files (1 round-trip)
469
544
val buildResult = loggedonUser.buildSources()
@@ -515,27 +590,6 @@ object CommandLineProcessor {
515
590
// helpers
516
591
// ------------------------------------------------------------------------------------------------------------------
517
592
518
- /** Reads a collection of source code files from disk, from a given directory.
519
- */
520
- private def readSourceFiles (sourceDirPath : String ) : Iterable [ScalatronRemote .SourceFile ] = {
521
- val sourceDir = new File (sourceDirPath)
522
- if (! sourceDir.exists()) {
523
- System .err.println(" error: local source directory does not exist: '%s'" .format(sourceDirPath))
524
- System .exit(- 1 )
525
- }
526
-
527
- val fileList = sourceDir.listFiles()
528
- if (fileList == null || fileList.isEmpty) {
529
- System .err.println(" error: local source directory is empty: '%s'" .format(sourceDirPath))
530
- System .exit(- 1 )
531
- }
532
- fileList.map(f => {
533
- val code = Source .fromFile(f).getLines().mkString(" \n " )
534
- ScalatronRemote .SourceFile (f.getName, code)
535
- })
536
- }
537
-
538
-
539
593
/** Accepts a closure; handles typical server exceptions. Exists with error code on such exceptions.
540
594
*/
541
595
private def handleScalatronExceptionsFor (action : => Unit ) {
0 commit comments