Skip to content

feat(dart_frog_cli): support for Dart workspaces #1825

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ COPY ./pubspec_overrides.yaml ./pubspec_overrides.yaml
{{/hasExternalDependencies}}
# Resolve app dependencies.
COPY pubspec.* ./
COPY pubspec_overrides.yaml* ./
RUN dart pub get

# Copy app source code and AOT compile it.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export 'src/create_bundle.dart';
export 'src/create_external_packages_folder.dart';
export 'src/dart_pub_get.dart';
export 'src/disable_workspace_resolution.dart';
export 'src/exit_overrides.dart';
export 'src/get_internal_path_dependencies.dart';
export 'src/get_pubspec_lock.dart';
export 'src/uses_workspace_resolution.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'dart:io';
import 'package:mason/mason.dart';
import 'package:path/path.dart' as path;

/// Opts out of dart workspaces until we can generate per package lockfiles.
/// https://github.com/dart-lang/pub/issues/4594
void disableWorkspaceResolution(
HookContext context, {
required String workingDirectory,
required void Function(int exitCode) exit,
}) {
try {
File(
path.join(workingDirectory, 'pubspec_overrides.yaml'),
).writeAsStringSync('resolution: null', mode: FileMode.append);
} on Exception catch (e) {
context.logger.err('$e');
exit(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:io';
import 'package:mason/mason.dart';
import 'package:path/path.dart' as path;
import 'package:pubspec_parse/pubspec_parse.dart';

/// Determines whether the project in the provided [workingDirectory]
/// is configured to use `resolution: workspace`.
bool usesWorkspaceResolution(
HookContext context, {
required String workingDirectory,
required void Function(int exitCode) exit,
}) {
final pubspecFile = File(path.join(workingDirectory, 'pubspec.yaml'));
if (!pubspecFile.existsSync()) return false;

final Pubspec pubspec;
try {
pubspec = Pubspec.parse(pubspecFile.readAsStringSync());
} on Exception catch (e) {
context.logger.err('$e');
exit(1);
return false;
}

return pubspec.resolution == 'workspace';
}
21 changes: 15 additions & 6 deletions bricks/dart_frog_prod_server/hooks/pre_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ Future<void> preGen(
Future<void> Function(String from, String to) copyPath = io_expanded.copyPath,
}) async {
final projectDirectory = directory ?? io.Directory.current;
final usesWorkspaces = usesWorkspaceResolution(
context,
workingDirectory: projectDirectory.path,
exit: exit,
);

if (usesWorkspaces) {
disableWorkspaceResolution(
context,
workingDirectory: projectDirectory.path,
exit: exit,
);
}

// We need to make sure that the pubspec.lock file is up to date
await dartPubGet(
Expand Down Expand Up @@ -64,9 +77,7 @@ Future<void> preGen(
'''Route conflict detected. ${lightCyan.wrap(originalFilePath)} and ${lightCyan.wrap(conflictingFilePath)} both resolve to ${lightCyan.wrap(conflictingEndpoint)}.''',
);
},
onViolationEnd: () {
exit(1);
},
onViolationEnd: () => exit(1),
);

reportRogueRoutes(
Expand All @@ -76,9 +87,7 @@ Future<void> preGen(
'''Rogue route detected.${defaultForeground.wrap(' ')}Rename ${lightCyan.wrap(filePath)} to ${lightCyan.wrap(idealPath)}.''',
);
},
onViolationEnd: () {
exit(1);
},
onViolationEnd: () => exit(1),
);

final customDockerFile = io.File(
Expand Down
1 change: 1 addition & 0 deletions bricks/dart_frog_prod_server/hooks/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies:
io: ^1.0.3
mason: ^0.1.0
path: ^1.8.1
pubspec_parse: ^1.5.0
yaml: ^3.1.2

dev_dependencies:
Expand Down
Loading