Update manifest handling and integrate with command_init

Introduced a new 'manifest' module to manage manifest files and updated 'command_init' to utilize this module, replacing previously hard-coded functionalities. Added 'manifest' package dependencies to Cargo.lock and command_init's Cargo.toml files.
This commit is contained in:
Ian Wijma 2024-01-05 21:15:34 +11:00
parent b3d7c776e8
commit 63d01ea03c
5 changed files with 158 additions and 9 deletions

77
Cargo.lock generated
View File

@ -113,6 +113,12 @@ dependencies = [
"manifest", "manifest",
] ]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.8" version = "0.3.8"
@ -123,12 +129,34 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.4.1" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "indexmap"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.151" version = "0.2.151"
@ -144,6 +172,10 @@ checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]] [[package]]
name = "manifest" name = "manifest"
version = "0.1.0" version = "0.1.0"
dependencies = [
"serde",
"serde_yaml",
]
[[package]] [[package]]
name = "mccli" name = "mccli"
@ -184,6 +216,45 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "ryu"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]]
name = "serde"
version = "1.0.194"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.194"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_yaml"
version = "0.9.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1bf28c79a99f70ee1f1d83d10c875d2e70618417fda01ad1785e027579d9d38"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
@ -232,6 +303,12 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]]
name = "unsafe-libyaml"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b"
[[package]] [[package]]
name = "utf8parse" name = "utf8parse"
version = "0.2.1" version = "0.2.1"

View File

@ -11,6 +11,10 @@ pub struct Arguments {
pub fn execute (_arguments: &Arguments) { pub fn execute (_arguments: &Arguments) {
let directory: PathBuf = PathBuf::from(_arguments.directory.clone()); let directory: PathBuf = PathBuf::from(_arguments.directory.clone());
let manifest = Manifest::new(&directory); let manifest = Manifest::from(&directory);
manifest.init();
match manifest.init() {
true => println!("Project initialized"),
false => println!("Project already initialized")
}
} }

View File

@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serde = {version = "1.0", features = ["derive"]}
serde_yaml = "0.9"

View File

@ -1,13 +1,17 @@
use std::error::Error;
use std::fs; use std::fs;
use std::path::{PathBuf}; use std::path::{PathBuf};
use crate::manifest_file::ManifestFile;
mod manifest_file;
#[derive(Debug)] #[derive(Debug)]
pub struct Manifest { pub struct Manifest {
directory: PathBuf directory: PathBuf,
} }
impl Manifest { impl Manifest {
pub fn new(path: &PathBuf) -> Manifest { pub fn from(path: &PathBuf) -> Manifest {
let mut directory: PathBuf = path.into(); let mut directory: PathBuf = path.into();
if path.is_file() { if path.is_file() {
@ -24,10 +28,35 @@ impl Manifest {
} }
pub fn init(&self) -> bool { pub fn init(&self) -> bool {
let directory: PathBuf = self.directory.clone(); let file: PathBuf = self.file_path();
println!("init: {:?}", directory); if !file.exists() {
let manifest_file: ManifestFile = ManifestFile::from(self.directory_name());
true return match manifest_file.write(&file) {
Ok(_) => true,
Err(_) => panic!("Unable to write to file: {:?}", &file)
}
}
false
}
pub fn file_path(&self) -> PathBuf {
return PathBuf::from(format!("{}/{}", self.directory.to_string_lossy(), "mccli.yaml"));
}
pub fn directory_path(&self) -> PathBuf {
return PathBuf::from(&self.directory);
}
pub fn directory_name(&self) -> String {
let directory = self.directory_path();
return directory
.file_name()
.unwrap()
.to_string_lossy()
.to_string();
} }
} }

View File

@ -0,0 +1,37 @@
use std::error::Error;
use std::fs;
use std::fs::File;
use serde::{Serialize, Deserialize};
use std::path::PathBuf;
use serde::de::DeserializeOwned;
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ManifestFile {
name: String,
}
impl ManifestFile {
pub fn from(name: String) -> ManifestFile {
return ManifestFile { name }
}
pub fn read<Content: DeserializeOwned>(path: &PathBuf) -> Result<Content, Box<dyn Error>> {
let file = File::open(path)?;
let data: Content = serde_yaml::from_reader(file)?;
Ok(data)
}
pub fn write(&self, path: &PathBuf) -> Result<bool, Box<dyn Error>> {
if path.exists() {
fs::remove_file(path)?;
}
let file = File::create(path)?;
serde_yaml::to_writer(file, self)?;
Ok(true)
}
}