Unverified Commit 67c501fc authored by Lin Jian's avatar Lin Jian Committed by GitHub
Browse files

Merge pull request #335438 from linj-fork/pr/refactor-emacs-tsc

emacsPackages.tsc: refactor
parents 429ea137 f7fbcb84
Loading
Loading
Loading
Loading
+285 −0
Original line number Diff line number Diff line
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
 "memchr",
]

[[package]]
name = "anyhow"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203"

[[package]]
name = "cc"
version = "1.0.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee"

[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "ctor"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccc0a48a9b826acdf4028595adc9db92caea352f7af011a3034acd172a52a0aa"
dependencies = [
 "quote",
 "syn",
]

[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
dependencies = [
 "darling_core",
 "darling_macro",
]

[[package]]
name = "darling_core"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
dependencies = [
 "fnv",
 "ident_case",
 "proc-macro2",
 "quote",
 "strsim",
 "syn",
]

[[package]]
name = "darling_macro"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
dependencies = [
 "darling_core",
 "quote",
 "syn",
]

[[package]]
name = "emacs"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6797a940189d353de79bec32abe717aeeecd79a08236e84404c888354e040665"
dependencies = [
 "anyhow",
 "ctor",
 "emacs-macros",
 "emacs_module",
 "once_cell",
 "rustc_version",
 "thiserror",
]

[[package]]
name = "emacs-macros"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69656fdfe7c2608b87164964db848b5c3795de7302e3130cce7131552c6be161"
dependencies = [
 "darling",
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "emacs-tree-sitter"
version = "0.18.0"
dependencies = [
 "emacs",
 "libloading",
 "once_cell",
 "tree-sitter",
]

[[package]]
name = "emacs_module"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3067bc974045ed2c6db333bd4fc30d3bdaafa6421a9a889fa7b2826b6f7f2fa"

[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"

[[package]]
name = "libloading"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afe203d669ec979b7128619bae5a63b7b42e9203c1b29146079ee05e2f604b52"
dependencies = [
 "cfg-if",
 "winapi",
]

[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"

[[package]]
name = "once_cell"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"

[[package]]
name = "proc-macro2"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a"
dependencies = [
 "unicode-xid",
]

[[package]]
name = "quote"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
 "aho-corasick",
 "memchr",
 "regex-syntax",
]

[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"

[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
 "semver",
]

[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
 "semver-parser",
]

[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"

[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"

[[package]]
name = "syn"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-xid",
]

[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
 "thiserror-impl",
]

[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "tree-sitter"
version = "0.20.0"
source = "git+https://github.com/ubolonton/tree-sitter?branch=improve-text-provider#475b822f47bdc58d832533448b6f6d9818554f37"
dependencies = [
 "cc",
 "regex",
]

[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"

[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
 "winapi-i686-pc-windows-gnu",
 "winapi-x86_64-pc-windows-gnu",
]

[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"

[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+31 −49
Original line number Diff line number Diff line
{ lib
, symlinkJoin
, melpaBuild
, fetchFromGitHub
, rustPlatform

, runtimeShell
, writeScript
, python3
, nix-prefetch-github
, nix
, stdenv
, nix-update-script
}:

let
  libExt = stdenv.hostPlatform.extensions.sharedLibrary;

  srcMeta = lib.importJSON ./src.json;
  inherit (srcMeta) version;

  src = fetchFromGitHub srcMeta.src;

  tsc = melpaBuild {
    inherit src;
    inherit version;

    pname = "tsc";
  tsc-dyn = rustPlatform.buildRustPackage rec {
    pname = "tsc-dyn";
    version = "0.18.0";

    sourceRoot = "${src.name}/core";
    src = fetchFromGitHub {
      owner = "emacs-tree-sitter";
      repo = "emacs-tree-sitter";
      rev = version;
      hash = "sha256-LrakDpP3ZhRQqz47dPcyoQnu5lROdaNlxGaQfQT6u+k=";
    };

  tsc-dyn = rustPlatform.buildRustPackage {
    inherit version;
    inherit src;

    pname = "tsc-dyn";
    cargoLock = {
      lockFile = ./Cargo.lock;
      outputHashes = {
        "tree-sitter-0.20.0" = "sha256-hGiJZFrQpO+xHXosbEKV2k64e2D8auNGEtdrFk2SsOU=";
      };
    };

    nativeBuildInputs = [ rustPlatform.bindgenHook ];
    sourceRoot = "${src.name}/core";

    postInstall = ''
      LIB=($out/lib/libtsc_dyn.*)
      TSC_PATH=$out/share/emacs/site-lisp/elpa/tsc-${version}
      install -d $TSC_PATH
      install -m444 $out/lib/libtsc_dyn.* $TSC_PATH/''${LIB/*libtsc_/tsc-}
      echo -n $version > $TSC_PATH/DYN-VERSION
      rm -r $out/lib
      pushd $out/lib
      mv --verbose libtsc_dyn${libExt} tsc-dyn${libExt}
      echo -n $version > DYN-VERSION
      popd
    '';

    inherit (srcMeta) cargoHash;
  };
in melpaBuild {
  pname = "tsc";
  inherit (tsc-dyn) version src;

  files = ''("core/*.el" "${tsc-dyn}/lib/*")'';

in symlinkJoin {
  name = "tsc-${version}";
  paths = [ tsc tsc-dyn ];
  ignoreCompilationError = false;

  passthru = {
    updateScript = let
      pythonEnv = python3.withPackages(ps: [ ps.requests ]);
    in writeScript "tsc-update" ''
      #!${runtimeShell}
      set -euo pipefail
      export PATH=${lib.makeBinPath [
        nix-prefetch-github
        nix
        pythonEnv
      ]}:$PATH
      exec python3 ${builtins.toString ./update.py} ${builtins.toString ./.}
    '';
    inherit tsc-dyn;
    updateScript = nix-update-script { attrPath = "emacsPackages.tsc.tsc-dyn"; };
  };

  meta = {
+0 −10
Original line number Diff line number Diff line
{
  "src": {
    "owner": "emacs-tree-sitter",
    "repo": "elisp-tree-sitter",
    "rev": "909717c685ff5a2327fa2ca8fb8a25216129361c",
    "hash": "sha256-LrakDpP3ZhRQqz47dPcyoQnu5lROdaNlxGaQfQT6u+k="
  },
  "version": "0.18.0",
  "cargoHash": "sha256-IRCZqszBkGF8anF/kpcPOzHdOP4lAtJBAp6FS5tAOx8="
}
+0 −123
Original line number Diff line number Diff line
#!/usr/bin/env python3
from textwrap import dedent
from os.path import (
    abspath,
    dirname,
    join,
)
from typing import (
    Dict,
    Any,
)
import subprocess
import tempfile
import json
import sys
import re

import requests


def eval_drv(nixpkgs: str, expr: str) -> Any:
    expr = "\n".join(
        (
            "with (import %s {});" % nixpkgs,
            expr,
        )
    )

    with tempfile.NamedTemporaryFile(mode="w") as f:
        f.write(dedent(expr))
        f.flush()
        p = subprocess.run(
            ["nix-instantiate", "--json", f.name], stdout=subprocess.PIPE, check=True
        )

    return p.stdout.decode().strip()


def get_src(tag_name: str) -> Dict[str, str]:
    p = subprocess.run(
        [
            "nix-prefetch-github",
            "--rev",
            tag_name,
            "--json",
            "emacs-tree-sitter",
            "elisp-tree-sitter",
        ],
        stdout=subprocess.PIPE,
        check=True,
    )
    src = json.loads(p.stdout)

    fields = ["owner", "repo", "rev", "hash"]

    return {f: src[f] for f in fields}


def get_cargo_hash(drv_path: str):
    # Note: No check=True since we expect this command to fail
    p = subprocess.run(["nix-store", "-r", drv_path], stderr=subprocess.PIPE)

    stderr = p.stderr.decode()
    lines = iter(stderr.split("\n"))

    for l in lines:
        if l.startswith("error: hash mismatch in fixed-output derivation"):
            break
    else:
        raise ValueError("Did not find expected hash mismatch message")

    for l in lines:
        m = re.match(r"\s+got:\s+(.+)$", l)
        if m:
            return m.group(1)

    raise ValueError("Could not extract actual hash: ", stderr)


if __name__ == "__main__":
    cwd = sys.argv[1]

    # This should point to the root default.nix of Nixpkgs tree
    nixpkgs = abspath(join(cwd, "../../../../../../.."))

    tag_name = requests.get(
        "https://api.github.com/repos/emacs-tree-sitter/elisp-tree-sitter/releases/latest"
    ).json()["tag_name"]

    src = get_src(tag_name)

    with tempfile.NamedTemporaryFile(mode="w") as f:
        json.dump(src, f)
        f.flush()

        drv_path = eval_drv(
            nixpkgs,
            """
        rustPlatform.buildRustPackage rec {
          pname = "tsc-dyn";
          version = "%s";
          nativeBuildInputs = [ clang ];
          src = fetchFromGitHub (lib.importJSON %s);
          sourceRoot = "${src.name}/core";
          cargoHash = lib.fakeHash;
        }
        """
            % (tag_name, f.name),
        )

    cargo_hash = get_cargo_hash(drv_path)

    with open(join(cwd, "src.json"), mode="w") as f:
        json.dump(
            {
                "src": src,
                "version": tag_name,
                "cargoHash": cargo_hash,
            },
            f,
            indent=2,
        )
        f.write("\n")