forked from dart-lang/webdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk_configuration.dart
178 lines (147 loc) · 5.35 KB
/
sdk_configuration.dart
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:path/path.dart' as p;
class InvalidSdkConfigurationException implements Exception {
final String? message;
InvalidSdkConfigurationException([this.message]);
@override
String toString() {
final message = this.message;
if (message == null) return 'Invalid SDK configuration';
return 'Invalid SDK configuration: $message';
}
}
/// SDK configuration provider interface.
///
/// Supports lazily populated configurations by allowing to create
/// configuration asynchronously.
abstract class SdkConfigurationProvider {
const SdkConfigurationProvider();
Future<SdkConfiguration> get configuration;
}
/// Dart SDK layout.
///
/// Contains definition of the default SDK layout.
/// We keep all the path constants in one place for ease of update.
class SdkLayout {
static final defaultSdkDirectory =
p.dirname(p.dirname(Platform.resolvedExecutable));
static final SdkLayout defaultSdkLayout =
SdkLayout.createDefault(defaultSdkDirectory);
final String sdkDirectory;
final String summaryPath;
final String dartdevcSnapshotPath;
@Deprecated('Only sound null safety is supported as of Dart 3.0')
final String soundSummaryPath;
@Deprecated('Only sound null safety is supported as of Dart 3.0')
final String weakSummaryPath;
SdkLayout.createDefault(String sdkDirectory)
: this(
sdkDirectory: sdkDirectory,
summaryPath: p.join(
sdkDirectory,
'lib',
'_internal',
'ddc_outline.dill',
),
dartdevcSnapshotPath: p.join(
sdkDirectory,
'bin',
'snapshots',
'dartdevc.dart.snapshot',
),
);
const SdkLayout({
required this.sdkDirectory,
required this.summaryPath,
this.soundSummaryPath = '',
this.weakSummaryPath = '',
required this.dartdevcSnapshotPath,
});
}
/// Dart SDK configuration.
///
/// Provides helpers to convert paths to uris that work on all platforms.
///
class SdkConfiguration {
static final defaultSdkLayout = SdkLayout.defaultSdkLayout;
static final defaultConfiguration =
SdkConfiguration.fromSdkLayout(SdkLayout.defaultSdkLayout);
final String? sdkDirectory;
final String? sdkSummaryPath;
final String? compilerWorkerPath;
@Deprecated('Only sound null safety is supported as of Dart 3.0')
final String? weakSdkSummaryPath;
@Deprecated('Only sound null safety is supported as of Dart 3.0')
final String? soundSdkSummaryPath;
const SdkConfiguration({
this.sdkDirectory,
this.sdkSummaryPath,
this.weakSdkSummaryPath,
this.soundSdkSummaryPath,
this.compilerWorkerPath,
});
const SdkConfiguration.empty() : this();
SdkConfiguration.fromSdkLayout(SdkLayout sdkLayout)
: this(
sdkDirectory: sdkLayout.sdkDirectory,
sdkSummaryPath: sdkLayout.summaryPath,
compilerWorkerPath: sdkLayout.dartdevcSnapshotPath,
);
static Uri? _toUri(String? path) => path == null ? null : p.toUri(path);
static Uri? _toAbsoluteUri(String? path) =>
path == null ? null : p.toUri(p.absolute(path));
Uri? get sdkDirectoryUri => _toUri(sdkDirectory);
Uri? get sdkSummaryUri => _toUri(sdkSummaryPath);
@Deprecated('Only sound null safety is supported as of Dart 3.0')
Uri? get soundSdkSummaryUri => _toUri(soundSdkSummaryPath);
@Deprecated('Only sound null safety is supported as of Dart 3.0')
Uri? get weakSdkSummaryUri => _toUri(weakSdkSummaryPath);
/// Note: has to be ///file: Uri to run in an isolate.
Uri? get compilerWorkerUri => _toAbsoluteUri(compilerWorkerPath);
/// Throws [InvalidSdkConfigurationException] if configuration does not
/// exist on disk.
void validate({FileSystem fileSystem = const LocalFileSystem()}) {
validateSdkDir(fileSystem: fileSystem);
validateSummaries(fileSystem: fileSystem);
validateCompilerWorker(fileSystem: fileSystem);
}
/// Throws [InvalidSdkConfigurationException] if SDK root does not
/// exist on the disk.
void validateSdkDir({FileSystem fileSystem = const LocalFileSystem()}) {
if (sdkDirectory == null ||
!fileSystem.directory(sdkDirectory).existsSync()) {
throw InvalidSdkConfigurationException(
'Sdk directory $sdkDirectory does not exist',
);
}
}
void validateSummaries({FileSystem fileSystem = const LocalFileSystem()}) {
if (sdkSummaryPath == null ||
!fileSystem.file(sdkSummaryPath).existsSync()) {
throw InvalidSdkConfigurationException(
'Sdk summary $sdkSummaryPath does not exist',
);
}
}
void validateCompilerWorker({
FileSystem fileSystem = const LocalFileSystem(),
}) {
if (compilerWorkerPath == null ||
!fileSystem.file(compilerWorkerPath).existsSync()) {
throw InvalidSdkConfigurationException(
'Compiler worker $compilerWorkerPath does not exist',
);
}
}
}
class DefaultSdkConfigurationProvider extends SdkConfigurationProvider {
const DefaultSdkConfigurationProvider();
@override
Future<SdkConfiguration> get configuration async =>
SdkConfiguration.defaultConfiguration;
}