-
Notifications
You must be signed in to change notification settings - Fork 3
/
publish_edition.sh
executable file
·180 lines (124 loc) · 4.53 KB
/
publish_edition.sh
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
#
# This script should be used from the root directory of the
# git.github.io repository. It will publish an edition using an
# existing draft (this will create a commit) and create a new empty
# draft for the following edition (this will create another commit).
#
# This script can be run like this:
#
# $ ../getreleases/publish_edition.sh [<next date> [<cur date>]]
#
# where <next date> is the publication date for the following edition
# and <cur date> is the publication date for the current edition.
#
# If <cur date> is not provided, the publication date for the current
# edition made from the current draft is today.
#
# If <next date> is not provided to the script, it will compute it by
# adding one month to today's date and then finding the following
# wednesday.
#
# Repo related constants
repo_url="https://github.com/git/git.github.io.git"
known_good_commit="5bc243932ea7938830757e8370df6bd86df39cab"
src_dir="rev_news/drafts"
dst_dir="_posts"
main_branch="master"
# Useful infos from environments
today=$(date "+%F")
basedir=$(dirname "$0")
# Helper functions
die() {
printf >&2 "FATAL: %s\n" "$@"
exit 1
}
add_order_suffix() {
perl -e '
my $nb = $ARGV[0];
if ($nb =~ m/^(.*[02-9\D])?1$/) {
print $nb . "st\n";
} elsif ($nb =~ m/^(.*[02-9\D])?2$/) {
print $nb . "nd\n";
} elsif ($nb =~ m/^(.*[02-9\D])?3$/) {
print $nb . "rd\n";
} else {
print $nb . "th\n";
}
' "$1"
}
# Process arguments
arg_next="$1"
arg_cur="$2"
test "$#" -le 2 ||
die "too many arguments" "Usage: $0 [<next date> [<cur date>]]"
# Compute nextdate, the publication date for the next edition
if test -n "$arg_next"
then
nextdate=$(date "+%F" --date="$arg_next") ||
die "failed to understand '$arg_next' as a date"
else
nextdate=$(date "+%F" --date="$today + 1 month")
day_of_week=$(date "+%u" --date="$nextdate")
test "$day_of_week" -lt 3 &&
nextdate=$(date "+%F" --date="$nextdate + $(( 3 - $day_of_week )) days")
test "$day_of_week" -gt 3 &&
nextdate=$(date "+%F" --date="$nextdate + $(( 10 - $day_of_week )) days")
fi
echo "Next publication date is: $(LANG=C date "+%A %B %d, %Y" --date="$nextdate")"
# Compute curdate, the publication date for the current edition
if test -n "$arg_cur"
then
curdate=$(date "+%F" --date="$arg_cur") ||
die "failed to understand '$arg_cur' as a date"
else
curdate="$today"
fi
echo "Current publication date is: $(LANG=C date "+%A %B %d, %Y" --date="$curdate")"
# Basic checks
type git >/dev/null || die "git not found" "we need git"
git show "$known_good_commit" >/dev/null 2>&1 ||
die "$known_good_commit not found" \
"we need to be in a repo cloned from $repo_url"
cur_branch=$(git rev-parse --abbrev-ref HEAD)
test "$cur_branch" = "$main_branch" || die "please switch to the '$main_branch' branch"
test -d "$src_dir" || die "no source '$src_dir' directory"
test -d "$dst_dir" || die "no destination '$dst_dir' directory"
nb_ed=$(ls "$src_dir"/edition-*.md | wc -l)
test "$nb_ed" -eq 0 && die "no 'edition-*.md' file in '$src_dir' directory"
test "$nb_ed" -gt 1 && die "more than one 'edition-*.md' file in '$src_dir' directory"
# Find edition info we need
edition=$(ls "$src_dir"/edition-*.md)
cur=$(expr "$edition" : "rev_news/drafts/edition-\([0-9]\+\).md")
test -n "$cur" || die "'$edition' should contain a number"
next=$(expr "$cur" + 1)
# Find dates we need
cur_month=$(LANG=C date "+%B %Y" --date="$curdate")
# prev_month=$(LANG=C date "+%B %Y" --date="$curdate - 31 days") # doesn't work well with '- 1 month'
next_month=$(LANG=C date "+%B %Y" --date="$curdate + 15 days")
f_day=$(LANG=C date "+%-d" --date="$nextdate")
f_month=$(LANG=C date "+%B" --date="$nextdate")
f_year=$(LANG=C date "+%Y" --date="$nextdate")
full_date="$f_month $(add_order_suffix $f_day), $f_year"
# Publish current draft
git mv "$src_dir"/edition-$cur.md "$dst_dir"/$curdate-edition-$cur.markdown
git commit -m "Publish rn-$cur in $dst_dir/"
# Create a draft for next edition
next_ed="$src_dir/edition-$next.md"
edition_template="$basedir/templates/edition-XXX.md"
test -f "$edition_template" ||
die "failed to find edition template at '$edition_template'"
cp "$edition_template" "$next_ed" ||
die "failed to 'cp $edition_template $next_ed'"
next_ord=$(add_order_suffix "$next")
perl -pi -e "
s/Edition _ED_NUM_/Edition $next/g;
s/_ED_ORD_ edition/${next_ord} edition/g;
s/_ED_DATE_/$nextdate/g;
s/_ED_FULL_DATE_/$full_date/g;
s/_ED_CUR_MONTH_YEAR_/$cur_month/g;
s/_ED_NEXT_MONTH_YEAR_/$next_month/g;
" "$next_ed"
git add "$next_ed" ||
die "failed to 'git add $next_ed'"
git commit -m "Add draft for rn-$next"