leo_lang/cli/helpers/context.rs
1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use aleo_std;
18use leo_errors::Result;
19use leo_package::{Manifest, Workspace};
20
21use aleo_std::aleo_dir;
22use std::{env::current_dir, path::PathBuf};
23
24/// Project context, manifest, current directory etc
25/// All the info that is relevant in most of the commands
26// TODO: Make `path` and `home` not pub, to prevent misuse through direct access.
27#[derive(Clone)]
28pub struct Context {
29 /// Path at which the command is called, None when default
30 pub path: Option<PathBuf>,
31 /// Path to use for the Aleo registry, None when default
32 pub home: Option<PathBuf>,
33 /// Recursive flag.
34 // TODO: Shift from callee to caller by including display method
35 pub recursive: bool,
36 /// If set, target this specific workspace member.
37 pub package_filter: Option<String>,
38}
39
40impl Context {
41 pub fn new(
42 path: Option<PathBuf>,
43 home: Option<PathBuf>,
44 recursive: bool,
45 package_filter: Option<String>,
46 ) -> Result<Context> {
47 Ok(Context { path, home, recursive, package_filter })
48 }
49
50 /// Returns the path of the parent directory to the Leo package.
51 pub fn parent_dir(&self) -> Result<PathBuf> {
52 match &self.path {
53 Some(path) => {
54 let mut path = path.clone();
55 path.pop();
56 Ok(path)
57 }
58 None => Ok(current_dir().map_err(crate::errors::cli_io_error)?),
59 }
60 }
61
62 /// Returns the path to the Leo package.
63 pub fn dir(&self) -> Result<PathBuf> {
64 match &self.path {
65 Some(path) => Ok(path.clone()),
66 None => Ok(current_dir().map_err(crate::errors::cli_io_error)?),
67 }
68 }
69
70 /// Returns the path to the Aleo registry directory.
71 pub fn home(&self) -> Result<PathBuf> {
72 match &self.home {
73 Some(path) => Ok(path.clone()),
74 None => Ok(aleo_dir()),
75 }
76 }
77
78 /// Opens the manifest file `program.json`.
79 pub fn open_manifest(&self) -> Result<Manifest> {
80 let path = self.dir()?;
81 let manifest_path = path.join(leo_package::MANIFEST_FILENAME);
82 let manifest = Manifest::read_from_file(manifest_path)?;
83 Ok(manifest)
84 }
85
86 /// Returns the workspace root and the ordered list of member directories to
87 /// operate on, respecting `--package` filtering and workspace discovery.
88 ///
89 /// - At workspace root without `--package`: all members in dependency order.
90 /// - At workspace root with `--package`: just that member.
91 /// - Inside a member directory: just that member.
92 /// - No workspace found: `None` (caller falls through to single-package behavior).
93 ///
94 /// When `Some`, the first tuple element is the canonicalized workspace root,
95 /// so callers needing it (e.g. `leo clean` removing the shared `build/`) do
96 /// not have to re-walk for it.
97 pub fn resolve_targets(&self) -> Result<Option<(PathBuf, Vec<PathBuf>)>> {
98 let dir = self.dir()?;
99
100 let workspace = match Workspace::discover(&dir)? {
101 Some(ws) => ws,
102 None => {
103 if self.package_filter.is_some() {
104 return Err(crate::errors::workspace_no_workspace().into());
105 }
106 return Ok(None);
107 }
108 };
109
110 let root = workspace.root_directory.clone();
111
112 if let Some(ref filter) = self.package_filter {
113 match workspace.find_member(filter) {
114 Some(path) => Ok(Some((root, vec![path.clone()]))),
115 None => Err(crate::errors::workspace_package_not_found(filter, root.display()).into()),
116 }
117 } else {
118 let canonical = dir.canonicalize().unwrap_or_else(|_| dir.clone());
119 if canonical == workspace.root_directory {
120 // At workspace root - operate on all members.
121 Ok(Some((root, workspace.member_paths)))
122 } else if workspace.is_member(&canonical) {
123 // Inside a member - operate on just this member.
124 Ok(Some((root, vec![canonical])))
125 } else {
126 // Inside the workspace tree but not in a member directory.
127 Ok(None)
128 }
129 }
130 }
131
132 /// Create a new `Context` pointing at a specific directory.
133 pub fn with_path(&self, path: PathBuf) -> Self {
134 Context { path: Some(path), home: self.home.clone(), recursive: self.recursive, package_filter: None }
135 }
136}