Skip to content

Commit

Permalink
Ran cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilith-In-Starlight committed Jan 18, 2024
1 parent 52ed58e commit c9c0cfd
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 222 deletions.
339 changes: 174 additions & 165 deletions src/compile.rs
Original file line number Diff line number Diff line change
@@ -1,185 +1,194 @@
use std::{io::{self, Write}, fs::{self, File}, path::{PathBuf, Path}};

use kismesis::{Kismesis, KisID, plugins, options::Settings, html, parser::errors::Err, reporting::{self, draw_error, DrawingInfo, ReportKind}, KismesisError};
use std::{
fs::{self, File},
io::{self, Write},
path::{Path, PathBuf},
};

use kismesis::{
html,
options::Settings,
parser::errors::Err,
plugins,
reporting::{self, draw_error, DrawingInfo, ReportKind},
KisID, Kismesis, KismesisError,
};

/// An Enum containing all the possible errors that the process of compiling a project might emit
pub enum Error {
IO(io::Error, PathBuf),
NoMainTemplate,
OutputNotInOutputFolder(PathBuf),
TemplateInOutputFolder(PathBuf),
Parse(Vec<Err>, KisID),
TriedToGetNonExistentTemplate(KisID),
IO(io::Error, PathBuf),
NoMainTemplate,
OutputNotInOutputFolder(PathBuf),
TemplateInOutputFolder(PathBuf),
Parse(Vec<Err>, KisID),
TriedToGetNonExistentTemplate(KisID),
}

/// Loads all the plugins in the plugins directory
#[cfg(feature = "plugins")]
fn check_for_plugins(program_path: &directories::ProjectDirs, engine: &mut Kismesis) {
let plugin_dir = program_path.data_dir().join("plugins");
let plugin_paths = fs::read_dir(plugin_dir).unwrap();
for entry in plugin_paths {
let entry = entry.unwrap();
let path = entry.path();
let data = path.join("plugin.ron");
let data = ron::from_str::<plugins::PluginData>(&fs::read_to_string(data).unwrap())
.unwrap();
let plugin_path = path.join("plugin.wasm");
engine.register_plugin(data.name, &plugin_path);
}
let plugin_dir = program_path.data_dir().join("plugins");
let plugin_paths = fs::read_dir(plugin_dir).unwrap();
for entry in plugin_paths {
let entry = entry.unwrap();
let path = entry.path();
let data = path.join("plugin.ron");
let data =
ron::from_str::<plugins::PluginData>(&fs::read_to_string(data).unwrap()).unwrap();
let plugin_path = path.join("plugin.wasm");
engine.register_plugin(data.name, &plugin_path);
}
}

/// Loads all the plugins in the plugins directory
#[cfg(not(feature = "plugins"))]
fn check_for_plugins(program_path: &directories::ProjectDirs, engine: &mut Kismesis) {
println!("Plugins are not being registered because this version of Kismesis was compiled without plugins")
println!("Plugins are not being registered because this version of Kismesis was compiled without plugins")
}

/// Compile a kismesis project
pub(crate) fn compile_project() {
let mut errors = Vec::new();
let mut engine = Kismesis::new();
let program_path =
directories::ProjectDirs::from("net.ampersandia", "ampersandia", "kismesis").unwrap();

check_for_plugins(&program_path, &mut engine);

let project_path = std::env::current_dir().unwrap();

let main_template_path = PathBuf::from("templates/main.ks");
let template_paths = recursive_crawl(&PathBuf::from("templates")).0;

for path in template_paths {
match engine.register_file(path, Some(project_path.clone())) {
Ok(x) => {
engine.register_template(x);
}
Err(x) => errors.push(x.into()),
}
}

if !errors.is_empty() {
report_errors(errors, &engine);
return;
}

let Some(main_template_id) = engine.verify_template_id(main_template_path) else {
errors.push(Error::NoMainTemplate);
report_errors(errors, &engine);
return;
};
let input_paths = recursive_crawl(&PathBuf::from("input")).0;

let settings = Settings::new();
for path in input_paths {
let parsed_file = match engine.register_file(path, Some(project_path.clone())) {
Ok(mut x) => {
x.template = Some(main_template_id.clone());
x
}
Err(x) => {
errors.push(x.into());
continue;
}
};
match html::generate_html(&parsed_file, vec![], &settings, &engine) {
Ok(x) => {
let output_path = PathBuf::from("output");
let file = match engine.get_file(parsed_file.file_id) {
Some(x) => x,
None => {
errors.push(Error::TriedToGetNonExistentTemplate(parsed_file.file_id));
return;
}
};
if let Some(path) = &file.path {
let mut output_path =
output_path.join::<PathBuf>(path.iter().skip(1).collect());
output_path.set_extension("html");
match output_path.parent() {
Some(parent) => match std::fs::create_dir_all(parent) {
Ok(_) => (),
Err(x) => {
errors.push(Error::IO(x, output_path.clone()));
continue;
}
},
None => errors.push(Error::OutputNotInOutputFolder(output_path.clone())),
};
let mut file = match File::create(&output_path) {
Ok(x) => x,
Err(x) => {
errors.push(Error::IO(x, output_path.clone()));
continue;
}
};
let file_text = match x.to_string() {
Ok(x) => x,
Err(_) => {
errors.push(Error::TemplateInOutputFolder(path.clone()));
continue;
}
};
match write!(file, "{}", file_text) {
Ok(x) => {
engine.drop_id(&parsed_file.file_id);
x
}
Err(x) => {
errors.push(Error::IO(x, output_path.clone()));
continue;
}
};
}
}
Err(errors) => {
for error in errors {
eprintln!("{}", reporting::draw_scoped_error(&error, &engine));
}
}
}
}

if !errors.is_empty() {
report_errors(errors, &engine)
}
}
let mut errors = Vec::new();
let mut engine = Kismesis::new();
let program_path =
directories::ProjectDirs::from("net.ampersandia", "ampersandia", "kismesis").unwrap();

check_for_plugins(&program_path, &mut engine);

let project_path = std::env::current_dir().unwrap();

let main_template_path = PathBuf::from("templates/main.ks");
let template_paths = recursive_crawl(&PathBuf::from("templates")).0;

for path in template_paths {
match engine.register_file(path, Some(project_path.clone())) {
Ok(x) => {
engine.register_template(x);
}
Err(x) => errors.push(x.into()),
}
}

if !errors.is_empty() {
report_errors(errors, &engine);
return;
}

let Some(main_template_id) = engine.verify_template_id(main_template_path) else {
errors.push(Error::NoMainTemplate);
report_errors(errors, &engine);
return;
};
let input_paths = recursive_crawl(&PathBuf::from("input")).0;

let settings = Settings::new();
for path in input_paths {
let parsed_file = match engine.register_file(path, Some(project_path.clone())) {
Ok(mut x) => {
x.template = Some(main_template_id.clone());
x
}
Err(x) => {
errors.push(x.into());
continue;
}
};
match html::generate_html(&parsed_file, vec![], &settings, &engine) {
Ok(x) => {
let output_path = PathBuf::from("output");
let file = match engine.get_file(parsed_file.file_id) {
Some(x) => x,
None => {
errors.push(Error::TriedToGetNonExistentTemplate(parsed_file.file_id));
return;
}
};
if let Some(path) = &file.path {
let mut output_path =
output_path.join::<PathBuf>(path.iter().skip(1).collect());
output_path.set_extension("html");
match output_path.parent() {
Some(parent) => match std::fs::create_dir_all(parent) {
Ok(_) => (),
Err(x) => {
errors.push(Error::IO(x, output_path.clone()));
continue;
}
},
None => errors.push(Error::OutputNotInOutputFolder(output_path.clone())),
};
let mut file = match File::create(&output_path) {
Ok(x) => x,
Err(x) => {
errors.push(Error::IO(x, output_path.clone()));
continue;
}
};
let file_text = match x.to_string() {
Ok(x) => x,
Err(_) => {
errors.push(Error::TemplateInOutputFolder(path.clone()));
continue;
}
};
match write!(file, "{}", file_text) {
Ok(x) => {
engine.drop_id(&parsed_file.file_id);
x
}
Err(x) => {
errors.push(Error::IO(x, output_path.clone()));
continue;
}
};
}
}
Err(errors) => {
for error in errors {
eprintln!("{}", reporting::draw_scoped_error(&error, &engine));
}
}
}
}

if !errors.is_empty() {
report_errors(errors, &engine)
}
}

/// Recursively get all Kismesis files
pub fn recursive_crawl(path: &Path) -> (Vec<PathBuf>, Vec<io::Error>) {
let mut errors = Vec::new();
let mut paths = Vec::new();
let entries = match fs::read_dir(path) {
Ok(x) => x,
Err(x) => return (vec![], vec![x]),
};
for entry in entries {
let entry = match entry {
Ok(x) => x,
Err(x) => {
errors.push(x);
continue;
}
};
let path = entry.path();
if path.is_dir() {
let (mut a, mut b) = recursive_crawl(&path);
errors.append(&mut b);
paths.append(&mut a)
} else if let Some(ext) = path.extension() {
if ext.to_string_lossy() == "ks" {
paths.push(path)
}
}
}

(paths, errors)
let mut errors = Vec::new();
let mut paths = Vec::new();
let entries = match fs::read_dir(path) {
Ok(x) => x,
Err(x) => return (vec![], vec![x]),
};
for entry in entries {
let entry = match entry {
Ok(x) => x,
Err(x) => {
errors.push(x);
continue;
}
};
let path = entry.path();
if path.is_dir() {
let (mut a, mut b) = recursive_crawl(&path);
errors.append(&mut b);
paths.append(&mut a)
} else if let Some(ext) = path.extension() {
if ext.to_string_lossy() == "ks" {
paths.push(path)
}
}
}

(paths, errors)
}

pub fn report_errors(errors: Vec<Error>, engine: &Kismesis) {

for error in errors {
match error {
for error in errors {
match error {
Error::IO(error, path) => eprintln!("Error reading `{}`: {}", path.to_string_lossy(), error),
Error::NoMainTemplate => eprintln!("Coudln't compile project because it doesn't have a template in templates/main.ks"),
Error::OutputNotInOutputFolder(path) => eprintln!("Tried to output {} to a location outside the project's output folder.\n\nThis is meant to be impossible, please contact the developer at https://ampersandia.net/", path.to_string_lossy()),
Expand All @@ -189,14 +198,14 @@ pub fn report_errors(errors: Vec<Error>, engine: &Kismesis) {
},
Error::TriedToGetNonExistentTemplate(id) => eprintln!("Tried to get a non-existent kismesis template {:?}", id)
}
}
}
}

impl From<KismesisError> for Error {
fn from(value: KismesisError) -> Self {
match value {
KismesisError::IOError(x, y) => Error::IO(x, y),
KismesisError::ParseError(x, y) => Error::Parse(x, y),
}
}
fn from(value: KismesisError) -> Self {
match value {
KismesisError::IOError(x, y) => Error::IO(x, y),
KismesisError::ParseError(x, y) => Error::Parse(x, y),
}
}
}
Loading

0 comments on commit c9c0cfd

Please sign in to comment.