diff --git a/plugin/nix-exprs/sbtix.nix b/plugin/nix-exprs/sbtix.nix index c286d12..8f9fed2 100644 --- a/plugin/nix-exprs/sbtix.nix +++ b/plugin/nix-exprs/sbtix.nix @@ -171,7 +171,7 @@ in rec { sbt stage mkdir -p $out/ cp target/universal/stage/* $out/ -r - for p in $out/bin/*; do + for p in $(find $out/bin/* -executable); do wrapProgram "$p" --prefix PATH : ${jre}/bin done ''; diff --git a/plugin/src/main/scala/se/nullable/sbtix/CoursierArtifactFetcher.scala b/plugin/src/main/scala/se/nullable/sbtix/CoursierArtifactFetcher.scala index f7309b3..9ebc7d8 100644 --- a/plugin/src/main/scala/se/nullable/sbtix/CoursierArtifactFetcher.scala +++ b/plugin/src/main/scala/se/nullable/sbtix/CoursierArtifactFetcher.scala @@ -185,7 +185,7 @@ class CoursierArtifactFetcher(logger: Logger, resolvers: Set[Resolver], credenti //only collect the http and https urls if (artifact.url.startsWith("http")) { //reduce the number of tried and failed metaArtifacts by checking if Coursier succeeded in its download - val checkSum = FindArtifactsOfRepo.fetchChecksum(artifact.url, "-Meta- Artifact",f.toURI().toURL()) + val checkSum = FindArtifactsOfRepo.fetchChecksum(artifact.url, "-Meta- Artifact",f.toURI().toURL()).get // TODO this might be expressed in a monad metaArtifactCollector.add(MetaArtifact(artifact.url,checkSum)) } } diff --git a/plugin/src/main/scala/se/nullable/sbtix/FindArtifacts.scala b/plugin/src/main/scala/se/nullable/sbtix/FindArtifacts.scala index 994e744..4ccf4e0 100644 --- a/plugin/src/main/scala/se/nullable/sbtix/FindArtifacts.scala +++ b/plugin/src/main/scala/se/nullable/sbtix/FindArtifacts.scala @@ -1,40 +1,56 @@ package se.nullable.sbtix +import java.io.{File, FileOutputStream, IOException} import java.net.URL +import java.nio.file.Files +import java.security.MessageDigest import java.util.concurrent.Semaphore import sbt.Logger -import scala.sys.process._ +import scala.util.{Failure, Success, Try} object FindArtifactsOfRepo { private val semaphore = new Semaphore(4, false) - def fetchChecksum(originalUrl: String, artifactType: String, url: URL): String = { - - val procLogger = sys.process.ProcessLogger { - mess => - if (mess.startsWith("path is")) { - val modMess = mess.replace("path is ", "") - println(s"$artifactType ${originalUrl} => $modMess") - } else if (mess.startsWith("error: unable to download")) { - System.err.println(mess) - } else { - System.err.println(mess) - } - } - + def fetchChecksum(originalUrl: String, artifactType: String, url: URL): Try[String] = { semaphore.acquireUninterruptibly() - val checksum = Seq("nix-prefetch-url", url.toString, "--type", "sha256").!!(procLogger).trim() + val checksum = calculateChecksum(url) semaphore.release() + checksum } + + private def calculateChecksum(url: URL): Try[String] = { + val tmpFile = File.createTempFile(s"sbtix-${url.toString}", ".tmp") + val path = tmpFile.toPath + + try { + val hash = MessageDigest getInstance "SHA-256" + val input = url.openConnection.getInputStream + val w = new FileOutputStream(tmpFile) + + Stream.continually(input.read).takeWhile(_ != -1).foreach(w.write) + + hash update (Files readAllBytes path) + input.close() + + Files.delete(path) + + Success { + hash.digest() map { "%02X" format _ } mkString + } + } catch { case e: IOException => + Files.delete(path) + Failure(e) + } + } } class FindArtifactsOfRepo(repoName: String, root: String) { def findArtifacts(logger: Logger, modules: Set[GenericModule]): Set[NixArtifact] = modules.flatMap { ga => - val rootUrl = new URL(root); + val rootUrl = new URL(root) val authedRootURI = ga.authed(rootUrl) //authenticated version of the rootUrl @@ -50,7 +66,7 @@ class FindArtifactsOfRepo(repoName: String, root: String) { repoName, calcUrl.toString.replace(authedRootURI.toString, "").stripPrefix("/"), calcUrl.toString, - FindArtifactsOfRepo.fetchChecksum(calcUrl.toString, "Artifact", artifactLocalFile.toURI.toURL)) + FindArtifactsOfRepo.fetchChecksum(calcUrl.toString, "Artifact", artifactLocalFile.toURI.toURL).get) } } diff --git a/sbtix-tool.nix b/sbtix-tool.nix index acc010e..641b427 100644 --- a/sbtix-tool.nix +++ b/sbtix-tool.nix @@ -1,4 +1,4 @@ -{ callPackage, writeText, writeScriptBin, stdenv, sbt, nix-prefetch-scripts }: +{ callPackage, writeText, writeScriptBin, stdenv, sbt }: let version = "0.2"; versionSnapshotSuffix = "-SNAPSHOT"; @@ -30,8 +30,6 @@ let sbtixScript = writeScriptBin "sbtix" '' #! ${stdenv.shell} - export PATH=${nix-prefetch-scripts}/bin:$PATH - # remove the ivy cache of sbtix so sbt retrieves from the sbtix nix repo. # without this your version of sbtix may be overriden by the local ivy cache. echo "Deleting any cached sbtix plugins in '~/.ivy'. So the most recent version from nix is used." diff --git a/tests/template-generation/run.sh b/tests/template-generation/run.sh index 0b39b1d..b334fa4 100755 --- a/tests/template-generation/run.sh +++ b/tests/template-generation/run.sh @@ -2,6 +2,7 @@ set -euxo pipefail +cd "$(dirname "$0")" for f in {,project/}repo.nix; do if [[ -e "$f" ]]; then rm "$f"