1
+ # !/usr/bin/env perl
2
+
3
+ use v5.20;
4
+ use strict;
5
+ use warnings;
6
+ use File::Basename;
7
+ use File::Spec;
8
+ use Cwd qw( abs_path) ;
9
+ use Getopt::Long;
10
+
11
+ # Get project root directory (one level up from bin/)
12
+ my $script_dir = dirname(abs_path($0 ));
13
+ my $project_root = dirname($script_dir );
14
+
15
+ # Parse command line arguments
16
+ my $capi_version ;
17
+ my $help ;
18
+ my $verbose ;
19
+
20
+ GetOptions(
21
+ ' version=s' => \$capi_version ,
22
+ ' verbose' => \$verbose ,
23
+ ' help' => \$help ,
24
+ ) or die usage();
25
+
26
+ # Show usage if help requested or missing required arguments
27
+ if ($help || !$capi_version ) {
28
+ print usage();
29
+ exit ($help ? 0 : 1);
30
+ }
31
+
32
+ # Validate inputs
33
+ validate_inputs();
34
+
35
+ # Validate the OpenAPI specification
36
+ validate_spec();
37
+
38
+ sub usage {
39
+ return <<EOF ;
40
+ Usage: $0 --version=VERSION [--verbose]
41
+
42
+ Validate CAPI OpenAPI specification
43
+
44
+ Required options:
45
+ --version=VERSION CAPI version to validate (e.g., 3.195.0)
46
+
47
+ Optional options:
48
+ --verbose Show detailed validation output
49
+ --help Show this help message
50
+
51
+ Examples:
52
+ $0 --version=3.195.0
53
+ $0 --version=3.181.0 --verbose
54
+ EOF
55
+ }
56
+
57
+ sub validate_inputs {
58
+ # Check if spec file exists
59
+ my $spec_file = File::Spec-> catfile($project_root , ' capi' , " ${capi_version} .openapi.yml" );
60
+ unless (-f $spec_file ) {
61
+ die " Error: OpenAPI spec file not found: $spec_file \n " .
62
+ " Please run 'make gen-openapi-spec' first to generate the specification.\n " ;
63
+ }
64
+
65
+ # Check if openapi-generator is available
66
+ my $generator_check = ` which openapi-generator 2>/dev/null || which openapi-generator-cli 2>/dev/null` ;
67
+ chomp $generator_check ;
68
+ unless ($generator_check ) {
69
+ die " Error: openapi-generator-cli not found.\n " .
70
+ " Please run 'make deps' to install dependencies.\n " ;
71
+ }
72
+ }
73
+
74
+ sub validate_spec {
75
+ my $spec_file = File::Spec-> catfile($project_root , ' capi' , " ${capi_version} .openapi.yml" );
76
+
77
+ # Determine which command to use (openapi-generator or openapi-generator-cli)
78
+ my $generator_cmd = ` which openapi-generator 2>/dev/null` ;
79
+ chomp $generator_cmd ;
80
+ $generator_cmd = ' openapi-generator-cli' unless $generator_cmd ;
81
+
82
+ # Build the validation command
83
+ my $cmd = " $generator_cmd validate -i '$spec_file '" ;
84
+
85
+ # Execute the validation
86
+ say " Validating CAPI $capi_version OpenAPI specification..." ;
87
+ say " Spec file: $spec_file " if $verbose ;
88
+ say " Command: $cmd " if $verbose ;
89
+
90
+ my $result = system ($cmd );
91
+
92
+ if ($result == 0) {
93
+ say " \n ✓ Validation passed!" ;
94
+ say " The OpenAPI specification is valid." ;
95
+
96
+ # Check if there were warnings in the output
97
+ my $output = ` $cmd 2>&1` ;
98
+ if ($output =~ / Warning/ ) {
99
+ say " \n ⚠ Note: There are some warnings (usually about unused models)." ;
100
+ say " These are typically not critical issues." ;
101
+ }
102
+
103
+ # Also check JSON version if verbose
104
+ check_json_spec() if $verbose ;
105
+
106
+ } else {
107
+ say " \n ✗ Validation failed!" ;
108
+ say " Please check the errors above and fix the specification." ;
109
+ exit (1);
110
+ }
111
+ }
112
+
113
+ sub check_json_spec {
114
+ my $json_file = File::Spec-> catfile($project_root , ' capi' , " ${capi_version} .openapi.json" );
115
+
116
+ if (-f $json_file ) {
117
+ say " \n Also validating JSON format..." ;
118
+
119
+ # Determine which command to use
120
+ my $generator_cmd = ` which openapi-generator 2>/dev/null` ;
121
+ chomp $generator_cmd ;
122
+ $generator_cmd = ' openapi-generator-cli' unless $generator_cmd ;
123
+
124
+ my $json_cmd = " $generator_cmd validate -i '$json_file '" ;
125
+ say " Command: $json_cmd " if $verbose ;
126
+
127
+ my $result = system ($json_cmd );
128
+
129
+ if ($result == 0) {
130
+ say " ✓ JSON specification is valid." ;
131
+ } else {
132
+ say " ✗ JSON specification validation failed." ;
133
+ }
134
+ }
135
+ }
0 commit comments