Commit f64b99e4 authored by Alexander Bantyev's avatar Alexander Bantyev Committed by Silvan Mosberger
Browse files

simple-binary-encoding: init at 1.36.0

SBE is an OSI layer 6 presentation for encoding and decoding binary
application messages for low-latency financial applications.
parent 34d43f04
Loading
Loading
Loading
Loading
+580 −0

File added.

Preview size limit exceeded, changes collapsed.

+132 −0
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  fetchFromGitHub,
  gradle,
  jre,
  runtimeShell,

  # Generating the usage instructions for script
  htmlq,
  html2text,
  txt2man,
}:
stdenv.mkDerivation (finalAttrs: {
  pname = "simple-binary-encoding";
  version = "1.36.0";

  src = fetchFromGitHub {
    owner = "aeron-io";
    repo = "simple-binary-encoding";
    tag = finalAttrs.version;
    hash = "sha256-xqfsJq86qIFBG1542GDWNP0ayGaOzfIZ1KR0s0m8vf0=";
  };

  nativeBuildInputs = [
    gradle
    htmlq
    html2text
    txt2man
  ];

  mitmCache = gradle.fetchDeps {
    pname = "simple-binary-encoding";
    pkg = finalAttrs.finalPackage;
    data = ./deps.json;
  };

  gradleBuildTask = "build";

  doCheck = true;
  gradleCheckTask = "runJavaExamples";

  outputs = [
    "out"
    "benchmarks"
    "samples"
    "tool"
  ];

  installPhase = ''
    runHook preInstall

    for output in $outputs; do
      if [[ "$output" != "out" ]]; then
        mkdir -p "''${!output}"/share/sbe
        cp "sbe-$output"/build/libs/*.jar "''${!output}/share/sbe"
        if [[ -d "sbe-$output"/build/docs/javadoc ]]; then
          cp -r "sbe-$output"/build/docs/javadoc "''${!output}/share/javadoc"
        fi
      fi
      if [[ "$output" == "tool" ]]; then
        cp sbe-all/build/libs/*.jar "$tool/share/sbe"
      fi
    done

    mkdir -p "$out/"{bin,share/doc,share/man}

    # Generate the usage file from the javadoc
    shopt -s globstar
    cat "$tool/share/javadoc"/**/sbe/SbeTool.html \
      | htmlq '.class-description .block' \
      | html2text \
      | sed -e 's/[$] java/$ sbetool/' -e 's/ -jar sbe.jar//' -e 's/System Properties:/Available Options:/' \
      > "$out/share/doc/sbetool.txt"

    echo >> "$out/share/doc/sbetool.txt"
    echo "All other arguments will be interpreted by java(1)." >> "$out/share/doc/sbetool.txt"

    # Generate a manpage
    sed \
      -e '1i\NAME\nsbetool - ' \
      -e 's/Usage:/\nSYNOPSIS\n/' \
      -e 's/Available Options:/\nOPTIONS\n/' \
      < "$out/share/doc/sbetool.txt" \
      | txt2man \
        -P simple-binary-encoding \
        -t "sbetool" -s1 \
        -r "$pname $verison" \
        -v "Simple Binary Encoding" \
        > "$out/share/man/sbetool.1"

    runtimeShell="${runtimeShell}" java="${lib.getExe jre}" \
      substituteAll ${./sbetool.sh} "$out/bin/sbetool"

    chmod +x "$out/bin/sbetool"

    runHook postInstall
  '';

  doInstallCheck = true;
  installCheckPhase = ''
    # Check that we have some usage documentation
    "$out"/bin/sbetool --help 2>&1 | grep -q Usage

    # Check that the script can pass arguments to the underlying jar
    tmpdir="$(mktemp -d)"
    "$out"/bin/sbetool \
      sbe-tool/src/test/resources/example-schema.xml \
      -Dsbe.target.language=golang \
      -Dsbe.output.dir="$tmpdir"
    find "$tmpdir/baseline" | grep -q '.*[.]go'

    # Check that -- passes as files
    tmpdir="$(mktemp -d)"
    cp sbe-tool/src/test/resources/example-schema.xml -- -e.xml
    "$out"/bin/sbetool \
      -Dsbe.target.language=golang \
      -Dsbe.output.dir="$tmpdir" \
      -- \
      -e.xml
    find "$tmpdir/baseline" | grep -q '.*[.]go'
  '';

  passthru.updateScript = ./update.sh;

  meta = {
    homepage = "https://github.com/aeron-io/simple-binary-encoding";
    license = lib.licenses.asl20;
    maintainers = with lib.maintainers; [ balsoft ];
    description = "OSI layer 6 presentation for encoding and decoding binary application messages for low-latency financial applications";
  };
})
+51 −0
Original line number Diff line number Diff line
#!@runtimeShell@

set -euo pipefail

usage() {
  cat "@out@/share/doc/sbetool.txt" >&2
}

if [[ $# -lt 1 ]]; then
  usage
  exit 1
fi

files=()
opts=()
opt_regex='^-.+$'
only_files=

for arg in "$@"; do
  if [[ -n $only_files ]] || ! [[ "$arg" =~ $opt_regex ]]; then
    files+=("$arg")
    continue
  fi
  if [[ "$arg" == "-h" ]] || [[ "$arg" == "--help" ]]; then
    usage
    exit 0
  fi
  if [[ "$arg" == "--" ]]; then
    only_files=true
    continue
  fi
  opts+=("$arg")
done

if [[ -z "${files[*]}" ]]; then
  echo "Error: no files provided. If your file starts with -, pass it after -- like this:" >&2
  echo "    sbetool -- -my-file.xml" >&2
  echo >&2
  usage 2>&1 | tail +2 1>&2
  exit 1
fi

# NB: `--add-opens` flag is added because newer version of `agrona` (a utility
# lib used in `sbetool`) depends on jdk.internal.misc.Unsafe
#
# The Unsafe API is deprecated and requires special opt-in to use.
@java@ \
  --add-opens java.base/jdk.internal.misc=ALL-UNNAMED \
  "${opts[@]}" \
  -jar "@tool@/share/sbe/sbe-all-@version@.jar" \
  "${files[@]}"
+24 −0
Original line number Diff line number Diff line
#!/usr/bin/env nix-shell
#!nix-shell -I nixpkgs=./. -i bash -p bash nix curl coreutils jq common-updater-scripts

set -eou pipefail

latest=$(curl ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} -sL https://api.github.com/repos/aeron-io/simple-binary-encoding/releases/latest)
latestTag=$(echo "$latest" | jq -r ".tag_name")
latestVersion="$latestTag"

currentVersion=$(nix-instantiate --eval -E "with import ./. {}; simple-binary-encoding.version" | tr -d '"')

echo "latest  version: $latestVersion"
echo "current version: $currentVersion"

if [[ "$latestVersion" == "$currentVersion" ]]; then
    echo "package is up-to-date"
    exit 0
fi

hash=$(nix --extra-experimental-features nix-command hash convert --hash-algo sha256 --to sri "$(nix-prefetch-url --unpack "https://github.com/aeron-io/simple-binary-encoding/archive/refs/tags/${latestTag}.tar.gz")")
update-source-version simple-binary-encoding "$latestVersion" "$hash"

"$(nix-build -A simple-binary-encoding.mitmCache.updateScript)"