ninja-core 1.14.1

A powerful, cross-platform package manager and runtime for managing tools and plugins (shurikens)
Documentation
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
427
428
use crate::common::types::ShurikenState;
use crate::manager::ShurikenManager;
use crate::utils::get_port_owner;
use crate::{common::types::FieldValue, scripting::NinjaEngine, scripting::templater::Templater};
use anyhow::Result;
use log::info;
use serde::{Deserialize, Serialize};
use serde_json::{Value as JsonValue, json};
use std::sync::Arc;
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
};
use tokio::{fs, sync::Mutex};

/// Represents a tool script associated with a Shuriken.
///
/// Tools are executable scripts that can be invoked to perform
/// specific tasks related to the Shuriken.
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Tool {
    /// The name of the tool
    pub name: String,
    /// Path to the tool's script file
    pub script: PathBuf,
    /// Optional human-readable description
    pub description: Option<String>,
}

/// Configuration settings for a Shuriken.
///
/// Specifies the path to configuration templates and runtime options.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ShurikenConfig {
    /// Path to the configuration file template (relative to Shuriken directory)
    #[serde(rename = "config-path")]
    pub config_path: PathBuf,
    /// Runtime configuration options applied during execution
    pub options: Option<HashMap<String, FieldValue>>,
}

/// Metadata describing a Shuriken in shuriken.toml.
///
/// Contains identifying information, type, version, and optional startup details.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ShurikenMetadata {
    /// Human-readable name of the Shuriken
    pub name: String,
    /// Unique identifier for the Shuriken
    pub id: String,
    /// Version string (e.g., "1.0.0")
    pub version: String,
    /// Optional list of TCP ports used by the service
    pub ports: Option<Vec<u16>>,
    /// Whether to verify ports are free before starting (default: false)
    #[serde(rename = "check-ports")]
    pub check_ports: Option<bool>,
    /// Path to the main startup script
    #[serde(rename = "script-path")]
    pub script_path: Option<PathBuf>,
    /// Type of Shuriken: "daemon", "binary", "library", etc.
    #[serde(rename = "type")]
    pub shuriken_type: String,
}

/// Logging configuration for a Shuriken.
///
/// Specifies where the Shuriken should write its log files.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LogsConfig {
    /// Path where logs should be written
    #[serde(rename = "log-path")]
    pub log_path: PathBuf,
}

async fn atomic_write_json(path: &Path, value: &JsonValue) -> Result<(), String> {
    use tokio::fs;

    let tmp_path = path.with_extension("tmp");

    let data = serde_json::to_vec(value).map_err(|e| e.to_string())?;
    fs::write(&tmp_path, data)
        .await
        .map_err(|e| format!("Failed to write tmp lockfile: {e}"))?;

    // Atomic-ish replace on most platforms
    std::fs::rename(&tmp_path, path).map_err(|e| format!("Failed to replace lockfile: {e}"))?;

    Ok(())
}

/// Represents the complete TOML structure of a shuriken.toml file.
///
/// This is the raw representation before being parsed into a `Shuriken` struct.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ShurikenToml {
    /// Required: Shuriken metadata section
    pub shuriken: ShurikenMetadata,
    /// Optional: Configuration section
    pub config: Option<ShurikenConfig>,
    /// Optional: Logging configuration
    pub logs: Option<LogsConfig>,
    /// Optional: Available tools
    pub tools: Option<Vec<Tool>>,
}

/// A loaded and managed Shuriken service instance.
///
/// Represents an installed Shuriken with metadata, configuration, logging, and runtime state.
/// The `state` and `dirty` flags are not serialized.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Shuriken {
    /// Shuriken metadata
    #[serde(rename = "shuriken")]
    pub metadata: ShurikenMetadata,
    /// Configuration settings
    pub config: Option<ShurikenConfig>,
    /// Logging configuration
    pub logs: Option<LogsConfig>,
    /// Available tools
    pub tools: Option<Vec<Tool>>,

    /// Current runtime state (Running, Idle, or Error)
    #[serde(skip)]
    pub state: Arc<Mutex<ShurikenState>>,
    /// Whether in-memory state differs from disk
    #[serde(skip)]
    pub dirty: Arc<Mutex<bool>>,
}

impl Shuriken {
    /// Starts this Shuriken by executing its startup script.
    ///
    /// Performs port availability checks if configured, creates necessary directories,
    /// compiles and executes the startup script, and writes a lock file.
    /// Updates internal state to `Running` on success.
    ///
    /// # Arguments
    /// - `engine`: Reference to the Lua scripting engine
    /// - `shuriken_dir`: Directory containing the Shuriken's files
    /// - `mgr`: Optional manager reference for script context
    ///
    /// # Returns
    /// - `Ok(())` if startup succeeded
    /// - `Err(msg)` if port check fails, script execution fails, or I/O fails
    pub async fn start(
        &self,
        engine: &NinjaEngine,
        shuriken_dir: &Path,
        mgr: Option<ShurikenManager>,
    ) -> Result<(), String> {
        info!("Starting shuriken {}", self.metadata.name);

        let lock_dir = shuriken_dir.join(".ninja");
        tokio::fs::create_dir_all(&lock_dir)
            .await
            .map_err(|e| format!("Failed to create .ninja directory: {}", e))?;
        let lock_path = lock_dir.join("shuriken.lck");

        if self.metadata.check_ports.unwrap_or(false) {
            info!("Checking ports for shuriken {}", self.metadata.name);
            if let Some(ports) = &self.metadata.ports {
                for port in ports {
                    let port_owner = get_port_owner(*port);
                    if let Some(owner) = port_owner {
                        let name = owner.name.unwrap_or_else(|| "Unknown".into());
                        return Err(format!(
                            "Port {} is already in use by process {}. Please free the port and try again.",
                            port, name
                        ));
                    }
                    else {
                        info!("Port {} is free", port);
                    }
                }
            }
        }

        if self.metadata.shuriken_type == "daemon"
            && let Some(script_path) = &self.metadata.script_path
        {
            let full_script_path = self.resolve_script_path(script_path, shuriken_dir);

            let stem = full_script_path
                .file_stem()
                .ok_or_else(|| "Invalid script path".to_string())?
                .to_string_lossy()
                .to_string();
            let compiled_path = lock_dir.join(format!("{stem}.ns"));

            if let Some(mgr) = mgr {
                engine
                    .execute_function("start", &compiled_path, Some(shuriken_dir), Some(mgr))
                    .await
                    .map_err(|e| format!("Script start failed: {}", e))?;
            }

            let lockfile_data = json!({
                "name": self.metadata.name,
                "type": "Script",
            });

            atomic_write_json(&lock_path, &lockfile_data).await?;

            let mut state = self.state.lock().await;
            *state = ShurikenState::Running;
        }
        Ok(())
    }

    /// Removes the lock file for this Shuriken (without stopping it).
    ///
    /// Useful for recovering from crashes where the lock file wasn't cleaned up.
    ///
    /// # Arguments
    /// - `root_path`: The root Ninja directory
    ///
    /// # Returns
    /// - `Ok(())` if lock file removed or didn't exist
    /// - `Err` if operation fails
    pub async fn lockpick(&self, root_path: &Path) -> anyhow::Result<()> {
        let root_path = root_path
            .join("shurikens")
            .join(&self.metadata.name.to_lowercase())
            .join(".ninja");

        if root_path.join("shuriken.lck").exists() {
            fs::remove_file(root_path.join("shuriken.lck")).await?;
        }

        Ok(())
    }

    /// Configures this Shuriken by templating its configuration file.
    ///
    /// Uses the `Templater` to render configuration templates with provided field values,
    /// then executes the `post_config` function if a script path is defined.
    ///
    /// # Arguments
    /// - `root_path`: The root Ninja directory
    /// - `engine`: Reference to the Lua scripting engine
    /// - `mgr`: Optional manager reference for script context
    ///
    /// # Returns
    /// - `Ok(())` if configuration succeeded
    /// - `Err` if no config/script path exists or template/script execution fails
    pub async fn configure(
        &self,
        root_path: &Path,
        engine: &NinjaEngine,
        mgr: Option<ShurikenManager>,
    ) -> anyhow::Result<()> {
        if let Some(ctx) = &self.config
            && let Some(script_path) = &self.metadata.script_path
        {
            let shuriken_fields = ctx.options.clone();
            let mut fields = HashMap::new();
            if let Some(partial_fields) = shuriken_fields {
                for (name, value) in partial_fields {
                    fields.insert(name, value);
                }
            }

            // Construct full path to the shuriken folder
            let shuriken_path = root_path
                .join("shurikens")
                .join(&self.metadata.name.to_lowercase());

            // Ensure the directory exists
            fs::create_dir_all(&shuriken_path).await?;

            // Initialize Templater with the fields and shuriken path
            let templater = Templater::new(fields, shuriken_path.clone())?;

            // Full path to write the generated config
            let config_full_path = shuriken_path.join(&ctx.config_path);

            // Ensure the parent directory of the config file exists
            if let Some(parent) = config_full_path.parent() {
                fs::create_dir_all(parent).await?;
            }

            templater
                .generate_config(config_full_path)
                .await
                .map_err(|e| anyhow::Error::msg(e.to_string()))?;

            engine
                .execute_function("post_config", script_path, Some(root_path), mgr)
                .await?;

            Ok(())
        } else {
            Err(anyhow::Error::msg(
                "Shuriken does not have a config or script path",
            ))
        }
    }

    /// Imports data for this Shuriken by executing its import script.
    ///
    /// Executes the `import` function if a script path is defined.
    /// Used to prepare or initialize data before the Shuriken starts.
    ///
    /// # Arguments
    /// - `engine`: Reference to the Lua scripting engine
    /// - `shuriken_dir`: Directory containing the Shuriken's files
    /// - `mgr`: Optional manager reference for script context
    ///
    /// # Returns
    /// - `Ok(())` if import succeeded
    /// - `Err(msg)` if no script path exists or script execution fails
    pub async fn import(
        &self,
        engine: &NinjaEngine,
        shuriken_dir: &Path,
        mgr: Option<ShurikenManager>,
    ) -> Result<(), String> {
        info!(
            "Shuriken {} is importing data! (if there is any)",
            &self.metadata.name
        );

        if let Some(script_path) = &self.metadata.script_path {
            let full_script_path = self.resolve_script_path(script_path, shuriken_dir);

            let stem = full_script_path
                .file_stem()
                .ok_or_else(|| "Invalid script".to_string())?
                .to_string_lossy()
                .to_string();
            let lock_dir = shuriken_dir.join(".ninja");
            let compiled_path = lock_dir.join(format!("{stem}.ns"));

            if let Some(mgr) = mgr {
                engine
                    .execute_function("import", &compiled_path, Some(shuriken_dir), Some(mgr))
                    .await
                    .map_err(|e| format!("Script import failed: {}", e))?;
            }
            Ok(())
        } else {
            Err("Shuriken does not have a script path".to_string())
        }
    }

    /// Resolves a script path, handling both absolute and relative paths.
    ///
    /// If the path is absolute, returns it as-is.
    /// Otherwise, joins it with the shuriken directory.
    ///
    /// # Arguments
    /// - `script_path`: The script path to resolve
    /// - `shuriken_dir`: The Shuriken's directory for relative resolution
    ///
    /// # Returns
    /// The resolved absolute path
    fn resolve_script_path(&self, script_path: &Path, shuriken_dir: &Path) -> PathBuf {
        if script_path.is_absolute() {
            script_path.to_path_buf()
        } else {
            shuriken_dir.join(script_path)
        }
    }

    /// Stops this running Shuriken by executing its stop script.
    ///
    /// Calls the `stop` function if defined, removes the lock file,
    /// and updates internal state to `Idle`.
    ///
    /// # Arguments
    /// - `engine`: Reference to the Lua scripting engine
    /// - `shuriken_dir`: Directory containing the Shuriken's files
    /// - `mgr`: Optional manager reference for script context
    ///
    /// # Returns
    /// - `Ok(())` if stop succeeded
    /// - `Err(msg)` if script execution fails or lock file removal fails
    pub async fn stop(
        &mut self,
        engine: &NinjaEngine,
        shuriken_dir: &Path,
        mgr: Option<ShurikenManager>,
    ) -> Result<(), String> {
        info!("Stopping shuriken {}", self.metadata.name);
        let lock_path = shuriken_dir.join(".ninja").join("shuriken.lck");

        if self.metadata.shuriken_type == "daemon"
            && let Some(script_path) = &self.metadata.script_path
        {
            let full_script_path = self.resolve_script_path(script_path, shuriken_dir);

            let stem = full_script_path
                .file_stem()
                .ok_or_else(|| "Invalid script".to_string())?
                .to_string_lossy()
                .to_string();
            let lock_dir = shuriken_dir.join(".ninja");
            let compiled_path = lock_dir.join(format!("{stem}.ns"));

            if let Some(mgr) = mgr {
                {
                    let mut state = self.state.lock().await;
                    engine
                        .execute_function("stop", &compiled_path, Some(shuriken_dir), Some(mgr))
                        .await
                        .map_err(|e| {
                            *state = ShurikenState::Error(e.to_string());
                            format!("Script stop failed: {}", e)
                        })?;
                } // Lock released here
            }

            if lock_path.exists() {
                tokio::fs::remove_file(&lock_path)
                    .await
                    .map_err(|e| format!("Failed to remove lockfile: {}", e))?;
            }

            let mut state = self.state.lock().await;
            *state = ShurikenState::Idle;
            Ok(())
        }
        else {
            return Err("Shuriken does not have a script path or is not a daemon".to_string());
        }
    }
}