2024-10-20 17:13:08 +01:00
|
|
|
#[cfg(feature = "version-management")]
|
|
|
|
use crate::cli::version::{
|
|
|
|
check_for_updates, current_version, get_or_download_version, max_installed_version,
|
|
|
|
};
|
|
|
|
use crate::cli::{auth::get_tokens, home_dir, repos::update_repo_dependencies, HOME_DIR};
|
2024-08-12 00:17:26 +01:00
|
|
|
use anyhow::Context;
|
|
|
|
use clap::Parser;
|
|
|
|
use colored::Colorize;
|
|
|
|
use indicatif::MultiProgress;
|
|
|
|
use indicatif_log_bridge::LogWrapper;
|
|
|
|
use pesde::{AuthConfig, Project, MANIFEST_FILE_NAME};
|
2024-09-03 15:01:48 +01:00
|
|
|
use std::{
|
|
|
|
collections::HashSet,
|
|
|
|
fs::create_dir_all,
|
|
|
|
path::{Path, PathBuf},
|
2024-10-20 17:13:08 +01:00
|
|
|
thread::spawn,
|
2024-09-03 15:01:48 +01:00
|
|
|
};
|
2024-08-12 00:17:26 +01:00
|
|
|
|
2024-07-12 23:09:37 +01:00
|
|
|
mod cli;
|
2024-07-22 21:00:09 +01:00
|
|
|
pub mod util;
|
2024-03-04 20:18:49 +00:00
|
|
|
|
2024-07-12 23:09:37 +01:00
|
|
|
#[derive(Parser, Debug)]
|
2024-09-28 23:04:35 +01:00
|
|
|
#[clap(
|
|
|
|
version,
|
|
|
|
about = "A package manager for the Luau programming language, supporting multiple runtimes including Roblox and Lune"
|
|
|
|
)]
|
2024-07-12 23:09:37 +01:00
|
|
|
#[command(disable_version_flag = true)]
|
|
|
|
struct Cli {
|
|
|
|
/// Print version
|
|
|
|
#[arg(short = 'v', short_alias = 'V', long, action = clap::builder::ArgAction::Version)]
|
|
|
|
version: (),
|
2024-03-04 20:18:49 +00:00
|
|
|
|
2024-07-12 23:09:37 +01:00
|
|
|
#[command(subcommand)]
|
2024-07-26 17:47:53 +01:00
|
|
|
subcommand: cli::commands::Subcommand,
|
2024-07-12 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
2024-07-28 17:19:54 +01:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn get_root(path: &std::path::Path) -> PathBuf {
|
|
|
|
match path.components().next().unwrap() {
|
|
|
|
std::path::Component::Prefix(prefix) => {
|
|
|
|
let mut string = prefix.as_os_str().to_string_lossy().to_string();
|
|
|
|
if string.ends_with(':') {
|
|
|
|
string.push(std::path::MAIN_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::path::PathBuf::from(&string)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn get_root(path: &std::path::Path) -> PathBuf {
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
|
|
|
|
let path = std::fs::canonicalize(path).unwrap();
|
|
|
|
let mut current = path.as_path();
|
|
|
|
|
|
|
|
while let Some(parent) = current.parent() {
|
|
|
|
if std::fs::metadata(parent).unwrap().dev() != std::fs::metadata(current).unwrap().dev() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
current.to_path_buf()
|
|
|
|
}
|
|
|
|
|
2024-07-26 17:47:53 +01:00
|
|
|
fn run() -> anyhow::Result<()> {
|
2024-08-12 00:17:26 +01:00
|
|
|
let cwd = std::env::current_dir().expect("failed to get current working directory");
|
|
|
|
|
2024-07-25 15:32:48 +01:00
|
|
|
#[cfg(windows)]
|
2024-07-26 17:47:53 +01:00
|
|
|
'scripts: {
|
2024-07-25 15:32:48 +01:00
|
|
|
let exe = std::env::current_exe().expect("failed to get current executable path");
|
2024-07-26 17:47:53 +01:00
|
|
|
if exe.parent().is_some_and(|parent| {
|
2024-08-12 00:17:26 +01:00
|
|
|
parent.file_name().is_some_and(|parent| parent != "bin")
|
2024-07-26 17:47:53 +01:00
|
|
|
|| parent
|
|
|
|
.parent()
|
2024-08-12 00:17:26 +01:00
|
|
|
.and_then(|parent| parent.file_name())
|
|
|
|
.is_some_and(|parent| parent != HOME_DIR)
|
2024-07-26 17:47:53 +01:00
|
|
|
}) {
|
|
|
|
break 'scripts;
|
|
|
|
}
|
|
|
|
|
2024-10-06 22:49:59 +01:00
|
|
|
let exe_name = exe.file_name().unwrap().to_string_lossy();
|
|
|
|
let exe_name = exe_name
|
|
|
|
.strip_suffix(std::env::consts::EXE_SUFFIX)
|
|
|
|
.unwrap_or(&exe_name);
|
2024-07-25 15:32:48 +01:00
|
|
|
|
2024-07-26 17:47:53 +01:00
|
|
|
if exe_name == env!("CARGO_BIN_NAME") {
|
|
|
|
break 'scripts;
|
|
|
|
}
|
2024-07-25 15:32:48 +01:00
|
|
|
|
2024-09-03 15:01:48 +01:00
|
|
|
// the bin script will search for the project root itself, so we do that to ensure
|
|
|
|
// consistency across platforms, since the script is executed using a shebang
|
|
|
|
// on unix systems
|
2024-07-26 17:47:53 +01:00
|
|
|
let status = std::process::Command::new("lune")
|
|
|
|
.arg("run")
|
2024-08-13 19:15:22 +01:00
|
|
|
.arg(exe.with_extension(""))
|
2024-09-03 15:01:48 +01:00
|
|
|
.arg("--")
|
2024-07-26 17:47:53 +01:00
|
|
|
.args(std::env::args_os().skip(1))
|
2024-09-03 15:01:48 +01:00
|
|
|
.current_dir(cwd)
|
2024-07-26 17:47:53 +01:00
|
|
|
.status()
|
|
|
|
.expect("failed to run lune");
|
2024-07-25 15:32:48 +01:00
|
|
|
|
2024-07-26 17:47:53 +01:00
|
|
|
std::process::exit(status.code().unwrap());
|
2024-07-25 15:32:48 +01:00
|
|
|
}
|
|
|
|
|
2024-09-03 15:01:48 +01:00
|
|
|
let (project_root_dir, project_workspace_dir) = 'finder: {
|
|
|
|
let mut current_path = Some(cwd.clone());
|
|
|
|
let mut project_root = None::<PathBuf>;
|
|
|
|
let mut workspace_dir = None::<PathBuf>;
|
|
|
|
|
|
|
|
fn get_workspace_members(path: &Path) -> anyhow::Result<HashSet<PathBuf>> {
|
|
|
|
let manifest = std::fs::read_to_string(path.join(MANIFEST_FILE_NAME))
|
|
|
|
.context("failed to read manifest")?;
|
|
|
|
let manifest: pesde::manifest::Manifest =
|
|
|
|
toml::from_str(&manifest).context("failed to parse manifest")?;
|
|
|
|
|
|
|
|
if manifest.workspace_members.is_empty() {
|
|
|
|
return Ok(HashSet::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest
|
|
|
|
.workspace_members
|
|
|
|
.iter()
|
|
|
|
.map(|member| path.join(member))
|
|
|
|
.map(|p| glob::glob(&p.to_string_lossy()))
|
|
|
|
.collect::<Result<Vec<_>, _>>()
|
|
|
|
.context("invalid glob patterns")?
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|paths| paths.into_iter())
|
|
|
|
.collect::<Result<HashSet<_>, _>>()
|
|
|
|
.context("failed to expand glob patterns")
|
|
|
|
}
|
|
|
|
|
|
|
|
while let Some(path) = current_path {
|
|
|
|
current_path = path.parent().map(|p| p.to_path_buf());
|
|
|
|
|
|
|
|
if !path.join(MANIFEST_FILE_NAME).exists() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match (project_root.as_ref(), workspace_dir.as_ref()) {
|
|
|
|
(Some(project_root), Some(workspace_dir)) => {
|
|
|
|
break 'finder (project_root.clone(), Some(workspace_dir.clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
(Some(project_root), None) => {
|
|
|
|
if get_workspace_members(&path)?.contains(project_root) {
|
|
|
|
workspace_dir = Some(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(None, None) => {
|
|
|
|
if get_workspace_members(&path)?.contains(&cwd) {
|
|
|
|
// initializing a new member of a workspace
|
|
|
|
break 'finder (cwd, Some(path));
|
|
|
|
} else {
|
|
|
|
project_root = Some(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(None, Some(_)) => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we mustn't expect the project root to be found, as that would
|
|
|
|
// disable the ability to run pesde in a non-project directory (for example to init it)
|
|
|
|
(project_root.unwrap_or_else(|| cwd.clone()), workspace_dir)
|
|
|
|
};
|
|
|
|
|
2024-07-23 00:20:50 +01:00
|
|
|
let multi = {
|
|
|
|
let logger = pretty_env_logger::formatted_builder()
|
|
|
|
.parse_env(pretty_env_logger::env_logger::Env::default().default_filter_or("info"))
|
|
|
|
.build();
|
|
|
|
let multi = MultiProgress::new();
|
|
|
|
|
|
|
|
LogWrapper::new(multi.clone(), logger).try_init().unwrap();
|
|
|
|
|
|
|
|
multi
|
|
|
|
};
|
2024-07-14 14:19:15 +01:00
|
|
|
|
2024-07-26 17:47:53 +01:00
|
|
|
let data_dir = home_dir()?.join("data");
|
|
|
|
create_dir_all(&data_dir).expect("failed to create data directory");
|
|
|
|
|
2024-07-28 17:19:54 +01:00
|
|
|
let home_cas_dir = data_dir.join("cas");
|
2024-08-11 17:29:48 +01:00
|
|
|
create_dir_all(&home_cas_dir).expect("failed to create cas directory");
|
2024-08-12 00:17:26 +01:00
|
|
|
let project_root = get_root(&project_root_dir);
|
2024-07-28 17:19:54 +01:00
|
|
|
let cas_dir = if get_root(&home_cas_dir) == project_root {
|
2024-10-14 18:40:02 +01:00
|
|
|
log::debug!("using home cas dir");
|
2024-07-28 17:19:54 +01:00
|
|
|
home_cas_dir
|
|
|
|
} else {
|
2024-10-14 18:40:02 +01:00
|
|
|
log::debug!("using cas dir in {}", project_root.display());
|
2024-07-28 17:19:54 +01:00
|
|
|
project_root.join(HOME_DIR).join("cas")
|
|
|
|
};
|
2024-07-26 17:47:53 +01:00
|
|
|
|
|
|
|
let project = Project::new(
|
2024-08-12 00:17:26 +01:00
|
|
|
project_root_dir,
|
2024-09-03 15:01:48 +01:00
|
|
|
project_workspace_dir,
|
2024-07-28 17:19:54 +01:00
|
|
|
data_dir,
|
|
|
|
cas_dir,
|
2024-10-14 18:40:02 +01:00
|
|
|
AuthConfig::new().with_tokens(get_tokens()?.0),
|
2024-07-26 17:47:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let reqwest = {
|
|
|
|
let mut headers = reqwest::header::HeaderMap::new();
|
|
|
|
|
|
|
|
headers.insert(
|
|
|
|
reqwest::header::ACCEPT,
|
|
|
|
"application/json"
|
|
|
|
.parse()
|
|
|
|
.context("failed to create accept header")?,
|
|
|
|
);
|
|
|
|
|
|
|
|
reqwest::blocking::Client::builder()
|
|
|
|
.user_agent(concat!(
|
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
"/",
|
|
|
|
env!("CARGO_PKG_VERSION")
|
|
|
|
))
|
|
|
|
.default_headers(headers)
|
|
|
|
.build()?
|
|
|
|
};
|
|
|
|
|
2024-09-28 23:37:38 +01:00
|
|
|
#[cfg(feature = "version-management")]
|
|
|
|
{
|
|
|
|
let target_version = project
|
|
|
|
.deser_manifest()
|
|
|
|
.ok()
|
|
|
|
.and_then(|manifest| manifest.pesde_version);
|
|
|
|
|
|
|
|
// store the current version in case it needs to be used later
|
|
|
|
get_or_download_version(&reqwest, ¤t_version())?;
|
|
|
|
|
|
|
|
let exe_path = if let Some(version) = target_version {
|
|
|
|
Some(get_or_download_version(&reqwest, &version)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let exe_path = if let Some(exe_path) = exe_path {
|
|
|
|
exe_path
|
|
|
|
} else {
|
|
|
|
get_or_download_version(&reqwest, &max_installed_version()?)?
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(exe_path) = exe_path {
|
|
|
|
let status = std::process::Command::new(exe_path)
|
|
|
|
.args(std::env::args_os().skip(1))
|
|
|
|
.status()
|
|
|
|
.expect("failed to run new version");
|
|
|
|
|
|
|
|
std::process::exit(status.code().unwrap());
|
|
|
|
}
|
2024-07-26 17:47:53 +01:00
|
|
|
|
2024-09-28 23:37:38 +01:00
|
|
|
display_err(check_for_updates(&reqwest), " while checking for updates");
|
2024-07-26 17:47:53 +01:00
|
|
|
}
|
2024-08-09 21:14:33 +01:00
|
|
|
|
2024-10-20 17:13:08 +01:00
|
|
|
let project_2 = project.clone();
|
|
|
|
let update_task = spawn(move || {
|
|
|
|
display_err(
|
|
|
|
update_repo_dependencies(&project_2),
|
|
|
|
" while updating repository dependencies",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
let cli = match Cli::try_parse() {
|
|
|
|
Ok(cli) => cli,
|
|
|
|
Err(err) => {
|
|
|
|
let _ = err.print();
|
|
|
|
update_task.join().expect("failed to join update task");
|
|
|
|
std::process::exit(err.exit_code());
|
|
|
|
}
|
|
|
|
};
|
2024-07-26 17:47:53 +01:00
|
|
|
|
2024-10-20 17:13:08 +01:00
|
|
|
cli.subcommand.run(project, multi, reqwest, update_task)
|
2024-07-26 17:47:53 +01:00
|
|
|
}
|
|
|
|
|
2024-09-03 22:08:08 +01:00
|
|
|
fn display_err(result: anyhow::Result<()>, prefix: &str) {
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("{}: {err}\n", format!("error{prefix}").red().bold());
|
2024-03-04 20:18:49 +00:00
|
|
|
|
2024-07-12 23:09:37 +01:00
|
|
|
let cause = err.chain().skip(1).collect::<Vec<_>>();
|
2024-03-04 20:18:49 +00:00
|
|
|
|
2024-07-12 23:09:37 +01:00
|
|
|
if !cause.is_empty() {
|
|
|
|
eprintln!("{}:", "caused by".red().bold());
|
|
|
|
for err in cause {
|
2024-07-17 18:38:01 +01:00
|
|
|
eprintln!(" - {err}");
|
2024-07-12 23:09:37 +01:00
|
|
|
}
|
|
|
|
}
|
2024-07-17 18:38:01 +01:00
|
|
|
|
|
|
|
let backtrace = err.backtrace();
|
|
|
|
match backtrace.status() {
|
|
|
|
std::backtrace::BacktraceStatus::Disabled => {
|
|
|
|
eprintln!(
|
|
|
|
"\n{}: set RUST_BACKTRACE=1 for a backtrace",
|
|
|
|
"help".yellow().bold()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
std::backtrace::BacktraceStatus::Captured => {
|
|
|
|
eprintln!("\n{}:\n{backtrace}", "backtrace".yellow().bold());
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
eprintln!("\n{}: not captured", "backtrace".yellow().bold());
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 22:08:08 +01:00
|
|
|
}
|
|
|
|
}
|
2024-07-17 18:38:01 +01:00
|
|
|
|
2024-09-03 22:08:08 +01:00
|
|
|
fn main() {
|
|
|
|
let result = run();
|
|
|
|
let is_err = result.is_err();
|
|
|
|
display_err(result, "");
|
|
|
|
if is_err {
|
2024-07-12 23:09:37 +01:00
|
|
|
std::process::exit(1);
|
2024-03-04 20:18:49 +00:00
|
|
|
}
|
|
|
|
}
|