Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ target
# Contains mutation testing data
**/mutants.out*/

# MacOS specific files
.DS_Store

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
Expand Down
15 changes: 14 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rohas-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = { workspace = true }
edition = { workspace = true }
authors = { workspace = true }
license = { workspace = true }
default-run = "rohas"

[[bin]]
name = "rohas"
Expand Down
2 changes: 2 additions & 0 deletions crates/rohas-cli/src/commands/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn engine_language_to_codegen_language(lang: EngineLanguage) -> Language {
match lang {
EngineLanguage::TypeScript => Language::TypeScript,
EngineLanguage::Python => Language::Python,
EngineLanguage::Rust => Language::Rust,
}
}

Expand All @@ -35,6 +36,7 @@ pub async fn execute(
let language = match lang.as_deref() {
Some("typescript") | Some("ts") => Language::TypeScript,
Some("python") | Some("py") => Language::Python,
Some("rust") | Some("rs") => Language::Rust,
None => match &config_path {
Some(config_path) => match EngineConfig::from_file(config_path) {
Ok(config) => {
Expand Down
48 changes: 36 additions & 12 deletions crates/rohas-cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,48 @@ pub async fn execute(
) -> Result<()> {
info!("Starting development server...");

let mut config = match EngineConfig::from_project_root() {
Ok(config) => {
info!("Loaded configuration from config/rohas.toml");
config
}
Err(e) => {
info!("Using default configuration ({})", e);
EngineConfig::default()
}
};
config.project_root = std::env::current_dir()?;
let actual_path = if !schema_path.exists() && schema_path.ends_with("index.ro") {
schema_path
.parent()
.map(|p| p.to_path_buf())
.unwrap_or(schema_path)
} else {
schema_path
schema_path.clone()
};

let project_root = if actual_path.file_name()
.and_then(|s| s.to_str())
.map(|s| s == "schema")
.unwrap_or(false)
{
actual_path
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| std::env::current_dir().unwrap_or_default())
} else {
actual_path.clone()
};

let config_path = project_root.join("config").join("rohas.toml");
let mut config = if config_path.exists() {
match EngineConfig::from_file(&config_path) {
Ok(mut cfg) => {
cfg.project_root = project_root.clone();
info!("Loaded configuration from {}", config_path.display());
cfg
}
Err(e) => {
info!("Failed to load config from {}: {}. Using defaults.", config_path.display(), e);
let mut cfg = EngineConfig::default();
cfg.project_root = project_root.clone();
cfg
}
}
} else {
info!("Config file not found: {}. Using default configuration.", config_path.display());
let mut cfg = EngineConfig::default();
cfg.project_root = project_root.clone();
cfg
};

let dev_server = DevServer::new(actual_path, config.clone(), watch);
Expand Down
37 changes: 37 additions & 0 deletions crates/rohas-codegen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,43 @@ target-version = "py39"
Ok(())
}

pub fn generate_cargo_toml(_schema: &Schema, output_dir: &Path) -> Result<()> {
let project_root = get_project_root(output_dir);
let project_name = extract_project_name(&project_root);

let lib_name = project_name.replace('-', "_");

let content = format!(
r#"[package]
name = "{}"
version = "0.1.0"
edition = "2021"

[workspace]

[lib]
name = "{}"
path = "src/lib.rs"

[dependencies]
rohas-runtime = {{ path = "../../crates/rohas-runtime" }}
serde = {{ version = "1.0", features = ["derive"] }}
serde_json = "1.0"
tokio = {{ version = "1.0", features = ["full"] }}
chrono = {{ version = "0.4", features = ["serde"] }}
tracing = "0.1"

[dev-dependencies]
tokio-test = "0.4"
"#,
project_name,
lib_name
);

fs::write(project_root.join("Cargo.toml"), content)?;
Ok(())
}

pub fn generate_gitignore(_schema: &Schema, output_dir: &Path) -> Result<()> {
let project_root = get_project_root(output_dir);
let content = r#"# Dependencies
Expand Down
24 changes: 23 additions & 1 deletion crates/rohas-codegen/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::Result;
use crate::{config, python, typescript, Language};
use crate::{config, python, rust, typescript, Language};
use rohas_parser::Schema;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -28,6 +28,7 @@ impl Generator {
match self.language {
Language::TypeScript => self.generate_typescript(schema, output_dir)?,
Language::Python => self.generate_python(schema, output_dir)?,
Language::Rust => self.generate_rust(schema, output_dir)?,
}

info!("Code generation completed successfully");
Expand Down Expand Up @@ -109,4 +110,25 @@ impl Generator {

Ok(())
}

fn generate_rust(&self, schema: &Schema, output_dir: &Path) -> Result<()> {
rust::generate_state(output_dir)?;
rust::generate_models(schema, output_dir)?;
rust::generate_dtos(schema, output_dir)?;
rust::generate_apis(schema, output_dir)?;
rust::generate_events(schema, output_dir)?;
rust::generate_crons(schema, output_dir)?;
rust::generate_websockets(schema, output_dir)?;
rust::generate_middlewares(schema, output_dir)?;
rust::generate_lib_rs(schema, output_dir)?;

info!("Generating Rust configuration files");
config::generate_cargo_toml(schema, output_dir)?;

if rust::is_in_rohas_workspace(output_dir) {
rust::generate_dev_scripts(output_dir)?;
}

Ok(())
}
}
2 changes: 2 additions & 0 deletions crates/rohas-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod config;
pub mod error;
pub mod generator;
pub mod python;
pub mod rust;
pub mod templates;
pub mod typescript;

Expand All @@ -15,6 +16,7 @@ use std::path::Path;
pub enum Language {
TypeScript,
Python,
Rust,
}

pub fn generate(schema: &Schema, output_dir: &Path, lang: Language) -> Result<()> {
Expand Down
Loading