|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use std::fs; |
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +use crate::config::CONFIG_FILE; |
| 6 | + |
| 7 | +/// Directory for rnr binaries |
| 8 | +const RNR_DIR: &str = ".rnr"; |
| 9 | +const BIN_DIR: &str = "bin"; |
| 10 | + |
| 11 | +/// Run the init command |
| 12 | +pub fn run() -> Result<()> { |
| 13 | + let current_dir = std::env::current_dir().context("Failed to get current directory")?; |
| 14 | + |
| 15 | + // Check if already initialized |
| 16 | + let rnr_dir = current_dir.join(RNR_DIR); |
| 17 | + if rnr_dir.exists() { |
| 18 | + println!("rnr is already initialized in this directory"); |
| 19 | + return Ok(()); |
| 20 | + } |
| 21 | + |
| 22 | + println!("Initializing rnr..."); |
| 23 | + |
| 24 | + // Create .rnr/bin directory |
| 25 | + let bin_dir = rnr_dir.join(BIN_DIR); |
| 26 | + fs::create_dir_all(&bin_dir).context("Failed to create .rnr/bin directory")?; |
| 27 | + println!(" Created {}/{}", RNR_DIR, BIN_DIR); |
| 28 | + |
| 29 | + // Download binaries for all platforms |
| 30 | + #[cfg(feature = "network")] |
| 31 | + { |
| 32 | + download_binaries(&bin_dir)?; |
| 33 | + } |
| 34 | + |
| 35 | + #[cfg(not(feature = "network"))] |
| 36 | + { |
| 37 | + println!(" Skipping binary download (network feature disabled)"); |
| 38 | + println!(" Please manually copy binaries to {}/{}", RNR_DIR, BIN_DIR); |
| 39 | + } |
| 40 | + |
| 41 | + // Create wrapper scripts |
| 42 | + create_wrapper_scripts(¤t_dir)?; |
| 43 | + |
| 44 | + // Create starter rnr.yaml if it doesn't exist |
| 45 | + let config_path = current_dir.join(CONFIG_FILE); |
| 46 | + if !config_path.exists() { |
| 47 | + create_starter_config(&config_path)?; |
| 48 | + } else { |
| 49 | + println!(" {} already exists, skipping", CONFIG_FILE); |
| 50 | + } |
| 51 | + |
| 52 | + println!("\nrnr initialized successfully!"); |
| 53 | + println!("\nNext steps:"); |
| 54 | + println!(" 1. Edit {} to define your tasks", CONFIG_FILE); |
| 55 | + println!(" 2. Run ./rnr --list to see available tasks"); |
| 56 | + println!(" 3. Run ./rnr <task> to execute a task"); |
| 57 | + println!(" 4. Commit the .rnr directory and wrapper scripts to your repo"); |
| 58 | + |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | + |
| 62 | +/// Download binaries for all platforms |
| 63 | +#[cfg(feature = "network")] |
| 64 | +fn download_binaries(bin_dir: &Path) -> Result<()> { |
| 65 | + // TODO: Implement actual binary downloads from rnr.dev |
| 66 | + // For now, just create placeholder files |
| 67 | + println!(" Downloading binaries..."); |
| 68 | + println!(" TODO: Download from https://rnr.dev/bin/latest/"); |
| 69 | + |
| 70 | + // Placeholder - in real implementation, download from server |
| 71 | + let platforms = ["rnr-linux", "rnr-macos", "rnr.exe"]; |
| 72 | + for platform in platforms { |
| 73 | + let path = bin_dir.join(platform); |
| 74 | + fs::write(&path, "# placeholder binary\n") |
| 75 | + .with_context(|| format!("Failed to create {}", path.display()))?; |
| 76 | + println!(" Created {}", platform); |
| 77 | + } |
| 78 | + |
| 79 | + Ok(()) |
| 80 | +} |
| 81 | + |
| 82 | +/// Create the wrapper scripts at the project root |
| 83 | +fn create_wrapper_scripts(project_root: &Path) -> Result<()> { |
| 84 | + // Unix wrapper script |
| 85 | + let unix_script = r#"#!/bin/sh |
| 86 | +exec "$(dirname "$0")/.rnr/bin/rnr-$(uname -s | tr A-Z a-z)" "$@" |
| 87 | +"#; |
| 88 | + |
| 89 | + let unix_path = project_root.join("rnr"); |
| 90 | + fs::write(&unix_path, unix_script).context("Failed to create rnr wrapper script")?; |
| 91 | + |
| 92 | + // Make executable on Unix |
| 93 | + #[cfg(unix)] |
| 94 | + { |
| 95 | + use std::os::unix::fs::PermissionsExt; |
| 96 | + let mut perms = fs::metadata(&unix_path)?.permissions(); |
| 97 | + perms.set_mode(0o755); |
| 98 | + fs::set_permissions(&unix_path, perms)?; |
| 99 | + } |
| 100 | + |
| 101 | + println!(" Created rnr (Unix wrapper)"); |
| 102 | + |
| 103 | + // Windows wrapper script |
| 104 | + let windows_script = r#"@echo off |
| 105 | +"%~dp0.rnr\bin\rnr.exe" %* |
| 106 | +"#; |
| 107 | + |
| 108 | + let windows_path = project_root.join("rnr.cmd"); |
| 109 | + fs::write(&windows_path, windows_script).context("Failed to create rnr.cmd wrapper script")?; |
| 110 | + println!(" Created rnr.cmd (Windows wrapper)"); |
| 111 | + |
| 112 | + Ok(()) |
| 113 | +} |
| 114 | + |
| 115 | +/// Create a starter rnr.yaml configuration |
| 116 | +fn create_starter_config(path: &Path) -> Result<()> { |
| 117 | + let starter = r#"# rnr task definitions |
| 118 | +# See https://github.com/CodingWithCalvin/rnr.cli for documentation |
| 119 | +
|
| 120 | +# Simple command (shorthand) |
| 121 | +hello: echo "Hello from rnr!" |
| 122 | +
|
| 123 | +# Full task definition |
| 124 | +build: |
| 125 | + description: Build the project |
| 126 | + cmd: echo "Add your build command here" |
| 127 | +
|
| 128 | +# Task with steps |
| 129 | +ci: |
| 130 | + description: Run CI pipeline |
| 131 | + steps: |
| 132 | + - cmd: echo "Step 1: Lint" |
| 133 | + - cmd: echo "Step 2: Test" |
| 134 | + - cmd: echo "Step 3: Build" |
| 135 | +"#; |
| 136 | + |
| 137 | + fs::write(path, starter).context("Failed to create rnr.yaml")?; |
| 138 | + println!(" Created {}", CONFIG_FILE); |
| 139 | + |
| 140 | + Ok(()) |
| 141 | +} |
0 commit comments