Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download font-subset #49234

Merged
merged 2 commits into from Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions packages/flutter_tools/lib/src/artifacts.dart
Expand Up @@ -62,17 +62,19 @@ enum Artifact {
// Fuchsia artifacts from the engine prebuilts.
fuchsiaKernelCompiler,
fuchsiaFlutterRunner,

/// Tools related to subsetting or icon font files.
fontSubset,
constFinder,
}

String _artifactToFileName(Artifact artifact, [ TargetPlatform platform, BuildMode mode ]) {
final String exe = platform == TargetPlatform.windows_x64 ? '.exe' : '';
switch (artifact) {
case Artifact.genSnapshot:
return 'gen_snapshot';
case Artifact.flutterTester:
if (platform == TargetPlatform.windows_x64) {
return 'flutter_tester.exe';
}
return 'flutter_tester';
return 'flutter_tester$exe';
case Artifact.snapshotDart:
return 'snapshot.dart';
case Artifact.flutterFramework:
Expand All @@ -98,10 +100,7 @@ String _artifactToFileName(Artifact artifact, [ TargetPlatform platform, BuildMo
case Artifact.frontendServerSnapshotForEngineDartSdk:
return 'frontend_server.dart.snapshot';
case Artifact.engineDartBinary:
if (platform == TargetPlatform.windows_x64) {
return 'dart.exe';
}
return 'dart';
return 'dart$exe';
case Artifact.dart2jsSnapshot:
return 'dart2js.dart.snapshot';
case Artifact.dartdevcSnapshot:
Expand Down Expand Up @@ -140,6 +139,10 @@ String _artifactToFileName(Artifact artifact, [ TargetPlatform platform, BuildMo
final String jitOrAot = mode.isJit ? '_jit' : '_aot';
final String productOrNo = mode.isRelease ? '_product' : '';
return 'flutter$jitOrAot${productOrNo}_runner-0.far';
case Artifact.fontSubset:
return 'font-subset$exe';
case Artifact.constFinder:
return 'const_finder.dart.snapshot';
}
assert(false, 'Invalid artifact $artifact.');
return null;
Expand Down Expand Up @@ -366,6 +369,9 @@ class CachedArtifacts extends Artifacts {
case Artifact.skyEnginePath:
final Directory dartPackageDirectory = _cache.getCacheDir('pkg');
return _fileSystem.path.join(dartPackageDirectory.path, _artifactToFileName(artifact));
case Artifact.fontSubset:
case Artifact.constFinder:
return _cache.getArtifactDirectory('font-subset').childFile(_artifactToFileName(artifact, platform, mode)).path;
default:
assert(false, 'Artifact $artifact not available for platform $platform.');
return null;
Expand Down Expand Up @@ -538,6 +544,10 @@ class LocalEngineArtifacts extends Artifacts {
final String jitOrAot = mode.isJit ? '_jit' : '_aot';
final String productOrNo = mode.isRelease ? '_product' : '';
return _fileSystem.path.join(engineOutPath, 'flutter$jitOrAot${productOrNo}_runner-0.far');
case Artifact.fontSubset:
return _fileSystem.path.join(_hostEngineOutPath, artifactFileName);
case Artifact.constFinder:
return _fileSystem.path.join(_hostEngineOutPath, 'gen', artifactFileName);
}
assert(false, 'Invalid artifact $artifact.');
return null;
Expand Down
33 changes: 32 additions & 1 deletion packages/flutter_tools/lib/src/cache.dart
Expand Up @@ -124,6 +124,7 @@ class Cache {
for (final String artifactName in IosUsbArtifacts.artifactNames) {
_artifacts.add(IosUsbArtifacts(artifactName, this));
}
_artifacts.add(FontSubsetArtifacts(this));
} else {
_artifacts.addAll(artifacts);
}
Expand Down Expand Up @@ -709,7 +710,10 @@ abstract class EngineCachedArtifact extends CachedArtifact {
final String cacheDir = toolsDir[0];
final String urlPath = toolsDir[1];
final Directory dir = globals.fs.directory(globals.fs.path.join(location.path, cacheDir));
await _downloadZipArchive('Downloading $cacheDir tools...', Uri.parse(url + urlPath), dir);

// Avoid printing things like 'Downloading linux-x64 tools...' multiple times.
final String friendlyName = urlPath.replaceAll('/artifacts.zip', '').replaceAll('.zip', '');
await _downloadZipArchive('Downloading $friendlyName tools...', Uri.parse(url + urlPath), dir);

_makeFilesExecutable(dir);

Expand Down Expand Up @@ -1176,6 +1180,33 @@ class MacOSFuchsiaSDKArtifacts extends _FuchsiaSDKArtifacts {
}
}

/// Cached artifacts for font subsetting.
class FontSubsetArtifacts extends EngineCachedArtifact {
FontSubsetArtifacts(Cache cache) : super(artifactName, cache, DevelopmentArtifact.universal);

static const String artifactName = 'font-subset';

@override
List<List<String>> getBinaryDirs() {
const Map<String, List<String>> artifacts = <String, List<String>> {
'macos': <String>['darwin-x64', 'darwin-x64/$artifactName.zip'],
'linux': <String>['linux-x64', 'linux-x64/$artifactName.zip'],
'windows': <String>['windows-x64', 'windows-x64/$artifactName.zip'],
};
final List<String> binaryDirs = artifacts[globals.platform.operatingSystem];
if (binaryDirs == null) {
throwToolExit('Unsupported operating system: ${globals.platform.operatingSystem}');
}
return <List<String>>[binaryDirs];
}

@override
List<String> getLicenseDirs() => const <String>[];

@override
List<String> getPackageDirs() => const <String>[];
}

/// Cached iOS/USB binary artifacts.
class IosUsbArtifacts extends CachedArtifact {
IosUsbArtifacts(String name, Cache cache) : super(
Expand Down
87 changes: 86 additions & 1 deletion packages/flutter_tools/test/general.shard/cache_test.dart
Expand Up @@ -278,17 +278,19 @@ void main() {
MemoryFileSystem memoryFileSystem;
MockCache mockCache;
MockOperatingSystemUtils mockOperatingSystemUtils;
MockHttpClient mockHttpClient;

setUp(() {
fakeHttpClient = FakeHttpClient();
mockHttpClient = MockHttpClient();
fakePlatform = FakePlatform()..environment = const <String, String>{};
memoryFileSystem = MemoryFileSystem();
mockCache = MockCache();
mockOperatingSystemUtils = MockOperatingSystemUtils();
when(mockOperatingSystemUtils.verifyZip(any)).thenReturn(true);
});

testUsingContext('makes binary dirs readable and executable by all', () async {
when(mockOperatingSystemUtils.verifyZip(any)).thenReturn(true);
final Directory artifactDir = globals.fs.systemTempDirectory.createTempSync('flutter_cache_test_artifact.');
final Directory downloadDir = globals.fs.systemTempDirectory.createTempSync('flutter_cache_test_download.');
when(mockCache.getArtifactDirectory(any)).thenReturn(artifactDir);
Expand Down Expand Up @@ -316,6 +318,47 @@ void main() {
OperatingSystemUtils: () => mockOperatingSystemUtils,
Platform: () => fakePlatform,
});

testUsingContext('prints a friendly name when downloading', () async {
when(mockOperatingSystemUtils.verifyZip(any)).thenReturn(false);
final MockHttpClientRequest httpClientRequest = MockHttpClientRequest();
final MockHttpClientResponse httpClientResponse = MockHttpClientResponse();
when(httpClientResponse.statusCode).thenReturn(200);

when(httpClientRequest.close()).thenAnswer((_) async => httpClientResponse);
when(mockHttpClient.getUrl(any)).thenAnswer((_) async => httpClientRequest);

final Directory artifactDir = globals.fs.systemTempDirectory.createTempSync('flutter_cache_test_artifact.');
final Directory downloadDir = globals.fs.systemTempDirectory.createTempSync('flutter_cache_test_download.');
when(mockCache.getArtifactDirectory(any)).thenReturn(artifactDir);
when(mockCache.getDownloadDir()).thenReturn(downloadDir);
final FakeCachedArtifact artifact = FakeCachedArtifact(
cache: mockCache,
binaryDirs: <List<String>>[
<String>['bin_dir', 'darwin-x64/artifacts.zip'],
<String>['font-subset', 'darwin-x64/font-subset.zip'],
],
requiredArtifacts: DevelopmentArtifact.universal,
);
await artifact.updateInner();
expect(testLogger.statusText, isNotNull);
expect(testLogger.statusText, isNotEmpty);
expect(
testLogger.statusText.split('\n'),
<String>[
'Downloading darwin-x64 tools...',
'Downloading darwin-x64/font-subset tools...',
'',
],
);
}, overrides: <Type, Generator>{
Cache: () => mockCache,
FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(),
HttpClientFactory: () => () => mockHttpClient,
OperatingSystemUtils: () => mockOperatingSystemUtils,
Platform: () => fakePlatform,
});
});

group('AndroidMavenArtifacts', () {
Expand Down Expand Up @@ -446,6 +489,45 @@ void main() {
]);
});
}, skip: !globals.platform.isLinux);


testUsingContext('FontSubset in univeral artifacts', () {
final MockCache mockCache = MockCache();
final FontSubsetArtifacts artifacts = FontSubsetArtifacts(mockCache);
expect(artifacts.developmentArtifact, DevelopmentArtifact.universal);
});

testUsingContext('FontSubset artifacts on linux', () {
final MockCache mockCache = MockCache();
final FontSubsetArtifacts artifacts = FontSubsetArtifacts(mockCache);
expect(artifacts.getBinaryDirs(), <List<String>>[<String>['linux-x64', 'linux-x64/font-subset.zip']]);
}, overrides: <Type, Generator> {
Platform: () => FakePlatform()..operatingSystem = 'linux',
});

testUsingContext('FontSubset artifacts on windows', () {
final MockCache mockCache = MockCache();
final FontSubsetArtifacts artifacts = FontSubsetArtifacts(mockCache);
expect(artifacts.getBinaryDirs(), <List<String>>[<String>['windows-x64', 'windows-x64/font-subset.zip']]);
}, overrides: <Type, Generator> {
Platform: () => FakePlatform()..operatingSystem = 'windows',
});

testUsingContext('FontSubset artifacts on macos', () {
final MockCache mockCache = MockCache();
final FontSubsetArtifacts artifacts = FontSubsetArtifacts(mockCache);
expect(artifacts.getBinaryDirs(), <List<String>>[<String>['darwin-x64', 'darwin-x64/font-subset.zip']]);
}, overrides: <Type, Generator> {
Platform: () => FakePlatform()..operatingSystem = 'macos',
});

testUsingContext('FontSubset artifacts on fuchsia', () {
final MockCache mockCache = MockCache();
final FontSubsetArtifacts artifacts = FontSubsetArtifacts(mockCache);
expect(() => artifacts.getBinaryDirs(), throwsToolExit(message: 'Unsupported operating system: ${globals.platform.operatingSystem}'));
}, overrides: <Type, Generator> {
Platform: () => FakePlatform()..operatingSystem = 'fuchsia',
});
}

class FakeCachedArtifact extends EngineCachedArtifact {
Expand Down Expand Up @@ -513,3 +595,6 @@ class MockCache extends Mock implements Cache {}
class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}
class MockPlatform extends Mock implements Platform {}
class MockVersionedPackageResolver extends Mock implements VersionedPackageResolver {}

class MockHttpClientRequest extends Mock implements HttpClientRequest {}
class MockHttpClientResponse extends Mock implements HttpClientResponse {}