Skip to content

Commit

Permalink
Update to support flavours and modulefile generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed May 28, 2024
1 parent f51c997 commit b2db802
Show file tree
Hide file tree
Showing 17 changed files with 476 additions and 124 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sccmod"
authors = ["Toby Davis \"Pencilcaseman\""]
version = "0.1.11"
version = "0.1.12"
edition = "2021"
readme = "README.md"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions src/builders/builder_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pyo3::{Bound, PyAny};
use std::fmt::Debug;
use std::path::Path;

pub trait BuilderImpl: Sized {
pub trait BuilderImpl: Sized + Clone {
/// Generate a builder object from a python object.
///
/// Returns [`Ok(obj)`] if the operation was successful, otherwise [`Err(string)`]
Expand Down Expand Up @@ -56,7 +56,7 @@ pub trait BuilderImpl: Sized {
) -> Result<(), String>;
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Builder {
CMake(CMake),
Make(Make),
Expand Down
4 changes: 2 additions & 2 deletions src/builders/cmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use pyo3::prelude::PyAnyMethods;
use pyo3::{Bound, PyAny};
use std::{fs, path, path::Path};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum CMakeBuildType {
Debug,
Release,
RelWithDebInfo,
MinSizeRel,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CMake {
pub build_type: CMakeBuildType,
pub jobs: usize,
Expand Down
2 changes: 1 addition & 1 deletion src/builders/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::PyAnyMethods;
use pyo3::{Bound, PyAny};
use std::{fs, path, path::Path, process::Command};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Make {
pub configure: bool,
pub jobs: usize,
Expand Down
20 changes: 18 additions & 2 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn resolver_boilerplate(

err.push_str(&format!(
" {index_str}: {}\n",
item.identifier.bold().cyan()
item.identifier().bold().cyan()
));
}

Expand Down Expand Up @@ -86,6 +86,22 @@ pub fn resolver_boilerplate(
}
}

pub fn info(config: &config::Config) -> Result<(), String> {
fn fmt<T: std::fmt::Debug>(name: &str, value: &T) {
// println!("{}", format!("{name} {value:?}").bold().purple());
println!("{} {}", name.bold().purple(), format!("{value:?}").cyan());
}

fmt("config file . . . . :", &std::env::var("SCCMOD_CONFIG"));
fmt("sccmod_module_paths :", &config.sccmod_module_paths);
fmt("modulefile root . . :", &config.modulefile_root);
fmt("build_root . . . . :", &config.build_root);
fmt("install_root . . . :", &config.install_root);
fmt("shell . . . . . . . :", &config.shell);

Ok(())
}

/// A callback function to list all available modules
///
/// # Errors
Expand All @@ -95,7 +111,7 @@ pub fn list_callback(_config: &config::Config) -> Result<(), String> {
println!("{}", "Available Modules:".bold().purple());

for p in &get_modules()? {
println!(" > {}", p.identifier.bold().cyan());
println!(" > {}", p.identifier().bold().cyan());
}

Ok(())
Expand Down
14 changes: 11 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::module::Module;
use std::fs;
use toml::Table;

#[derive(Debug)]
pub struct Config {
pub module_paths: Vec<String>,
pub sccmod_module_paths: Vec<String>,
pub modulefile_root: String,
pub build_root: String,
pub install_root: String,
pub shell: String,
Expand Down Expand Up @@ -33,7 +35,7 @@ pub fn read() -> Result<Config, String> {

let table = config.parse::<Table>().map_err(|err| err.to_string())?;

let module_paths: Vec<String> = table["module_paths"]
let sccmod_module_paths: Vec<String> = table["sccmod_module_paths"]
.as_array()
.ok_or_else(|| "`module_paths` must be an array of strings".to_string())
.and_then(|paths| {
Expand All @@ -47,6 +49,11 @@ pub fn read() -> Result<Config, String> {
.collect()
})?;

let modulefile_root: String = table["modulefile_root"]
.as_str()
.ok_or_else(|| "`modulefile_root` must be a string".to_string())?
.to_string();

let build_root: String = table["build_root"]
.as_str()
.ok_or_else(|| "`build_root` must be a string".to_string())?
Expand All @@ -63,7 +70,8 @@ pub fn read() -> Result<Config, String> {
.to_string();

Ok(Config {
module_paths,
sccmod_module_paths,
modulefile_root,
build_root,
install_root,
shell,
Expand Down
4 changes: 2 additions & 2 deletions src/downloaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{fs, path::Path, process::Command};

const FILE_NAME: &str = "curl_download_result";

pub trait DownloaderImpl: Sized {
pub trait DownloaderImpl: Sized + Clone {
/// Convert from a Python `Downloader` instance to a Rust [`Downloader`] instance.
/// If this is not possible (due to an invalid value, for example), [`Err`] is returned
/// containing an error message as a [`String`]
Expand Down Expand Up @@ -266,7 +266,7 @@ impl DownloaderImpl for Curl {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Downloader {
GitClone(GitClone),
Curl(Curl),
Expand Down
75 changes: 75 additions & 0 deletions src/flavours.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::module::{get_modules, Dependency, Module};

pub fn generate(module: &Module) -> Result<Vec<(Vec<Module>, usize)>, String> {
let modules = get_modules()?;

// 1. Extract dependent modules and classes
let required_modules: Vec<&Module> = module
.dependencies
.iter()
.filter_map(|dep| {
if let Dependency::Module(name) = dep {
Some(name)
} else {
None
}
})
.map(|name| {
modules
.iter()
.find(|m| &m.identifier() == name)
.ok_or(format!(
"Failed to find module matching dependency '{name}'"
))
})
.collect::<Result<Vec<&Module>, String>>()?;

let required_classes: Vec<String> = module
.dependencies
.iter()
.filter_map(|dep| {
if let Dependency::Class(name) = dep {
Some(name.to_owned())
} else {
None
}
})
.collect();

// 2. Extract modules with matching class
let available_per_class: Vec<Vec<&Module>> = required_classes
.iter()
.map(|class| modules.iter().filter(|m| &m.class == class).collect())
.collect();

// 3. Generate permutations of the classes
let mut index = vec![0usize; required_classes.len() + 1];

let mut permutations = Vec::new();

let end = required_classes.len();
while index[end] == 0 {
let mut perm: Vec<Module> = (0..end)
.zip(index.iter().enumerate())
.map(|(_, (class, &idx))| available_per_class[class][idx].to_owned())
.collect();

// Add pre-defined modules
perm.extend(required_modules.iter().map(|&m| m.to_owned()));

let permutation = (perm, required_classes.len());

permutations.push(permutation);

index[0] += 1;

let mut i = 0;
while i < end && index[i] >= available_per_class[i].len() {
index[i] = 0;
index[i + 1] += 1;
i += 1;
}
}

Ok(permutations)
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod config;
pub mod downloaders;
pub mod environment;
pub mod file_manager;
pub mod flavours;
pub mod info;
pub mod log;
pub mod module;
Expand Down
16 changes: 10 additions & 6 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use colored::Colorize;
use std::io::Write;

fn remove_tabs(txt: &str) -> String {
txt.replace('\t', " ")
}

/// Print `message` as an error to the console and panic
#[allow(clippy::missing_panics_doc)]
pub fn error(message: &str) -> ! {
println!(
"{} : {}",
"SCCMod Err".bold().truecolor(255, 0, 0),
message.italic().truecolor(255, 100, 25)
remove_tabs(message).italic().truecolor(255, 100, 25)
);
panic!("An error occurred");
}
Expand All @@ -17,7 +21,7 @@ pub fn warn(message: &str) {
println!(
"{}: {}",
"SCCMod Warn".bold().truecolor(255, 255, 0),
message.italic().truecolor(225, 225, 50)
remove_tabs(message).italic().truecolor(225, 225, 50)
);
}

Expand All @@ -26,7 +30,7 @@ pub fn info(message: &str) {
println!(
"{}: {}",
"SCCMod Info".bold().truecolor(50, 150, 255),
message.italic().truecolor(50, 150, 255)
remove_tabs(message).italic().truecolor(50, 150, 255)
);
}

Expand All @@ -35,7 +39,7 @@ pub fn status(message: &str) {
println!(
"{}: {}",
"SCCMod Status".bold().truecolor(200, 65, 215),
message.italic().truecolor(230, 55, 235)
remove_tabs(message).italic().truecolor(230, 55, 235)
);
}

Expand All @@ -53,7 +57,7 @@ pub fn info_carriage(message: &str) {
print!(
"{}: {}\r",
"SCCMod Info".bold().truecolor(50, 150, 255),
message.italic().truecolor(50, 150, 255)
remove_tabs(message).italic().truecolor(50, 150, 255)
);

std::io::stdout().flush().unwrap();
Expand All @@ -73,7 +77,7 @@ pub fn warn_carriage(message: &str) {
print!(
"{}: {}\r",
"SCCMod Warn".bold().truecolor(255, 255, 0),
message.italic().truecolor(225, 225, 50)
remove_tabs(message).italic().truecolor(225, 225, 50)
);

std::io::stdout().flush().unwrap();
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn main() -> Result<(), String> {
// println!("{:?}", shell.exec());

let config = config::read()?;

// println!("Module Paths: {:?}", config.module_paths);
// println!("Build Root: {}", config.build_root);
// println!("Install Root: {}", config.install_root);
Expand All @@ -22,6 +23,13 @@ fn cli(config: &config::Config) -> Result<(), String> {
let command = cli::Command {
name: "sccmod",
subcommands: vec![
cli::Command {
name: "info",
subcommands: Vec::new(),
arguments: Vec::new(),
help: "Print sccmod information",
callback: Some(callbacks::info),
},
cli::Command {
name: "list",
subcommands: Vec::new(),
Expand Down
Loading

0 comments on commit b2db802

Please sign in to comment.