@devaloop/devalang 0.0.1-alpha.8 → 0.0.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/.cargo/config.toml +2 -0
  2. package/.devalang +10 -4
  3. package/.github/workflows/ci.yml +103 -0
  4. package/Cargo.toml +80 -48
  5. package/README.md +135 -158
  6. package/docs/CHANGELOG.md +413 -1
  7. package/docs/CONTRIBUTING.md +101 -0
  8. package/docs/ROADMAP.md +10 -7
  9. package/docs/TODO.md +21 -9
  10. package/examples/automation.deva +42 -0
  11. package/examples/bank.deva +7 -0
  12. package/examples/condition.deva +8 -12
  13. package/examples/duration.deva +9 -0
  14. package/examples/events.deva +12 -0
  15. package/examples/function.deva +15 -0
  16. package/examples/group.deva +3 -3
  17. package/examples/index.deva +57 -10
  18. package/examples/loop.deva +7 -12
  19. package/examples/pattern.deva +8 -0
  20. package/examples/plugin.deva +16 -0
  21. package/examples/synth.deva +14 -0
  22. package/examples/variables.deva +2 -2
  23. package/out-tsc/bin/index.d.ts +2 -0
  24. package/out-tsc/bin/index.js +51 -7
  25. package/out-tsc/core/functions/index.d.ts +37 -0
  26. package/out-tsc/core/functions/index.js +76 -0
  27. package/out-tsc/core/index.d.ts +6 -0
  28. package/out-tsc/core/index.js +22 -0
  29. package/out-tsc/core/types/index.d.ts +4 -0
  30. package/out-tsc/core/types/index.js +20 -0
  31. package/out-tsc/core/types/plugin.d.ts +18 -0
  32. package/out-tsc/core/types/plugin.js +2 -0
  33. package/out-tsc/core/types/result.d.ts +27 -0
  34. package/out-tsc/core/types/result.js +2 -0
  35. package/out-tsc/core/types/statement.d.ts +106 -0
  36. package/out-tsc/core/types/statement.js +2 -0
  37. package/out-tsc/core/types/value.d.ts +43 -0
  38. package/out-tsc/core/types/value.js +2 -0
  39. package/out-tsc/index.d.ts +7 -0
  40. package/out-tsc/index.js +42 -1
  41. package/out-tsc/pkg/devalang_core.d.ts +13 -0
  42. package/out-tsc/pkg/devalang_core.js +50 -0
  43. package/out-tsc/pkg/devalang_core_bg.wasm.d.ts +33 -0
  44. package/out-tsc/scripts/copy-wasm-dts.d.ts +1 -0
  45. package/out-tsc/scripts/copy-wasm-dts.js +73 -0
  46. package/out-tsc/scripts/postinstall.d.ts +1 -0
  47. package/out-tsc/scripts/postinstall.js +83 -0
  48. package/out-tsc/scripts/version/bump.d.ts +1 -0
  49. package/out-tsc/scripts/version/fetch.d.ts +1 -0
  50. package/out-tsc/scripts/version/fetch.js +1 -5
  51. package/out-tsc/scripts/version/index.d.ts +1 -0
  52. package/out-tsc/scripts/version/sync.d.ts +1 -0
  53. package/package.json +28 -7
  54. package/project-version.json +4 -4
  55. package/rust/cli/bank/api.rs +122 -0
  56. package/rust/cli/bank/commands.rs +275 -0
  57. package/rust/cli/bank/mod.rs +29 -0
  58. package/rust/cli/build/commands.rs +103 -0
  59. package/rust/cli/build/mod.rs +2 -0
  60. package/rust/cli/build/process.rs +146 -0
  61. package/rust/cli/check/mod.rs +208 -0
  62. package/rust/cli/discover/commands.rs +253 -0
  63. package/rust/cli/discover/config.rs +111 -0
  64. package/rust/cli/discover/fs.rs +19 -0
  65. package/rust/cli/discover/install.rs +103 -0
  66. package/rust/cli/discover/metadata.rs +48 -0
  67. package/rust/cli/discover/mod.rs +5 -0
  68. package/rust/cli/{init.rs → init/commands.rs} +32 -23
  69. package/rust/cli/init/mod.rs +1 -0
  70. package/rust/cli/install/addon.rs +118 -0
  71. package/rust/cli/install/bank.rs +53 -0
  72. package/rust/cli/install/commands.rs +35 -0
  73. package/rust/cli/install/mod.rs +4 -0
  74. package/rust/cli/install/plugin.rs +61 -0
  75. package/rust/cli/login/commands.rs +124 -0
  76. package/rust/cli/login/mod.rs +1 -0
  77. package/rust/cli/mod.rs +12 -205
  78. package/rust/cli/parser.rs +314 -0
  79. package/rust/cli/play/commands.rs +324 -0
  80. package/rust/cli/play/io.rs +17 -0
  81. package/rust/cli/play/mod.rs +5 -0
  82. package/rust/cli/play/process.rs +150 -0
  83. package/rust/cli/play/realtime.rs +91 -0
  84. package/rust/cli/play/utils.rs +23 -0
  85. package/rust/cli/telemetry/commands.rs +22 -0
  86. package/rust/cli/telemetry/event_creator.rs +80 -0
  87. package/rust/cli/telemetry/mod.rs +3 -0
  88. package/rust/cli/telemetry/send.rs +51 -0
  89. package/rust/cli/{template.rs → template/commands.rs} +69 -57
  90. package/rust/cli/template/mod.rs +1 -0
  91. package/rust/cli/update/commands.rs +6 -0
  92. package/rust/cli/update/mod.rs +1 -0
  93. package/rust/config/driver.rs +103 -0
  94. package/rust/config/mod.rs +3 -16
  95. package/rust/config/ops.rs +26 -0
  96. package/rust/config/settings.rs +101 -0
  97. package/rust/core/audio/engine/helpers.rs +170 -0
  98. package/rust/core/audio/engine/mod.rs +7 -0
  99. package/rust/core/audio/engine/sample.rs +366 -0
  100. package/rust/core/audio/engine/synth.rs +325 -0
  101. package/rust/core/audio/evaluator.rs +310 -31
  102. package/rust/core/audio/interpreter/arrow_call.rs +311 -0
  103. package/rust/core/audio/interpreter/automate.rs +18 -0
  104. package/rust/core/audio/interpreter/call.rs +294 -42
  105. package/rust/core/audio/interpreter/condition.rs +71 -65
  106. package/rust/core/audio/interpreter/driver.rs +542 -204
  107. package/rust/core/audio/interpreter/function.rs +26 -0
  108. package/rust/core/audio/interpreter/let_.rs +38 -19
  109. package/rust/core/audio/interpreter/load.rs +19 -18
  110. package/rust/core/audio/interpreter/loop_.rs +114 -59
  111. package/rust/core/audio/interpreter/mod.rs +14 -11
  112. package/rust/core/audio/interpreter/sleep.rs +28 -36
  113. package/rust/core/audio/interpreter/spawn.rs +252 -65
  114. package/rust/core/audio/interpreter/tempo.rs +40 -16
  115. package/rust/core/audio/interpreter/trigger.rs +239 -69
  116. package/rust/core/audio/loader/mod.rs +1 -1
  117. package/rust/core/audio/loader/trigger.rs +97 -52
  118. package/rust/core/audio/mod.rs +7 -6
  119. package/rust/core/audio/player.rs +70 -54
  120. package/rust/core/audio/renderer.rs +54 -57
  121. package/rust/core/audio/special/easing.rs +189 -0
  122. package/rust/core/audio/special/env.rs +45 -0
  123. package/rust/core/audio/special/math.rs +134 -0
  124. package/rust/core/audio/special/mod.rs +9 -0
  125. package/rust/core/audio/special/modulator.rs +143 -0
  126. package/rust/core/builder/mod.rs +86 -80
  127. package/rust/core/debugger/lexer.rs +27 -27
  128. package/rust/core/debugger/mod.rs +30 -21
  129. package/rust/core/debugger/module.rs +55 -0
  130. package/rust/core/debugger/preprocessor.rs +27 -27
  131. package/rust/core/debugger/store.rs +40 -25
  132. package/rust/core/error/mod.rs +269 -60
  133. package/rust/core/lexer/driver.rs +61 -0
  134. package/rust/core/lexer/handler/arrow.rs +82 -0
  135. package/rust/core/lexer/handler/at.rs +21 -21
  136. package/rust/core/lexer/handler/brace.rs +41 -41
  137. package/rust/core/lexer/handler/colon.rs +21 -21
  138. package/rust/core/lexer/handler/comment.rs +30 -30
  139. package/rust/core/lexer/handler/dot.rs +21 -21
  140. package/rust/core/lexer/handler/driver.rs +337 -215
  141. package/rust/core/lexer/handler/identifier.rs +47 -40
  142. package/rust/core/lexer/handler/indent.rs +66 -52
  143. package/rust/core/lexer/handler/mod.rs +15 -13
  144. package/rust/core/lexer/handler/newline.rs +23 -23
  145. package/rust/core/lexer/handler/number.rs +31 -31
  146. package/rust/core/lexer/handler/operator.rs +46 -44
  147. package/rust/core/lexer/handler/parenthesis.rs +41 -0
  148. package/rust/core/lexer/handler/slash.rs +21 -0
  149. package/rust/core/lexer/handler/string.rs +63 -63
  150. package/rust/core/lexer/mod.rs +3 -30
  151. package/rust/core/lexer/token.rs +21 -12
  152. package/rust/core/mod.rs +10 -10
  153. package/rust/core/parser/driver.rs +584 -312
  154. package/rust/core/parser/handler/arrow_call.rs +253 -0
  155. package/rust/core/parser/handler/at.rs +279 -162
  156. package/rust/core/parser/handler/bank.rs +104 -41
  157. package/rust/core/parser/handler/condition.rs +83 -74
  158. package/rust/core/parser/handler/dot.rs +148 -112
  159. package/rust/core/parser/handler/identifier/automate.rs +254 -0
  160. package/rust/core/parser/handler/identifier/call.rs +91 -0
  161. package/rust/core/parser/handler/identifier/emit.rs +70 -0
  162. package/rust/core/parser/handler/identifier/function.rs +113 -0
  163. package/rust/core/parser/handler/identifier/group.rs +89 -0
  164. package/rust/core/parser/handler/identifier/let_.rs +173 -0
  165. package/rust/core/parser/handler/identifier/mod.rs +55 -0
  166. package/rust/core/parser/handler/identifier/on.rs +107 -0
  167. package/rust/core/parser/handler/identifier/print.rs +49 -0
  168. package/rust/core/parser/handler/identifier/sleep.rs +43 -0
  169. package/rust/core/parser/handler/identifier/spawn.rs +91 -0
  170. package/rust/core/parser/handler/identifier/synth.rs +135 -0
  171. package/rust/core/parser/handler/loop_.rs +194 -66
  172. package/rust/core/parser/handler/mod.rs +9 -7
  173. package/rust/core/parser/handler/pattern.rs +74 -0
  174. package/rust/core/parser/handler/tempo.rs +57 -47
  175. package/rust/core/parser/mod.rs +3 -4
  176. package/rust/core/parser/statement.rs +11 -88
  177. package/rust/core/plugin/loader.rs +137 -0
  178. package/rust/core/plugin/mod.rs +2 -0
  179. package/rust/core/plugin/runner.rs +347 -0
  180. package/rust/core/preprocessor/loader.rs +637 -179
  181. package/rust/core/preprocessor/mod.rs +4 -4
  182. package/rust/core/preprocessor/module.rs +60 -53
  183. package/rust/core/preprocessor/processor.rs +114 -67
  184. package/rust/core/preprocessor/resolver/bank.rs +49 -47
  185. package/rust/core/preprocessor/resolver/call.rs +124 -53
  186. package/rust/core/preprocessor/resolver/condition.rs +95 -66
  187. package/rust/core/preprocessor/resolver/driver.rs +324 -182
  188. package/rust/core/preprocessor/resolver/function.rs +69 -0
  189. package/rust/core/preprocessor/resolver/group.rs +94 -118
  190. package/rust/core/preprocessor/resolver/let_.rs +32 -0
  191. package/rust/core/preprocessor/resolver/loop_.rs +318 -145
  192. package/rust/core/preprocessor/resolver/mod.rs +16 -10
  193. package/rust/core/preprocessor/resolver/pattern.rs +83 -0
  194. package/rust/core/preprocessor/resolver/spawn.rs +99 -53
  195. package/rust/core/preprocessor/resolver/synth.rs +54 -0
  196. package/rust/core/preprocessor/resolver/tempo.rs +48 -49
  197. package/rust/core/preprocessor/resolver/trigger.rs +116 -111
  198. package/rust/core/preprocessor/resolver/value.rs +176 -0
  199. package/rust/core/store/export.rs +28 -28
  200. package/rust/core/store/function.rs +40 -0
  201. package/rust/core/store/global.rs +61 -39
  202. package/rust/core/store/import.rs +28 -28
  203. package/rust/core/store/mod.rs +5 -4
  204. package/rust/core/store/variable.rs +51 -28
  205. package/rust/core/utils/mod.rs +1 -2
  206. package/rust/core/utils/path.rs +37 -46
  207. package/rust/lib.rs +308 -117
  208. package/rust/main.rs +364 -65
  209. package/rust/types/Cargo.toml +11 -0
  210. package/rust/types/src/addons.rs +55 -0
  211. package/rust/types/src/ast.rs +202 -0
  212. package/rust/types/src/config.rs +74 -0
  213. package/rust/types/src/lib.rs +12 -0
  214. package/rust/types/src/telemetry.rs +85 -0
  215. package/rust/utils/Cargo.toml +26 -0
  216. package/rust/utils/src/error.rs +186 -0
  217. package/rust/utils/src/file.rs +94 -0
  218. package/rust/utils/src/first_usage.rs +97 -0
  219. package/rust/utils/{mod.rs → src/lib.rs} +9 -6
  220. package/rust/utils/{logger.rs → src/logger.rs} +200 -123
  221. package/rust/utils/src/path.rs +88 -0
  222. package/rust/utils/src/signature.rs +41 -0
  223. package/rust/utils/{spinner.rs → src/spinner.rs} +20 -21
  224. package/rust/utils/src/version.rs +27 -0
  225. package/rust/utils/{watcher.rs → src/watcher.rs} +46 -33
  226. package/rust/web/api.rs +5 -0
  227. package/rust/web/cdn.rs +34 -0
  228. package/rust/web/mod.rs +3 -0
  229. package/rust/web/sso.rs +5 -0
  230. package/templates/minimal/README.md +143 -127
  231. package/templates/welcome/README.md +143 -127
  232. package/templates/welcome/src/index.deva +56 -8
  233. package/templates/welcome/src/variables.deva +2 -4
  234. package/tests/integration.rs +21 -0
  235. package/tests/rust/cli_check_build.rs +21 -0
  236. package/tests/rust/cli_help.rs +12 -0
  237. package/tests/rust/cli_template_list.rs +10 -0
  238. package/tests/rust/cli_version.rs +11 -0
  239. package/tests/typescript/index.spec.ts +136 -0
  240. package/tests/typescript/playhead.spec.ts +36 -0
  241. package/tests/typescript/render_e2e.spec.ts +77 -0
  242. package/tsconfig.json +12 -10
  243. package/typescript/bin/index.ts +19 -5
  244. package/typescript/core/functions/index.ts +83 -0
  245. package/typescript/core/index.ts +6 -0
  246. package/typescript/core/types/index.ts +4 -0
  247. package/typescript/core/types/plugin.ts +19 -0
  248. package/typescript/core/types/result.ts +29 -0
  249. package/typescript/core/types/statement.ts +47 -0
  250. package/typescript/core/types/value.ts +29 -0
  251. package/typescript/index.ts +8 -1
  252. package/typescript/pkg/devalang_core.d.ts +4 -0
  253. package/typescript/pkg/devalang_core.ts +49 -0
  254. package/typescript/scripts/copy-wasm-dts.ts +41 -0
  255. package/typescript/scripts/postinstall.ts +85 -0
  256. package/typescript/scripts/version/bump.ts +0 -1
  257. package/typescript/scripts/version/fetch.ts +1 -6
  258. package/typescript/scripts/version/index.ts +0 -1
  259. package/docs/COMMANDS.md +0 -85
  260. package/docs/CONFIG.md +0 -30
  261. package/docs/SYNTAX.md +0 -210
  262. package/out-tsc/bin/devalang.exe +0 -0
  263. package/out-tsc/scripts/postbuild.js +0 -11
  264. package/rust/cli/build.rs +0 -137
  265. package/rust/cli/check.rs +0 -117
  266. package/rust/cli/play.rs +0 -193
  267. package/rust/config/loader.rs +0 -13
  268. package/rust/core/audio/engine.rs +0 -126
  269. package/rust/core/parser/handler/identifier.rs +0 -262
  270. package/rust/core/shared/duration.rs +0 -8
  271. package/rust/core/shared/mod.rs +0 -2
  272. package/rust/core/shared/value.rs +0 -18
  273. package/rust/core/utils/validation.rs +0 -35
  274. package/rust/utils/file.rs +0 -35
  275. package/rust/utils/signature.rs +0 -17
  276. package/rust/utils/version.rs +0 -15
  277. package/typescript/scripts/postbuild.ts +0 -8
@@ -0,0 +1,48 @@
1
+ use devalang_types::AddonMetadata;
2
+ use toml::Value;
3
+
4
+ pub fn parse_metadata_file(addon_type: &str, metadata_content: &str) -> Option<AddonMetadata> {
5
+ let parsed = metadata_content.parse::<Value>().ok()?;
6
+
7
+ let table = (match addon_type {
8
+ "bank" => parsed.get("bank"),
9
+ "plugin" => parsed.get("plugin"),
10
+ "preset" => parsed.get("preset"),
11
+ "template" => parsed.get("template"),
12
+ _ => None,
13
+ })?;
14
+
15
+ let name = table
16
+ .get("name")
17
+ .and_then(|v| v.as_str())
18
+ .unwrap_or("")
19
+ .to_string();
20
+ let version = table
21
+ .get("version")
22
+ .and_then(|v| v.as_str())
23
+ .unwrap_or("")
24
+ .to_string();
25
+ let description = table
26
+ .get("description")
27
+ .and_then(|v| v.as_str())
28
+ .unwrap_or("")
29
+ .to_string();
30
+ let author = table
31
+ .get("author")
32
+ .and_then(|v| v.as_str())
33
+ .unwrap_or("unknown")
34
+ .to_string();
35
+ let access = table
36
+ .get("access")
37
+ .and_then(|v| v.as_str())
38
+ .unwrap_or("")
39
+ .to_string();
40
+
41
+ Some(AddonMetadata {
42
+ name,
43
+ author,
44
+ version,
45
+ description,
46
+ access,
47
+ })
48
+ }
@@ -0,0 +1,5 @@
1
+ pub mod commands;
2
+ pub mod config;
3
+ pub mod fs;
4
+ pub mod install;
5
+ pub mod metadata;
@@ -1,29 +1,35 @@
1
- use std::{ fs, path::Path };
2
- use include_dir::{ include_dir, Dir };
3
- use crate::{ cli::template::get_available_templates, utils::file::copy_dir_recursive };
1
+ use crate::cli::template::commands::get_available_templates;
2
+ use devalang_utils::file::copy_dir_recursive;
3
+ use include_dir::{Dir, include_dir};
4
+ use std::{fs, path::Path};
4
5
 
5
6
  #[cfg(feature = "cli")]
6
- use inquire::{ Select, Confirm };
7
+ use inquire::{Confirm, Select};
7
8
 
8
9
  static TEMPLATES_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/templates");
9
10
 
10
11
  #[cfg(feature = "cli")]
11
12
  pub fn handle_init_command(name: Option<String>, template: Option<String>) {
12
13
  let current_dir = std::env::current_dir().unwrap();
13
- let project_name = name
14
- .clone()
15
- .unwrap_or_else(|| {
16
- current_dir.file_name().unwrap_or_default().to_string_lossy().to_string()
17
- });
14
+ let project_name = name.clone().unwrap_or_else(|| {
15
+ current_dir
16
+ .file_name()
17
+ .unwrap_or_default()
18
+ .to_string_lossy()
19
+ .to_string()
20
+ });
18
21
 
19
22
  // Select a template if not provided
20
23
  let selected_template = template.unwrap_or_else(|| {
21
- Select::new("Select a template for your project:", get_available_templates())
22
- .prompt()
23
- .unwrap_or_else(|_| {
24
- eprintln!("No template selected. Exiting...");
25
- std::process::exit(1);
26
- })
24
+ Select::new(
25
+ "Select a template for your project:",
26
+ get_available_templates(),
27
+ )
28
+ .prompt()
29
+ .unwrap_or_else(|_| {
30
+ eprintln!("No template selected. Exiting...");
31
+ std::process::exit(1);
32
+ })
27
33
  });
28
34
 
29
35
  if selected_template.is_empty() {
@@ -34,12 +40,11 @@ pub fn handle_init_command(name: Option<String>, template: Option<String>) {
34
40
  if name.is_none() {
35
41
  // Case of initialization in the current directory
36
42
  if fs::read_dir(&current_dir).unwrap().next().is_some() {
37
- let confirm = Confirm::new(
38
- "The current directory is not empty. Do you want to continue?"
39
- )
40
- .with_default(false)
41
- .prompt()
42
- .unwrap_or(false);
43
+ let confirm =
44
+ Confirm::new("The current directory is not empty. Do you want to continue?")
45
+ .with_default(false)
46
+ .prompt()
47
+ .unwrap_or(false);
43
48
 
44
49
  if !confirm {
45
50
  eprintln!("Operation cancelled by the user.");
@@ -65,7 +70,11 @@ pub fn handle_init_command(name: Option<String>, template: Option<String>) {
65
70
  fs::create_dir_all(&target_path).expect("Error creating project directory");
66
71
 
67
72
  scaffold_project_current_dir(&target_path, selected_template);
68
- println!("✅ Initialized '{}' project in: {}", project_name, target_path.display());
73
+ println!(
74
+ "✅ Initialized '{}' project in: {}",
75
+ project_name,
76
+ target_path.display()
77
+ );
69
78
  }
70
79
  }
71
80
 
@@ -75,5 +84,5 @@ fn scaffold_project_current_dir(path: &Path, template: String) {
75
84
  std::process::exit(1);
76
85
  });
77
86
 
78
- copy_dir_recursive(template_dir, path, &template_dir.path());
87
+ copy_dir_recursive(template_dir, path, template_dir.path());
79
88
  }
@@ -0,0 +1 @@
1
+ pub mod commands;
@@ -0,0 +1,118 @@
1
+ use crate::{
2
+ cli::install::{bank::install_bank, plugin::install_plugin},
3
+ config::settings::get_user_config,
4
+ web::api::get_api_url,
5
+ };
6
+ use devalang_types::AddonType;
7
+ use std::path::Path;
8
+
9
+ pub async fn install_addon(
10
+ addon_type: AddonType,
11
+ name: &str,
12
+ target_dir: &Path,
13
+ ) -> Result<(), String> {
14
+ match addon_type {
15
+ AddonType::Bank => install_bank(name, target_dir).await,
16
+ AddonType::Plugin => install_plugin(name, target_dir).await,
17
+ AddonType::Preset => Err("Preset installation not implemented".into()),
18
+ AddonType::Template => Err("Template installation not implemented".into()),
19
+ }
20
+ }
21
+
22
+ pub async fn ask_api_for_signed_url(addon_type: AddonType, slug: &str) -> Result<String, String> {
23
+ let api_url = get_api_url();
24
+
25
+ use devalang_utils::logger::LogLevel;
26
+ use devalang_utils::logger::Logger;
27
+
28
+ // Require an authenticated user for addon installation: token must be present and non-empty
29
+ let stored_token_opt = get_user_config().and_then(|cfg| {
30
+ let t = cfg.session.clone();
31
+ if t.trim().is_empty() { None } else { Some(t) }
32
+ });
33
+
34
+ if stored_token_opt.is_none() {
35
+ let logger = Logger::new();
36
+ let msg = "Authentication required — run `devalang login` to authenticate";
37
+ logger.log_message(LogLevel::Error, msg);
38
+ return Err("Authentication required: run 'devalang login' to authenticate".to_string());
39
+ }
40
+
41
+ let request_url = if let Some(token) = &stored_token_opt {
42
+ format!(
43
+ "{}/v1/assets/url?type={}&slug={}&token={}",
44
+ api_url,
45
+ match addon_type {
46
+ AddonType::Bank => "bank",
47
+ AddonType::Plugin => "plugin",
48
+ AddonType::Preset => "preset",
49
+ AddonType::Template => "template",
50
+ },
51
+ slug,
52
+ token
53
+ )
54
+ } else {
55
+ format!(
56
+ "{}/v1/assets/url?type={}&slug={}",
57
+ api_url,
58
+ match addon_type {
59
+ AddonType::Bank => "bank",
60
+ AddonType::Plugin => "plugin",
61
+ AddonType::Preset => "preset",
62
+ AddonType::Template => "template",
63
+ },
64
+ slug
65
+ )
66
+ };
67
+
68
+ let mut headers = reqwest::header::HeaderMap::new();
69
+ if let Some(token) = stored_token_opt {
70
+ headers.insert(
71
+ "Authorization",
72
+ format!("Bearer {}", token).parse().unwrap(),
73
+ );
74
+ }
75
+
76
+ let client: reqwest::Client = reqwest::Client::builder()
77
+ .default_headers(headers)
78
+ .build()
79
+ .map_err(|_| "Failed to build HTTP client".to_string())?;
80
+
81
+ let resp = client
82
+ .get(&request_url)
83
+ .send()
84
+ .await
85
+ .map_err(|e| format!("Failed to receive response: {}", e))?;
86
+
87
+ let status = resp.status();
88
+ let body_text = resp
89
+ .text()
90
+ .await
91
+ .map_err(|e| format!("Failed to read response body: {}", e))?;
92
+
93
+ // Try to parse JSON; if parsing fails, return body for diagnostics
94
+ let json: serde_json::Value = match serde_json::from_str(&body_text) {
95
+ Ok(v) => v,
96
+ Err(_) => {
97
+ return Err(format!(
98
+ "Invalid JSON response (status {}): {}",
99
+ status, body_text
100
+ ));
101
+ }
102
+ };
103
+
104
+ // Extract payload.url safely
105
+ let signed_url_opt = json
106
+ .get("payload")
107
+ .and_then(|p| p.get("url"))
108
+ .and_then(|u| u.as_str())
109
+ .map(|s| s.to_string());
110
+
111
+ if let Some(signed_url) = signed_url_opt {
112
+ Ok(signed_url)
113
+ } else {
114
+ // Provide detailed diagnostics to help user understand why it's null
115
+ let err_msg = format!("API returned no URL (status {}): {}", status, body_text);
116
+ Err(err_msg)
117
+ }
118
+ }
@@ -0,0 +1,53 @@
1
+ use crate::{
2
+ cli::install::addon::ask_api_for_signed_url, config::ops::load_config,
3
+ web::cdn::download_from_cdn,
4
+ };
5
+ use devalang_types::AddonType;
6
+ use devalang_utils::{
7
+ logger::{LogLevel, Logger},
8
+ path as path_utils,
9
+ };
10
+ use std::path::Path;
11
+
12
+ pub async fn install_bank(name: &str, target_dir: &Path) -> Result<(), String> {
13
+ let logger = Logger::new();
14
+
15
+ let signed_url = ask_api_for_signed_url(AddonType::Bank, name).await?;
16
+
17
+ let bank_dir = target_dir.join("banks");
18
+ let archive_path = path_utils::ensure_deva_dir()?
19
+ .join("tmp")
20
+ .join(format!("{}.devabank", name));
21
+ let extract_path = bank_dir.join(name);
22
+
23
+ download_from_cdn(&signed_url, &archive_path)
24
+ .await
25
+ .map_err(|e| format!("Failed to download: {}", e))?;
26
+
27
+ if extract_path.exists() {
28
+ logger.log_message(
29
+ LogLevel::Warning,
30
+ &format!(
31
+ "Bank '{}' already exists at '{}'. Skipping install.",
32
+ name,
33
+ extract_path.display()
34
+ ),
35
+ );
36
+
37
+ return Ok(());
38
+ }
39
+
40
+ // Add the bank to the config
41
+ let config_path = path_utils::get_devalang_config_path()?;
42
+ let _config = load_config(Some(&config_path))
43
+ .ok_or_else(|| format!("Failed to load config from '{}'", config_path.display()))?;
44
+
45
+ let _dependency_path = &format!("devalang://bank/{}", name);
46
+
47
+ devalang_utils::file::extract_zip_safely(&archive_path, &extract_path)
48
+ .map_err(|e| format!("Failed to extract: {}", e))?;
49
+
50
+ // TODO: Add the bank to the config
51
+
52
+ Ok(())
53
+ }
@@ -0,0 +1,35 @@
1
+ use crate::cli::install::addon::install_addon;
2
+ #[cfg(feature = "cli")]
3
+ use devalang_types::AddonType;
4
+ use devalang_utils::path as path_utils;
5
+
6
+ /// Handles the installation command for a given addon type and name.
7
+ #[cfg(feature = "cli")]
8
+ pub async fn handle_install_command(name: String, addon_type: AddonType) -> Result<(), String> {
9
+ use devalang_utils::{
10
+ logger::{LogLevel, Logger},
11
+ spinner::start_spinner,
12
+ };
13
+
14
+ let logger = Logger::new();
15
+ let deva_dir = path_utils::ensure_deva_dir()?;
16
+
17
+ let spinner = start_spinner("Installing...");
18
+
19
+ if let Err(e) = install_addon(addon_type.clone(), name.as_str(), &deva_dir).await {
20
+ spinner.finish_and_clear();
21
+ logger.log_message_with_trace(
22
+ LogLevel::Error,
23
+ &format!("Error installing {:?} '{}'", addon_type, name),
24
+ vec![&e],
25
+ );
26
+ } else {
27
+ spinner.finish_and_clear();
28
+ logger.log_message(
29
+ LogLevel::Success,
30
+ &format!("{:?} '{}' installed successfully!", addon_type, name),
31
+ );
32
+ }
33
+
34
+ Ok(())
35
+ }
@@ -0,0 +1,4 @@
1
+ pub mod addon;
2
+ pub mod bank;
3
+ pub mod commands;
4
+ pub mod plugin;
@@ -0,0 +1,61 @@
1
+ use crate::{
2
+ config::ops::load_config,
3
+ web::cdn::{download_from_cdn, get_cdn_url},
4
+ };
5
+ use devalang_utils::path as path_utils;
6
+ use std::{fs, path::Path};
7
+
8
+ pub async fn install_plugin(name: &str, target_dir: &Path) -> Result<(), String> {
9
+ let cdn_url = get_cdn_url();
10
+ let url = format!("{}/plugin/{}/download", cdn_url, name);
11
+
12
+ let plugin_dir = target_dir.join("plugins");
13
+
14
+ // Ensure .deva/tmp exists and build archive path
15
+ let deva_dir = path_utils::ensure_deva_dir()?;
16
+ let tmp_dir = deva_dir.join("tmp");
17
+ if !tmp_dir.exists() {
18
+ fs::create_dir_all(&tmp_dir)
19
+ .map_err(|e| format!("Failed to create tmp dir '{}': {}", tmp_dir.display(), e))?;
20
+ }
21
+
22
+ let archive_path = tmp_dir.join(format!("{}.devaplugin", name));
23
+ let extract_path = plugin_dir.join(name);
24
+
25
+ if extract_path.exists() {
26
+ println!(
27
+ "Plugin '{}' already exists at '{}'. Skipping install.",
28
+ name,
29
+ extract_path.display()
30
+ );
31
+ return Ok(());
32
+ }
33
+
34
+ download_from_cdn(&url, &archive_path)
35
+ .await
36
+ .map_err(|e| format!("Failed to download: {}", e))?;
37
+
38
+ // Add the plugin to the config: locate project root from target_dir
39
+ let project_root = path_utils::find_project_root_from(target_dir)
40
+ .ok_or_else(|| "Failed to determine project root from target_dir".to_string())?;
41
+
42
+ let config_path = project_root.join(path_utils::DEVALANG_CONFIG);
43
+ if !config_path.exists() {
44
+ return Err(format!(
45
+ "Config file not found at '{}'. Please run 'devalang init' before adding an addon",
46
+ config_path.display()
47
+ ));
48
+ }
49
+
50
+ let _config = load_config(Some(&config_path))
51
+ .ok_or_else(|| format!("Failed to load config from '{}'", config_path.display()))?;
52
+
53
+ let _dependency_path = &format!("devalang://plugin/{}", name);
54
+
55
+ devalang_utils::file::extract_zip_safely(&archive_path, &extract_path)
56
+ .map_err(|e| format!("Failed to extract: {}", e))?;
57
+
58
+ // TODO: Add the plugin to the config
59
+
60
+ Ok(())
61
+ }
@@ -0,0 +1,124 @@
1
+ use crate::config::settings::set_user_config_value;
2
+ use crate::web::sso::get_sso_url;
3
+ use std::str::FromStr;
4
+ use tiny_http::Header;
5
+ use tiny_http::{Response, Server};
6
+ use webbrowser;
7
+
8
+ /// Handle the login command
9
+ /// This function initiates the login process by opening the browser and waiting for the callback.
10
+ #[cfg(feature = "cli")]
11
+ pub async fn handle_login_command() -> Result<(), String> {
12
+ use devalang_utils::logger::LogLevel;
13
+ use devalang_utils::logger::Logger;
14
+ use devalang_utils::spinner::start_spinner;
15
+
16
+ let logger = Logger::new();
17
+
18
+ let mut listener_port = 7878;
19
+
20
+ let test_port_already_in_use = format!("127.0.0.1:{}", listener_port);
21
+ while std::net::TcpListener::bind(&test_port_already_in_use).is_err() {
22
+ listener_port += 1;
23
+ }
24
+
25
+ let redirect_uri = format!("http://127.0.0.1:{}/callback", listener_port);
26
+ let login_url = format!(
27
+ "{}/?response_type=code&referer=cli&redirect_uri={}",
28
+ get_sso_url(),
29
+ redirect_uri
30
+ );
31
+
32
+ if webbrowser::open(&login_url).is_ok() {
33
+ logger.log_message(LogLevel::Info, "Opening browser for login...");
34
+ logger.log_message(
35
+ LogLevel::Info,
36
+ &format!(
37
+ "If the browser does not open, please visit the following URL: {}",
38
+ login_url
39
+ ),
40
+ );
41
+ } else {
42
+ logger.log_message(
43
+ LogLevel::Info,
44
+ "Please open the following URL in your browser to login:",
45
+ );
46
+ logger.log_message(LogLevel::Info, &login_url);
47
+ }
48
+
49
+ let server = Server::http(format!("127.0.0.1:{}", listener_port)).unwrap();
50
+
51
+ let spinner = start_spinner("Waiting for authentication...");
52
+
53
+ for request in server.incoming_requests() {
54
+ let query = request.url().to_string();
55
+ if request.url().starts_with("/callback") {
56
+ if query.contains("session=") || query.contains("error=") {
57
+ let token = query.split("session=").nth(1).unwrap_or("").to_string();
58
+
59
+ if !token.is_empty() {
60
+ let response_html = r#"
61
+ <html>
62
+ <body>
63
+ <h1>Authentication Successful</h1>
64
+ <h2>You can now close this window.</h2>
65
+ </body>
66
+ </html>
67
+ "#;
68
+
69
+ let response = Response::from_string(response_html)
70
+ .with_status_code(200)
71
+ .with_header(Header::from_str("Content-Type: text/html").unwrap());
72
+
73
+ request.respond(response).unwrap();
74
+
75
+ save_token(&token);
76
+
77
+ spinner.finish_and_clear();
78
+
79
+ logger.log_message(
80
+ LogLevel::Success,
81
+ "Authentication successful. Token saved to ~/.devalang/config.json",
82
+ );
83
+
84
+ break;
85
+ } else {
86
+ spinner.finish_and_clear();
87
+ logger.log_message(LogLevel::Error, "Invalid session token.");
88
+ request
89
+ .respond(Response::from_string("Invalid session token."))
90
+ .unwrap();
91
+
92
+ break;
93
+ }
94
+ } else {
95
+ println!("Invalid callback: {}", request.url());
96
+
97
+ spinner.finish_and_clear();
98
+ logger.log_message(LogLevel::Error, "Invalid callback.");
99
+ request
100
+ .respond(Response::from_string("Invalid callback."))
101
+ .unwrap();
102
+
103
+ break;
104
+ }
105
+ } else if request.url().starts_with("/favicon.ico") {
106
+ // Ignore favicon requests
107
+ } else {
108
+ spinner.finish_and_clear();
109
+ logger.log_message(LogLevel::Error, "Invalid request.");
110
+ request
111
+ .respond(Response::from_string("Invalid request."))
112
+ .unwrap();
113
+
114
+ break;
115
+ }
116
+ }
117
+
118
+ Ok(())
119
+ }
120
+
121
+ /// Save the session token to a file in the user's home directory
122
+ fn save_token(token: &str) {
123
+ set_user_config_value("session", serde_json::Value::String(token.to_string()));
124
+ }
@@ -0,0 +1 @@
1
+ pub mod commands;