Skip to content

Commit

Permalink
fix: don't override Intellij module config files (#351)
Browse files Browse the repository at this point in the history
Fixes #333
  • Loading branch information
blaugold authored Jul 14, 2022
1 parent a423718 commit 850e9f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 0 additions & 2 deletions docs/commands/clean.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ These files include;
- `{packageRoot}/.dart_tool/package_config.json`
- `{packageRoot}/.dart_tool/package_config_subset`
- `{packageRoot}/.dart_tool/version`
- `{packageRoot}/melos_{packageName}.iml`
- `{workspaceRoot}/melos_{workspaceName}.iml`
- `{workspaceRoot}/.idea/runConfigurations/melos_*.xml`

This can be useful to give your workspace a 'fresh start', e.g.;
Expand Down
39 changes: 31 additions & 8 deletions packages/melos/lib/src/common/intellij_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class IntellijProject {
return joinAll([package.path, 'melos_${package.name}.iml']);
}

String pathWorkspaceModuleIml() {
final workspaceModuleName = _workspace.config.name.toLowerCase();
return joinAll([_workspace.path, 'melos_$workspaceModuleName.iml']);
}

String injectTemplateVariable({
required String template,
required String variableName,
Expand Down Expand Up @@ -167,21 +172,43 @@ class IntellijProject {
}

Future<void> writePackageModule(Package package) async {
final path = pathPackageModuleIml(package);
if (fileExists(path)) {
// The user might have modified the module, so we don't want to overwrite
// them.
return;
}

final template = await readFileTemplate(
moduleTemplateFileForPackageType(package.type),
templateCategory: 'modules',
);
return forceWriteToFile(pathPackageModuleIml(package), template);

return forceWriteToFile(path, template);
}

Future<void> writePackageModules() async {
await Future.forEach(
_workspace.filteredPackages.values,
writePackageModule,
);
}

Future<void> writeWorkspaceModule() async {
final path = pathWorkspaceModuleIml();
if (fileExists(path)) {
// The user might have modified the module, so we don't want to overwrite
// them.
return;
}

final ideaWorkspaceModuleImlTemplate = await readFileTemplate(
'workspace_root_module.iml',
templateCategory: 'modules',
);
final workspaceModuleName = _workspace.config.name.toLowerCase();

return forceWriteToFile(
joinAll([_workspace.path, 'melos_$workspaceModuleName.iml']),
path,
ideaWorkspaceModuleImlTemplate,
);
}
Expand Down Expand Up @@ -312,11 +339,7 @@ class IntellijProject {
await writeNameFile();

// <WORKSPACE_ROOT>/<PACKAGE_DIR>/<PACKAGE_NAME>.iml

await Future.forEach(_workspace.filteredPackages.values,
(Package package) async {
await writePackageModule(package);
});
await writePackageModules();

// <WORKSPACE_ROOT>/<WORKSPACE_NAME>.iml
await writeWorkspaceModule();
Expand Down

0 comments on commit 850e9f8

Please sign in to comment.