@@ -6,6 +6,7 @@ import 'dart:ffi';
66import 'dart:io' ;
77
88import 'package:args/args.dart' ;
9+ import 'package:path/path.dart' as p;
910import 'package:yaml/yaml.dart' ;
1011
1112void main (List <String > arguments) async {
@@ -108,6 +109,141 @@ abstract class Task {
108109 });
109110}
110111
112+ /// Ensures all packages are included in the pub workspace or a reason is
113+ /// provided why they cannot be part of the workspace.
114+ class WorkspaceTask extends Task {
115+ const WorkspaceTask ()
116+ : super (
117+ name: 'workspace' ,
118+ helpMessage:
119+ 'Check that all packages are included in the pub workspace.' ,
120+ );
121+
122+ @override
123+ Future <void > run ({
124+ required List <String > packages,
125+ required ArgResults argResults,
126+ }) async {
127+ final packagesInRepository = _packagesInRepository ();
128+ final packagesInWorkspacePubspec = _packagesInWorkspacePubspec ();
129+
130+ final error = < String > [];
131+
132+ if (packagesInWorkspacePubspec.missingReason.isNotEmpty) {
133+ error
134+ ..add (
135+ 'The following packages are commented out in the workspace '
136+ 'pubspec, but no reason is given why they cannot be part of the '
137+ 'workspace:' ,
138+ )
139+ ..addAll (
140+ packagesInWorkspacePubspec.missingReason.map (
141+ (package) => ' - $package ' ,
142+ ),
143+ )
144+ ..add (
145+ 'Please add a trailing comment to their entry providing a reason '
146+ 'for their exclusion from the workspace.' ,
147+ )
148+ ..add ('' );
149+ }
150+
151+ final notInRepository = packagesInWorkspacePubspec.packages.difference (
152+ packagesInRepository,
153+ );
154+ if (notInRepository.isNotEmpty) {
155+ error
156+ ..add (
157+ 'The following packages are listed in the workspace pubspec, but '
158+ 'do not exist in the repository:' ,
159+ )
160+ ..addAll (notInRepository.map ((package) => ' - $package ' ))
161+ ..add ('Please remove them from the workspace pubspec.' )
162+ ..add ('' );
163+ }
164+
165+ final notInWorkspacePubspec = packagesInRepository.difference (
166+ packagesInWorkspacePubspec.packages,
167+ );
168+ if (notInWorkspacePubspec.isNotEmpty) {
169+ error
170+ ..add (
171+ 'The following packages exist in the repository, but are not part '
172+ 'of the workspace:' ,
173+ )
174+ ..addAll (notInWorkspacePubspec.map ((package) => ' - $package ' ))
175+ ..add (
176+ 'Please add them to the workspace. If that is not possible, add a '
177+ 'commented out entry to the root pubspec and provide a reason why '
178+ 'they cannot be part of the workspace as a trailing comment.' ,
179+ )
180+ ..add ('' );
181+ }
182+ if (error.isNotEmpty) {
183+ print (error.join ('\n ' ));
184+ exit (1 );
185+ }
186+ }
187+
188+ Set <String > _packagesInRepository () {
189+ final packages = < String > {};
190+ final rootDir = Directory .fromUri (repositoryRoot.resolve ('pkgs' ));
191+ for (final entity in rootDir.listSync (recursive: true )) {
192+ if (entity is File && entity.path.endsWith ('pubspec.yaml' )) {
193+ packages.add (
194+ Uri .file (
195+ p.relative (entity.parent.path, from: repositoryRoot.toFilePath ()),
196+ ).toString (),
197+ );
198+ }
199+ }
200+ return packages;
201+ }
202+
203+ ({Set <String > packages, List <String > missingReason})
204+ _packagesInWorkspacePubspec () {
205+ final pubspecLines = File .fromUri (
206+ repositoryRoot.resolve ('pubspec.yaml' ),
207+ ).readAsStringSync ().split ('\n ' );
208+ final workspaceEntries = pubspecLines
209+ .skipWhile ((line) => ! line.trim ().startsWith ('workspace:' ))
210+ .skip (1 )
211+ .takeWhile (
212+ (line) =>
213+ line.trim ().startsWith ('- ' ) || line.trim ().startsWith ('# - ' ),
214+ );
215+
216+ final packages = < String > {};
217+ final packagesWithMissingReason = < String > [];
218+
219+ // Regex breakdown:
220+ // ^\s* : Start of line and any leading whitespace
221+ // (#\s*)? : Optional leading '#' followed by optional whitespace (Group 1)
222+ // -\s+ : The YAML list dash '-' and at least one space
223+ // ([^\s#]+) : The actual path - any characters that aren't space or '#' (Group 2)
224+ // (\s*#.*)? : Optional trailing '#' and everything after it (Group 3)
225+ final regex = RegExp (r'^\s*(#\s*)?-\s+([^\s#]+)(\s*#.*)?' );
226+
227+ for (final entry in workspaceEntries) {
228+ final match = regex.firstMatch (entry);
229+
230+ if (match != null ) {
231+ final hasLeadingHash = match.group (1 ) != null ;
232+ final path = match.group (2 );
233+ final trailingComment = match.group (3 );
234+
235+ if (hasLeadingHash) {
236+ if (trailingComment == null || trailingComment.trim ().length < 6 ) {
237+ packagesWithMissingReason.add (path! );
238+ }
239+ }
240+ packages.add (path! );
241+ }
242+ }
243+ return (packages: packages, missingReason: packagesWithMissingReason);
244+ }
245+ }
246+
111247/// Fetches dependencies using `dart pub get` .
112248///
113249/// This is a prerequisite for most other tasks.
@@ -428,6 +564,7 @@ const testTask = TestTask();
428564const exampleTask = ExampleTask ();
429565const coverageTask = CoverageTask ();
430566const apiToolTask = ApiToolTask ();
567+ const workspaceTask = WorkspaceTask ();
431568
432569// The order of tasks is intentional.
433570final tasks = [
@@ -439,6 +576,7 @@ final tasks = [
439576 exampleTask,
440577 coverageTask,
441578 apiToolTask,
579+ workspaceTask,
442580];
443581
444582final Uri repositoryRoot = Platform .script.resolve ('../' );
0 commit comments