Skip to content

Commit

Permalink
Write state file
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Sep 21, 2023
1 parent 7eaf62f commit 11aedda
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 64 deletions.
51 changes: 19 additions & 32 deletions autoload/dpp.vim
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
function dpp#load_state(path) abort
function dpp#load_state(base_path) abort
if !('#dpp'->exists())
call dpp#min#_init()
endif
endfunction

function dpp#begin(path, options = {}) abort
if !has('patch-9.0.1276') && !has('nvim-0.10')
call dpp#util#_error('dpp.vim requires Vim 9.0.1276+ or NeoVim 0.10+.')
return 1
endif

if !('#dpp'->exists())
call dpp#_init()
call dpp#min#_init()
endif

let g:dpp#_options = extend(g:dpp#_options, a:options)
Expand Down Expand Up @@ -119,12 +117,6 @@ function dpp#end() abort
if !(depends->empty())
call dpp#source(depends)
endif

if !has('vim_starting')
call dpp#call_hook('add')
call dpp#call_hook('source')
call dpp#call_hook('post_source')
endif
endfunction

function dpp#get(name = '') abort
Expand All @@ -137,29 +129,24 @@ function dpp#source(plugins = g:dpp#_plugins->values()) abort
endfunction

function dpp#make_state(base_path, config_path) abort
if !has('patch-9.0.1276') && !has('nvim-0.10')
call dpp#util#_error('dpp.vim requires Vim 9.0.1276+ or NeoVim 0.10+.')
return 1
endif

if !(a:config_path->filereadable())
call dpp#util#print_error(printf('"%s" is not found.', a:config_path))
return
return 1
endif

return dpp#denops#_notify('makeState', [a:base_path, a:config_path])
endfunction

function dpp#is_sudo() abort
return $SUDO_USER !=# '' && $USER !=# $SUDO_USER
\ && $HOME !=# ('~'.$USER)->expand()
\ && $HOME ==# ('~'.$SUDO_USER)->expand()
endfunction

function dpp#_init() abort
let g:dpp#_plugins = {}
let g:dpp#_options = {}
if !('#dpp'->exists())
call dpp#min#_init()
endif

const g:dpp#_progname = has('nvim') && exists('$NVIM_APPNAME') ?
\ $NVIM_APPNAME : v:progname->fnamemodify(':r')
const g:dpp#_init_runtimepath = &runtimepath
" Check sudo
if g:dpp#_is_sudo
return
endif

augroup dpp
autocmd!
augroup END
return dpp#denops#_notify('makeState', [a:base_path, a:config_path])
endfunction
16 changes: 16 additions & 0 deletions autoload/dpp/min.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function dpp#min#_init() abort
let g:dpp#_cache_version = 1
let g:dpp#_plugins = {}
let g:dpp#_options = {}
let g:dpp#_is_sudo = $SUDO_USER !=# '' && $USER !=# $SUDO_USER
\ && $HOME !=# ('~'.$USER)->expand()
\ && $HOME ==# ('~'.$SUDO_USER)->expand()

const g:dpp#_progname = has('nvim') && exists('$NVIM_APPNAME') ?
\ $NVIM_APPNAME : v:progname->fnamemodify(':r')
const g:dpp#_init_runtimepath = &runtimepath

augroup dpp
autocmd!
augroup END
endfunction
2 changes: 0 additions & 2 deletions denops/dpp/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export function main(denops: Denops) {
});

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

return Promise.resolve();
},
};
}
63 changes: 34 additions & 29 deletions denops/dpp/dpp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, Denops, is } from "./deps.ts";
import { assertEquals, Denops, is, vars } from "./deps.ts";
import {
ActionName,
BaseExt,
Expand All @@ -19,7 +19,7 @@ import {
} from "./context.ts";
import { Loader } from "./loader.ts";
import { defaultExtOptions } from "./base/ext.ts";
import { errorException } from "./utils.ts";
import { errorException, isDirectory } from "./utils.ts";

export class Dpp {
private loader: Loader;
Expand Down Expand Up @@ -63,16 +63,38 @@ export class Dpp {
}

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);
recordPlugins[plugin.name] = initPlugin(plugin, basePath);
}

console.log(recordPlugins);

if (!isDirectory(basePath)) {
await Deno.mkdir(basePath, { recursive: true });
}

// Write state file
const progname = await vars.g.get(denops, "dpp#_progname");
const stateFile = `${basePath}/cache_${progname}`;

const cacheVersion = await vars.g.get(denops, "dpp#_cache_version");
const initRuntimepath = await vars.g.get(denops, "dpp#_init_runtimepath");
const stateLines = [
`if g:dpp#_cache_version !=# ${cacheVersion} || ` +
`g:dpp#_init_runtimepath !=# '${initRuntimepath}' | ` +
" | throw ''Cache loading error'' | endif",
"let [s:plugins, s:ftplugin] = dpp#min#_load_cache_raw()",
"if s:plugins->empty() | throw 'Cache loading error' | endif",
"let g:dpp#_plugins = s:plugins",
"let g:dpp#ftplugin = s:ftplugin",
`let g:dpp#_base_path = '${basePath}'`,
`let &runtimepath = `,
];

console.log(stateFile);
console.log(stateLines);
}

private async getExt(
Expand Down Expand Up @@ -160,10 +182,10 @@ function extArgs<
return [o, p];
}

function initPlugin(plugin: Plugin, basePath: string, isSudo: boolean): Plugin {
function initPlugin(plugin: Plugin, basePath: string): Plugin {
if (!plugin.path) {
// Set default path from basePath
plugin.path = `${basePath}/repos/${plugin.name}`;
plugin.path = `${basePath}/repos/${plugin.repo ?? plugin.name}`;
}

if (plugin.rev && plugin.rev.length > 0) {
Expand All @@ -178,13 +200,10 @@ function initPlugin(plugin: Plugin, basePath: string, isSudo: boolean): Plugin {

// Set rtp
if (!plugin.rtp || plugin.rtp.length != 0) {
plugin.rtp = `${plugin.path}/${plugin.rtp}`;
plugin.rtp = !plugin.rtp ? plugin.path : `${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];
Expand Down Expand Up @@ -223,7 +242,7 @@ Deno.test("initPlugin", () => {
rev: "[hoge]",
script_type: "foo",
rtp: "autoload",
}, "base", false),
}, "base"),
{
name: "foo",
path: "base/repos/foo__hoge_/foo",
Expand All @@ -235,30 +254,16 @@ Deno.test("initPlugin", () => {
},
);

// 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),
}, "base"),
{
name: "foo",
path: "base/repos/foo",
rtp: "",
rtp: "base/repos/foo",
lazy: true,
merged: false,
on_ft: "foo",
Expand Down
1 change: 0 additions & 1 deletion denops/dpp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,4 @@ export type Plugin = {
rtp?: string;
script_type?: string;
timeout?: number;
trusted?: boolean;
};

0 comments on commit 11aedda

Please sign in to comment.