@@ -37,51 +37,28 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
37
37
),
38
38
);
39
39
40
- try {
41
- if (element is ! ClassElement ) {
42
- final friendlyName = element.displayName;
43
-
44
- throw InvalidGenerationSourceError (
45
- 'Generator cannot target `$friendlyName `.' ,
46
- todo: 'Remove the [Openapi] annotation from `$friendlyName `.' ,
47
- );
48
- } else {
49
- if (! (annotations.read ('useNextGen' ).literalValue as bool )) {
50
- if (annotations.read ('cachePath' ).literalValue != null ) {
51
- throw InvalidGenerationSourceError (
52
- 'useNextGen must be set when using cachePath' ,
53
- todo:
54
- 'Either set useNextGen: true on the annotation or remove the custom cachePath' ,
55
- );
56
- }
57
- }
40
+ if (element is ! ClassElement ) {
41
+ final friendlyName = element.displayName;
58
42
43
+ throw InvalidGenerationSourceError (
44
+ 'Generator cannot target `$friendlyName `.' ,
45
+ todo: 'Remove the [Openapi] annotation from `$friendlyName `.' ,
46
+ );
47
+ } else {
48
+ if (! (annotations.read ('useNextGen' ).literalValue as bool ) &&
49
+ annotations.read ('cachePath' ).literalValue != null ) {
50
+ throw AssertionError ('useNextGen must be set when using cachePath' );
51
+ }
52
+ try {
59
53
// Transform the annotations.
60
54
final args = GeneratorArguments (annotations: annotations);
61
-
62
55
// Determine if the project has a dependency on the flutter sdk or not.
63
56
final baseCommand = await checkPubspecAndWrapperForFlutterSupport (
64
57
wrapper: args.wrapper, providedPubspecPath: args.pubspecPath)
65
58
? 'flutter'
66
59
: 'dart' ;
67
60
68
- if (! args.useNextGen) {
69
- final path =
70
- '${args .outputDirectory }${Platform .pathSeparator }lib${Platform .pathSeparator }api.dart' ;
71
- if (await File (path).exists ()) {
72
- if (! args.alwaysRun) {
73
- logOutputMessage (
74
- log: log,
75
- communication: OutputMessage (
76
- message:
77
- 'Generated client already exists at [$path ] and configuration is annotated with alwaysRun: [${args .alwaysRun }]. Therefore, skipping this build. Note that the "alwaysRun" config will be removed in future versions.' ,
78
- level: Level .INFO ,
79
- ),
80
- );
81
- return '' ;
82
- }
83
- }
84
- } else {
61
+ if (args.useNextGen) {
85
62
// If the flag to use the next generation of the generator is applied
86
63
// use the new functionality.
87
64
return generatorV2 (
@@ -90,26 +67,42 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
90
67
annotatedPath: buildStep.inputId.path);
91
68
}
92
69
70
+ final path =
71
+ '${args .outputDirectory }${Platform .pathSeparator }lib${Platform .pathSeparator }api.dart' ;
72
+ if (await File (path).exists ()) {
73
+ if (! args.alwaysRun) {
74
+ logOutputMessage (
75
+ log: log,
76
+ communication: OutputMessage (
77
+ message:
78
+ 'Generated client already exists at [$path ] and configuration is annotated with alwaysRun: [${args .alwaysRun }]. Therefore, skipping this build. Note that the "alwaysRun" config will be removed in future versions.' ,
79
+ level: Level .INFO ,
80
+ ),
81
+ );
82
+ return '' ;
83
+ }
84
+ }
85
+
93
86
await runOpenApiJar (arguments: args);
94
87
await fetchDependencies (baseCommand: baseCommand, args: args);
95
88
await generateSources (baseCommand: baseCommand, args: args);
96
- }
97
- } catch (e, st) {
98
- late OutputMessage communication;
99
- if (e is ! OutputMessage ) {
100
- communication = OutputMessage (
101
- message: '- There was an error generating the spec.' ,
102
- level: Level .SEVERE ,
103
- additionalContext: e,
104
- stackTrace: st,
105
- );
106
- } else {
107
- communication = e;
108
- }
89
+ } catch (e, st) {
90
+ late OutputMessage communication;
91
+ if (e is ! OutputMessage ) {
92
+ communication = OutputMessage (
93
+ message: '- There was an error generating the spec.' ,
94
+ level: Level .SEVERE ,
95
+ additionalContext: e,
96
+ stackTrace: st,
97
+ );
98
+ } else {
99
+ communication = e;
100
+ }
109
101
110
- logOutputMessage (log: log, communication: communication);
102
+ logOutputMessage (log: log, communication: communication);
103
+ }
104
+ return '' ;
111
105
}
112
- return '' ;
113
106
}
114
107
115
108
/// Runs the OpenAPI compiler with the given [args] .
@@ -130,23 +123,18 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
130
123
// Include java environment variables in openApiCliCommand
131
124
var javaOpts = Platform .environment['JAVA_OPTS' ] ?? '' ;
132
125
133
- ProcessResult result;
134
- if (! testMode) {
135
- result = await Process .run (
136
- 'java' ,
137
- [
126
+ final result = await runExternalProcess (
127
+ command: Command (
128
+ executable: 'java' ,
129
+ arguments: [
138
130
if (javaOpts.isNotEmpty) javaOpts,
139
131
'-jar' ,
140
132
binPath,
141
133
...args,
142
134
],
143
- workingDirectory: Directory .current.path,
144
- runInShell: Platform .isWindows,
145
- );
146
- } else {
147
- result = ProcessResult (999999 , 0 , null , null );
148
- }
149
-
135
+ ),
136
+ workingDirectory: Directory .current.path,
137
+ );
150
138
if (result.exitCode != 0 ) {
151
139
return Future .error (
152
140
OutputMessage (
@@ -359,17 +347,8 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
359
347
),
360
348
);
361
349
362
- ProcessResult results;
363
- if (! testMode) {
364
- results = await Process .run (
365
- command.executable,
366
- command.arguments,
367
- runInShell: Platform .isWindows,
368
- workingDirectory: args.outputDirectory,
369
- );
370
- } else {
371
- results = ProcessResult (99999 , 0 , null , null );
372
- }
350
+ final results = await runExternalProcess (
351
+ command: command, workingDirectory: args.outputDirectory);
373
352
374
353
if (results.exitCode != 0 ) {
375
354
return Future .error (
@@ -415,18 +394,8 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
415
394
),
416
395
);
417
396
418
- ProcessResult results;
419
- if (! testMode) {
420
- results = await Process .run (
421
- command.executable,
422
- command.arguments,
423
- runInShell: Platform .isWindows,
424
- workingDirectory: args.outputDirectory,
425
- );
426
- } else {
427
- results = ProcessResult (999999 , 0 , null , null );
428
- }
429
-
397
+ final results = await runExternalProcess (
398
+ command: command, workingDirectory: args.outputDirectory);
430
399
if (results.exitCode != 0 ) {
431
400
return Future .error (
432
401
OutputMessage (
@@ -499,17 +468,8 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
499
468
/// Format the generated code in the output directory.
500
469
Future <void > formatCode ({required GeneratorArguments args}) async {
501
470
final command = Command (executable: 'dart' , arguments: ['format' , './' ]);
502
- ProcessResult result;
503
- if (! testMode) {
504
- result = await Process .run (
505
- command.executable,
506
- command.arguments,
507
- workingDirectory: args.outputDirectory,
508
- runInShell: Platform .isWindows,
509
- );
510
- } else {
511
- result = ProcessResult (99999 , 0 , null , null );
512
- }
471
+ final result = await runExternalProcess (
472
+ command: command, workingDirectory: args.outputDirectory);
513
473
514
474
if (result.exitCode != 0 ) {
515
475
return Future .error (
@@ -522,9 +482,20 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
522
482
);
523
483
} else {
524
484
logOutputMessage (
525
- log: log,
526
- communication:
527
- OutputMessage (message: 'Successfully formatted code.' ));
485
+ log: log,
486
+ communication: OutputMessage (
487
+ message: 'Successfully formatted code.' ,
488
+ ),
489
+ );
528
490
}
529
491
}
492
+
493
+ Future <ProcessResult > runExternalProcess (
494
+ {required Command command, required String workingDirectory}) =>
495
+ Process .run (
496
+ command.executable,
497
+ command.arguments,
498
+ workingDirectory: workingDirectory,
499
+ runInShell: Platform .isWindows,
500
+ );
530
501
}
0 commit comments