|
| 1 | +// Copyright (C) 2018 The Delphi Team. |
| 2 | +// See the LICENCE file distributed with this work for additional |
| 3 | +// information regarding copyright ownership. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +package de.upb.cs.swt.delphi.cli |
| 17 | + |
| 18 | +import java.io.{BufferedWriter, FileOutputStream, FileWriter} |
| 19 | +import java.nio.file.{Files, Paths} |
| 20 | + |
| 21 | +import com.softwaremill.sttp._ |
| 22 | +import de.upb.cs.swt.delphi.cli.artifacts.{QueryStorageMetadata, Result, RetrieveResult, SearchResult} |
| 23 | +import org.joda.time.DateTime |
| 24 | +import org.joda.time.format.DateTimeFormat |
| 25 | +import spray.json._ |
| 26 | +import de.upb.cs.swt.delphi.cli.artifacts.StorageMetadataJson.queryStorageMetadataFormat |
| 27 | + |
| 28 | +class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, backend: SttpBackend[Id, Nothing]){ |
| 29 | + |
| 30 | + def writeQueryResults(results: List[SearchResult]): Unit = { |
| 31 | + val metadata = buildQueryMetadata(results) |
| 32 | + |
| 33 | + val folderPath = Paths.get(config.output, DateTimeFormat.forPattern("YYYY-MM-dd_HH_mm_ss").print(metadata.timestamp)) |
| 34 | + Files.createDirectory(folderPath) |
| 35 | + |
| 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 |
| 54 | + val writer = new BufferedWriter(new FileWriter(metadataPath)) |
| 55 | + writer.write(metadata.toJson.prettyPrint) |
| 56 | + writer.close() |
| 57 | + |
| 58 | + val outputMode = config.outputMode.getOrElse(OutputMode.PomOnly) |
| 59 | + |
| 60 | + info() |
| 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 | + |
| 68 | + info() |
| 69 | + print("Downloading files: 00 %") |
| 70 | + |
| 71 | + results |
| 72 | + .map(r => r.toMavenRelativeUrl() + s"/${r.metadata.artifactId}-${r.metadata.version}") |
| 73 | + .map(relUrl => "https://repo1.maven.org/maven2/" + relUrl).foreach( urlWithoutExtension => { |
| 74 | + |
| 75 | + writeProgressValue((100f * progressCnt ).toInt / results.size) |
| 76 | + progressCnt += 1 |
| 77 | + |
| 78 | + var artifactsToRetrieve = Seq[String]() |
| 79 | + if (outputMode == OutputMode.PomOnly || outputMode == OutputMode.All){ |
| 80 | + artifactsToRetrieve = artifactsToRetrieve ++ Seq(s"$urlWithoutExtension.pom") |
| 81 | + } |
| 82 | + if(outputMode == OutputMode.JarOnly || outputMode == OutputMode.All){ |
| 83 | + artifactsToRetrieve = artifactsToRetrieve ++ Seq(s"$urlWithoutExtension.jar") |
| 84 | + } |
| 85 | + artifactsToRetrieve.foreach( url => { |
| 86 | + sttp.get(uri"$url").response(asByteArray).send().body match { |
| 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") |
| 90 | + } |
| 91 | + }) |
| 92 | + }) |
| 93 | + writeProgressValue(100) |
| 94 | + info() |
| 95 | + info() |
| 96 | + info(f"Successfully wrote results to $folderPath.") |
| 97 | + } |
| 98 | + |
| 99 | + private def info(value: String = ""):Unit = config.consoleOutput.outputInformation(value) |
| 100 | + private def error(value: String = ""):Unit = config.consoleOutput.outputError(value) |
| 101 | + |
| 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 | + } |
| 106 | + private def buildQueryMetadata(results: List[SearchResult]) = |
| 107 | + QueryStorageMetadata( query = config.query, |
| 108 | + results = results, |
| 109 | + serverVersion = serverVersion, |
| 110 | + serverUrl = config.server, |
| 111 | + clientVersion = BuildInfo.version, |
| 112 | + timestamp = DateTime.now(), |
| 113 | + resultLimit = config.limit.getOrElse(50), |
| 114 | + outputMode = config.outputMode.getOrElse(OutputMode.PomOnly).toString |
| 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 | + ) |
| 127 | +} |
0 commit comments