Skip to content

Commit

Permalink
Use moduleNamePrefix everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Aug 15, 2022
1 parent 88d2d53 commit 16bbc2a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
5 changes: 4 additions & 1 deletion packages/melos/lib/src/commands/clean.dart
Expand Up @@ -60,7 +60,10 @@ mixin _CleanMixin on _Melos {
Future<void> cleanIntelliJ(MelosWorkspace workspace) async {
if (dirExists(workspace.ide.intelliJ.runConfigurationsDir.path)) {
final melosXmlGlob = createGlob(
join(workspace.ide.intelliJ.runConfigurationsDir.path, 'melos_*.xml'),
join(
workspace.ide.intelliJ.runConfigurationsDir.path,
'$kRunConfigurationPrefix*.xml',
),
currentDirectoryPath: workspace.path,
);

Expand Down
1 change: 1 addition & 0 deletions packages/melos/lib/src/commands/runner.dart
Expand Up @@ -24,6 +24,7 @@ import '../common/exception.dart';
import '../common/git.dart';
import '../common/git_commit.dart';
import '../common/glob.dart';
import '../common/intellij_project.dart';
import '../common/io.dart';
import '../common/pending_package_update.dart';
import '../common/platform.dart';
Expand Down
66 changes: 38 additions & 28 deletions packages/melos/lib/src/common/intellij_project.dart
Expand Up @@ -25,6 +25,8 @@ import '../workspace.dart';
import 'io.dart';
import 'platform.dart';

const String kRunConfigurationPrefix = 'melos_';

const String _kTemplatesDirName = 'templates';
const String _kIntellijDirName = 'intellij';
const String _kDotIdeaDirName = '.idea';
Expand Down Expand Up @@ -75,17 +77,28 @@ class IntellijProject {
return joinAll([pathDotIdea, 'modules.xml']);
}

Future<String> pathTemplatesForDirectory(String directory) async {
return joinAll([await pathTemplates, directory]);
String _fullModuleName(String name) {
return '${_workspace.config.ide.intelliJ.moduleNamePrefix}$name';
}

String get workspaceModuleName {
return _fullModuleName(_workspace.name.toLowerCase());
}

String packageModuleName(Package package) {
return _fullModuleName(package.name);
}

String get pathWorkspaceModuleIml {
return joinAll([_workspace.path, '$workspaceModuleName.iml']);
}

String pathPackageModuleIml(Package package) {
return joinAll([package.path, 'melos_${package.name}.iml']);
return joinAll([package.path, '${packageModuleName(package)}.iml']);
}

String pathWorkspaceModuleIml() {
final workspaceModuleName = _workspace.config.name.toLowerCase();
return joinAll([_workspace.path, 'melos_$workspaceModuleName.iml']);
Future<String> pathTemplatesForDirectory(String directory) async {
return joinAll([await pathTemplates, directory]);
}

String injectTemplateVariable({
Expand All @@ -111,19 +124,6 @@ class IntellijProject {
return updatedTemplate;
}

String ideaModuleStringForName(String moduleName, {String? relativePath}) {
var module = '';
if (relativePath == null) {
module =
'<module fileurl="file://\$PROJECT_DIR\$/melos_$moduleName.iml" filepath="\$PROJECT_DIR\$/melos_$moduleName.iml" />';
} else {
module =
'<module fileurl="file://\$PROJECT_DIR\$/$relativePath/melos_$moduleName.iml" filepath="\$PROJECT_DIR\$/$relativePath/melos_$moduleName.iml" />';
}
// Pad to preserve formatting on generated file. Indent x6.
return ' $module';
}

/// Reads a file template from the templates directory.
///
/// Additionally keeps a cache to reduce reads.
Expand All @@ -148,6 +148,18 @@ class IntellijProject {
return template;
}

String ideaModuleStringForName(String moduleName, {String? relativePath}) {
final imlPath = relativePath != null
? '$relativePath/$moduleName.iml'
: '$moduleName.iml';
final module = '<module '
'fileurl="file://\$PROJECT_DIR\$/$imlPath" '
'filepath="\$PROJECT_DIR\$/$imlPath" '
'/>';
// Pad to preserve formatting on generated file. Indent x6.
return ' $module';
}

Future<void> forceWriteToFile(String filePath, String fileContents) async {
await writeTextFileAsync(filePath, fileContents, recursive: true);
}
Expand Down Expand Up @@ -195,7 +207,7 @@ class IntellijProject {
}

Future<void> writeWorkspaceModule() async {
final path = pathWorkspaceModuleIml();
final path = pathWorkspaceModuleIml;
if (fileExists(path)) {
// The user might have modified the module, so we don't want to overwrite
// them.
Expand All @@ -206,7 +218,6 @@ class IntellijProject {
'workspace_root_module.iml',
templateCategory: 'modules',
);

return forceWriteToFile(
path,
ideaWorkspaceModuleImlTemplate,
Expand All @@ -215,13 +226,10 @@ class IntellijProject {

Future<void> writeModulesXml() async {
final ideaModules = <String>[];
final workspaceModuleName = _workspace.config.name.toLowerCase();
final moduleNamePrefix = _workspace.config.ide.intelliJ.moduleNamePrefix;
for (final package in _workspace.filteredPackages.values) {
final moduleName = '$moduleNamePrefix${package.name}';
ideaModules.add(
ideaModuleStringForName(
moduleName,
packageModuleName(package),
relativePath: package.pathRelativeToWorkspace,
),
);
Expand Down Expand Up @@ -262,6 +270,8 @@ class IntellijProject {

await Future.forEach(runConfigurations.keys, (String scriptName) async {
final scriptArgs = runConfigurations[scriptName]!;
final pathSafeScriptArgs =
scriptArgs.replaceAll(RegExp('[^A-Za-z0-9]'), '_');

final generatedRunConfiguration =
injectTemplateVariables(melosScriptTemplate, {
Expand All @@ -273,7 +283,7 @@ class IntellijProject {
final outputFile = joinAll([
pathDotIdea,
'runConfigurations',
'melos_${scriptArgs.replaceAll(RegExp('[^A-Za-z0-9]'), '_')}.xml'
'$kRunConfigurationPrefix$pathSafeScriptArgs.xml'
]);

await forceWriteToFile(outputFile, generatedRunConfiguration);
Expand Down Expand Up @@ -340,10 +350,10 @@ class IntellijProject {
// <WORKSPACE_ROOT>/.idea/.name
await writeNameFile();

// <WORKSPACE_ROOT>/<PACKAGE_DIR>/<PACKAGE_NAME>.iml
// <WORKSPACE_ROOT>/<PACKAGE_DIR>/<MODULE_NAME_PREFIX><PACKAGE_NAME>.iml
await writePackageModules();

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

// <WORKSPACE_ROOT>/.idea/modules.xml
Expand Down
12 changes: 7 additions & 5 deletions packages/melos/lib/src/workspace_configs.dart
Expand Up @@ -22,7 +22,6 @@ import 'package:glob/glob.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart';

import '../melos.dart';
import 'common/git_repository.dart';
import 'common/glob.dart';
import 'common/io.dart';
Expand Down Expand Up @@ -75,9 +74,10 @@ IDEConfigs(
/// IntelliJ-specific configurations
@immutable
class IntelliJConfig {
const IntelliJConfig(
{this.enabled = _defaultEnabled,
this.moduleNamePrefix = _defaultModuleNamePrefix});
const IntelliJConfig({
this.enabled = _defaultEnabled,
this.moduleNamePrefix = _defaultModuleNamePrefix,
});

factory IntelliJConfig.fromYaml(Object? yaml) {
if (yaml is Map<Object?, Object?>) {
Expand All @@ -92,7 +92,9 @@ class IntelliJConfig {
? assertKeyIsA<bool>(key: 'enabled', map: yaml, path: 'ide/intellij')
: _defaultEnabled;
return IntelliJConfig(
enabled: enabled, moduleNamePrefix: moduleNamePrefix);
enabled: enabled,
moduleNamePrefix: moduleNamePrefix,
);
} else {
final enabled = assertIsA<bool>(
value: yaml,
Expand Down
2 changes: 1 addition & 1 deletion packages/melos/test/workspace_config_test.dart
Expand Up @@ -340,7 +340,7 @@ void main() {
const IntelliJConfig(enabled: false),
);
});

test('yields "moduleNamePrefix" of "melos_" by default', () {
expect(
IntelliJConfig.fromYaml(true).moduleNamePrefix,
Expand Down

0 comments on commit 16bbc2a

Please sign in to comment.