-
Notifications
You must be signed in to change notification settings - Fork 3
/
ecogen.pm6
81 lines (63 loc) · 2.7 KB
/
ecogen.pm6
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
class App::ecogen { }
my $GIT_CMD = %*ENV<GIT_CMD> // 'git';
sub from-json($text) { Rakudo::Internals::JSON.from-json($text) }
sub to-json(|c) { Rakudo::Internals::JSON.to-json(|c) }
sub powershell-webrequest($uri) {
return Nil unless once { $*DISTRO.is-win && so try run('powershell', '-help', :!out, :!err) };
my $content = shell("cmd /c powershell -executionpolicy bypass -command (Invoke-WebRequest -UseBasicParsing -URI $uri).Content", :out).out.slurp-rest(:close);
return $content;
}
sub curl($uri) {
return Nil unless once { so try run('curl', '--help', :!out, :!err) };
my $content = run('curl', '--max-time', 60, '-s', '-L', $uri, :out).out.slurp-rest(:close);
return $content;
}
sub wget($uri) {
return Nil unless once { so try run('wget', '--help', :!out, :!err) };
my $content = run('wget', '--timeout=60', '-qO-', $uri, :out).out.slurp-rest(:close);
return $content;
}
role Ecosystem {
method IO { ... }
method meta-uris { ... }
method index-file { $.IO.parent.child("{self.IO.basename}.json") }
method package-list(@meta-uris = $.meta-uris) {
state @packages =
grep { .defined },
map { try from-json($_) },
map { try self.slurp-http($_) },
@meta-uris;
}
method update-local-package-list(@metas is copy = $.package-list) {
my $handle = (self.index-file.absolute ~ ".tmp." ~ now.Int).IO.open(:w);
LEAVE { try $handle.close; try $handle.unlink; }
$handle.print("[\n");
while @metas.shift -> $meta {
$handle.print(~to-json($meta));
$handle.print("\n,\n") if @metas.elems;
}
$handle.print("\n]");
$handle.close;
self.index-file.unlink;
sleep 1;
$handle.path.rename(self.index-file);
return self.index-file.slurp;
}
method update-remote-package-list($remote-uri) {
unless self.IO.parent.child('.git').e {
run $GIT_CMD, 'init', :cwd(self.IO.parent);
run $GIT_CMD, 'remote', 'add', 'origin', $remote-uri, :cwd(self.IO.parent);
}
try { so run $GIT_CMD, 'remote', 'set-url', 'origin', $remote-uri, :cwd(self.IO.parent) }
try { so run $GIT_CMD, 'pull', 'origin', 'master', :cwd(self.IO.parent) }
if so run $GIT_CMD, 'add', self.index-file.basename, :cwd(self.IO.parent) {
try { so run $GIT_CMD, 'commit', '-m', "'ecosystem update: {time}'", :cwd(self.IO.parent) }
try { so run $GIT_CMD, 'push', 'origin', 'master', :cwd(self.IO.parent) }
}
}
method slurp-http($uri) {
sleep 1;
say "Fetching $uri";
return powershell-webrequest($uri) // curl($uri) // wget($uri);
}
}