1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:meta/meta.dart';
import 'file_system.dart';
import 'logger.dart';
/// Project is generated from a template on Flutter project creation.
/// Sometimes (due to behavior changes in Xcode, Gradle, etc) these files need to be altered
/// from the original template.
abstract class ProjectMigrator {
ProjectMigrator(this.logger);
@protected
final Logger logger;
void migrate();
/// Return null if the line should be deleted.
@protected
String? migrateLine(String line) {
return line;
}
@protected
String migrateFileContents(String fileContents) {
return fileContents;
}
@protected
bool get migrationRequired => _migrationRequired;
bool _migrationRequired = false;
@protected
/// Calls [migrateLine] per line, then [migrateFileContents]
/// including the line migrations.
void processFileLines(File file) {
final List<String> lines = file.readAsLinesSync();
final StringBuffer newProjectContents = StringBuffer();
final String basename = file.basename;
for (final String line in lines) {
final String? newProjectLine = migrateLine(line);
if (newProjectLine == null) {
logger.printTrace('Migrating $basename, removing:');
logger.printTrace(' $line');
_migrationRequired = true;
continue;
}
if (newProjectLine != line) {
logger.printTrace('Migrating $basename, replacing:');
logger.printTrace(' $line');
logger.printTrace('with:');
logger.printTrace(' $newProjectLine');
_migrationRequired = true;
}
newProjectContents.writeln(newProjectLine);
}
final String projectContentsWithMigratedLines = newProjectContents.toString();
final String projectContentsWithMigratedContents = migrateFileContents(projectContentsWithMigratedLines);
if (projectContentsWithMigratedLines != projectContentsWithMigratedContents) {
logger.printTrace('Migrating $basename contents');
_migrationRequired = true;
}
if (migrationRequired) {
logger.printStatus('Upgrading $basename');
file.writeAsStringSync(projectContentsWithMigratedContents);
}
}
}
class ProjectMigration {
ProjectMigration(this.migrators);
final List<ProjectMigrator> migrators;
void run() {
for (final ProjectMigrator migrator in migrators) {
migrator.migrate();
}
}
}