Skip to content
Merged
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
81 changes: 69 additions & 12 deletions packages/melos/lib/src/common/intellij_project.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:path/path.dart' as p;
import 'package:xml/xml.dart' as xml;

import '../common/utils.dart' as utils;
import '../package.dart';
Expand Down Expand Up @@ -133,19 +134,27 @@
return template;
}

String ideaModuleStringForName(String moduleName, {String? relativePath}) {
xml.XmlElement ideaModuleElementForName(
String moduleName, {
String? relativePath,
}) {
var imlPath = relativePath != null
? p.normalize('$relativePath/$moduleName.iml')
: '$moduleName.iml';

// Use `/` instead of `\` no matter what platform is.
imlPath = imlPath.replaceAll(r'\', '/');
final module =
'<module '
'fileurl="file://\$PROJECT_DIR\$/$imlPath" '
'filepath="\$PROJECT_DIR\$/$imlPath" '
'/>';
// Pad to preserve formatting on generated file.
return module.padLeft(6);

return xml.XmlElement(
xml.XmlName('module'),
[
xml.XmlAttribute(
xml.XmlName('fileurl'),
'file://\$PROJECT_DIR\$/$imlPath',
),
xml.XmlAttribute(xml.XmlName('filepath'), '\$PROJECT_DIR\$/$imlPath'),
],
);
}

Future<void> forceWriteToFile(String filePath, String fileContents) async {
Expand Down Expand Up @@ -212,22 +221,70 @@
);
}

Future<List<xml.XmlElement>> mergeExistingModules(
List<xml.XmlElement> melosModules,
) async {
if (!fileExists(pathModulesXml)) return melosModules;

final text = await readTextFileAsync(pathModulesXml);
try {
final doc = xml.XmlDocument.parse(text);

String createNormalizedKey(xml.XmlElement e) {
final v =
e.getAttribute('filepath') ??
e.getAttribute('filePath') ??
e.getAttribute('fileurl') ??
e.getAttribute('fileUrl');
return (v ?? '')
.trim()
.replaceAll('\\', '/')

Check notice on line 241 in packages/melos/lib/src/common/intellij_project.dart

View workflow job for this annotation

GitHub Actions / analyze

Use a raw string to avoid using escapes.

Try making the string a raw string and removing the escapes. See https://dart.dev/diagnostics/use_raw_strings to learn more about this problem.
.replaceFirst(
RegExp(r'^file://\$PROJECT_DIR\$/', caseSensitive: false),
'',
)
.replaceFirst(
RegExp(r'^\$PROJECT_DIR\$/', caseSensitive: false),
'',
);
}

final seen = melosModules
.map(createNormalizedKey)
.where((s) => s.isNotEmpty)
.toSet();

final extras = doc
.findAllElements('module')
.where(
(m) => seen.add(createNormalizedKey(m)),
)
.map((m) => m.copy());

return [...melosModules.map((e) => e.copy()), ...extras];
} catch (_) {
return melosModules;
}
}

Future<void> writeModulesXml() async {
final ideaModules = <String>[];
final ideaModules = <xml.XmlElement>[];

for (final package in _workspace.filteredPackages.values) {
ideaModules.add(
ideaModuleStringForName(
ideaModuleElementForName(
packageModuleName(package),
relativePath: package.pathRelativeToWorkspace,
),
);
}
ideaModules.add(ideaModuleStringForName(workspaceModuleName));
ideaModules.add(ideaModuleElementForName(workspaceModuleName));
final mergedModules = await mergeExistingModules(ideaModules);
final ideaModulesXmlTemplate = await readFileTemplate('modules.xml');
final generatedModulesXml = injectTemplateVariable(
template: ideaModulesXmlTemplate,
variableName: 'modules',
variableValue: ideaModules.join('\n'),
variableValue: mergedModules.join('\n'),
);
return forceWriteToFile(pathModulesXml, generatedModulesXml);
}
Expand Down
1 change: 1 addition & 0 deletions packages/melos/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
string_scanner: ^1.4.1
yaml: ^3.1.3
yaml_edit: ^2.2.2
xml: ^6.6.1

Check notice on line 49 in packages/melos/pubspec.yaml

View workflow job for this annotation

GitHub Actions / analyze

Dependencies not sorted alphabetically.

Try sorting the dependencies alphabetically (A to Z).

dev_dependencies:
mockito: ^5.5.0
Expand Down
Loading