feat: support toolchain managers in scripts

This commit is contained in:
daimond113 2024-08-12 01:17:26 +02:00
parent 08e808b4b9
commit 6442030f93
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
6 changed files with 63 additions and 33 deletions

View file

@ -8,7 +8,7 @@ use anyhow::Context;
use clap::Args; use clap::Args;
use indicatif::MultiProgress; use indicatif::MultiProgress;
use pesde::{lockfile::Lockfile, manifest::target::TargetKind, Project}; use pesde::{lockfile::Lockfile, manifest::target::TargetKind, Project, MANIFEST_FILE_NAME};
use crate::cli::{bin_dir, files::make_executable, IsUpToDate}; use crate::cli::{bin_dir, files::make_executable, IsUpToDate};
@ -48,9 +48,20 @@ fn bin_link_file(alias: &str) -> String {
format!( format!(
r#"{prefix}local process = require("@lune/process") r#"{prefix}local process = require("@lune/process")
local fs = require("@lune/fs") local fs = require("@lune/fs")
local project_root = process.cwd
local path_components = string.split(string.gsub(project_root, "\\", "/"), "/")
for i = #path_components, 1, -1 do
local path = table.concat(path_components, "/", 1, i)
if fs.isFile(path .. "/{MANIFEST_FILE_NAME}") then
project_root = path
break
end
end
for _, packages_folder in {{ {all_folders} }} do for _, packages_folder in {{ {all_folders} }} do
local path = `{{process.cwd}}/{{packages_folder}}/{alias}.bin.luau` local path = `{{project_root}}/{{packages_folder}}/{alias}.bin.luau`
if fs.isFile(path) then if fs.isFile(path) then
require(path) require(path)

View file

@ -1,9 +1,3 @@
use std::{
collections::BTreeMap,
fs::create_dir_all,
path::{Path, PathBuf},
};
use crate::{ use crate::{
linking::generator::get_file_types, linking::generator::get_file_types,
lockfile::DownloadedGraph, lockfile::DownloadedGraph,
@ -13,6 +7,12 @@ use crate::{
source::{fs::store_in_cas, traits::PackageRef, version_id::VersionId}, source::{fs::store_in_cas, traits::PackageRef, version_id::VersionId},
Project, LINK_LIB_NO_FILE_FOUND, PACKAGES_CONTAINER_NAME, Project, LINK_LIB_NO_FILE_FOUND, PACKAGES_CONTAINER_NAME,
}; };
use std::{
collections::BTreeMap,
ffi::OsStr,
fs::create_dir_all,
path::{Path, PathBuf},
};
/// Generates linking modules for a project /// Generates linking modules for a project
pub mod generator; pub mod generator;
@ -100,8 +100,9 @@ impl Project {
execute_script( execute_script(
ScriptName::RobloxSyncConfigGenerator, ScriptName::RobloxSyncConfigGenerator,
&script_path.to_path(self.path()), &script_path.to_path(self.path()),
build_files, std::iter::once(container_folder.as_os_str())
&container_folder, .chain(build_files.iter().map(OsStr::new)),
self,
false, false,
) )
.map_err(|e| { .map_err(|e| {

View file

@ -1,3 +1,13 @@
use std::{fs::create_dir_all, path::PathBuf};
use anyhow::Context;
use clap::Parser;
use colored::Colorize;
use indicatif::MultiProgress;
use indicatif_log_bridge::LogWrapper;
use pesde::{AuthConfig, Project, MANIFEST_FILE_NAME};
use crate::cli::{ use crate::cli::{
auth::get_token, auth::get_token,
home_dir, home_dir,
@ -5,13 +15,6 @@ use crate::cli::{
version::{check_for_updates, current_version, get_or_download_version, max_installed_version}, version::{check_for_updates, current_version, get_or_download_version, max_installed_version},
HOME_DIR, HOME_DIR,
}; };
use anyhow::Context;
use clap::Parser;
use colored::Colorize;
use indicatif::MultiProgress;
use indicatif_log_bridge::LogWrapper;
use pesde::{AuthConfig, Project};
use std::{fs::create_dir_all, path::PathBuf};
mod cli; mod cli;
pub mod util; pub mod util;
@ -62,14 +65,30 @@ fn get_root(path: &std::path::Path) -> PathBuf {
} }
fn run() -> anyhow::Result<()> { fn run() -> anyhow::Result<()> {
let cwd = std::env::current_dir().expect("failed to get current working directory");
let project_root_dir = 'finder: {
let mut project_root = cwd.clone();
while project_root.components().count() > 1 {
if project_root.join(MANIFEST_FILE_NAME).exists() {
break 'finder project_root;
}
project_root = project_root.parent().unwrap().to_path_buf();
}
cwd.clone()
};
#[cfg(windows)] #[cfg(windows)]
'scripts: { 'scripts: {
let exe = std::env::current_exe().expect("failed to get current executable path"); let exe = std::env::current_exe().expect("failed to get current executable path");
if exe.parent().is_some_and(|parent| { if exe.parent().is_some_and(|parent| {
parent.as_os_str() != "bin" parent.file_name().is_some_and(|parent| parent != "bin")
|| parent || parent
.parent() .parent()
.is_some_and(|parent| parent.as_os_str() != HOME_DIR) .and_then(|parent| parent.file_name())
.is_some_and(|parent| parent != HOME_DIR)
}) { }) {
break 'scripts; break 'scripts;
} }
@ -85,7 +104,7 @@ fn run() -> anyhow::Result<()> {
.arg("run") .arg("run")
.arg(exe.with_extension("luau")) .arg(exe.with_extension("luau"))
.args(std::env::args_os().skip(1)) .args(std::env::args_os().skip(1))
.current_dir(std::env::current_dir().unwrap()) .current_dir(project_root_dir)
.status() .status()
.expect("failed to run lune"); .expect("failed to run lune");
@ -103,8 +122,6 @@ fn run() -> anyhow::Result<()> {
multi multi
}; };
let cwd = std::env::current_dir().expect("failed to get current working directory");
let data_dir = home_dir()?.join("data"); let data_dir = home_dir()?.join("data");
create_dir_all(&data_dir).expect("failed to create data directory"); create_dir_all(&data_dir).expect("failed to create data directory");
@ -112,7 +129,7 @@ fn run() -> anyhow::Result<()> {
let home_cas_dir = data_dir.join("cas"); let home_cas_dir = data_dir.join("cas");
create_dir_all(&home_cas_dir).expect("failed to create cas directory"); create_dir_all(&home_cas_dir).expect("failed to create cas directory");
let project_root = get_root(&cwd); let project_root = get_root(&project_root_dir);
let cas_dir = if get_root(&home_cas_dir) == project_root { let cas_dir = if get_root(&home_cas_dir) == project_root {
home_cas_dir home_cas_dir
} else { } else {
@ -120,7 +137,7 @@ fn run() -> anyhow::Result<()> {
}; };
let project = Project::new( let project = Project::new(
cwd, project_root_dir,
data_dir, data_dir,
cas_dir, cas_dir,
AuthConfig::new().with_github_token(token.as_ref()), AuthConfig::new().with_github_token(token.as_ref()),

View file

@ -203,7 +203,7 @@ impl Project {
.cloned() .cloned()
else { else {
return Err(Box::new(errors::DependencyGraphError::NoMatchingVersion( return Err(Box::new(errors::DependencyGraphError::NoMatchingVersion(
format!("{specifier} ({})", manifest.target.kind()), format!("{specifier} ({target})"),
))); )));
}; };

View file

@ -1,3 +1,4 @@
use crate::Project;
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
fmt::{Display, Formatter}, fmt::{Display, Formatter},
@ -29,11 +30,11 @@ impl Display for ScriptName {
} }
} }
pub(crate) fn execute_script<A: IntoIterator<Item = S>, S: AsRef<OsStr>, P: AsRef<Path>>( pub(crate) fn execute_script<A: IntoIterator<Item = S>, S: AsRef<OsStr>>(
script_name: ScriptName, script_name: ScriptName,
script_path: &Path, script_path: &Path,
args: A, args: A,
cwd: P, project: &Project,
return_stdout: bool, return_stdout: bool,
) -> Result<Option<String>, std::io::Error> { ) -> Result<Option<String>, std::io::Error> {
match Command::new("lune") match Command::new("lune")
@ -41,7 +42,7 @@ pub(crate) fn execute_script<A: IntoIterator<Item = S>, S: AsRef<OsStr>, P: AsRe
.arg(script_path.as_os_str()) .arg(script_path.as_os_str())
.arg("--") .arg("--")
.args(args) .args(args)
.current_dir(cwd) .current_dir(project.path())
.stdin(Stdio::inherit()) .stdin(Stdio::inherit())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())

View file

@ -19,7 +19,7 @@ struct SourcemapNode {
pub(crate) fn find_lib_path( pub(crate) fn find_lib_path(
project: &Project, project: &Project,
cwd: &Path, package_dir: &Path,
) -> Result<Option<RelativePathBuf>, errors::FindLibPathError> { ) -> Result<Option<RelativePathBuf>, errors::FindLibPathError> {
let manifest = project.deser_manifest()?; let manifest = project.deser_manifest()?;
@ -34,12 +34,12 @@ pub(crate) fn find_lib_path(
let result = execute_script( let result = execute_script(
ScriptName::SourcemapGenerator, ScriptName::SourcemapGenerator,
&script_path.to_path(&project.path), &script_path.to_path(&project.path),
["--wally"], [package_dir],
cwd, project,
true, true,
)?; )?;
if let Some(result) = result { if let Some(result) = result.filter(|result| !result.is_empty()) {
let node: SourcemapNode = serde_json::from_str(&result)?; let node: SourcemapNode = serde_json::from_str(&result)?;
Ok(node.file_paths.into_iter().find(|path| { Ok(node.file_paths.into_iter().find(|path| {
path.extension() path.extension()