From dabb2c34c276d770760c7e938d969630b4bf97ff Mon Sep 17 00:00:00 2001 From: ronnnnn Date: Sun, 16 Feb 2025 18:18:18 +0900 Subject: [PATCH] feat: nilts_core --- .github/workflows/publish.yml | 13 +- .release-please-manifest.json | 3 +- melos.yaml | 20 +- packages/nilts/LICENSE | 2 +- packages/nilts/README.md | 4 +- packages/nilts_core/.gitignore | 24 ++ packages/nilts_core/.pubignore | 2 + packages/nilts_core/CHANGELOG.md | 0 packages/nilts_core/LICENSE | 21 ++ packages/nilts_core/README.md | 36 +++ packages/nilts_core/analysis_options.yaml | 1 + packages/nilts_core/example/example.md | 1 + packages/nilts_core/lib/nilts_core.dart | 2 + packages/nilts_core/lib/src/dart_version.dart | 217 ++++++++++++++++++ .../lib/src/utils/library_element_ext.dart | 20 ++ packages/nilts_core/pubspec.yaml | 35 +++ .../test/dart_version_test.dart | 4 +- release-please-config.json | 59 +++++ 18 files changed, 451 insertions(+), 13 deletions(-) create mode 100644 packages/nilts_core/.gitignore create mode 100644 packages/nilts_core/.pubignore create mode 100644 packages/nilts_core/CHANGELOG.md create mode 100644 packages/nilts_core/LICENSE create mode 100644 packages/nilts_core/README.md create mode 100644 packages/nilts_core/analysis_options.yaml create mode 100644 packages/nilts_core/example/example.md create mode 100644 packages/nilts_core/lib/nilts_core.dart create mode 100644 packages/nilts_core/lib/src/dart_version.dart create mode 100644 packages/nilts_core/lib/src/utils/library_element_ext.dart create mode 100644 packages/nilts_core/pubspec.yaml rename packages/{nilts_test => nilts_core}/test/dart_version_test.dart (98%) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 45e0a95..194e103 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,12 +4,23 @@ on: push: tags: - 'nilts-v[0-9]+.[0-9]+.[0-9]+*' + - 'nilts_core-v[0-9]+.[0-9]+.[0-9]+*' jobs: - publish: + publish_nilts: + if: startsWith(github.ref, 'refs/tags/nilts-v') permissions: id-token: write # Required for authentication using OIDC uses: dart-lang/setup-dart/.github/workflows/publish.yml@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c # v1 with: environment: 'pub.dev' working-directory: packages/nilts + + publish_nilts_core: + if: startsWith(github.ref, 'refs/tags/nilts_core-v') + permissions: + id-token: write # Required for authentication using OIDC + uses: dart-lang/setup-dart/.github/workflows/publish.yml@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c # v1 + with: + environment: 'pub.dev' + working-directory: packages/nilts_core diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 3ec9dc4..755f0be 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,4 @@ { - "packages/nilts": "0.18.3" + "packages/nilts": "0.18.3", + "packages/nilts_core": "0.1.0" } diff --git a/melos.yaml b/melos.yaml index 9c52d41..46a84c6 100644 --- a/melos.yaml +++ b/melos.yaml @@ -50,11 +50,13 @@ scripts: description: lint lint:dart: - run: melos exec -c 1 --scope="nilts" -- dart analyze --fatal-infos --fatal-warnings + run: melos exec -c 1 --ignore="nilts_test" -- dart analyze --fatal-infos --fatal-warnings description: lint with dart lint:flutter: - run: melos exec -c 1 --scope="nilts_test" -- flutter analyze --fatal-infos --fatal-warnings + exec: flutter analyze --fatal-infos --fatal-warnings + packageFilters: + scope: nilts_test description: lint with flutter fmt: @@ -92,13 +94,15 @@ scripts: description: test test:custom_lint: - run: melos exec -c 1 --scope="nilts_test" -- flutter pub run custom_lint + exec: flutter pub run custom_lint + packageFilters: + scope: nilts_test description: custom_lint test test:unit: exec: very_good test packageFilters: - scope: nilts_test + scope: nilts_core description: unit test build:example: @@ -108,9 +112,13 @@ scripts: description: build on nilts_example build:example:android: - run: melos exec -c 1 --scope="nilts_example" -- flutter build appbundle + exec: flutter build appbundle + packageFilters: + scope: nilts_example description: build android on nilts_example build:example:ios: - run: melos exec -c 1 --scope="nilts_example" -- flutter build ios --no-codesign + exec: flutter build ios --no-codesign + packageFilters: + scope: nilts_example description: build ios on nilts_example diff --git a/packages/nilts/LICENSE b/packages/nilts/LICENSE index d005946..b1fd748 100644 --- a/packages/nilts/LICENSE +++ b/packages/nilts/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Seiya Kokushi +Copyright (c) 2023- Seiya Kokushi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/packages/nilts/README.md b/packages/nilts/README.md index ed75e0b..071ccc2 100644 --- a/packages/nilts/README.md +++ b/packages/nilts/README.md @@ -8,9 +8,9 @@ nilts is lint rules, quick fixes and assists for Dart and Flutter projects that [![pub][badge-pub]](https://pub.dev/packages/nilts) [![license][badge-license]](https://github.com/dassssshers/nilts/blob/main/packages/nilts/LICENSE) -[badge-build]: https://img.shields.io/github/actions/workflow/status/ronnnnn/nilts/build.yml?style=for-the-badge&logo=github%20actions&logoColor=%232088FF&color=gray&link=https%3A%2F%2Fgithub.com%2Fronnnnn%2Fnilts%2Factions%2Fworkflows%2Fbuild.yml +[badge-build]: https://img.shields.io/github/actions/workflow/status/dassssshers/nilts/build.yml?style=for-the-badge&logo=github%20actions&logoColor=%232088FF&color=gray&link=https%3A%2F%2Fgithub.com%2Fdassssshers%2Fnilts%2Factions%2Fworkflows%2Fbuild.yml [badge-pub]: https://img.shields.io/pub/v/nilts?style=for-the-badge&logo=dart&logoColor=%230175C2&color=gray&link=https%3A%2F%2Fpub.dev%2Fpackages%2Fnilts -[badge-license]: https://img.shields.io/badge/license-mit-green?style=for-the-badge&logo=github&logoColor=%23181717&color=gray&link=https%3A%2F%2Fgithub.com%2Fronnnnn%2Fnilts%2Fblob%2Fmain%2Fpackages%2Fnilts%2FLICENSE +[badge-license]: https://img.shields.io/badge/license-mit-green?style=for-the-badge&logo=github&logoColor=%23181717&color=gray&link=https%3A%2F%2Fgithub.com%2Fdassssshers%2Fnilts%2Fblob%2Fmain%2Fpackages%2Fnilts%2FLICENSE diff --git a/packages/nilts_core/.gitignore b/packages/nilts_core/.gitignore new file mode 100644 index 0000000..ddb6f32 --- /dev/null +++ b/packages/nilts_core/.gitignore @@ -0,0 +1,24 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock +**/doc/api/ +.packages +build/ diff --git a/packages/nilts_core/.pubignore b/packages/nilts_core/.pubignore new file mode 100644 index 0000000..f891fbb --- /dev/null +++ b/packages/nilts_core/.pubignore @@ -0,0 +1,2 @@ +# Resources +/resources \ No newline at end of file diff --git a/packages/nilts_core/CHANGELOG.md b/packages/nilts_core/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/packages/nilts_core/LICENSE b/packages/nilts_core/LICENSE new file mode 100644 index 0000000..b1fd748 --- /dev/null +++ b/packages/nilts_core/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023- Seiya Kokushi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/nilts_core/README.md b/packages/nilts_core/README.md new file mode 100644 index 0000000..28a6283 --- /dev/null +++ b/packages/nilts_core/README.md @@ -0,0 +1,36 @@ +
+ +# nilts_core + +nilts_core is a core package for the [nilts](https://github.com/dassssshers/nilts). + +[![build][badge-build]](https://github.com/dassssshers/nilts/actions/workflows/build.yml) +[![pub][badge-pub]](https://pub.dev/packages/nilts_core) +[![license][badge-license]](https://github.com/dassssshers/nilts/blob/main/packages/nilts_core/LICENSE) + +[badge-build]: https://img.shields.io/github/actions/workflow/status/dassssshers/nilts/build.yml?style=for-the-badge&logo=github%20actions&logoColor=%232088FF&color=gray&link=https%3A%2F%2Fgithub.com%2Fdassssshers%2Fnilts%2Factions%2Fworkflows%2Fbuild.yml +[badge-pub]: https://img.shields.io/pub/v/nilts_core?style=for-the-badge&logo=dart&logoColor=%230175C2&color=gray&link=https%3A%2F%2Fpub.dev%2Fpackages%2Fnilts_core +[badge-license]: https://img.shields.io/badge/license-mit-green?style=for-the-badge&logo=github&logoColor=%23181717&color=gray&link=https%3A%2F%2Fgithub.com%2Fdassssshers%2Fnilts%2Fblob%2Fmain%2Fpackages%2Fnilts_core%2FLICENSE + +
+ +--- + +## Contents + +- [Feature requests](#feature-requests) +- [Bug reports](#bug-reports) +- [Contributing](#contributing) + +## Feature requests + +If you have any feature requests, please create [an issue from this template](https://github.com/dassssshers/nilts/issues/new?&labels=feat&template=feat.yml). + +## Bug reports + +If you find any bugs, please create [an issue from this template](https://github.com/dassssshers/nilts/issues/new?&labels=bug&template=bug.yml). + +## Contributing + +Welcome your contributions!! +Please read [CONTRIBUTING](https://github.com/dassssshers/nilts/blob/main/CONTRIBUTING.md) docs before submitting your PR. diff --git a/packages/nilts_core/analysis_options.yaml b/packages/nilts_core/analysis_options.yaml new file mode 100644 index 0000000..f04c6cf --- /dev/null +++ b/packages/nilts_core/analysis_options.yaml @@ -0,0 +1 @@ +include: ../../analysis_options.yaml diff --git a/packages/nilts_core/example/example.md b/packages/nilts_core/example/example.md new file mode 100644 index 0000000..f58edcf --- /dev/null +++ b/packages/nilts_core/example/example.md @@ -0,0 +1 @@ +Refer to [nilts_example](https://github.com/dassssshers/nilts/tree/main/examples/nilts_example) for example project. diff --git a/packages/nilts_core/lib/nilts_core.dart b/packages/nilts_core/lib/nilts_core.dart new file mode 100644 index 0000000..d737bf3 --- /dev/null +++ b/packages/nilts_core/lib/nilts_core.dart @@ -0,0 +1,2 @@ +export 'src/dart_version.dart'; +export 'src/utils/library_element_ext.dart'; diff --git a/packages/nilts_core/lib/src/dart_version.dart b/packages/nilts_core/lib/src/dart_version.dart new file mode 100644 index 0000000..7730fdc --- /dev/null +++ b/packages/nilts_core/lib/src/dart_version.dart @@ -0,0 +1,217 @@ +import 'dart:io'; + +import 'package:meta/meta.dart'; + +/// A class that represents the Dart version. +/// +/// The Dart versioning string from [Platform.version] is as follows: +/// +/// ```txt +/// // stable channel +/// 3.0.5 (stable) (Mon Jun 12 18:31:49 2023 +0000) on "macos_arm64" +/// // beta channel +/// 3.0.0-417.4.beta (beta) (Tue May 2 10:26:14 2023 +0000) on "macos_arm64" +/// // dev channel +/// 3.0.0-417.0.dev (dev) (Thu Apr 6 09:10:27 2023 -0700) on "macos_arm64" +/// ``` +/// +/// See also: +/// +/// - [Get the Dart SDK | Dart](https://dart.dev/get-dart#release-channels) +@immutable +class DartVersion { + /// Creates a new [DartVersion] instance. + const DartVersion({ + required this.major, + required this.minor, + required this.patch, + this.pre, + this.prePatch, + this.channel = DartReleaseChannel.stable, + }); + + /// Creates a new [DartVersion] instance from [Platform.version]. + factory DartVersion.fromPlatform() { + return DartVersion.fromString(Platform.version); + } + + /// Creates a new [DartVersion] instance from the given [version]. + factory DartVersion.fromString(String version) { + final pattern = RegExp( + r'^(?0|[1-9]\d*)\.(?0|[1-9]\d*)\.(?0|[1-9]\d*)(-(?
0|[1-9]\d*)\.(?0|[1-9]\d*)\.(?(beta|dev)?))?.*$',
+    );
+    final match = pattern.firstMatch(version);
+    if (match == null) {
+      throw ArgumentError('Invalid Dart versioning string');
+    }
+
+    final major = match.namedGroup('major');
+    final minor = match.namedGroup('minor');
+    final patch = match.namedGroup('patch');
+    final pre = match.namedGroup('pre');
+    final prePatch = match.namedGroup('prePatch');
+    final channelString = match.namedGroup('channel');
+
+    if (major == null || minor == null || patch == null) {
+      throw ArgumentError('Invalid Dart versioning string');
+    }
+    if (channelString != null &&
+        channelString != 'beta' &&
+        channelString != 'dev') {
+      throw ArgumentError('Invalid Dart versioning string');
+    }
+    if (channelString != null && (pre == null || prePatch == null)) {
+      throw ArgumentError('Invalid Dart versioning string');
+    }
+    if (channelString == null && (pre != null || prePatch != null)) {
+      throw ArgumentError('Invalid Dart versioning string');
+    }
+
+    final DartReleaseChannel channel;
+    switch (channelString) {
+      case 'dev':
+        channel = DartReleaseChannel.dev;
+      case 'beta':
+        channel = DartReleaseChannel.beta;
+      case null:
+        channel = DartReleaseChannel.stable;
+      default:
+        throw ArgumentError('Invalid Dart versioning string');
+    }
+
+    return DartVersion(
+      major: int.parse(major),
+      minor: int.parse(minor),
+      patch: int.parse(patch),
+      pre: pre == null ? null : int.parse(pre),
+      prePatch: prePatch == null ? null : int.parse(prePatch),
+      channel: channel,
+    );
+  }
+
+  /// The major version.
+  final int major;
+
+  /// The minor version.
+  final int minor;
+
+  /// The patch version.
+  final int patch;
+
+  /// The prerelease version.
+  final int? pre;
+
+  /// The prerelease patch version.
+  final int? prePatch;
+
+  /// The channel
+  final DartReleaseChannel channel;
+
+  /// Whether this version is stable.
+  bool get isStable => channel == DartReleaseChannel.stable;
+
+  /// Whether this version is beta.
+  bool get isBeta => channel == DartReleaseChannel.beta;
+
+  /// Whether this version is dev.
+  bool get isDev => channel == DartReleaseChannel.dev;
+
+  /// Whether this version is newer release than [other].
+  ///
+  /// Returns `1` if this version is newer than [other].
+  /// Returns `-1` if this version is older than [other].
+  /// Returns `0` if this version is same as [other].
+  int compareTo(DartVersion other) {
+    if (major != other.major) {
+      return major.compareTo(other.major);
+    }
+    if (minor != other.minor) {
+      return minor.compareTo(other.minor);
+    }
+    if (patch != other.patch) {
+      return patch.compareTo(other.patch);
+    }
+    if (channel != other.channel) {
+      if (isStable) {
+        return 1;
+      } else if (isBeta) {
+        if (other.isStable) {
+          return -1;
+        } else if (other.isDev) {
+          return 1;
+        }
+      } else if (isDev) {
+        if (other.isStable) {
+          return -1;
+        } else if (other.isBeta) {
+          return -1;
+        }
+      }
+    }
+    if (pre != other.pre) {
+      return pre!.compareTo(other.pre!);
+    }
+    if (prePatch != other.prePatch) {
+      return prePatch!.compareTo(other.prePatch!);
+    }
+    return 0;
+  }
+
+  @override
+  bool operator ==(Object other) =>
+      identical(this, other) ||
+      other is DartVersion &&
+          runtimeType == other.runtimeType &&
+          major == other.major &&
+          minor == other.minor &&
+          patch == other.patch &&
+          pre == other.pre &&
+          prePatch == other.prePatch &&
+          channel == other.channel;
+
+  @override
+  int get hashCode =>
+      major.hashCode ^
+      minor.hashCode ^
+      patch.hashCode ^
+      patch.hashCode ^
+      prePatch.hashCode ^
+      channel.hashCode;
+
+  /// Whether this version is older release than [other].
+  bool operator <(DartVersion other) => compareTo(other) < 0;
+
+  /// Whether this version is older or same release than [other].
+  bool operator <=(DartVersion other) => compareTo(other) <= 0;
+
+  /// Whether this version is newer release than [other].
+  bool operator >(DartVersion other) => compareTo(other) > 0;
+
+  /// Whether this version is newer or same release than [other].
+  bool operator >=(DartVersion other) => compareTo(other) >= 0;
+
+  @override
+  String toString() {
+    final mainVersion = '$major.$minor.$patch';
+    if (isStable) return mainVersion;
+
+    return '$mainVersion-$pre.$prePatch.$channel';
+  }
+}
+
+/// Enum of Dart release channel.
+///
+/// See also:
+///
+/// - [Get the Dart SDK | Dart](https://dart.dev/get-dart#release-channels)
+enum DartReleaseChannel {
+  /// Stable channel.
+  stable,
+
+  /// Beta channel.
+  beta,
+
+  /// Dev channel.
+  dev,
+  ;
+}
diff --git a/packages/nilts_core/lib/src/utils/library_element_ext.dart b/packages/nilts_core/lib/src/utils/library_element_ext.dart
new file mode 100644
index 0000000..f256f4f
--- /dev/null
+++ b/packages/nilts_core/lib/src/utils/library_element_ext.dart
@@ -0,0 +1,20 @@
+import 'package:analyzer/dart/element/element.dart';
+
+/// Extension methods for [LibraryElement].
+extension LibraryElementExt on LibraryElement {
+  /// Returns `true` if this library is from the package
+  /// with the given [packageName].
+  bool checkPackage({required String packageName}) {
+    final libraryUri = Uri.tryParse(identifier);
+    if (libraryUri == null) return false;
+    if (libraryUri.scheme != 'package') return false;
+    if (libraryUri.pathSegments.first != packageName) return false;
+    return true;
+  }
+
+  /// Returns `true` if this library is from the package `flutter`.
+  bool get isFlutter => checkPackage(packageName: 'flutter');
+
+  /// Returns `true` if this library is from the package `flutter_test`.
+  bool get isFlutterTest => checkPackage(packageName: 'flutter_test');
+}
diff --git a/packages/nilts_core/pubspec.yaml b/packages/nilts_core/pubspec.yaml
new file mode 100644
index 0000000..c422b02
--- /dev/null
+++ b/packages/nilts_core/pubspec.yaml
@@ -0,0 +1,35 @@
+# Additional information about this file can be found at
+# https://dart.dev/tools/pub/pubspec
+
+name: nilts_core
+version: 0.1.0
+description: core package for nilts.
+homepage: https://github.com/dassssshers/nilts
+repository: https://github.com/dassssshers/nilts
+issue_tracker: https://github.com/dassssshers/nilts/issues
+platforms:
+  android:
+  ios:
+  linux:
+  macos:
+  web:
+  windows:
+funding:
+  - https://github.com/sponsors/ronnnnn
+topics:
+  - lints
+  - lint
+  - analysis
+  - code-style
+  - tools
+
+environment:
+  sdk: '>=3.0.0 <4.0.0'
+
+dependencies:
+  analyzer: ^7.0.0
+  meta: ^1.9.1
+
+dev_dependencies:
+  test: ^1.25.15
+  very_good_analysis: ^7.0.0
diff --git a/packages/nilts_test/test/dart_version_test.dart b/packages/nilts_core/test/dart_version_test.dart
similarity index 98%
rename from packages/nilts_test/test/dart_version_test.dart
rename to packages/nilts_core/test/dart_version_test.dart
index 4d5566a..2e68229 100644
--- a/packages/nilts_test/test/dart_version_test.dart
+++ b/packages/nilts_core/test/dart_version_test.dart
@@ -1,5 +1,5 @@
-import 'package:flutter_test/flutter_test.dart';
-import 'package:nilts/src/dart_version.dart';
+import 'package:nilts_core/nilts_core.dart';
+import 'package:test/test.dart';
 
 void main() {
   test('Test regex match', () async {
diff --git a/release-please-config.json b/release-please-config.json
index 68c2346..5a14eb3 100644
--- a/release-please-config.json
+++ b/release-please-config.json
@@ -58,6 +58,65 @@
       "pull-request-title-pattern": "release: ${component} ${version}",
       "release-label": "release",
       "release-type": "dart"
+    },
+    "packages/nilts_core": {
+      "bump-minor-pre-major": true,
+      "bump-patch-for-minor-pre-major": false,
+      "changelog-path": "CHANGELOG.md",
+      "changelog-sections": [
+        {
+          "type": "build",
+          "section": "Build System"
+        },
+        {
+          "type": "clean",
+          "section": "Code Cleanup"
+        },
+        {
+          "type": "docs",
+          "section": "Documentation"
+        },
+        {
+          "type": "feat",
+          "section": "New Features"
+        },
+        {
+          "type": "fix",
+          "section": "Bug Fixes"
+        },
+        {
+          "type": "refactor",
+          "section": "Code Refactoring"
+        },
+        {
+          "type": "revert",
+          "section": "Reverts"
+        },
+        {
+          "type": "style",
+          "section": "Code Style"
+        },
+        {
+          "type": "test",
+          "section": "Tests"
+        },
+        {
+          "type": "update",
+          "section": "Feature Updates"
+        },
+        {
+          "type": "upgrade",
+          "section": "Dependency Upgrades"
+        }
+      ],
+      "draft": false,
+      "include-v-in-tag": true,
+      "label": "release",
+      "package-name": "nilts_core",
+      "prerelease": false,
+      "pull-request-title-pattern": "release: ${component} ${version}",
+      "release-label": "release",
+      "release-type": "dart"
     }
   },
   "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json"