haskell: add translator hackage

This commit is contained in:
DavHau 2022-09-27 15:47:38 +02:00
parent 12ae9b891b
commit 2f7f3c1289
8 changed files with 166 additions and 11 deletions

View File

@ -55,6 +55,9 @@ libraries.io also supports other interesting popularity metrics:
# get platform
platformQuery=$(jq ".\"$platform\"" -c -r ${l.toFile "platform-map.json" (l.toJSON platformMap)})
if [ "$platformQuery" == "null" ]; then
platformQuery=$platform
fi
echo "Starting to query $numPages pages..."

View File

@ -10,14 +10,15 @@ input = json.load(sys.stdin)
projects = []
for package in input:
versions = package['versions']
latest_version =\
(sorted(versions, key=lambda v: v['published_at'])[-1])['number']
projects.append(dict(
id=f"{package['name']}-{latest_version}",
name=package['name'],
version=latest_version,
translator=platform,
))
versions = sorted(versions, key=lambda v: v['published_at'], reverse=True)
if versions:
latest_version = versions[0]['number']
projects.append(dict(
id=f"{package['name']}-{latest_version}",
name=package['name'],
version=latest_version,
translator=platform,
))
with open(out_file) as f:
existing_projects = json.load(f)

View File

@ -3,6 +3,14 @@
"compiler": {
"name": "ghc",
"version": "8.10.7"
},
"cabal-files": {
"aeson": {
"1.2.2.0": {
"url": "https://hackage.haskell.org/package/aeson-1.2.2.0/revision/2.cabal",
"hash": "sha256:57a8f61df46d5ac6aeb33f01a26267aaf853c82ac9d99dda0f8f3c93a9f740de"
}
}
}
}
}

View File

@ -0,0 +1,126 @@
{
dlib,
lib,
...
}: let
l = lib // builtins;
in {
type = "impure";
# A derivation which outputs a single executable at `$out`.
# The executable will be called by dream2nix for translation
# The input format is specified in /specifications/translator-call-example.json.
# The first arg `$1` will be a json file containing the input parameters
# like defined in /src/specifications/translator-call-example.json and the
# additional arguments required according to extraArgs
#
# The program is expected to create a file at the location specified
# by the input parameter `outFile`.
# The output file must contain the dream lock data encoded as json.
# See /src/specifications/dream-lock-example.json
translateBin = {
# dream2nix utils
subsystems,
utils,
# nixpkgs dependenies
bash,
coreutils,
curl,
gnutar,
gzip,
haskellPackages,
jq,
moreutils,
nix,
python3,
writeScriptBin,
...
}:
utils.writePureShellScript
[
bash
coreutils
curl
gnutar
gzip
haskellPackages.cabal-install
haskellPackages.ghc
jq
moreutils
nix
python3
]
''
# accroding to the spec, the translator reads the input from a json file
jsonInput=$1
# read the json input
outputFile=$(realpath -m $(jq '.outputFile' -c -r $jsonInput))
name=$(jq '.project.name' -c -r $jsonInput)
version=$(jq '.project.version' -c -r $jsonInput)
source=$(jq '.source' -c -r $jsonInput)
relPath=$(jq '.project.relPath' -c -r $jsonInput)
pushd $TMPDIR
# download and unpack package source
mkdir source
curl -L https://hackage.haskell.org/package/$name-$version/$name-$version.tar.gz > $TMPDIR/tarball
cd source
cat $TMPDIR/tarball | tar xz --strip-components 1
# trigger creation of `dist-newstyle` directory
cabal update
cabal freeze
cd -
# generate arguments for cabal-plan translator
echo "{
\"source\": \"$TMPDIR/source\",
\"outputFile\": \"$outputFile\",
\"project\": {
\"relPath\": \"\",
\"subsystemInfo\": {}
}
}" > $TMPDIR/newJsonInput
popd
# execute cabal-plan translator
${subsystems.haskell.translators.cabal-plan.translateBin} $TMPDIR/newJsonInput
# finalize dream-lock. Add source and export default package
# set correct package version under `packages`
export version
export hash=$(sha256sum $TMPDIR/tarball | cut -d " " -f 1)
cat $outputFile \
| python3 ${./fixup-dream-lock.py} $TMPDIR/sourceInfo.json \
| sponge $outputFile
'';
# If the translator requires additional arguments, specify them here.
# When users run the CLI, they will be asked to specify these arguments.
# There are only two types of arguments:
# - string argument (type = "argument")
# - boolean flag (type = "flag")
# String arguments contain a default value and examples. Flags do not.
extraArgs = {
# Example: boolean option
# Flags always default to 'false' if not specified by the user
noDev = {
description = "Exclude dev dependencies";
type = "flag";
};
# Example: string option
theAnswer = {
default = "42";
description = "The Answer to the Ultimate Question of Life";
examples = [
"0"
"1234"
];
type = "argument";
};
};
}

View File

@ -0,0 +1,17 @@
import json
import os
import sys
lock = json.load(sys.stdin)
version = os.environ.get('version')
hash = os.environ.get('hash')
# set default package version correctly
name = lock['_generic']['defaultPackage']
lock['sources'][name][version] = dict(
type="http",
url=f"https://hackage.haskell.org/package/{name}-{version}/{name}-{version}.tar.gz",
hash=f"sha256:{hash}",
)
print(json.dumps(lock, indent=2))

View File

@ -71,7 +71,7 @@
# set correct package version under `packages`
cat $outputFile \
| python3 ${./fixup-dream-lock.py} $TMPDIR/sourceInfo.json \
| python3 ${./fixup-dream-lock.py} \
| sponge $outputFile
'';

View File

@ -43,9 +43,9 @@
version=$(jq '.project.version' -c -r $jsonInput)
pushd $TMPDIR
mkdir source
# download and unpack package source
mkdir source
curl -L https://crates.io/api/v1/crates/$name/$version/download > $TMPDIR/tarball
cd source
cat $TMPDIR/tarball | tar xz --strip-components 1

View File

@ -67,7 +67,7 @@ in {
source=$(jq '.source' -c -r $jsonInput)
relPath=$(jq '.project.relPath' -c -r $jsonInput)
cd $TMPDIR
pushd $TMPDIR
# TODO:
# read input files/dirs and produce a json file at $outputFile
# containing the dream lock similar to /src/specifications/dream-lock-example.json