Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin aliases #5820

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions config/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,20 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
let mut path_array: Vec<String> = package_path.split(";").map(|s| s.to_owned()).collect();

fn prefix_path(array: &mut Vec<String>, path: &Path) {
array.insert(0, format!("{}/?.lua", path.display()));
array.insert(1, format!("{}/?/init.lua", path.display()));
array.push(format!("{}/?.lua", path.display()));
array.push(format!("{}/?/init.lua", path.display()));
}

prefix_path(&mut path_array, &crate::HOME_DIR.join(".wezterm"));
for dir in crate::CONFIG_DIRS.iter() {
prefix_path(&mut path_array, dir);
}
path_array.insert(
2,
format!("{}/plugins/?/plugin/init.lua", crate::DATA_DIR.display()),
);
path_array.push(format!(
"{}/plugins/?/plugin/init.lua",
crate::DATA_DIR.display()
));

prefix_path(&mut path_array, &crate::DATA_DIR.join("plugins"));

if let Ok(exe) = std::env::current_exe() {
if let Some(path) = exe.parent() {
Expand Down
9 changes: 9 additions & 0 deletions docs/config/lua/wezterm.plugin/index.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `wezterm.plugin` module

{{since('???')}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure of how to get the correct version for these.


The `wezterm.plugin` module exposes functions that allow working with plugins, among other things installing and updating plugin.

## Available functions


11 changes: 11 additions & 0 deletions docs/config/lua/wezterm.plugin/list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `wezterm.plugin.list()`

{{since('???')}}

Returns an array of all the plugins within the plugin directory.

The elements consists of tables with:
`url` from where the git repository is hosted.
`component`, that is the name of the local root repository root directory.
`plugin_dir` the full path to the repository root directory.

6 changes: 6 additions & 0 deletions docs/config/lua/wezterm.plugin/plugin_dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# `wezterm.plugin.plugin_dir()`

{{since('???')}}

Returns the path of the plugin root directory.

13 changes: 13 additions & 0 deletions docs/config/lua/wezterm.plugin/require.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `wezterm.plugin.require()`

{{since('???')}}

Takes a `url` string as argument, which it will use to clone the git repository if not already cloned, and then require the lua files.

It will do so by making a directory based on the `url` but with `/` and `\` replaced by `sZs`, `:` replaced by `sCs` and `.` replaced by `sDs`.
It will place this directory in the plugins directory and then clone the repository into it.

It will try to require `init.lua` from the repository root or in the `plugin/` sub directory.

It is recommended to use [require_as_alias](./require_as_alias.md) instead to make it easy to require lua files via the alias instead of the modified url name.

10 changes: 10 additions & 0 deletions docs/config/lua/wezterm.plugin/require_as_alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `wezterm.plugin.require_as_alias()`

{{since('???')}}

Takes a `url` string and a `alias` string as argument. The alias cannot contain `\`, `/`, `:` or `,`.

It will make a sub directory with the name of the alias in the plugins sub directory and then clone the git repository from the url into this directory.

It is assumed that the lua files are placed in a `init.lua` in the repository root or in the `plugin/` sub-directory of the repository root.

6 changes: 6 additions & 0 deletions docs/config/lua/wezterm.plugin/update_all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# `wezterm.plugin.update_all()`

{{since('???')}}

Will update each plugin in the plugin directory, by first fetching, then merging if there are any changes.

44 changes: 44 additions & 0 deletions lua-api-crates/plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ impl RepoSpec {
})
}

fn add_alias(&mut self, alias: &str) -> anyhow::Result<()> {
if alias != compute_repo_dir(alias) {
anyhow::bail!("Invalid alias: {alias}");
}

self.component = alias.to_string();
Ok(())
}

fn load_from_dir(path: PathBuf) -> anyhow::Result<Self> {
let component = path
.file_name()
Expand Down Expand Up @@ -202,6 +211,28 @@ fn require_plugin(lua: &Lua, url: String) -> anyhow::Result<Value> {
}
}

fn require_plugin_with_alias(lua: &Lua, url: String, alias: String) -> anyhow::Result<Value> {
let mut spec = RepoSpec::parse(url)?;
spec.add_alias(&alias)?;

if !spec.is_checked_out() {
spec.check_out()?;
}

let require: mlua::Function = lua.globals().get("require")?;
match require.call::<_, Value>(alias) {
Ok(value) => Ok(value),
Err(err) => {
log::error!(
"Failed to require {} which is stored in {:?}: {err:#}",
spec.component,
spec.checkout_path()
);
Err(err.into())
}
}
}

fn list_plugins() -> anyhow::Result<Vec<RepoSpec>> {
let mut plugins = vec![];

Expand All @@ -227,6 +258,19 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> {
})?,
)?;

plugin_mod.set(
"require_as_alias",
lua.create_function(|lua: &Lua, (repo_spec, alias): (String, String)| {
require_plugin_with_alias(lua, repo_spec, alias)
.map_err(|e| mlua::Error::external(format!("{e:#}")))
})?,
)?;

plugin_mod.set(
"plugin_dir",
lua.create_function(|lua: &Lua, _: ()| to_lua(lua, RepoSpec::plugins_dir()))?,
)?;

plugin_mod.set(
"list",
lua.create_function(|lua, _: ()| {
Expand Down
Loading