Skip to content

Commit 4ccd3b3

Browse files
author
Johannes Düsing
committed
Storing JAR and POM files now also possible for the retrieve command
1 parent 4685d61 commit 4ccd3b3

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed

src/main/scala/de/upb/cs/swt/delphi/cli/DelphiCLI.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,18 @@ object DelphiCLI {
9191
arg[String]("id").action((x, c) => c.copy(id = x)).text("The ID of the project to retrieve"),
9292
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)"),
9393
opt[Unit]('f', "file").action((_, c) => c.copy(opts = List("file"))).text("Use to load the ID from file, " +
94-
"with the filepath given in place of the ID")
94+
"with the filepath given in place of the ID"),
95+
opt[String](name = "output")
96+
.validate(x => if (Files.isDirectory(Paths.get(x))) success else failure(f"Output directory not found at $x"))
97+
.action((x, c) => c.copy(output = x))
98+
.text("Directory to write the result to"),
99+
opt[String](name = "outputmode")
100+
.validate(x => OutputMode.fromString(x) match {
101+
case Some(_) => success
102+
case None => failure("Only JarOnly, PomOnly and All are supported for output modes.")
103+
})
104+
.action((x, c) => c.copy(outputMode = OutputMode.fromString(x)))
105+
.text("Defines what to store. Supported are JarOnly, PomOnly and All. Defaults to PomOnly. Requires output to be set.")
95106
)
96107

97108
cmd("search").action((s, c) => c.copy(mode = "search"))

src/main/scala/de/upb/cs/swt/delphi/cli/FileOutput.scala

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,60 @@ import java.io.{BufferedWriter, FileOutputStream, FileWriter}
1919
import java.nio.file.{Files, Paths}
2020

2121
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}
2323
import org.joda.time.DateTime
2424
import org.joda.time.format.DateTimeFormat
2525
import spray.json._
26-
2726
import de.upb.cs.swt.delphi.cli.artifacts.StorageMetadataJson.queryStorageMetadataFormat
2827

2928
class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, backend: SttpBackend[Id, Nothing]){
3029

31-
def writeSearchResults(results: List[SearchResult]) : Unit = {
30+
def writeQueryResults(results: List[SearchResult]): Unit = {
3231
val metadata = buildQueryMetadata(results)
3332

3433
val folderPath = Paths.get(config.output, DateTimeFormat.forPattern("YYYY-MM-dd_HH_mm_ss").print(metadata.timestamp))
3534
Files.createDirectory(folderPath)
3635

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
3854
val writer = new BufferedWriter(new FileWriter(metadataPath))
3955
writer.write(metadata.toJson.prettyPrint)
4056
writer.close()
4157

4258
val outputMode = config.outputMode.getOrElse(OutputMode.PomOnly)
43-
var progressCnt = 0f
4459

4560
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+
4768
info()
4869
print("Downloading files: 00 %")
4970

5071
results
5172
.map(r => r.toMavenRelativeUrl() + s"/${r.metadata.artifactId}-${r.metadata.version}")
5273
.map(relUrl => "https://repo1.maven.org/maven2/" + relUrl).foreach( urlWithoutExtension => {
5374

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)
5776
progressCnt += 1
5877

5978
var artifactsToRetrieve = Seq[String]()
@@ -65,15 +84,13 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
6584
}
6685
artifactsToRetrieve.foreach( url => {
6786
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")
7390
}
7491
})
7592
})
76-
print("\b\b\b\b100 %")
93+
writeProgressValue(100)
7794
info()
7895
info()
7996
info(f"Successfully wrote results to $folderPath.")
@@ -82,7 +99,10 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
8299
private def info(value: String = ""):Unit = config.consoleOutput.outputInformation(value)
83100
private def error(value: String = ""):Unit = config.consoleOutput.outputError(value)
84101

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+
}
86106
private def buildQueryMetadata(results: List[SearchResult]) =
87107
QueryStorageMetadata( query = config.query,
88108
results = results,
@@ -93,4 +113,15 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
93113
resultLimit = config.limit.getOrElse(50),
94114
outputMode = config.outputMode.getOrElse(OutputMode.PomOnly).toString
95115
)
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+
)
96127
}

src/main/scala/de/upb/cs/swt/delphi/cli/artifacts/SearchResult.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package de.upb.cs.swt.delphi.cli.artifacts
1818

19-
import spray.json.DefaultJsonProtocol
19+
import spray.json.{DefaultJsonProtocol, JsValue, RootJsonFormat}
2020

2121
trait Result{
2222
val id: String
@@ -49,4 +49,13 @@ object SearchResultJson extends DefaultJsonProtocol {
4949
implicit val artifactFormat = jsonFormat5(ArtifactMetadata)
5050
implicit val searchResultFormat = jsonFormat3(SearchResult)
5151
implicit val retrieveResultFormat = jsonFormat3(RetrieveResult)
52+
53+
implicit object ResultJsonObject extends RootJsonFormat[Result] {
54+
override def read(json: JsValue): Result = searchResultFormat.read(json)
55+
56+
override def write(obj: Result): JsValue = obj match {
57+
case x: SearchResult => searchResultFormat.write(x)
58+
case x: RetrieveResult => retrieveResultFormat.write(x)
59+
}
60+
}
5261
}

src/main/scala/de/upb/cs/swt/delphi/cli/artifacts/StorageMetadata.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package de.upb.cs.swt.delphi.cli.artifacts
1818

1919
import org.joda.time.DateTime
2020
import spray.json.{DefaultJsonProtocol, JsonFormat}
21-
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson.searchResultFormat
21+
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson.ResultJsonObject
2222
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultsJson.DateJsonFormat
2323

2424
trait StorageMetadata {
@@ -30,7 +30,7 @@ trait StorageMetadata {
3030
}
3131

3232
case class QueryStorageMetadata(query: String,
33-
results: Seq[SearchResult],
33+
results: Seq[Result],
3434
resultLimit: Int,
3535
clientVersion: String,
3636
serverVersion: String,

src/main/scala/de/upb/cs/swt/delphi/cli/commands/RetrieveCommand.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ object RetrieveCommand extends Command {
6262
information.apply("Results written to file '" + config.csv + "'")
6363
}
6464
}
65+
if(!config.output.equals("")){
66+
val jsonArr = s.parseJson.asInstanceOf[JsArray].elements
67+
val retrieveResults = jsonArr.map(r => r.convertTo[RetrieveResult])
68+
69+
new FileOutput(executeGet(Seq("version")).getOrElse("UNKNOWN"))
70+
.writeRetrieveResults(retrieveResults)
71+
}
6572
})
6673
}
6774
}

src/main/scala/de/upb/cs/swt/delphi/cli/commands/SearchCommand.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object SearchCommand extends Command with DefaultJsonProtocol{
106106

107107
if (!config.output.equals("")){
108108
val output = new FileOutput(executeGet(Seq("version")).getOrElse("UNKNOWN"))
109-
output.writeSearchResults(sr)
109+
output.writeQueryResults(sr)
110110
}
111111

112112
if (!config.csv.equals("")) {

0 commit comments

Comments
 (0)