jbuild 0.1.8

High-performance Java build tool supporting Maven and Gradle
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
//! Maven Reactor Build
//!
//! Implements Maven's reactor build functionality for multi-module projects.
//! Based on Maven's ReactorBuildStatus, ProjectBuildList, and related classes.

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use anyhow::Result;

/// Build status for a project in the reactor
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectBuildStatus {
    /// Not yet started
    Pending,
    /// Currently building
    Building,
    /// Build succeeded
    Success,
    /// Build failed
    Failed,
    /// Skipped due to dependency failure
    Skipped,
}

/// A project in the reactor build
#[derive(Debug, Clone)]
pub struct ReactorProject {
    /// Project identifier (groupId:artifactId)
    pub id: String,
    /// Group ID
    pub group_id: String,
    /// Artifact ID
    pub artifact_id: String,
    /// Version
    pub version: String,
    /// Project directory
    pub base_dir: PathBuf,
    /// Dependencies on other reactor projects
    pub reactor_dependencies: Vec<String>,
    /// Build status
    pub status: ProjectBuildStatus,
    /// Build duration in milliseconds
    pub build_time_ms: Option<u64>,
    /// Error message if failed
    pub error: Option<String>,
}

impl ReactorProject {
    pub fn new(
        group_id: impl Into<String>,
        artifact_id: impl Into<String>,
        version: impl Into<String>,
        base_dir: PathBuf,
    ) -> Self {
        let group_id = group_id.into();
        let artifact_id = artifact_id.into();
        Self {
            id: format!("{group_id}:{artifact_id}"),
            group_id,
            artifact_id: artifact_id.clone(),
            version: version.into(),
            base_dir,
            reactor_dependencies: Vec::new(),
            status: ProjectBuildStatus::Pending,
            build_time_ms: None,
            error: None,
        }
    }

    pub fn add_dependency(&mut self, project_id: impl Into<String>) {
        self.reactor_dependencies.push(project_id.into());
    }

    pub fn mark_building(&mut self) {
        self.status = ProjectBuildStatus::Building;
    }

    pub fn mark_success(&mut self, build_time_ms: u64) {
        self.status = ProjectBuildStatus::Success;
        self.build_time_ms = Some(build_time_ms);
    }

    pub fn mark_failed(&mut self, error: impl Into<String>, build_time_ms: u64) {
        self.status = ProjectBuildStatus::Failed;
        self.error = Some(error.into());
        self.build_time_ms = Some(build_time_ms);
    }

    pub fn mark_skipped(&mut self) {
        self.status = ProjectBuildStatus::Skipped;
    }
}

/// Reactor build status tracking
#[derive(Debug, Default)]
pub struct ReactorBuildStatus {
    /// All projects in the reactor
    projects: Vec<ReactorProject>,
    /// Project ID to index mapping
    project_index: HashMap<String, usize>,
    /// Whether to fail fast on first error
    fail_fast: bool,
    /// Whether the build has been halted
    halted: bool,
}

impl ReactorBuildStatus {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_fail_fast(mut self, fail_fast: bool) -> Self {
        self.fail_fast = fail_fast;
        self
    }

    /// Add a project to the reactor
    pub fn add_project(&mut self, project: ReactorProject) {
        let id = project.id.clone();
        let index = self.projects.len();
        self.projects.push(project);
        self.project_index.insert(id, index);
    }

    /// Get a project by ID
    pub fn get_project(&self, id: &str) -> Option<&ReactorProject> {
        self.project_index.get(id).map(|&i| &self.projects[i])
    }

    /// Get a mutable project by ID
    pub fn get_project_mut(&mut self, id: &str) -> Option<&mut ReactorProject> {
        self.project_index.get(id).map(|&i| &mut self.projects[i])
    }

    /// Get all projects
    pub fn projects(&self) -> &[ReactorProject] {
        &self.projects
    }

    /// Get projects in build order (topological sort)
    pub fn build_order(&self) -> Result<Vec<&ReactorProject>> {
        let mut result = Vec::new();
        let mut visited = HashSet::new();
        let mut temp_visited = HashSet::new();

        for project in &self.projects {
            if !visited.contains(&project.id) {
                self.visit(&project.id, &mut visited, &mut temp_visited, &mut result)?;
            }
        }

        Ok(result)
    }

    fn visit<'a>(
        &'a self,
        id: &str,
        visited: &mut HashSet<String>,
        temp_visited: &mut HashSet<String>,
        result: &mut Vec<&'a ReactorProject>,
    ) -> Result<()> {
        if temp_visited.contains(id) {
            return Err(anyhow::anyhow!("Circular dependency detected: {id}"));
        }
        if visited.contains(id) {
            return Ok(());
        }

        temp_visited.insert(id.to_string());

        if let Some(project) = self.get_project(id) {
            for dep_id in &project.reactor_dependencies {
                self.visit(dep_id, visited, temp_visited, result)?;
            }
            result.push(project);
        }

        temp_visited.remove(id);
        visited.insert(id.to_string());

        Ok(())
    }

    /// Check if all dependencies of a project have succeeded
    pub fn can_build(&self, project_id: &str) -> bool {
        if let Some(project) = self.get_project(project_id) {
            for dep_id in &project.reactor_dependencies {
                if let Some(dep) = self.get_project(dep_id) {
                    if dep.status != ProjectBuildStatus::Success {
                        return false;
                    }
                }
            }
            true
        } else {
            false
        }
    }

    /// Check if the build should continue
    pub fn should_continue(&self) -> bool {
        if self.halted {
            return false;
        }

        if self.fail_fast {
            // Check if any project has failed
            !self.projects.iter().any(|p| p.status == ProjectBuildStatus::Failed)
        } else {
            // Continue if there are pending projects that can be built
            self.projects.iter().any(|p| {
                p.status == ProjectBuildStatus::Pending && self.can_build(&p.id)
            })
        }
    }

    /// Halt the build
    pub fn halt(&mut self) {
        self.halted = true;
    }

    /// Get summary statistics
    pub fn summary(&self) -> ReactorSummary {
        let mut summary = ReactorSummary::default();
        
        for project in &self.projects {
            match project.status {
                ProjectBuildStatus::Success => {
                    summary.success += 1;
                    if let Some(time) = project.build_time_ms {
                        summary.total_time_ms += time;
                    }
                }
                ProjectBuildStatus::Failed => summary.failed += 1,
                ProjectBuildStatus::Skipped => summary.skipped += 1,
                _ => {}
            }
        }

        summary.total = self.projects.len();
        summary
    }

    /// Skip all projects that depend on failed projects
    pub fn skip_downstream(&mut self) {
        let failed_ids: HashSet<String> = self.projects
            .iter()
            .filter(|p| p.status == ProjectBuildStatus::Failed)
            .map(|p| p.id.clone())
            .collect();

        // Find all projects that transitively depend on failed projects
        let mut to_skip = HashSet::new();
        for project in &self.projects {
            if project.status == ProjectBuildStatus::Pending
                && self.depends_on_any(&project.id, &failed_ids) {
                    to_skip.insert(project.id.clone());
                }
        }

        // Mark them as skipped
        for id in to_skip {
            if let Some(project) = self.get_project_mut(&id) {
                project.mark_skipped();
            }
        }
    }

    fn depends_on_any(&self, project_id: &str, targets: &HashSet<String>) -> bool {
        if let Some(project) = self.get_project(project_id) {
            for dep_id in &project.reactor_dependencies {
                if targets.contains(dep_id) {
                    return true;
                }
                if self.depends_on_any(dep_id, targets) {
                    return true;
                }
            }
        }
        false
    }
}

/// Summary of reactor build
#[derive(Debug, Default)]
pub struct ReactorSummary {
    pub total: usize,
    pub success: usize,
    pub failed: usize,
    pub skipped: usize,
    pub total_time_ms: u64,
}

impl ReactorSummary {
    pub fn is_success(&self) -> bool {
        self.failed == 0
    }

    pub fn format_time(&self) -> String {
        let seconds = self.total_time_ms / 1000;
        let minutes = seconds / 60;
        let remaining_seconds = seconds % 60;
        
        if minutes > 0 {
            format!("{minutes}:{remaining_seconds:02} min")
        } else {
            format!("{}.{:03} s", seconds, self.total_time_ms % 1000)
        }
    }
}

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

    #[test]
    fn test_reactor_project() {
        let project = ReactorProject::new("com.example", "my-app", "1.0.0", PathBuf::from("/project"));
        
        assert_eq!(project.id, "com.example:my-app");
        assert_eq!(project.status, ProjectBuildStatus::Pending);
    }

    #[test]
    fn test_project_status_transitions() {
        let mut project = ReactorProject::new("g", "a", "1.0", PathBuf::from("/"));
        
        project.mark_building();
        assert_eq!(project.status, ProjectBuildStatus::Building);
        
        project.mark_success(1000);
        assert_eq!(project.status, ProjectBuildStatus::Success);
        assert_eq!(project.build_time_ms, Some(1000));
    }

    #[test]
    fn test_reactor_build_order() {
        let mut reactor = ReactorBuildStatus::new();
        
        let core = ReactorProject::new("g", "core", "1.0", PathBuf::from("/core"));
        let mut api = ReactorProject::new("g", "api", "1.0", PathBuf::from("/api"));
        api.add_dependency("g:core");
        let mut app = ReactorProject::new("g", "app", "1.0", PathBuf::from("/app"));
        app.add_dependency("g:api");
        
        reactor.add_project(app);
        reactor.add_project(api);
        reactor.add_project(core);
        
        let order = reactor.build_order().unwrap();
        
        // core should come before api, api before app
        let core_pos = order.iter().position(|p| p.artifact_id == "core").unwrap();
        let api_pos = order.iter().position(|p| p.artifact_id == "api").unwrap();
        let app_pos = order.iter().position(|p| p.artifact_id == "app").unwrap();
        
        assert!(core_pos < api_pos);
        assert!(api_pos < app_pos);
    }

    #[test]
    fn test_can_build() {
        let mut reactor = ReactorBuildStatus::new();
        
        let mut core = ReactorProject::new("g", "core", "1.0", PathBuf::from("/core"));
        core.status = ProjectBuildStatus::Success;
        
        let mut api = ReactorProject::new("g", "api", "1.0", PathBuf::from("/api"));
        api.add_dependency("g:core");
        
        reactor.add_project(core);
        reactor.add_project(api);
        
        assert!(reactor.can_build("g:api"));
    }

    #[test]
    fn test_fail_fast() {
        let mut reactor = ReactorBuildStatus::new().with_fail_fast(true);
        
        let mut project = ReactorProject::new("g", "a", "1.0", PathBuf::from("/"));
        project.mark_failed("error", 100);
        reactor.add_project(project);
        
        assert!(!reactor.should_continue());
    }

    #[test]
    fn test_summary() {
        let mut reactor = ReactorBuildStatus::new();
        
        let mut p1 = ReactorProject::new("g", "a", "1.0", PathBuf::from("/a"));
        p1.mark_success(1000);
        
        let mut p2 = ReactorProject::new("g", "b", "1.0", PathBuf::from("/b"));
        p2.mark_failed("error", 500);
        
        reactor.add_project(p1);
        reactor.add_project(p2);
        
        let summary = reactor.summary();
        assert_eq!(summary.total, 2);
        assert_eq!(summary.success, 1);
        assert_eq!(summary.failed, 1);
        assert!(!summary.is_success());
    }
}