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
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
513
//! Repository traits and implementations for Artifact context

use super::value_objects::ArtifactCoordinates;
use crate::domain::shared::value_objects::Version;
use anyhow::{anyhow, Result};
use std::fs;
use std::path::PathBuf;

/// Artifact metadata from repository
#[derive(Debug, Clone)]
pub struct ArtifactMetadata {
    pub coordinates: ArtifactCoordinates,
    pub dependencies: Vec<ArtifactCoordinates>,
}

/// Repository for artifact storage and retrieval
pub trait ArtifactRepository: Send + Sync {
    /// Find an artifact in the repository
    fn find(&self, coords: &ArtifactCoordinates) -> Result<Option<PathBuf>> {
        if self.exists(coords) {
            Ok(Some(self.path().join(coords.repository_path())))
        } else {
            Ok(None)
        }
    }

    /// Install an artifact to the repository
    fn install(&self, coords: &ArtifactCoordinates, file: PathBuf) -> Result<()>;

    /// Check if an artifact exists
    fn exists(&self, coords: &ArtifactCoordinates) -> bool;

    /// Get the repository path
    fn path(&self) -> &PathBuf;

    /// Get artifact metadata (for dependency resolution)
    fn get_metadata(&self, coordinates: &ArtifactCoordinates) -> Result<ArtifactMetadata>;

    /// List available versions for an artifact
    fn list_versions(&self, coordinates: &ArtifactCoordinates) -> Result<Vec<Version>>;

    /// Download an artifact
    fn download(&self, coordinates: &ArtifactCoordinates) -> Result<Vec<u8>>;
}

/// Local repository implementation (e.g., ~/.m2/repository)
pub struct LocalRepository {
    base_path: PathBuf,
}

impl LocalRepository {
    /// Create a new local repository
    pub fn new(base_path: PathBuf) -> Result<Self> {
        if !base_path.exists() {
            fs::create_dir_all(&base_path)?;
        }
        Ok(Self { base_path })
    }

    /// Get default Maven local repository (~/.m2/repository)
    pub fn default_maven() -> Result<Self> {
        let home = dirs::home_dir().ok_or_else(|| anyhow!("Could not determine home directory"))?;
        let repo_path = home.join(".m2").join("repository");
        Self::new(repo_path)
    }

    /// Get artifact file path
    fn artifact_path(&self, coords: &ArtifactCoordinates) -> PathBuf {
        self.base_path.join(coords.repository_path())
    }

    /// Get POM file path
    fn pom_path(&self, coords: &ArtifactCoordinates) -> PathBuf {
        let pom_file = format!("{}-{}.pom", coords.artifact_id(), coords.version());
        self.base_path
            .join(coords.group_id().replace('.', "/"))
            .join(coords.artifact_id())
            .join(coords.version())
            .join(pom_file)
    }

    /// Parse POM file for metadata
    fn parse_pom(&self, pom_path: &PathBuf) -> Result<ArtifactMetadata> {
        if !pom_path.exists() {
            return Err(anyhow!("POM file not found: {pom_path:?}"));
        }

        let content = fs::read_to_string(pom_path)?;
        let dependencies = self.extract_dependencies_from_pom(&content)?;

        let coords = self.extract_coordinates_from_pom(&content)?;

        Ok(ArtifactMetadata {
            coordinates: coords,
            dependencies,
        })
    }

    fn extract_coordinates_from_pom(&self, _content: &str) -> Result<ArtifactCoordinates> {
        ArtifactCoordinates::from_gav("unknown:unknown:unknown")
    }

    fn extract_dependencies_from_pom(&self, _content: &str) -> Result<Vec<ArtifactCoordinates>> {
        Ok(Vec::new())
    }
}

impl ArtifactRepository for LocalRepository {
    fn install(&self, coords: &ArtifactCoordinates, source: PathBuf) -> Result<()> {
        let dest = self.artifact_path(coords);

        if let Some(parent) = dest.parent() {
            fs::create_dir_all(parent)?;
        }

        fs::copy(source, dest)?;
        Ok(())
    }

    fn exists(&self, coords: &ArtifactCoordinates) -> bool {
        self.artifact_path(coords).exists()
    }

    fn path(&self) -> &PathBuf {
        &self.base_path
    }

    fn get_metadata(&self, coordinates: &ArtifactCoordinates) -> Result<ArtifactMetadata> {
        let pom_path = self.pom_path(coordinates);
        self.parse_pom(&pom_path)
    }

    fn list_versions(&self, coordinates: &ArtifactCoordinates) -> Result<Vec<Version>> {
        let group_path = self
            .base_path
            .join(coordinates.group_id().replace('.', "/"))
            .join(coordinates.artifact_id());

        if !group_path.exists() {
            return Ok(Vec::new());
        }

        let mut versions = Vec::new();
        for entry in fs::read_dir(group_path)? {
            let entry = entry?;
            if entry.file_type()?.is_dir()
                && let Some(name) = entry.file_name().to_str() {
                    versions.push(Version::new(name));
                }
        }

        Ok(versions)
    }

    fn download(&self, coordinates: &ArtifactCoordinates) -> Result<Vec<u8>> {
        let path = self.artifact_path(coordinates);
        if !path.exists() {
            return Err(anyhow!("Artifact not found: {path:?}"));
        }
        fs::read(&path).map_err(|e| anyhow!("Failed to read artifact: {e}"))
    }
}

/// Remote repository implementation (e.g., Maven Central)
pub struct RemoteRepository {
    #[allow(dead_code)]
    url: String,
    cache_dir: PathBuf,
}

impl RemoteRepository {
    /// Create a new remote repository
    pub fn new(url: String, cache_dir: PathBuf) -> Result<Self> {
        if !cache_dir.exists() {
            fs::create_dir_all(&cache_dir)?;
        }
        Ok(Self { url, cache_dir })
    }

    /// Create Maven Central repository
    pub fn maven_central() -> Result<Self> {
        let home = dirs::home_dir().ok_or_else(|| anyhow!("Could not determine home directory"))?;
        let cache = home.join(".jbuild").join("cache").join("maven-central");
        Self::new("https://repo1.maven.org/maven2".to_string(), cache)
    }

    /// Get cached artifact path
    fn cache_path(&self, coords: &ArtifactCoordinates) -> PathBuf {
        self.cache_dir.join(coords.repository_path())
    }
}

impl ArtifactRepository for RemoteRepository {
    fn install(&self, coords: &ArtifactCoordinates, source: PathBuf) -> Result<()> {
        let dest = self.cache_path(coords);

        if let Some(parent) = dest.parent() {
            fs::create_dir_all(parent)?;
        }

        fs::copy(source, dest)?;
        Ok(())
    }

    fn exists(&self, coords: &ArtifactCoordinates) -> bool {
        self.cache_path(coords).exists()
    }

    fn path(&self) -> &PathBuf {
        &self.cache_dir
    }

    fn get_metadata(&self, coordinates: &ArtifactCoordinates) -> Result<ArtifactMetadata> {
        Ok(ArtifactMetadata {
            coordinates: coordinates.clone(),
            dependencies: Vec::new(),
        })
    }

    fn list_versions(&self, _coordinates: &ArtifactCoordinates) -> Result<Vec<Version>> {
        Ok(Vec::new())
    }

    fn download(&self, coordinates: &ArtifactCoordinates) -> Result<Vec<u8>> {
        let cached = self.cache_path(coordinates);
        if cached.exists() {
            return fs::read(&cached).map_err(|e| anyhow!("Failed to read cached artifact: {e}"));
        }

        Err(anyhow!("Remote download not yet implemented"))
    }
}

/// Repository chain for fallback logic
#[derive(Default)]
pub struct RepositoryChain {
    pub repositories: Vec<Box<dyn ArtifactRepository>>,
}

impl RepositoryChain {
    /// Create a new repository chain
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a repository to the chain
    pub fn add_repository(&mut self, repo: Box<dyn ArtifactRepository>) {
        self.repositories.push(repo);
    }

    /// Create default chain (local -> Maven Central)
    pub fn new_default() -> Result<Self> {
        let mut chain = Self::new();
        chain.add_repository(Box::new(LocalRepository::default_maven()?));
        chain.add_repository(Box::new(RemoteRepository::maven_central()?));
        Ok(chain)
    }
}

impl ArtifactRepository for RepositoryChain {
    fn install(&self, coords: &ArtifactCoordinates, file: PathBuf) -> Result<()> {
        if let Some(repo) = self.repositories.first() {
            repo.install(coords, file)
        } else {
            Err(anyhow!("No repositories in chain"))
        }
    }

    fn exists(&self, coords: &ArtifactCoordinates) -> bool {
        self.repositories.iter().any(|repo| repo.exists(coords))
    }

    fn path(&self) -> &PathBuf {
        self.repositories
            .first()
            .map(|repo| repo.path())
            .unwrap_or_else(|| {
                static DEFAULT_PATH: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
                DEFAULT_PATH.get_or_init(|| PathBuf::from("."))
            })
    }

    fn get_metadata(&self, coordinates: &ArtifactCoordinates) -> Result<ArtifactMetadata> {
        for repo in &self.repositories {
            if let Ok(metadata) = repo.get_metadata(coordinates) {
                return Ok(metadata);
            }
        }
        Err(anyhow!("Artifact metadata not found in any repository"))
    }

    fn list_versions(&self, coordinates: &ArtifactCoordinates) -> Result<Vec<Version>> {
        for repo in &self.repositories {
            if let Ok(versions) = repo.list_versions(coordinates)
                && !versions.is_empty() {
                    return Ok(versions);
                }
        }
        Ok(Vec::new())
    }

    fn download(&self, coordinates: &ArtifactCoordinates) -> Result<Vec<u8>> {
        for repo in &self.repositories {
            if let Ok(data) = repo.download(coordinates) {
                return Ok(data);
            }
        }
        Err(anyhow!("Artifact not found in any repository"))
    }
}



#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_local_repository_creation() {
        let temp_dir = TempDir::new().unwrap();
        let repo = LocalRepository::new(temp_dir.path().to_path_buf());
        assert!(repo.is_ok());
    }

    #[test]
    fn test_local_repository_install_and_exists() {
        let temp_dir = TempDir::new().unwrap();
        let repo = LocalRepository::new(temp_dir.path().to_path_buf()).unwrap();

        let coords = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();

        let source_file = temp_dir.path().join("test.jar");
        fs::write(&source_file, b"test content").unwrap();

        assert!(!repo.exists(&coords));

        let result = repo.install(&coords, source_file);
        assert!(result.is_ok());

        assert!(repo.exists(&coords));
    }

    #[test]
    fn test_local_repository_list_versions() {
        let temp_dir = TempDir::new().unwrap();
        let repo = LocalRepository::new(temp_dir.path().to_path_buf()).unwrap();

        let coords1 = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();
        let coords2 = ArtifactCoordinates::from_gav("com.example:test:2.0.0").unwrap();

        let source1 = temp_dir.path().join("test1.jar");
        let source2 = temp_dir.path().join("test2.jar");
        fs::write(&source1, b"test1").unwrap();
        fs::write(&source2, b"test2").unwrap();

        repo.install(&coords1, source1).unwrap();
        repo.install(&coords2, source2).unwrap();

        let versions = repo.list_versions(&coords1).unwrap();
        assert_eq!(versions.len(), 2);
    }

    #[test]
    fn test_local_repository_download() {
        let temp_dir = TempDir::new().unwrap();
        let repo = LocalRepository::new(temp_dir.path().to_path_buf()).unwrap();

        let coords = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();

        let source_file = temp_dir.path().join("test.jar");
        let content = b"test artifact content";
        fs::write(&source_file, content).unwrap();

        repo.install(&coords, source_file).unwrap();

        let downloaded = repo.download(&coords).unwrap();
        assert_eq!(downloaded, content);
    }

    #[test]
    fn test_local_repository_download_not_found() {
        let temp_dir = TempDir::new().unwrap();
        let repo = LocalRepository::new(temp_dir.path().to_path_buf()).unwrap();

        let coords = ArtifactCoordinates::from_gav("com.example:nonexistent:1.0.0").unwrap();

        let result = repo.download(&coords);
        assert!(result.is_err());
    }

    #[test]
    fn test_remote_repository_creation() {
        let temp_dir = TempDir::new().unwrap();
        let repo = RemoteRepository::new(
            "https://repo1.maven.org/maven2".to_string(),
            temp_dir.path().to_path_buf(),
        );
        assert!(repo.is_ok());
    }

    #[test]
    fn test_remote_repository_cache() {
        let temp_dir = TempDir::new().unwrap();
        let repo = RemoteRepository::new(
            "https://repo1.maven.org/maven2".to_string(),
            temp_dir.path().to_path_buf(),
        )
        .unwrap();

        let coords = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();

        let source_file = temp_dir.path().join("test.jar");
        fs::write(&source_file, b"cached content").unwrap();

        repo.install(&coords, source_file).unwrap();

        assert!(repo.exists(&coords));

        let cached = repo.download(&coords).unwrap();
        assert_eq!(cached, b"cached content");
    }

    #[test]
    fn test_repository_chain_creation() {
        let chain = RepositoryChain::new();
        assert_eq!(chain.repositories.len(), 0);
    }

    #[test]
    fn test_repository_chain_add_repository() {
        let temp_dir = TempDir::new().unwrap();
        let mut chain = RepositoryChain::new();

        let repo = LocalRepository::new(temp_dir.path().to_path_buf()).unwrap();
        chain.add_repository(Box::new(repo));

        assert_eq!(chain.repositories.len(), 1);
    }

    #[test]
    fn test_repository_chain_fallback() {
        let temp_dir1 = TempDir::new().unwrap();
        let temp_dir2 = TempDir::new().unwrap();

        let repo1 = LocalRepository::new(temp_dir1.path().to_path_buf()).unwrap();
        let repo2 = LocalRepository::new(temp_dir2.path().to_path_buf()).unwrap();

        let coords = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();

        let source_file = temp_dir2.path().join("test.jar");
        fs::write(&source_file, b"from repo2").unwrap();
        repo2.install(&coords, source_file).unwrap();

        let mut chain = RepositoryChain::new();
        chain.add_repository(Box::new(repo1));
        chain.add_repository(Box::new(repo2));

        assert!(chain.exists(&coords));

        let data = chain.download(&coords).unwrap();
        assert_eq!(data, b"from repo2");
    }

    #[test]
    fn test_repository_chain_install_to_first() {
        let temp_dir1 = TempDir::new().unwrap();
        let temp_dir2 = TempDir::new().unwrap();

        let repo1 = LocalRepository::new(temp_dir1.path().to_path_buf()).unwrap();
        let repo2 = LocalRepository::new(temp_dir2.path().to_path_buf()).unwrap();

        let mut chain = RepositoryChain::new();
        chain.add_repository(Box::new(repo1));
        chain.add_repository(Box::new(repo2));

        let coords = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();
        let source_file = temp_dir1.path().join("test.jar");
        fs::write(&source_file, b"test").unwrap();

        let result = chain.install(&coords, source_file);
        assert!(result.is_ok());
    }

    #[test]
    fn test_repository_chain_empty_error() {
        let chain = RepositoryChain::new();
        let coords = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();

        let temp_dir = TempDir::new().unwrap();
        let temp_file = temp_dir.path().join("test.jar");
        fs::write(&temp_file, b"test").unwrap();

        let result = chain.install(&coords, temp_file);
        assert!(result.is_err());
    }

    #[test]
    fn test_artifact_metadata() {
        let coords = ArtifactCoordinates::from_gav("com.example:test:1.0.0").unwrap();
        let dep1 = ArtifactCoordinates::from_gav("com.example:dep1:1.0.0").unwrap();
        let dep2 = ArtifactCoordinates::from_gav("com.example:dep2:2.0.0").unwrap();

        let metadata = ArtifactMetadata {
            coordinates: coords.clone(),
            dependencies: vec![dep1, dep2],
        };

        assert_eq!(metadata.coordinates.group_id(), "com.example");
        assert_eq!(metadata.dependencies.len(), 2);
    }
}