Skip to content

Commit

Permalink
refactor: tidy up workspace name validation and fix docs (#522)
Browse files Browse the repository at this point in the history
Fixes #519
  • Loading branch information
blaugold authored May 15, 2023
1 parent 60c8619 commit 3d76097
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ If you don't have a `pubspec.yaml` file at the workspace root yet, create one
now:

```yaml
name: <project>_workspace
name: my_project_workspace

environment:
sdk: '>=2.18.0 <3.0.0'
Expand All @@ -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: <project>
name: my_project
packages:
- packages/*
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/migrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If you don't have a `pubspec.yaml` file at the workspace root yet, create one
now:

```yaml
name: <project>_workspace
name: my_project_workspace

environment:
sdk: '>=2.18.0 <3.0.0'
Expand Down
5 changes: 2 additions & 3 deletions packages/melos/lib/src/command_runner/version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions packages/melos/lib/src/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ extension Let<T> 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.
///
Expand Down
7 changes: 7 additions & 0 deletions packages/melos/lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions packages/melos/lib/src/workspace_configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -855,11 +856,9 @@ class MelosWorkspaceConfig {
required String path,
}) {
final name = assertKeyIsA<String>(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.',
);
}

Expand Down

0 comments on commit 3d76097

Please sign in to comment.