ploys 0.6.0

A utility to manage projects, packages, releases and deployments.
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! Project inspection and management utilities
//!
//! This module includes utilities for inspecting and managing projects located
//! in one of several supported formats including a local Git repository and a
//! remote GitHub repository.
//!
//! ## Git
//!
//! To open a local Git project use the [`Project::git`] constructor and pass in
//! a path to the project on the local file system. The target directory must be
//! initialized as a Git repository.
//!
//! ```no_run
//! use ploys::project::Project;
//!
//! let project = Project::git(".").unwrap();
//!
//! println!("Name:       {}", project.name());
//! println!("Repository: {}", project.repository().unwrap());
//! ```
//!
//! ## GitHub
//!
//! To open a remote GitHub project use the [`Project::github`] constructor and
//! pass in a string in the `owner/repo` format. The target identifier must
//! match an existing GitHub repository.
//!
//! ```no_run
//! use ploys::project::Project;
//!
//! let project = Project::github("ploys/ploys").unwrap();
//!
//! println!("Name:       {}", project.name());
//! println!("Repository: {}", project.repository().unwrap());
//! ```

pub mod config;
mod error;
mod packages;
mod release;

use std::str::FromStr;

use bytes::Bytes;
use either::Either;
use relative_path::{RelativePath, RelativePathBuf};

use crate::package::lockfile::CargoLockfile;
use crate::package::manifest::CargoManifest;
use crate::package::{BumpOrVersion, Package, PackageKind};
use crate::repository::types::staging::Staging;
use crate::repository::{Remote, RepoSpec, Repository, Stage};

pub use self::config::Config;
pub use self::error::Error;
pub use self::packages::Packages;
pub use self::release::{ReleaseBuilder, ReleaseRequest, ReleaseRequestBuilder};

/// A project from one of several supported repositories.
///
/// A valid project contains a `Ploys.toml` configuration file in the following
/// format. Note that only the `project.name` field is required.
///
/// ```toml
/// [project]
/// name = "{project-name}"
/// description = "{project-description}"
/// repository = "https://github.com/{project-owner}/{project-name}"
/// ```
pub struct Project<T = Staging> {
    pub(crate) repository: T,
    config: Config,
}

impl Project {
    /// Creates a new project.
    pub fn new(name: impl Into<String>) -> Self {
        let config = Config::new(name);

        Self {
            repository: Staging::new()
                .with_file("Ploys.toml", config.to_string().into_bytes())
                .expect("infallible"),
            config,
        }
    }
}

impl<T> Project<T>
where
    T: Repository,
{
    /// Opens an existing project from the given repository.
    pub fn open(repository: T) -> Result<Self, Error<T::Error>> {
        let config = repository
            .get_file("Ploys.toml")
            .map_err(Error::Repository)?
            .ok_or(self::config::Error::Missing)?;

        Ok(Self {
            config: Config::from_bytes(&config)?,
            repository,
        })
    }
}

impl<T> Project<T> {
    /// Gets the project name.
    pub fn name(&self) -> &str {
        self.config.project().name()
    }

    /// Gets the project description.
    pub fn description(&self) -> Option<&str> {
        self.config.project_description()
    }

    /// Sets the project description.
    pub fn set_description(&mut self, description: impl Into<String>) -> &mut Self {
        self.config.set_project_description(description);
        self
    }

    /// Builds the project with the given description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.set_description(description);
        self
    }

    /// Gets the project repository.
    pub fn repository(&self) -> Option<RepoSpec> {
        self.config.project_repository()
    }

    /// Sets the project repository.
    pub fn set_repository(&mut self, repository: impl Into<RepoSpec>) -> &mut Self {
        self.config.set_project_repository(repository);
        self
    }

    /// Builds the project with the given repository.
    pub fn with_repository(mut self, repository: impl Into<RepoSpec>) -> Self {
        self.set_repository(repository);
        self
    }

    /// Gets the project authors.
    pub fn authors(&self) -> impl Iterator<Item = &str> {
        self.config.project_authors()
    }

    /// Sets the project authors.
    pub fn set_authors(
        &mut self,
        authors: impl IntoIterator<Item = impl Into<String>>,
    ) -> &mut Self {
        self.config
            .set_project_authors(authors.into_iter().map(Into::into));
        self
    }

    /// Builds the project with the given authors.
    pub fn with_authors(mut self, authors: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.set_authors(authors);
        self
    }
}

impl<T> Project<T>
where
    T: Stage,
{
    /// Adds a file to the project.
    pub fn add_file(
        &mut self,
        path: impl Into<RelativePathBuf>,
        file: impl Into<Bytes>,
    ) -> Result<&mut Self, Error<T::Error>> {
        self.repository
            .add_file(path, file)
            .map_err(Error::Repository)?;

        Ok(self)
    }

    /// Builds the project with the given file.
    pub fn with_file(
        mut self,
        path: impl Into<RelativePathBuf>,
        file: impl Into<Bytes>,
    ) -> Result<Self, Error<T::Error>> {
        self.add_file(path, file)?;

        Ok(self)
    }
}

impl<T> Project<T>
where
    T: Repository,
{
    /// Gets a file at the given path.
    pub fn get_file(
        &self,
        path: impl AsRef<RelativePath>,
    ) -> Result<Option<Bytes>, Error<T::Error>> {
        if path.as_ref() == "Ploys.toml" {
            return Ok(Some(self.config.to_string().into()));
        }

        self.repository.get_file(path).map_err(Error::Repository)
    }

    /// Gets a file at the given path in the specified format.
    #[allow(clippy::type_complexity)]
    pub fn get_file_as<U>(
        &self,
        path: impl AsRef<RelativePath>,
    ) -> Result<Option<U>, Either<Error<T::Error>, U::Err>>
    where
        U: FromStr,
    {
        match self.get_file(path).map_err(Either::Left)? {
            Some(bytes) => match std::str::from_utf8(&bytes) {
                Ok(str) => str.parse().map(Some).map_err(Either::Right),
                Err(err) => Err(Either::Left(Error::Utf8(err))),
            },
            None => Ok(None),
        }
    }
}

impl<T> Project<T>
where
    T: Stage,
{
    /// Adds the given package to the project.
    pub fn add_package(
        &mut self,
        package: impl Into<Package>,
    ) -> Result<&mut Self, Error<T::Error>> {
        let package = package.into();
        let base_path = RelativePath::new("packages").join(package.name());

        for path in package.repository.get_index().expect("infallible") {
            if path == package.manifest_path() {
                continue;
            }

            if let Some(file) = package
                .repository
                .get_file(&path)
                .expect("valid path from index")
            {
                self.add_file(base_path.join(path), file)?;
            }
        }

        self.add_file(
            base_path.join(package.manifest_path()),
            package.manifest().to_string().into_bytes(),
        )?;

        match package.kind() {
            PackageKind::Cargo => {
                let mut manifest = self
                    .get_file_as::<CargoManifest>("Cargo.toml")
                    .map_err(|err| {
                        err.map_right(crate::package::Error::Manifest)
                            .map_right(Error::Package)
                            .into_inner()
                    })?
                    .unwrap_or_default();

                manifest.add_workspace_member("packages/*");
                manifest.add_workspace_member(base_path.join(package.path()).as_str());

                let mut lockfile = self
                    .get_file_as::<CargoLockfile>("Cargo.lock")
                    .map_err(|err| {
                        err.map_right(crate::package::Error::Lockfile)
                            .map_right(Error::Package)
                            .into_inner()
                    })?
                    .unwrap_or_default();

                lockfile.add_package(package.manifest().try_as_cargo_ref().expect("cargo"));

                self.add_file("Cargo.toml", manifest.to_string().into_bytes())?;
                self.add_file("Cargo.lock", lockfile.to_string().into_bytes())?;
            }
        }

        Ok(self)
    }

    /// Builds the project with the given package.
    pub fn with_package(mut self, package: impl Into<Package>) -> Result<Self, Error<T::Error>> {
        self.add_package(package)?;

        Ok(self)
    }
}

impl<T> Project<T>
where
    T: Repository,
{
    /// Gets a package with the given name.
    pub fn get_package(&self, name: impl AsRef<str>) -> Option<Package<&T>> {
        self.packages()
            .find(|package| package.name() == name.as_ref())
    }

    /// Gets an iterator over the project packages.
    pub fn packages(&self) -> Packages<'_, T> {
        Packages::new(self)
    }
}

impl<T> Project<T>
where
    T: Repository,
{
    /// Reloads the project configuration.
    pub fn reload(&mut self) -> Result<&mut Self, Error<T::Error>> {
        let config = self
            .repository
            .get_file("Ploys.toml")
            .map_err(Error::Repository)?
            .ok_or(self::config::Error::Missing)?;

        self.config = Config::from_bytes(&config)?;

        Ok(self)
    }

    /// Builds the project with reloaded project configuration.
    pub fn reloaded(mut self) -> Result<Self, Error<T::Error>> {
        self.reload()?;

        Ok(self)
    }
}

impl<T> Project<T>
where
    T: Remote + Clone,
{
    /// Constructs a new package release request builder.
    pub fn create_package_release_request(
        &self,
        package: impl AsRef<str>,
        version: impl Into<BumpOrVersion>,
    ) -> Result<ReleaseRequestBuilder<'_, T>, Error<T::Error>> {
        let package = self
            .get_package(package.as_ref())
            .ok_or_else(|| {
                Error::Package(crate::package::Error::NotFound(
                    package.as_ref().to_string(),
                ))
            })?
            .detached();

        Ok(ReleaseRequestBuilder::new(self, package, version.into()))
    }

    /// Constructs a new package release builder.
    pub fn create_package_release(
        &self,
        package: impl AsRef<str>,
    ) -> Result<ReleaseBuilder<'_, T>, Error<T::Error>> {
        let package = self
            .get_package(package.as_ref())
            .ok_or_else(|| {
                Error::Package(crate::package::Error::NotFound(
                    package.as_ref().to_string(),
                ))
            })?
            .detached();

        Ok(ReleaseBuilder::new(self, package))
    }
}

#[cfg(feature = "fs")]
mod fs {
    use std::io::{Error as IoError, ErrorKind};
    use std::path::PathBuf;

    use crate::repository::types::fs::{Error as FsError, FileSystem};
    use crate::repository::types::staging::Staging;
    use crate::repository::{Commit, Open, Repository, Stage};

    use super::{Error, Project};

    /// The [`FileSystem`] repository constructors.
    impl Project<FileSystem> {
        /// Opens a project from a [`FileSystem`] repository.
        pub fn fs<P>(path: P) -> Result<Self, Error<FsError>>
        where
            P: Into<PathBuf>,
        {
            Self::open(FileSystem::open(path)?)
        }

        /// Opens a project from a [`FileSystem`] repository in the current
        /// directory.
        pub fn current_dir() -> Result<Self, Error<FsError>> {
            Self::open(FileSystem::current_dir()?)
        }

        /// Writes the project to the file system.
        ///
        /// This method writes any staged file changes in this project to the
        /// file system, including the addition and removal of files. This
        /// includes the current project configuration, overriding any changes
        /// on disk.
        pub fn write(&mut self) -> Result<&mut Self, Error<FsError>> {
            self.repository.commit(())?;

            Ok(self)
        }
    }

    impl Project<Staging> {
        /// Writes the project to the file system.
        ///
        /// This method upgrades the project to use a [`FileSystem`] repository
        /// by writing the contents of the [`Staging`] repository to the file
        /// system. This includes the current project configuration stored in
        /// the project and not the original configuration from the repository.
        pub fn write<P>(self, path: P, force: bool) -> Result<Project<FileSystem>, Error<FsError>>
        where
            P: Into<PathBuf>,
        {
            let path = path.into();
            let repository = FileSystem::open(&path)?;

            if !force && repository.get_index()?.count() > 0 {
                return Err(Error::Repository(FsError::Io(IoError::new(
                    ErrorKind::DirectoryNotEmpty,
                    "Expected an empty directory",
                ))));
            }

            Ok(Project {
                repository: repository
                    .with_files(self.repository)?
                    .with_file("Ploys.toml", self.config.to_string())?
                    .committed(())?,
                config: self.config,
            })
        }
    }
}

#[cfg(feature = "git")]
mod git {
    use std::path::PathBuf;

    use crate::repository::Open;
    use crate::repository::revision::Revision;
    use crate::repository::types::git::{Error as GitError, Git};

    use super::{Error, Project};

    /// The [`Git`] repository constructors.
    impl Project<Git> {
        /// Opens a project from a [`Git`] repository.
        pub fn git<P>(path: P) -> Result<Self, Error<GitError>>
        where
            P: Into<PathBuf>,
        {
            Self::open(Git::open(path).map_err(Error::Repository)?)
        }

        /// Opens a project from a [`Git`] repository and revision.
        pub fn git_with_revision<P, V>(path: P, revision: V) -> Result<Self, Error<GitError>>
        where
            P: Into<PathBuf>,
            V: Into<Revision>,
        {
            Self::open(Git::open(path)?.with_revision(revision))
        }
    }

    impl TryFrom<Git> for Project<Git> {
        type Error = Error<GitError>;

        fn try_from(repository: Git) -> Result<Self, Self::Error> {
            Self::open(repository)
        }
    }
}

#[cfg(feature = "github")]
mod github {
    use crate::repository::Open;
    use crate::repository::revision::Revision;
    use crate::repository::types::github::{Error as GitHubError, GitHub, GitHubRepoSpec};

    use super::{Error, Project};

    /// The [`GitHub`] repository constructors.
    impl Project<GitHub> {
        /// Opens a project from a [`GitHub`] repository.
        pub fn github<R>(repo: R) -> Result<Self, Error<GitHubError>>
        where
            R: TryInto<GitHubRepoSpec, Error: Into<GitHubError>>,
        {
            Self::open(GitHub::open(repo)?.validated()?)
        }

        /// Opens a project from a [`GitHub`] repository and revision.
        pub fn github_with_revision<R, V>(repo: R, revision: V) -> Result<Self, Error<GitHubError>>
        where
            R: TryInto<GitHubRepoSpec, Error: Into<GitHubError>>,
            V: Into<Revision>,
        {
            Self::open(GitHub::open(repo)?.with_revision(revision).validated()?)
        }

        /// Opens a project from a [`GitHub`] repository and authentication
        /// token.
        pub fn github_with_authentication_token<R, T>(
            repo: R,
            token: T,
        ) -> Result<Self, Error<GitHubError>>
        where
            R: TryInto<GitHubRepoSpec, Error: Into<GitHubError>>,
            T: Into<String>,
        {
            Self::open(
                GitHub::open(repo)?
                    .with_authentication_token(token)
                    .validated()?,
            )
        }

        /// Opens a project from a [`GitHub`] repository, revision, and
        /// authentication token.
        pub fn github_with_revision_and_authentication_token<R, V, T>(
            repo: R,
            revision: V,
            token: T,
        ) -> Result<Self, Error<GitHubError>>
        where
            R: TryInto<GitHubRepoSpec, Error: Into<GitHubError>>,
            V: Into<Revision>,
            T: Into<String>,
        {
            Self::open(
                GitHub::open(repo)?
                    .with_revision(revision)
                    .with_authentication_token(token)
                    .validated()?,
            )
        }
    }

    impl TryFrom<GitHub> for Project<GitHub> {
        type Error = Error<GitHubError>;

        fn try_from(repository: GitHub) -> Result<Self, Self::Error> {
            Self::open(repository)
        }
    }
}

#[cfg(all(feature = "fs", feature = "git"))]
mod fs_git {
    use crate::repository::Open;
    use crate::repository::revision::Revision;
    use crate::repository::types::fs::FileSystem;
    use crate::repository::types::git::{Error as GitError, Git};

    use super::{Error, Project};

    impl Project<FileSystem> {
        /// Upgrades to a [`Git`] repository without changing configuration.
        ///
        /// This method opens a [`Git`] repository at the same path as the
        /// [`FileSystem`] repository, keeping the project configuration from
        /// the current repository. Any changes to files that have not been
        /// committed to the target revision will not be accessible. This could
        /// lead to an invalid configuration state so the [`Project::reload`]
        /// and [`Project::reloaded`] methods are available to reload the
        /// configuration.
        pub fn into_git(
            self,
            revision: impl Into<Revision>,
        ) -> Result<Project<Git>, Error<GitError>> {
            Ok(Project {
                repository: Git::open(self.repository.path())?.with_revision(revision),
                config: self.config,
            })
        }

        /// Initializes a new [`Git`] repository.
        ///
        /// This method creates a new a [`Git`] repository at the same path as
        /// the [`FileSystem`] repository, keeping the project configuration
        /// from the current repository. This does not stage or commit changes
        /// so the new repository will appear to be empty.
        pub fn init_git(self) -> Result<Project<Git>, Error<GitError>> {
            Ok(Project {
                repository: Git::init(self.repository.path())?,
                config: self.config,
            })
        }
    }

    impl TryFrom<Project<FileSystem>> for Project<Git> {
        type Error = Error<GitError>;

        fn try_from(project: Project<FileSystem>) -> Result<Self, Self::Error> {
            project.into_git(Revision::head())
        }
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use semver::Version;

    use crate::changelog::Changelog;
    use crate::package::Package;
    use crate::package::lockfile::CargoLockfile;
    use crate::package::manifest::CargoManifest;
    use crate::repository::types::staging::Staging;
    use crate::repository::{RepoSpec, Stage};

    use super::Project;

    #[test]
    fn test_builder() {
        let project = Project::new("example")
            .with_description("An example project.")
            .with_repository("ploys/example".parse::<RepoSpec>().unwrap())
            .with_authors(["Joe Bloggs <joe.bloggs@example.com>"]);

        assert_eq!(project.name(), "example");
        assert_eq!(project.description().unwrap(), "An example project.");
        assert_eq!(
            project.repository().unwrap(),
            "ploys/example".parse::<RepoSpec>().unwrap()
        );
        assert_eq!(
            project.authors().collect::<Vec<_>>(),
            ["Joe Bloggs <joe.bloggs@example.com>"]
        );

        let mut project = project.reloaded().unwrap();

        assert_eq!(project.name(), "example");
        assert_eq!(project.description(), None);
        assert_eq!(project.repository(), None);
        assert_eq!(project.authors().count(), 0);

        let package_a = Package::new_cargo("example-one");
        let package_b = Package::new_cargo("example-two")
            .with_version(Version::new(0, 1, 0))
            .with_file("CHANGELOG.md", Changelog::new().to_string().into_bytes())
            .unwrap();

        project.add_package(package_a).unwrap();
        project.add_package(package_b).unwrap();

        let package_a = project.get_package("example-one").unwrap();
        let package_b = project.get_package("example-two").unwrap();

        assert_eq!(package_a.name(), "example-one");
        assert_eq!(package_a.version(), Version::new(0, 0, 0));
        assert_eq!(package_a.get_file("CHANGELOG.md").unwrap(), None);

        assert_eq!(package_b.name(), "example-two");
        assert_eq!(package_b.version(), Version::new(0, 1, 0));
        assert_eq!(
            package_b.get_file_as("CHANGELOG.md").unwrap(),
            Some(Changelog::new())
        );

        let manifest = project
            .get_file_as::<CargoManifest>("Cargo.toml")
            .unwrap()
            .unwrap();

        let members = manifest.members().unwrap();

        assert!(members.includes(Path::new("packages/example-one")));
        assert!(members.includes(Path::new("packages/example-two")));

        let lockfile = project
            .get_file_as::<CargoLockfile>("Cargo.lock")
            .unwrap()
            .unwrap();

        assert_eq!(
            lockfile.get_package_version("example-one"),
            Some(Version::new(0, 0, 0))
        );
        assert_eq!(
            lockfile.get_package_version("example-two"),
            Some(Version::new(0, 1, 0))
        );
    }

    #[test]
    fn test_project_staging_repository() {
        let repository = Staging::new()
            .with_file("Ploys.toml", "[project]\nname = \"example\"")
            .unwrap();
        let mut project = Project::open(repository).unwrap();

        assert_eq!(project.name(), "example");
        assert_eq!(project.description(), None);

        project.set_description("An example project.");

        assert_eq!(project.description(), Some("An example project."));

        let mut project = project.reloaded().unwrap();

        assert_eq!(project.name(), "example");
        assert_eq!(project.description(), None);

        project.add_file("hello-world.txt", "Hello World!").unwrap();

        let txt = project.get_file("hello-world.txt").unwrap();

        assert_eq!(txt, Some("Hello World!".into()));
    }
}