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 |
my $validate = 0; |
29 |
|
30 |
GetOptions( |
31 |
'h|help' => \$help, |
32 |
'v|verbose:i' => \$verbose, |
33 |
's|source:s' => \$swaggerFile, |
34 |
'd|destination:s' => \$swaggerMinifiedFile, |
35 |
'V|validate' => \$validate, |
36 |
); |
37 |
|
38 |
my $usage = <<USAGE; |
39 |
|
40 |
minifySwagger.pl |
41 |
|
42 |
Reads the swagger.json -file and all referenced JSON-Schema -files and merges |
43 |
them into one merged and minified version. This minified swagger-file is |
44 |
intended to be shared to our API consumers. |
45 |
|
46 |
By default you must run this script from the same directory as the 'swagger.json' |
47 |
and the minified specification is written to 'swagger.min.json'. |
48 |
This is a convenience to make the minification process as easy as possible. |
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 |
-V --validate Instead of minimizing, prints errors to stdout. |
63 |
|
64 |
EXAMPLES: |
65 |
|
66 |
perl minifySwagger.pl -v 1 -s api/v1/swagger/swagger.json -d swag.json |
67 |
cd api/v1/swagger && perl minifySwagger.pl |
68 |
|
69 |
USAGE |
70 |
|
71 |
if ($help) { |
72 |
print $usage; |
73 |
exit 0; |
74 |
} |
75 |
if ($verbose > 0) { |
76 |
$ENV{SWAGGER2_DEBUG} = $verbose; |
77 |
} |
78 |
|
79 |
|
80 |
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. |
81 |
my $swagger = Swagger2->new($swaggerFile); |
82 |
$swagger = $swagger->expand; #Fetch all JSON-Schema references |
83 |
|
84 |
open(SWOUT, ">:encoding(UTF-8)", $swaggerMinifiedFile) or die "$0: Couldn't open the minified Swagger2 output file:\n $!"; |
85 |
print SWOUT $swagger->to_string() if not($validate); |
86 |
close(SWOUT); |