Skip to main content

lux_cli/
workspace.rs

1use lux_lib::package;
2use std::{collections::HashSet, path::PathBuf, str::FromStr};
3
4use itertools::Itertools;
5use lux_lib::{
6    config::Config,
7    drivers::sync::{Sync, SyncMode, TargetSet},
8    git, lua,
9    tree::Tree,
10    workspace::Workspace,
11};
12use miette::{Context, Result};
13use walkdir::WalkDir;
14
15pub fn top_level_ignored_files(project: &Workspace) -> Vec<PathBuf> {
16    let top_level_project_files = ignore::WalkBuilder::new(project.root())
17        .max_depth(Some(1))
18        .build()
19        .filter_map(Result::ok)
20        .filter_map(|entry| {
21            let file = entry.into_path();
22            if file.is_dir() || file.extension().is_some_and(|ext| ext == "lua") {
23                Some(file)
24            } else {
25                None
26            }
27        })
28        .collect::<HashSet<_>>();
29
30    let top_level_files = WalkDir::new(project.root())
31        .max_depth(1)
32        .into_iter()
33        .filter_map(Result::ok)
34        .filter_map(|entry| {
35            let file = entry.into_path();
36            if file.is_dir() || file.extension().is_some_and(|ext| ext == "lua") {
37                Some(file)
38            } else {
39                None
40            }
41        })
42        .collect::<HashSet<_>>();
43
44    top_level_files
45        .difference(&top_level_project_files)
46        .cloned()
47        .collect_vec()
48}
49
50/// Used for parsing alternatives between a git URL shorthand and a package requirement.
51// The `FromStr` instance tries to parse a git URL shorthand first (expecting a git host prefix),
52// and then a package requirement.
53#[derive(Debug, Clone)]
54pub enum PackageReqOrGitShorthand {
55    PackageReq(package::Req),
56    GitShorthand(git::remote::Shorthand),
57}
58
59impl FromStr for PackageReqOrGitShorthand {
60    type Err = miette::Report;
61
62    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
63        match git::remote::Shorthand::parse_with_prefix(s) {
64            Ok(shorthand) => Ok(Self::GitShorthand(shorthand)),
65            Err(_) => Ok(Self::PackageReq(package::Req::parse(s)?)),
66        }
67    }
68}
69
70/// Get the current workspaces tree, or fall back to
71/// the user tree if not in a project
72pub fn current_workspace_or_user_tree(config: &Config) -> Result<Tree> {
73    let workspace = Workspace::current()?;
74    Ok(match &workspace {
75        Some(workspace) => workspace.tree(config)?,
76        None => {
77            let lua_version = lua::Version::from(config)?.clone();
78            config.user_tree(lua_version)?
79        }
80    })
81}
82
83pub async fn sync_dependencies_if_locked(workspace: &Workspace, config: &Config) -> Result<()> {
84    // NOTE: We only update the lockfile if one exists.
85    // Otherwise, the next `lx build` will remove the packages.
86    Sync::new(workspace, config)
87        .mode(SyncMode::Frozen)
88        .targets(TargetSet::all(workspace, true))
89        .sync()
90        .await
91        .wrap_err("syncing dependencies with the workspace lockfile failed.")?;
92    Ok(())
93}
94
95pub fn exists_matching_workspace_member(package_req: &package::Req) -> Result<bool> {
96    let workspace = Workspace::current()?;
97    Ok(workspace.is_some_and(|ws| {
98        ws.select_member(package_req.name()).is_ok_and(|project| {
99            project
100                .toml()
101                .version()
102                .is_ok_and(|version| package_req.version_req().matches(&version))
103        })
104    }))
105}