| OLD | NEW |
| 1 Dart Mojo Applications | 1 Dart Mojo Applications |
| 2 ==== | 2 ==== |
| 3 | 3 |
| 4 ## Mojo Application API | 4 ## Mojo Application API |
| 5 | 5 |
| 6 *TODO(zra)* | 6 *TODO(zra)* |
| 7 | 7 |
| 8 ## Application Packaging | 8 ## Application Packaging |
| 9 | 9 |
| 10 All Dart sources for a Mojo application are collected in a specially formatted | 10 All Dart sources for a Mojo application are collected in a specially formatted |
| 11 zip file, which is understood by Dart's content handler in the Mojo shell. | 11 snapshot file, which is understood by Dart's content handler in the Mojo shell. |
| 12 This section describes what the various parts of that package are, and how they | 12 This section describes what the various parts of that package are, and how they |
| 13 all make it to the right place. | 13 all make it to the right place. |
| 14 | 14 |
| 15 ### GN Template | 15 ### GN Template |
| 16 | 16 |
| 17 Dart Mojo applications are built with the GN template | 17 Dart Mojo applications are built with the GN template 'dart_pkg' defined in |
| 18 'dartzip_packaged_application' defined in `//mojo/public/dart/rules.gni`. | 18 `//mojo/public/dart/rules.gni`. Here is an example: |
| 19 Here is an example: | |
| 20 | 19 |
| 21 | 20 |
| 22 ``` | 21 ``` |
| 23 dartzip_packaged_application("foo") { | 22 dart_pkg("foo") { |
| 24 output_name = "dart_foo" | 23 app_name_override = "dart_foo" |
| 25 uses_pub = true | 24 app = "lib/main.dart" |
| 26 sources = [ | 25 sources = [ |
| 27 "main.dart", | 26 "lib/foo.dart", |
| 28 "foo.dart", | 27 "pubspec.yaml", |
| 29 ] | 28 ] |
| 30 deps = [ | 29 deps = [ |
| 30 ":foo_mojom", |
| 31 "//mojo/public/dart", | 31 "//mojo/public/dart", |
| 32 "//mojo/services/network/public/interfaces", | 32 ] |
| 33 } |
| 34 |
| 35 mojom("foo_mojom") { |
| 36 sources = [ |
| 37 "foo.mojom", |
| 33 ] | 38 ] |
| 34 } | 39 } |
| 35 ``` | 40 ``` |
| 36 | 41 |
| 37 There are several parts: | 42 There are several parts. See the documentation in `//mojo/public/dart/rules.gni` |
| 38 * `output_name` is the name of the resulting .mojo file if it should be | 43 for all the details. |
| 39 different from the name of the target. (In this case we get dart_foo.mojo | |
| 40 instead of foo.mojo.) | |
| 41 * `uses_pub` should be true when the application depends on Dart packages pulled | |
| 42 down from pub. The application should have `pubspec.yaml` and `pubspec.lock` | |
| 43 files adjacent to `main.dart`. More on this below. | |
| 44 * `sources` is the list of Dart sources for the application. Each application | |
| 45 **must** contain a `main.dart` file. `main.dart` must be the library entry | |
| 46 point, and must contain the `main()` function. | |
| 47 * `deps` has the usual meaning. In the example above, | |
| 48 `//mojo/services/network/public/interfaces` indicates that the "foo" | |
| 49 application uses the Dart bindings generated for the network service. | |
| 50 | 44 |
| 51 ### pub packages | 45 ### pub packages |
| 52 | 46 |
| 53 Dart Mojo applications may use packages from the pub package repository at | 47 Dart Mojo applications may use packages from the pub package repository at |
| 54 pub.dartlang.org. | 48 pub.dartlang.org. |
| 55 | 49 |
| 56 The "foo" example above has `uses_pub` set to true. Suppose its `pubspec.yaml` | 50 The "foo" example above has `uses_pub` set to true. Suppose the "foo" package's |
| 57 is as follows: | 51 `pubspec.yaml` is as follows: |
| 58 | 52 |
| 59 ``` | 53 ``` |
| 60 name: foo | 54 name: foo |
| 61 version: 0.0.1 | 55 version: 0.0.1 |
| 62 description: Foo | 56 description: Foo |
| 63 dependencies: | 57 dependencies: |
| 64 crypto: ">=0.9.0 <0.10.0" | 58 crypto: ">=0.9.0 <0.10.0" |
| 65 ``` | 59 ``` |
| 66 | 60 |
| 67 The script `//mojo/public/tools/git/dart_pub_get.py` should be run before build | 61 The script `//mojo/public/tools/git/dart_pub_get.py` should be run before build |
| 68 time, e.g. as a "runhooks" action during `gclient sync`. The script traverses | 62 time, e.g. as a "runhooks" action during `gclient sync`. The script traverses |
| 69 a directory tree looking for `pubspec.yaml` files. On finding one, in the | 63 a directory tree looking for `pubspec.yaml` files. On finding one, in the |
| 70 containing directory, it runs `pub get`. This creates a "packages/" directory | 64 containing directory, it runs `pub get`. This creates a "packages/" directory |
| 71 in the source tree adjacent to the `pubspec.yaml` file containing the downloaded | 65 in the source tree adjacent to the `pubspec.yaml` file containing the downloaded |
| 72 Dart packages. `pub get` also creates a `pubspec.lock` file that locks down | 66 Dart packages. `pub get` also creates a `pubspec.lock` file that locks down |
| 73 pub packages to specific versions. This `pubspec.lock` file must be checked in | 67 pub packages to specific versions. This `pubspec.lock` file must be checked in |
| 74 in order to have hermetic builds. | 68 in order to have hermetic builds. |
| 75 | 69 |
| 76 During the build, The `dartzip_packaged_application` rule looks for a | 70 During the build, The `dart_pkg` rule looks for a "packages/" directory, and |
| 77 "packages/" directory, and copies its contents into the zip file. | 71 ensures that its contents are available when running the application. |
| 78 | 72 |
| 79 ### Generated bindings | 73 ### Generated bindings |
| 80 | 74 |
| 81 The script `//mojo/public/tools/bindings/generators/mojom_dart_generator.py` | 75 The script `//mojo/public/tools/bindings/generators/mojom_dart_generator.py` |
| 82 and the templates under `//mojo/public/tools/bindings/generators/dart_templates` | 76 and the templates under `//mojo/public/tools/bindings/generators/dart_templates` |
| 83 govern how `.mojom` files are compiled into Dart code. | 77 govern how `.mojom` files are compiled into Dart code. |
| 84 | 78 |
| 85 Consider the `network_error.mojom` file from the network services used by our | 79 Consider the `foo.mojom` file used by our example: |
| 86 "foo" example: | |
| 87 | 80 |
| 88 ``` | 81 ``` |
| 89 module mojo; | 82 [DartPackage="foo"] |
| 83 module foo; |
| 90 | 84 |
| 91 struct NetworkError { | 85 struct Foo { |
| 92 int32 code; | 86 int32 code; |
| 93 string? description; | 87 string? description; |
| 94 }; | 88 }; |
| 95 ``` | 89 ``` |
| 96 | 90 |
| 97 This contents of this file are in the `mojo` module. The Dart source generated | 91 This contents of this file are in the `foo` module. The Dart source generated |
| 98 for this file will end up under, e.g. | 92 for this file will end up under, e.g. `//out/Debug/gen/dart- |
| 99 `//out/Debug/gen/dart-gen/mojom/mojo/network_error.mojom.dart`, along with the | 93 pkg/foo/lib/foo/network_error.mojom.dart`, along with the other Dart sources |
| 100 other Dart sources generated for `.mojom` files in the `mojo` module. | 94 generated for `.mojom` files with the "foo" `DartPackage` annotation in the |
| 95 `foo` module. |
| 101 | 96 |
| 102 ### Resulting layout | 97 ### Resulting file |
| 98 |
| 99 The `dart_pkg` rule has two results. The first result is a Dart snapshot file |
| 100 zipped up into a .mojo file in the build output directory---something like |
| 101 `//out/Release/foo.mojo`. This file is understood by the Dart content handler |
| 102 and is suitable for deployment. The second result is a directory layout of the |
| 103 "foo" app that can be served by a webserver. When the URL of `lib/main.dart` is |
| 104 given to the `mojo_shell`, the app will be run in the Dart content handler. |
| 103 | 105 |
| 104 They layout for our "foo" example will be the following: | 106 They layout for our "foo" example will be the following: |
| 105 | 107 |
| 106 ``` | 108 ``` |
| 107 //main.dart | 109 //lib/main.dart |
| 108 //foo.dart | 110 //lib/foo.dart |
| 109 //crypto/... # Dart's crypto pub package. | 111 //lib/foo/foo.mojom.dart |
| 110 //mojo/public/dart/... # Mojo SDK Dart libraries. | 112 //packages/crypto/... # Dart's crypto pub package. |
| 111 //mojom/mojo/... # Generated bindings in the mojo module. | 113 //packages/mojo/... # Mojo SDK Dart libraries. |
| 112 ``` | 114 ``` |
| 113 | 115 |
| 114 Where `//mojo/public/dart` contains Dart's Mojo bindings, `//crypto` contains | 116 Where `//packages/mojo` contains Dart's Mojo bindings, `//packages/crypto` |
| 115 the `crypto` pub package, and `//mojom/mojo` contains the generated bindings in | 117 contains the `crypto` pub package, and `//lib/foo/` contains the bindings |
| 116 the mojom module for the network service. | 118 generated for `foo.mojom`. |
| 117 | 119 |
| 118 Mojo's Dart content handler sets the package root for a Dart application to be | 120 Mojo's Dart content handler sets the package root for a Dart application to be |
| 119 the root directory of the unpacked zip file. Therefore, Dart sources in this | 121 the packages directory. Therefore, Dart sources in this application can use the |
| 120 application can use the following imports: | 122 following imports: |
| 121 | 123 |
| 122 ```dart | 124 ```dart |
| 123 import 'package:crypto/crypto.dart'; | 125 import 'package:crypto/crypto.dart'; |
| 124 import 'package:mojo/public/dart/application.dart'; | 126 import 'package:foo/foo/foo.mojom.dart'; |
| 125 import 'package:mojom/mojo/network_error.mojom.dart'; | 127 import 'package:mojo/application.dart'; |
| 126 ``` | 128 ``` |
| OLD | NEW |