@@ -108,20 +108,31 @@ class ComponentGeneratorService
108108
109109 if (createSourceDeclarationResult == FileOperationResult .alreadyExists) {
110110 final codeLines = < String > [];
111- final newImports = declarationBody.split ('\n ' ).where (
112- (line) => line.startsWith ("import 'package:" ),
113- );
111+
112+ final declarationBodyLines = declarationBody.split ('\n ' );
114113
115114 final existingContent = File (declarationFilePath).readAsStringSync ();
116115
117- final newRequests = sourceComponent.requests.where (
118- (request) => ! existingContent.contains (request.operationId.camelCase),
119- );
116+ final newRequests = < RequestComponent > [];
117+
118+ for (final request in sourceComponent.requests) {
119+ if (! existingContent.contains (request.operationId.camelCase)) {
120+ newRequests.add (request);
121+ }
122+ }
120123
121124 if (newRequests.isEmpty) {
122125 return ;
123126 }
124127
128+ final newImports = < String > [];
129+
130+ for (final line in declarationBodyLines) {
131+ if (line.trim ().startsWith ("import 'package:" )) {
132+ newImports.add (line);
133+ }
134+ }
135+
125136 for (final request in newRequests) {
126137 codeLines.add (
127138 request.getRequestDeclaration (),
@@ -153,22 +164,30 @@ class ComponentGeneratorService
153164
154165 final existingContent = File (implementationFilePath).readAsStringSync ();
155166
156- final newImports = implementationBody.split ('\n ' ).where (
157- (line) => line.startsWith ("import 'package:" ),
158- );
167+ final newRequests = < RequestComponent > [];
159168
160- final newRequests = sourceComponent.requests.where (
161- (request) => ! existingContent.contains (request.operationId.camelCase),
162- );
163-
164- final newEndpoints = sourceComponent.requests.map (
165- (request) => request.getVariableDeclaration (),
166- );
169+ for (final request in sourceComponent.requests) {
170+ if (! existingContent.contains (request.operationId.camelCase)) {
171+ newRequests.add (request);
172+ }
173+ }
167174
168175 if (newRequests.isEmpty) {
169176 return ;
170177 }
171178
179+ final newImports = < String > [];
180+
181+ for (final line in implementationBody.split ('\n ' )) {
182+ if (line.trim ().startsWith ("import 'package:" )) {
183+ newImports.add (line);
184+ }
185+ }
186+
187+ final newEndpoints = sourceComponent.requests.map (
188+ (request) => request.getVariableDeclaration (),
189+ );
190+
172191 for (final request in newRequests) {
173192 codeLines.add (
174193 request.getRequestBody (),
@@ -638,7 +657,17 @@ class ComponentGeneratorService
638657 final file = File (filePath);
639658
640659 try {
641- final existingContent = await file.readAsLines ();
660+ final existingContent = await file.readAsString ()
661+ ..trim ();
662+
663+ final rawContentLines = existingContent.split (';' ).toList ();
664+
665+ final existingContentLines = rawContentLines
666+ .map (
667+ (line) =>
668+ '${line .trim ()}${line == rawContentLines .last ? '' : ';' }' ,
669+ )
670+ .toList ();
642671
643672 //Conditions for identifying lines
644673 bool importCondition (String line) => line.startsWith ("import 'package:" );
@@ -651,40 +680,43 @@ class ComponentGeneratorService
651680 line.contains (r'/$' ) &&
652681 line.contains ('=>' );
653682
654- final firstEndpointIndex = existingContent.indexWhere (endpointCondition);
655-
656- final afterClassIndex = existingContent.indexWhere (
657- (line) =>
658- line.trim ().startsWith ('class' ) &&
659- line.contains ('implements' ) &&
660- line.contains ('{' ),
661- ) +
662- 1 ;
663-
664683 //Combine imports
665684 if (newImports.isNotEmpty) {
666- final existingImports = existingContent.where (importCondition).toSet ()
685+ final existingImports = existingContentLines
686+ .where (importCondition)
687+ .toSet ()
667688 ..addAll (newImports);
668689
669- existingContent
690+ existingContentLines
670691 ..removeWhere (importCondition)
671692 ..insert (0 , existingImports.toSet ().join ('\n ' ));
672693 }
673694
695+ final firstEndpointIndex =
696+ existingContentLines.indexWhere (endpointCondition);
697+
698+ final afterClassIndex = existingContentLines.indexWhere (
699+ (line) =>
700+ line.trim ().startsWith ('class' ) &&
701+ line.contains ('implements' ) &&
702+ line.contains ('{' ),
703+ ) +
704+ 1 ;
705+
674706 //Combine endpoints
675707 if (newEndpoints.isNotEmpty) {
676- final existingEndpoints = existingContent
708+ final existingEndpoints = existingContentLines
677709 .where (endpointCondition)
678710 .map ((line) => line.trim ())
679711 .toList ()
680712 ..addAll (
681- existingContent
713+ existingContentLines
682714 .where (pathEndpointCondition)
683715 .map ((line) => line.trim ()),
684716 )
685717 ..addAll (newEndpoints);
686718
687- existingContent
719+ existingContentLines
688720 ..removeWhere (endpointCondition)
689721 ..removeWhere (pathEndpointCondition)
690722 ..insert (
@@ -699,23 +731,27 @@ class ComponentGeneratorService
699731 //Combine mappers
700732 if (newMappers.isNotEmpty) {
701733 bool condition (String line) => line.endsWith ('Mappers();' );
702- final mapperIndex = existingContent .indexWhere (condition);
734+ final mapperIndex = existingContentLines .indexWhere (condition);
703735
704- final existingMappers =
705- existingContent.where (condition).map ((line) => line.trim ()).toSet ()
706- ..addAll (newMappers)
707- ..add ('\n ' );
736+ final existingMappers = existingContentLines
737+ .where (condition)
738+ .map ((line) => line.trim ())
739+ .toSet ()
740+ ..addAll (newMappers)
741+ ..add ('\n ' );
708742
709- existingContent
743+ existingContentLines
710744 ..removeWhere (condition)
711745 ..insert (mapperIndex, existingMappers.toSet ().join ('\n ' ));
712746 }
713747
714- final closingBracketIndex = existingContent.lastIndexOf ('}' );
748+ final closingBracketIndex = existingContentLines.lastIndexWhere (
749+ (line) => line.trim ().contains ('}' ),
750+ );
715751
716- existingContent .insert (closingBracketIndex, fileBody);
752+ existingContentLines .insert (closingBracketIndex, fileBody);
717753
718- final resultFileBody = existingContent .join ('\n ' );
754+ final resultFileBody = existingContentLines .join ('\n ' );
719755
720756 await file.writeAsString (resultFileBody);
721757 logger.i ('File updated: $filePath ' );
0 commit comments