Skip to content

Commit

Permalink
Add initPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Sep 21, 2023
1 parent 13eb468 commit 7eaf62f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 4 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion denops/dpp/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function main(denops: Denops) {
dpp,
});

await dpp.makeState(basePath, plugins);
await dpp.makeState(denops, basePath, plugins);

return Promise.resolve();
},
Expand Down
119 changes: 117 additions & 2 deletions denops/dpp/dpp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Denops } from "./deps.ts";
import { assertEquals, Denops, is } from "./deps.ts";
import {
ActionName,
BaseExt,
Expand Down Expand Up @@ -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<string, Plugin> = {};
for (const plugin of plugins) {
recordPlugins[plugin.name] = initPlugin(plugin, basePath, isSudo);
}

// Write state file
}

private async getExt(
Expand Down Expand Up @@ -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",
},
);
});
2 changes: 1 addition & 1 deletion denops/dpp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type Action<Params extends BaseActionParams> = {
export type Plugin = {
augroup?: string;
build?: string;
depends?: string[];
depends?: string | string[];
frozen?: boolean;
ftplugin?: Record<string, string>;
if?: boolean | string;
Expand Down

0 comments on commit 7eaf62f

Please sign in to comment.