Skip to content

Commit

Permalink
Write .dart_tool/pub/workspace_ref.json (#4240)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm authored Apr 26, 2024
1 parent 6619725 commit 0e7446a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,12 @@ See $workspacesDocUrl for more information.''',

Entrypoint? _example;

/// Writes the .dart_tool/package_config.json file
/// Writes the .dart_tool/package_config.json file and workspace references to
/// it.
///
/// If the workspace is non-trivial: For each package in the workspace write:
/// `.dart_tool/pub/workspace_ref.json` with a pointer to the workspace root
/// package dir.
Future<void> writePackageConfigFile() async {
ensureDir(p.dirname(packageConfigPath));
writeTextFile(
Expand All @@ -418,6 +423,21 @@ See $workspacesDocUrl for more information.''',
.pubspec.sdkConstraints[sdk.identifier]?.effectiveConstraint,
),
);
if (workspaceRoot.workspaceChildren.isNotEmpty) {
for (final package in workspaceRoot.transitiveWorkspace) {
final workspaceRefDir = p.join(package.dir, '.dart_tool', 'pub');
final workspaceRefPath = p.join(workspaceRefDir, 'workspace_ref.json');
ensureDir(workspaceRefDir);
final relativeRootPath =
p.relative(workspaceRoot.dir, from: workspaceRefDir);
writeTextFile(
workspaceRefPath,
'${JsonEncoder.withIndent(' ').convert({
'workspaceRoot': relativeRootPath,
})}\n',
);
}
}
}

/// Returns the contents of the `.dart_tool/package_config` file generated
Expand Down
20 changes: 20 additions & 0 deletions test/workspace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as p;
Expand Down Expand Up @@ -51,6 +52,25 @@ void main() {
],
generatorVersion: '3.5.0',
).validate();
final workspaceRefA = jsonDecode(
File(
p.join(
sandbox,
appPath,
'pkgs',
'a',
'.dart_tool',
'pub',
'workspace_ref.json',
),
).readAsStringSync(),
);
expect(workspaceRefA, {'workspaceRoot': p.join('..', '..', '..', '..')});
final workspaceRefMyApp = jsonDecode(
File(p.join(sandbox, appPath, '.dart_tool', 'pub', 'workspace_ref.json'))
.readAsStringSync(),
);
expect(workspaceRefMyApp, {'workspaceRoot': p.join('..', '..')});
});

test(
Expand Down

0 comments on commit 0e7446a

Please sign in to comment.