use crate::paths;
use anyhow::Result;
use std::path::Path;
const DOCKER_COMPOSE: &str = include_str!("../../templates/docker-compose.yml.template");
const DOCKER_COMPOSE_REMOTE: &str = include_str!("../../templates/docker/docker-compose.remote.yml.template");
const DOCKERFILE_REMOTE: &str = include_str!("../../templates/docker/Dockerfile.remote.template");
const DOCKERFILE_ROBOT_BUILDER: &str = include_str!("../../templates/docker/robot-builder.Dockerfile.template");
const ENV_EXAMPLE: &str = include_str!("../../templates/.env.example.template");
pub struct InfraTemplates;
impl InfraTemplates {
pub fn new() -> Self {
Self
}
async fn ensure_docker_dir(&self, path: &Path) -> Result<()> {
let docker_dir = path.join(paths::docker::DIR);
if !docker_dir.exists() {
tokio::fs::create_dir_all(&docker_dir).await?;
}
Ok(())
}
pub async fn create_docker_compose(&self, path: &Path, project_name: &str) -> Result<()> {
self.ensure_docker_dir(path).await?;
let content = DOCKER_COMPOSE.replace("{{project_name}}", project_name);
tokio::fs::write(path.join(paths::docker::COMPOSE_FILE), content).await?;
Ok(())
}
pub async fn create_docker_compose_remote(&self, path: &Path) -> Result<()> {
self.ensure_docker_dir(path).await?;
tokio::fs::write(path.join(paths::docker::COMPOSE_REMOTE_FILE), DOCKER_COMPOSE_REMOTE).await?;
Ok(())
}
pub async fn create_dockerfile_remote(&self, path: &Path) -> Result<()> {
self.ensure_docker_dir(path).await?;
tokio::fs::write(path.join(paths::docker::DOCKERFILE_REMOTE), DOCKERFILE_REMOTE).await?;
Ok(())
}
pub async fn create_dockerfile_robot_builder(&self, path: &Path, project_name: &str) -> Result<()> {
self.ensure_docker_dir(path).await?;
let content = DOCKERFILE_ROBOT_BUILDER.replace("{{project_name}}", project_name);
tokio::fs::write(path.join(paths::docker::DOCKERFILE_ROBOT_BUILDER), content).await?;
Ok(())
}
pub async fn create_remote_docker_files(&self, path: &Path) -> Result<()> {
self.ensure_docker_dir(path).await?;
self.create_docker_compose_remote(path).await?;
self.create_dockerfile_remote(path).await?;
Ok(())
}
pub async fn create_env_example(
&self,
path: &Path,
project_name: &str,
framework_path: Option<String>,
) -> Result<()> {
let robot_id = project_name.replace('-', "_");
let framework_line = if let Some(fw_path) = framework_path {
format!("MECHA10_FRAMEWORK_PATH={}", fw_path)
} else {
"# MECHA10_FRAMEWORK_PATH=/path/to/mecha10-monorepo".to_string()
};
let content = ENV_EXAMPLE
.replace("{{robot_id}}", &robot_id)
.replace("# MECHA10_FRAMEWORK_PATH=/path/to/mecha10-monorepo", &framework_line);
tokio::fs::write(path.join(paths::env::EXAMPLE), &content).await?;
tokio::fs::write(path.join(paths::env::FILE), &content).await?;
Ok(())
}
}
impl Default for InfraTemplates {
fn default() -> Self {
Self::new()
}
}