Skip to content

Commit 418f680

Browse files
Add offline mode
1 parent a5ac4ac commit 418f680

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

webdev/lib/src/command/configuration.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const nullSafetyAuto = 'auto';
3737
const disableDdsFlag = 'disable-dds';
3838
const enableExperimentOption = 'enable-experiment';
3939
const canaryFeaturesFlag = 'canary';
40+
const offlineFlag = 'offline';
4041

4142
ReloadConfiguration _parseReloadConfiguration(ArgResults argResults) {
4243
var auto = argResults.options.contains(autoOption)
@@ -107,6 +108,7 @@ class Configuration {
107108
final String? _nullSafety;
108109
final List<String>? _experiments;
109110
final bool? _canaryFeatures;
111+
final bool? _offline;
110112

111113
Configuration({
112114
bool? autoRun,
@@ -133,6 +135,7 @@ class Configuration {
133135
String? nullSafety,
134136
List<String>? experiments,
135137
bool? canaryFeatures,
138+
bool? offline,
136139
}) : _autoRun = autoRun,
137140
_chromeDebugPort = chromeDebugPort,
138141
_debugExtension = debugExtension,
@@ -154,7 +157,8 @@ class Configuration {
154157
_verbose = verbose,
155158
_nullSafety = nullSafety,
156159
_experiments = experiments,
157-
_canaryFeatures = canaryFeatures {
160+
_canaryFeatures = canaryFeatures,
161+
_offline = offline {
158162
_validateConfiguration();
159163
}
160164

@@ -229,7 +233,8 @@ class Configuration {
229233
verbose: other._verbose ?? _verbose,
230234
nullSafety: other._nullSafety ?? _nullSafety,
231235
experiments: other._experiments ?? _experiments,
232-
canaryFeatures: other._canaryFeatures ?? _canaryFeatures);
236+
canaryFeatures: other._canaryFeatures ?? _canaryFeatures,
237+
offline: other._offline ?? _offline);
233238

234239
factory Configuration.noInjectedClientDefaults() =>
235240
Configuration(autoRun: false, debug: false, debugExtension: false);
@@ -284,6 +289,8 @@ class Configuration {
284289

285290
bool get canaryFeatures => _canaryFeatures ?? false;
286291

292+
bool get offline => _offline ?? false;
293+
287294
/// Returns a new configuration with values updated from the parsed args.
288295
static Configuration fromArgs(ArgResults? argResults,
289296
{Configuration? defaultConfiguration}) {
@@ -408,6 +415,10 @@ class Configuration {
408415
? argResults[canaryFeaturesFlag] as bool?
409416
: defaultConfiguration.canaryFeatures;
410417

418+
final offline = argResults.options.contains(offlineFlag)
419+
? argResults[offlineFlag] as bool?
420+
: defaultConfiguration.verbose;
421+
411422
return Configuration(
412423
autoRun: defaultConfiguration.autoRun,
413424
chromeDebugPort: chromeDebugPort,
@@ -433,6 +444,7 @@ class Configuration {
433444
nullSafety: nullSafety,
434445
experiments: experiments,
435446
canaryFeatures: canaryFeatures,
447+
offline: offline,
436448
);
437449
}
438450
}

webdev/lib/src/command/shared.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ void addSharedArgs(ArgParser argParser,
7474
abbr: 'v',
7575
defaultsTo: false,
7676
negatable: false,
77-
help: 'Enables verbose logging.');
77+
help: 'Enables verbose logging.')
78+
..addFlag(offlineFlag,
79+
defaultsTo: false,
80+
negatable: false,
81+
help: 'Disable feching from pub.dev.');
7882
}
7983

8084
/// Parses the provided [Configuration] to return a list of
@@ -103,7 +107,9 @@ List<String> buildRunnerArgs(Configuration configuration) {
103107
}
104108

105109
Future<void> validatePubspecLock(Configuration configuration) async {
106-
final pubspecLock = await PubspecLock.read();
110+
final pubspecLock = await PubspecLock.read(
111+
offline: configuration.offline
112+
);
107113
await checkPubspecLock(pubspecLock,
108114
requireBuildWebCompilers: configuration.requireBuildWebCompilers);
109115
}

webdev/lib/src/pubspec.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ class PubspecLock {
6868

6969
PubspecLock(this._packages);
7070

71-
static Future<PubspecLock> read() async {
72-
await _runPubDeps();
71+
static Future<PubspecLock> read({
72+
bool offline = false
73+
}) async {
74+
if (!offline) {
75+
await _runPubDeps();
76+
}
7377
var dir = p.absolute(p.current);
7478
while (true) {
7579
final candidate = p.join(

webdev/test/installation_test.dart

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ void main() {
3030
Process? serveProcess;
3131
Directory? tempDir0;
3232

33+
final testScript =
34+
File(p.join(p.dirname(Platform.script.toFilePath()), 'test.dart'))
35+
.readAsStringSync();
36+
final thisScript = File.fromUri(Uri.parse(testScript.substring(
37+
testScript.lastIndexOf('import', testScript.indexOf('as test;')) + 8,
38+
testScript.indexOf('as test;') - 2)));
39+
final packageDir = p.dirname(p.dirname(thisScript.path));
40+
3341
Future<void> expectStdoutAndCleanExit(Process process,
3442
{required String expectedStdout}) async {
3543
final stdoutCompleter = _captureOutput(
@@ -127,7 +135,7 @@ void main() {
127135
// Verify that `dart pub global activate` works:
128136
activateProcess = await Process.start(
129137
'dart',
130-
['pub', 'global', 'activate', 'webdev'],
138+
['pub', 'global', 'activate', '--source', 'path', packageDir],
131139
);
132140
await expectStdoutAndCleanExit(
133141
activateProcess!,
@@ -141,6 +149,41 @@ void main() {
141149
await expectStdoutThenExit(serveProcess!,
142150
expectedStdout: 'Serving `web` on');
143151
});
152+
153+
test('activate and serve webdev fails with offline', () async {
154+
final tempDir = tempDir0!;
155+
final tempPath = tempDir.path;
156+
157+
// Verify that we can create a new Dart app:
158+
createProcess = await Process.start(
159+
'dart',
160+
['create', '--no-pub', '--template', 'web', 'temp_app'],
161+
workingDirectory: tempPath,
162+
);
163+
await expectStdoutAndCleanExit(
164+
createProcess!,
165+
expectedStdout: 'Created project temp_app in temp_app!',
166+
);
167+
final appPath = p.join(tempPath, 'temp_app');
168+
expect(await Directory(appPath).exists(), isTrue);
169+
170+
// Verify that `dart pub global activate` works:
171+
activateProcess = await Process.start(
172+
'dart',
173+
['pub', 'global', 'activate', '--source', 'path', packageDir],
174+
);
175+
await expectStdoutAndCleanExit(
176+
activateProcess!,
177+
expectedStdout: 'Activated webdev',
178+
);
179+
180+
// Verify that `webdev serve` works for our new app:
181+
serveProcess = await Process.start('dart',
182+
['pub', 'global', 'run', 'webdev', 'serve', '--offline', 'web:8081'],
183+
workingDirectory: appPath);
184+
await expectStdoutThenExit(serveProcess!,
185+
expectedStdout: 'Cannot open file\n pubspec.lock\n');
186+
});
144187
}
145188

146189
Future<int> _waitForExitOrTimeout(Process process) {

0 commit comments

Comments
 (0)