Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2016 KohaSuomi |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Getopt::Long qw(:config no_ignore_case); |
23 |
|
24 |
my $help = 0; |
25 |
my $verbose = 0; |
26 |
my $swaggerFile = "swagger.json"; |
27 |
my $swaggerMinifiedFile = "swagger.min.json"; |
28 |
|
29 |
GetOptions( |
30 |
'h|help' => \$help, |
31 |
'v|verbose:i' => \$verbose, |
32 |
's|source:s' => \$swaggerFile, |
33 |
'd|destination:s' => \$swaggerMinifiedFile, |
34 |
); |
35 |
|
36 |
my $usage = <<USAGE; |
37 |
|
38 |
minifySwagger.pl |
39 |
|
40 |
Reads the swagger.json -file and all referenced JSON-Schema -files and merges |
41 |
them into one merged and minified version. This minified swagger-file is |
42 |
intended to be shared to our API consumers. |
43 |
|
44 |
By default you must run this script from the same directory as the 'swagger.json' |
45 |
and the minified specification is written to 'swagger.min.json'. |
46 |
This is a convenience to make the minification process as easy as possible. |
47 |
|
48 |
!DO NOT! set the executable flag on, or this can be ran from the internetz. |
49 |
|
50 |
-h --help This happy helpful help! |
51 |
|
52 |
-v --verbose 0, default, no output. |
53 |
1, turn on internal Swagger debugging flags |
54 |
2, more verbose Swagger2 debugging |
55 |
|
56 |
-s --source Which Swagger2-specification file to minify? |
57 |
Defaults to "swagger.json" |
58 |
|
59 |
-d --destination Where to write the minified Swagger2-spec? |
60 |
Defaults to "swagger.min.json" |
61 |
|
62 |
EXAMPLES: |
63 |
|
64 |
perl minifySwagger.pl -v 1 -s api/v1/swagger/swagger.json -d swag.json |
65 |
cd api/v1/swagger && perl minifySwagger.pl |
66 |
|
67 |
USAGE |
68 |
|
69 |
if ($help) { |
70 |
print $usage; |
71 |
exit 0; |
72 |
} |
73 |
if ($verbose > 0) { |
74 |
$ENV{SWAGGER2_DEBUG} = $verbose; |
75 |
} |
76 |
|
77 |
|
78 |
require Swagger2; #When you import the Swagger2-libraries, the environment variables are checked and cannot be altered anymore. So set verbosity first, then load libs. |
79 |
my $swagger = Swagger2->new($swaggerFile); |
80 |
$swagger = $swagger->expand; #Fetch all JSON-Schema references |
81 |
|
82 |
my @errors = $swagger->validate; |
83 |
print join("\n", "Swagger2: Invalid spec:", @errors)."\n" if @errors; |
84 |
exit 1 if @errors; |
85 |
|
86 |
removeNonSwagger2Values($swagger); |
87 |
|
88 |
open(SWOUT, ">:encoding(UTF-8)", $swaggerMinifiedFile) or die "$0: Couldn't open the minified Swagger2 output file '$swaggerMinifiedFile':\n $!"; |
89 |
print SWOUT $swagger->to_string(); |
90 |
close(SWOUT); |
91 |
|
92 |
##For some reason stringifying the Swagger2-spec adds non-valid parameters, like "/id" |
93 |
sub removeNonSwagger2Values { |
94 |
my ($swagger) = @_; |
95 |
|
96 |
my $data = $swagger->api_spec->data; |
97 |
delete($data->{id}); |
98 |
} |