Skip to content

Commit

Permalink
feat: web app builder helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Oct 25, 2024
1 parent c52d408 commit 6d88c45
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 26 deletions.
15 changes: 1 addition & 14 deletions packages/flutter_build/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:tekartik_lints/strict.yaml

# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types

analyzer:
# exclude:
# - path/to/excluded/files/**
include: package:tekartik_lints/package.yaml
4 changes: 4 additions & 0 deletions packages/flutter_build/lib/app_build_menu.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'app_build.dart';
export 'src/app_build_menu.dart'
show menuFlutterWebAppBuilderContent, menuFlutterWebAppContent;
export 'src/controller.dart' show BuildShellController;
66 changes: 58 additions & 8 deletions packages/flutter_build/lib/src/app_build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:process_run/shell.dart';
import 'package:process_run/stdio.dart';
import 'package:tekartik_common_build/common_app_builder.dart';
import 'package:tekartik_deploy/fs_deploy.dart';
import 'package:tekartik_flutter_build/src/controller.dart';
import 'package:tekartik_web_publish/web_publish.dart';

String _fixFolder(String path, String folder) {
Expand All @@ -13,7 +14,14 @@ String _fixFolder(String path, String folder) {
return join(path, folder);
}

enum FlutterWebRenderer { html, canvasKit }
/// Web rendered (html deprecated
enum FlutterWebRenderer {
/// Deprecated
html,

/// Default
canvasKit
}

/// Build options.
class FlutterWebAppBuildOptions {
Expand All @@ -29,11 +37,19 @@ class FlutterWebAppBuildOptions {

/// Web app options
class FlutterWebAppOptions {
/// Project path
late final String path;

/// Deploy dir
late final String deployDir;

/// Serve web port
late final int webPort;

/// Build options
final FlutterWebAppBuildOptions? buildOptions;

/// Web app options
FlutterWebAppOptions(
{
/// default to current directory
Expand All @@ -46,6 +62,7 @@ class FlutterWebAppOptions {
this.webPort = webPort ?? webAppServeWebPortDefault;
}

/// Copy
FlutterWebAppOptions copyWith({
String? path,
String? deployDir,
Expand All @@ -61,17 +78,44 @@ class FlutterWebAppOptions {

/// Convenient builder.
class FlutterWebAppBuilder implements CommonAppBuilder {
/// Information target name (dev, prod...)
final String? target;

/// Options
final FlutterWebAppOptions options;

/// Deployer
final WebAppDeployer? deployer;

/// Optional controller
BuildShellController? controller;

/// Project path.
@override
String get path => options.path;

FlutterWebAppBuilder({required this.options, this.deployer});
/// Constructor
FlutterWebAppBuilder(
{FlutterWebAppOptions? options,
this.deployer,
this.controller,
this.target})
: options = options ?? FlutterWebAppOptions();

/// CopyWith
FlutterWebAppBuilder copyWith({BuildShellController? controller}) {
return FlutterWebAppBuilder(
controller: controller ?? this.controller,
options: options,
target: target,
deployer: deployer);
}

Shell get _shell => controller?.shell ?? Shell();

/// Build
Future<void> build() async {
var shell = Shell().cd(options.path);
var shell = _shell;
var renderOptions = '';
var wasm = options.buildOptions?.wasm ?? false;
if (!wasm) {
Expand Down Expand Up @@ -108,36 +152,42 @@ class FlutterWebAppBuilder implements CommonAppBuilder {
dst: Directory(deployDir));
}

/// Clean
Future<void> clean() async {
await flutterWebAppClean(options.path);
}

/// Run (serve)
Future<void> run() async {
var shell = Shell().cd(options.path);
await shell.run('flutter run -d chrome');
var shell = _shell;
await shell.run('flutter run -d chrome --web-port ${options.webPort}');
}

/// Deploy
Future<void> deploy() async {
if (deployer == null) {
throw StateError('Missing deployer');
}
await deployer!.deploy(path: options.deployDir);
}

/// Serve build
Future<void> serve() async {
await checkAndActivatePackage('dhttpd');
print('http://localhost:${options.webPort}');
stdout.writeln('http://localhost:${options.webPort}');
var deployDir = _fixFolder(path, options.deployDir);
var shell = Shell().cd(deployDir);
var shell = _shell;
await shell.run(
'dart pub global run dhttpd:dhttpd . --port ${options.webPort} --headers=Cross-Origin-Embedder-Policy=credentialless;Cross-Origin-Opener-Policy=same-origin');
'dart pub global run dhttpd:dhttpd --path ${shellArgument(deployDir)} --port ${options.webPort} --headers=Cross-Origin-Embedder-Policy=credentialless;Cross-Origin-Opener-Policy=same-origin');
}

/// Build and serve
Future<void> buildAndServe() async {
await build();
await serve();
}

/// Build and deploy
Future<void> buildAndDeploy() async {
await build();
await deploy();
Expand Down
115 changes: 115 additions & 0 deletions packages/flutter_build/lib/src/app_build_menu.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import 'package:dev_build/menu/menu.dart';
import 'package:dev_build/shell.dart';
import 'package:path/path.dart';
import 'package:tekartik_flutter_build/app_build.dart';

import 'controller.dart';

/// Single builder menu
void menuFlutterWebAppBuilderContent({required FlutterWebAppBuilder builder}) {
//shellDebug = devWarning(true);
var path = builder.options.path;
var shell = Shell(workingDirectory: path);
var buildController = BuildShellController(shell: shell);
builder = builder.copyWith(controller: buildController);

enter(() async {
write('App path: ${absolute(path)}');
});

void cancel() {
buildController.cancel();
}

item('cancel current build/server', () async {
cancel();
});

if (builder.deployer != null) {
item('build and deploy', () async {
cancel();
await builder.buildAndDeploy();
});
}

item('build', () async {
cancel();
await builder.build();
});

item('run', () async {
cancel();
await builder.run();
});

item('serve', () async {
cancel();
await builder.serve();
});
if (builder.deployer != null) {
item('deploy', () async {
cancel();
await builder.deploy();
});
}

item('build and serve', () async {
cancel();
await builder.buildAndServe();
});
item('clean', () async {
cancel();
await builder.clean();
});
item('generateVersion', () async {
await builder.generateVersion();
});
}

/// Menu
void menuFlutterWebAppContent({required List<FlutterWebAppBuilder> builders}) {
if (builders.length >= 2) {
for (var builder in builders) {
menu('target ${builder.target}', () {
menuFlutterWebAppBuilderContent(builder: builder);
});
}

menu('all', () {
var actionController = BuildShellController();

void cancel() {
actionController.cancel();
}

item('build', () async {
cancel();
for (var builder in builders) {
await builder.build();
}
});

item('build and deploy', () async {
cancel();
for (var builder in builders) {
await builder.buildAndDeploy();
}
});
item('deploy', () async {
cancel();
for (var builder in builders) {
await builder.deploy();
}
});

item('clean', () async {
cancel();
for (var builder in builders) {
await builder.clean();
}
});
});
} else {
menuFlutterWebAppBuilderContent(builder: builders.first);
}
}
27 changes: 27 additions & 0 deletions packages/flutter_build/lib/src/controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:io';

import 'package:dev_build/shell.dart';

/// Shell controller
class BuildShellController {
/// Constructor
BuildShellController({Shell? shell}) {
_shell = shell;
}

/// Shell
Shell get shell => _shell!;
Shell? _shell;

/// Cancel current shell
void cancel() {
_shell?.kill(ProcessSignal.sigkill);
}
}

/// Private extension to set the shell
extension BuildShellControllerPrvExt on BuildShellController {
set shell(Shell shell) {
_shell = shell;
}
}
8 changes: 4 additions & 4 deletions packages/flutter_build/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ environment:
sdk: ^3.5.0

dependencies:
path: '>=1.7.0'
process_run: '>=1.0.0+1'
dev_build: '>=0.1.0'
path: ">=1.9.0"
process_run: ">=1.2.1+1"
dev_build: ">=1.1.0+2"
tekartik_common_build:
git:
url: https://github.com/tekartik/app_build.dart
Expand All @@ -31,7 +31,7 @@ dependencies:
ref: dart3a
path: packages/web_publish
dev_dependencies:
test: '>=1.14.4'
test: ">=1.24.0"

dependency_overrides:
tekartik_web_publish:
Expand Down

0 comments on commit 6d88c45

Please sign in to comment.