Skip to content

Commit

Permalink
Generator taking target folder as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 7, 2023
1 parent 179f79e commit 17e048b
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 51 deletions.
3 changes: 1 addition & 2 deletions ecosystem/python/exporter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ readme.workspace = true

[dependencies]
ligen-ir.workspace = true
ligen-traits.workspace = true
ligen-common.workspace = true
ligen-generator.workspace = true
20 changes: 9 additions & 11 deletions ecosystem/python/exporter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::{path::PathBuf, str::FromStr};
use ligen_ir::{Module, Library};
use prelude::*;

use ligen_traits::generator::file_generator::{TemplateRegister, Template, TemplateBasedGenerator};

use ligen_generator::{file_generator::{TemplateRegister, Template, TemplateBasedGenerator}, register_templates};

#[derive(Debug, Default)]
pub struct PythonGenerator {}

impl TemplateRegister for PythonGenerator {
fn register_templates(&self, _template: &mut Template) -> Result<()> {
fn register_templates(&self, template: &mut Template) -> Result<()> {
register_templates!(template, module);
// register_templates!(template, identifier, arguments, implementation, method, function, module, object, parameters, library);
Ok(())
}
Expand All @@ -26,14 +26,12 @@ impl TemplateBasedGenerator for PythonGenerator {
PathBuf::from("python".to_string())
}

fn module_generation_path(&self, _library: &Library, _module: &Module) -> PathBuf {
// let is_root_module = library.root_module == *module;
// let name = if is_root_module { "lib.rs" } else { "mod.rs" };
let path = PathBuf::from_str("src").unwrap();
// path = path.join(PathBuf::from(module.path.clone().without_first()));
// path = path.join(name);
// FIXME: This is not working.
println!("{}", path.display());
fn module_generation_path(&self, library: &Library, module: &Module) -> PathBuf {
let is_root_module = library.root_module == *module;
let name = if is_root_module { "lib.rs" } else { "mod.rs" };
let mut path = PathBuf::from_str("src").unwrap();
path = path.join(PathBuf::from(module.identifier.name.clone()));
path = path.join(name);
path
}
}
2 changes: 1 addition & 1 deletion ecosystem/python/exporter/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub use ligen_common::*;
pub use ligen_generator::prelude::*;
129 changes: 129 additions & 0 deletions ecosystem/python/exporter/src/templates/ligen/python/instagrapi/src
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
fn main() {
println!("Hello!")
}
3 changes: 3 additions & 0 deletions ecosystem/python/exporter/src/templates/module.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello!")
}
1 change: 1 addition & 0 deletions ecosystem/rust/cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license.workspace = true
[dependencies]
ligen-ir.workspace = true
ligen-traits.workspace = true
ligen-generator.workspace = true
ligen-utils.workspace = true
ligen-parser.workspace = true
ligen-rust-parser.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/rust/cargo/src/exporter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ligen_ir::Library;
use ligen_traits::generator::file_generator::{FileGenerator, FileSet};
use ligen_generator::file_generator::{FileGenerator, FileSet};
use std::path::PathBuf;
use std::str::FromStr;
use ligen_traits::prelude::*;
Expand Down
6 changes: 5 additions & 1 deletion ligen/generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ readme.workspace = true

[dependencies]
ligen-common.workspace = true
ligen-ir.workspace = true
ligen-ir.workspace = true
ligen-utils.workspace = true

serde_json.workspace = true
handlebars = "4.1.6"
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::generator::Generator;

use ligen_ir::Library;
use ligen_utils::fs::write_file;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

/// File generator.
pub trait FileGenerator {
Expand All @@ -23,15 +23,8 @@ pub trait FileGenerator {
fn generate_files(&self, library: &Library, file_set: &mut FileSet) -> Result<()>;

/// Saves the file set.
fn save_file_set(&self, library: &Library, file_set: FileSet) -> Result<()> {
let target = std::env::var("OUT_DIR")
.ok()
.map(PathBuf::from)
.and_then(|path| path
.ancestors()
.nth(4)
.map(Path::to_path_buf))
.unwrap_or(std::env::current_dir()?);
fn save_file_set(&self, library: &Library, file_set: FileSet, folder: &std::path::Path) -> Result<()> {
let target = folder.to_path_buf();
let target_ligen_dir = target
.join("ligen")
.join(self.base_path());
Expand All @@ -45,10 +38,10 @@ pub trait FileGenerator {
}

impl <T: FileGenerator> Generator for T {
fn generate(&self, library: &Library) -> Result<()> {
fn generate(&self, library: &Library, folder: &std::path::Path) -> Result<()> {
let mut file_set = FileSet::default();
self.generate_files(library, &mut file_set)?;
self.save_file_set(library, file_set)?;
self.save_file_set(library, file_set, folder)?;
Ok(())
}
}
4 changes: 3 additions & 1 deletion ligen/generator/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
use crate::prelude::*;

pub mod file_generator;

/// Generator trait.
pub trait Generator {
/// The Generator's entry point.
fn generate(&self, library: &ligen_ir::Library) -> Result<()>;
fn generate(&self, library: &ligen_ir::Library, folder: &std::path::Path) -> Result<()>;
}
2 changes: 1 addition & 1 deletion ligen/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
pub use ligen_utils::prelude::*;

pub use ligen_traits::generator::Generator;
pub use ligen_generator::Generator;
pub use ligen_ir::prelude::*;
1 change: 0 additions & 1 deletion ligen/traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ ligen-utils.workspace = true
ligen-common.workspace = true
serde.workspace = true
serde_json.workspace = true
handlebars = "4.1.6"
11 changes: 0 additions & 11 deletions ligen/traits/src/generator/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion ligen/traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod prelude;
pub mod marshalling;
pub mod build;
pub mod generator;
1 change: 1 addition & 0 deletions tools/editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ligen-parser.workspace = true
ligen-cargo.workspace = true
ligen-python-parser.workspace = true
ligen-traits.workspace = true
ligen-generator.workspace = true
ligen-python-exporter.workspace = true
egui_tiles = "0.2.0"
stacker = "0.1.15"
Expand Down
17 changes: 10 additions & 7 deletions tools/editor/src/gui/ui/layout/editor/generators/generator.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use ligen_cargo::parser::library;
use ligen_traits::generator;

use crate::prelude::*;
use crate::gui::ui::editor::{widget::Widget, settings::Settings};

pub struct Generator {
generator: Box<dyn generator::Generator>,
generator: Box<dyn ligen_generator::Generator>,
result: String
}

impl Generator {
pub fn new<T: generator::Generator + 'static>(generator: T) -> Self {
pub fn new<T: ligen_generator::Generator + 'static>(generator: T) -> Self {
let generator = Box::new(generator);
let result = Default::default();
Self { generator, result }
Expand All @@ -22,10 +21,14 @@ impl Widget for Generator {
fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, input: &mut Self::Input) {
ui.label("Generator");
if ui.button("Generate").clicked() {
match self.generator.generate(input) {
Ok(_) => self.result = "Success".to_string(),
Err(error) => self.result = format!("Error: {:?}", error)
};
let entry = rfd::FileDialog::new()
.pick_folder();
if let Some(entry) = entry {
match self.generator.generate(input, &entry) {
Ok(_) => self.result = "Success".to_string(),
Err(error) => self.result = format!("Error: {:?}", error)
};
}
}
ui.label(&self.result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ impl Widget for Parser {
}
});
}
}
}

0 comments on commit 17e048b

Please sign in to comment.