diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart index 71c0311e16..a5c72356e5 100644 --- a/packages/flutter_tools/lib/src/cache.dart +++ b/packages/flutter_tools/lib/src/cache.dart @@ -9,10 +9,8 @@ import 'dart:async'; import 'package:crypto/crypto.dart'; import 'package:file/memory.dart'; import 'package:meta/meta.dart'; -import 'package:package_config/package_config.dart'; import 'package:process/process.dart'; -import 'android/android_studio.dart'; import 'base/common.dart'; import 'base/error_handling_io.dart'; import 'base/file_system.dart'; @@ -21,16 +19,16 @@ import 'base/logger.dart'; import 'base/net.dart'; import 'base/os.dart' show OperatingSystemUtils; import 'base/platform.dart'; -import 'base/process.dart'; import 'base/user_messages.dart'; import 'build_info.dart'; import 'convert.dart'; -import 'dart/package_map.dart'; -import 'dart/pub.dart'; import 'features.dart'; -import 'globals.dart' as globals; -import 'runner/flutter_command.dart'; -import 'runner/flutter_command_runner.dart'; + +const String kFlutterRootEnvironmentVariableName = 'FLUTTER_ROOT'; // should point to //flutter/ (root of flutter/flutter repo) +const String kFlutterEngineEnvironmentVariableName = 'FLUTTER_ENGINE'; // should point to //engine/src/ (root of flutter/engine repo) +const String kSnapshotFileName = 'flutter_tools.snapshot'; // in //flutter/bin/cache/ +const String kFlutterToolsScriptFileName = 'flutter_tools.dart'; // in //flutter/packages/flutter_tools/bin/ +const String kFlutterEnginePackageName = 'sky_engine'; /// A tag for a set of development artifacts that need to be cached. class DevelopmentArtifact { @@ -99,6 +97,9 @@ class DevelopmentArtifact { } /// A wrapper around the `bin/cache/` directory. +/// +/// This does not provide any artifacts by default. See [FlutterCache] for the default +/// artifact set. class Cache { /// [rootOverride] is configurable for testing. /// [artifacts] is configurable for testing. @@ -115,37 +116,8 @@ class Cache { _platform = platform, _osUtils = osUtils, _net = Net(logger: logger, platform: platform), - _fsUtils = FileSystemUtils(fileSystem: fileSystem, platform: platform) { - if (artifacts == null) { - _artifacts.add(MaterialFonts(this)); - _artifacts.add(GradleWrapper(this)); - _artifacts.add(AndroidGenSnapshotArtifacts(this, platform: _platform)); - _artifacts.add(AndroidInternalBuildArtifacts(this)); - _artifacts.add(IOSEngineArtifacts(this, platform: _platform)); - _artifacts.add(FlutterWebSdk(this, platform: _platform)); - _artifacts.add(FlutterSdk(this, platform: _platform)); - _artifacts.add(WindowsEngineArtifacts(this, platform: _platform)); - _artifacts.add(MacOSEngineArtifacts(this, platform: _platform)); - _artifacts.add(LinuxEngineArtifacts(this, platform: _platform)); - _artifacts.add(LinuxFuchsiaSDKArtifacts(this, platform: _platform)); - _artifacts.add(MacOSFuchsiaSDKArtifacts(this, platform: _platform)); - _artifacts.add(FlutterRunnerSDKArtifacts(this, platform: _platform)); - _artifacts.add(FlutterRunnerDebugSymbols(this, platform: _platform)); - for (final String artifactName in IosUsbArtifacts.artifactNames) { - _artifacts.add(IosUsbArtifacts(artifactName, this, platform: _platform)); - } - _artifacts.add(FontSubsetArtifacts(this, platform: _platform)); - _artifacts.add(PubDependencies( - logger: _logger, - // flutter root and pub must be lazily initialized to avoid accessing - // before the version is determined. - flutterRoot: () => flutterRoot, - pub: () => pub, - )); - } else { - _artifacts.addAll(artifacts); - } - } + _fsUtils = FileSystemUtils(fileSystem: fileSystem, platform: platform), + _artifacts = artifacts; /// Create a [Cache] for testing. /// @@ -183,10 +155,19 @@ class Cache { final Platform _platform; final FileSystem _fileSystem; final OperatingSystemUtils _osUtils; + final Directory _rootOverride; + final List _artifacts; + final Net _net; + final FileSystemUtils _fsUtils; ArtifactUpdater get _artifactUpdater => __artifactUpdater ??= _createUpdater(); ArtifactUpdater __artifactUpdater; + @protected + void registerArtifact(ArtifactSet artifactSet) { + _artifacts.add(artifactSet); + } + /// This has to be lazy because it requires FLUTTER_ROOT to be initialized. ArtifactUpdater _createUpdater() { return ArtifactUpdater( @@ -199,16 +180,10 @@ class Cache { ); } - Net _net; - FileSystemUtils _fsUtils; - static const List _hostsBlockedInChina = [ 'storage.googleapis.com', ]; - final Directory _rootOverride; - final List _artifacts = []; - // Initialized by FlutterCommandRunner on startup. static String flutterRoot; @@ -718,149 +693,8 @@ abstract class CachedArtifact extends ArtifactSet { FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils, ); - - Uri _toStorageUri(String path) => Uri.parse('${cache.storageBaseUrl}/$path'); } -/// Ensures that the source files for all of the dependencies for the -/// flutter_tool are present. -/// -/// This does not handle cases where the source files are modified or the -/// directory contents are incomplete. -class PubDependencies extends ArtifactSet { - PubDependencies({ - // Needs to be lazy to avoid reading from the cache before the root is initialized. - @required String Function() flutterRoot, - @required Logger logger, - @required Pub Function() pub, - }) : _logger = logger, - _flutterRoot = flutterRoot, - _pub = pub, - super(DevelopmentArtifact.universal); - - final String Function() _flutterRoot; - final Logger _logger; - final Pub Function() _pub; - - @override - Future isUpToDate( - FileSystem fileSystem, - ) async { - final File toolPackageConfig = fileSystem.file( - fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools', '.dart_tool', 'package_config.json'), - ); - if (!toolPackageConfig.existsSync()) { - return false; - } - final PackageConfig packageConfig = await loadPackageConfigWithLogging( - toolPackageConfig, - logger: _logger, - throwOnError: false, - ); - if (packageConfig == null || packageConfig == PackageConfig.empty) { - return false; - } - for (final Package package in packageConfig.packages) { - if (!fileSystem.directory(package.packageUriRoot).existsSync()) { - return false; - } - } - return true; - } - - @override - String get name => 'pub_dependencies'; - - @override - Future update( - ArtifactUpdater artifactUpdater, - Logger logger, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - await _pub().get( - context: PubContext.pubGet, - directory: fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools'), - generateSyntheticPackage: false, - ); - } -} - -/// A cached artifact containing fonts used for Material Design. -class MaterialFonts extends CachedArtifact { - MaterialFonts(Cache cache) : super( - 'material_fonts', - cache, - DevelopmentArtifact.universal, - ); - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) { - final Uri archiveUri = _toStorageUri(version); - return artifactUpdater.downloadZipArchive('Downloading Material fonts...', archiveUri, location); - } -} - -/// A cached artifact containing the web dart:ui sources, platform dill files, -/// and libraries.json. -/// -/// 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( - 'flutter_web_sdk', - cache, - DevelopmentArtifact.web, - ); - - final Platform _platform; - - @override - Directory get location => cache.getWebSdkDirectory(); - - @override - String get version => cache.getVersionFor('engine'); - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - 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'); - 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 = fileSystem.path.split(entity.path); - segments.remove('flutter_web_sdk'); - final String newPath = fileSystem.path.joinAll(segments); - final File newFile = fileSystem.file(newPath); - if (!newFile.existsSync()) { - newFile.createSync(recursive: true); - } - entity.copySync(newPath); - } - } - } -} abstract class EngineCachedArtifact extends CachedArtifact { EngineCachedArtifact( @@ -984,718 +818,6 @@ abstract class EngineCachedArtifact extends CachedArtifact { } } -/// A cached artifact containing the dart:ui source code. -class FlutterSdk extends EngineCachedArtifact { - FlutterSdk(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super( - 'flutter_sdk', - cache, - DevelopmentArtifact.universal, - ); - - final Platform _platform; - - @override - List getPackageDirs() => const ['sky_engine']; - - @override - List> getBinaryDirs() { - // Currently only Linux supports both arm64 and x64. - final String arch = cache.getHostPlatformArchName(); - return >[ - ['common', 'flutter_patched_sdk.zip'], - ['common', 'flutter_patched_sdk_product.zip'], - if (cache.includeAllPlatforms) ...>[ - ['windows-x64', 'windows-x64/artifacts.zip'], - ['linux-$arch', 'linux-$arch/artifacts.zip'], - ['darwin-x64', 'darwin-x64/artifacts.zip'], - ] - else if (_platform.isWindows) - ['windows-x64', 'windows-x64/artifacts.zip'] - else if (_platform.isMacOS) - ['darwin-x64', 'darwin-x64/artifacts.zip'] - else if (_platform.isLinux) - ['linux-$arch', 'linux-$arch/artifacts.zip'], - ]; - } - - @override - List getLicenseDirs() => const []; -} - -class MacOSEngineArtifacts extends EngineCachedArtifact { - MacOSEngineArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super( - 'macos-sdk', - cache, - DevelopmentArtifact.macOS, - ); - - final Platform _platform; - - @override - List getPackageDirs() => const []; - - @override - List> getBinaryDirs() { - if (_platform.isMacOS || ignorePlatformFiltering) { - return _macOSDesktopBinaryDirs; - } - return const >[]; - } - - @override - List getLicenseDirs() => const []; -} - -/// Artifacts required for desktop Windows builds. -class WindowsEngineArtifacts extends EngineCachedArtifact { - WindowsEngineArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super( - 'windows-sdk', - cache, - DevelopmentArtifact.windows, - ); - - final Platform _platform; - - @override - List getPackageDirs() => const []; - - @override - List> getBinaryDirs() { - if (_platform.isWindows || ignorePlatformFiltering) { - return _windowsDesktopBinaryDirs; - } - return const >[]; - } - - @override - List getLicenseDirs() => const []; -} - -/// Artifacts required for desktop Linux builds. -class LinuxEngineArtifacts extends EngineCachedArtifact { - LinuxEngineArtifacts(Cache cache, { - @required Platform platform - }) : _platform = platform, - super( - 'linux-sdk', - cache, - DevelopmentArtifact.linux, - ); - - final Platform _platform; - - @override - List getPackageDirs() => const []; - - @override - List> getBinaryDirs() { - if (_platform.isLinux || ignorePlatformFiltering) { - final String arch = cache.getHostPlatformArchName(); - return >[ - ['linux-$arch', 'linux-$arch/linux-$arch-flutter-gtk.zip'], - ['linux-$arch-profile', 'linux-$arch-profile/linux-$arch-flutter-gtk.zip'], - ['linux-$arch-release', 'linux-$arch-release/linux-$arch-flutter-gtk.zip'], - ]; - } - return const >[]; - } - - @override - List getLicenseDirs() => const []; -} - -/// The artifact used to generate snapshots for Android builds. -class AndroidGenSnapshotArtifacts extends EngineCachedArtifact { - AndroidGenSnapshotArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super( - 'android-sdk', - cache, - DevelopmentArtifact.androidGenSnapshot, - ); - - final Platform _platform; - - @override - List getPackageDirs() => const []; - - @override - List> getBinaryDirs() { - return >[ - if (cache.includeAllPlatforms) ...>[ - ..._osxBinaryDirs, - ..._linuxBinaryDirs, - ..._windowsBinaryDirs, - ..._dartSdks, - ] else if (_platform.isWindows) - ..._windowsBinaryDirs - else if (_platform.isMacOS) - ..._osxBinaryDirs - else if (_platform.isLinux) - ..._linuxBinaryDirs, - ]; - } - - @override - List getLicenseDirs() { return []; } -} - -/// A cached artifact containing the Maven dependencies used to build Android projects. -/// -/// This is a no-op if the android SDK is not available. -class AndroidMavenArtifacts extends ArtifactSet { - AndroidMavenArtifacts(this.cache, { - @required Platform platform, - }) : _platform = platform, - super(DevelopmentArtifact.androidMaven); - - final Platform _platform; - final Cache cache; - - @override - Future update( - ArtifactUpdater artifactUpdater, - Logger logger, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - if (globals.androidSdk == null) { - return; - } - final Directory tempDir = cache.getRoot().createTempSync( - 'flutter_gradle_wrapper.', - ); - globals.gradleUtils.injectGradleWrapperIfNeeded(tempDir); - - final Status status = logger.startProgress('Downloading Android Maven dependencies...'); - final File gradle = tempDir.childFile( - _platform.isWindows ? 'gradlew.bat' : 'gradlew', - ); - try { - final String gradleExecutable = gradle.absolute.path; - final String flutterSdk = globals.fsUtils.escapePath(Cache.flutterRoot); - final RunResult processResult = await globals.processUtils.run( - [ - gradleExecutable, - '-b', globals.fs.path.join(flutterSdk, 'packages', 'flutter_tools', 'gradle', 'resolve_dependencies.gradle'), - '--project-cache-dir', tempDir.path, - 'resolveDependencies', - ], - environment: { - if (javaPath != null) - 'JAVA_HOME': javaPath, - }, - ); - if (processResult.exitCode != 0) { - logger.printError('Failed to download the Android dependencies'); - } - } finally { - status.stop(); - tempDir.deleteSync(recursive: true); - globals.androidSdk?.reinitialize(); - } - } - - @override - Future isUpToDate(FileSystem fileSystem) async { - // The dependencies are downloaded and cached by Gradle. - // The tool doesn't know if the dependencies are already cached at this point. - // Therefore, call Gradle to figure this out. - return false; - } - - @override - String get name => 'android-maven-artifacts'; -} - -/// Artifacts used for internal builds. The flutter tool builds Android projects -/// using the artifacts cached by [AndroidMavenArtifacts]. -class AndroidInternalBuildArtifacts extends EngineCachedArtifact { - AndroidInternalBuildArtifacts(Cache cache) : super( - 'android-internal-build-artifacts', - cache, - DevelopmentArtifact.androidInternalBuild, - ); - - @override - List getPackageDirs() => const []; - - @override - List> getBinaryDirs() { - return _androidBinaryDirs; - } - - @override - List getLicenseDirs() { return []; } -} - -class IOSEngineArtifacts extends EngineCachedArtifact { - IOSEngineArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super( - 'ios-sdk', - cache, - DevelopmentArtifact.iOS, - ); - - final Platform _platform; - - @override - List> getBinaryDirs() { - return >[ - if (_platform.isMacOS || ignorePlatformFiltering) - ..._iosBinaryDirs, - ]; - } - - @override - List getLicenseDirs() { - if (_platform.isMacOS || ignorePlatformFiltering) { - return const ['ios', 'ios-profile', 'ios-release']; - } - return const []; - } - - @override - List getPackageDirs() { - return []; - } -} - -/// A cached artifact containing Gradle Wrapper scripts and binaries. -/// -/// While this is only required for Android, we need to always download it due -/// the ensurePlatformSpecificTooling logic. -class GradleWrapper extends CachedArtifact { - GradleWrapper(Cache cache) : super( - 'gradle_wrapper', - cache, - DevelopmentArtifact.universal, - ); - - List get _gradleScripts => ['gradlew', 'gradlew.bat']; - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - final Uri archiveUri = _toStorageUri(version); - await artifactUpdater.downloadZippedTarball('Downloading Gradle Wrapper...', archiveUri, location); - // Delete property file, allowing templates to provide it. - // Remove NOTICE file. Should not be part of the template. - final File propertiesFile = fileSystem.file(fileSystem.path.join(location.path, 'gradle', 'wrapper', 'gradle-wrapper.properties')); - final File noticeFile = fileSystem.file(fileSystem.path.join(location.path, 'NOTICE')); - ErrorHandlingFileSystem.deleteIfExists(propertiesFile); - ErrorHandlingFileSystem.deleteIfExists(noticeFile); - } - - @override - bool isUpToDateInner( - FileSystem fileSystem, - ) { - final String gradleWrapper = fileSystem.path.join('gradle', 'wrapper', 'gradle-wrapper.jar'); - final Directory wrapperDir = cache.getCacheDir(fileSystem.path.join('artifacts', 'gradle_wrapper')); - if (!fileSystem.directory(wrapperDir).existsSync()) { - return false; - } - for (final String scriptName in _gradleScripts) { - final File scriptFile = fileSystem.file(fileSystem.path.join(wrapperDir.path, scriptName)); - if (!scriptFile.existsSync()) { - return false; - } - } - final File gradleWrapperJar = fileSystem.file(fileSystem.path.join(wrapperDir.path, gradleWrapper)); - if (!gradleWrapperJar.existsSync()) { - return false; - } - return true; - } -} - -const String _cipdBaseUrl = 'https://chrome-infra-packages.appspot.com/dl'; - -/// Common functionality for pulling Fuchsia SDKs. -abstract class _FuchsiaSDKArtifacts extends CachedArtifact { - _FuchsiaSDKArtifacts(Cache cache, String platform) : - _path = 'fuchsia/sdk/core/$platform-amd64', - super( - 'fuchsia-$platform', - cache, - DevelopmentArtifact.fuchsia, - ); - - final String _path; - - @override - Directory get location => cache.getArtifactDirectory('fuchsia'); - - Future _doUpdate(ArtifactUpdater artifactUpdater) { - final String url = '$_cipdBaseUrl/$_path/+/$version'; - return artifactUpdater.downloadZipArchive('Downloading package fuchsia SDK...', - Uri.parse(url), location); - } -} - -/// The pre-built flutter runner for Fuchsia development. -class FlutterRunnerSDKArtifacts extends CachedArtifact { - FlutterRunnerSDKArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super( - 'flutter_runner', - cache, - DevelopmentArtifact.flutterRunner, - ); - - final Platform _platform; - - @override - Directory get location => cache.getArtifactDirectory('flutter_runner'); - - @override - String get version => cache.getVersionFor('engine'); - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - if (!_platform.isLinux && !_platform.isMacOS) { - return; - } - final String url = '$_cipdBaseUrl/flutter/fuchsia/+/git_revision:$version'; - await artifactUpdater.downloadZipArchive('Downloading package flutter runner...', Uri.parse(url), location); - } -} - -/// Implementations of this class can resolve URLs for packages that are versioned. -/// -/// See also [CipdArchiveResolver]. -abstract class VersionedPackageResolver { - const VersionedPackageResolver(); - - /// Returns the URL for the artifact. - String resolveUrl(String packageName, String version); -} - -/// Resolves the CIPD archive URL for a given package and version. -class CipdArchiveResolver extends VersionedPackageResolver { - const CipdArchiveResolver(); - - @override - String resolveUrl(String packageName, String version) { - return '$_cipdBaseUrl/flutter/$packageName/+/git_revision:$version'; - } -} - -/// The debug symbols for flutter runner for Fuchsia development. -class FlutterRunnerDebugSymbols extends CachedArtifact { - FlutterRunnerDebugSymbols(Cache cache, { - @required Platform platform, - this.packageResolver = const CipdArchiveResolver(), - }) : _platform = platform, - super('flutter_runner_debug_symbols', cache, DevelopmentArtifact.flutterRunner); - - final VersionedPackageResolver packageResolver; - final Platform _platform; - - @override - Directory get location => cache.getArtifactDirectory(name); - - @override - String get version => cache.getVersionFor('engine'); - - Future _downloadDebugSymbols(String targetArch, ArtifactUpdater artifactUpdater) async { - final String packageName = 'fuchsia-debug-symbols-$targetArch'; - final String url = packageResolver.resolveUrl(packageName, version); - await artifactUpdater.downloadZipArchive( - 'Downloading debug symbols for flutter runner - arch:$targetArch...', - Uri.parse(url), - location, - ); - } - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - if (!_platform.isLinux && !_platform.isMacOS) { - return; - } - await _downloadDebugSymbols('x64', artifactUpdater); - await _downloadDebugSymbols('arm64', artifactUpdater); - } -} - -/// The Fuchsia core SDK for Linux. -class LinuxFuchsiaSDKArtifacts extends _FuchsiaSDKArtifacts { - LinuxFuchsiaSDKArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super(cache, 'linux'); - - final Platform _platform; - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - if (!_platform.isLinux) { - return; - } - return _doUpdate(artifactUpdater); - } -} - -/// The Fuchsia core SDK for MacOS. -class MacOSFuchsiaSDKArtifacts extends _FuchsiaSDKArtifacts { - MacOSFuchsiaSDKArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super(cache, 'mac'); - - final Platform _platform; - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - if (!_platform.isMacOS) { - return; - } - return _doUpdate(artifactUpdater); - } -} - -/// Cached artifacts for font subsetting. -class FontSubsetArtifacts extends EngineCachedArtifact { - FontSubsetArtifacts(Cache cache, { - @required Platform platform, - }) : _platform = platform, - super(artifactName, cache, DevelopmentArtifact.universal); - - final Platform _platform; - - static const String artifactName = 'font-subset'; - - @override - List> getBinaryDirs() { - // Currently only Linux supports both arm64 and x64. - final String arch = cache.getHostPlatformArchName(); - final Map> artifacts = > { - 'macos': ['darwin-x64', 'darwin-x64/$artifactName.zip'], - 'linux': ['linux-$arch', 'linux-$arch/$artifactName.zip'], - 'windows': ['windows-x64', 'windows-x64/$artifactName.zip'], - }; - if (cache.includeAllPlatforms) { - return artifacts.values.toList(); - } else { - final List binaryDirs = artifacts[_platform.operatingSystem]; - if (binaryDirs == null) { - throwToolExit('Unsupported operating system: ${_platform.operatingSystem}'); - } - return >[binaryDirs]; - } - } - - @override - List getLicenseDirs() => const []; - - @override - List getPackageDirs() => const []; -} - -/// Cached iOS/USB binary artifacts. -class IosUsbArtifacts extends CachedArtifact { - IosUsbArtifacts(String name, Cache cache, { - @required Platform platform, - }) : _platform = platform, - super( - name, - cache, - DevelopmentArtifact.universal, - ); - - final Platform _platform; - - static const List artifactNames = [ - 'libimobiledevice', - 'usbmuxd', - 'libplist', - 'openssl', - 'ios-deploy', - ]; - - // For unknown reasons, users are getting into bad states where libimobiledevice is - // downloaded but some executables are missing from the zip. The names here are - // used for additional download checks below, so we can re-download if they are - // missing. - static const Map> _kExecutables = >{ - 'libimobiledevice': [ - 'idevicescreenshot', - 'idevicesyslog', - ], - 'usbmuxd': [ - 'iproxy', - ], - }; - - @override - Map get environment { - return { - 'DYLD_LIBRARY_PATH': cache.getArtifactDirectory(name).path, - }; - } - - @override - bool isUpToDateInner(FileSystem fileSystem) { - final List executables =_kExecutables[name]; - if (executables == null) { - return true; - } - for (final String executable in executables) { - if (!location.childFile(executable).existsSync()) { - return false; - } - } - return true; - } - - @override - Future updateInner( - ArtifactUpdater artifactUpdater, - FileSystem fileSystem, - OperatingSystemUtils operatingSystemUtils, - ) async { - if (!_platform.isMacOS && !ignorePlatformFiltering) { - return; - } - if (location.existsSync()) { - location.deleteSync(recursive: true); - } - await artifactUpdater.downloadZipArchive('Downloading $name...', archiveUri, location); - } - - @visibleForTesting - Uri get archiveUri => Uri.parse('${cache.storageBaseUrl}/flutter_infra_release/ios-usb-dependencies${cache.useUnsignedMacBinaries ? '/unsigned' : ''}/$name/$version/$name.zip'); -} - -// Many characters are problematic in filenames, especially on Windows. -final Map> _flattenNameSubstitutions = >{ - r'@'.codeUnitAt(0): '@@'.codeUnits, - r'/'.codeUnitAt(0): '@s@'.codeUnits, - r'\'.codeUnitAt(0): '@bs@'.codeUnits, - r':'.codeUnitAt(0): '@c@'.codeUnits, - r'%'.codeUnitAt(0): '@per@'.codeUnits, - r'*'.codeUnitAt(0): '@ast@'.codeUnits, - r'<'.codeUnitAt(0): '@lt@'.codeUnits, - r'>'.codeUnitAt(0): '@gt@'.codeUnits, - r'"'.codeUnitAt(0): '@q@'.codeUnits, - r'|'.codeUnitAt(0): '@pip@'.codeUnits, - r'?'.codeUnitAt(0): '@ques@'.codeUnits, -}; - -/// Given a name containing slashes, colons, and backslashes, expand it into -/// something that doesn't. -String _flattenNameNoSubdirs(String fileName) { - final List replacedCodeUnits = [ - for (int codeUnit in fileName.codeUnits) - ..._flattenNameSubstitutions[codeUnit] ?? [codeUnit], - ]; - return String.fromCharCodes(replacedCodeUnits); -} - -// TODO(jonahwilliams): upload debug desktop artifacts to host-debug and -// remove from existing host folder. -// https://github.com/flutter/flutter/issues/38935 -const List> _windowsDesktopBinaryDirs = >[ - ['windows-x64', 'windows-x64/windows-x64-flutter.zip'], - ['windows-x64', 'windows-x64/flutter-cpp-client-wrapper.zip'], - ['windows-x64-profile', 'windows-x64-profile/windows-x64-flutter.zip'], - ['windows-x64-release', 'windows-x64-release/windows-x64-flutter.zip'], -]; - -const List> _macOSDesktopBinaryDirs = >[ - ['darwin-x64', 'darwin-x64/FlutterMacOS.framework.zip'], - ['darwin-x64-profile', 'darwin-x64-profile/FlutterMacOS.framework.zip'], - ['darwin-x64-profile', 'darwin-x64-profile/artifacts.zip'], - ['darwin-x64-release', 'darwin-x64-release/FlutterMacOS.framework.zip'], - ['darwin-x64-release', 'darwin-x64-release/artifacts.zip'], -]; - -const List> _osxBinaryDirs = >[ - ['android-arm-profile/darwin-x64', 'android-arm-profile/darwin-x64.zip'], - ['android-arm-release/darwin-x64', 'android-arm-release/darwin-x64.zip'], - ['android-arm64-profile/darwin-x64', 'android-arm64-profile/darwin-x64.zip'], - ['android-arm64-release/darwin-x64', 'android-arm64-release/darwin-x64.zip'], - ['android-x64-profile/darwin-x64', 'android-x64-profile/darwin-x64.zip'], - ['android-x64-release/darwin-x64', 'android-x64-release/darwin-x64.zip'], -]; - -const List> _linuxBinaryDirs = >[ - ['android-arm-profile/linux-x64', 'android-arm-profile/linux-x64.zip'], - ['android-arm-release/linux-x64', 'android-arm-release/linux-x64.zip'], - ['android-arm64-profile/linux-x64', 'android-arm64-profile/linux-x64.zip'], - ['android-arm64-release/linux-x64', 'android-arm64-release/linux-x64.zip'], - ['android-x64-profile/linux-x64', 'android-x64-profile/linux-x64.zip'], - ['android-x64-release/linux-x64', 'android-x64-release/linux-x64.zip'], -]; - -const List> _windowsBinaryDirs = >[ - ['android-arm-profile/windows-x64', 'android-arm-profile/windows-x64.zip'], - ['android-arm-release/windows-x64', 'android-arm-release/windows-x64.zip'], - ['android-arm64-profile/windows-x64', 'android-arm64-profile/windows-x64.zip'], - ['android-arm64-release/windows-x64', 'android-arm64-release/windows-x64.zip'], - ['android-x64-profile/windows-x64', 'android-x64-profile/windows-x64.zip'], - ['android-x64-release/windows-x64', 'android-x64-release/windows-x64.zip'], -]; - -const List> _iosBinaryDirs = >[ - ['ios', 'ios/artifacts.zip'], - ['ios-profile', 'ios-profile/artifacts.zip'], - ['ios-release', 'ios-release/artifacts.zip'], -]; - -const List> _androidBinaryDirs = >[ - ['android-x86', 'android-x86/artifacts.zip'], - ['android-x64', 'android-x64/artifacts.zip'], - ['android-arm', 'android-arm/artifacts.zip'], - ['android-arm-profile', 'android-arm-profile/artifacts.zip'], - ['android-arm-release', 'android-arm-release/artifacts.zip'], - ['android-arm64', 'android-arm64/artifacts.zip'], - ['android-arm64-profile', 'android-arm64-profile/artifacts.zip'], - ['android-arm64-release', 'android-arm64-release/artifacts.zip'], - ['android-x64-profile', 'android-x64-profile/artifacts.zip'], - ['android-x64-release', 'android-x64-release/artifacts.zip'], - ['android-x86-jit-release', 'android-x86-jit-release/artifacts.zip'], -]; - -const List> _dartSdks = > [ - ['darwin-x64', 'dart-sdk-darwin-x64.zip'], - ['linux-x64', 'dart-sdk-linux-x64.zip'], - ['windows-x64', 'dart-sdk-windows-x64.zip'], -]; - /// An API for downloading and un-archiving artifacts, such as engine binaries or /// additional source code. class ArtifactUpdater { @@ -1970,3 +1092,28 @@ String flattenNameSubdirs(Uri url, FileSystem fileSystem){ final Iterable convertedPieces = pieces.map(_flattenNameNoSubdirs); return fileSystem.path.joinAll(convertedPieces); } + +/// Given a name containing slashes, colons, and backslashes, expand it into +/// something that doesn't. +String _flattenNameNoSubdirs(String fileName) { + final List replacedCodeUnits = [ + for (int codeUnit in fileName.codeUnits) + ..._flattenNameSubstitutions[codeUnit] ?? [codeUnit], + ]; + return String.fromCharCodes(replacedCodeUnits); +} + +// Many characters are problematic in filenames, especially on Windows. +final Map> _flattenNameSubstitutions = >{ + r'@'.codeUnitAt(0): '@@'.codeUnits, + r'/'.codeUnitAt(0): '@s@'.codeUnits, + r'\'.codeUnitAt(0): '@bs@'.codeUnits, + r':'.codeUnitAt(0): '@c@'.codeUnits, + r'%'.codeUnitAt(0): '@per@'.codeUnits, + r'*'.codeUnitAt(0): '@ast@'.codeUnits, + r'<'.codeUnitAt(0): '@lt@'.codeUnits, + r'>'.codeUnitAt(0): '@gt@'.codeUnits, + r'"'.codeUnitAt(0): '@q@'.codeUnits, + r'|'.codeUnitAt(0): '@pip@'.codeUnits, + r'?'.codeUnitAt(0): '@ques@'.codeUnits, +}; diff --git a/packages/flutter_tools/lib/src/context_runner.dart b/packages/flutter_tools/lib/src/context_runner.dart index 141298d68a..5d306f6fa4 100644 --- a/packages/flutter_tools/lib/src/context_runner.dart +++ b/packages/flutter_tools/lib/src/context_runner.dart @@ -39,6 +39,7 @@ import 'doctor.dart'; import 'emulator.dart'; import 'features.dart'; import 'flutter_application_package.dart'; +import 'flutter_cache.dart'; import 'flutter_device_manager.dart'; import 'flutter_features.dart'; import 'fuchsia/fuchsia_device.dart' show FuchsiaDeviceTools; @@ -145,7 +146,7 @@ Future runInContext( logger: globals.logger, platform: globals.platform, ), - Cache: () => Cache( + Cache: () => FlutterCache( fileSystem: globals.fs, logger: globals.logger, platform: globals.platform, diff --git a/packages/flutter_tools/lib/src/flutter_cache.dart b/packages/flutter_tools/lib/src/flutter_cache.dart new file mode 100644 index 0000000000..3808af887a --- /dev/null +++ b/packages/flutter_tools/lib/src/flutter_cache.dart @@ -0,0 +1,894 @@ +// 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. + +// @dart = 2.8 + +import 'dart:async'; + +import 'package:meta/meta.dart'; +import 'package:package_config/package_config.dart'; + +import 'android/android_studio.dart'; +import 'base/common.dart'; +import 'base/error_handling_io.dart'; +import 'base/file_system.dart'; +import 'base/logger.dart'; +import 'base/os.dart' show OperatingSystemUtils; +import 'base/platform.dart'; +import 'base/process.dart'; +import 'cache.dart'; +import 'dart/package_map.dart'; +import 'dart/pub.dart'; +import 'globals.dart' as globals; +import 'runner/flutter_command.dart'; + +/// An implementation of the [Cache] which provides all of Flutter's default artifacts. +class FlutterCache extends Cache { + /// [rootOverride] is configurable for testing. + /// [artifacts] is configurable for testing. + FlutterCache({ + @required Logger logger, + @required FileSystem fileSystem, + @required Platform platform, + @required OperatingSystemUtils osUtils, + }) : super(logger: logger, fileSystem: fileSystem, platform: platform, osUtils: osUtils, artifacts: []) { + registerArtifact(MaterialFonts(this)); + registerArtifact(GradleWrapper(this)); + registerArtifact(AndroidGenSnapshotArtifacts(this, platform: platform)); + registerArtifact(AndroidInternalBuildArtifacts(this)); + registerArtifact(IOSEngineArtifacts(this, platform: platform)); + registerArtifact(FlutterWebSdk(this, platform: platform)); + registerArtifact(FlutterSdk(this, platform: platform)); + registerArtifact(WindowsEngineArtifacts(this, platform: platform)); + registerArtifact(MacOSEngineArtifacts(this, platform: platform)); + registerArtifact(LinuxEngineArtifacts(this, platform: platform)); + registerArtifact(LinuxFuchsiaSDKArtifacts(this, platform: platform)); + registerArtifact(MacOSFuchsiaSDKArtifacts(this, platform: platform)); + registerArtifact(FlutterRunnerSDKArtifacts(this, platform: platform)); + registerArtifact(FlutterRunnerDebugSymbols(this, platform: platform)); + for (final String artifactName in IosUsbArtifacts.artifactNames) { + registerArtifact(IosUsbArtifacts(artifactName, this, platform: platform)); + } + registerArtifact(FontSubsetArtifacts(this, platform: platform)); + registerArtifact(PubDependencies( + logger: logger, + // flutter root and pub must be lazily initialized to avoid accessing + // before the version is determined. + flutterRoot: () => Cache.flutterRoot, + pub: () => pub, + )); + } +} + + +/// Ensures that the source files for all of the dependencies for the +/// flutter_tool are present. +/// +/// This does not handle cases where the source files are modified or the +/// directory contents are incomplete. +class PubDependencies extends ArtifactSet { + PubDependencies({ + // Needs to be lazy to avoid reading from the cache before the root is initialized. + @required String Function() flutterRoot, + @required Logger logger, + @required Pub Function() pub, + }) : _logger = logger, + _flutterRoot = flutterRoot, + _pub = pub, + super(DevelopmentArtifact.universal); + + final String Function() _flutterRoot; + final Logger _logger; + final Pub Function() _pub; + + @override + Future isUpToDate( + FileSystem fileSystem, + ) async { + final File toolPackageConfig = fileSystem.file( + fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools', '.dart_tool', 'package_config.json'), + ); + if (!toolPackageConfig.existsSync()) { + return false; + } + final PackageConfig packageConfig = await loadPackageConfigWithLogging( + toolPackageConfig, + logger: _logger, + throwOnError: false, + ); + if (packageConfig == null || packageConfig == PackageConfig.empty) { + return false; + } + for (final Package package in packageConfig.packages) { + if (!fileSystem.directory(package.packageUriRoot).existsSync()) { + return false; + } + } + return true; + } + + @override + String get name => 'pub_dependencies'; + + @override + Future update( + ArtifactUpdater artifactUpdater, + Logger logger, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + await _pub().get( + context: PubContext.pubGet, + directory: fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools'), + generateSyntheticPackage: false, + ); + } +} + +/// A cached artifact containing fonts used for Material Design. +class MaterialFonts extends CachedArtifact { + MaterialFonts(Cache cache) : super( + 'material_fonts', + cache, + DevelopmentArtifact.universal, + ); + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) { + final Uri archiveUri = _toStorageUri(version); + return artifactUpdater.downloadZipArchive('Downloading Material fonts...', archiveUri, location); + } + + Uri _toStorageUri(String path) => Uri.parse('${cache.storageBaseUrl}/$path'); +} + +/// A cached artifact containing the web dart:ui sources, platform dill files, +/// and libraries.json. +/// +/// 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( + 'flutter_web_sdk', + cache, + DevelopmentArtifact.web, + ); + + final Platform _platform; + + @override + Directory get location => cache.getWebSdkDirectory(); + + @override + String get version => cache.getVersionFor('engine'); + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + 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'); + 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 = fileSystem.path.split(entity.path); + segments.remove('flutter_web_sdk'); + final String newPath = fileSystem.path.joinAll(segments); + final File newFile = fileSystem.file(newPath); + if (!newFile.existsSync()) { + newFile.createSync(recursive: true); + } + entity.copySync(newPath); + } + } + } +} + +/// A cached artifact containing the dart:ui source code. +class FlutterSdk extends EngineCachedArtifact { + FlutterSdk(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super( + 'flutter_sdk', + cache, + DevelopmentArtifact.universal, + ); + + final Platform _platform; + + @override + List getPackageDirs() => const ['sky_engine']; + + @override + List> getBinaryDirs() { + // Currently only Linux supports both arm64 and x64. + final String arch = cache.getHostPlatformArchName(); + return >[ + ['common', 'flutter_patched_sdk.zip'], + ['common', 'flutter_patched_sdk_product.zip'], + if (cache.includeAllPlatforms) ...>[ + ['windows-x64', 'windows-x64/artifacts.zip'], + ['linux-$arch', 'linux-$arch/artifacts.zip'], + ['darwin-x64', 'darwin-x64/artifacts.zip'], + ] + else if (_platform.isWindows) + ['windows-x64', 'windows-x64/artifacts.zip'] + else if (_platform.isMacOS) + ['darwin-x64', 'darwin-x64/artifacts.zip'] + else if (_platform.isLinux) + ['linux-$arch', 'linux-$arch/artifacts.zip'], + ]; + } + + @override + List getLicenseDirs() => const []; +} + +class MacOSEngineArtifacts extends EngineCachedArtifact { + MacOSEngineArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super( + 'macos-sdk', + cache, + DevelopmentArtifact.macOS, + ); + + final Platform _platform; + + @override + List getPackageDirs() => const []; + + @override + List> getBinaryDirs() { + if (_platform.isMacOS || ignorePlatformFiltering) { + return _macOSDesktopBinaryDirs; + } + return const >[]; + } + + @override + List getLicenseDirs() => const []; +} + +/// Artifacts required for desktop Windows builds. +class WindowsEngineArtifacts extends EngineCachedArtifact { + WindowsEngineArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super( + 'windows-sdk', + cache, + DevelopmentArtifact.windows, + ); + + final Platform _platform; + + @override + List getPackageDirs() => const []; + + @override + List> getBinaryDirs() { + if (_platform.isWindows || ignorePlatformFiltering) { + return _windowsDesktopBinaryDirs; + } + return const >[]; + } + + @override + List getLicenseDirs() => const []; +} + +/// Artifacts required for desktop Linux builds. +class LinuxEngineArtifacts extends EngineCachedArtifact { + LinuxEngineArtifacts(Cache cache, { + @required Platform platform + }) : _platform = platform, + super( + 'linux-sdk', + cache, + DevelopmentArtifact.linux, + ); + + final Platform _platform; + + @override + List getPackageDirs() => const []; + + @override + List> getBinaryDirs() { + if (_platform.isLinux || ignorePlatformFiltering) { + final String arch = cache.getHostPlatformArchName(); + return >[ + ['linux-$arch', 'linux-$arch/linux-$arch-flutter-gtk.zip'], + ['linux-$arch-profile', 'linux-$arch-profile/linux-$arch-flutter-gtk.zip'], + ['linux-$arch-release', 'linux-$arch-release/linux-$arch-flutter-gtk.zip'], + ]; + } + return const >[]; + } + + @override + List getLicenseDirs() => const []; +} + +/// The artifact used to generate snapshots for Android builds. +class AndroidGenSnapshotArtifacts extends EngineCachedArtifact { + AndroidGenSnapshotArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super( + 'android-sdk', + cache, + DevelopmentArtifact.androidGenSnapshot, + ); + + final Platform _platform; + + @override + List getPackageDirs() => const []; + + @override + List> getBinaryDirs() { + return >[ + if (cache.includeAllPlatforms) ...>[ + ..._osxBinaryDirs, + ..._linuxBinaryDirs, + ..._windowsBinaryDirs, + ..._dartSdks, + ] else if (_platform.isWindows) + ..._windowsBinaryDirs + else if (_platform.isMacOS) + ..._osxBinaryDirs + else if (_platform.isLinux) + ..._linuxBinaryDirs, + ]; + } + + @override + List getLicenseDirs() { return []; } +} + +/// A cached artifact containing the Maven dependencies used to build Android projects. +/// +/// This is a no-op if the android SDK is not available. +class AndroidMavenArtifacts extends ArtifactSet { + AndroidMavenArtifacts(this.cache, { + @required Platform platform, + }) : _platform = platform, + super(DevelopmentArtifact.androidMaven); + + final Platform _platform; + final Cache cache; + + @override + Future update( + ArtifactUpdater artifactUpdater, + Logger logger, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + if (globals.androidSdk == null) { + return; + } + final Directory tempDir = cache.getRoot().createTempSync( + 'flutter_gradle_wrapper.', + ); + globals.gradleUtils.injectGradleWrapperIfNeeded(tempDir); + + final Status status = logger.startProgress('Downloading Android Maven dependencies...'); + final File gradle = tempDir.childFile( + _platform.isWindows ? 'gradlew.bat' : 'gradlew', + ); + try { + final String gradleExecutable = gradle.absolute.path; + final String flutterSdk = globals.fsUtils.escapePath(Cache.flutterRoot); + final RunResult processResult = await globals.processUtils.run( + [ + gradleExecutable, + '-b', globals.fs.path.join(flutterSdk, 'packages', 'flutter_tools', 'gradle', 'resolve_dependencies.gradle'), + '--project-cache-dir', tempDir.path, + 'resolveDependencies', + ], + environment: { + if (javaPath != null) + 'JAVA_HOME': javaPath, + }, + ); + if (processResult.exitCode != 0) { + logger.printError('Failed to download the Android dependencies'); + } + } finally { + status.stop(); + tempDir.deleteSync(recursive: true); + globals.androidSdk?.reinitialize(); + } + } + + @override + Future isUpToDate(FileSystem fileSystem) async { + // The dependencies are downloaded and cached by Gradle. + // The tool doesn't know if the dependencies are already cached at this point. + // Therefore, call Gradle to figure this out. + return false; + } + + @override + String get name => 'android-maven-artifacts'; +} + +/// Artifacts used for internal builds. The flutter tool builds Android projects +/// using the artifacts cached by [AndroidMavenArtifacts]. +class AndroidInternalBuildArtifacts extends EngineCachedArtifact { + AndroidInternalBuildArtifacts(Cache cache) : super( + 'android-internal-build-artifacts', + cache, + DevelopmentArtifact.androidInternalBuild, + ); + + @override + List getPackageDirs() => const []; + + @override + List> getBinaryDirs() { + return _androidBinaryDirs; + } + + @override + List getLicenseDirs() { return []; } +} + +class IOSEngineArtifacts extends EngineCachedArtifact { + IOSEngineArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super( + 'ios-sdk', + cache, + DevelopmentArtifact.iOS, + ); + + final Platform _platform; + + @override + List> getBinaryDirs() { + return >[ + if (_platform.isMacOS || ignorePlatformFiltering) + ..._iosBinaryDirs, + ]; + } + + @override + List getLicenseDirs() { + if (_platform.isMacOS || ignorePlatformFiltering) { + return const ['ios', 'ios-profile', 'ios-release']; + } + return const []; + } + + @override + List getPackageDirs() { + return []; + } +} + +/// A cached artifact containing Gradle Wrapper scripts and binaries. +/// +/// While this is only required for Android, we need to always download it due +/// the ensurePlatformSpecificTooling logic. +class GradleWrapper extends CachedArtifact { + GradleWrapper(Cache cache) : super( + 'gradle_wrapper', + cache, + DevelopmentArtifact.universal, + ); + + List get _gradleScripts => ['gradlew', 'gradlew.bat']; + + Uri _toStorageUri(String path) => Uri.parse('${cache.storageBaseUrl}/$path'); + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + final Uri archiveUri = _toStorageUri(version); + await artifactUpdater.downloadZippedTarball('Downloading Gradle Wrapper...', archiveUri, location); + // Delete property file, allowing templates to provide it. + // Remove NOTICE file. Should not be part of the template. + final File propertiesFile = fileSystem.file(fileSystem.path.join(location.path, 'gradle', 'wrapper', 'gradle-wrapper.properties')); + final File noticeFile = fileSystem.file(fileSystem.path.join(location.path, 'NOTICE')); + ErrorHandlingFileSystem.deleteIfExists(propertiesFile); + ErrorHandlingFileSystem.deleteIfExists(noticeFile); + } + + @override + bool isUpToDateInner( + FileSystem fileSystem, + ) { + final String gradleWrapper = fileSystem.path.join('gradle', 'wrapper', 'gradle-wrapper.jar'); + final Directory wrapperDir = cache.getCacheDir(fileSystem.path.join('artifacts', 'gradle_wrapper')); + if (!fileSystem.directory(wrapperDir).existsSync()) { + return false; + } + for (final String scriptName in _gradleScripts) { + final File scriptFile = fileSystem.file(fileSystem.path.join(wrapperDir.path, scriptName)); + if (!scriptFile.existsSync()) { + return false; + } + } + final File gradleWrapperJar = fileSystem.file(fileSystem.path.join(wrapperDir.path, gradleWrapper)); + if (!gradleWrapperJar.existsSync()) { + return false; + } + return true; + } +} + +const String _cipdBaseUrl = 'https://chrome-infra-packages.appspot.com/dl'; + +/// Common functionality for pulling Fuchsia SDKs. +abstract class _FuchsiaSDKArtifacts extends CachedArtifact { + _FuchsiaSDKArtifacts(Cache cache, String platform) : + _path = 'fuchsia/sdk/core/$platform-amd64', + super( + 'fuchsia-$platform', + cache, + DevelopmentArtifact.fuchsia, + ); + + final String _path; + + @override + Directory get location => cache.getArtifactDirectory('fuchsia'); + + Future _doUpdate(ArtifactUpdater artifactUpdater) { + final String url = '$_cipdBaseUrl/$_path/+/$version'; + return artifactUpdater.downloadZipArchive('Downloading package fuchsia SDK...', + Uri.parse(url), location); + } +} + +/// The pre-built flutter runner for Fuchsia development. +class FlutterRunnerSDKArtifacts extends CachedArtifact { + FlutterRunnerSDKArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super( + 'flutter_runner', + cache, + DevelopmentArtifact.flutterRunner, + ); + + final Platform _platform; + + @override + Directory get location => cache.getArtifactDirectory('flutter_runner'); + + @override + String get version => cache.getVersionFor('engine'); + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + if (!_platform.isLinux && !_platform.isMacOS) { + return; + } + final String url = '$_cipdBaseUrl/flutter/fuchsia/+/git_revision:$version'; + await artifactUpdater.downloadZipArchive('Downloading package flutter runner...', Uri.parse(url), location); + } +} + +/// Implementations of this class can resolve URLs for packages that are versioned. +/// +/// See also [CipdArchiveResolver]. +abstract class VersionedPackageResolver { + const VersionedPackageResolver(); + + /// Returns the URL for the artifact. + String resolveUrl(String packageName, String version); +} + +/// Resolves the CIPD archive URL for a given package and version. +class CipdArchiveResolver extends VersionedPackageResolver { + const CipdArchiveResolver(); + + @override + String resolveUrl(String packageName, String version) { + return '$_cipdBaseUrl/flutter/$packageName/+/git_revision:$version'; + } +} + +/// The debug symbols for flutter runner for Fuchsia development. +class FlutterRunnerDebugSymbols extends CachedArtifact { + FlutterRunnerDebugSymbols(Cache cache, { + @required Platform platform, + this.packageResolver = const CipdArchiveResolver(), + }) : _platform = platform, + super('flutter_runner_debug_symbols', cache, DevelopmentArtifact.flutterRunner); + + final VersionedPackageResolver packageResolver; + final Platform _platform; + + @override + Directory get location => cache.getArtifactDirectory(name); + + @override + String get version => cache.getVersionFor('engine'); + + Future _downloadDebugSymbols(String targetArch, ArtifactUpdater artifactUpdater) async { + final String packageName = 'fuchsia-debug-symbols-$targetArch'; + final String url = packageResolver.resolveUrl(packageName, version); + await artifactUpdater.downloadZipArchive( + 'Downloading debug symbols for flutter runner - arch:$targetArch...', + Uri.parse(url), + location, + ); + } + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + if (!_platform.isLinux && !_platform.isMacOS) { + return; + } + await _downloadDebugSymbols('x64', artifactUpdater); + await _downloadDebugSymbols('arm64', artifactUpdater); + } +} + +/// The Fuchsia core SDK for Linux. +class LinuxFuchsiaSDKArtifacts extends _FuchsiaSDKArtifacts { + LinuxFuchsiaSDKArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super(cache, 'linux'); + + final Platform _platform; + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + if (!_platform.isLinux) { + return; + } + return _doUpdate(artifactUpdater); + } +} + +/// The Fuchsia core SDK for MacOS. +class MacOSFuchsiaSDKArtifacts extends _FuchsiaSDKArtifacts { + MacOSFuchsiaSDKArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super(cache, 'mac'); + + final Platform _platform; + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + if (!_platform.isMacOS) { + return; + } + return _doUpdate(artifactUpdater); + } +} + +/// Cached artifacts for font subsetting. +class FontSubsetArtifacts extends EngineCachedArtifact { + FontSubsetArtifacts(Cache cache, { + @required Platform platform, + }) : _platform = platform, + super(artifactName, cache, DevelopmentArtifact.universal); + + final Platform _platform; + + static const String artifactName = 'font-subset'; + + @override + List> getBinaryDirs() { + // Currently only Linux supports both arm64 and x64. + final String arch = cache.getHostPlatformArchName(); + final Map> artifacts = > { + 'macos': ['darwin-x64', 'darwin-x64/$artifactName.zip'], + 'linux': ['linux-$arch', 'linux-$arch/$artifactName.zip'], + 'windows': ['windows-x64', 'windows-x64/$artifactName.zip'], + }; + if (cache.includeAllPlatforms) { + return artifacts.values.toList(); + } else { + final List binaryDirs = artifacts[_platform.operatingSystem]; + if (binaryDirs == null) { + throwToolExit('Unsupported operating system: ${_platform.operatingSystem}'); + } + return >[binaryDirs]; + } + } + + @override + List getLicenseDirs() => const []; + + @override + List getPackageDirs() => const []; +} + +/// Cached iOS/USB binary artifacts. +class IosUsbArtifacts extends CachedArtifact { + IosUsbArtifacts(String name, Cache cache, { + @required Platform platform, + }) : _platform = platform, + super( + name, + cache, + DevelopmentArtifact.universal, + ); + + final Platform _platform; + + static const List artifactNames = [ + 'libimobiledevice', + 'usbmuxd', + 'libplist', + 'openssl', + 'ios-deploy', + ]; + + // For unknown reasons, users are getting into bad states where libimobiledevice is + // downloaded but some executables are missing from the zip. The names here are + // used for additional download checks below, so we can re-download if they are + // missing. + static const Map> _kExecutables = >{ + 'libimobiledevice': [ + 'idevicescreenshot', + 'idevicesyslog', + ], + 'usbmuxd': [ + 'iproxy', + ], + }; + + @override + Map get environment { + return { + 'DYLD_LIBRARY_PATH': cache.getArtifactDirectory(name).path, + }; + } + + @override + bool isUpToDateInner(FileSystem fileSystem) { + final List executables =_kExecutables[name]; + if (executables == null) { + return true; + } + for (final String executable in executables) { + if (!location.childFile(executable).existsSync()) { + return false; + } + } + return true; + } + + @override + Future updateInner( + ArtifactUpdater artifactUpdater, + FileSystem fileSystem, + OperatingSystemUtils operatingSystemUtils, + ) async { + if (!_platform.isMacOS && !ignorePlatformFiltering) { + return; + } + if (location.existsSync()) { + location.deleteSync(recursive: true); + } + await artifactUpdater.downloadZipArchive('Downloading $name...', archiveUri, location); + } + + @visibleForTesting + Uri get archiveUri => Uri.parse('${cache.storageBaseUrl}/flutter_infra_release/ios-usb-dependencies${cache.useUnsignedMacBinaries ? '/unsigned' : ''}/$name/$version/$name.zip'); +} + +// TODO(jonahwilliams): upload debug desktop artifacts to host-debug and +// remove from existing host folder. +// https://github.com/flutter/flutter/issues/38935 +const List> _windowsDesktopBinaryDirs = >[ + ['windows-x64', 'windows-x64/windows-x64-flutter.zip'], + ['windows-x64', 'windows-x64/flutter-cpp-client-wrapper.zip'], + ['windows-x64-profile', 'windows-x64-profile/windows-x64-flutter.zip'], + ['windows-x64-release', 'windows-x64-release/windows-x64-flutter.zip'], +]; + +const List> _macOSDesktopBinaryDirs = >[ + ['darwin-x64', 'darwin-x64/FlutterMacOS.framework.zip'], + ['darwin-x64-profile', 'darwin-x64-profile/FlutterMacOS.framework.zip'], + ['darwin-x64-profile', 'darwin-x64-profile/artifacts.zip'], + ['darwin-x64-release', 'darwin-x64-release/FlutterMacOS.framework.zip'], + ['darwin-x64-release', 'darwin-x64-release/artifacts.zip'], +]; + +const List> _osxBinaryDirs = >[ + ['android-arm-profile/darwin-x64', 'android-arm-profile/darwin-x64.zip'], + ['android-arm-release/darwin-x64', 'android-arm-release/darwin-x64.zip'], + ['android-arm64-profile/darwin-x64', 'android-arm64-profile/darwin-x64.zip'], + ['android-arm64-release/darwin-x64', 'android-arm64-release/darwin-x64.zip'], + ['android-x64-profile/darwin-x64', 'android-x64-profile/darwin-x64.zip'], + ['android-x64-release/darwin-x64', 'android-x64-release/darwin-x64.zip'], +]; + +const List> _linuxBinaryDirs = >[ + ['android-arm-profile/linux-x64', 'android-arm-profile/linux-x64.zip'], + ['android-arm-release/linux-x64', 'android-arm-release/linux-x64.zip'], + ['android-arm64-profile/linux-x64', 'android-arm64-profile/linux-x64.zip'], + ['android-arm64-release/linux-x64', 'android-arm64-release/linux-x64.zip'], + ['android-x64-profile/linux-x64', 'android-x64-profile/linux-x64.zip'], + ['android-x64-release/linux-x64', 'android-x64-release/linux-x64.zip'], +]; + +const List> _windowsBinaryDirs = >[ + ['android-arm-profile/windows-x64', 'android-arm-profile/windows-x64.zip'], + ['android-arm-release/windows-x64', 'android-arm-release/windows-x64.zip'], + ['android-arm64-profile/windows-x64', 'android-arm64-profile/windows-x64.zip'], + ['android-arm64-release/windows-x64', 'android-arm64-release/windows-x64.zip'], + ['android-x64-profile/windows-x64', 'android-x64-profile/windows-x64.zip'], + ['android-x64-release/windows-x64', 'android-x64-release/windows-x64.zip'], +]; + +const List> _iosBinaryDirs = >[ + ['ios', 'ios/artifacts.zip'], + ['ios-profile', 'ios-profile/artifacts.zip'], + ['ios-release', 'ios-release/artifacts.zip'], +]; + +const List> _androidBinaryDirs = >[ + ['android-x86', 'android-x86/artifacts.zip'], + ['android-x64', 'android-x64/artifacts.zip'], + ['android-arm', 'android-arm/artifacts.zip'], + ['android-arm-profile', 'android-arm-profile/artifacts.zip'], + ['android-arm-release', 'android-arm-release/artifacts.zip'], + ['android-arm64', 'android-arm64/artifacts.zip'], + ['android-arm64-profile', 'android-arm64-profile/artifacts.zip'], + ['android-arm64-release', 'android-arm64-release/artifacts.zip'], + ['android-x64-profile', 'android-x64-profile/artifacts.zip'], + ['android-x64-release', 'android-x64-release/artifacts.zip'], + ['android-x86-jit-release', 'android-x86-jit-release/artifacts.zip'], +]; + +const List> _dartSdks = > [ + ['darwin-x64', 'dart-sdk-darwin-x64.zip'], + ['linux-x64', 'dart-sdk-linux-x64.zip'], + ['windows-x64', 'dart-sdk-windows-x64.zip'], +]; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart index abaf70b7cb..550a75bc65 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart @@ -22,12 +22,6 @@ import '../globals.dart' as globals; import '../tester/flutter_tester.dart'; import '../web/web_device.dart'; -const String kFlutterRootEnvironmentVariableName = 'FLUTTER_ROOT'; // should point to //flutter/ (root of flutter/flutter repo) -const String kFlutterEngineEnvironmentVariableName = 'FLUTTER_ENGINE'; // should point to //engine/src/ (root of flutter/engine repo) -const String kSnapshotFileName = 'flutter_tools.snapshot'; // in //flutter/bin/cache/ -const String kFlutterToolsScriptFileName = 'flutter_tools.dart'; // in //flutter/packages/flutter_tools/bin/ -const String kFlutterEnginePackageName = 'sky_engine'; - class FlutterCommandRunner extends CommandRunner { FlutterCommandRunner({ bool verboseHelp = false }) : super( 'flutter', diff --git a/packages/flutter_tools/lib/src/runner/local_engine.dart b/packages/flutter_tools/lib/src/runner/local_engine.dart index 80f9160bbd..c51138ccb6 100644 --- a/packages/flutter_tools/lib/src/runner/local_engine.dart +++ b/packages/flutter_tools/lib/src/runner/local_engine.dart @@ -13,8 +13,8 @@ import '../base/file_system.dart'; import '../base/logger.dart'; import '../base/platform.dart'; import '../base/user_messages.dart' hide userMessages; +import '../cache.dart'; import '../dart/package_map.dart'; -import 'flutter_command_runner.dart'; /// A strategy for locating the out/ directory of a local engine build. /// diff --git a/packages/flutter_tools/test/general.shard/artifacts_test.dart b/packages/flutter_tools/test/general.shard/artifacts_test.dart index 1486fbaa1d..0a0502d8ff 100644 --- a/packages/flutter_tools/test/general.shard/artifacts_test.dart +++ b/packages/flutter_tools/test/general.shard/artifacts_test.dart @@ -33,6 +33,7 @@ void main() { platform: platform, logger: BufferLogger.test(), osUtils: FakeOperatingSystemUtils(), + artifacts: [], ); artifacts = CachedArtifacts( fileSystem: fileSystem, @@ -187,6 +188,7 @@ void main() { platform: platform, logger: BufferLogger.test(), osUtils: FakeOperatingSystemUtils(), + artifacts: [], ); artifacts = LocalEngineArtifacts( fileSystem.path.join(fileSystem.currentDirectory.path, 'out', 'android_debug_unopt'), diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index 369c1a704e..d8b10e91ce 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -16,6 +16,7 @@ import 'package:flutter_tools/src/base/os.dart'; import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/dart/pub.dart'; +import 'package:flutter_tools/src/flutter_cache.dart'; import 'package:meta/meta.dart'; import 'package:mockito/mockito.dart'; diff --git a/packages/flutter_tools/test/general.shard/project_test.dart b/packages/flutter_tools/test/general.shard/project_test.dart index e34c3a2e24..6736e7ccd8 100644 --- a/packages/flutter_tools/test/general.shard/project_test.dart +++ b/packages/flutter_tools/test/general.shard/project_test.dart @@ -814,6 +814,7 @@ void _testInMemory(String description, Future Function() testMethod) { transfer(Cache( fileSystem: globals.fs, logger: logger, + artifacts: [], osUtils: OperatingSystemUtils( fileSystem: globals.fs, logger: logger, @@ -858,6 +859,7 @@ void _testInMemory(String description, Future Function() testMethod) { fileSystem: testFileSystem, osUtils: globals.os, platform: globals.platform, + artifacts: [], ), FlutterProjectFactory: () => FlutterProjectFactory( fileSystem: testFileSystem,