homeboy 0.50.1

CLI for multi-component deployment and development workflow automation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use serde::Serialize;
use std::path::PathBuf;

use crate::component::{self, Component};
use crate::config::{is_json_input, parse_bulk_ids};
use crate::error::{Error, Result};
use crate::module::{self, exec_context};
use crate::output::{BulkResult, BulkSummary, ItemOutcome};
use crate::paths;
use crate::permissions;
use crate::ssh::execute_local_command_in_dir;
use crate::utils::command::CapturedOutput;
use crate::utils::shell;

// === Build Command Resolution ===

#[derive(Debug, Clone)]
pub enum ResolvedBuildCommand {
    ComponentDefined(String),
    ModuleProvided {
        command: String,
        source: String,
    },
    LocalScript {
        command: String,
        script_name: String,
    },
}

impl ResolvedBuildCommand {
    pub fn command(&self) -> &str {
        match self {
            ResolvedBuildCommand::ComponentDefined(cmd) => cmd,
            ResolvedBuildCommand::ModuleProvided { command, .. } => command,
            ResolvedBuildCommand::LocalScript { command, .. } => command,
        }
    }
}

/// Resolve build command for a component using the following priority:
/// 1. Explicit component.build_command (always wins)
/// 2. Module's bundled script (module.build.module_script)
/// 3. Local script matching module's script_names pattern
pub(crate) fn resolve_build_command(component: &Component) -> Result<ResolvedBuildCommand> {
    // 1. Explicit component override takes precedence
    if let Some(cmd) = &component.build_command {
        return Ok(ResolvedBuildCommand::ComponentDefined(cmd.clone()));
    }

    // 2. Check module for bundled script or local script patterns
    if let Some(modules) = &component.modules {
        for module_id in modules.keys() {
            if let Ok(module) = module::load_module(module_id) {
                if let Some(build) = &module.build {
                    // Check for module's bundled script
                    let bundled = build.module_script.as_ref().and_then(|module_script| {
                        paths::module(module_id).ok().and_then(|module_dir| {
                            let script_path = module_dir.join(module_script);
                            script_path.exists().then(|| {
                                let quoted_path = shell::quote_path(&script_path.to_string_lossy());
                                let command = build
                                    .command_template
                                    .as_ref()
                                    .map(|t| t.replace("{{script}}", &quoted_path))
                                    .unwrap_or_else(|| format!("sh {}", quoted_path));
                                ResolvedBuildCommand::ModuleProvided {
                                    command,
                                    source: format!("{}:{}", module_id, module_script),
                                }
                            })
                        })
                    });
                    if let Some(result) = bundled {
                        return Ok(result);
                    }

                    // Check for local script matching module's script_names
                    let local_path = PathBuf::from(&component.local_path);
                    for script_name in &build.script_names {
                        let local_script = local_path.join(script_name);
                        if local_script.exists() {
                            let command = build
                                .command_template
                                .as_ref()
                                .map(|t| t.replace("{{script}}", script_name))
                                .unwrap_or_else(|| format!("sh {}", script_name));
                            return Ok(ResolvedBuildCommand::LocalScript {
                                command,
                                script_name: script_name.clone(),
                            });
                        }
                    }
                }
            }
        }
    }

    // Check if any module provides build (makes build_command optional)
    if module::module_provides_build(component) {
        // Module provides build config but no matching scripts found
        Err(Error::validation_invalid_argument(
            "buildCommand",
            format!(
                "Component '{}' links a module with build support, but no build script was found.\n\
                 Expected: module's bundled script OR local script matching module pattern.\n\
                 Check module installation or add a local build.sh to the component directory.",
                component.id
            ),
            Some(component.id.clone()),
            None,
        ))
    } else {
        // No modules with build support - explicit buildCommand required
        Err(Error::validation_invalid_argument(
            "buildCommand",
            format!("Component '{}' has no build configuration", component.id),
            Some(component.id.clone()),
            Some(vec![
                format!("Configure buildCommand: homeboy component set {} --json '{{\"buildCommand\": \"<command>\"}}'", component.id),
                format!("Link a module with build support: homeboy component set {} --json '{{\"modules\": {{\"wordpress\": {{}}}}}}'", component.id),
            ]),
        ))
    }
}

// === Public API ===

#[derive(Debug, Clone, Serialize)]
pub struct BuildOutput {
    pub command: String,
    pub component_id: String,
    pub build_command: String,
    #[serde(flatten)]
    pub output: CapturedOutput,
    pub success: bool,
}

#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum BuildResult {
    Single(BuildOutput),
    Bulk(BulkResult<BuildOutput>),
}

/// Run build for one or more components.
///
/// Accepts either:
/// - A single component ID: "extrachill-api"
/// - A JSON spec: {"componentIds": ["api", "users"]}
pub fn run(input: &str) -> Result<(BuildResult, i32)> {
    if is_json_input(input) {
        run_bulk(input)
    } else {
        run_single(input)
    }
}

/// Build a component for deploy context.
/// Returns (exit_code, error_message) - None error means success.
///
/// Shell execution is required for build commands by design:
/// - Build commands execute shell scripts (bash, sh, npm, composer, etc.)
/// - Scripts use shell features (pipes, redirects, environment variables)
/// - Examples: "bash {{script}}", "sh build.sh", "npm run build"
/// - Build processes often require chaining with &&, ||, ;
/// - Direct execution cannot handle shell scripts or shell features
///
/// See executor.rs for detailed execution strategy decision tree
pub(crate) fn build_component(component: &component::Component) -> (Option<i32>, Option<String>) {
    // Validate local_path before attempting build
    let validated_path = match component::validate_local_path(component) {
        Ok(p) => p,
        Err(e) => return (Some(1), Some(format_path_validation_error(component, &e))),
    };

    let resolved = match resolve_build_command(component) {
        Ok(r) => r,
        Err(e) => return (Some(1), Some(e.to_string())),
    };

    let build_cmd = resolved.command().to_string();

    // Fix local permissions before build to ensure zip has correct permissions
    let local_path_str = validated_path.to_string_lossy().to_string();
    permissions::fix_local_permissions(&local_path_str);

    // Get module path env vars for build command (matches pre-build script behavior)
    let env_vars = get_build_env_vars(component);
    let env_refs: Vec<(&str, &str)> = env_vars
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();

    let output = execute_local_command_in_dir(
        &build_cmd,
        Some(&local_path_str),
        if env_refs.is_empty() {
            None
        } else {
            Some(&env_refs)
        },
    );

    if output.success {
        (Some(output.exit_code), None)
    } else {
        (
            Some(output.exit_code),
            Some(format_build_error(
                &component.id,
                &build_cmd,
                &local_path_str,
                output.exit_code,
                &output.stderr,
                &output.stdout,
            )),
        )
    }
}

/// Format a path validation error with build context.
fn format_path_validation_error(component: &component::Component, error: &Error) -> String {
    format!(
        "Build failed for component '{}':\n  {}\n\nHint: Update local_path with:\n  homeboy component set {} --local-path \"/path/to/component\"",
        component.id,
        error.message,
        component.id
    )
}

/// Format a build error message with context from stderr/stdout.
/// Only includes universal POSIX exit code hints - Homeboy is technology-agnostic.
fn format_build_error(
    component_id: &str,
    build_cmd: &str,
    working_dir: &str,
    exit_code: i32,
    stderr: &str,
    stdout: &str,
) -> String {
    // Get useful output (prefer stderr, fall back to stdout)
    let output_text = if stderr.trim().is_empty() {
        stdout
    } else {
        stderr
    };

    // Get last 15 lines for context
    let tail: Vec<&str> = output_text.lines().rev().take(15).collect();
    let output_tail: String = tail.into_iter().rev().collect::<Vec<_>>().join("\n");

    // Translate universal POSIX exit codes only (no tool-specific hints)
    let hint = match exit_code {
        127 => "\nHint: Command not found. Check that the build command and its dependencies are installed and in PATH.",
        126 => "\nHint: Permission denied. Check file permissions on the build script.",
        _ => "",
    };

    let mut msg = format!(
        "Build failed for '{}' (exit code {}).\n  Command: {}\n  Working directory: {}",
        component_id, exit_code, build_cmd, working_dir
    );

    if !output_tail.is_empty() {
        msg.push_str("\n\n--- Build output (last 15 lines) ---\n");
        msg.push_str(&output_tail);
        msg.push_str("\n--- End of output ---");
    }

    if !hint.is_empty() {
        msg.push_str(hint);
    }

    msg
}

// === Internal implementation ===

fn run_single(component_id: &str) -> Result<(BuildResult, i32)> {
    let (output, exit_code) = execute_build(component_id, None)?;
    Ok((BuildResult::Single(output), exit_code))
}

/// Build a single component with an overridden local_path.
///
/// Use this for workspace clones, temporary checkouts, or CI builds
/// where the source lives somewhere other than the configured `local_path`.
pub fn run_with_path(component_id: &str, path: &str) -> Result<(BuildResult, i32)> {
    let (output, exit_code) = execute_build(component_id, Some(path))?;
    Ok((BuildResult::Single(output), exit_code))
}

fn run_bulk(json_spec: &str) -> Result<(BuildResult, i32)> {
    let input = parse_bulk_ids(json_spec)?;

    let mut results = Vec::with_capacity(input.component_ids.len());
    let mut succeeded = 0usize;
    let mut failed = 0usize;

    for id in &input.component_ids {
        match execute_build(id, None) {
            Ok((output, _)) => {
                if output.success {
                    succeeded += 1;
                } else {
                    failed += 1;
                }
                results.push(ItemOutcome {
                    id: id.clone(),
                    result: Some(output),
                    error: None,
                });
            }
            Err(e) => {
                failed += 1;
                results.push(ItemOutcome {
                    id: id.clone(),
                    result: None,
                    error: Some(e.to_string()),
                });
            }
        }
    }

    let exit_code = if failed > 0 { 1 } else { 0 };

    Ok((
        BuildResult::Bulk(BulkResult {
            action: "build".to_string(),
            results,
            summary: BulkSummary {
                total: succeeded + failed,
                succeeded,
                failed,
            },
        }),
        exit_code,
    ))
}

/// Build a pre-resolved component (supports both registered and discovered components).
pub fn run_component(component: &Component) -> Result<(BuildResult, i32)> {
    let (output, exit_code) = execute_build_component(component)?;
    Ok((BuildResult::Single(output), exit_code))
}

fn execute_build(component_id: &str, path_override: Option<&str>) -> Result<(BuildOutput, i32)> {
    let mut comp = component::load(component_id)?;
    if let Some(path) = path_override {
        comp.local_path = path.to_string();
    }
    execute_build_component(&comp)
}

fn execute_build_component(comp: &Component) -> Result<(BuildOutput, i32)> {
    // Validate required modules are installed before resolving build commands.
    // Without this, missing modules cause vague "no build command" errors.
    module::validate_required_modules(comp)?;

    // Validate local_path before attempting build
    let validated_path = component::validate_local_path(comp)?;
    let local_path_str = validated_path.to_string_lossy().to_string();

    let resolved = resolve_build_command(comp)?;
    let build_cmd = resolved.command().to_string();

    // Run pre-build script if module provides one
    if let Some((exit_code, stderr)) = run_pre_build_scripts(comp)? {
        if exit_code != 0 {
            return Ok((
                BuildOutput {
                    command: "build.run".to_string(),
                    component_id: comp.id.clone(),
                    build_command: build_cmd,
                    output: CapturedOutput::new(String::new(), stderr),
                    success: false,
                },
                exit_code,
            ));
        }
    }

    // Fix local permissions before build to ensure zip has correct permissions
    permissions::fix_local_permissions(&local_path_str);

    // Get module path env vars for build command (matches pre-build script behavior)
    let env_vars = get_build_env_vars(comp);
    let env_refs: Vec<(&str, &str)> = env_vars
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();

    let cmd_output = execute_local_command_in_dir(
        &build_cmd,
        Some(&local_path_str),
        if env_refs.is_empty() {
            None
        } else {
            Some(&env_refs)
        },
    );

    Ok((
        BuildOutput {
            command: "build.run".to_string(),
            component_id: comp.id.clone(),
            build_command: build_cmd,
            output: CapturedOutput::new(cmd_output.stdout, cmd_output.stderr),
            success: cmd_output.success,
        },
        cmd_output.exit_code,
    ))
}

/// Run pre-build scripts from all configured modules.
/// Returns Some((exit_code, stderr)) if any script fails, None if all pass or no scripts.
fn run_pre_build_scripts(comp: &Component) -> Result<Option<(i32, String)>> {
    let modules = match &comp.modules {
        Some(m) => m,
        None => return Ok(None),
    };

    for module_id in modules.keys() {
        let module = match module::load_module(module_id) {
            Ok(m) => m,
            Err(_) => continue,
        };

        let build_config = match &module.build {
            Some(b) => b,
            None => continue,
        };

        let pre_build_script = match &build_config.pre_build_script {
            Some(s) => s,
            None => continue,
        };

        let module_path = paths::module(module_id)?;
        let script_path = module_path.join(pre_build_script);

        if !script_path.exists() {
            continue;
        }

        let env: [(&str, &str); 4] = [
            ("HOMEBOY_MODULE_PATH", &module_path.to_string_lossy()),
            (exec_context::COMPONENT_ID, &comp.id),
            (exec_context::COMPONENT_PATH, &comp.local_path),
            ("HOMEBOY_PLUGIN_PATH", &comp.local_path),
        ];

        let output = execute_local_command_in_dir(&script_path.to_string_lossy(), None, Some(&env));

        if !output.success {
            let combined = if output.stderr.is_empty() {
                output.stdout
            } else {
                output.stderr
            };
            return Ok(Some((output.exit_code, combined)));
        }
    }

    Ok(None)
}

/// Get environment variables for build commands (module path, component path).
/// Matches the env vars passed to pre-build scripts for consistency.
fn get_build_env_vars(comp: &Component) -> Vec<(String, String)> {
    let mut env = Vec::new();

    // Always pass the component ID so build scripts can name artifacts consistently
    env.push((
        exec_context::COMPONENT_ID.to_string(),
        comp.id.clone(),
    ));

    if let Some(modules) = &comp.modules {
        for module_id in modules.keys() {
            if let Ok(module) = module::load_module(module_id) {
                if module.build.is_some() {
                    if let Ok(module_path) = paths::module(module_id) {
                        let module_path_str = module_path.to_string_lossy().to_string();
                        env.push(("HOMEBOY_MODULE_PATH".to_string(), module_path_str));
                        env.push((
                            exec_context::COMPONENT_PATH.to_string(),
                            comp.local_path.clone(),
                        ));
                        env.push(("HOMEBOY_PLUGIN_PATH".to_string(), comp.local_path.clone()));
                        break; // Use first module with build config
                    }
                }
            }
        }
    }

    env
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_json_input_detects_json() {
        assert!(is_json_input(r#"{"componentIds": ["a"]}"#));
        assert!(is_json_input(r#"  {"componentIds": ["a"]}"#));
        assert!(!is_json_input("extrachill-api"));
        assert!(!is_json_input("some-component-id"));
    }
}