Skip to content

Instantly share code, notes, and snippets.

@samcv
Created April 26, 2017 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samcv/3448cce3b0229561f1a76ddbace67a8b to your computer and use it in GitHub Desktop.
Save samcv/3448cce3b0229561f1a76ddbace67a8b to your computer and use it in GitHub Desktop.
use JSON::Fast;
use lib 'lib';
use github;
sub add-to-meta (Str:D :$license!, IO::Path:D :$file!) {
die unless $license;
my Str:D $json = $file.IO.slurp;
return if from-json($json)<license>;
$json = get-new-meta($json, $license);
from-json($json)<license> or die;
$file.IO.spurt($json);
}
sub get-new-meta (Str:D $meta-text, Str:D $license) {
my @newlines;
my @lines = $meta-text.lines;
for @lines -> $line {
if $line ~~ /^ (\s*) '"name"' \s* ':' (\s*)/ {
my $index = $line.index(':');
@newlines.push: $line;
my $string = $0"license";
if $string.chars < $index {
$string ~= ' ' x ($index - $string.chars);
}
else {
$string ~= ' ';
}
$string ~= ':';
$string ~= $1"$license",;
@newlines.push: $string;
}
else {
@newlines.push: $line;
}
}
@newlines.join("\n") ~ "\n";
}
sub MAIN (Str:D $file = "list2.txt", Bool :$no-pull = False) {
my $OWD = $*CWD;
my @projs;
my @finished;
for $file.IO.slurp.lines {
@projs.push(~$<slug> => ~$<license>) if m:s/$<slug>=(\S+) $<license>=(\S+)/;
}
my $dir = 'pbof'.IO;
$dir.mkdir unless $dir.d;
chdir 'pbof';
my $MDIR = $*CWD;
for @projs -> $pair {
chdir $MDIR;
chdir(clone-slug $pair.key);
my @metas = dir.grep(/:i 'meta.info'|'meta6.json'/);
if @metas != 1 {
note "Too many/few meta files in $pair.key() {@metas.join(' ')}";
next;
}
add-to-meta :license($pair.value), :file(@metas[0]);
note "Creating branch";
run 'git', 'branch', '-v', 'SPDX-license', :out;
note "Checking out branch";
run 'git', 'checkout', 'SPDX-license', :out;
note "Adding @metas[0] to git";
run 'git', 'add', @metas[0], :out;
my $meta = @metas[0];
my $commit-text = qq:to/END/;
Use SPDX identifier in license field of $meta
Use the standardized identifier for the license field.
For more details see https://design.perl6.org/S22.html#license
END
spurt "commit.txt", $commit-text;
note "Commiting";
qqx{git commit -v --file commit.txt};
note "Forking";
qx{hub fork};
note "pushing";
qx{git push --force samcv SPDX-license}; #, :out;
if $no-pull {
say $pair.key;
@finished.push($pair.key => 'FORCEPUSH');
}
else {
my $url = qx{hub pull-request -F commit.txt}.chomp;
say $pair.key, " ", $url;
@finished.push($pair.key => $url);
}
"hub-PR.json".IO.spurt(to-json(@finished));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment