From e9ad65af8c322b4ac8fcf22ccb0b74d35f0fc014 Mon Sep 17 00:00:00 2001 From: Elijah Quartey Date: Thu, 18 Jul 2024 09:17:23 -0500 Subject: [PATCH] refactor copy/upload to accept list of files --- infra-gen2/tool/deploy_gen2.dart | 78 +++++++++++++++----------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/infra-gen2/tool/deploy_gen2.dart b/infra-gen2/tool/deploy_gen2.dart index 256ecd1033..0c37588ecf 100644 --- a/infra-gen2/tool/deploy_gen2.dart +++ b/infra-gen2/tool/deploy_gen2.dart @@ -127,11 +127,7 @@ void main(List arguments) async { // Copy config files to shared paths _copyConfigFile( backendGroup.sharedOutputs, - amplifyOutputs, - ); - _copyConfigFile( - backendGroup.sharedOutputs, - amplifyConfiguration, + [amplifyOutputs, amplifyConfiguration], ); // Check if the S3 bucket exists @@ -148,11 +144,7 @@ void main(List arguments) async { // Upload config files to S3 bucket _uploadConfigFileToS3( bucketName, - amplifyOutputs, - ); - _uploadConfigFileToS3( - bucketName, - amplifyConfiguration, + [amplifyOutputs, amplifyConfiguration], ); print('✅ Deployment for $categoryName Category complete'); @@ -300,25 +292,27 @@ void _appendEnvironments( /// Copy a given config file to a list of shared paths void _copyConfigFile( List outputPaths, - File configFile, + List configFiles, ) { if (outputPaths.length <= 1) { return; } - final fileName = configFile.path.split('/').last; + for (final configFile in configFiles) { + final fileName = configFile.path.split('/').last; - print('👯 Copying $fileName to other shared paths'); - for (final outputPath in outputPaths) { - final destination = p.join(repoRoot.path, outputPath); - final outputFile = File(p.join(destination, fileName)); + print('👯 Copying $fileName to other shared paths'); + for (final outputPath in outputPaths) { + final destination = p.join(repoRoot.path, outputPath); + final outputFile = File(p.join(destination, fileName)); - if (!outputFile.existsSync()) { - outputFile.createSync(recursive: true); - } - final amplifyOutputsContents = configFile.readAsStringSync(); + if (!outputFile.existsSync()) { + outputFile.createSync(recursive: true); + } + final amplifyOutputsContents = configFile.readAsStringSync(); - outputFile.writeAsStringSync(amplifyOutputsContents); + outputFile.writeAsStringSync(amplifyOutputsContents); + } } } @@ -397,29 +391,31 @@ void _createS3Bucket(String bucketName) { /// Upload the amplify_outputs.dart file to the S3 bucket void _uploadConfigFileToS3( String bucketName, - File configFile, + List configFiles, ) { - final fileName = configFile.path.split('/').last; - print('📲 Uploading $fileName to S3 bucket'); - final downloadRes = Process.runSync( - 'aws', - [ - '--profile=${Platform.environment['AWS_PROFILE'] ?? 'default'}', - 's3', - 'cp', - configFile.path, - 's3://$bucketName/$fileName', - ], - stdoutEncoding: utf8, - stderrEncoding: utf8, - ); - if (downloadRes.exitCode != 0) { - throw Exception( - '❌ Error downloading $bucketName config from S3: ' - '${downloadRes.stdout}\n${downloadRes.stderr}', + for (final configFile in configFiles) { + final fileName = configFile.path.split('/').last; + print('📲 Uploading $fileName to S3 bucket'); + final downloadRes = Process.runSync( + 'aws', + [ + '--profile=${Platform.environment['AWS_PROFILE'] ?? 'default'}', + 's3', + 'cp', + configFile.path, + 's3://$bucketName/$fileName', + ], + stdoutEncoding: utf8, + stderrEncoding: utf8, ); + if (downloadRes.exitCode != 0) { + throw Exception( + '❌ Error downloading $bucketName config from S3: ' + '${downloadRes.stdout}\n${downloadRes.stderr}', + ); + } + print('👍 $fileName successfully uploaded to S3 bucket'); } - print('👍 $fileName successfully uploaded to S3 bucket'); } /// Generates gen 1 amplifyconfiguration.dart file