-
Notifications
You must be signed in to change notification settings - Fork 29.7k
[gen_l10n] Add option to format generated localizations files #109171
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
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
|
@christopherfujino @HansMuller can I get a quick look at the changes just to make sure this makes sense before I go and update |
HansMuller
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| ); | ||
| argParser.addFlag( | ||
| 'format', | ||
| help: 'When specified, the `dart format` command is run after generating the files.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the files" is OK, since the reader is unlikely to think that we're referring to .arb files (or whatever), but it might be a clearer to clarify this a little.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
|
I'm confused why this is opt-in and even optional. In what situation wouldn't you need the file to be formatted like the rest of the project ? How will this play out with vs code auto gen of localization on save ? |
|
|
|
||
| final FileSystem _fileSystem; | ||
| final Logger _logger; | ||
| final Artifacts? _artifacts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to be nullable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, since globals.artifacts is nullable.
| } | ||
| final String dartBinary = _artifacts!.getHostArtifact(HostArtifact.engineDartBinary).path; | ||
| final List<String> command = <String>[dartBinary, 'format', ...outputFileList]; | ||
| final Process process = await _processManager.start(command); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't need to interact with the sub-process, calling .run() is cleaner:
final ProcessResult result = await _processManager.run(command);
if (result.exitCode != 0) {There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
| logger: globals.logger, | ||
| artifacts: globals.artifacts, | ||
| processManager: globals.processManager, | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
christopherfujino
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach looks good to me
How can formatting be a breaking change ? It stills obeys whatever analys options.yaml that's there right ? so any CI
|
|
Not necessarily breaking, but it would be a bit annoying/confusing having to commit unexpected changes due to formatting changes. For automatic generation, I'll add a format option to the yaml config. |
|
I think #108563 has already done what this PR has done. And we have another test case issue with test timeout that need to be resolved. Also, @thkim1011 is on the review list. |
| } on L10nException catch (e) { | ||
| throwToolExit(e.message); | ||
| // All other post processing. | ||
| if (boolArg('format') ?? false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will only apply to the format when using flutter tools directly. Generalizing is also included in building targets.
If you don't, #103414 should also be considered as a breaking change.
Generated files can be affected only once after they update their SDK. We do have a couple of patches during SDK updates like ignoring frame rates limitation when building iOS apps. |
That's only valid for the first localization gen and only because it did not originally obey formatting rules. |
@AlexV525 is there a specific reason why we should pass in |
| fileSystem = MemoryFileSystem.test(); | ||
| logger = BufferLogger.test(); | ||
| artifacts = Artifacts.test(); | ||
| processManager = const LocalProcessManager(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this running real sub-processes? If so, it will need a real file system, and should be moved to the commands.shard/permable directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this so that we use a FakeProcessManager instead.
IMO it belongs to the generator, something like who generates them should format them. In #108563 the generator obtain the artifacts and the process manager not only from the context runner, but also from a build target's environment. |
| throwToolExit(e.message); | ||
| // All other post processing. | ||
| if (format) { | ||
| if (_artifacts == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this would be a programming error, no? When is artifacts null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, I just checked the rest of the commands in executable.dart and they seem to just pass in artifacts as artifacts! so I'll do that.
jonahwilliams
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with nit
|
I don't see any tests that verified if generated files are well-formatted. |
|
Whether or not a file is formatted is something that dartfmt knows, but its not the tools responsibility to test. If we've tested that dartfmt has been invoked that is good enough for me |
…#167029) Migration of Localization Generation [Option 1](https://docs.flutter.dev/release/breaking-changes/flutter-generate-i10n-source#migration-guide) breaks `dart format . --set-exit-if-changed` checks (as commonly used in pipelines), as the generated file does not comply the `dart format` rules. This PR: - Formats the generated localization files by default - In contrast to [this comment](#109171 (comment)) I don't see it as breaking as the files are generated dynamically on every change and there's no need in migrating them. Also any tests should not fail as either the format was done manually afterwards anyways, or the formatting was not checked at all (in third party apps / libraries) as the output had not a correct format in any use case of checking via `dart format`. - Add tests for `no-format` - Add option to add arbitrary arguments to the `dart format` command to allow customized formatting Closes #167982 Contributes to: #102983 (comment) *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
…102983) (flutter#167029) Migration of Localization Generation [Option 1](https://docs.flutter.dev/release/breaking-changes/flutter-generate-i10n-source#migration-guide) breaks `dart format . --set-exit-if-changed` checks (as commonly used in pipelines), as the generated file does not comply the `dart format` rules. This PR: - Formats the generated localization files by default - In contrast to [this comment](flutter#109171 (comment)) I don't see it as breaking as the files are generated dynamically on every change and there's no need in migrating them. Also any tests should not fail as either the format was done manually afterwards anyways, or the formatting was not checked at all (in third party apps / libraries) as the output had not a correct format in any use case of checking via `dart format`. - Add tests for `no-format` - Add option to add arbitrary arguments to the `dart format` command to allow customized formatting Closes flutter#167982 Contributes to: flutter#102983 (comment) *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
…102983) (flutter#167029) Migration of Localization Generation [Option 1](https://docs.flutter.dev/release/breaking-changes/flutter-generate-i10n-source#migration-guide) breaks `dart format . --set-exit-if-changed` checks (as commonly used in pipelines), as the generated file does not comply the `dart format` rules. This PR: - Formats the generated localization files by default - In contrast to [this comment](flutter#109171 (comment)) I don't see it as breaking as the files are generated dynamically on every change and there's no need in migrating them. Also any tests should not fail as either the format was done manually afterwards anyways, or the formatting was not checked at all (in third party apps / libraries) as the output had not a correct format in any use case of checking via `dart format`. - Add tests for `no-format` - Add option to add arbitrary arguments to the `dart format` command to allow customized formatting Closes flutter#167982 Contributes to: flutter#102983 (comment) *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
See title. This adds a
--formatflag to theflutter gen-l10ncommand. We do this by returning the list of generated files fromLocalizationsGenerator.writeOutputFilesand doing post processing from thegenerate_localizations.dartfile. This should also be the way of adding any additional post processing steps to thegen-l10ncommand going forward.Also, something else to note is that all the tests in
generate_localizations_test.darthas been calling the command twice. I didn't think that the check for exitStatus was necessary, so I removed them.Fixes #98122
Relevant PRs: #108563, #98701.