1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
// use std::path::{Path, PathBuf};
//
// use anyhow::Context;
// use dialoguer::{theme::ColorfulTheme, Select};
// use wasmer_backend_api::{types::UserWithNamespaces, WasmerClient};
//
// use super::prompts::PackageCheckMode;
//
// const WASM_STATIC_SERVER_PACKAGE: &str = "wasmer/static-web-server";
// const WASM_STATIC_SERVER_VERSION: &str = "1";
//
// const WASMER_WINTER_JS_PACKAGE: &str = "wasmer/winterjs";
// const WASMER_WINTER_JS_VERSION: &str = "*";
//
// const WASM_PYTHON_PACKAGE: &str = "wasmer/python";
// const WASM_PYTHON_VERSION: &str = "3.12.6";
//
// const SAMPLE_INDEX_HTML: &str = include_str!("./templates/static-site/index.html");
// const SAMPLE_JS_WORKER: &str = include_str!("./templates/js-worker/index.js");
// const SAMPLE_PY_APPLICATION: &str = include_str!("./templates/py-application/main.py");
//
// #[derive(clap::ValueEnum, Clone, Copy, Debug)]
// pub enum PackageType {
// #[clap(name = "regular")]
// Regular,
// /// A static website.
// #[clap(name = "static-website")]
// StaticWebsite,
// /// A js-worker
// #[clap(name = "js-worker")]
// JsWorker,
// /// A py-worker
// #[clap(name = "py-application")]
// PyApplication,
// }
//
// #[derive(Clone, Copy, Debug)]
// pub enum CreateMode {
// Create,
// SelectExisting,
// #[allow(dead_code)]
// CreateOrSelect,
// }
//
// fn prompt_for_package_type() -> Result<PackageType, anyhow::Error> {
// let theme = ColorfulTheme::default();
// Select::with_theme(&theme)
// .with_prompt("What type of package do you want to create?")
// .items(&["Basic pacakge", "Static website"])
// .interact()
// .map(|idx| match idx {
// 0 => PackageType::Regular,
// 1 => PackageType::StaticWebsite,
// _ => unreachable!(),
// })
// .map_err(anyhow::Error::from)
// }
//
// #[derive(Debug)]
// pub struct PackageWizard {
// pub path: PathBuf,
// pub type_: Option<PackageType>,
//
// pub create_mode: CreateMode,
//
// /// Namespace to use.
// pub namespace: Option<String>,
// /// Default namespace to use.
// /// Will still show a prompt, with this as the default value.
// /// Ignored if [`Self::namespace`] is set.
// pub namespace_default: Option<String>,
//
// /// Pre-configured package name.
// pub name: Option<String>,
//
// pub user: Option<UserWithNamespaces>,
// }
//
// pub struct PackageWizardOutput {
// pub api: Option<wasmer_backend_api::types::Package>,
// pub local_path: Option<PathBuf>,
// pub local_manifest: Option<wasmer_config::package::Manifest>,
// }
//
// impl PackageWizard {
// fn build_new_package(&self) -> Result<PackageWizardOutput, anyhow::Error> {
// let ty = match self.type_ {
// Some(t) => t,
// None => prompt_for_package_type()?,
// };
//
// if !self.path.is_dir() {
// std::fs::create_dir_all(&self.path).with_context(|| {
// format!("Failed to create directory: '{}'", self.path.display())
// })?;
// }
//
// let manifest = match ty {
// PackageType::Regular => todo!(),
// PackageType::StaticWebsite => initialize_static_site(&self.path)?,
// PackageType::JsWorker => initialize_js_worker(&self.path)?,
// PackageType::PyApplication => initialize_py_worker(&self.path)?,
// };
//
// let manifest_path = self.path.join("wasmer.toml");
// let manifest_raw = manifest
// .to_string()
// .context("could not serialize package manifest")?;
// std::fs::write(manifest_path, manifest_raw)
// .with_context(|| format!("Failed to write manifest to '{}'", self.path.display()))?;
//
// Ok(PackageWizardOutput {
// api: None,
// local_path: Some(self.path.clone()),
// local_manifest: Some(manifest),
// })
// }
//
// async fn prompt_existing_package(
// &self,
// api: Option<&WasmerClient>,
// ) -> Result<PackageWizardOutput, anyhow::Error> {
// // Existing package
// let check = if api.is_some() {
// Some(PackageCheckMode::MustExist)
// } else {
// None
// };
//
// eprintln!("Enter the name of an existing package:");
// let (_ident, api) = super::prompts::prompt_for_package("Package", None, check, api).await?;
// Ok(PackageWizardOutput {
// api,
// local_path: None,
// local_manifest: None,
// })
// }
//
// pub async fn run(
// self,
// api: Option<&WasmerClient>,
// ) -> Result<PackageWizardOutput, anyhow::Error> {
// match self.create_mode {
// CreateMode::Create => self.build_new_package(),
// CreateMode::SelectExisting => self.prompt_existing_package(api).await,
// CreateMode::CreateOrSelect => {
// let theme = ColorfulTheme::default();
// let index = Select::with_theme(&theme)
// .with_prompt("What package do you want to use?")
// .items(&["Create new package", "Use existing package"])
// .default(0)
// .interact()?;
//
// match index {
// 0 => self.build_new_package(),
// 1 => self.prompt_existing_package(api).await,
// other => {
// unreachable!("Unexpected index: {other}");
// }
// }
// }
// }
// }
// }
//
// fn initialize_static_site(path: &Path) -> Result<wasmer_config::package::Manifest, anyhow::Error> {
// let pubdir_name = "public";
// let pubdir = path.join(pubdir_name);
// if !pubdir.is_dir() {
// std::fs::create_dir_all(&pubdir)
// .with_context(|| format!("Failed to create directory: '{}'", pubdir.display()))?;
// }
// let index = pubdir.join("index.html");
//
// let static_html = SAMPLE_INDEX_HTML.replace("{{title}}", "My static website");
//
// if !index.is_file() {
// std::fs::write(&index, static_html.as_str())
// .with_context(|| "Could not write index.html file".to_string())?;
// } else {
// // The index.js file already exists, so we can ask the user if they want to overwrite it
// let theme = dialoguer::theme::ColorfulTheme::default();
// let should_overwrite = dialoguer::Confirm::with_theme(&theme)
// .with_prompt("index.html already exists. Do you want to overwrite it?")
// .interact()
// .unwrap();
// if should_overwrite {
// std::fs::write(&index, static_html.as_str())
// .with_context(|| "Could not write index.html file".to_string())?;
// }
// }
//
// let raw_static_site_toml = format!(
// r#"
// [dependencies]
// "{}" = "{}"
//
// [fs]
// public = "{}"
// "#,
// WASM_STATIC_SERVER_PACKAGE, WASM_STATIC_SERVER_VERSION, pubdir_name
// );
//
// let manifest = wasmer_config::package::Manifest::parse(raw_static_site_toml.as_str())
// .map_err(|e| anyhow::anyhow!("Could not parse js worker manifest: {}", e))?;
//
// Ok(manifest)
// }
//
// fn initialize_js_worker(path: &Path) -> Result<wasmer_config::package::Manifest, anyhow::Error> {
// let srcdir_name = "src";
// let srcdir = path.join(srcdir_name);
// if !srcdir.is_dir() {
// std::fs::create_dir_all(&srcdir)
// .with_context(|| format!("Failed to create directory: '{}'", srcdir.display()))?;
// }
//
// let index_js = srcdir.join("index.js");
//
// let sample_js = SAMPLE_JS_WORKER.replace("{{package}}", "My JS worker");
//
// if !index_js.is_file() {
// std::fs::write(&index_js, sample_js.as_str())
// .with_context(|| "Could not write index.js file".to_string())?;
// }
//
// // get the remote repository if it exists
// // Todo: add this to the manifest
// // let remote_repo_url = Command::new("git")
// // .arg("remote")
// // .arg("get-url")
// // .arg("origin")
// // .output()
// // .map_or("".to_string(), |f| String::from_utf8(f.stdout).unwrap());
//
// let raw_js_worker_toml = format!(
// r#"
// [dependencies]
// "{winterjs_pkg}" = "{winterjs_version}"
//
// [fs]
// "/src" = "./src"
//
// [[command]]
// name = "script"
// module = "{winterjs_pkg}:winterjs"
// runner = "https://webc.org/runner/wasi"
//
// [command.annotations.wasi]
// main-args = ["/src/index.js"]
// env = ["JS_PATH=/src/index.js"]
// "#,
// winterjs_pkg = WASMER_WINTER_JS_PACKAGE,
// winterjs_version = WASMER_WINTER_JS_VERSION,
// );
//
// let manifest = wasmer_config::package::Manifest::parse(raw_js_worker_toml.as_str())
// .map_err(|e| anyhow::anyhow!("Could not parse js worker manifest: {}", e))?;
//
// Ok(manifest)
// }
//
// fn initialize_py_worker(path: &Path) -> Result<wasmer_config::package::Manifest, anyhow::Error> {
// let appdir_name = "src";
// let appdir = path.join(appdir_name);
// if !appdir.is_dir() {
// std::fs::create_dir_all(&appdir)
// .with_context(|| format!("Failed to create directory: '{}'", appdir.display()))?;
// }
// let main_py = appdir.join("main.py");
//
// let sample_main = SAMPLE_PY_APPLICATION.replace("{{package}}", "My Python Worker");
//
// if !main_py.is_file() {
// std::fs::write(&main_py, sample_main.as_str())
// .with_context(|| "Could not write main.py file".to_string())?;
// }
//
// // Todo: add this to the manifest
// // let remote_repo_url = Command::new("git")
// // .arg("remote")
// // .arg("get-url")
// // .arg("origin")
// // .output()
// // .map_or("".to_string(), |f| String::from_utf8(f.stdout).unwrap());
//
// let raw_py_worker_toml = format!(
// r#"
// [dependencies]
// "{}" = "{}"
//
// [fs]
// "/src" = "./src"
// # "/.env" = "./.env/" # Bundle the virtualenv
//
// [[command]]
// name = "script"
// module = "{}:python" # The "python" atom from "wasmer/python"
// runner = "wasi"
//
// [command.annotations.wasi]
// main-args = ["/src/main.py"]
// # env = ["PYTHON_PATH=/app/.env:/etc/python3.12/site-packages"] # Make our virtualenv accessible
// "#,
// WASM_PYTHON_PACKAGE, WASM_PYTHON_VERSION, WASM_PYTHON_PACKAGE
// );
//
// let manifest = wasmer_config::package::Manifest::parse(raw_py_worker_toml.as_str())
// .map_err(|e| anyhow::anyhow!("Could not parse py worker manifest: {}", e))?;
//
// Ok(manifest)
// }
// #[cfg(test)]
// mod tests {
// use super::*;
//
// #[tokio::test]
// async fn test_package_wizard_create_static_site() {
// let dir = tempfile::tempdir().unwrap();
//
// PackageWizard {
// path: dir.path().to_owned(),
// type_: Some(PackageType::StaticWebsite),
// create_mode: CreateMode::Create,
// namespace: None,
// namespace_default: None,
// name: None,
// user: None,
// }
// .run(None)
// .await
// .unwrap();
//
// let manifest = std::fs::read_to_string(dir.path().join("wasmer.toml")).unwrap();
// pretty_assertions::assert_eq!(
// manifest,
// r#"[dependencies]
// "wasmer/static-web-server" = "^1"
//
// [fs]
// public = "public"
// "#,
// );
//
// assert!(dir.path().join("public").join("index.html").is_file());
// }
//
// #[tokio::test]
// async fn test_package_wizard_create_js_worker() {
// let dir = tempfile::tempdir().unwrap();
//
// PackageWizard {
// path: dir.path().to_owned(),
// type_: Some(PackageType::JsWorker),
// create_mode: CreateMode::Create,
// namespace: None,
// namespace_default: None,
// name: None,
// user: None,
// }
// .run(None)
// .await
// .unwrap();
// let manifest = std::fs::read_to_string(dir.path().join("wasmer.toml")).unwrap();
//
// pretty_assertions::assert_eq!(
// manifest,
// r#"[dependencies]
// "wasmer/winterjs" = "*"
//
// [fs]
// "/src" = "./src"
//
// [[command]]
// name = "script"
// module = "wasmer/winterjs:winterjs"
// runner = "https://webc.org/runner/wasi"
//
// [command.annotations.wasi]
// env = ["JS_PATH=/src/index.js"]
// main-args = ["/src/index.js"]
// "#,
// );
//
// assert!(dir.path().join("src").join("index.js").is_file());
// }
//
// #[tokio::test]
// async fn test_package_wizard_create_py_worker() {
// let dir = tempfile::tempdir().unwrap();
//
// PackageWizard {
// path: dir.path().to_owned(),
// type_: Some(PackageType::PyApplication),
// create_mode: CreateMode::Create,
// namespace: None,
// namespace_default: None,
// name: None,
// user: None,
// }
// .run(None)
// .await
// .unwrap();
// let manifest = std::fs::read_to_string(dir.path().join("wasmer.toml")).unwrap();
//
// pretty_assertions::assert_eq!(
// manifest,
// r#"[dependencies]
// "wasmer/python" = "^3.12.6"
//
// [fs]
// "/src" = "./src"
//
// [[command]]
// name = "script"
// module = "wasmer/python:python"
// runner = "wasi"
//
// [command.annotations.wasi]
// main-args = ["/src/main.py"]
// "#,
// );
//
// assert!(dir.path().join("src").join("main.py").is_file());
// }
// }