application_package.dart 2.62 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../application_package.dart';
import '../base/file_system.dart';
import '../build_info.dart';
8
import '../globals.dart' as globals;
9 10 11
import '../project.dart';

abstract class FuchsiaApp extends ApplicationPackage {
12
  FuchsiaApp({required String projectBundleId}) : super(id: projectBundleId);
13 14

  /// Creates a new [FuchsiaApp] from a fuchsia sub project.
15
  static FuchsiaApp? fromFuchsiaProject(FuchsiaProject project) {
16 17 18 19 20
    if (!project.existsSync()) {
      // If the project doesn't exist at all the current hint to run flutter
      // create is accurate.
      return null;
    }
21 22 23 24 25 26 27 28
    return BuildableFuchsiaApp(
      project: project,
    );
  }

  /// Creates a new [FuchsiaApp] from an existing .far archive.
  ///
  /// [applicationBinary] is the path to the .far archive.
29
  static FuchsiaApp? fromPrebuiltApp(FileSystemEntity applicationBinary) {
30
    final FileSystemEntityType entityType = globals.fs.typeSync(applicationBinary.path);
31
    if (entityType != FileSystemEntityType.file) {
32
      globals.printError('File "${applicationBinary.path}" does not exist or is not a .far file. Use far archive.');
33 34
      return null;
    }
35
    return PrebuiltFuchsiaApp(
36
      applicationPackage: applicationBinary,
37 38 39 40 41 42 43 44 45 46
    );
  }

  @override
  String get displayName => id;

  /// The location of the 'far' archive containing the built app.
  File farArchive(BuildMode buildMode);
}

47
class PrebuiltFuchsiaApp extends FuchsiaApp implements PrebuiltApplicationPackage {
48
  PrebuiltFuchsiaApp({
49 50 51
    required this.applicationPackage,
  }) : // TODO(zanderso): Extract the archive and extract the id from meta/package.
       super(projectBundleId: applicationPackage.path);
52

53 54
  @override
  File farArchive(BuildMode buildMode) => globals.fs.file(applicationPackage);
55 56

  @override
57
  String get name => applicationPackage.path;
58 59

  @override
60
  final FileSystemEntity applicationPackage;
61 62 63
}

class BuildableFuchsiaApp extends FuchsiaApp {
64
  BuildableFuchsiaApp({required this.project}) :
65 66 67 68 69 70
      super(projectBundleId: project.project.manifest.appName);

  final FuchsiaProject project;

  @override
  File farArchive(BuildMode buildMode) {
71
    // TODO(zanderso): Distinguish among build modes.
72
    final String outDir = getFuchsiaBuildDirectory();
73
    final String pkgDir = globals.fs.path.join(outDir, 'pkg');
74
    final String appName = project.project.manifest.appName;
75
    return globals.fs.file(globals.fs.path.join(pkgDir, '$appName-0.far'));
76 77 78 79 80
  }

  @override
  String get name => project.project.manifest.appName;
}