Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add port number feature to build command #1346

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
.vscode/settings.json
.vscode/
.DS_Store

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions packages/dart_frog_cli/lib/src/commands/build/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ class BuildCommand extends DartFrogCommand {
}) : _generator = generator ?? MasonGenerator.fromBundle,
_prodServerBuilderConstructor =
prodServerBuilderConstructor ?? ProdServerBuilder.new {
argParser.addOption(
'dart-version',
defaultsTo: 'stable',
help: 'The Dart SDK version used to build the Dockerfile, defaults to'
' stable.',
);
argParser
..addOption(
'dart-version',
defaultsTo: 'stable',
help: 'The Dart SDK version used to build the Dockerfile, defaults to'
' stable.',
)
..addOption(
'port',
abbr: 'p',
defaultsTo: '8080',
help: 'Which port number the server should start on.',
);
}

final GeneratorBuilder _generator;
Expand All @@ -38,11 +45,14 @@ class BuildCommand extends DartFrogCommand {
@override
Future<int> run() async {
final dartVersion = results['dart-version'] as String;
final port = (results['port'] as String?) ?? '8080';

final generator = await _generator(dartFrogProdServerBundle);

final builder = _prodServerBuilderConstructor(
logger: logger,
dartVersion: dartVersion,
port: port,
workingDirectory: cwd,
prodServerBundleGenerator: generator,
);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
typedef ProdServerBuilderConstructor = ProdServerBuilder Function({
required Logger logger,
required String dartVersion,
required String port,
required io.Directory workingDirectory,
required MasonGenerator prodServerBundleGenerator,
});
Expand All @@ -28,6 +29,7 @@ class ProdServerBuilder {
required this.dartVersion,
required this.workingDirectory,
required this.prodServerBundleGenerator,
this.port = '8080',
@visibleForTesting
RuntimeCompatibilityCallback? runtimeCompatibilityCallback,
}) : _ensureRuntimeCompatibility =
Expand All @@ -36,6 +38,9 @@ class ProdServerBuilder {
/// The Dart SDK version used to build the Dockerfile.
final String dartVersion;

/// The port number the server should start on.
final String port;

/// [Logger] instance used to wrap stdout.
final Logger logger;

Expand All @@ -51,15 +56,16 @@ class ProdServerBuilder {
Future<ExitCode> build() async {
_ensureRuntimeCompatibility(workingDirectory);

var vars = <String, dynamic>{
final vars = <String, dynamic>{
'dartVersion': dartVersion,
'port': port,
};

logger.detail('[codegen] running pre-gen...');
await prodServerBundleGenerator.hooks.preGen(
vars: vars,
workingDirectory: workingDirectory.path,
onVarsChanged: (v) => vars = v,
onVarsChanged: (v) => vars..addAll(v),
);

logger.detail('[codegen] running generate...');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void main() {
prodServerBuilderConstructor: ({
required Logger logger,
required String dartVersion,
required String port,
required Directory workingDirectory,
required MasonGenerator prodServerBundleGenerator,
}) {
Expand Down Expand Up @@ -87,6 +88,7 @@ void main() {
prodServerBuilderConstructor: ({
required Logger logger,
required String dartVersion,
required String port,
required Directory workingDirectory,
required MasonGenerator prodServerBundleGenerator,
}) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ void main() {
final generatorHooks = _MockGeneratorHooks();
when(
() => generatorHooks.preGen(
vars: {'dartVersion': 'stable'},
vars: {'dartVersion': 'stable', 'port': '8080'},
workingDirectory: any(named: 'workingDirectory'),
onVarsChanged: any(named: 'onVarsChanged'),
),
).thenAnswer((invocation) async {
(invocation.namedArguments[const Symbol('onVarsChanged')] as void
Function(Map<String, dynamic>))
.call({'dartVersion': 'stable'});
.call({'dartVersion': 'stable', 'port': '8080'});
});
when(
() => generator.generate(
any(),
vars: {'dartVersion': 'stable'},
vars: {'dartVersion': 'stable', 'port': '8080'},
fileConflictResolution: FileConflictResolution.overwrite,
),
).thenAnswer((_) async => []);
Expand Down
Loading