Skip to content

Commit

Permalink
Merge plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Oct 16, 2023
1 parent 656adeb commit f983e09
Showing 1 changed file with 86 additions and 14 deletions.
100 changes: 86 additions & 14 deletions denops/dpp/dpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,26 @@ export class Dpp {
configReturn: ConfigReturn,
) {
// Initialize plugins
const protocols = await this.getProtocols(denops, options);
const recordPlugins: Record<string, Plugin> = {};
for (const plugin of configReturn.plugins) {
recordPlugins[plugin.name] = initPlugin(plugin, basePath);
recordPlugins[plugin.name] = initPlugin(
await detectPlugin(
denops,
options,
protocols,
plugin,
),
basePath,
);
}

const dppRuntimepath = `${basePath}/${name}/.dpp`;
if (!await isDirectory(dppRuntimepath)) {
await Deno.mkdir(dppRuntimepath, { recursive: true });
if (await isDirectory(dppRuntimepath)) {
// Remove old runtime files
await Deno.remove(dppRuntimepath, { recursive: true });
}
await Deno.mkdir(dppRuntimepath, { recursive: true });

// Get runtimepath
// NOTE: Use init_runtimepath.
Expand Down Expand Up @@ -208,11 +219,11 @@ export class Dpp {
);
}
}
console.log(inlineVimrcs);
//console.log(inlineVimrcs);

// Write state file
const stateFile = `${basePath}/${name}/state.vim`;
console.log(stateFile);
//console.log(stateFile);
await Deno.writeTextFile(stateFile, stateLines.join("\n"));

const cacheFile = `${basePath}/${name}/cache.vim`;
Expand All @@ -224,7 +235,7 @@ export class Dpp {
configReturn.checkFiles ?? [],
]),
];
console.log(cacheFile);
//console.log(cacheFile);
await Deno.writeTextFile(cacheFile, cacheLines.join("\n"));

//console.log(stateLines);
Expand All @@ -241,21 +252,26 @@ export class Dpp {
dppRuntimepath: string,
recordPlugins: Record<string, Plugin>,
) {
// Copy help files
const ftdetectDir = `${dppRuntimepath}/ftdetect`;
if (!await isDirectory(ftdetectDir)) {
await Deno.mkdir(ftdetectDir, { recursive: true });
}
const docDir = `${dppRuntimepath}/doc`;
if (!await isDirectory(docDir)) {
await Deno.mkdir(docDir, { recursive: true });
}

// Copy both ftdetect and help files
for (const plugin of Object.values(recordPlugins)) {
const srcDir = `${plugin.path}/doc`;
if (!plugin.path || !await isDirectory(srcDir)) {
continue;
}
for (const srcDir of [`${plugin.path}/doc`, `${plugin.path}/ftdetect`]) {
if (!plugin.path || !await isDirectory(srcDir)) {
continue;
}

for await (const entry of Deno.readDir(srcDir)) {
const path = join(srcDir, entry.name);
await copy(path, join(docDir, entry.name), { overwrite: true });
for await (const entry of Deno.readDir(srcDir)) {
const path = join(srcDir, entry.name);
await copy(path, join(docDir, entry.name), { overwrite: true });
}
}
}

Expand All @@ -269,6 +285,27 @@ export class Dpp {
`:helptags failed`,
);
}

// Merge plugin files
for (
const plugin of Object.values(recordPlugins).filter((plugin) =>
plugin.merged
)
) {
if (!plugin.path || !await isDirectory(plugin.path)) {
continue;
}

for await (const entry of Deno.readDir(plugin.path)) {
if (entry.name === "doc" || entry.name === "ftdetect") {
// Already copied
continue;
}

const path = join(plugin.path, entry.name);
await copy(path, join(dppRuntimepath, entry.name), { overwrite: true });
}
}
}

private async getExt(
Expand Down Expand Up @@ -502,6 +539,41 @@ function initPlugin(plugin: Plugin, basePath: string): Plugin {
return plugin;
}

async function detectPlugin(
denops: Denops,
options: DppOptions,
protocols: Record<ProtocolName, Protocol>,
plugin: Plugin,
) {
// Detect protocol
if ("protocol" in plugin) {
return plugin;
}

for (
const protocol of options.protocols.filter((protocolName) =>
protocols[protocolName]
).map((protocolName) => protocols[protocolName])
) {
const detect = await protocol.protocol.detect({
denops: denops,
plugin,
protocolOptions: protocol.options,
protocolParams: protocol.params,
});

if (detect) {
// Overwrite by detect()
Object.assign(plugin, {
...detect,
protocol: protocol.protocol.name,
});
}
}

return plugin;
}

Deno.test("initPlugin", () => {
assertEquals(
initPlugin({
Expand Down

0 comments on commit f983e09

Please sign in to comment.