-
Notifications
You must be signed in to change notification settings - Fork 42
feat: Add automatic sorting for pubspec.yaml dependencies #88
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
Open
Franklyn-R-Silva
wants to merge
5
commits into
fluttercommunity:master
Choose a base branch
from
Franklyn-R-Silva:feat/sort-pubspec
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b5a7e2
feat: add flutter lldb helper and initialization scripts
Franklyn-R-Silva 631be45
feat: add automatic sorting for pubspec.yaml dependencies
Franklyn-R-Silva aa811a9
feat: add sorting feedback for pubspec.yaml dependencies
Franklyn-R-Silva 3582357
feat: Allow custom path to the pubspec.yaml file and add tests for cl…
Franklyn-R-Silva 76216b8
docs: Update contributor list and improve test comments for clarity
Franklyn-R-Silva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
example/example_app/ios/Flutter/ephemeral/flutter_lldb_helper.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # | ||
| # Generated file, do not edit. | ||
| # | ||
|
|
||
| import lldb | ||
|
|
||
| def handle_new_rx_page(frame: lldb.SBFrame, bp_loc, extra_args, intern_dict): | ||
| """Intercept NOTIFY_DEBUGGER_ABOUT_RX_PAGES and touch the pages.""" | ||
| base = frame.register["x0"].GetValueAsAddress() | ||
| page_len = frame.register["x1"].GetValueAsUnsigned() | ||
|
|
||
| # Note: NOTIFY_DEBUGGER_ABOUT_RX_PAGES will check contents of the | ||
| # first page to see if handled it correctly. This makes diagnosing | ||
| # misconfiguration (e.g. missing breakpoint) easier. | ||
| data = bytearray(page_len) | ||
| data[0:8] = b'IHELPED!' | ||
|
|
||
| error = lldb.SBError() | ||
| frame.GetThread().GetProcess().WriteMemory(base, data, error) | ||
| if not error.Success(): | ||
| print(f'Failed to write into {base}[+{page_len}]', error) | ||
| return | ||
|
|
||
| def __lldb_init_module(debugger: lldb.SBDebugger, _): | ||
| target = debugger.GetDummyTarget() | ||
| # Caveat: must use BreakpointCreateByRegEx here and not | ||
| # BreakpointCreateByName. For some reasons callback function does not | ||
| # get carried over from dummy target for the later. | ||
| bp = target.BreakpointCreateByRegex("^NOTIFY_DEBUGGER_ABOUT_RX_PAGES$") | ||
| bp.SetScriptCallbackFunction('{}.handle_new_rx_page'.format(__name__)) | ||
| bp.SetAutoContinue(True) | ||
| print("-- LLDB integration loaded --") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # | ||
| # Generated file, do not edit. | ||
| # | ||
|
|
||
| command script import --relative-to-command-file flutter_lldb_helper.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // 🎯 Dart imports: | ||
| import 'dart:io'; | ||
|
|
||
| // 📦 Package imports: | ||
| import 'package:yaml/yaml.dart'; | ||
| import 'package:yaml_edit/yaml_edit.dart'; | ||
|
|
||
| /// Sorts the dependencies (dependencies, dev_dependencies, dependency_overrides) | ||
| /// in pubspec.yaml alphabetically. | ||
| /// | ||
| /// Returns true if changes were made to the file, and false otherwise | ||
| /// (if the file doesn't exist or is already sorted). | ||
| bool sortPubspec({String path = 'pubspec.yaml'}) { | ||
| final file = File(path); | ||
|
|
||
| // 1. Check if the file exists. | ||
| if (!file.existsSync()) { | ||
| return false; | ||
| } | ||
|
|
||
| final content = file.readAsStringSync(); | ||
| final editor = YamlEditor(content); | ||
| final yaml = loadYaml(content); | ||
|
|
||
| // 2. Define the sections that should be sorted. | ||
| final sectionsToCheck = [ | ||
| 'dependencies', | ||
| 'dev_dependencies', | ||
| 'dependency_overrides' | ||
|
Franklyn-R-Silva marked this conversation as resolved.
|
||
| ]; | ||
|
Franklyn-R-Silva marked this conversation as resolved.
|
||
|
|
||
| var changed = false; | ||
|
|
||
| // 3. Iterate over sections and apply sorting logic. | ||
| for (final section in sectionsToCheck) { | ||
| if (yaml is Map && yaml.containsKey(section)) { | ||
| final sectionMap = yaml[section]; | ||
|
|
||
| // Skip if the section is not a Map or is empty | ||
| if (sectionMap is! Map || sectionMap.isEmpty) continue; | ||
|
|
||
| // Get the keys (package names) | ||
| final keys = sectionMap.keys.map((e) => e.toString()).toList(); | ||
|
|
||
| // Create a list of sorted keys | ||
| final sortedKeys = List<String>.from(keys)..sort(); | ||
|
|
||
| // If the current order is different from the sorted order, apply the change | ||
| if (keys.toString() != sortedKeys.toString()) { | ||
|
Franklyn-R-Silva marked this conversation as resolved.
|
||
| // Recreate the map with the new order | ||
| final sortedMap = {for (var key in sortedKeys) key: sectionMap[key]}; | ||
|
|
||
| // Update the file virtually (using yaml_edit to preserve comments and structure) | ||
| editor.update([section], sortedMap); | ||
| changed = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 4. If any change was made, write the new content to the file. | ||
| if (changed) { | ||
| file.writeAsStringSync(editor.toString()); | ||
| } | ||
|
|
||
| return changed; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is no error handling for malformed YAML files. If the pubspec.yaml contains invalid YAML syntax, loadYaml will throw a YamlException, which will crash the program. Consider wrapping the YAML parsing in a try-catch block and returning false on error, similar to how the function handles missing files.