File: Staging.rakumod

package info (click to toggle)
rakudo 2024.09-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,508 kB
  • sloc: perl: 4,815; ansic: 2,724; java: 2,622; javascript: 590; makefile: 434; sh: 370; cpp: 152
file content (78 lines) | stat: -rw-r--r-- 2,566 bytes parent folder | download | duplicates (2)
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
class CompUnit::Repository::Staging is CompUnit::Repository::Installation {
    has Str $.name;
    has CompUnit::Repository $!parent;

    submethod TWEAK(--> Nil) {
        $!parent = CompUnit::RepositoryRegistry.repository-for-name($!name);
        CompUnit::RepositoryRegistry.register-name($!name, self);
    }

    method short-id(--> Str:D) { 'staging' }

    method path-spec(CompUnit::Repository::Staging:D: --> Str:D) {
        self.^name ~ '#name(' ~ $!name ~ ')#' ~ self.prefix.absolute
    }

    method source-file(CompUnit::Repository::Staging:D:
      Str:D $name
    --> IO::Path) {
        my $file := self.prefix.add($name);
        $file.e ?? $file !! $!parent.source-file($name)
    }

    method resource(CompUnit::Repository::Staging:D:
      $dist-id, $key
    ) {
        # check if the dist is installed here
        (try self.distribution($dist-id))
          # we have the dist, so it's safe to access the resource the normal way
          ?? callsame()
          # lookup failed, so it's probably not installed here but in the parent
          !! $!parent.resource($dist-id, $key)
    }

    my sub really-unlink(IO::Path:D $io) {
        # attempt to unlink until succeeds at .1 second intervals
        # needed on Windows because it can easily race and fail there
        if Rakudo::Internals.IS-WIN {
            for ^10 {
                .throw without $io.unlink;  # throw actual failures
                last unless $io.e;
                sleep .1;
            }
        }
        else {
            .throw without $io.unlink;      # throw actual failures
        }
    }

    method remove-artifacts(CompUnit::Repository::Staging:D: --> Nil) {
        my $io := self.prefix;
        really-unlink($io.child("version"));
        really-unlink(.IO) for Rakudo::Internals.DIR-RECURSE(
          $io.absolute, :file(*.ends-with(".lock"))
        );
    }

    method deploy(CompUnit::Repository::Staging:D: --> Nil) {
        my $from    := self.prefix.absolute;
        my $relpath := $from.chars;
        my $to      := $!parent.prefix;

        for Rakudo::Internals.DIR-RECURSE($from) -> $path {
            my $destination := $to.add($path.substr($relpath));
            $destination.parent.mkdir;
            $path.IO.copy: $destination;
        }
    }

    sub self-destruct(IO::Path:D $io --> Nil) {
        .d ?? self-destruct($_) !! .unlink for $io.dir;
        $io.rmdir;
    }
    method self-destruct(CompUnit::Repository::Staging:D: --> Nil) {
        self-destruct self.prefix;
    }
}

# vim: expandtab shiftwidth=4