Skip to main content

lux_lib/operations/
exec.rs

1use std::io;
2use tokio::process::Command;
3
4use crate::{
5    config::Config,
6    lua_rockspec::LuaVersionError,
7    lua_version::{LuaVersion, LuaVersionUnset},
8    operations::{BuildWorkspace, BuildWorkspaceError, Install},
9    package::{PackageReq, PackageVersionReqError},
10    path::{Paths, PathsError},
11    remote_package_db::RemotePackageDBError,
12    tree::{self, InstallTree, TreeError},
13    workspace::{Workspace, WorkspaceTreeError},
14};
15use bon::Builder;
16use itertools::Itertools;
17use miette::Diagnostic;
18use thiserror::Error;
19use which::which;
20
21use super::{InstallError, PackageInstallSpec};
22
23/// Rocks package runner, providing fine-grained control
24/// over how a package should be run.
25#[derive(Builder)]
26#[builder(start_fn = new, finish_fn(name = _exec, vis = ""))]
27pub struct Exec<'a> {
28    #[builder(start_fn)]
29    command: &'a str,
30    #[builder(start_fn)]
31    workspace: Option<&'a Workspace>,
32    #[builder(start_fn)]
33    config: &'a Config,
34
35    #[builder(field)]
36    args: Vec<String>,
37
38    disable_loader: Option<bool>,
39}
40
41impl<State: exec_builder::State> ExecBuilder<'_, State> {
42    pub fn arg(mut self, arg: impl Into<String>) -> Self {
43        self.args.push(arg.into());
44        self
45    }
46
47    pub fn args(mut self, args: impl IntoIterator<Item: Into<String>>) -> Self {
48        self.args.extend(args.into_iter().map_into());
49        self
50    }
51}
52
53impl<State> ExecBuilder<'_, State>
54where
55    State: exec_builder::State + exec_builder::IsComplete,
56{
57    pub async fn exec(self) -> Result<(), ExecError>
58    where
59        State: exec_builder::IsComplete,
60    {
61        exec(self._exec()).await
62    }
63}
64
65#[derive(Error, Debug, Diagnostic)]
66pub enum ExecError {
67    #[error("failed to run {cmd}: {source}")]
68    RunCommandFailed {
69        cmd: String,
70        source: io::Error,
71        #[help]
72        help: Option<String>,
73    },
74    #[error(
75        "{cmd} exited with non-zero exit code: {}",
76        exit_code.map(|code| code.to_string()).unwrap_or("unknown".into())
77    )]
78    #[diagnostic(help("check the command's output for errors."))]
79    RunCommandNonZeroExitCode { cmd: String, exit_code: Option<i32> },
80    #[error(transparent)]
81    #[diagnostic(transparent)]
82    LuaVersionUnset(#[from] LuaVersionUnset),
83    #[error(transparent)]
84    #[diagnostic(transparent)]
85    Tree(#[from] TreeError),
86    #[error(transparent)]
87    #[diagnostic(transparent)]
88    Paths(#[from] PathsError),
89    #[error(transparent)]
90    #[diagnostic(transparent)]
91    LuaVersionError(#[from] LuaVersionError),
92    #[error(transparent)]
93    #[diagnostic(transparent)]
94    BuildProject(#[from] Box<BuildWorkspaceError>),
95    #[error(transparent)]
96    #[diagnostic(transparent)]
97    InstallCommand(#[from] Box<InstallCommandError>),
98    #[error(transparent)]
99    #[diagnostic(transparent)]
100    WorkspaceTree(#[from] WorkspaceTreeError),
101    #[error("failed to execute '{0}':\n{1}")]
102    Io(String, io::Error),
103}
104
105impl From<BuildWorkspaceError> for ExecError {
106    fn from(source: BuildWorkspaceError) -> Self {
107        Self::BuildProject(Box::new(source))
108    }
109}
110
111impl From<InstallCommandError> for ExecError {
112    fn from(source: InstallCommandError) -> Self {
113        Self::InstallCommand(Box::new(source))
114    }
115}
116
117#[derive(Error, Debug, Diagnostic)]
118#[error(transparent)]
119pub enum InstallCommandError {
120    InstallError(#[from] Box<InstallError>),
121    PackageVersionReqError(#[from] PackageVersionReqError),
122    RemotePackageDBError(#[from] RemotePackageDBError),
123    Tree(#[from] TreeError),
124    LuaVersionUnset(#[from] LuaVersionUnset),
125}
126
127impl From<InstallError> for InstallCommandError {
128    fn from(source: InstallError) -> Self {
129        Self::InstallError(Box::new(source))
130    }
131}
132
133async fn exec(run: Exec<'_>) -> Result<(), ExecError> {
134    let lua_version = run
135        .workspace
136        .map(|project| project.lua_version(run.config))
137        .transpose()?
138        .unwrap_or(LuaVersion::from(run.config)?.clone());
139
140    if let Some(project) = run.workspace {
141        BuildWorkspace::new(project, run.config)
142            .no_lock(false)
143            .only_deps(false)
144            .build()
145            .await?;
146    } else if which(run.command).is_err() {
147        install_command(run.command, run.config).await?
148    };
149
150    let user_tree = run.config.user_tree(lua_version)?;
151    let mut paths = Paths::new(&user_tree)?;
152
153    if let Some(project) = run.workspace {
154        paths.prepend(&Paths::new(&project.tree(run.config)?)?);
155    }
156
157    let lua_init = if run.disable_loader.unwrap_or(false) {
158        None
159    } else if user_tree.version().lux_lib_dir().is_none() {
160        tracing::warn!(
161            r#"lux-lua library not found.
162Cannot use the `lux.loader`.
163To suppress this warning, set the `--no-loader` option."#
164        );
165        None
166    } else {
167        Some(paths.init())
168    };
169
170    let status = match Command::new(run.command)
171        .args(run.args)
172        .env("PATH", paths.path_prepended().joined())
173        .env("LUA_INIT", lua_init.unwrap_or_default())
174        .env("LUA_PATH", paths.package_path().joined())
175        .env("LUA_CPATH", paths.package_cpath().joined())
176        .status()
177        .await
178    {
179        Ok(status) => Ok(status),
180        Err(err) => {
181            let help = if err.to_string().starts_with("No such file") {
182                Some(format!(
183                    "make sure '{}' is available on your PATH",
184                    run.command
185                ))
186            } else {
187                None
188            };
189            Err(ExecError::RunCommandFailed {
190                cmd: run.command.to_string(),
191                source: err,
192                help,
193            })
194        }
195    }?;
196    if status.success() {
197        Ok(())
198    } else {
199        Err(ExecError::RunCommandNonZeroExitCode {
200            cmd: run.command.to_string(),
201            exit_code: status.code(),
202        })
203    }
204}
205
206/// Ensure that a command is installed.
207/// This defaults to the local project tree if cwd is a project root.
208async fn install_command(command: &str, config: &Config) -> Result<(), InstallCommandError> {
209    let install_spec = PackageInstallSpec::new(
210        PackageReq::new(command.into(), None)?,
211        tree::EntryType::Entrypoint,
212    )
213    .build();
214    let tree = config.user_tree(LuaVersion::from(config)?.clone())?;
215    Install::new(config)
216        .package(install_spec)
217        .tree(tree)
218        .install()
219        .await?;
220    Ok(())
221}