From 7eaf62faf54a6a301187c7f18b4be027789f3468 Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Thu, 21 Sep 2023 12:12:44 +0900 Subject: [PATCH] Add initPlugin --- Makefile | 16 ++++++ denops/dpp/app.ts | 2 +- denops/dpp/dpp.ts | 119 +++++++++++++++++++++++++++++++++++++++++++- denops/dpp/types.ts | 2 +- 4 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..72227fa --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +TS=$(shell find denops -name "*.ts") +TSTEST=$(shell grep -rl "Deno.test" denops) + +lint: + deno check ${TS} + deno fmt --check denops + deno test --unstable --no-run -A ${TS} + deno lint --unstable denops + +test: + deno test --unstable -A ${TSTEST} + +format: + deno fmt denops + +.PHONY: lint test format diff --git a/denops/dpp/app.ts b/denops/dpp/app.ts index dfe6971..a97bfd5 100644 --- a/denops/dpp/app.ts +++ b/denops/dpp/app.ts @@ -26,7 +26,7 @@ export function main(denops: Denops) { dpp, }); - await dpp.makeState(basePath, plugins); + await dpp.makeState(denops, basePath, plugins); return Promise.resolve(); }, diff --git a/denops/dpp/dpp.ts b/denops/dpp/dpp.ts index d4e7276..74fc9d6 100644 --- a/denops/dpp/dpp.ts +++ b/denops/dpp/dpp.ts @@ -1,4 +1,4 @@ -import { Denops } from "./deps.ts"; +import { assertEquals, Denops, is } from "./deps.ts"; import { ActionName, BaseExt, @@ -62,8 +62,17 @@ export class Dpp { return ret; } - async makeState(basePath: string, plugins: Plugin[]) { + async makeState(denops: Denops, basePath: string, plugins: Plugin[]) { + // Check sudo + const isSudo = await denops.call("dpp#is_sudo") as boolean; + // Initialize plugins + const recordPlugins: Record = {}; + for (const plugin of plugins) { + recordPlugins[plugin.name] = initPlugin(plugin, basePath, isSudo); + } + + // Write state file } private async getExt( @@ -150,3 +159,109 @@ function extArgs< ]); return [o, p]; } + +function initPlugin(plugin: Plugin, basePath: string, isSudo: boolean): Plugin { + if (!plugin.path) { + // Set default path from basePath + plugin.path = `${basePath}/repos/${plugin.name}`; + } + + if (plugin.rev && plugin.rev.length > 0) { + // Add revision path + plugin.path += `_${plugin.rev.replaceAll(/[^\w.-]/g, "_")}`; + } + + if (plugin.script_type && plugin.script_type.length > 0) { + // Add script_type path + plugin.path += `/${plugin.script_type}`; + } + + // Set rtp + if (!plugin.rtp || plugin.rtp.length != 0) { + plugin.rtp = `${plugin.path}/${plugin.rtp}`; + } + // Chomp + plugin.rtp = plugin.rtp.replace(/\/$/, ""); + if (isSudo && !plugin.trusted) { + plugin.rtp = ""; + } + + if (plugin.depends && is.String(plugin.depends)) { + plugin.depends = [plugin.depends]; + } + + if (!plugin.lazy) { + plugin.lazy = [ + "on_ft", + "on_cmd", + "on_func", + "on_lua", + "on_map", + "on_path", + "on_if", + "on_event", + "on_source", + ].filter((key) => key in plugin).length > 0; + } + + if (!plugin.merged) { + plugin.merged = !plugin.lazy && [ + "local", + "build", + "if", + "hook_post_update", + ].filter((key) => key in plugin).length <= 0; + } + + return plugin; +} + +Deno.test("initPlugin", () => { + assertEquals( + initPlugin({ + name: "foo", + rev: "[hoge]", + script_type: "foo", + rtp: "autoload", + }, "base", false), + { + name: "foo", + path: "base/repos/foo__hoge_/foo", + rtp: "base/repos/foo__hoge_/foo/autoload", + rev: "[hoge]", + script_type: "foo", + lazy: false, + merged: true, + }, + ); + + // sudo + assertEquals( + initPlugin({ + name: "foo", + }, "base", true), + { + name: "foo", + path: "base/repos/foo", + rtp: "", + lazy: false, + merged: true, + }, + ); + + // lazy + assertEquals( + initPlugin({ + name: "foo", + on_ft: "foo", + }, "base", true), + { + name: "foo", + path: "base/repos/foo", + rtp: "", + lazy: true, + merged: false, + on_ft: "foo", + }, + ); +}); diff --git a/denops/dpp/types.ts b/denops/dpp/types.ts index 4a9f01c..b1d496d 100644 --- a/denops/dpp/types.ts +++ b/denops/dpp/types.ts @@ -70,7 +70,7 @@ export type Action = { export type Plugin = { augroup?: string; build?: string; - depends?: string[]; + depends?: string | string[]; frozen?: boolean; ftplugin?: Record; if?: boolean | string;