ognibuild 0.2.11

Detect and run any build system
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Support for Perl build systems.
//!
//! This module provides functionality for building, testing, and installing
//! Perl packages using various build systems such as DistZilla, Makefile.PL,
//! and ExtUtils::MakeMaker.

use crate::analyze::AnalyzedError;
use crate::buildsystem::{guaranteed_which, BuildSystem, DependencyCategory, Error};
use crate::dependencies::perl::PerlModuleDependency;
use crate::fix_build::{BuildFixer, IterateBuildError};
use crate::installer::Error as InstallerError;
use crate::session::Session;
use std::collections::HashMap;
use std::io::Read;
use std::path::{Path, PathBuf};

fn read_cpanfile(
    session: &dyn Session,
    args: Vec<&str>,
    category: DependencyCategory,
    fixers: &[&dyn BuildFixer<InstallerError>],
) -> impl Iterator<Item = (DependencyCategory, PerlModuleDependency)> {
    let mut argv = vec!["cpanfile-dump"];
    argv.extend(args);

    session
        .command(argv)
        .run_fixing_problems::<_, crate::buildsystem::Error>(fixers)
        .unwrap()
        .into_iter()
        .filter_map(move |line| {
            let line = line.trim();
            if !line.is_empty() {
                Some((category.clone(), PerlModuleDependency::simple(line)))
            } else {
                None
            }
        })
}

/// Extract declared dependencies from a cpanfile.
///
/// # Arguments
/// * `session` - The session to use for executing commands
/// * `fixers` - Fixers to apply if reading the cpanfile fails
///
/// # Returns
/// A list of dependencies declared in the cpanfile
pub fn declared_deps_from_cpanfile(
    session: &dyn Session,
    fixers: &[&dyn BuildFixer<InstallerError>],
) -> Vec<(DependencyCategory, PerlModuleDependency)> {
    read_cpanfile(
        session,
        vec!["--configure", "--build"],
        DependencyCategory::Build,
        fixers,
    )
    .chain(read_cpanfile(
        session,
        vec!["--test"],
        DependencyCategory::Test,
        fixers,
    ))
    .collect()
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
/// Metadata from META.yml for a Perl package.
///
/// This contains the parsed metadata from a META.yml file, which describes
/// the package and its dependencies.
pub struct Meta {
    name: String,
    #[serde(rename = "abstract")]
    r#abstract: String,
    version: String,
    license: String,
    author: Vec<String>,
    distribution_type: String,
    requires: HashMap<String, String>,
    recommends: HashMap<String, String>,
    build_requires: HashMap<String, String>,
    resources: HashMap<String, String>,
    #[serde(rename = "meta-spec")]
    meta_spec: HashMap<String, String>,
    generated_by: String,
    configure_requires: HashMap<String, String>,
}

/// Extract declared dependencies from a META.yml file.
///
/// # Arguments
/// * `f` - A reader for the META.yml file
///
/// # Returns
/// A list of dependencies declared in the META.yml file
pub fn declared_deps_from_meta_yml<R: Read>(
    f: R,
) -> Vec<(DependencyCategory, PerlModuleDependency)> {
    // See http://module-build.sourceforge.net/META-spec-v1.4.html for the specification of the format.
    let data: Meta = serde_yaml::from_reader(f).unwrap();

    let mut ret = vec![];

    // TODO: handle versions
    for name in data.requires.keys() {
        ret.push((
            DependencyCategory::Universal,
            PerlModuleDependency::simple(name),
        ));
    }
    for name in data.build_requires.keys() {
        ret.push((
            DependencyCategory::Build,
            PerlModuleDependency::simple(name),
        ));
    }
    for name in data.configure_requires.keys() {
        ret.push((
            DependencyCategory::Build,
            PerlModuleDependency::simple(name),
        ));
    }
    // TODO(jelmer): recommends
    ret
}

#[derive(Debug)]
/// DistZilla build system for Perl packages.
///
/// This build system handles Perl packages that use DistZilla for building.
pub struct DistZilla {
    path: PathBuf,
    dist_inkt_class: Option<String>,
}

impl DistZilla {
    /// Create a new DistZilla build system with the specified path.
    ///
    /// # Arguments
    /// * `path` - The path to the Perl package directory
    ///
    /// # Returns
    /// A new DistZilla build system instance
    pub fn new(path: PathBuf) -> Self {
        let mut dist_inkt_class = None;
        let mut f = std::fs::File::open(&path).unwrap();
        let mut contents = String::new();
        f.read_to_string(&mut contents).unwrap();
        for line in contents.lines() {
            let rest = if let Some(rest) = line.strip_prefix(";;") {
                rest
            } else {
                continue;
            };
            let (key, value) = if let Some((key, value)) = rest.split_once('=') {
                (key.trim(), value.trim())
            } else {
                continue;
            };
            if key == "class" && value.starts_with("'Dist::Inkt") {
                dist_inkt_class = Some(value[1..value.len() - 1].to_string());
                break;
            }
        }
        Self {
            path,
            dist_inkt_class,
        }
    }

    /// Set up the DistZilla build environment.
    ///
    /// # Arguments
    /// * `installer` - The installer to use for dependencies
    ///
    /// # Returns
    /// Ok on success or an error
    pub fn setup(
        &self,
        installer: &dyn crate::installer::Installer,
    ) -> Result<(), crate::installer::Error> {
        let dep = crate::dependencies::perl::PerlModuleDependency::simple("Dist::Inkt");
        installer.install(&dep, crate::installer::InstallationScope::Global)?;
        Ok(())
    }

    /// Probe a directory for a DistZilla build system.
    ///
    /// # Arguments
    /// * `path` - The path to check
    ///
    /// # Returns
    /// A DistZilla build system if one exists at the path, `None` otherwise
    pub fn probe(path: &Path) -> Option<Box<dyn BuildSystem>> {
        let dist_ini_path = path.join("dist.ini");
        if dist_ini_path.exists() && !path.join("Makefile.PL").exists() {
            Some(Box::new(Self::new(dist_ini_path)))
        } else {
            None
        }
    }
}

impl BuildSystem for DistZilla {
    fn name(&self) -> &str {
        "Dist::Zilla"
    }

    fn dist(
        &self,
        session: &dyn Session,
        installer: &dyn crate::installer::Installer,
        target_directory: &Path,
        quiet: bool,
    ) -> Result<std::ffi::OsString, crate::buildsystem::Error> {
        self.setup(installer)?;
        let dc = crate::dist_catcher::DistCatcher::default(&session.external_path(Path::new(".")));
        if self.dist_inkt_class.is_some() {
            session
                .command(vec![guaranteed_which(session, installer, "distinkt-dist")
                    .unwrap()
                    .to_str()
                    .unwrap()])
                .quiet(quiet)
                .run_detecting_problems()?;
        } else {
            // Default to invoking Dist::Zilla
            session
                .command(vec![
                    guaranteed_which(session, installer, "dzil")
                        .unwrap()
                        .to_str()
                        .unwrap(),
                    "build",
                    "--tgz",
                ])
                .quiet(quiet)
                .run_detecting_problems()?;
        }
        Ok(dc.copy_single(target_directory).unwrap().unwrap())
    }

    fn test(
        &self,
        session: &dyn Session,
        installer: &dyn crate::installer::Installer,
    ) -> Result<(), crate::buildsystem::Error> {
        // see also https://perlmaven.com/how-to-run-the-tests-of-a-typical-perl-module
        self.setup(installer)?;
        session
            .command(vec![
                guaranteed_which(session, installer, "dzil")
                    .unwrap()
                    .to_str()
                    .unwrap(),
                "test",
            ])
            .run_detecting_problems()?;
        Ok(())
    }

    fn build(
        &self,
        session: &dyn Session,
        installer: &dyn crate::installer::Installer,
    ) -> Result<(), crate::buildsystem::Error> {
        self.setup(installer)?;
        session
            .command(vec![
                guaranteed_which(session, installer, "dzil")
                    .unwrap()
                    .to_str()
                    .unwrap(),
                "build",
            ])
            .run_detecting_problems()?;
        Ok(())
    }

    fn clean(
        &self,
        _session: &dyn Session,
        _installer: &dyn crate::installer::Installer,
    ) -> Result<(), crate::buildsystem::Error> {
        Err(Error::Unimplemented)
    }

    fn install(
        &self,
        _session: &dyn Session,
        _installerr: &dyn crate::installer::Installer,
        _install_target: &crate::buildsystem::InstallTarget,
    ) -> Result<(), crate::buildsystem::Error> {
        Err(Error::Unimplemented)
    }

    fn get_declared_dependencies(
        &self,
        session: &dyn Session,
        fixers: Option<&[&dyn crate::fix_build::BuildFixer<InstallerError>]>,
    ) -> Result<
        Vec<(DependencyCategory, Box<dyn crate::dependency::Dependency>)>,
        crate::buildsystem::Error,
    > {
        let mut ret = vec![];
        if self.path.exists() {
            let lines = session
                .command(vec!["dzil", "authordeps"])
                .run_fixing_problems::<_, crate::buildsystem::Error>(fixers.unwrap_or(&[]))
                .unwrap();
            for entry in lines {
                ret.push((
                    DependencyCategory::Build,
                    Box::new(PerlModuleDependency::simple(entry.trim()))
                        as Box<dyn crate::dependency::Dependency>,
                ));
            }
        }
        if self.path.parent().unwrap().join("cpanfile").exists() {
            ret.extend(
                declared_deps_from_cpanfile(session, fixers.unwrap_or(&[]))
                    .into_iter()
                    .map(|(category, dep)| {
                        (
                            category,
                            Box::new(dep) as Box<dyn crate::dependency::Dependency>,
                        )
                    }),
            );
        }
        Ok(ret)
    }

    fn get_declared_outputs(
        &self,
        _session: &dyn Session,
        _fixers: Option<&[&dyn crate::fix_build::BuildFixer<InstallerError>]>,
    ) -> Result<Vec<Box<dyn crate::output::Output>>, crate::buildsystem::Error> {
        Err(Error::Unimplemented)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[derive(Debug)]
/// Module::Build::Tiny build system for Perl packages.
///
/// This build system handles Perl packages that use Module::Build::Tiny for building,
/// including support for Minilla.
pub struct PerlBuildTiny {
    path: PathBuf,
    minilla: bool,
}

impl PerlBuildTiny {
    /// Create a new PerlBuildTiny build system with the specified path.
    ///
    /// # Arguments
    /// * `path` - The path to the Perl package directory
    ///
    /// # Returns
    /// A new PerlBuildTiny build system instance
    pub fn new(path: PathBuf) -> Self {
        let minilla = path.join("minil.toml").exists();
        Self { path, minilla }
    }

    fn setup(
        &self,
        session: &dyn Session,
        fixers: Option<&[&dyn BuildFixer<InstallerError>]>,
    ) -> Result<(), crate::buildsystem::Error> {
        let fixers = fixers.unwrap_or(&[]);
        let argv = vec!["perl", "Build.PL"];
        session
            .command(argv)
            .run_fixing_problems::<_, crate::buildsystem::Error>(fixers)?;
        Ok(())
    }

    /// Probe a directory for a Module::Build::Tiny build system.
    ///
    /// # Arguments
    /// * `path` - The path to check
    ///
    /// # Returns
    /// A PerlBuildTiny build system if one exists at the path, `None` otherwise
    pub fn probe(path: &Path) -> Option<Box<dyn BuildSystem>> {
        if path.join("Build.PL").exists() {
            log::debug!("Found Build.PL, assuming Module::Build::Tiny package.");
            Some(Box::new(Self::new(path.to_path_buf())))
        } else {
            None
        }
    }
}

impl BuildSystem for PerlBuildTiny {
    fn name(&self) -> &str {
        "Module::Build::Tiny"
    }

    fn dist(
        &self,
        session: &dyn Session,
        _installer: &dyn crate::installer::Installer,
        target_directory: &Path,
        quiet: bool,
    ) -> Result<std::ffi::OsString, crate::buildsystem::Error> {
        self.setup(session, None)?;
        let dc = crate::dist_catcher::DistCatcher::default(&session.external_path(Path::new(".")));
        if self.minilla {
            // minil seems to return 0 even if it didn't produce a tarball :(
            crate::analyze::run_detecting_problems(
                session,
                vec!["minil", "dist"],
                Some(&|_, _| dc.find_files().is_none()),
                quiet,
                None,
                None,
                None,
                None,
            )?;
        } else {
            match session
                .command(vec!["./Build", "dist"])
                .run_detecting_problems()
            {
                Err(AnalyzedError::Unidentified { lines, .. })
                    if lines.iter().any(|l| {
                        l.contains("Can't find dist packages without a MANIFEST file")
                    }) =>
                {
                    session
                        .command(vec!["./Build", "manifest"])
                        .run_detecting_problems()?;
                    session
                        .command(vec!["./Build", "dist"])
                        .run_detecting_problems()
                }
                Err(AnalyzedError::Unidentified { lines, .. })
                    if lines.iter().any(|l| l.contains("No such action 'dist'")) =>
                {
                    unimplemented!("Module::Build::Tiny dist command not supported");
                }
                other => other,
            }?;
        }
        Ok(dc.copy_single(target_directory).unwrap().unwrap())
    }

    fn test(
        &self,
        session: &dyn Session,
        _installer: &dyn crate::installer::Installer,
    ) -> Result<(), crate::buildsystem::Error> {
        self.setup(session, None)?;
        if self.minilla {
            session
                .command(vec!["minil", "test"])
                .run_detecting_problems()?;
        } else {
            session
                .command(vec!["./Build", "test"])
                .run_detecting_problems()?;
        }
        Ok(())
    }

    fn build(
        &self,
        session: &dyn Session,
        _installer: &dyn crate::installer::Installer,
    ) -> Result<(), crate::buildsystem::Error> {
        self.setup(session, None)?;
        session
            .command(vec!["./Build", "build"])
            .run_detecting_problems()?;
        Ok(())
    }

    fn clean(
        &self,
        session: &dyn Session,
        _installer: &dyn crate::installer::Installer,
    ) -> Result<(), crate::buildsystem::Error> {
        self.setup(session, None)?;
        session
            .command(vec!["./Build", "clean"])
            .run_detecting_problems()?;
        Ok(())
    }

    fn install(
        &self,
        session: &dyn Session,
        _installer: &dyn crate::installer::Installer,
        _install_target: &crate::buildsystem::InstallTarget,
    ) -> Result<(), crate::buildsystem::Error> {
        self.setup(session, None)?;
        if self.minilla {
            session
                .command(vec!["minil", "install"])
                .run_detecting_problems()?;
        } else {
            session
                .command(vec!["./Build", "install"])
                .run_detecting_problems()?;
        }
        Ok(())
    }

    fn get_declared_dependencies(
        &self,
        session: &dyn Session,
        fixers: Option<&[&dyn crate::fix_build::BuildFixer<InstallerError>]>,
    ) -> Result<
        Vec<(DependencyCategory, Box<dyn crate::dependency::Dependency>)>,
        crate::buildsystem::Error,
    > {
        self.setup(session, fixers)?;
        if self.minilla {
            // Minilla doesn't seem to have a way to just regenerate the metadata :(
        } else {
            let cmd = session.command(vec!["./Build", "distmeta"]);

            if let Some(fixers) = fixers {
                match cmd.run_fixing_problems::<_, crate::buildsystem::Error>(fixers) {
                    Err(IterateBuildError::Unidentified { lines, .. })
                        if lines
                            .iter()
                            .any(|l| l.contains("No such action 'distmeta'")) =>
                    {
                        // Module::Build::Tiny doesn't have a distmeta action
                        Ok(Vec::new())
                    }
                    Err(IterateBuildError::Unidentified { lines, .. })
                        if lines.iter().any(|l| {
                            l.contains(
                                "Do not run distmeta. Install Minilla and `minil install` instead.",
                            )
                        }) =>
                    {
                        log::warn!(
                            "did not detect  minilla, but it is required to get the dependencies"
                        );
                        Ok(Vec::new())
                    }
                    other => other,
                }?;
            } else {
                cmd.run_detecting_problems()?;
            }
        }
        let meta_yml_path = self.path.join("META.yml");
        if meta_yml_path.exists() {
            let f = std::fs::File::open(&meta_yml_path).unwrap();
            Ok(declared_deps_from_meta_yml(f)
                .into_iter()
                .map(|(category, dep)| {
                    (
                        category,
                        Box::new(dep) as Box<dyn crate::dependency::Dependency>,
                    )
                })
                .collect())
        } else {
            Ok(vec![])
        }
    }

    fn get_declared_outputs(
        &self,
        _session: &dyn Session,
        _fixers: Option<&[&dyn crate::fix_build::BuildFixer<InstallerError>]>,
    ) -> Result<Vec<Box<dyn crate::output::Output>>, crate::buildsystem::Error> {
        Err(Error::Unimplemented)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}