xbp 10.5.0

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
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
//! deployment configuration management module
//!
//! handles reading and writing deployment configurations
//! including xbp.json files and cli argument parsing
//! supports both legacy single service and new multi service formats
//! validates service configurations for duplicates and invalid targets

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

use crate::utils::{collapse_home_to_env, expand_home_in_string, find_xbp_config_upwards};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceCommands {
    #[serde(default)]
    pub pre: Option<String>,
    #[serde(default)]
    pub install: Option<String>,
    #[serde(default)]
    pub build: Option<String>,
    #[serde(default)]
    pub start: Option<String>,
    #[serde(default)]
    pub dev: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceConfig {
    pub name: String,
    pub target: String,
    pub branch: String,
    pub port: u16,
    #[serde(default)]
    pub root_directory: Option<String>,
    #[serde(default)]
    pub environment: Option<HashMap<String, String>>,
    #[serde(default)]
    pub url: Option<String>,
    #[serde(default)]
    pub healthcheck_path: Option<String>,
    #[serde(default)]
    pub restart_policy: Option<String>,
    #[serde(default)]
    pub restart_policy_max_failure_count: Option<u32>,
    #[serde(default)]
    pub start_wrapper: Option<String>,
    #[serde(default)]
    pub commands: Option<ServiceCommands>,
    #[serde(default)]
    pub force_run_from_root: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct XbpConfig {
    pub project_name: String,
    pub port: u16,
    pub build_dir: String,
    #[serde(default)]
    pub app_type: Option<String>,
    #[serde(default)]
    pub build_command: Option<String>,
    #[serde(default)]
    pub start_command: Option<String>,
    #[serde(default)]
    pub install_command: Option<String>,
    #[serde(default)]
    pub environment: Option<HashMap<String, String>>,
    #[serde(default)]
    pub services: Option<Vec<ServiceConfig>>,
    #[serde(default)]
    pub kafka_brokers: Option<String>,
    #[serde(default)]
    pub kafka_topic: Option<String>,
    #[serde(default)]
    pub kafka_public_url: Option<String>,
    #[serde(default)]
    pub log_files: Option<Vec<String>>,
    #[serde(default)]
    pub monitor_url: Option<String>,
    #[serde(default)]
    pub monitor_method: Option<String>,
    #[serde(default)]
    pub monitor_expected_code: Option<u16>,
    #[serde(default)]
    pub monitor_interval: Option<u64>,
    // Legacy fields for backward compatibility
    #[serde(default)]
    pub target: Option<String>,
    #[serde(default)]
    pub branch: Option<String>,
    #[serde(default)]
    pub crate_name: Option<String>,
    #[serde(default)]
    pub npm_script: Option<String>,
    #[serde(default)]
    pub port_storybook: Option<u16>,
    #[serde(default)]
    pub url: Option<String>,
    #[serde(default)]
    pub url_storybook: Option<String>,
}

#[derive(Debug, Clone)]
pub struct DeploymentConfig {
    pub app_name: String,
    pub port: u16,
    pub app_dir: PathBuf,
    pub build_command: Option<String>,
    pub start_command: Option<String>,
    pub install_command: Option<String>,
    pub environment: HashMap<String, String>,
}

impl DeploymentConfig {
    /// Create deployment config from CLI arguments and xbp.json fallback
    pub async fn from_args_or_config(
        app_name: Option<String>,
        port: Option<u16>,
        app_dir: Option<PathBuf>,
        config_path: Option<PathBuf>,
    ) -> Result<Self, String> {
        // Try to load from xbp.json if not all args provided
        let xbp_config = if app_name.is_none() || port.is_none() || app_dir.is_none() {
            match Self::load_xbp_config(config_path).await {
                Ok(config) => Some(config),
                Err(_) => None, // Ignore config file errors if we have CLI args
            }
        } else {
            None
        };

        let app_name = app_name
            .or_else(|| xbp_config.as_ref().map(|c| c.project_name.clone()))
            .ok_or("Missing app name")?;

        let port = port
            .or_else(|| xbp_config.as_ref().map(|c| c.port))
            .ok_or("Missing port")?;

        let app_dir = app_dir
            .or_else(|| xbp_config.as_ref().map(|c| PathBuf::from(&c.build_dir)))
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_default());

        let app_dir = app_dir
            .canonicalize()
            .map_err(|e| format!("Failed to resolve app directory: {}", e))?;

        // Get build/start commands from config if available
        let build_command = xbp_config.as_ref().and_then(|c| c.build_command.clone());
        let start_command = xbp_config.as_ref().and_then(|c| c.start_command.clone());
        let install_command = xbp_config.as_ref().and_then(|c| c.install_command.clone());
        let environment = xbp_config
            .as_ref()
            .and_then(|c| c.environment.clone())
            .unwrap_or_default();

        Ok(DeploymentConfig {
            app_name,
            port,
            app_dir,
            build_command,
            start_command,
            install_command,
            environment,
        })
    }

    /// Load xbp.json configuration from file
    pub async fn load_xbp_config(config_path: Option<PathBuf>) -> Result<XbpConfig, String> {
        let cwd = std::env::current_dir().unwrap_or_default();

        let (project_root, resolved_path, resolved_kind) = if let Some(p) = config_path.clone() {
            let root = p
                .parent()
                .map(|pp| pp.to_path_buf())
                .unwrap_or_else(|| cwd.clone());
            (root, p, "auto")
        } else {
            let found = find_xbp_config_upwards(&cwd)
                .ok_or_else(|| "Configuration file not found".to_string())?;
            (found.project_root, found.config_path, found.kind)
        };

        let dot_json = project_root.join(".xbp").join("xbp.json");
        let dot_yaml = project_root.join(".xbp").join("xbp.yaml");

        if config_path.is_none() && dot_json.exists() && !dot_yaml.exists() {
            if let Ok(contents) = fs::read_to_string(&dot_json) {
                if let Ok(mut cfg) = serde_json::from_str::<XbpConfig>(&contents) {
                    cfg.build_dir = collapse_home_to_env(&cfg.build_dir);
                    if let Some(services) = &mut cfg.services {
                        for s in services {
                            if let Some(rd) = &s.root_directory {
                                s.root_directory = Some(collapse_home_to_env(rd));
                            }
                        }
                    }
                    if let Ok(yaml) = serde_yaml::to_string(&cfg) {
                        let _ = fs::create_dir_all(project_root.join(".xbp"));
                        let _ = fs::write(&dot_yaml, yaml);
                    }
                }
            }
        }

        let (config_path, kind) = (resolved_path, resolved_kind);

        let content = fs::read_to_string(&config_path)
            .map_err(|e| format!("Failed to read config: {}", e))?;

        let mut config: XbpConfig = match kind {
            "yaml" => serde_yaml::from_str(&content)
                .map_err(|e| format!("Failed to parse YAML config: {}", e))?,
            "json" => serde_json::from_str(&content)
                .map_err(|e| format!("Failed to parse JSON config: {}", e))?,
            _ => {
                if config_path
                    .extension()
                    .and_then(|s| s.to_str())
                    .map(|ext| ext.eq_ignore_ascii_case("yaml") || ext.eq_ignore_ascii_case("yml"))
                    .unwrap_or(false)
                {
                    serde_yaml::from_str(&content)
                        .map_err(|e| format!("Failed to parse YAML config: {}", e))?
                } else {
                    serde_json::from_str(&content)
                        .map_err(|e| format!("Failed to parse JSON config: {}", e))?
                }
            }
        };

        config.build_dir = expand_home_in_string(&config.build_dir);
        if let Some(services) = &mut config.services {
            for s in services {
                if let Some(rd) = &s.root_directory {
                    s.root_directory = Some(expand_home_in_string(rd));
                }
            }
        }

        // Validate services if present
        if let Some(services) = &config.services {
            validate_services(services)?;
        }

        Ok(config)
    }

    /// Save updated configuration back to xbp.json
    pub async fn save_xbp_config(&self, config_path: Option<PathBuf>) -> Result<(), String> {
        let dir = self.app_dir.join(".xbp");
        let json_path = dir.join("xbp.json");
        let yaml_path = dir.join("xbp.yaml");

        // Create .xbp directory if it doesn't exist
        fs::create_dir_all(&dir)
            .map_err(|e| format!("Failed to create config directory: {}", e))?;

        let mut xbp_config = XbpConfig {
            project_name: self.app_name.clone(),
            port: self.port,
            build_dir: self.app_dir.to_string_lossy().to_string(),
            app_type: None,
            build_command: self.build_command.clone(),
            start_command: self.start_command.clone(),
            install_command: self.install_command.clone(),
            environment: if self.environment.is_empty() {
                None
            } else {
                Some(self.environment.clone())
            },
            services: None,
            kafka_brokers: None,
            kafka_topic: None,
            kafka_public_url: None,
            log_files: None,
            monitor_url: None,
            monitor_method: None,
            monitor_expected_code: None,
            monitor_interval: None,
            target: None,
            branch: None,
            crate_name: None,
            npm_script: None,
            port_storybook: None,
            url: None,
            url_storybook: None,
        };

        xbp_config.build_dir = collapse_home_to_env(&xbp_config.build_dir);

        let write_json = json_path.exists() || !yaml_path.exists();
        let write_yaml = yaml_path.exists() || !json_path.exists();

        if write_json {
            let content = serde_json::to_string_pretty(&xbp_config)
                .map_err(|e| format!("Failed to serialize config (json): {}", e))?;
            let out_path = config_path.unwrap_or(json_path);
            fs::write(&out_path, content)
                .map_err(|e| format!("Failed to write config file: {}", e))?;
        }

        if write_yaml {
            let yaml = serde_yaml::to_string(&xbp_config)
                .map_err(|e| format!("Failed to serialize config (yaml): {}", e))?;
            fs::write(&yaml_path, yaml)
                .map_err(|e| format!("Failed to write yaml config: {}", e))?;
        }

        Ok(())
    }

    /// Update port in the configuration
    pub fn update_port(&mut self, new_port: u16) {
        self.port = new_port;
    }

    /// Merge with deployment recommendations from project detection
    pub fn merge_with_recommendations(
        &mut self,
        recommendations: &super::project_detector::DeploymentRecommendations,
    ) {
        // Use recommendations if not already set
        if self.build_command.is_none() {
            self.build_command = recommendations.build_command.clone();
        }

        if self.start_command.is_none() {
            self.start_command = recommendations.start_command.clone();
        }

        if self.install_command.is_none() {
            self.install_command = recommendations.install_command.clone();
        }

        // Use recommended process name if app_name is generic
        if let Some(recommended_name) = &recommendations.process_name {
            if self.app_name == "app" || self.app_name == "unknown" {
                self.app_name = recommended_name.clone();
            }
        }
    }
}

/// Validate services configuration
pub fn validate_services(services: &[ServiceConfig]) -> Result<(), String> {
    let mut names = std::collections::HashSet::new();
    let mut ports = std::collections::HashSet::new();
    let mut urls = std::collections::HashSet::new();

    for service in services {
        // Check for duplicate names
        if !names.insert(&service.name) {
            return Err(format!("Duplicate service name found: {}", service.name));
        }

        // Check for duplicate ports
        if !ports.insert(service.port) {
            return Err(format!("Duplicate port found: {}", service.port));
        }

        // Check for duplicate URLs
        if let Some(url) = &service.url {
            if !urls.insert(url) {
                return Err(format!("Duplicate URL found: {}", url));
            }
        }

        // Validate target
        let valid_targets = ["python", "expressjs", "nextjs", "rust"];
        if !valid_targets.contains(&service.target.as_str()) {
            return Err(format!(
                "Invalid target '{}' for service '{}'. Must be one of: python, expressjs, nextjs, rust",
                service.target, service.name
            ));
        }
    }

    Ok(())
}

/// Find a service by name
pub fn get_service_by_name(config: &XbpConfig, name: &str) -> Result<ServiceConfig, String> {
    if let Some(services) = &config.services {
        services
            .iter()
            .find(|s| s.name == name)
            .cloned()
            .ok_or_else(|| format!("Service '{}' not found in configuration", name))
    } else {
        Err("No services configured. This project uses legacy single-service format.".to_string())
    }
}

/// Get all services from config (or create a virtual one from legacy config)
pub fn get_all_services(config: &XbpConfig) -> Vec<ServiceConfig> {
    if let Some(services) = &config.services {
        services.clone()
    } else {
        // Backward compatibility: create a virtual service from top-level config
        vec![ServiceConfig {
            name: config.project_name.clone(),
            target: config.target.clone().unwrap_or_else(|| "rust".to_string()),
            branch: config.branch.clone().unwrap_or_else(|| "main".to_string()),
            port: config.port,
            root_directory: Some(config.build_dir.clone()),
            environment: config.environment.clone(),
            url: config.url.clone(),
            healthcheck_path: None,
            restart_policy: Some("on_failure".to_string()),
            restart_policy_max_failure_count: Some(10),
            start_wrapper: Some("pm2".to_string()),
            commands: Some(ServiceCommands {
                pre: None,
                install: config.install_command.clone(),
                build: config.build_command.clone(),
                start: config.start_command.clone(),
                dev: None,
            }),
            force_run_from_root: Some(false),
        }]
    }
}