Skip to content

Conversation

@christerswahn
Copy link
Collaborator

@christerswahn christerswahn commented May 6, 2025

Upon migrating scloud to the new BetterCommand and BetterCommandRunner a couple of use patterns needed better support:

  • Be able to specify a ConfigurationBroker that depends on dynamic state from the command or command runner
  • Be able to have a bespoke Configuration subclass

This PR enables these.

The goal is to release a final 0.5.0.beta after this and then migrate both the serverpod command and the scloud commands to use ut.

Summary by CodeRabbit

  • New Features

    • Added a new constructor for configuration objects to allow direct copying of their internal state.
  • Refactor

    • Simplified configuration resolution and error handling for commands and runners.
    • Centralized environment variable support and error formatting utilities.
    • Improved grouping and organization of exports for easier maintenance.
  • Chores

    • Removed unused imports and obsolete configuration resolver code.

@coderabbitai
Copy link

coderabbitai bot commented May 6, 2025

📝 Walkthrough

Walkthrough

The changes remove the ConfigResolver abstraction and related files, refactor BetterCommand and BetterCommandRunner to resolve configuration directly using a static method, and introduce explicit environment variable handling. Error formatting utilities are moved and slightly modified. A named constructor for cloning configurations is added. Minor import and export cleanups are also made.

Changes

File(s) Change Summary
lib/src/better_command_runner/config_resolver.dart Deleted the file, removing the ConfigResolver interface, its DefaultConfigResolver implementation, and related utility functions for error formatting.
lib/src/better_command_runner/better_command.dart Refactored BetterCommand to remove ConfigResolver usage, add explicit environment variable handling, update constructor and fields, move error handling to run(), and update documentation and imports.
lib/src/better_command_runner/better_command_runner.dart Refactored BetterCommandRunner to remove ConfigResolver, add environment variable support, centralize configuration error handling in run, add error formatting utility functions, and update documentation.
lib/src/config/configuration.dart Added a named constructor Configuration.from for cloning configuration instances.
lib/cli_tools.dart Reordered and deduplicated exports, added exports for config.dart and docs_generator.dart near the top.
example/main.dart
test/documentation_generator/generate_markdown_test.dart
Removed unused import statements for configuration-related files.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BetterCommandRunner
    participant BetterCommand
    participant Configuration

    User->>BetterCommandRunner: run(args)
    BetterCommandRunner->>Configuration: resolve(args, envVariables)
    Configuration-->>BetterCommandRunner: Configuration instance
    BetterCommandRunner->>BetterCommand: run()
    BetterCommand->>Configuration: resolve(argResults, envVariables)
    Configuration-->>BetterCommand: Configuration instance
    BetterCommand->>BetterCommand: runWithConfig(configuration)
Loading

Possibly related PRs

Suggested reviewers

  • Isakdl

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4777d0d and cf73d3c.

📒 Files selected for processing (2)
  • lib/src/better_command_runner/better_command_runner.dart (8 hunks)
  • lib/src/config/configuration.dart (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • lib/src/config/configuration.dart
  • lib/src/better_command_runner/better_command_runner.dart

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@christerswahn christerswahn changed the title Refactor: Updated BetterCommand* API on learnings from scloud refactor: Updated BetterCommand* API on learnings from scloud May 6, 2025
@christerswahn christerswahn marked this pull request as ready for review May 6, 2025 07:58
@christerswahn christerswahn requested a review from SandPod May 6, 2025 07:59
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
lib/src/config/configuration.dart (1)

790-798: Preserve option ordering and avoid shared mutable state in the copy-constructor

  1. configuration.options returns the keys of the original _config map, which may not preserve the original insertion order stored in configuration._options.
  2. Map.from(configuration._config) performs a shallow copy; if OptionResolution ever becomes mutable, the two Configuration instances would silently share the same resolution objects.

Consider copying the private fields directly and cloning the map entries to guard against future mutability:

Configuration.from({
   required final Configuration<O> configuration,
-})  : _options = List.from(configuration.options),
-     _config  = Map.from(configuration._config),
+})  : _options = List<O>.from(configuration._options),          // keeps order
+     _config  = configuration._config.map(
+       (k, v) => MapEntry(k, OptionResolution<V>.from(v)),      // deep copy
+     ),
      _errors  = List.from(configuration._errors);

(Assumes an appropriate OptionResolution.from constructor; otherwise create one or rely on copyWith.)

lib/src/better_command_runner/better_command_runner.dart (2)

73-76: Make the environment map immutable inside the runner

envVariables currently references the supplied map (or Platform.environment), allowing external code to mutate it after the runner is created. To avoid surprising behaviour, wrap it in an UnmodifiableMapView:

-import 'dart:io' show Platform;
+import 'dart:io' show Platform;
+import 'dart:collection' show UnmodifiableMapView;-  final Map<String, String> envVariables;
+  final Map<String, String> envVariables;-        envVariables = env ?? Platform.environment,
+        envVariables = UnmodifiableMapView(env ?? Platform.environment),

381-386: Edge case: multibyte first character handling

formatConfigError upper-cases the first code unit rather than the first Unicode code-point, e.g. "dž test" becomes "Dž test" (incorrect) and emojis are broken. If this matters, use characters package or StringExtension.capitalize() from package:characters to be Unicode-safe.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f85576 and 4777d0d.

📒 Files selected for processing (7)
  • example/main.dart (0 hunks)
  • lib/cli_tools.dart (1 hunks)
  • lib/src/better_command_runner/better_command.dart (5 hunks)
  • lib/src/better_command_runner/better_command_runner.dart (8 hunks)
  • lib/src/better_command_runner/config_resolver.dart (0 hunks)
  • lib/src/config/configuration.dart (1 hunks)
  • test/documentation_generator/generate_markdown_test.dart (0 hunks)
💤 Files with no reviewable changes (3)
  • test/documentation_generator/generate_markdown_test.dart
  • example/main.dart
  • lib/src/better_command_runner/config_resolver.dart
🔇 Additional comments (14)
lib/cli_tools.dart (1)

3-4: Exports look good

Adding config.dart and keeping a single docs_generator.dart export removes the previous duplication and surfaces the new public API.

lib/src/better_command_runner/better_command.dart (13)

2-2: Import added for environment variables support

The addition of the Platform import supports the new environment variables handling capability.


8-8: Clean import statement

The simplified import is good practice, removing unnecessary complexity.


10-14: Improved class documentation

The enhanced documentation clearly explains the purpose and capabilities of the BetterCommand class.


21-22: New environment variables field

Good addition for explicit environment variable handling. This makes configuration resolution more flexible and testable.


32-33: Clear documentation for environment parameter

The documentation clearly explains the optional env parameter and its default behavior.


35-38: Enhanced messageOutput documentation

Improved documentation clarity with better formatting and section headers.


63-63: Environment variables parameter and initialization

Good implementation of the environment variables parameter with a sensible default to Platform.environment.

Also applies to: 66-66


81-82: Type safety improvement for parent getter

Using dynamic typing here helps avoid type conflicts in the inheritance hierarchy while maintaining the correct runtime type. This is a pragmatic solution.


100-102: Clear documentation of error handling

Good documentation update that clearly explains the error handling behavior in the run method.


119-123: Clear documentation for configuration resolution

The updated documentation clearly indicates that this method can be overridden, which is important for extensibility.


124-128: Simplified configuration resolution

Good simplification by directly using Configuration.resolve instead of a separate ConfigResolver abstraction. This removes an unnecessary layer while maintaining the same functionality.


131-131: Improved method documentation

Clear documentation that describes the purpose of the runWithConfig method.


109-114:

✅ Verification successful

Centralized error handling

Good refactoring to centralize error handling in the run method. This is a better separation of concerns that allows resolveConfiguration to be overridden more easily without losing error handling.

However, there's an implicit dependency on the formatConfigError function which isn't visible in this file. Make sure this function is accessible.


🏁 Script executed:

#!/bin/bash
# Check if formatConfigError is defined and accessible
grep -r "formatConfigError" --include="*.dart" .

Length of output: 398


🏁 Script executed:

#!/bin/bash
# Display the top of better_command.dart to check imports
sed -n '1,50p' lib/src/better_command_runner/better_command.dart

Length of output: 1953


formatConfigError is properly imported and accessible

The refactoring centralizes error handling nicely. The free function formatConfigError is defined in better_command_runner.dart and is correctly imported via the relative import in better_command.dart. No further action needed.

@christerswahn christerswahn requested review from Isakdl and removed request for SandPod May 6, 2025 08:11
Copy link

@Isakdl Isakdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants