Add path-absolutize crate and use it in build.rs and init.rs

This commit is contained in:
Moritz Hölting 2024-04-09 11:40:51 +02:00
parent 0432c8466e
commit 742d128030
3 changed files with 15 additions and 11 deletions

View File

@ -20,4 +20,5 @@ toml = "0.8.12"
shulkerscript-lang = {path = "../shulkerscript-lang", features = ["shulkerbox"], default-features = false} shulkerscript-lang = {path = "../shulkerscript-lang", features = ["shulkerbox"], default-features = false}
shulkerbox = {path = "../shulkerbox", default-features = false} shulkerbox = {path = "../shulkerbox", default-features = false}
git2 = { version = "0.18.3", default-features = false } git2 = { version = "0.18.3", default-features = false }
path-absolutize = "3.1.1"

View File

@ -1,3 +1,5 @@
use path_absolutize::Absolutize;
use crate::{ use crate::{
config::ProjectConfig, config::ProjectConfig,
error::{Error, Result}, error::{Error, Result},
@ -5,8 +7,6 @@ use crate::{
}; };
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use crate::util;
#[derive(Debug, clap::Args, Clone)] #[derive(Debug, clap::Args, Clone)]
pub struct BuildArgs { pub struct BuildArgs {
/// The path of the project to build. /// The path of the project to build.
@ -17,8 +17,10 @@ pub struct BuildArgs {
pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> { pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
let path = args.path.as_path(); let path = args.path.as_path();
let str_path = util::to_absolute_path(path)?; print_info(&format!(
print_info(&format!("Building project at {}", str_path)); "Building project at {}",
path.absolutize()?.display()
));
let toml_path = if !path.exists() { let toml_path = if !path.exists() {
print_error("The specified path does not exist."); print_error("The specified path does not exist.");
@ -61,7 +63,7 @@ pub fn build(_verbose: bool, args: &BuildArgs) -> Result<()> {
print_info(&format!( print_info(&format!(
"Finished building project to {}", "Finished building project to {}",
util::to_absolute_path(&dist_path)? dist_path.absolutize_from(path)?.display()
)); ));
Ok(()) Ok(())

View File

@ -7,12 +7,12 @@ use clap::ValueEnum;
use git2::{ use git2::{
IndexAddOption as GitIndexAddOption, Repository as GitRepository, Signature as GitSignature, IndexAddOption as GitIndexAddOption, Repository as GitRepository, Signature as GitSignature,
}; };
use path_absolutize::Absolutize;
use crate::{ use crate::{
config::ProjectConfig, config::ProjectConfig,
error::{Error, Result}, error::{Error, Result},
terminal_output::{print_error, print_info, print_success}, terminal_output::{print_error, print_info, print_success},
util::to_absolute_path,
}; };
#[derive(Debug, clap::Args, Clone)] #[derive(Debug, clap::Args, Clone)]
@ -32,6 +32,7 @@ pub struct InitArgs {
/// Force initialization even if the directory is not empty. /// Force initialization even if the directory is not empty.
#[clap(short, long)] #[clap(short, long)]
force: bool, force: bool,
/// The version control system to initialize.
#[clap(long, default_value = "git")] #[clap(long, default_value = "git")]
vcs: VersionControlSystem, vcs: VersionControlSystem,
} }
@ -118,7 +119,7 @@ fn create_pack_config(
if verbose { if verbose {
print_info(&format!( print_info(&format!(
"Created pack.toml file at {}.", "Created pack.toml file at {}.",
to_absolute_path(&path)? path.absolutize()?.display()
)); ));
} }
Ok(()) Ok(())
@ -130,7 +131,7 @@ fn create_dir(path: &Path, verbose: bool) -> std::io::Result<()> {
if verbose { if verbose {
print_info(&format!( print_info(&format!(
"Created directory at {}.", "Created directory at {}.",
to_absolute_path(path)? path.absolutize()?.display()
)); ));
} }
} }
@ -143,7 +144,7 @@ fn create_gitignore(path: &Path, verbose: bool) -> std::io::Result<()> {
if verbose { if verbose {
print_info(&format!( print_info(&format!(
"Created .gitignore file at {}.", "Created .gitignore file at {}.",
to_absolute_path(&gitignore)? gitignore.absolutize()?.display()
)); ));
} }
Ok(()) Ok(())
@ -155,7 +156,7 @@ fn create_pack_png(path: &Path, verbose: bool) -> std::io::Result<()> {
if verbose { if verbose {
print_info(&format!( print_info(&format!(
"Created pack.png file at {}.", "Created pack.png file at {}.",
to_absolute_path(&pack_png)? pack_png.absolutize()?.display()
)); ));
} }
Ok(()) Ok(())
@ -173,7 +174,7 @@ fn create_main_file(path: &Path, namespace: &str, verbose: bool) -> std::io::Res
if verbose { if verbose {
print_info(&format!( print_info(&format!(
"Created main.shu file at {}.", "Created main.shu file at {}.",
to_absolute_path(&main_file)? main_file.absolutize()?.display()
)); ));
} }
Ok(()) Ok(())