Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request #33 from Ma27/refactoring/#27/get-rid-of-nix-depen…
Browse files Browse the repository at this point in the history
…dency-for-hash-computations

Generate CheckSum using `java.security.MessageDigest`
  • Loading branch information
nightkr authored May 5, 2018
2 parents 709e2a6 + 762e1fb commit 07baff0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion plugin/nix-exprs/sbtix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
'';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
52 changes: 34 additions & 18 deletions plugin/src/main/scala/se/nullable/sbtix/FindArtifacts.scala
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
}
}

Expand Down
4 changes: 1 addition & 3 deletions sbtix-tool.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ callPackage, writeText, writeScriptBin, stdenv, sbt, nix-prefetch-scripts }:
{ callPackage, writeText, writeScriptBin, stdenv, sbt }:
let
version = "0.2";
versionSnapshotSuffix = "-SNAPSHOT";
Expand Down Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions tests/template-generation/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -euxo pipefail

cd "$(dirname "$0")"
for f in {,project/}repo.nix; do
if [[ -e "$f" ]]; then
rm "$f"
Expand Down

0 comments on commit 07baff0

Please sign in to comment.