2021-02-20 09:12:21 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
2021-09-03 14:50:36 +00:00
|
|
|
#!nix-shell update-shell.nix -i python3
|
|
|
|
|
2021-02-20 09:12:21 +00:00
|
|
|
|
|
|
|
# format:
|
|
|
|
# $ nix run nixpkgs.python3Packages.black -c black update.py
|
|
|
|
# type-check:
|
|
|
|
# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py
|
|
|
|
# linted:
|
|
|
|
# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py
|
|
|
|
|
2021-08-31 11:36:11 +00:00
|
|
|
# If you see `HTTP Error 429: too many requests` errors while running this script,
|
|
|
|
# refer to:
|
|
|
|
#
|
|
|
|
# https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md#updating-plugins-in-nixpkgs-updating-plugins-in-nixpkgs
|
2022-02-17 06:56:44 +00:00
|
|
|
#
|
|
|
|
# (or the equivalent file /doc/languages-frameworks/vim.section.md from Nixpkgs master tree).
|
|
|
|
#
|
2021-08-31 11:36:11 +00:00
|
|
|
|
2021-02-20 09:12:21 +00:00
|
|
|
import inspect
|
|
|
|
import os
|
|
|
|
import sys
|
2021-07-30 00:25:15 +00:00
|
|
|
import logging
|
|
|
|
import textwrap
|
2021-02-20 09:12:21 +00:00
|
|
|
from typing import List, Tuple
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
log = logging.getLogger()
|
2022-05-01 22:58:41 +00:00
|
|
|
|
|
|
|
sh = logging.StreamHandler()
|
|
|
|
formatter = logging.Formatter('%(name)s:%(levelname)s: %(message)s')
|
|
|
|
sh.setFormatter(formatter)
|
|
|
|
log.addHandler(sh)
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2021-02-20 09:12:21 +00:00
|
|
|
# Import plugin update library from maintainers/scripts/pluginupdate.py
|
|
|
|
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
|
2022-02-17 06:56:44 +00:00
|
|
|
# Ideally, ROOT.(parent^5) points to root of Nixpkgs official tree
|
|
|
|
sys.path.insert(0, os.path.join(ROOT.parent.parent.parent.parent.parent, "maintainers", "scripts"))
|
2021-02-20 09:12:21 +00:00
|
|
|
import pluginupdate
|
2022-05-01 22:58:41 +00:00
|
|
|
from pluginupdate import run_nix_expr, PluginDesc
|
2021-02-20 09:12:21 +00:00
|
|
|
|
|
|
|
GET_PLUGINS = f"""(with import <localpkgs> {{}};
|
|
|
|
let
|
2022-05-01 22:58:41 +00:00
|
|
|
inherit (vimUtils.override {{inherit vim;}}) buildNeovimPluginFrom2Nix buildVimPluginFrom2Nix;
|
2021-02-20 09:12:21 +00:00
|
|
|
generated = callPackage {ROOT}/generated.nix {{
|
2022-05-01 22:58:41 +00:00
|
|
|
inherit buildNeovimPluginFrom2Nix buildVimPluginFrom2Nix;
|
2021-02-20 09:12:21 +00:00
|
|
|
}};
|
|
|
|
hasChecksum = value: lib.isAttrs value && lib.hasAttrByPath ["src" "outputHash"] value;
|
|
|
|
getChecksum = name: value:
|
|
|
|
if hasChecksum value then {{
|
|
|
|
submodules = value.src.fetchSubmodules or false;
|
|
|
|
sha256 = value.src.outputHash;
|
|
|
|
rev = value.src.rev;
|
|
|
|
}} else null;
|
|
|
|
checksums = lib.mapAttrs getChecksum generated;
|
|
|
|
in lib.filterAttrs (n: v: v != null) checksums)"""
|
|
|
|
|
2022-05-01 22:58:41 +00:00
|
|
|
GET_PLUGINS_LUA = """
|
|
|
|
with import <localpkgs> {};
|
|
|
|
lib.attrNames lua51Packages"""
|
|
|
|
|
2021-02-20 09:12:21 +00:00
|
|
|
HEADER = (
|
2022-02-16 14:55:43 +00:00
|
|
|
"# This file has been generated by ./pkgs/applications/editors/vim/plugins/update.py. Do not edit!"
|
2021-02-20 09:12:21 +00:00
|
|
|
)
|
|
|
|
|
2022-05-01 22:58:41 +00:00
|
|
|
def isNeovimPlugin(plug: pluginupdate.Plugin) -> bool:
|
|
|
|
'''
|
|
|
|
Whether it's a neovim-only plugin
|
|
|
|
We can check if it's available in lua packages
|
|
|
|
'''
|
|
|
|
global luaPlugins
|
|
|
|
if plug.normalized_name in luaPlugins:
|
|
|
|
log.debug("%s is a neovim plugin", plug)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-02-20 09:12:21 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
class VimEditor(pluginupdate.Editor):
|
2022-05-01 22:58:41 +00:00
|
|
|
def generate_nix(self, plugins: List[Tuple[PluginDesc, pluginupdate.Plugin]], outfile: str):
|
2022-01-10 23:20:52 +00:00
|
|
|
sorted_plugins = sorted(plugins, key=lambda v: v[0].name.lower())
|
2021-07-30 00:25:15 +00:00
|
|
|
|
|
|
|
with open(outfile, "w+") as f:
|
|
|
|
f.write(HEADER)
|
|
|
|
f.write(textwrap.dedent("""
|
2022-05-01 22:58:41 +00:00
|
|
|
{ lib, buildVimPluginFrom2Nix, buildNeovimPluginFrom2Nix, fetchFromGitHub, fetchgit }:
|
2021-07-30 00:25:15 +00:00
|
|
|
|
|
|
|
final: prev:
|
2022-05-01 22:58:41 +00:00
|
|
|
{
|
|
|
|
"""
|
2021-07-30 00:25:15 +00:00
|
|
|
))
|
2022-01-10 23:20:52 +00:00
|
|
|
for pdesc, plugin in sorted_plugins:
|
2022-05-01 22:58:41 +00:00
|
|
|
content = self.plugin2nix(pdesc, plugin)
|
|
|
|
f.write(content)
|
|
|
|
f.write("\n}\n")
|
|
|
|
print(f"updated {outfile}")
|
|
|
|
|
|
|
|
def plugin2nix(self, pdesc: PluginDesc, plugin: pluginupdate.Plugin) -> str:
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2022-05-01 22:58:41 +00:00
|
|
|
repo = pdesc.repo
|
|
|
|
isNeovim = isNeovimPlugin(plugin)
|
|
|
|
|
|
|
|
content = f" {plugin.normalized_name} = "
|
|
|
|
src_nix = repo.as_nix(plugin)
|
|
|
|
content += """{buildFn} {{
|
2021-08-30 00:05:12 +00:00
|
|
|
pname = "{plugin.name}";
|
2021-08-14 16:19:00 +00:00
|
|
|
version = "{plugin.version}";
|
2022-01-10 23:20:52 +00:00
|
|
|
src = {src_nix};
|
|
|
|
meta.homepage = "{repo.uri}";
|
2021-08-14 16:19:00 +00:00
|
|
|
}};
|
2021-02-20 09:12:21 +00:00
|
|
|
|
2022-05-01 22:58:41 +00:00
|
|
|
""".format(
|
|
|
|
buildFn="buildNeovimPluginFrom2Nix" if isNeovim else "buildVimPluginFrom2Nix", plugin=plugin, src_nix=src_nix, repo=repo)
|
|
|
|
print(content)
|
|
|
|
return content
|
2021-02-20 09:12:21 +00:00
|
|
|
|
|
|
|
def main():
|
2022-05-01 22:58:41 +00:00
|
|
|
|
|
|
|
global luaPlugins
|
2022-06-22 01:35:55 +00:00
|
|
|
luaPlugins = run_nix_expr(GET_PLUGINS_LUA)
|
2022-05-01 22:58:41 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
editor = VimEditor("vim", ROOT, GET_PLUGINS)
|
|
|
|
parser = editor.create_parser()
|
|
|
|
args = parser.parse_args()
|
|
|
|
pluginupdate.update_plugins(editor, args)
|
2021-02-20 09:12:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|