2024-08-08 23:47:02 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2024-08-06 20:50:11 +00:00
|
|
|
use async_trait::async_trait;
|
2024-08-08 23:47:02 +00:00
|
|
|
use cargo_generate::{generate, GenerateArgs, TemplatePath, Vcs};
|
2024-08-15 06:20:31 +00:00
|
|
|
use tokio::fs;
|
2024-07-23 09:35:38 +00:00
|
|
|
use tracing::trace;
|
|
|
|
|
|
|
|
use crate::{
|
2024-08-15 06:20:31 +00:00
|
|
|
config::Config,
|
2024-08-06 20:50:11 +00:00
|
|
|
error::Error,
|
|
|
|
handler::Handler,
|
|
|
|
request::init::InitRequest,
|
|
|
|
response::{init::InitResponse, Response},
|
2024-07-23 09:35:38 +00:00
|
|
|
};
|
|
|
|
|
2024-08-06 20:50:11 +00:00
|
|
|
#[async_trait]
|
2024-07-23 09:35:38 +00:00
|
|
|
impl Handler for InitRequest {
|
|
|
|
type Error = Error;
|
2024-08-02 19:19:07 +00:00
|
|
|
type Response = Response;
|
2024-07-23 09:35:38 +00:00
|
|
|
|
2024-08-15 06:20:31 +00:00
|
|
|
async fn handle(self, config: Config) -> Result<Self::Response, Self::Error> {
|
2024-07-23 09:35:38 +00:00
|
|
|
trace!("initializing directory structure...");
|
2024-08-06 20:50:11 +00:00
|
|
|
|
2024-08-08 23:47:02 +00:00
|
|
|
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
|
|
|
|
|
2024-08-15 06:20:31 +00:00
|
|
|
let parent = self
|
|
|
|
.name
|
|
|
|
.parent()
|
|
|
|
.map(|p| p.to_path_buf())
|
|
|
|
.expect("path already validated");
|
|
|
|
fs::create_dir_all(&parent)
|
|
|
|
.await
|
|
|
|
.map_err(|e| Error::GenericErr(e.to_string()))?;
|
|
|
|
|
|
|
|
let file_name = self
|
|
|
|
.name
|
|
|
|
.file_name()
|
|
|
|
.and_then(|f| f.to_str())
|
|
|
|
.expect("path already validated");
|
|
|
|
|
2024-08-08 23:47:02 +00:00
|
|
|
let wasm_pack_args = GenerateArgs {
|
2024-08-15 06:20:31 +00:00
|
|
|
name: Some(file_name.to_string()),
|
|
|
|
destination: Some(config.app_dir.join(parent)),
|
|
|
|
overwrite: true,
|
2024-08-08 23:47:02 +00:00
|
|
|
vcs: Some(Vcs::Git),
|
|
|
|
template_path: TemplatePath {
|
|
|
|
// git: Some("git@github.com:informalsystems/cycles-quartz.git".to_string()), // TODO: replace with public http address when open-sourced
|
|
|
|
path: Some(root_dir.join("apps/transfers").display().to_string()),
|
|
|
|
..TemplatePath::default()
|
|
|
|
},
|
|
|
|
..GenerateArgs::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let result_dir = generate(wasm_pack_args)
|
|
|
|
.expect("something went wrong!")
|
|
|
|
.display()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
Ok(InitResponse { result_dir }.into())
|
2024-07-23 09:35:38 +00:00
|
|
|
}
|
|
|
|
}
|