Skip to content

Commit

Permalink
Download platform-agnostic Flutter Web SDK in the flutter_tool (#118654)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Terkelsen authored Jan 26, 2023
1 parent 1b779b6 commit 42bd5f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 88 deletions.
34 changes: 4 additions & 30 deletions packages/flutter_tools/lib/src/flutter_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FlutterCache extends Cache {
registerArtifact(AndroidGenSnapshotArtifacts(this, platform: platform));
registerArtifact(AndroidInternalBuildArtifacts(this));
registerArtifact(IOSEngineArtifacts(this, platform: platform));
registerArtifact(FlutterWebSdk(this, platform: platform));
registerArtifact(FlutterWebSdk(this));
registerArtifact(FlutterSdk(this, platform: platform));
registerArtifact(WindowsEngineArtifacts(this, platform: platform));
registerArtifact(MacOSEngineArtifacts(this, platform: platform));
Expand Down Expand Up @@ -158,16 +158,13 @@ class MaterialFonts extends CachedArtifact {
///
/// This SDK references code within the regular Dart sdk to reduce download size.
class FlutterWebSdk extends CachedArtifact {
FlutterWebSdk(Cache cache, {required Platform platform})
: _platform = platform,
super(
FlutterWebSdk(Cache cache)
: super(
'flutter_web_sdk',
cache,
DevelopmentArtifact.web,
);

final Platform _platform;

@override
Directory get location => cache.getWebSdkDirectory();

Expand All @@ -180,15 +177,7 @@ class FlutterWebSdk extends CachedArtifact {
FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils,
) async {
String platformName = 'flutter-web-sdk-';
if (_platform.isMacOS) {
platformName += 'darwin-x64';
} else if (_platform.isLinux) {
platformName += 'linux-x64';
} else if (_platform.isWindows) {
platformName += 'windows-x64';
}
final Uri url = Uri.parse('${cache.storageBaseUrl}/flutter_infra_release/flutter/$version/$platformName.zip');
final Uri url = Uri.parse('${cache.storageBaseUrl}/flutter_infra_release/flutter/$version/flutter-web-sdk.zip');
ErrorHandlingFileSystem.deleteIfExists(location, recursive: true);
await artifactUpdater.downloadZipArchive('Downloading Web SDK...', url, location);
// This is a temporary work-around for not being able to safely download into a shared directory.
Expand All @@ -205,21 +194,6 @@ class FlutterWebSdk extends CachedArtifact {
entity.copySync(newPath);
}
}

// If the flutter_web_sdk folder doesn't already contain CanvasKit, then
// download it from CIPD.
// TODO(hterkelsen): This whole section can be removed when we are always building
// CanvasKit as part of flutter_web_sdk. See https://github.com/flutter/flutter/issues/113073
final File expectedCanvasKitFile = fileSystem.file(fileSystem.path.join(location.path, 'canvaskit', 'canvaskit.wasm'));
if (!expectedCanvasKitFile.existsSync()) {
final String canvasKitVersion = cache.getVersionFor('canvaskit')!;
final String canvasKitUrl = '${cache.cipdBaseUrl}/flutter/web/canvaskit_bundle/+/$canvasKitVersion';
return artifactUpdater.downloadZipArchive(
'Downloading CanvasKit...',
Uri.parse(canvasKitUrl),
location,
);
}
}
}

Expand Down
63 changes: 5 additions & 58 deletions packages/flutter_tools/test/general.shard/cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ void main() {
final Cache cache = Cache.test(processManager: FakeProcessManager.any(), fileSystem: fileSystem);
final Directory webCacheDirectory = cache.getWebSdkDirectory();
final FakeArtifactUpdater artifactUpdater = FakeArtifactUpdater();
final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform());
final FlutterWebSdk webSdk = FlutterWebSdk(cache);

final List<String> messages = <String>[];
final List<String> downloads = <String>[];
Expand All @@ -778,17 +778,14 @@ void main() {

expect(messages, <String>[
'Downloading Web SDK...',
'Downloading CanvasKit...',
]);

expect(downloads, <String>[
'https://storage.googleapis.com/flutter_infra_release/flutter/hijklmnop/flutter-web-sdk-linux-x64.zip',
'https://chrome-infra-packages.appspot.com/dl/flutter/web/canvaskit_bundle/+/abcdefg',
'https://storage.googleapis.com/flutter_infra_release/flutter/hijklmnop/flutter-web-sdk.zip',
]);

expect(locations, <String>[
'cache/bin/cache/flutter_web_sdk',
'cache/bin/cache/flutter_web_sdk',
]);

expect(webCacheDirectory.childFile('foo'), exists);
Expand Down Expand Up @@ -820,7 +817,7 @@ void main() {
);
final Directory webCacheDirectory = cache.getWebSdkDirectory();
final FakeArtifactUpdater artifactUpdater = FakeArtifactUpdater();
final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform());
final FlutterWebSdk webSdk = FlutterWebSdk(cache);

final List<String> downloads = <String>[];
final List<String> locations = <String>[];
Expand All @@ -835,57 +832,7 @@ void main() {
await webSdk.updateInner(artifactUpdater, fileSystem, FakeOperatingSystemUtils());

expect(downloads, <String>[
'https://flutter.storage.com/override/flutter_infra_release/flutter/hijklmnop/flutter-web-sdk-linux-x64.zip',
'https://flutter.storage.com/override/flutter_infra_release/cipd/flutter/web/canvaskit_bundle/+/abcdefg',
]);
});

testWithoutContext('FlutterWebSdk does not download CanvasKit if it is already in flutter_web_sdk', () async {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final Directory internalDir = fileSystem.currentDirectory
.childDirectory('cache')
.childDirectory('bin')
.childDirectory('internal');
final File canvasKitVersionFile = internalDir.childFile('canvaskit.version');
canvasKitVersionFile.createSync(recursive: true);
canvasKitVersionFile.writeAsStringSync('abcdefg');

final File engineVersionFile = internalDir.childFile('engine.version');
engineVersionFile.createSync(recursive: true);
engineVersionFile.writeAsStringSync('hijklmnop');

final Cache cache = Cache.test(processManager: FakeProcessManager.any(), fileSystem: fileSystem);
final FakeArtifactUpdater artifactUpdater = FakeArtifactUpdater();
final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform());

final List<String> messages = <String>[];
final List<String> downloads = <String>[];
final List<String> locations = <String>[];
artifactUpdater.onDownloadZipArchive = (String message, Uri uri, Directory location) {
messages.add(message);
downloads.add(uri.toString());
locations.add(location.path);
location.createSync(recursive: true);
location.childDirectory('canvaskit').createSync();
location.childDirectory('canvaskit').childFile('canvaskit.js').createSync();
location.childDirectory('canvaskit').childFile('canvaskit.wasm').createSync();
location.childDirectory('canvaskit').childDirectory('profiling').createSync();
location.childDirectory('canvaskit').childDirectory('profiling').childFile('canvaskit.js').createSync();
location.childDirectory('canvaskit').childDirectory('profiling').childFile('canvaskit.wasm').createSync();
};

await webSdk.updateInner(artifactUpdater, fileSystem, FakeOperatingSystemUtils());

expect(messages, <String>[
'Downloading Web SDK...',
]);

expect(downloads, <String>[
'https://storage.googleapis.com/flutter_infra_release/flutter/hijklmnop/flutter-web-sdk-linux-x64.zip',
]);

expect(locations, <String>[
'cache/bin/cache/flutter_web_sdk',
'https://flutter.storage.com/override/flutter_infra_release/flutter/hijklmnop/flutter-web-sdk.zip',
]);
});

Expand All @@ -895,7 +842,7 @@ void main() {
final Cache cache = Cache.test(processManager: FakeProcessManager.any(), fileSystem: fileSystem);
final Directory webCacheDirectory = cache.getWebSdkDirectory();
final FakeArtifactUpdater artifactUpdater = FakeArtifactUpdater();
final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform());
final FlutterWebSdk webSdk = FlutterWebSdk(cache);

artifactUpdater.onDownloadZipArchive = (String message, Uri uri, Directory location) {
location.createSync(recursive: true);
Expand Down

0 comments on commit 42bd5f2

Please sign in to comment.