-
Notifications
You must be signed in to change notification settings - Fork 2
/
version-commit
executable file
·123 lines (98 loc) · 3.73 KB
/
version-commit
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
#!/usr/bin/env perl -w
#
# version-commit
# John Simpson <[email protected]> 2022-04-01
# Last updated 2023-07-03
#
# This script does two things:
#
# - Read a JSON structure from STDIN. This will be a list with two elements.
# Print the contents of the second element, without any changes.
# - Read 'theme/index-template.hbs' and use information about the current
# state of the git working directory to write the 'theme/index.hbs' file.
#
###############################################################################
#
# MIT License
#
# Copyright (c) 2022 John Simpson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
require 5.005 ;
use strict ;
use JSON ;
my $infile = 'theme/index-template.hbs' ;
my $outfile = 'theme/index.hbs' ;
###############################################################################
#
# Run an external command, return its output
sub runit($)
{
my $cmd = shift ;
my $rv = '' ;
open ( R , "$cmd 2>&1 |" )
or die "ERROR: runit('$cmd'): $!\n" ;
while ( my $line = <R> )
{
$rv .= $line ;
}
close R ;
return $rv ;
}
###############################################################################
###############################################################################
###############################################################################
#
# (1) Read STDIN, decode the JSON, print just the second element.
my $input = '' ;
{ local $/ = undef ; $input = <> ; }
my $j = decode_json( $input ) ;
print encode_json( $j->[1] ) ;
###############################################################################
#
# (2) Create the theme/index.hbs file.
########################################
# Build the values we'll need.
my $commit_hash = runit( 'git describe --always --tags --dirty' ) ;
$commit_hash =~ s|\s+$|| ;
my $commit_time = runit( 'TZ=UTC0 git log -1 --no-show-signature --date=iso-local --format=%cd' ) ;
$commit_time =~ s|\s+$|| ;
my @d = gmtime() ;
my $now = sprintf( '%04d-%02d-%02d %02d:%02d:%02d +0000' ,
$d[5]+1900 , $d[4]+1 , $d[3] , $d[2] , $d[1] , $d[0] ) ;
########################################
# Read 'theme/index-template.hbs' into memory.
my $html = '' ;
open( I , '<' , $infile )
or die "ERROR: cannot read '$infile': $!\n" ;
{ local $/ = undef ; $html = <I> ; }
close I ;
########################################
# Substitute the values into the HTML.
$html =~ s|\@VERSION_COMMIT_HASH\@|$commit_hash|gs ;
$html =~ s|\@VERSION_COMMIT_TIME\@|$commit_time|gs ;
$html =~ s|\@VERSION_COMMIT_NOW\@|$now|gs ;
########################################
# Write the output file.
open ( O , '>' , $outfile )
or die "ERROR: cannot write '$outfile': $!\n" ;
print O $html ;
close O ;