aboutsummaryrefslogtreecommitdiff
path: root/bin/vim-pack
blob: 25bc16543c0c4b35f6164b8d17034ecf63fd5c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python

import os
import glob
import asyncio
import subprocess

INTSTALL_DIR=os.path.expandvars("$HOME/.vim/pack/vendor")

class Package(object):
    def __init__(self, name, location, type='opt', posthook=None):
        self.name = name
        self.location = location
        self.type = type
        self.posthook = posthook
        self.path = self.get_path()

    def fail(self):
        print("%s : FAIL" % self.name)

    def get_path(self):
        return os.path.expandvars("$HOME/.vim/pack/vendor/%s/%s" % (self.type, self.name))

    async def update(self):
        cmd = "cd %s && git pull" % self.path
        print("%s : Updating ..." % self.name)

        if not os.path.exists(self.path):
            os.mkdir(self.path)
            cmd = "git clone --depth 1 %s %s" % (self.location, self.path)
        if not os.path.isdir(self.path):
            print("ERROR: package directory error: %s" % self.name)
            self.fail()
            return

        process = await asyncio.create_subprocess_shell(
            cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
        )

        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            print("ERROR: git command error")
            print(stdout)
            print(stderr)
            self.fail()
            return

        if self.posthook:
            self.posthook()

        print("%s : Done" % (self.name))

        return ""

packages = [
    Package(
        "nnn.vim", 
        "[email protected]:mcchrish/nnn.vim.git",
        "opt",
    ),
]

def update_packages(packages): 
    tasks = [ package.update() for package in packages ]
    loop = asyncio.get_event_loop()
    commands = asyncio.gather(*tasks)
    result = loop.run_until_complete(commands)
    loop.close()
    # print(result)

def clean(packages):
    paths = [ package.path for package in packages ]
    to_clean = [ 
            path 
            for path in glob.glob(os.path.expandvars("$HOME/.vim/pack/vendor/*/*"))
            if path not in paths
    ]

    if not to_clean:
        return

    print("The following directory will be cleaned: [y/N]")
    print(to_clean)
    if input() not in ["y", "Yes", "yes"]:
        return

    cmd = ["rm", "-rf"] + to_clean
    result = subprocess.run(cmd, capture_output=True, shell=False)
    print(' '.join(cmd))
    if result.returncode != 0:
        print("FAIL on cleaning the following directory:")
        print(result.stdout)
        print(result.stderr)
    else:
        print("DONE")


def main():
    update_packages(packages)
    clean(packages)

if __name__ == "__main__":
    main()