@@ -19,41 +19,60 @@ import java.io.{BufferedWriter, FileOutputStream, FileWriter}
19
19
import java .nio .file .{Files , Paths }
20
20
21
21
import com .softwaremill .sttp ._
22
- import de .upb .cs .swt .delphi .cli .artifacts .{QueryStorageMetadata , SearchResult }
22
+ import de .upb .cs .swt .delphi .cli .artifacts .{QueryStorageMetadata , Result , RetrieveResult , SearchResult }
23
23
import org .joda .time .DateTime
24
24
import org .joda .time .format .DateTimeFormat
25
25
import spray .json ._
26
-
27
26
import de .upb .cs .swt .delphi .cli .artifacts .StorageMetadataJson .queryStorageMetadataFormat
28
27
29
28
class FileOutput (serverVersion : String = " UNKNOWN" )(implicit config: Config , backend : SttpBackend [Id , Nothing ]){
30
29
31
- def writeSearchResults (results : List [SearchResult ]) : Unit = {
30
+ def writeQueryResults (results : List [SearchResult ]): Unit = {
32
31
val metadata = buildQueryMetadata(results)
33
32
34
33
val folderPath = Paths .get(config.output, DateTimeFormat .forPattern(" YYYY-MM-dd_HH_mm_ss" ).print(metadata.timestamp))
35
34
Files .createDirectory(folderPath)
36
35
37
- val metadataPath = Paths .get(folderPath.toString, " query-metadata.json" ).toString
36
+ downloadResultFiles(results, metadata, folderPath.toString)
37
+ }
38
+
39
+ def writeRetrieveResults (results : Seq [RetrieveResult ]): Unit = {
40
+ val metadata = buildRetrieveMetadata(results)
41
+ val first = results.head
42
+
43
+ val timestamp = DateTimeFormat .forPattern(" YYYY-MM-dd_HH_mm_ss" ).print(metadata.timestamp)
44
+ val folderPath = Paths .get(config.output, s " ${first.metadata.artifactId}- ${first.metadata.version}- $timestamp" )
45
+ Files .createDirectory(folderPath)
46
+
47
+ downloadResultFiles(results, metadata, folderPath.toString)
48
+ }
49
+
50
+
51
+ private def downloadResultFiles (results : Seq [Result ], metadata : QueryStorageMetadata , folderPath : String ) : Unit = {
52
+ // Write Metadata first
53
+ val metadataPath = Paths .get(folderPath, " query-metadata.json" ).toString
38
54
val writer = new BufferedWriter (new FileWriter (metadataPath))
39
55
writer.write(metadata.toJson.prettyPrint)
40
56
writer.close()
41
57
42
58
val outputMode = config.outputMode.getOrElse(OutputMode .PomOnly )
43
- var progressCnt = 0f
44
59
45
60
info()
46
- info(f " Output Mode is ${outputMode.toString}, destination is ${folderPath.toString}" )
61
+ outputMode match {
62
+ case OutputMode .PomOnly => info(f " All associated POM files will be stored in $folderPath" )
63
+ case OutputMode .JarOnly => info(f " All associated JAR files will be stored in $folderPath" )
64
+ case _ => info(f " All associated JAR and POM files will be stored in $folderPath" )
65
+ }
66
+ var progressCnt = 0f
67
+
47
68
info()
48
69
print(" Downloading files: 00 %" )
49
70
50
71
results
51
72
.map(r => r.toMavenRelativeUrl() + s " / ${r.metadata.artifactId}- ${r.metadata.version}" )
52
73
.map(relUrl => " https://repo1.maven.org/maven2/" + relUrl).foreach( urlWithoutExtension => {
53
74
54
- print(" \b\b\b\b " )
55
- val progressValue = (100f * progressCnt ).toInt / results.size
56
- print(s " ${if (progressValue < 10 ) f " 0 $progressValue" else progressValue} % " )
75
+ writeProgressValue((100f * progressCnt ).toInt / results.size)
57
76
progressCnt += 1
58
77
59
78
var artifactsToRetrieve = Seq [String ]()
@@ -65,15 +84,13 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
65
84
}
66
85
artifactsToRetrieve.foreach( url => {
67
86
sttp.get(uri " $url" ).response(asByteArray).send().body match {
68
- case Right (value) =>
69
- new FileOutputStream (Paths .get(folderPath.toString, url.splitAt(url.lastIndexOf('/' ))._2).toString)
70
- .write(value)
71
- case Left (value) =>
72
- error(f " Failed to download artifact from $url, got: $value" )
87
+ case Right (value) => new FileOutputStream (Paths .get(folderPath, url.splitAt(url.lastIndexOf('/' ))._2).toString)
88
+ .write(value)
89
+ case Left (value) => error(f " Failed to download artifact from $url, got: $value" )
73
90
}
74
91
})
75
92
})
76
- print( " \b\b\b\b 100 % " )
93
+ writeProgressValue( 100 )
77
94
info()
78
95
info()
79
96
info(f " Successfully wrote results to $folderPath. " )
@@ -82,7 +99,10 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
82
99
private def info (value : String = " " ): Unit = config.consoleOutput.outputInformation(value)
83
100
private def error (value : String = " " ): Unit = config.consoleOutput.outputError(value)
84
101
85
-
102
+ private def writeProgressValue (progressValue : Int ): Unit = {
103
+ print(" \b\b\b\b " )
104
+ print(s " ${if (progressValue < 10 ) f " 0 $progressValue" else progressValue} % " )
105
+ }
86
106
private def buildQueryMetadata (results : List [SearchResult ]) =
87
107
QueryStorageMetadata ( query = config.query,
88
108
results = results,
@@ -93,4 +113,15 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
93
113
resultLimit = config.limit.getOrElse(50 ),
94
114
outputMode = config.outputMode.getOrElse(OutputMode .PomOnly ).toString
95
115
)
116
+
117
+ private def buildRetrieveMetadata (results : Seq [RetrieveResult ]) =
118
+ QueryStorageMetadata (query = f " Retrieve ${config.id}" ,
119
+ results = results,
120
+ serverVersion = serverVersion,
121
+ serverUrl = config.server,
122
+ clientVersion = BuildInfo .version,
123
+ timestamp = DateTime .now(),
124
+ resultLimit = 1 ,
125
+ outputMode = config.outputMode.getOrElse(OutputMode .PomOnly ).toString
126
+ )
96
127
}
0 commit comments