diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart index b752f047e4..bc273b7dc9 100644 --- a/packages/flutter_tools/lib/src/cache.dart +++ b/packages/flutter_tools/lib/src/cache.dart @@ -117,7 +117,7 @@ class Cache { _artifacts.add(AndroidInternalBuildArtifacts(this)); _artifacts.add(IOSEngineArtifacts(this)); - _artifacts.add(FlutterWebSdk(this)); + _artifacts.add(FlutterWebSdk(this, platform: _platform)); _artifacts.add(FlutterSdk(this)); _artifacts.add(WindowsEngineArtifacts(this, platform: _platform)); _artifacts.add(MacOSEngineArtifacts(this)); @@ -691,11 +691,15 @@ class MaterialFonts extends CachedArtifact { /// /// This SDK references code within the regular Dart sdk to reduce download size. class FlutterWebSdk extends CachedArtifact { - FlutterWebSdk(Cache cache) : super( - 'flutter_web_sdk', - cache, - DevelopmentArtifact.web, - ); + FlutterWebSdk(Cache cache, {Platform platform}) + : _platform = platform ?? globals.platform, + super( + 'flutter_web_sdk', + cache, + DevelopmentArtifact.web, + ); + + final Platform _platform; @override Directory get location => cache.getWebSdkDirectory(); @@ -706,22 +710,26 @@ class FlutterWebSdk extends CachedArtifact { @override Future updateInner(ArtifactUpdater artifactUpdater) async { String platformName = 'flutter-web-sdk-'; - if (globals.platform.isMacOS) { + if (_platform.isMacOS) { platformName += 'darwin-x64'; - } else if (globals.platform.isLinux) { + } else if (_platform.isLinux) { platformName += 'linux-x64'; - } else if (globals.platform.isWindows) { + } else if (_platform.isWindows) { platformName += 'windows-x64'; } final Uri url = Uri.parse('${cache.storageBaseUrl}/flutter_infra/flutter/$version/$platformName.zip'); + if (location.existsSync()) { + location.deleteSync(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. + final FileSystem fileSystem = location.fileSystem; for (final FileSystemEntity entity in location.listSync(recursive: true)) { if (entity is File) { - final List segments = globals.fs.path.split(entity.path); + final List segments = fileSystem.path.split(entity.path); segments.remove('flutter_web_sdk'); - final String newPath = globals.fs.path.joinAll(segments); - final File newFile = globals.fs.file(newPath); + final String newPath = fileSystem.path.joinAll(segments); + final File newFile = fileSystem.file(newPath); if (!newFile.existsSync()) { newFile.createSync(recursive: true); } @@ -1556,7 +1564,11 @@ class ArtifactUpdater { _ensureExists(tempFile.parent); final IOSink ioSink = tempFile.openWrite(); await _download(url, ioSink); + await ioSink.flush(); await ioSink.close(); + if (!tempFile.existsSync()) { + throw Exception('Did not find downloaded file ${tempFile.path}'); + } } on Exception catch (err) { _logger.printTrace(err.toString()); retries -= 1; diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index 427f14c16e..94863e199e 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -636,6 +636,28 @@ void main() { expect(logger.errorText, contains('Failed to delete some stamp files')); }); + testWithoutContext('FlutterWebSdk deletes previous directory contents', () { + final MemoryFileSystem fileSystem = MemoryFileSystem.test(); + final Directory webStuff = fileSystem.directory('web-stuff'); + final MockCache cache = MockCache(); + final MockArtifactUpdater artifactUpdater = MockArtifactUpdater(); + final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform(operatingSystem: 'linux')); + + when(cache.getWebSdkDirectory()).thenReturn(webStuff); + when(artifactUpdater.downloadZipArchive('Downloading Web SDK...', any, any)) + .thenAnswer((Invocation invocation) async { + final Directory location = invocation.positionalArguments[2] as Directory; + location.createSync(recursive: true); + location.childFile('foo').createSync(); + }); + webStuff.childFile('bar').createSync(recursive: true); + + webSdk.updateInner(artifactUpdater); + + expect(webStuff.childFile('foo'), exists); + expect(webStuff.childFile('bar'), isNot(exists)); + }); + testWithoutContext('Cache handles exception thrown if stamp file cannot be parsed', () { final FileSystem fileSystem = MemoryFileSystem.test(); final Logger logger = BufferLogger.test();