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

5 6
// @dart = 2.8

7 8 9 10 11
import 'package:meta/meta.dart';

import '../application_package.dart';
import '../base/file_system.dart';
import '../build_info.dart';
12
import '../globals_null_migrated.dart' as globals;
13 14 15 16 17 18 19
import '../project.dart';

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

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

  /// Creates a new [FuchsiaApp] from an existing .far archive.
  ///
  /// [applicationBinary] is the path to the .far archive.
  factory FuchsiaApp.fromPrebuiltApp(FileSystemEntity applicationBinary) {
34
    final FileSystemEntityType entityType = globals.fs.typeSync(applicationBinary.path);
35
    if (entityType != FileSystemEntityType.file) {
36
      globals.printError('File "${applicationBinary.path}" does not exist or is not a .far file. Use far archive.');
37 38
      return null;
    }
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    return PrebuiltFuchsiaApp(
      farArchive: applicationBinary.path,
    );
  }

  @override
  String get displayName => id;

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

class PrebuiltFuchsiaApp extends FuchsiaApp {
  PrebuiltFuchsiaApp({
    @required String farArchive,
  }) : _farArchive = farArchive,
       // TODO(zra): Extract the archive and extract the id from meta/package.
       super(projectBundleId: farArchive);

  final String _farArchive;

  @override
61
  File farArchive(BuildMode buildMode) => globals.fs.file(_farArchive);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

  @override
  String get name => _farArchive;
}

class BuildableFuchsiaApp extends FuchsiaApp {
  BuildableFuchsiaApp({this.project}) :
      super(projectBundleId: project.project.manifest.appName);

  final FuchsiaProject project;

  @override
  File farArchive(BuildMode buildMode) {
    // TODO(zra): Distinguish among build modes.
    final String outDir = getFuchsiaBuildDirectory();
77
    final String pkgDir = globals.fs.path.join(outDir, 'pkg');
78
    final String appName = project.project.manifest.appName;
79
    return globals.fs.file(globals.fs.path.join(pkgDir, '$appName-0.far'));
80 81 82 83 84
  }

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