Maintain/Build/Definition.rs
1//=============================================================================//
2// File Path: Element/Maintain/Source/Build/Definition.rs
3//=============================================================================//
4// Module: Definition
5//
6// Brief Description: Build system type definitions and data structures.
7//
8// RESPONSIBILITIES:
9// ================
10//
11// Primary:
12// - Define argument parsing structures
13// - Define configuration file parsing structures
14// - Define file guard structures
15//
16// Secondary:
17// - Provide type-safe access to build configuration
18//
19// ARCHITECTURAL ROLE:
20// ===================
21//
22// Position:
23// - Infrastructure/Data structures layer
24// - Type definitions
25//
26// Dependencies (What this module requires):
27// - External crates: clap, serde, std (fs, log, path)
28// - Internal modules: Constant
29// - Traits implemented: Drop (for Guard), Parser (for Argument)
30//
31// Dependents (What depends on this module):
32// - Build orchestration functions
33// - Entry point functions
34//
35// IMPLEMENTATION DETAILS:
36// =======================
37//
38// Design Patterns:
39// - Builder pattern (via clap derive)
40// - Data transfer object pattern
41// - RAII pattern (Guard)
42//
43// Performance Considerations:
44// - Complexity: O(n) - depends on operation
45// - Memory usage patterns: Struct-based memory layout
46// - Hot path optimizations: None
47//
48// Thread Safety:
49// - Thread-safe: Yes (Argument derives Clone, Guard not thread-safe by design)
50// - Synchronization mechanisms used: None
51// - Interior mutability considerations: None
52//
53// Error Handling:
54// - Error types returned: BuildError
55// - Recovery strategies: Guard restores files on error
56//
57// EXAMPLES:
58// =========
59//
60// Example 1: Parsing arguments
61use std::{
62 fs,
63 path::{Path, PathBuf},
64};
65
66use clap::Parser;
67use serde::Deserialize;
68use log::{error, info};
69
70/// ```rust
71/// use clap::Parser;
72///
73/// use crate::Maintain::Source::Build::Definition;
74/// let argument = Argument::parse();
75/// println!("Building directory: {}", argument.Directory);
76/// ```
77// Example 2: Creating a guard
78/// ```rust
79/// use crate::Maintain::Source::Build::Definition;
80/// let guard = Guard::New(file_path, description.to_string())?;
81/// ```
82// Example 3: Parsing cargo.toml
83/// ```rust
84/// use crate::Maintain::Source::Build::Definition;
85/// let content = std::fs::read_to_string("Cargo.toml")?;
86/// let manifest:Manifest = toml::from_str(&content)?;
87/// ```
88//
89//=============================================================================//
90// IMPLEMENTATION
91//=============================================================================//
92use crate::Build::Constant::*;
93
94//=============================================================================
95// Argument Definition
96//=============================================================================
97
98/// Represents parsed command-line arguments and environment variables that
99/// control the build.
100///
101/// This struct is generated by `clap` from the `Parser` derive macro and
102/// automatically parses command-line arguments and environment variables into
103/// a strongly-typed configuration object. Each field can be configured via:
104/// - Command-line arguments (long form, e.g., `--directory`)
105/// - Environment variables (as specified in the `env` attribute)
106/// - Default values (as specified in the `default_value` attribute)
107///
108/// Required fields must be provided, while optional fields can be omitted.
109/// The `Command` field is special in that it accepts all remaining arguments
110/// as the build command to execute.
111///
112/// # Field Descriptions
113///
114/// - **Directory**: The main directory of the project containing Cargo.toml
115/// - **Name**: The original base name of the project/package
116/// - **Prefix**: The prefix for the application's bundle identifier
117/// - **Browser**: Flag for browser-specific build aspects
118/// - **Bundle**: Flag for bundling-specific aspects
119/// - **Compile**: Flag for compile-specific aspects
120/// - **Clean**: Flag for cleaning-specific aspects
121/// - **Debug**: Flag for debug-specific aspects
122/// - **Dependency**: Information about a dependency (org/repo or boolean)
123/// - **Environment**: The Node.js environment (development, production, etc.)
124/// - **NodeVersion**: The Node.js sidecar version to bundle
125/// - **Command**: The build command and its arguments to execute
126#[derive(Parser, Debug, Clone)]
127#[clap(
128 author,
129 version,
130 about = "Prepares, builds, and restores project configurations."
131)]
132pub struct Argument {
133 /// The main directory of the project.
134 ///
135 /// This field specifies the project directory that contains the
136 /// `Cargo.toml` and `tauri.conf.json` files. It can be set via:
137 /// - Command-line: `--directory <path>`
138 /// - Environment: `MOUNTAIN_DIR`
139 /// - Default: "Element/Mountain"
140 #[clap(long, env = DirEnv, default_value = DirectoryDefault)]
141 pub Directory:String,
142
143 /// The original base name of the project/package.
144 ///
145 /// This field specifies the base name of the project, which serves as
146 /// the suffix for generated product names. It can be set via:
147 /// - Command-line: `--name <name>`
148 /// - Environment: `MOUNTAIN_ORIGINAL_BASE_NAME`
149 /// - Default: "Mountain"
150 #[clap(long, env = NameEnv, default_value = NameDefault)]
151 pub Name:String,
152
153 /// The prefix for the application's bundle identifier.
154 ///
155 /// This field specifies the reverse domain prefix for the bundle
156 /// identifier. It can be set via:
157 /// - Command-line: `--prefix <prefix>`
158 /// - Environment: `MOUNTAIN_BUNDLE_ID_PREFIX`
159 /// - Default: "land.editor.binary"
160 #[clap(long, env = PrefixEnv, default_value = PrefixDefault)]
161 pub Prefix:String,
162
163 /// Flag or value indicating browser-specific build aspects.
164 ///
165 /// When set to "true", enables browser-specific configuration and affects
166 /// the generated product name and bundle identifier.
167 #[clap(long, env = BrowserEnv)]
168 pub Browser:Option<String>,
169
170 /// Flag or value indicating bundling-specific aspects.
171 ///
172 /// When set to "true", enables bundling-specific configuration and affects
173 /// the generated product name and bundle identifier.
174 #[clap(long, env = BundleEnv)]
175 pub Bundle:Option<String>,
176
177 /// Flag or value indicating compile-specific aspects.
178 ///
179 /// When set to "true", enables compile-specific configuration and affects
180 /// the generated product name and bundle identifier.
181 #[clap(long, env = CompileEnv)]
182 pub Compile:Option<String>,
183
184 /// Flag or value indicating cleaning-specific aspects.
185 ///
186 /// When set to "true", enables clean-specific configuration and affects
187 /// the generated product name and bundle identifier.
188 #[clap(long, env = CleanEnv)]
189 pub Clean:Option<String>,
190
191 /// Flag or value indicating debug-specific aspects.
192 ///
193 /// When set to "true", enables debug-specific configuration and affects
194 /// the generated product name and bundle identifier. Also automatically
195 /// detected if the command contains "--debug".
196 #[clap(long, env = DebugEnv)]
197 pub Debug:Option<String>,
198
199 /// Flag indicating the Mountain workbench profile (debug-mountain).
200 /// Mutually exclusive with `Electron` and `Browser`; when set to "true"
201 /// the product name gains a `_Mountain` suffix so Cargo/Tauri emit a
202 /// distinct binary path that doesn't collide with other workbenches.
203 #[clap(long, env = MountainEnv)]
204 pub Mountain:Option<String>,
205
206 /// Flag indicating the Electron workbench profile (debug-electron).
207 /// Mutually exclusive with `Mountain` and `Browser`; adds a
208 /// `_Electron` suffix to the generated product name + identifier.
209 #[clap(long, env = ElectronEnv)]
210 pub Electron:Option<String>,
211
212 /// Compiler variant (e.g. "Rest"). When present the product name gains
213 /// a `_Compiler<Name>` suffix so variant compilers don't collide with
214 /// the default TypeScript/tsc path.
215 #[clap(long, env = CompilerEnv)]
216 pub Compiler:Option<String>,
217
218 /// Comma-joined Cargo features derived from `.env.Land` tier selections
219 /// by `Maintain/Script/TierEnvironment.sh`. When present and the build
220 /// command is `pnpm tauri build [--debug]`, these are forwarded to Cargo
221 /// via `-- --features "<list>"` so tier-gated Rust code compiles in.
222 /// Example: "TierRemoteProcedureCallSharedMemory,TierLoggerRing".
223 #[clap(long, env = CargoFeaturesEnv)]
224 pub CargoFeatures:Option<String>,
225
226 /// JSON blob of `__LandTier_<Capability>__` replacement tokens produced
227 /// by `Maintain/Script/TierEnvironment.sh`. Cocoon's `TargetConfig.ts`
228 /// merges it into its esbuild `define` options so tier constants compile
229 /// into the Cocoon bundle. Maintain only needs to pass it through to
230 /// the child process environment - no parsing happens here.
231 #[clap(long, env = CocoonEsbuildDefineEnv)]
232 pub CocoonEsbuildDefine:Option<String>,
233
234 /// Information about a dependency, often 'org/repo' or a boolean string.
235 ///
236 /// This field specifies dependency information that affects the generated
237 /// product name and bundle identifier. Can be:
238 /// - "true" for generic dependencies
239 /// - "org/repo" for specific repository dependencies
240 /// - Any custom string
241 #[clap(long, env = DependencyEnv)]
242 pub Dependency:Option<String>,
243
244 /// The Node.js environment (e.g., "development", "production").
245 ///
246 /// This field specifies the Node.js runtime environment and affects the
247 /// generated product name and bundle identifier.
248 #[clap(long, env = NodeEnv)]
249 pub Environment:Option<String>,
250
251 /// Specifies the Node.js sidecar version to bundle (e.g., "22").
252 ///
253 /// This field specifies which Node.js version should be bundled as a
254 /// sidecar binary with the application. The executable is sourced from
255 /// the Element/SideCar directory.
256 #[clap(long, env = NodeVersionEnv)]
257 pub NodeVersion:Option<String>,
258
259 /// The build command and its arguments to execute.
260 ///
261 /// This field accepts all remaining command-line arguments as the build
262 /// command to execute after configuring the project files. This field
263 /// is required and must be provided as the final arguments.
264 ///
265 /// Example: `pnpm tauri build`
266 #[clap(required = true, last = true)]
267 pub Command:Vec<String>,
268}
269
270//=============================================================================
271// Guard Definition (RAII Pattern)
272//=============================================================================
273
274/// Manages the backup and restoration of a single file using the RAII pattern.
275///
276/// This struct ensures that an original file is restored to its initial state
277/// when the Guard goes out of scope, providing a safe way to temporarily
278/// modify configuration files during the build process.
279///
280/// # RAII Pattern
281///
282/// The Guard implements the RAII (Resource Acquisition Is Initialization)
283/// pattern:
284/// - **Acquisition**: When created, it creates a backup of the original file
285/// - **Release**: When dropped, it restores the original file from the backup
286///
287/// This ensures that files are restored even if:
288/// - The function returns early
289/// - An error occurs and propagates up
290/// - A panic occurs
291///
292/// # Backup Naming
293///
294/// Backup files are created by appending a suffix to the original file
295/// extension:
296/// - `Cargo.toml` → `Cargo.toml.Backup`
297/// - `tauri.conf.json` → `tauri.conf.json.Backup`
298///
299/// # Error Handling
300///
301/// The Guard constructor returns an error if:
302/// - A backup file already exists at the target location
303/// - The original file cannot be copied (IO error)
304///
305/// This prevents accidental overwriting of existing backups and ensures
306/// data safety.
307///
308/// # Fields
309///
310/// - **Path**: The path to the original file
311/// - **Store**: The path to the backup file
312/// - **Active**: Whether a backup was created
313/// - **Note**: A descriptive note for logging purposes
314pub struct Guard {
315 /// The path to the original file that will be modified.
316 Path:PathBuf,
317
318 /// The path to the backup file created by this guard.
319 Store:PathBuf,
320
321 /// Whether a backup was actually created
322 /// (false if the original file didn't exist).
323 Active:bool,
324
325 /// Whether the guard is armed to restore on drop.
326 /// When true, the file will be restored on drop.
327 /// When false (disarmed), the modified file is preserved.
328 Armed:bool,
329
330 /// A descriptive note for logging and debugging purposes.
331 #[allow(dead_code)]
332 Note:String,
333}
334
335impl Guard {
336 /// Creates a new Guard that backs up the specified file.
337 ///
338 /// This constructor creates a backup of the original file if it exists,
339 /// and prepares to restore it when the Guard is dropped. The backup
340 /// file is created with a special suffix to prevent accidental conflicts.
341 ///
342 /// # Parameters
343 ///
344 /// * `OriginalPath` - The path to the file to be backed up
345 /// * `Description` - A descriptive note for logging purposes
346 ///
347 /// # Returns
348 ///
349 /// Returns a `Result` containing the Guard or a `BuildError` if:
350 /// - A backup file already exists
351 /// - The file cannot be copied
352 ///
353 /// # Errors
354 ///
355 /// * `BuildError::Exists` - If a backup file already exists
356 /// * `BuildError::Io` - If the file copy operation fails
357 ///
358 /// # Example
359 ///
360 /// ```no_run
361 /// use crate::Maintain::Source::Build::Definition;
362 /// let path = PathBuf::from("Cargo.toml");
363 /// let guard = Guard::New(path, "Cargo manifest".to_string())?;
364 /// ```
365 ///
366 /// # Safety
367 ///
368 /// The Guard ensures that the original file is restored even in the
369 /// presence of panics, providing exception safety.
370 pub fn New(OriginalPath:PathBuf, Description:String) -> Result<Self, crate::Build::Error::Error> {
371 let BackupPath = OriginalPath.with_extension(format!(
372 "{}{}",
373 OriginalPath.extension().unwrap_or_default().to_str().unwrap_or(""),
374 BackupSuffix
375 ));
376
377 if BackupPath.exists() {
378 error!("Backup file {} already exists.", BackupPath.display());
379
380 return Err(crate::Build::Error::Error::Exists(BackupPath));
381 }
382
383 let mut BackupMade = false;
384
385 if OriginalPath.exists() {
386 fs::copy(&OriginalPath, &BackupPath)?;
387
388 info!(
389 target: "Build::Guard",
390 "Backed {} to {}",
391 OriginalPath.display(),
392 BackupPath.display()
393 );
394
395 BackupMade = true;
396 }
397
398 Ok(Self {
399 Path:OriginalPath,
400 Store:BackupPath,
401 Active:BackupMade,
402 Armed:true,
403 Note:Description,
404 })
405 }
406
407 /// Returns a reference to the original file path.
408 ///
409 /// This method provides read access to the path of the original file
410 /// that this guard is protecting.
411 ///
412 /// # Returns
413 ///
414 /// A reference to the original file's `Path`.
415 ///
416 /// # Example
417 ///
418 /// ```no_run
419 /// use crate::Maintain::Source::Build::Definition;
420 /// let guard = Guard::New(original_path, description.to_string())?;
421 /// println!("Original file: {}", guard.Path().display());
422 /// ```
423 pub fn Path(&self) -> &Path { &self.Path }
424
425 /// Returns a reference to the backup file path.
426 ///
427 /// This method provides read access to the path where the backup
428 /// file is stored.
429 ///
430 /// # Returns
431 ///
432 /// A reference to the backup file's `Path`.
433 ///
434 /// # Example
435 ///
436 /// ```no_run
437 /// use crate::Maintain::Source::Build::Definition;
438 /// let guard = Guard::New(original_path, description.to_string())?;
439 /// println!("Backup file: {}", guard.Store().display());
440 /// ```
441 pub fn Store(&self) -> &Path { &self.Store }
442
443 /// Disarms the guard, preventing restoration of the original file,
444 /// and deletes the backup so the next build is not blocked.
445 pub fn disarm(&mut self) {
446 self.Armed = false;
447 if self.Active && self.Store.exists() {
448 let _ = fs::remove_file(&self.Store);
449 }
450 }
451}
452
453/// Drop implementation that automatically restores the original file.
454///
455/// This is the core of the RAII pattern: when the Guard goes out of scope,
456/// it automatically restores the original file from the backup. This ensures
457/// that file modifications are temporary and that the system is left in a
458/// consistent state even if an error occurs.
459///
460/// # Behavior
461///
462/// - If no backup was created (original file didn't exist), does nothing
463/// - If backup exists, copies it back to the original location
464/// - After successful restore, deletes the backup file
465/// - Logs success or failure of the restoration process
466///
467/// # Panics
468///
469/// This implementation handles panics internally and does not propagate them.
470/// If the restoration fails, it logs an error but does not panic, ensuring
471/// that cleanup failures don't cause secondary failures.
472impl Drop for Guard {
473 fn drop(&mut self) {
474 // Only restore if armed (build failed) and backup is active
475 if self.Armed && self.Active && self.Store.exists() {
476 info!(
477 target: "Build::Guard",
478 "Restoring {} from {}...",
479 self.Path.display(),
480 self.Store.display()
481 );
482
483 if let Ok(_) = fs::copy(&self.Store, &self.Path) {
484 info!(target: "Build::Guard", "Restore successful.");
485
486 if let Err(e) = fs::remove_file(&self.Store) {
487 error!(
488 target: "Build::Guard",
489 "Failed to delete backup {}: {}",
490 self.Store.display(),
491 e
492 );
493 }
494 } else if let Err(e) = fs::copy(&self.Store, &self.Path) {
495 error!(
496 target: "Build::Guard",
497 "Restore FAILED: {}. {} is now inconsistent.",
498 e,
499 self.Path.display()
500 );
501 }
502 }
503 }
504}
505
506//=============================================================================
507// Manifest Definition
508//=============================================================================
509
510/// Represents the `package` section of a `Cargo.toml` manifest.
511///
512/// This struct contains metadata extracted from the `[package]` section
513/// of a Cargo.toml file. It is used for deserializing TOML content using
514/// the `toml` crate's deserialization functionality.
515///
516/// Currently, this struct only extracts the version information, which
517/// is needed for updating the Tauri configuration file with the correct
518/// version during the build process.
519///
520/// # TOML Format
521///
522/// The expected TOML structure:
523/// ```toml
524/// [package]
525/// name = "Mountain"
526/// version = "1.0.0"
527/// # ... other fields
528/// ```
529///
530/// # Fields
531///
532/// - **package**: Contains the metadata extracted from the package section
533#[derive(Deserialize, Debug)]
534pub struct Manifest {
535 /// Represents metadata within the `package` section of `Cargo.toml`.
536 ///
537 /// This nested struct contains the individual metadata fields from the
538 /// package section, including the version string.
539 package:Meta,
540}
541
542impl Manifest {
543 /// Retrieves the version string from the manifest.
544 ///
545 /// This method provides access to the version field, which is stored
546 /// privately. The version is used when updating the Tauri configuration
547 /// file during the build process.
548 ///
549 /// # Returns
550 ///
551 /// A string slice containing the version number.
552 ///
553 /// # Example
554 ///
555 /// ```no_run
556 /// use crate::Maintain::Source::Build::Definition;
557 /// let version = manifest.get_version();
558 /// println!("Application version: {}", version);
559 /// ```
560 pub fn get_version(&self) -> &str { &self.package.version }
561}
562
563/// Represents metadata within the `package` section of `Cargo.toml`.
564///
565/// This struct contains individual metadata fields from the package section.
566/// Currently, only the version field is stored, but additional fields can
567/// be added as needed (e.g., name, description, authors, etc.).
568///
569/// All fields are private and should be accessed through methods on the
570/// parent `Manifest` struct.
571#[derive(Deserialize, Debug)]
572struct Meta {
573 /// The version string from the package metadata.
574 ///
575 /// This field contains the version identifier as specified in the
576 /// Cargo.toml file (e.g., "1.0.0", "2.3.4-beta.1").
577 version:String,
578}