poulpy/spqlios/lib/scripts/prepare-release
spqlios/lib/scripts/prepare-releaseRaw
1#!/usr/bin/perl
2##
3## This script will help update manifest.yaml and Changelog.md before a release
4## Any merge to master that changes the version line in manifest.yaml
5## is considered as a new release.
6##
7## When ready to make a release, please run ./scripts/prepare-release
8## and commit push the final result!
9use File::Basename;
10use Cwd 'abs_path';
11
12# find its way to the root of git's repository
13my $scriptsdirname = dirname(abs_path(__FILE__));
14chdir "$scriptsdirname/..";
15print "✓ Entering directory:".`pwd`;
16
17# ensures that the current branch is ahead of origin/main
18my $diff= `git diff`;
19chop $diff;
20if ($diff =~ /./) {
21 die("ERROR: Please commit all the changes before calling the prepare-release script.");
22} else {
23 print("✓ All changes are comitted.\n");
24}
25system("git fetch origin");
26my $vcount = `git rev-list --left-right --count origin/main...HEAD`;
27$vcount =~ /^([0-9]+)[ \t]*([0-9]+)$/;
28if ($2>0) {
29 die("ERROR: the current HEAD is not ahead of origin/main\n. Please use git merge origin/main.");
30} else {
31 print("✓ Current HEAD is up to date with origin/main.\n");
32}
33
34mkdir ".changes";
35my $currentbranch = `git rev-parse --abbrev-ref HEAD`;
36chop $currentbranch;
37$currentbranch =~ s/[^a-zA-Z._-]+/-/g;
38my $changefile=".changes/$currentbranch.md";
39my $origmanifestfile=".changes/$currentbranch--manifest.yaml";
40my $origchangelogfile=".changes/$currentbranch--Changelog.md";
41
42my $exit_code=system("wget -O $origmanifestfile https://raw.githubusercontent.com/tfhe/spqlios-fft/main/manifest.yaml");
43if ($exit_code!=0 or ! -f $origmanifestfile) {
44 die("ERROR: failed to download manifest.yaml");
45}
46$exit_code=system("wget -O $origchangelogfile https://raw.githubusercontent.com/tfhe/spqlios-fft/main/Changelog.md");
47if ($exit_code!=0 or ! -f $origchangelogfile) {
48 die("ERROR: failed to download Changelog.md");
49}
50
51# read the current version (from origin/main manifest)
52my $vmajor = 0;
53my $vminor = 0;
54my $vpatch = 0;
55my $versionline = `grep '^version: ' $origmanifestfile | cut -d" " -f2`;
56chop $versionline;
57if (not $versionline =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)$/) {
58 die("ERROR: invalid version in manifest file: $versionline\n");
59} else {
60 $vmajor = int($1);
61 $vminor = int($2);
62 $vpatch = int($3);
63}
64print "Version in manifest file: $vmajor.$vminor.$vpatch\n";
65
66if (not -f $changefile) {
67 ## create a changes file
68 open F,">$changefile";
69 print F "# Changefile for branch $currentbranch\n\n";
70 print F "## Type of release (major,minor,patch)?\n\n";
71 print F "releasetype: patch\n\n";
72 print F "## What has changed (please edit)?\n\n";
73 print F "- This has changed.\n";
74 close F;
75}
76
77system("editor $changefile");
78
79# compute the new version
80my $nvmajor;
81my $nvminor;
82my $nvpatch;
83my $changelog;
84my $recordchangelog=0;
85open F,"$changefile";
86while ($line=<F>) {
87 chop $line;
88 if ($recordchangelog) {
89 ($line =~ /^$/) and next;
90 $changelog .= "$line\n";
91 next;
92 }
93 if ($line =~ /^releasetype *: *patch *$/) {
94 $nvmajor=$vmajor;
95 $nvminor=$vminor;
96 $nvpatch=$vpatch+1;
97 }
98 if ($line =~ /^releasetype *: *minor *$/) {
99 $nvmajor=$vmajor;
100 $nvminor=$vminor+1;
101 $nvpatch=0;
102 }
103 if ($line =~ /^releasetype *: *major *$/) {
104 $nvmajor=$vmajor+1;
105 $nvminor=0;
106 $nvpatch=0;
107 }
108 if ($line =~ /^## What has changed/) {
109 $recordchangelog=1;
110 }
111}
112close F;
113print "New version: $nvmajor.$nvminor.$nvpatch\n";
114print "Changes:\n$changelog";
115
116# updating manifest.yaml
117open F,"manifest.yaml";
118open G,">.changes/manifest.yaml";
119while ($line=<F>) {
120 if ($line =~ /^version *: */) {
121 print G "version: $nvmajor.$nvminor.$nvpatch\n";
122 next;
123 }
124 print G $line;
125}
126close F;
127close G;
128# updating Changelog.md
129open F,"$origchangelogfile";
130open G,">.changes/Changelog.md";
131print G <<EOF
132# Changelog
133
134All notable changes to this project will be documented in this file.
135this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
136
137EOF
138;
139print G "## [$nvmajor.$nvminor.$nvpatch] - ".`date '+%Y-%m-%d'`."\n";
140print G "$changelog\n";
141my $skip_section=1;
142while ($line=<F>) {
143 if ($line =~ /^## +\[([0-9]+)\.([0-9]+)\.([0-9]+)\] +/) {
144 if ($1>$nvmajor) {
145 die("ERROR: found larger version $1.$2.$3 in the Changelog.md\n");
146 } elsif ($1<$nvmajor) {
147 $skip_section=0;
148 } elsif ($2>$nvminor) {
149 die("ERROR: found larger version $1.$2.$3 in the Changelog.md\n");
150 } elsif ($2<$nvminor) {
151 $skip_section=0;
152 } elsif ($3>$nvpatch) {
153 die("ERROR: found larger version $1.$2.$3 in the Changelog.md\n");
154 } elsif ($2<$nvpatch) {
155 $skip_section=0;
156 } else {
157 $skip_section=1;
158 }
159 }
160 ($skip_section) and next;
161 print G $line;
162}
163close F;
164close G;
165
166print "-------------------------------------\n";
167print "THIS WILL BE UPDATED:\n";
168print "-------------------------------------\n";
169system("diff -u manifest.yaml .changes/manifest.yaml");
170system("diff -u Changelog.md .changes/Changelog.md");
171print "-------------------------------------\n";
172print "To proceed: press <enter> otherwise <CTRL+C>\n";
173my $bla;
174$bla=<STDIN>;
175system("cp -vf .changes/manifest.yaml manifest.yaml");
176system("cp -vf .changes/Changelog.md Changelog.md");
177system("git commit -a -m \"Update version and changelog.\"");
178system("git push");
179print("✓ Changes have been committed and pushed!\n");
180print("✓ A new release will be created when this branch is merged to main.\n");
181