linkPackages method
Generates Pub/Flutter related temporary files such as .packages or pubspec.lock.
Implementation
Future<void> linkPackages(MelosWorkspace workspace) async {
final pluginTemporaryPath =
p.join(workspace.melosToolPath, pathRelativeToWorkspace);
await Future.forEach(generatedPubFilePaths, (String tempFilePath) async {
final fileToCopy = p.join(pluginTemporaryPath, tempFilePath);
if (!fileExists(fileToCopy)) {
return;
}
var temporaryFileContents = await readTextFileAsync(fileToCopy);
// Ensure the file generator tool name and version is for 'melos'.
if (tempFilePath.endsWith('package_config.json')) {
final packageConfig = jsonDecode(temporaryFileContents) as Map;
packageConfig.addAll(<String, String>{
'generator': 'melos',
'generatorVersion': melosVersion,
});
temporaryFileContents =
const JsonEncoder.withIndent(' ').convert(packageConfig);
}
final regexPathSeparator = '${currentPlatform.isWindows ? r'\' : ''}'
'${currentPlatform.pathSeparator}';
final melosToolPathRegExp = RegExp(
'\\.dart_tool${regexPathSeparator}melos_tool$regexPathSeparator',
);
// Remove the `.dart_tool\melos_tool` path from any relative file paths
// in any of the generated files as since we mirrored the pub files to the
// melos_tool directory for mutations they now contain this path.
temporaryFileContents =
temporaryFileContents.replaceAll(melosToolPathRegExp, '');
await writeTextFileAsync(
p.join(path, tempFilePath),
temporaryFileContents,
recursive: true,
);
});
}