2019-06-04 16:50:29 +00:00
#!/usr/bin/env bash
#
# This script takes a maven-inputs.txt file and builds a Nix expression that can be used by maven-repo-builder.nix to produce a path to a local Maven repository
#
function getSHA( ) {
nix-prefetch-url --type sha256 " $1 " 2> /dev/null
}
_tmp = $( mktemp)
sort $1 | uniq > $_tmp
trap " rm $_tmp " EXIT INT
apacheUrl = 'https://repo.maven.apache.org/maven2'
clojarsUrl = 'https://repo.clojars.org'
fabricUrl = 'https://maven.fabric.io/public'
googleUrl = 'https://dl.google.com/dl/android/maven2'
gradleUrl = 'http://repo.gradle.org/gradle/libs-releases-local'
gradlePluginsUrl = 'https://plugins.gradle.org/m2'
javaUrl = 'https://maven.java.net/content/repositories/releases'
jcenterUrl = 'https://jcenter.bintray.com'
jitpackUrl = 'https://jitpack.io'
mavenUrl = 'https://repo1.maven.org/maven2'
sonatypeSnapshotsUrl = 'https://oss.sonatype.org/content/repositories/snapshots'
sonatypePublicGridUrl = 'https://repository.sonatype.org/content/groups/sonatype-public-grid'
# Writes Nix attribute set describing a package (represented by its URL)
function writeEntry( ) {
local depurl = $1
if [ [ " $depurl " = " $apacheUrl " * ] ] ; then
host = 'apache'
prefix = $apacheUrl
elif [ [ " $depurl " = " $clojarsUrl " * ] ] ; then
host = 'clojars'
prefix = $clojarsUrl
elif [ [ " $depurl " = " $fabricUrl " * ] ] ; then
host = 'fabric-io'
prefix = $fabricUrl
elif [ [ " $depurl " = " $googleUrl " * ] ] ; then
host = 'google'
prefix = $googleUrl
elif [ [ " $depurl " = " $gradleUrl " * ] ] ; then
host = 'gradle'
prefix = $gradleUrl
elif [ [ " $depurl " = " $gradlePluginsUrl " * ] ] ; then
host = 'gradlePlugins'
prefix = $gradlePluginsUrl
elif [ [ " $depurl " = " $jcenterUrl " * ] ] ; then
host = 'jcenter'
prefix = $jcenterUrl
elif [ [ " $depurl " = " $jitpackUrl " * ] ] ; then
host = 'jitpack'
prefix = $jitpackUrl
elif [ [ " $depurl " = " $javaUrl " * ] ] ; then
host = 'java'
prefix = $javaUrl
elif [ [ " $depurl " = " $mavenUrl " * ] ] ; then
host = 'maven'
prefix = $mavenUrl
elif [ [ " $depurl " = " $sonatypeSnapshotsUrl " * ] ] ; then
host = 'sonatypeSnapshots'
prefix = $sonatypeSnapshotsUrl
elif [ [ " $depurl " = " $sonatypePublicGridUrl " * ] ] ; then
host = 'sonatypePublicGrid'
prefix = $sonatypePublicGridUrl
else
echo " Unknown host in $depurl , exiting. "
exit 1
fi
deppath = " ${ depurl / $prefix \/ / } "
pom_sha256 = $( getSHA " $depurl .pom " )
[ -n " $pom_sha256 " ] && pom_sha1 = $( curl -s " $depurl .pom.sha1 " ) || unset pom_sha1
jar_sha256 = $( getSHA " $depurl .jar " )
type = 'jar'
if [ -z " $jar_sha256 " ] ; then
jar_sha256 = $( getSHA " $depurl .aar " )
[ -n " $jar_sha256 " ] && type = 'aar'
fi
[ -n " $jar_sha256 " ] && jar_sha1 = $( curl -s " $depurl . ${ type } .sha1 " ) || unset jar_sha1
if [ -z " $pom_sha256 " ] && [ -z " $jar_sha256 " ] && [ -z " $aar_sha256 " ] ; then
echo " Warning: failed to download $depurl " > /dev/stderr
echo "Exiting." > /dev/stderr
exit 1
fi
2019-07-29 08:33:35 +00:00
echo -n " \" $depurl \" =
{
2019-06-04 16:50:29 +00:00
host = repositories.$host ;
2019-07-29 08:33:35 +00:00
path =
\" $deppath \" ;
2019-06-04 16:50:29 +00:00
type = \" $type \" ; "
if [ -n " $pom_sha256 " ] ; then
echo -n "
pom = {
sha1 = \" $pom_sha1 \" ;
sha256 = \" $pom_sha256 \" ;
} ; "
fi
if [ -n " $jar_sha256 " ] ; then
echo -n "
jar = {
sha1 = \" $jar_sha1 \" ;
sha256 = \" $jar_sha256 \" ;
} ; "
fi
echo "
} ; "
}
lineCount = $( wc -l $1 | cut -f 1 -d ' ' )
currentLine = 0
pstr = "[=======================================================================]"
echo " # Auto-generated by $( realpath --relative-to= " $( dirname $1 ) " ${ BASH_SOURCE } )
2019-07-29 08:33:35 +00:00
{ } :
2019-06-04 16:50:29 +00:00
let
repositories = {
apache = \" $mavenUrl \" ;
clojars = \" $clojarsUrl \" ;
fabric-io = \" $fabricUrl \" ;
google = \" $googleUrl \" ;
gradle = \" $gradleUrl \" ;
gradlePlugins = \" $gradlePluginsUrl \" ;
java = \" $javaUrl \" ;
jcenter = \" $jcenterUrl \" ;
jitpack = \" $jitpackUrl \" ;
maven = \" $mavenUrl \" ;
2019-07-29 08:33:35 +00:00
sonatypeSnapshots =
\" $sonatypeSnapshotsUrl \" ;
sonatypePublicGrid =
\" $sonatypePublicGridUrl \" ;
2019-06-04 16:50:29 +00:00
} ;
in { "
echo " Generating Nix file from $1 ... " > /dev/stderr
while read depurl
do
currentLine = $(( $currentLine + 1 ))
pd = $(( $currentLine * 73 / $lineCount ))
printf " \r%3d.%1d%% %. ${ pd } s " $(( $currentLine * 100 / $lineCount )) $(( ( $currentLine * 1000 / $lineCount ) % 10 )) $pstr > /dev/stderr
if [ -z " $depurl " ] ; then
continue
fi
writeEntry $depurl
# com.android.tools.build:aapt2 package also includes a platform-specific package, so we should add that too
if [ [ " $depurl " = *'com/android/tools/build/aapt2/' * ] ] ; then
writeEntry " $depurl -linux "
writeEntry " $depurl -osx "
fi
done < $_tmp
echo "}"