-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.pl
executable file
·157 lines (140 loc) · 5.28 KB
/
app.pl
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
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::JSON qw(decode_json encode_json);
use Mojo::Util qw(dumper);
use FindBin qw($Bin);
use File::Spec::Functions;
# Configuration
my $config = plugin 'JSONConfig';
app->secrets( [ $config->{'app_secret'} ] );
plugin Minion => { File => $config->{'minion_db'} };
my $repo = $config->{'repo'};
my $repo_url = $config->{'repo_url'};
# Startup/Bootstrap
unless ( -d $repo ) {
my $git_clone = `git clone $repo_url`;
}
# Job queue task
app->minion->add_task(
convert_doc => sub {
my $job = shift;
my $doc = shift;
$job->app->log->info( "Converting $doc" );
# Get the latest files from the Github repo
my $git_pull = `cd $repo && git pull`;
$job->app->log->info( "Git pull results: $git_pull" );
# Determine which formats need to be output (.md, .docx, .odt)
my %format_alts = (
md => [ 'docx', 'odt' ],
docx => [ 'odt', 'md' ],
odt => [ 'md', 'docx' ]
);
# Dictionary for the extentions
my %formats = (
md => 'markdown',
docx => 'docx',
odt => 'odt'
);
# Dictionary for the repo directories
my %format_dirs = (
md => 'markdown',
docx => 'msword',
odt => 'openoffice'
);
my ( $volume, $directories, $file ) = File::Spec->splitpath( $doc );
my @dirs = File::Spec->splitdir( $directories );
#### TODO replace this with something more sensible:
my ( $ext ) = $file =~ /\.(\w+)$/;
my ( $file_name ) = $file =~ /(.*)\./;
####
#if ( $ext eq 'odt' ) {
#$job->app->log->info( "No support for odt yet..." );
#return;
#}
unless ( $formats{$ext} ) {
$job->app->log->info( "We don't support $ext just yet..." );
return;
}
unless ( $directories =~ /$format_dirs{ $ext }/ ) {
$job->app->log->info(
"The file doesn't appear to be in an expected directory..." );
return;
}
my $alts = $format_alts{$ext};
# Output the modified file in the alternate formats
for my $to_create ( @$alts ) {
$job->app->log->info( "Going to create: $to_create" );
# Do I have an adequate directory to put this file in?
my $target_dir = File::Spec->catdir( $repo, $dirs[0],
$format_dirs{$to_create} );
unless ( -d $target_dir ) {
$job->app->log->info( "No target directory, creating..." );
mkdir $target_dir;
}
my $cmd;
if ( $ext eq 'odt' ) { # Use odt2html rubygem for odt files
$job->app->log->info( "Making HTML from odt for $doc" );
my $output_html
= `odt2html --in $repo/$doc --out $target_dir/$file_name.html`;
$job->app->log->info( "HTML output is: $output_html" )
if $output_html;
$cmd
= "pandoc -f html -t $formats{ $to_create } $target_dir/$file_name.html -o $target_dir/$file_name.$to_create";
my $output = `$cmd`;
$job->app->log->info( $cmd );
my $output_delete = `rm $target_dir/$file_name.html`;
$job->app->log->info( $output_delete ) if $output_delete;
}
else { # Otherwise, just use pandoc
$cmd
= "pandoc -f $formats{ $ext } -t $formats{ $to_create } $repo/$directories$file -o $target_dir/$file_name.$to_create";
my $output = `$cmd`;
$job->app->log->info( $cmd );
}
}
# Git commit the changes
my $git_commit
= `cd $repo && git add . && git commit -a -m 'Doc bot: updating alternate versions of $file'`;
# Git push the changes back to the repo
my $git_push = `cd $repo && git push`;
}
);
post '/github' => sub {
my $c = shift;
my $body = $c->req->body;
my $payload = decode_json $body;
my $commits = $payload->{'commits'};
my $jobs = 0;
for my $commit ( @$commits ) {
# If this is a doc bot commit, skip it
if ( $commit->{'message'} =~ /Doc bot:/g ) {
$c->app->log->info( "Skipping because this is a doc bot commit" );
next;
}
# If files are added or changed, add them to the queue
if ( $commit->{'modified'} || $commit->{'added'} ) {
my @changes = @{ $commit->{'modified'} };
push( @changes, @{ $commit->{'added'} } );
for my $mod ( @changes ) {
$c->app->log->info( "Adding $mod to job queue" );
$jobs++;
$c->minion->enqueue( 'convert_doc', [$mod] );
}
return $c->render(
json => {
jobs => $jobs,
message => "Added $jobs jobs to the queue",
version => "Version: $config->{'app_version'}"
},
status => 200
);
}
}
if ( $jobs == 0 ) {
return $c->render(
json => { message => "No new or changed files" },
status => 422
);
}
};
app->start;