This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfervour
executable file
·226 lines (195 loc) · 5.49 KB
/
fervour
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env perl
# Custom post generation script for internal use by authors of
# embedjournal.com
#
# Owner: EmbedJournal
# Author: Siddharth Chandrasekaran
# Email: [email protected]
# Date: 05 April 2017
#
use warnings;
use strict;
use Time::Piece;
use POSIX 'strftime';
use Getopt::Long;
use Pod::Usage 'pod2usage';
use YAML::XS qw(LoadFile DumpFile);
use File::Basename;
=head1 SYNOPSIS
Jekyll Fervour Management Utility.
fervour [OPTIONS]
=head1 OPTIONS
-b, --build Build site for specified ENV (--env) or for configured ENV
-u, --update Updated git repo to latest pushed changes
-c, --category CAT Create category CAT (experimental)
-e, --env ENV Specify (override) build environment. Can be (dev|prod).
-s, --ss-cert Setup self signed SSL certificate
-r, --run Run nginx container
-i, --init Initialize the repository. (run this only after a fresh clone)
-h, --help Print this help
=head1 VERSION
0.01
=cut
my $argc = @ARGV;
my %g_config;
GetOptions(
'build' => sub { build_site() },
'update' => sub { git_update() },
'ss-cert' => sub { create_self_signed_ssl_certificate() },
'init' => sub { init_depot() },
'help' => sub { pod2usage(1) },
'category=s' => \ my $category_name,
'env=s' => \ my $build_env,
'ss-cert' => \ my $do_create_cert,
'run' => \ my $run_server,
'help' => sub { pod2usage(1) },
);
pod2usage(1) if($argc == 0);
sub readInput {
my $inp;
while (1) {
print "$_[0]: ";
chomp($inp = <STDIN>);
return $inp if $inp;
}
}
sub getSlug
{
my $s = shift;
$s = lc $s;
$s =~ s/\s+/-/g;
$s =~ s/\//-/g;
$s =~ s/[^A-Za-z0-9\-\.]//g;
$s =~ s/-+/-/g;
return $s;
}
sub write_categories_yml
{
my $yml_object = shift;
my $file = shift;
open my $f, ">", $file // die "Unable to open file $file";
foreach (@{$yml_object}) {
# Wrap description at 100 chars and strip tailing newline.
$_->{description} =~ s/(.{0,100}(?:\s|$))/ $1\n/g;
$_->{description} =~ s/\s+$//;
# Write data to file.
print $f "-\n";
print $f " name: \"$_->{name}\"\n";
print $f " url: \"$_->{url}\"\n";
print $f " image: \"$_->{image}\"\n";
print $f " description: >\n";
print $f "$_->{description}\n";
}
close ($f);
}
sub load_config()
{
my ($line, $k, $v);
return undef unless (-f ".config");
open(my $f, "<", ".config");
while (<$f>) {
chomp; ++$line;
next if (/^$/);
next if (/^#.*/);
($k, $v) = /^\s?+(\S+)\s?+=\s?+(\S+)\s?+$/;
if (!defined($k) || !defined($v)) {
print "Invalid. In file .config at line $line: $_\n";
next;
}
# print("k:$k v:$v\n");
$g_config{$k}=$v;
}
}
sub create_category()
{
load_config();
my $tf = '/tmp/cat_' . get_rand_token() . '.yml';
open my $f, ">", $tf // die "cannot crete file $tf";
print $f "name: \"$category_name\"\n";
print $f "image: \"/assets/images/categories/default.jpg\"\n";
print $f "description: >\n";
print $f " TODO, add a short summary for your category. This can extend\n";
print $f " multiple lines (like this one) too.\n";
close($f);
system(vi => $tf);
print "\n";
system(cat => $tf);
exit if (readInput("\n\nDo you wish to proceed? [y/N]") !~ /y/i);
my $new_cat;
$new_cat = LoadFile("$tf");
$category_name = $new_cat->{name};
my $cat_slug = getSlug($category_name);
$new_cat->{url} = "/category/$cat_slug";
system(mkdir => "_posts/$cat_slug");
system(mkdir => "assets/posts/$cat_slug");
open my $cat_file, ">", "_category/$cat_slug.md";
print $cat_file "---\n";
print $cat_file "title: \"$category_name\"\n";
print $cat_file "paginate:\n";
print $cat_file " limit: false\n";
print $cat_file "category: \"Embedded\"\n";
print $cat_file "---\n";
close($cat_file);
# Append this new category to existing category list
my $yaml = LoadFile('_data/categories.yml');
push @{$yaml}, $new_cat;
write_categories_yml($yaml, '_data/categories.yml');
unlink($tf);
}
sub generate_scaled_images()
{
my @file_list;
my @post_file_list = `find content/images/ jekyll/assets/images/categories/ -type f -name 'post-thumb.*' | grep -v '/scaled/'`;
my @category_file_list = `find jekyll/assets/images/categories/ -maxdepth 1 -type f`;
@file_list = (@post_file_list, @category_file_list);
foreach (@file_list) {
chomp;
#print("Testing $_\n");
my ($name, $path) = fileparse($_);
next if (-f "$path/scaled/$name");
print "Generating scaled image at ${path}scaled/$name\n";
system("mkdir -p $path/scaled");
system("convert $path/$name -resize 80x80^ -gravity center -extent 80x80 $path/scaled/$name");
}
}
sub git_update()
{
system("git pull origin master");
system("git pull --recurse-submodules");
exit;
}
sub create_self_signed_ssl_certificate()
{
unless (-f "site/ssl/embedjournal-key.pem") {
printf "Creating self-signed SSL certificate for embedjournal.com\n";
system("mkdir -p site/ssl");
system("openssl req -x509 -newkey rsa:4096 -nodes"
." -subj '/CN=embedjournal.com'"
." -keyout site/ssl/embedjournal-key.pem"
." -out site/ssl/embedjournal-cert.pem"
." -days 365"
." > /dev/null 2>&1"
);
}
exit;
}
sub init_depot()
{
system("cp .env.sample .env");
generate_scaled_images();
}
sub build_site()
{
print "fervour: defering build to docker!\n";
system("docker-compose up embedjournal");
exit;
}
sub run_nginx()
{
print "fervour: starting server...\n";
system("docker-compose up -d server");
exit;
}
load_config();
run_nginx() if $run_server;
create_category() if (defined $category_name);