1// Copyright 2019 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'package:meta/meta.dart'; 6 7import '../application_package.dart'; 8import '../base/file_system.dart'; 9import '../build_info.dart'; 10import '../globals.dart'; 11import '../project.dart'; 12 13abstract class FuchsiaApp extends ApplicationPackage { 14 FuchsiaApp({@required String projectBundleId}) : super(id: projectBundleId); 15 16 /// Creates a new [FuchsiaApp] from a fuchsia sub project. 17 factory FuchsiaApp.fromFuchsiaProject(FuchsiaProject project) { 18 if (!project.existsSync()) { 19 // If the project doesn't exist at all the current hint to run flutter 20 // create is accurate. 21 return null; 22 } 23 return BuildableFuchsiaApp( 24 project: project, 25 ); 26 } 27 28 /// Creates a new [FuchsiaApp] from an existing .far archive. 29 /// 30 /// [applicationBinary] is the path to the .far archive. 31 factory FuchsiaApp.fromPrebuiltApp(FileSystemEntity applicationBinary) { 32 final FileSystemEntityType entityType = fs.typeSync(applicationBinary.path); 33 if (entityType != FileSystemEntityType.file) { 34 printError('File "${applicationBinary.path}" does not exist or is not a .far file. Use far archive.'); 35 return null; 36 } 37 return PrebuiltFuchsiaApp( 38 farArchive: applicationBinary.path, 39 ); 40 } 41 42 @override 43 String get displayName => id; 44 45 /// The location of the 'far' archive containing the built app. 46 File farArchive(BuildMode buildMode); 47} 48 49class PrebuiltFuchsiaApp extends FuchsiaApp { 50 PrebuiltFuchsiaApp({ 51 @required String farArchive, 52 }) : _farArchive = farArchive, 53 // TODO(zra): Extract the archive and extract the id from meta/package. 54 super(projectBundleId: farArchive); 55 56 final String _farArchive; 57 58 @override 59 File farArchive(BuildMode buildMode) => fs.file(_farArchive); 60 61 @override 62 String get name => _farArchive; 63} 64 65class BuildableFuchsiaApp extends FuchsiaApp { 66 BuildableFuchsiaApp({this.project}) : 67 super(projectBundleId: project.project.manifest.appName); 68 69 final FuchsiaProject project; 70 71 @override 72 File farArchive(BuildMode buildMode) { 73 // TODO(zra): Distinguish among build modes. 74 final String outDir = getFuchsiaBuildDirectory(); 75 final String pkgDir = fs.path.join(outDir, 'pkg'); 76 final String appName = project.project.manifest.appName; 77 return fs.file(fs.path.join(pkgDir, '$appName-0.far')); 78 } 79 80 @override 81 String get name => project.project.manifest.appName; 82} 83