From 3d76097db131b760368766ecbf7e133f2e1db23e Mon Sep 17 00:00:00 2001 From: Gabriel Terwesten Date: Mon, 15 May 2023 22:16:47 +0200 Subject: [PATCH] refactor: tidy up workspace name validation and fix docs (#522) Fixes #519 --- docs/configuration/overview.mdx | 2 +- docs/getting-started.mdx | 4 ++-- docs/guides/migrations.mdx | 2 +- packages/melos/lib/src/command_runner/version.dart | 5 ++--- packages/melos/lib/src/common/utils.dart | 2 -- packages/melos/lib/src/package.dart | 7 +++++++ packages/melos/lib/src/workspace_configs.dart | 7 +++---- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/docs/configuration/overview.mdx b/docs/configuration/overview.mdx index be62a59f9..8b6c6a14e 100644 --- a/docs/configuration/overview.mdx +++ b/docs/configuration/overview.mdx @@ -19,7 +19,7 @@ project. The name of this project for display purposes within IO environments and IDEs. ```yaml -name: My Awesome Project +name: my_project ``` ## repository diff --git a/docs/getting-started.mdx b/docs/getting-started.mdx index 1cba6b57a..272465ea8 100644 --- a/docs/getting-started.mdx +++ b/docs/getting-started.mdx @@ -37,7 +37,7 @@ If you don't have a `pubspec.yaml` file at the workspace root yet, create one now: ```yaml -name: _workspace +name: my_project_workspace environment: sdk: '>=2.18.0 <3.0.0' @@ -58,7 +58,7 @@ Next create a `melos.yaml` file at the repository root. Within the `melos.yaml` file, add the `name` and `packages` fields: ```yaml -name: +name: my_project packages: - packages/* diff --git a/docs/guides/migrations.mdx b/docs/guides/migrations.mdx index 67c92aa23..6c8689ff7 100644 --- a/docs/guides/migrations.mdx +++ b/docs/guides/migrations.mdx @@ -24,7 +24,7 @@ If you don't have a `pubspec.yaml` file at the workspace root yet, create one now: ```yaml -name: _workspace +name: my_project_workspace environment: sdk: '>=2.18.0 <3.0.0' diff --git a/packages/melos/lib/src/command_runner/version.dart b/packages/melos/lib/src/command_runner/version.dart index f51ccf09d..bac192d28 100644 --- a/packages/melos/lib/src/command_runner/version.dart +++ b/packages/melos/lib/src/command_runner/version.dart @@ -261,9 +261,8 @@ class VersionCommand extends MelosCommand { return ManualVersionChange.incrementBuildNumber(); } - final semverReleaseType = SemverReleaseType.values.firstWhereOrNull( - (releaseType) => describeEnum(releaseType) == argument, - ); + final semverReleaseType = SemverReleaseType.values + .firstWhereOrNull((releaseType) => releaseType.name == argument); if (semverReleaseType != null) { return ManualVersionChange.incrementBySemverReleaseType( semverReleaseType, diff --git a/packages/melos/lib/src/common/utils.dart b/packages/melos/lib/src/common/utils.dart index ff9614d55..a004014f9 100644 --- a/packages/melos/lib/src/common/utils.dart +++ b/packages/melos/lib/src/common/utils.dart @@ -64,8 +64,6 @@ extension Let on T? { } } -String describeEnum(Object value) => value.toString().split('.').last; - /// Utility function to write inline multi-line strings with indentation and /// without trailing a new line. /// diff --git a/packages/melos/lib/src/package.dart b/packages/melos/lib/src/package.dart index b29afb75b..16fcd552d 100644 --- a/packages/melos/lib/src/package.dart +++ b/packages/melos/lib/src/package.dart @@ -75,6 +75,13 @@ Uri get pubUrl => Uri.parse( currentPlatform.environment['PUB_HOSTED_URL'] ?? 'https://pub.dev', ); +final _isValidPubPackageNameRegExp = + RegExp(r'^[a-z][a-z\d_-]*$', caseSensitive: false); + +/// Returns whether the given [name] is a valid pub package name. +bool isValidPubPackageName(String name) => + _isValidPubPackageNameRegExp.hasMatch(name); + /// Enum representing what type of package this is. enum PackageType { dartPackage, diff --git a/packages/melos/lib/src/workspace_configs.dart b/packages/melos/lib/src/workspace_configs.dart index 8213249c8..8ec981c3c 100644 --- a/packages/melos/lib/src/workspace_configs.dart +++ b/packages/melos/lib/src/workspace_configs.dart @@ -29,6 +29,7 @@ import 'common/glob.dart'; import 'common/io.dart'; import 'common/utils.dart'; import 'common/validation.dart'; +import 'package.dart'; import 'scripts.dart'; /// IDE-specific configurations. @@ -855,11 +856,9 @@ class MelosWorkspaceConfig { required String path, }) { final name = assertKeyIsA(key: 'name', map: yaml); - final isValidDartPackageNameRegExp = - RegExp(r'^[a-z][a-z\d_-]*$', caseSensitive: false); - if (!isValidDartPackageNameRegExp.hasMatch(name)) { + if (!isValidPubPackageName(name)) { throw MelosConfigException( - 'The name $name is not a valid dart package name', + 'The name $name is not a valid pub package name.', ); }