diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cf41862..16e4dc3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: - name: panvimdoc uses: kdheepak/panvimdoc@main with: - vimdoc: nvim-plugin-template + vimdoc: minpm treesitter: true - uses: stefanzweifel/git-auto-commit-action@v4 with: @@ -61,3 +61,4 @@ jobs: luarocks install luacheck luarocks install vusted vusted ./test + vusted ./test diff --git a/doc/nvim-plugin-template.txt b/doc/minpm.txt similarity index 100% rename from doc/nvim-plugin-template.txt rename to doc/minpm.txt diff --git a/lua/minpm/init.lua b/lua/minpm/init.lua new file mode 100644 index 0000000..30202ed --- /dev/null +++ b/lua/minpm/init.lua @@ -0,0 +1,116 @@ +local api, stdpath, uv = vim.api, vim.fn.stdpath, vim.uv +local repos, INSTALL, UPDATE = {}, 0, 1 +local data_dir = stdpath('data') +---@diagnostic disable-next-line: param-type-mismatch +local STARTDIR = vim.fs.joinpath(data_dir, 'site', 'minpm', 'start') +---@diagnostic disable-next-line: param-type-mismatch +local OPTDIR = vim.fs.joinpath(data_dir, 'site', 'minpm', 'opt') + +local function as_table(data) + return type(data) ~= 'table' and { data } or data +end + +local use_meta = {} +use_meta.__index = use_meta +function use_meta:event(e) + self.event = as_table(e) + self.islazy = true + return self +end + +function use_meta:ft(ft) + self.ft = as_table(ft) + self.islazy = true + return self +end + +function use_meta:setup(config) + self.setup = config + return self +end + +function use_meta:config(config) + assert(type(config) == 'function') + self.config = config + return self +end + +local function info_win() + local bufnr = api.nvim_create_buf(false, false) + local win = api.nvim_open_win(bufnr, true, { + relative = 'editor', + height = math.floor(vim.o.lines * 0.5), + width = math.floor(vim.o.columns * 0.8), + row = 3, + col = 10, + border = 'rounded', + noautocmd = true, + }) + vim.wo[win].wrap = false + return win, bufnr +end + +local bufnr, winid + +local function handle_git_output(index, data) + vim.schedule(function() + if not winid then + winid, bufnr = info_win() + end + api.nvim_buf_set_lines(bufnr, index - 1, index, false, { data }) + end) +end + +function use_meta:do_action(index, action) + local tail = vim.split(self.name, '/')[2] + local path = vim.fs.joinpath(self.islazy and OPTDIR or STARTDIR, tail) + local url = ('https://github.com/%s'):format(self.name) + local cmd = action == INSTALL and { 'git', 'clone', '--progress', url, path } + or { 'git', '-C', '--progress', path, 'pull' } + uv.fs_stat(path, function(_, stat) + if stat and stat.type == 'directory' then + return + end + coroutine.resume(coroutine.create(function() + local co = assert(coroutine.running()) + vim.system(cmd, { + timeout = 5000, + stderr = function(err, data) + coroutine.resume(co, err, data) + end, + }) + while true do + local err, data = coroutine.yield() + if not data then + break + end + local lines = err and err or data + lines = lines:gsub('\r', '\n'):gsub('\n+', '\n') + lines = vim.split(lines, '\n', { trimempty = true }) + handle_git_output(index, ('%s: %s'):format(self.name, lines[#lines])) + end + end)) + end) +end + +local function action(act) + local index = 0 + vim.iter(repos):map(function(repo) + index = index + 1 + repo:do_action(index, act) + end) +end + +return { + use = function(name) + local repo = setmetatable({ name = name }, use_meta) + repos[#repos + 1] = repo + return repo + end, + install = function() + action(INSTALL) + end, + update = function() + action(UPDATE) + end, +} diff --git a/lua/nvim-plugin-template/init.lua b/lua/nvim-plugin-template/init.lua deleted file mode 100644 index afa0777..0000000 --- a/lua/nvim-plugin-template/init.lua +++ /dev/null @@ -1,7 +0,0 @@ -local function example() - return true -end - -return { - example = example, -} diff --git a/plugin/nvim-plugin-template.lua b/plugin/minpm.lua similarity index 100% rename from plugin/nvim-plugin-template.lua rename to plugin/minpm.lua diff --git a/rename.py b/rename.py deleted file mode 100644 index ecd2c14..0000000 --- a/rename.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/python -# -*- coding: UTF-8 -*- - -import os, sys -pdir = os.getcwd() - -# ANSI color codes -class Colors: - RED = '\033[91m' - GREEN = '\033[92m' - YELLOW = '\033[93m' - BLUE = '\033[94m' - RESET = '\033[0m' - -def print_colored(message, color): - print(color + message + Colors.RESET) - -if len(sys.argv) != 2: - print_colored("plugin name is missing", Colors.RED) - sys.exit(1) - -new_name = sys.argv[1] -for dir in os.listdir(pdir): - if dir == "lua": - os.rename(os.path.join("lua", "nvim-plugin-template"), os.path.join("lua",new_name)) - print_colored("Renamed files under lua folder successed", Colors.GREEN) - if dir == "plugin": - os.rename(os.path.join("plugin", "nvim-plugin-template.lua"), - os.path.join("plugin",new_name + ".lua")) - print_colored("Renamed files under plugin folder successed", Colors.GREEN) - if dir == 'doc': - os.rename(os.path.join("doc", "nvim-plugin-template.txt"), - os.path.join("doc",new_name + ".txt")) - print_colored("Renamed files under doc folder successed", Colors.GREEN) - if dir == '.github': - with open(os.path.join(".github","workflows","ci.yml"), 'r+') as f: - d = f.read() - t = d.replace('nvim-plugin-template', new_name) - f.seek(0, 0) - f.write(t) - print_colored("Ci yaml has been updated", Colors.GREEN) - -choice = input("Do you need plugin folder in your plugin (y|n): ") -if choice.lower() == 'n': - os.remove(os.path.join(os.getcwd(), 'plugin')) - -choice = input("Do you want also remove example code in init.lua and test (y|n): ") -if choice.lower() == 'y': - with open(os.path.join(pdir, 'lua',new_name,'init.lua'), 'w') as f: - f.truncate() - - with open(os.path.join(pdir, 'test','plugin_spec.lua'), 'w') as f: - f.truncate() - -os.remove(os.path.join(os.getcwd(), 'rename.py')) -print_colored("All works done enjoy", Colors.YELLOW) diff --git a/test/plugin_spec.lua b/test/plugin_spec.lua index 4f1ef32..e69de29 100644 --- a/test/plugin_spec.lua +++ b/test/plugin_spec.lua @@ -1,8 +0,0 @@ -local example = require('nvim-plugin-template').example - -describe('neovim plugin', function() - it('work as expect', function() - local result = example() - assert.is_true(result) - end) -end)