Skip to content

More robustly pick valid unique names for each asset. #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2024

Conversation

bramp
Copy link
Contributor

@bramp bramp commented Oct 16, 2023

What does this change?

Changed AssetTypeIsUniqueWithoutExtension to more robustly pick valid unique names for each asset.

The strategy is as follows:

1) Convert the asset file name, to a valid dart identifier, that is
   - Replace non ASCII chars with ASCII.
   - Ensure the name starts with a letter (not number or _).
   - Style the name per the camelCase or snakeCase rules.
2) Use the asset name without extension. If unique and not a dart reserved word use that.
3) Use the asset name with extension. If unique and not a dart reserved word use that.
4) If there are any collisions, append a underscore suffix to each item until they are unique.

This will now handle a wide-range of names that weren't previously supported. Dart identifiers, assets starting with numbers, asserts with non-ascii characters, assets with conflicting names (e.g 'a.png' and 'A.png'). See the tests in packages/core/test/assets_gen_test.dart.

Fixes #250, #346, and #420 🎯. Happy to discuss on #420.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open a GitHub issue as a bug/feature request before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
    • [X Ensure the tests (melos run test)
    • Ensure the analyzer and formatter pass (melos run format to automatically apply formatting)
  • Appropriate docs were updated (if necessary)

@AlexV525
Copy link
Member

@bramp Hi, could you rebase so we can review this?

@AlexV525 AlexV525 mentioned this pull request Mar 10, 2024
4 tasks
@bramp
Copy link
Contributor Author

bramp commented Mar 10, 2024

Trying to rebase, but it seems a bunch of changes, such as #471 are causing me merge conflicts, and I may need to refactor a bunch :(

bramp added 2 commits March 10, 2024 14:26
…e of the asset. Instead of having this code scattered in a few places.
… unique names for each asset.

The strategy is as follows:

1) Convert the asset file name, to a valid dart identifier, that is
   - Replace non ASCII chars with ASCII.
   - Ensure the name starts with a letter (not number or _).
   - Style the name per the camelCase or snakeCase rules.
2) Use the asset name without extension. If unique and not a dart reserved word use that.
3) Use the asset name with extension. If unique and not a dart reserved word use that.
4) If there are any collisions, append a underscore suffix to each item until they are unique.

This fixes FlutterGen#420 and FlutterGen#250. It also conflicts with PR FlutterGen#346, but this does everything that does plus more.
Copy link

codecov bot commented Mar 10, 2024

Codecov Report

Attention: Patch coverage is 83.63636% with 9 lines in your changes are missing coverage. Please review.

Project coverage is 97.22%. Comparing base (063458a) to head (f9239d9).

Files Patch % Lines
packages/core/lib/settings/asset_type.dart 76.31% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #433      +/-   ##
==========================================
- Coverage   98.35%   97.22%   -1.14%     
==========================================
  Files          21       22       +1     
  Lines         730      756      +26     
==========================================
+ Hits          718      735      +17     
- Misses         12       21       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bramp
Copy link
Contributor Author

bramp commented Mar 10, 2024

Ok, rebased, and all tests now pass. Please take a look.

I'm a little unsure if this will now resolve conflicts with "root assets" with conflicting names. But I think to resolve that a larger refactor is needed (such as getting the list of all root assets, and then deconflicting the names).

I also noticed that code coverage dropped, it seems because I added a toString for debugging, and that's not convered by a test. I can add that, but it was purely for debugging, not part of the output of the problem.

@@ -14,6 +14,9 @@ extension StringExt on String {
}
}

String camelCase(String s) => s.camelCase();
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't seem to be an improvement compare to the original usages.

Copy link
Contributor Author

@bramp bramp Mar 17, 2024

Choose a reason for hiding this comment

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

Because now you can pass camelCase as a function, eg.

UniqueAssetType(assetType: assetType, style: camelCase)

@bramp
Copy link
Contributor Author

bramp commented Mar 18, 2024

Ok, made the required changes. Please take a look.

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

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

👍

@AlexV525 AlexV525 merged commit 086be3a into FlutterGen:main Mar 20, 2024
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.

error when png name is start with number
2 participants