|
1 | | -import 'dart:io'; |
2 | | - |
3 | 1 | import 'package:collection/collection.dart'; |
4 | 2 | import 'package:ground_control_client/ground_control_client.dart'; |
5 | | -import 'package:path/path.dart' as p; |
6 | 3 | import 'package:serverpod_cloud_cli/command_logger/command_logger.dart'; |
7 | 4 | import 'package:serverpod_cloud_cli/shared/exceptions/exit_exceptions.dart'; |
8 | | -import 'package:serverpod_cloud_cli/commands/deploy/prepare_workspace.dart' |
9 | | - show WorkspaceProject; |
10 | 5 | import 'package:serverpod_cloud_cli/shared/user_interaction/user_confirmations.dart'; |
11 | 6 | import 'package:serverpod_cloud_cli/util/printers/table_printer.dart'; |
12 | | -import 'package:serverpod_cloud_cli/util/pubspec_validator.dart'; |
13 | | -import 'package:serverpod_cloud_cli/util/scloud_config/scloud_config_model.dart'; |
14 | | -import 'package:serverpod_cloud_cli/util/scloud_config/scloud_config_io.dart'; |
15 | | -import 'package:serverpod_cloud_cli/util/scloudignore.dart'; |
| 7 | +import 'package:serverpod_cloud_cli/util/project_files_writer.dart'; |
16 | 8 |
|
17 | 9 | abstract class ProjectCommands { |
18 | 10 | static const defaultPlanName = 'early-access'; |
@@ -97,40 +89,6 @@ abstract class ProjectCommands { |
97 | 89 | }); |
98 | 90 | } |
99 | 91 |
|
100 | | - if (isServerpodServerDirectory(Directory(projectDir))) { |
101 | | - // write scloud project files unless the config file already exists |
102 | | - |
103 | | - final scloudYamlFile = File(configFilePath); |
104 | | - if (scloudYamlFile.existsSync()) { |
105 | | - logger.success('Serverpod Cloud project created.', newParagraph: true); |
106 | | - |
107 | | - return; |
108 | | - } |
109 | | - |
110 | | - final config = ScloudConfig( |
111 | | - projectId: projectId, |
112 | | - scripts: ScloudScripts.empty(), |
113 | | - ); |
114 | | - |
115 | | - await logger.progress( |
116 | | - 'Writing cloud project configuration files.', |
117 | | - () async { |
118 | | - _writeProjectFiles(logger, config, projectDir, configFilePath); |
119 | | - return true; |
120 | | - }, |
121 | | - ); |
122 | | - } else { |
123 | | - logger.terminalCommand( |
124 | | - message: |
125 | | - 'Since no Serverpod server directory was identified, ' |
126 | | - 'an scloud.yaml configuration file has not been created. ' |
127 | | - 'Use the link command to create it in the server ' |
128 | | - 'directory of this project:', |
129 | | - newParagraph: true, |
130 | | - 'scloud project link --project $projectId', |
131 | | - ); |
132 | | - } |
133 | | - |
134 | 92 | logger.success('Serverpod Cloud project created.', newParagraph: true); |
135 | 93 | } |
136 | 94 |
|
@@ -212,15 +170,15 @@ abstract class ProjectCommands { |
212 | 170 | required final String projectDirectory, |
213 | 171 | required final String configFilePath, |
214 | 172 | }) async { |
215 | | - final config = ScloudConfig( |
216 | | - projectId: projectId, |
217 | | - scripts: ScloudScripts.empty(), |
218 | | - ); |
219 | | - |
220 | 173 | await logger.progress( |
221 | 174 | 'Writing cloud project configuration files.', |
222 | 175 | () async { |
223 | | - _writeProjectFiles(logger, config, projectDirectory, configFilePath); |
| 176 | + ProjectFilesWriter.writeFiles( |
| 177 | + projectId: projectId, |
| 178 | + preDeployScripts: [], |
| 179 | + configFilePath: configFilePath, |
| 180 | + projectDirectory: projectDirectory, |
| 181 | + ); |
224 | 182 | return true; |
225 | 183 | }, |
226 | 184 | ); |
@@ -290,96 +248,4 @@ abstract class ProjectCommands { |
290 | 248 | ); |
291 | 249 | } |
292 | 250 | } |
293 | | - |
294 | | - static void _writeProjectFiles( |
295 | | - final CommandLogger logger, |
296 | | - final ScloudConfig config, |
297 | | - final String projectDirectory, |
298 | | - final String configFilePath, |
299 | | - ) { |
300 | | - final workspaceRootDir = _findWorkspaceRootDir( |
301 | | - logger, |
302 | | - Directory(projectDirectory), |
303 | | - ); |
304 | | - |
305 | | - try { |
306 | | - ScloudConfigIO.writeToFile(config, configFilePath); |
307 | | - final relativePath = p.relative(configFilePath); |
308 | | - logger.debug( |
309 | | - "Wrote the '$relativePath' configuration file for '${config.projectId}'.", |
310 | | - ); |
311 | | - } on Exception catch (e, s) { |
312 | | - throw FailureException.nested( |
313 | | - e, |
314 | | - s, |
315 | | - 'Failed to write to the $configFilePath file', |
316 | | - ); |
317 | | - } |
318 | | - |
319 | | - try { |
320 | | - ScloudIgnore.writeTemplateIfNotExists( |
321 | | - rootFolder: workspaceRootDir?.path ?? projectDirectory, |
322 | | - ); |
323 | | - logger.debug("Wrote the '${ScloudIgnore.fileName}' file."); |
324 | | - } on Exception catch (e, s) { |
325 | | - throw FailureException.nested( |
326 | | - e, |
327 | | - s, |
328 | | - 'Failed to write to ${ScloudIgnore.fileName} file', |
329 | | - ); |
330 | | - } |
331 | | - |
332 | | - if (workspaceRootDir != null) { |
333 | | - try { |
334 | | - final updated = _updateGitIgnore(workspaceRootDir); |
335 | | - if (updated) { |
336 | | - logger.debug( |
337 | | - "Added '${ScloudIgnore.scloudDirName}/' to '.gitignore' in the workspace directory.", |
338 | | - ); |
339 | | - } |
340 | | - } on Exception catch (e, s) { |
341 | | - throw FailureException.nested( |
342 | | - e, |
343 | | - s, |
344 | | - 'Failed to write to the .gitignore file', |
345 | | - ); |
346 | | - } |
347 | | - } |
348 | | - } |
349 | | - |
350 | | - static Directory? _findWorkspaceRootDir( |
351 | | - final CommandLogger logger, |
352 | | - final Directory projectDir, |
353 | | - ) { |
354 | | - final projectPubspec = TenantProjectPubspec.fromProjectDir(projectDir); |
355 | | - |
356 | | - if (projectPubspec.isWorkspaceResolved()) { |
357 | | - final (workspaceRootDir, workspacePubspec) = |
358 | | - WorkspaceProject.findWorkspaceRoot(projectDir); |
359 | | - return workspaceRootDir; |
360 | | - } |
361 | | - |
362 | | - return null; |
363 | | - } |
364 | | - |
365 | | - static bool _updateGitIgnore(final Directory workspaceRootDir) { |
366 | | - const scloudIgnoreTemplate = |
367 | | - ''' |
368 | | -# scloud deployment generated files should not be committed to git |
369 | | -**/${ScloudIgnore.scloudDirName}/ |
370 | | -'''; |
371 | | - final gitIgnoreFile = File(p.join(workspaceRootDir.path, '.gitignore')); |
372 | | - final String content; |
373 | | - if (gitIgnoreFile.existsSync()) { |
374 | | - final read = gitIgnoreFile.readAsStringSync(); |
375 | | - if (read.contains('${ScloudIgnore.scloudDirName}/')) { |
376 | | - return false; |
377 | | - } |
378 | | - content = read.endsWith('\n') ? '$read\n' : '$read\n\n'; |
379 | | - } else { |
380 | | - content = ''; |
381 | | - } |
382 | | - gitIgnoreFile.writeAsStringSync('$content$scloudIgnoreTemplate'); |
383 | | - return true; |
384 | | - } |
385 | 251 | } |
0 commit comments