Skip to content

Commit

Permalink
+chore: Clean code and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
robmllze committed Sep 4, 2024
1 parent 65bba9b commit 9fa5b58
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 73 deletions.
40 changes: 36 additions & 4 deletions .github/scripts/update_changelog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'dart:io';

void main(List<String> 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()) {
Expand All @@ -29,19 +29,19 @@ void main(List<String> 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);
Expand Down Expand Up @@ -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<int> 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;
}
52 changes: 41 additions & 11 deletions .github/workflows/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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~
Expand All @@ -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
23 changes: 5 additions & 18 deletions DEVELOPER_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Developer Notes

## Commmit Tag Descriptions
## Some Commin Commmit Tags

- `feat`: New feature or enhancement
- `fix`: Bug fix or issue resolution
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ linter:
unnecessary_this: true

analyzer:
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
exclude:
- build/**

Expand All @@ -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
Expand Down
23 changes: 16 additions & 7 deletions lib/src/configs/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import '/src/_index.g.dart';
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

/// A configuration class, used to map strings to values.
class Config<TConfigRef extends ConfigRef> extends Equatable {
class Config<TConfigRef extends ConfigRef<dynamic, dynamic>> extends Equatable {
//
//
//
Expand All @@ -28,10 +28,10 @@ class Config<TConfigRef extends ConfigRef> extends Equatable {
final TConfigRef? ref;

/// The parsed fields of the config.
late final Map parsedFields;
late final Map<dynamic, dynamic> parsedFields;

// The unparsed data of the config.
late final Map data;
late final Map<dynamic, dynamic> data;

//
//
Expand All @@ -56,13 +56,22 @@ class Config<TConfigRef extends ConfigRef> extends Equatable {
//

/// Sets the fields of the config from a JSON map.
void setFields(Map data) {
void setFields(Map<dynamic, dynamic> 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(),
),
),
);
}

//
Expand All @@ -77,7 +86,7 @@ class Config<TConfigRef extends ConfigRef> 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,
Expand All @@ -92,7 +101,7 @@ class Config<TConfigRef extends ConfigRef> extends Equatable {
data,
settings: settingsOverride,
);
final res = let<T>(r) ?? fallback;
final res = letOrNull<T>(r) ?? fallback;
return res;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/extract_scopes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ Iterable<String> extractScopes(
return result.isNotEmpty ? result : null;
}

return letAsOrNull<List>($parse())?.map((e) => e?.toString()).nonNulls ?? [];
return letAsOrNull<List<dynamic>>($parse())?.map((e) => e?.toString()).nonNulls ?? [];
}
7 changes: 3 additions & 4 deletions lib/src/utils/parse_source_for_strings_and_comments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions lib/src/utils/recursive_replace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<dynamic, dynamic> recursiveReplace(
Map<dynamic, dynamic> 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<T>(dynamic inputKey, dynamic inputValue) {
dynamic $replace(dynamic inputKey, dynamic inputValue) {
dynamic r;
if (inputValue is Map) {
r = {};
r = <dynamic, dynamic>{};
for (final e in inputValue.entries) {
final k = e.key;

Expand Down Expand Up @@ -65,5 +65,5 @@ Map recursiveReplace(
}

final res = $replace('', input);
return res;
return res as Map<dynamic, dynamic>;
}
Loading

0 comments on commit 9fa5b58

Please sign in to comment.