Skip to content

Commit

Permalink
Declare tasks programatically rather than declaratively
Browse files Browse the repository at this point in the history
This has a few benefits:

* It allows us to dynamically choose task dependencies (so for example
  the Linux standalone package can depend on the native executable
  task when running on Linux).

* Similar tasks can be defined programmatically.

* Task names are decoupled from Dart function names, which in turn
  allows us to avoid manually namespacing the package in favor of
  encouraging it to be imported with a Dart namespace.

* It works around google/grinder.dart#337 and google/grinder.dart#338.
  • Loading branch information
nex3 committed Jul 25, 2019
1 parent bf920d0 commit d6a3740
Show file tree
Hide file tree
Showing 13 changed files with 726 additions and 760 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,27 @@ and non-Dart users alike. It also integrates with Travis CI to make it easy to
automatically deploy packages.

[Grinder]: https://pub.dev/packages/grinder

To use this package, import `package:cli_pkg/cli_pkg.dart` and call
[`pkg.addAllTasks()`][] before calling [`grind()`][]:

[`pkg.addAllTasks()`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/addAllTasks.html
[`grind()`]: https://pub.dev/documentation/grinder/latest/grinder/grind.html

```dart
import 'package:cli_pkg/cli_pkg.dart' as pkg;
import 'package:grinder/grinder.dart';
void main(List<String> args) {
pkg.addAllTasks();
grind(args);
}
```

The following sets of tasks are provided, each of which can also be enabled
individually:

* [Creating standalone archives for your package.](doc/standalone.md)
* [Uploading standalone archives to GitHub releases.](doc/github.md)

It's strongly recommended that this package be imported with the prefix `pkg`.
53 changes: 53 additions & 0 deletions doc/github.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
These tasks upload [standalone packages][] to GitHub releases. They're enabled
by calling [`pkg.addGithubTasks()`][], which automatically calls
[`pkg.addStandaloneTasks()`][]

[standalone packages]: standalone.md
[`pkg.addGithubTasks()`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/addGithubTasks.html
[`pkg.addStandaloneTasks()`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/addStandaloneTasks.html

## `pkg-github-release`

Uses configuration: [`pkg.humanName`][], [`pkg.version`][],
[`pkg.githubRepo`][], [`pkg.githubReleaseNotes`][]

[`pkg.humanName`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/humanName.html
[`pkg.version`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/version.html
[`pkg.githubRepo`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/githubRepo.html
[`pkg.githubReleaseNotes`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/githubReleaseNotes.html

Creates a GitHub release for the current version of this package, without any
files uploaded to it.

## `pkg-github-$os`

Depends on: [`pkg-standalone-$os-ia32`, `pkg-standalone-$os-x64`][]

[`pkg-standalone-$os-ia32`, `pkg-standalone-$os-x64`]: standalone.md#pkg-standalone-os-arch

Uses configuration: [`pkg.version`][], [`pkg.githubRepo`][], [`pkg.standaloneName`][]

[`pkg.standaloneName`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/standaloneName.html

Uploads 32- and 64-bit executable packages for the given operating system
(`linux`, `windows`, or `macos`) to the GitHub release for the current version.

Any OS's packages can be built and uploaded regardless of the OS running the
task, but if the host OS matches the target OS the 64-bit executable will be
built as a native executable, which is substantially faster.

This must be invoked after [`pkg-github-release`][], but it doesn't have a
built-in dependency so that different OSs' packages can be built in different
build steps.

[`pkg-github-release`]: #pkg-github-release

## `pkg-github-all`

Depends on: [`pkg-github-release`][], [`pkg-github-linux`, `pkg-github-macos`,
`pkg-github-windows`][].

[`pkg-github-linux`, `pkg-github-macos`, `pkg-github-windows`]: #pkg-github-os

A utility task for creating a release and uploading packages for all operating
systems in the same step.
70 changes: 70 additions & 0 deletions doc/standalone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
These tasks create self-contained archives containing the Dart VM and snapshots
of the package's executables, which can then be easily distributed. They're the
basis for many other tasks that upload the packages to various package managers.
They're enabled by calling [`pkg.addStandaloneTasks()`][].

[`pkg.addStandaloneTasks()`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/addStandaloneTasks.html

## `pkg-compile-snapshot`

Uses configuration: [`pkg.entrypoints`][]

[`pkg.entrypoints`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/entrypoints.html

Output: `build/$entrypoint.snapshot`

Compiles each executable in the package to a [kernel snapshot][snapshot].

[snapshots]: https://github.com/dart-lang/sdk/wiki/Snapshots

## `pkg-compile-native`

Uses configuration: [`pkg.entrypoints`][], [`pkg.version`][]

[`pkg.version`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/version.html

Output: `build/$entrypoint.native`

Compiles each executable in the package to a native code snapshot (what Dart
calls an ["AOT Application snapshot"][snapshot]). This is unavailable on 32-bit
host systems.

Defines an environment constant named `version` set to [`pkg.version`][] that
can be accessed from within each entrypoint via [`String.fromEnvironment()`][].

[`String.fromEnvironment()`]: https://api.dartlang.org/stable/dart-core/String/String.fromEnvironment.html

## `pkg-standalone-$os-$arch`

Depends on: [`pkg-compile-snapshot`][] or [`pkg-compile-native`][]

[`pkg-compile-snapshot`]: #pkg-compile-snapshot
[`pkg-compile-native`]: #pkg-compile-native

Uses configuration: [`pkg.version`][], [`pkg.standaloneName`][], [`pkg.entrypoints`][]

[`pkg.standaloneName`]: https://pub.dev/documentation/dart_cli_pkg/latest/cli_pkg/standaloneName.html

Output: `build/$standaloneName-$version-$os-$arch.(tar.gz|zip)`

Creates an archive that contains all this package's entrypoints along with the
Dart VM for the given operating system and architecture, with top-level scripts
that can be used to invoke them.

Any OS's packages can be built regardless of the OS running the task, but if the
host OS matches the target OS *and* the architecture is 64-bit, executables will
be built as native (["AOT"][snapshot]) executables, which are substantially
faster and smaller than the kernel snapshots that are generated otherwise.

This produces a ZIP file in Windows, and a gzipped TAR file on Linux and Mac OS.

## `pkg-standalone-all`

Depends on: [`pkg-standalone-linux-ia32`, `pkg-standalone-linux-x64`,
`pkg-standalone-macos-ia32`, `pkg-standalone-macos-x64`,
`pkg-standalone-windows-ia32`, `pkg-standalone-windows-x64`][]

[`pkg-standalone-linux-ia32`, `pkg-standalone-linux-x64`, `pkg-standalone-macos-ia32`, `pkg-standalone-macos-x64`, `pkg-standalone-windows-ia32`, `pkg-standalone-windows-x64`]: #pkg-standalone-os-arch

A utility task for creating a packages for all operating systems in the same
step.
66 changes: 12 additions & 54 deletions lib/cli_pkg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:grinder/grinder.dart';

import 'github.dart' as github;
import 'standalone.dart' as standalone;

export 'src/info.dart' hide pubspec;

// Manually export tasks to work around google/grinder.dart#337

@Task('Build Dart script snapshot(s).')
void pkgCompileSnapshot() => standalone.pkgCompileSnapshot();

@Task('Build Dart native executable(s).')
void pkgCompileNative() => standalone.pkgCompileNative();

@Depends(pkgCompileSnapshot)
@Task('Build a standalone 32-bit package for Linux.')
Future<void> pkgStandaloneLinuxIa32() => standalone.pkgStandaloneLinuxIa32();

@Task('Build a standalone 64-bit package for Linux.')
Future<void> pkgStandaloneLinuxX64() => standalone.pkgStandaloneLinuxX64();

@Depends(pkgCompileSnapshot)
@Task('Build a standalone 32-bit package for Mac OS.')
Future<void> pkgStandaloneMacOsIa32() => standalone.pkgStandaloneMacOsIa32();

@Task('Build a standalone 64-bit package for Mac OS.')
Future<void> pkgStandaloneMacOsX64() => standalone.pkgStandaloneMacOsX64();

@Depends(pkgCompileSnapshot)
@Task('Build a standalone 32-bit package for Windows.')
Future<void> pkgStandaloneWindowsIa32() =>
standalone.pkgStandaloneWindowsIa32();

@Task('Build a standalone 64-bit package for Windows.')
Future<void> pkgStandaloneWindowsX64() => standalone.pkgStandaloneWindowsX64();

@Task('Build all standalone packages.')
Future<void> pkgStandaloneAll() => standalone.pkgStandaloneAll();

@Task('Create a GitHub release, without executables.')
Future<void> pkgGithubRelease() => github.pkgGithubRelease();

@Task('Release Linux executables to GitHub.')
Future<void> pkgGithubLinux() => github.pkgGithubLinux();

@Task('Release Mac OS executables to GitHub.')
Future<void> pkgGithubMacOs() => pkgGithubMacOs();

@Task('Release Windows executables to GitHub.')
Future<void> pkgGithubWindows() => pkgGithubWindows();

@Task('Create a GitHub release with all executables.')
Future<void> pkgGithubAll() => pkgGithubAll();
import 'src/github.dart';
import 'src/standalone.dart';

export 'src/info.dart';
export 'src/github.dart';
export 'src/standalone.dart';

/// Enables all tasks from the `cli_pkg` package.
void addAllTasks() {
addGithubTasks();
addStandaloneTasks();
}
Loading

0 comments on commit d6a3740

Please sign in to comment.