├── .gitignore ├── .metadata ├── CHANGELOG.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── bin ├── main.dart └── pubspec_dependency_sorter.dart ├── images └── pubspec _dependency_sorter.png ├── lib └── pubspec_dependency_sorter.dart ├── pubspec.yaml └── test └── pubspec_dependency_sorter.dart /.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | migrate_working_dir/ 12 | 13 | # IntelliJ related 14 | *.iml 15 | *.ipr 16 | *.iws 17 | .idea/ 18 | 19 | # The .vscode folder contains launch configuration and tasks you configure in 20 | # VS Code which you may wish to be included in version control, so this line 21 | # is commented out by default. 22 | #.vscode/ 23 | 24 | # Flutter/Dart/Pub related 25 | # Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. 26 | /pubspec.lock 27 | **/doc/api/ 28 | .dart_tool/ 29 | .packages 30 | build/ 31 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: e3c29ec00c9c825c891d75054c63fcc46454dca1 8 | channel: stable 9 | 10 | project_type: package 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.0.1 2 | 3 | * First release 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2022, Danche Ng'ang'a. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 13 | Package helps to sort the flutter/dart packages and plugins alphabetically, This makes it easier when managing too many packages and when working with teams 14 | 15 | ## Features 16 | 17 | * sort dependencies 18 | * prevent dependecy conflicts 19 | * clean up the pubspec.yaml file 20 | 21 | ## Getting started 22 | Install flutter or dart sdk then depend on the package. 23 | ```dart 24 | dev_dependencies: 25 | pubspec_dependency_sorter: ^1.0.4 26 | ``` 27 | 28 | ## Usage 29 | 30 | To use the package run in your flutter/dart app root directory 31 | ```dart 32 | dart run pubspec_dependency_sorter 33 | ``` 34 | if your pubspec.yam is located somewhere else use the following command by passing the **path of the directory** where the pubspec.yaml file is located. 35 | ```dart 36 | dart run pubspec_dependency_sorter PATH-TO-YOUR-DIRECTORY 37 | ``` 38 | ## sample output 39 | ![dependency_sorter sample output](https://github.com/Genialngash/pubspec-dependency-sorter/blob/main/images/pubspec%20_dependency_sorter.png) 40 | 41 | 42 | ## Additional information 43 | Feel free to add features,improvemets and fix bugs then create a PR. 44 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | include: package:flutter_lints/flutter.yaml 2 | 3 | # Additional information about this file can be found at 4 | # https://dart.dev/guides/language/analysis-options 5 | -------------------------------------------------------------------------------- /bin/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:pubspec_dependency_sorter/pubspec_dependency_sorter.dart'; 2 | 3 | void main(List args) { 4 | pubsecDependencySorter(args: args); 5 | } 6 | -------------------------------------------------------------------------------- /bin/pubspec_dependency_sorter.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022, Danche Nganga. All rights reserved. Use of this source code 2 | // is governed by a BSD-style license that can be found in the LICENSE file. 3 | import 'dart:io'; 4 | import 'package:logger/logger.dart'; 5 | import 'package:pubspec/pubspec.dart'; 6 | import 'package:yaml_writer/yaml_writer.dart'; 7 | 8 | main(List args) async { 9 | var logger = Logger( 10 | filter: NewFilter(), // log in release and debug mode 11 | printer: PrettyPrinter( 12 | methodCount: 0, // number of method calls to be displayed 13 | errorMethodCount: 8, // number of method calls if stacktrace is provided 14 | colors: true, // Colorful log messages 15 | printEmojis: true, // Print an emoji for each log message 16 | printTime: true // Should each log print contain a timestamp 17 | ), // Use the PrettyPrinter to format and print log 18 | output: null, // Use the default LogOutput (-> send everything to console) 19 | ); 20 | 21 | logger.w("Starting.."); 22 | try { 23 | String path = ''; 24 | if (args.isEmpty) { 25 | path = Directory.current.path; 26 | } else { 27 | path = args[0]; 28 | } 29 | // specify the directory 30 | Directory myDirectory = Directory(path); 31 | 32 | // load pubSpec 33 | var pubSpec = await PubSpec.load(myDirectory); 34 | //get the dependencies 35 | Map dependencies = pubSpec.dependencies; 36 | //get the dev dependency override 37 | Map devDependencies = pubSpec.devDependencies; 38 | //get the dependency override 39 | Map dependencyOverrides = 40 | pubSpec.dependencyOverrides; 41 | 42 | // sort the dependencies 43 | var sortedDependencies = _sortDependencies(dependencies); 44 | logger.i("<<<--- sorted dependencies."); 45 | 46 | //sort dev dependency 47 | var sortedDevDependencies = _sortDependencies(devDependencies); 48 | logger.i("<<<--- sorted dev dependencies."); 49 | 50 | //sort dependency override 51 | var sortedDependencyOverrides = _sortDependencies(dependencyOverrides); 52 | logger.i("<<<---- sorted dependency overrides."); 53 | // change the dependencies and dependency overrides 54 | 55 | // logger.i("Sorted dependencies: ${sortDependenciesByKey.toString()}"); 56 | var newPubSpec = pubSpec.copy( 57 | dependencies: sortedDependencies, 58 | dependencyOverrides: sortedDependencyOverrides, 59 | devDependencies: sortedDevDependencies, 60 | unParsedYaml: pubSpec.unParsedYaml, 61 | ); 62 | 63 | // save it with yaml writer 64 | var yamlWriter = YamlWriter(allowUnquotedStrings: true); 65 | 66 | // Convert the pubspec to a yaml document 67 | var yamlDoc = yamlWriter.write(newPubSpec.toJson()); 68 | 69 | // Write the yaml to the pubspec.yaml file 70 | File file = File("${myDirectory.path}/pubspec.yaml"); 71 | await file.writeAsString(yamlDoc); 72 | 73 | logger.i("Saved the changes"); 74 | 75 | logger.i( 76 | "Done---< please star and like the package. https://github.com/Genialngash/pubspec-dependency-sorter >"); 77 | } catch (e) { 78 | logger.e(e); 79 | } 80 | } 81 | 82 | Map _sortDependencies( 83 | Map dependencies) { 84 | // Convert the dependencies to a list and sort them 85 | var sortedDependencies = dependencies.keys.toList(); 86 | sortedDependencies.sort((a, b) { 87 | return a.toString().compareTo(b.toString()); 88 | }); 89 | 90 | // sort the dependencies 91 | Map sortDependenciesByKey = {}; 92 | for (var key in sortedDependencies) { 93 | sortDependenciesByKey[key] = dependencies[key]!; 94 | } 95 | return sortDependenciesByKey; 96 | } 97 | 98 | class NewFilter extends LogFilter { 99 | @override 100 | bool shouldLog(LogEvent event) { 101 | return true; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /images/pubspec _dependency_sorter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danchengash/pubspec-dependency-sorter/e1a4118e9c67c257f2232e28c8de4d84ad0d3508/images/pubspec _dependency_sorter.png -------------------------------------------------------------------------------- /lib/pubspec_dependency_sorter.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022, Danche Nganga. All rights reserved. Use of this source code 2 | // is governed by a BSD-style license that can be found in the LICENSE file. 3 | import 'dart:io'; 4 | 5 | import 'package:logger/logger.dart'; 6 | import 'package:pubspec/pubspec.dart'; 7 | import 'package:yaml_writer/yaml_writer.dart'; 8 | 9 | pubsecDependencySorter({required List args}) async { 10 | var logger = Logger( 11 | filter: NewFilter(), // log in release and debug mode 12 | printer: PrettyPrinter( 13 | methodCount: 0, // number of method calls to be displayed 14 | errorMethodCount: 8, // number of method calls if stacktrace is provided 15 | colors: true, // Colorful log messages 16 | printEmojis: true, // Print an emoji for each log message 17 | printTime: true // Should each log print contain a timestamp 18 | ), // Use the PrettyPrinter to format and print log 19 | output: null, // Use the default LogOutput (-> send everything to console) 20 | ); 21 | 22 | logger.w("Starting.."); 23 | try { 24 | String path = ''; 25 | if (args.isEmpty) { 26 | path = Directory.current.path; 27 | } else { 28 | path = args[0]; 29 | } 30 | // specify the directory 31 | Directory myDirectory = Directory(path); 32 | 33 | // load pubSpec 34 | var pubSpec = await PubSpec.load(myDirectory); 35 | //get the dependencies 36 | Map dependencies = pubSpec.dependencies; 37 | //get the dev dependency override 38 | Map devDependencies = pubSpec.devDependencies; 39 | //get the dependency override 40 | Map dependencyOverrides = 41 | pubSpec.dependencyOverrides; 42 | 43 | // sort the dependencies 44 | var sortedDependencies = _sortDependencies(dependencies); 45 | logger.i("<<<--- sorted dependencies."); 46 | 47 | //sort dev dependency 48 | var sortedDevDependencies = _sortDependencies(devDependencies); 49 | logger.i("<<<--- sorted dev dependencies."); 50 | 51 | //sort dependency override 52 | var sortedDependencyOverrides = _sortDependencies(dependencyOverrides); 53 | logger.i("<<<---- sorted dependency overrides."); 54 | // change the dependencies and dependency overrides 55 | 56 | // logger.i("Sorted dependencies: ${sortDependenciesByKey.toString()}"); 57 | var newPubSpec = pubSpec.copy( 58 | dependencies: sortedDependencies, 59 | dependencyOverrides: sortedDependencyOverrides, 60 | devDependencies: sortedDevDependencies, 61 | ); 62 | 63 | // save it with yaml writer 64 | var yamlWriter = YamlWriter(allowUnquotedStrings: true); 65 | 66 | // Convert the pubspec to a yaml document 67 | var yamlDoc = yamlWriter.write(newPubSpec.toJson()); 68 | 69 | // Write the yaml to the pubspec.yaml file 70 | File file = File("${myDirectory.path}/pubspec.yaml"); 71 | await file.writeAsString(yamlDoc); 72 | 73 | logger.i("Saved the changes"); 74 | 75 | logger.i( 76 | "Done---< please star and like the package. https://github.com/Genialngash/pubspec-dependency-sorter >"); 77 | } catch (e) { 78 | logger.e(e); 79 | } 80 | } 81 | 82 | Map _sortDependencies( 83 | Map dependencies) { 84 | // Convert the dependencies to a list and sort them 85 | var sortedDependencies = dependencies.keys.toList(); 86 | sortedDependencies.sort((a, b) { 87 | return a.toString().compareTo(b.toString()); 88 | }); 89 | 90 | // sort the dependencies 91 | Map sortDependenciesByKey = {}; 92 | for (var key in sortedDependencies) { 93 | sortDependenciesByKey[key] = dependencies[key]!; 94 | } 95 | return sortDependenciesByKey; 96 | } 97 | 98 | class NewFilter extends LogFilter { 99 | @override 100 | bool shouldLog(LogEvent event) { 101 | return true; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: pubspec_dependency_sorter 2 | version: 1.0.5 3 | Author: Danche Nganga 4 | homepage: https://github.com/danchengash/pubspec-dependency-sorter 5 | description: helps sort flutter,dart pubspec.yaml dependecies alphabetically. 6 | environment: 7 | sdk: '>=2.17.0 <4.0.0' 8 | # flutter: '>=1.17.0' 9 | dependencies: 10 | logger: ^2.0.1 11 | pubspec: ^2.3.0 12 | flutter: 13 | sdk: flutter 14 | yaml_writer: ^2.0.0 15 | dev_dependencies: 16 | flutter_lints: ^2.0.0 17 | flutter_test: 18 | sdk: flutter -------------------------------------------------------------------------------- /test/pubspec_dependency_sorter.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter_test/flutter_test.dart'; 2 | 3 | 4 | void main() { 5 | test('adds one to input values', () { 6 | 7 | }); 8 | } 9 | --------------------------------------------------------------------------------