File size: 2,678 Bytes
a4ff84b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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.

import '../application_package.dart';
import '../base/file_system.dart';
import '../build_info.dart';
import '../globals.dart' as globals;
import '../project.dart';

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

  /// Creates a new [FuchsiaApp] from a fuchsia sub project.
  static FuchsiaApp? fromFuchsiaProject(FuchsiaProject project) {
    if (!project.existsSync()) {
      // If the project doesn't exist at all the current hint to run flutter
      // create is accurate.
      return null;
    }
    return BuildableFuchsiaApp(
      project: project,
    );
  }

  /// Creates a new [FuchsiaApp] from an existing .far archive.
  ///
  /// [applicationBinary] is the path to the .far archive.
  static FuchsiaApp? fromPrebuiltApp(FileSystemEntity applicationBinary) {
    final FileSystemEntityType entityType = globals.fs.typeSync(applicationBinary.path);
    if (entityType != FileSystemEntityType.file) {
      globals.printError('File "${applicationBinary.path}" does not exist or is not a .far file. Use far archive.');
      return null;
    }
    return PrebuiltFuchsiaApp(
      applicationPackage: applicationBinary,
    );
  }

  @override
  String get displayName => id;

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

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

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

  @override
  String get name => applicationPackage.path;

  @override
  final FileSystemEntity applicationPackage;
}

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

  final FuchsiaProject project;

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

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