From 9fa5b58ef5bc031c274e44b7884d7aed18acb991 Mon Sep 17 00:00:00 2001 From: robmllze <86869208+robmllze@users.noreply.github.com> Date: Wed, 4 Sep 2024 13:38:12 +1000 Subject: [PATCH] +chore: Clean code and update dependencies --- .github/scripts/update_changelog.dart | 40 ++++++++++++-- .github/workflows/prepare.yml | 52 +++++++++++++++---- .github/workflows/publish.yml | 10 ++-- DEVELOPER_NOTES.md | 23 ++------ README.md | 9 ---- analysis_options.yaml | 5 ++ lib/src/configs/config.dart | 23 +++++--- lib/src/utils/extract_scopes.dart | 2 +- ...parse_source_for_strings_and_comments.dart | 7 ++- lib/src/utils/recursive_replace.dart | 12 ++--- lib/src/utils/replace_patterns.dart | 9 ++-- pubspec.yaml | 6 +-- 12 files changed, 125 insertions(+), 73 deletions(-) diff --git a/.github/scripts/update_changelog.dart b/.github/scripts/update_changelog.dart index faf44b1..3451764 100644 --- a/.github/scripts/update_changelog.dart +++ b/.github/scripts/update_changelog.dart @@ -16,7 +16,7 @@ import 'dart:io'; void main(List args) { final version = args.isNotEmpty ? args[0] : '0.1.0'; - final newReleaseNotes = args.length > 1 ? args[1] : 'Initial commit'; + final comitMesssage = args.length > 1 ? args[1].replaceFirst('+', '') : ''; final changelogPath = 'CHANGELOG.md'; final file = File(changelogPath); if (!file.existsSync()) { @@ -29,19 +29,19 @@ void main(List args) { final versionExist = sections.where((e) => e.version == version).isNotEmpty; if (versionExist) { sections.where((e) => e.version == version).forEach((e) { - e.addUpdate(newReleaseNotes); + e.addUpdate(comitMesssage); }); } else { sections.add( _VersionSection( version: version, releasedAt: DateTime.now().toUtc(), - updates: {newReleaseNotes}, + updates: {comitMesssage}, ), ); } contents = '# Changelog\n\n${(sections.toList()..sort((a, b) { - return b.version.compareTo(a.version); + return compareVersions(b.version, a.version); })).map((e) => e.toString()).join('\n')}'; file.writeAsStringSync(contents); @@ -129,3 +129,35 @@ class _VersionSection { return '## [$version]\n\n- Released @ ${releasedAt.month}/${releasedAt.year} (UTC)\n$updatesString\n'; } } + +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ + +int compareVersions(String version1, String version2) { + List parseVersion(String version) { + // Split by the '+' first to handle the build number + final parts = version.split('+'); + final versionParts = parts[0].split('.').map(int.tryParse).map((e) => e ?? 0).toList(); + + // Add the build number as the last part (if it exists) + if (parts.length > 1) { + versionParts.add(int.tryParse(parts[1]) ?? 0); + } + + return versionParts; + } + + final v1 = parseVersion(version1); + final v2 = parseVersion(version2); + + final maxLength = v1.length > v2.length ? v1.length : v2.length; + + for (var i = 0; i < maxLength; i++) { + final part1 = i < v1.length ? v1[i] : 0; + final part2 = i < v2.length ? v2[i] : 0; + + if (part1 > part2) return 1; + if (part1 < part2) return -1; + } + + return 0; +} diff --git a/.github/workflows/prepare.yml b/.github/workflows/prepare.yml index 39f31b4..454990b 100644 --- a/.github/workflows/prepare.yml +++ b/.github/workflows/prepare.yml @@ -25,40 +25,70 @@ jobs: prepare: runs-on: ubuntu-latest steps: + # Checkout the code from the repository - name: Checkout code uses: actions/checkout@v3 + # Get the latest commit message + - name: Get commit messages + id: get_commits + run: | + COMMIT_MESSAGES=$(git log --format=%B -n 1 HEAD) + echo "::set-output name=COMMIT_MESSAGES::${COMMIT_MESSAGES}" + + # Check if the commit message starts with '+' + - name: Check commit message + id: check_message + run: | + if echo "${{ steps.get_commits.outputs.COMMIT_MESSAGES }}" | grep -q "^+"; then + echo "::set-output name=PROCEED::true" + else + echo "::set-output name=PROCEED::false" + fi + + # Debug environment variables + - name: Debug environment variables + run: | + echo "PROCEED=${{ steps.check_message.outputs.PROCEED }}" + echo "COMMIT_MESSAGES=${{ steps.get_commits.outputs.COMMIT_MESSAGES }}" + + # Set up Dart if the commit message check passed - name: Set up Dart + if: ${{ steps.check_message.outputs.PROCEED == 'true' }} uses: dart-lang/setup-dart@v1.2 + # Format Dart code if the commit message check passed - name: Format Dart code + if: ${{ steps.check_message.outputs.PROCEED == 'true' }} run: dart format . + # Apply Dart fixes if the commit message check passed - name: Apply Dart fixes + if: ${{ steps.check_message.outputs.PROCEED == 'true' }} run: dart fix --apply + # Extract the version from pubspec.yaml if the commit message check passed - name: Extract version from pubspec.yaml + if: ${{ steps.check_message.outputs.PROCEED == 'true' }} id: get_version run: | VERSION=$(grep "version:" pubspec.yaml | sed 's/version: //') - echo "Extracted version: $VERSION" - echo "::set-output name=extracted_version::$VERSION" - - - name: Get commit messages - id: get_commits - run: | - COMMIT_MESSAGES=$(git log --format=%B -n 1 HEAD) - echo "::set-output name=messages::${COMMIT_MESSAGES}" + echo "Version extracted from pubspec.yaml: $VERSION" + echo "::set-output name=PUBSPEC_VERSION::${VERSION}" + # Update CHANGELOG.md if the commit message check passed - name: Update CHANGELOG.md + if: ${{ steps.check_message.outputs.PROCEED == 'true' }} run: | - RELEASE_NOTES="${{ steps.get_commits.outputs.messages }}" - dart run .github/scripts/update_changelog.dart "${{ steps.get_version.outputs.extracted_version }}" "$RELEASE_NOTES" + RELEASE_NOTES="${{ steps.get_commits.outputs.COMMIT_MESSAGES }}" + dart run .github/scripts/update_changelog.dart "${{ steps.get_version.outputs.PUBSPEC_VERSION }}" "$RELEASE_NOTES" + # Commit and push changes if the commit message check passed - name: Commit and push changes + if: ${{ steps.check_message.outputs.PROCEED == 'true' }} run: | git config user.name github-actions git config user.email github-actions@github.com git add . - git commit -m "Prepare version ${{ steps.get_version.outputs.extracted_version }}" + git commit -m "Prepare version ${{ steps.get_version.outputs.PUBSPEC_VERSION }}" git push diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index e27507d..b03fbf1 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,11 +1,11 @@ ##.title ## ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ## -## Dart/Flutter (DF) Packages by DevCetra.com & contributors. SSee MIT LICENSE -## file in root directory. +## Dart/Flutter (DF) Packages by DevCetra.com & contributors. The use of this +## source code is governed by an MIT-style license described in the LICENSE +## file located in this project's root directory. ## -## A workflow that publishes a Dart package to pub.dev. For more about -## publishing, see: https://dart.dev/tools/pub/automated-publishing +## See: https://opensource.org/license/mit ## ## ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ##.title~ @@ -24,5 +24,5 @@ on: jobs: publish: permissions: - id-token: write # Required for authentication using OIDC + id-token: write uses: dart-lang/setup-dart/.github/workflows/publish.yml@v1 diff --git a/DEVELOPER_NOTES.md b/DEVELOPER_NOTES.md index ccec336..b4d03e5 100644 --- a/DEVELOPER_NOTES.md +++ b/DEVELOPER_NOTES.md @@ -1,6 +1,6 @@ # Developer Notes -## Commmit Tag Descriptions +## Some Commin Commmit Tags - `feat`: New feature or enhancement - `fix`: Bug fix or issue resolution @@ -20,6 +20,10 @@ https://github.com/robmllze/YOUR_PROJECT_NAME/settings/actions +## Changelog + +If your commit message starts with `+`, the `prepare.yml` workflow will automatically format and apply fixes to the code, and add the provided commit message to `CHANGELOG.md` under the current version specified in `pubspec.yaml`. + ## Public Repo Setup ```sh @@ -46,14 +50,6 @@ git push -u origin main ## macOS and Linux -### Fetching Generators - -```bash -rm -rf ___generators/ -git clone https://github.com/robmllze/___generators.git -dart pub get -C ___generators -``` - ### Adding the Workflow ```bash @@ -71,15 +67,6 @@ find . -name '.DS_Store' -type f -delete ## Windows -### Fetching Generators - -```bash -rmdir /s /q ___generators/ -git clone https://github.com/robmllze/___generators.git -dart pub get -C ___generators -rmdir /s /q ___generators/.git -``` - ### Adding the Workflow ```bash diff --git a/README.md b/README.md index 02a2e43..825de5a 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ This is an open-source project, and we warmly welcome contributions from everyon ### Ways you can contribute: -- **Join the discussions and ask questions:** Your curiosity can lead to valuable insights and improvements. - **Buy me a coffee:** If you'd like to support the project financially, consider [buying me a coffee](https://www.buymeacoffee.com/robmllze). Your support helps cover the costs of development and keeps the project growing. - **Share your ideas:** Every perspective matters, and your ideas can spark innovation. - **Report bugs:** Help us identify and fix issues to make the project more robust. @@ -41,14 +40,6 @@ No matter how you choose to contribute, your involvement is greatly appreciated --- -### Join Reddit Discussions: - -💬 https://www.reddit.com/r/df_config/ - -### Join GitHub Discussions: - -💬 https://github.com/robmllze/df_config/discussions/ - ### Chief Maintainer: 📧 Email _Robert Mollentze_ at robmllze@gmail.com diff --git a/analysis_options.yaml b/analysis_options.yaml index 3907ad7..c3796f5 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -32,6 +32,10 @@ linter: unnecessary_this: true analyzer: + language: + strict-casts: true + strict-inference: true + strict-raw-types: true exclude: - build/** @@ -42,6 +46,7 @@ analyzer: depend_on_referenced_packages: error flutter_style_todos: error invalid_use_of_protected_member: error + invalid_override_of_non_virtual_member: error no_leading_underscores_for_local_identifiers: error prefer_final_in_for_each: error prefer_relative_imports: error diff --git a/lib/src/configs/config.dart b/lib/src/configs/config.dart index c82647d..7763ae2 100644 --- a/lib/src/configs/config.dart +++ b/lib/src/configs/config.dart @@ -19,7 +19,7 @@ import '/src/_index.g.dart'; // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ /// A configuration class, used to map strings to values. -class Config extends Equatable { +class Config> extends Equatable { // // // @@ -28,10 +28,10 @@ class Config extends Equatable { final TConfigRef? ref; /// The parsed fields of the config. - late final Map parsedFields; + late final Map parsedFields; // The unparsed data of the config. - late final Map data; + late final Map data; // // @@ -56,13 +56,22 @@ class Config extends Equatable { // /// Sets the fields of the config from a JSON map. - void setFields(Map data) { + void setFields(Map data) { this.data ..clear() ..addAll(data); this.parsedFields ..clear() - ..addAll(expandJson(recursiveReplace(data, settings: this.settings))); + ..addAll( + expandJson( + recursiveReplace( + data, + settings: this.settings, + ).mapKeys( + (e) => e.toString(), + ), + ), + ); } // @@ -77,7 +86,7 @@ class Config extends Equatable { ReplacePatternsSettings? settings, }) { final settingsOverride = settings ?? this.settings; - final expandedArgs = expandJson(args); + final expandedArgs = expandJson(args.mapKeys((e) => e.toString())); var data = { ...this.parsedFields, ...expandedArgs, @@ -92,7 +101,7 @@ class Config extends Equatable { data, settings: settingsOverride, ); - final res = let(r) ?? fallback; + final res = letOrNull(r) ?? fallback; return res; } diff --git a/lib/src/utils/extract_scopes.dart b/lib/src/utils/extract_scopes.dart index 3ae71af..302dc6c 100644 --- a/lib/src/utils/extract_scopes.dart +++ b/lib/src/utils/extract_scopes.dart @@ -57,5 +57,5 @@ Iterable extractScopes( return result.isNotEmpty ? result : null; } - return letAsOrNull($parse())?.map((e) => e?.toString()).nonNulls ?? []; + return letAsOrNull>($parse())?.map((e) => e?.toString()).nonNulls ?? []; } diff --git a/lib/src/utils/parse_source_for_strings_and_comments.dart b/lib/src/utils/parse_source_for_strings_and_comments.dart index 671f8fd..692d9d8 100644 --- a/lib/src/utils/parse_source_for_strings_and_comments.dart +++ b/lib/src/utils/parse_source_for_strings_and_comments.dart @@ -51,11 +51,10 @@ ParseSourceForStringsAndCommentsResult parseSourceForStringsAndComments( } quotedStrings.removeWhere( (a) => - singleLineComments.cast().firstWhere( + singleLineComments.firstWhere( (b) => b.contains(a), - orElse: () => null, - ) != - null, + orElse: () => '', + ) .isNotEmpty, ); return ParseSourceForStringsAndCommentsResult( List.unmodifiable(quotedStrings), diff --git a/lib/src/utils/recursive_replace.dart b/lib/src/utils/recursive_replace.dart index d4470f5..3564468 100644 --- a/lib/src/utils/recursive_replace.dart +++ b/lib/src/utils/recursive_replace.dart @@ -20,19 +20,19 @@ import 'replace_patterns.dart'; /// placeholders defined by the map's own key-value pairs. It supports nested /// structures (maps and lists) and replaces placeholders in strings with /// corresponding values. -Map recursiveReplace( - Map input, { +Map recursiveReplace( + Map input, { ReplacePatternsSettings settings = const ReplacePatternsSettings(), }) { final data = expandFlattenedJson( - flattenJson(input, separator: settings.separator), + flattenJson(input.mapKeys((e) => e.toString()), separator: settings.separator), separator: settings.separator, ); - dynamic $replace(dynamic inputKey, dynamic inputValue) { + dynamic $replace(dynamic inputKey, dynamic inputValue) { dynamic r; if (inputValue is Map) { - r = {}; + r = {}; for (final e in inputValue.entries) { final k = e.key; @@ -65,5 +65,5 @@ Map recursiveReplace( } final res = $replace('', input); - return res; + return res as Map; } diff --git a/lib/src/utils/replace_patterns.dart b/lib/src/utils/replace_patterns.dart index 7ba77f0..4d8d19a 100644 --- a/lib/src/utils/replace_patterns.dart +++ b/lib/src/utils/replace_patterns.dart @@ -14,7 +14,7 @@ /// map, supporting default values and custom delimiters. String replacePatterns( String input, - Map data, { + Map data, { ReplacePatternsSettings settings = const ReplacePatternsSettings(), }) { var output = input; @@ -30,9 +30,8 @@ String replacePatterns( final e1 = parts.elementAtOrNull(1); final key = (e1 ?? e0)!; final defaultValue = e0 ?? key; - final data1 = settings.caseSensitive - ? data - : data.map((k, v) => MapEntry(k.toString().toLowerCase(), v)); + final data1 = + settings.caseSensitive ? data : data.map((k, v) => MapEntry(k.toString().toLowerCase(), v)); final key1 = settings.caseSensitive ? key : key.toLowerCase(); final suggestedReplacementValue = data1[key1]; final replacementValue = @@ -51,7 +50,7 @@ extension ReplaceAllPatternsOnStringExtension on String { /// Replaces placeholders in this string with corresponding values from a /// provided map, supporting default values and custom delimiters. String replacePatterns( - Map data, { + Map data, { ReplacePatternsSettings settings = const ReplacePatternsSettings(), }) { return _replacePatterns( diff --git a/pubspec.yaml b/pubspec.yaml index a18c196..b5a9647 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ name: df_config description: A package that provides methods to load configuration data and access it at runtime. -version: 0.3.0 +version: 0.4.0 repository: https://github.com/robmllze/df_config funding: - https://www.buymeacoffee.com/robmllze @@ -31,8 +31,8 @@ environment: ## ----------------------------------------------------------------------------- dependencies: - df_collection: ^0.3.0 - df_type: ^0.2.3 + df_collection: ^0.4.0 + df_type: ^0.5.2 df_string: ^0.2.1 yaml: ^3.1.2 meta: ^1.15.0