2 Commits

Author SHA1 Message Date
956d70d037 Error: Not found: 'package:recase/recase.dart',引入本地recase 2022-06-06 02:13:18 +08:00
cd1a89c1b6 library version update 2022-06-06 01:14:07 +08:00
4 changed files with 152 additions and 21 deletions

View File

@@ -3,8 +3,7 @@ import 'dart:convert';
import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
import 'package:meta/meta.dart';
import 'package:recase/recase.dart';
import 'recase4.0.0/recase.dart';
import 'models.dart';
@@ -18,8 +17,8 @@ Builder genFromJson(BuilderOptions options) {
class GeneratorOptions {
GeneratorOptions({
@required this.fontName,
@required this.selectionJsonPath,
required this.fontName,
required this.selectionJsonPath,
});
factory GeneratorOptions.fromOptions(BuilderOptions options) {
@@ -32,12 +31,12 @@ class GeneratorOptions {
return GeneratorOptions(
fontName: config['font_name'] as String,
selectionJsonPath: config['selection_json_path'] as String,
selectionJsonPath: config['selection_json_path'],
);
}
final String fontName;
final String selectionJsonPath;
final String? selectionJsonPath;
}
class IcomoonBuilder implements Builder {
@@ -80,7 +79,7 @@ class IcomoonBuilder implements Builder {
..fields.addAll(fields));
final content = Library(
(b) => b.body..add(icomoonClass),
).accept(DartEmitter(Allocator()));
).accept(DartEmitter(allocator: Allocator()));
await buildStep.writeAsString(
buildStep.inputId.changeExtension('.dart'),

View File

@@ -1,8 +1,7 @@
import 'package:meta/meta.dart';
class SelectionModel {
SelectionModel({
@required this.icons,
required this.icons,
});
factory SelectionModel.fromJson(Map<String, dynamic> json) {
@@ -18,7 +17,7 @@ class SelectionModel {
}
class IcomoonIcon {
IcomoonIcon({@required this.properties});
IcomoonIcon({required this.properties});
factory IcomoonIcon.fromJson(Map<String, dynamic> json) {
final properties = json['properties'] as Map<String, dynamic>;
@@ -30,7 +29,7 @@ class IcomoonIcon {
}
class IconProperties {
IconProperties({@required this.name, @required this.code});
IconProperties({required this.name, required this.code});
factory IconProperties.fromJson(Map<String, dynamic> json) {
return IconProperties(

134
lib/recase4.0.0/recase.dart Normal file
View File

@@ -0,0 +1,134 @@
/// An instance of text to be re-cased.
class ReCase {
final RegExp _upperAlphaRegex = RegExp(r'[A-Z]');
final symbolSet = {' ', '.', '/', '_', '\\', '-'};
late String originalText;
late List<String> _words;
ReCase(String text) {
this.originalText = text;
this._words = _groupIntoWords(text);
}
List<String> _groupIntoWords(String text) {
StringBuffer sb = StringBuffer();
List<String> words = [];
bool isAllCaps = text.toUpperCase() == text;
for (int i = 0; i < text.length; i++) {
String char = text[i];
String? nextChar = i + 1 == text.length ? null : text[i + 1];
if (symbolSet.contains(char)) {
continue;
}
sb.write(char);
bool isEndOfWord = nextChar == null ||
(_upperAlphaRegex.hasMatch(nextChar) && !isAllCaps) ||
symbolSet.contains(nextChar);
if (isEndOfWord) {
words.add(sb.toString());
sb.clear();
}
}
return words;
}
/// camelCase
String get camelCase => _getCamelCase();
/// CONSTANT_CASE
String get constantCase => _getConstantCase();
/// Sentence case
String get sentenceCase => _getSentenceCase();
/// snake_case
String get snakeCase => _getSnakeCase();
/// dot.case
String get dotCase => _getSnakeCase(separator: '.');
/// param-case
String get paramCase => _getSnakeCase(separator: '-');
/// path/case
String get pathCase => _getSnakeCase(separator: '/');
/// PascalCase
String get pascalCase => _getPascalCase();
/// Header-Case
String get headerCase => _getPascalCase(separator: '-');
/// Title Case
String get titleCase => _getPascalCase(separator: ' ');
String _getCamelCase({String separator: ''}) {
List<String> words = this._words.map(_upperCaseFirstLetter).toList();
if (_words.isNotEmpty) {
words[0] = words[0].toLowerCase();
}
return words.join(separator);
}
String _getConstantCase({String separator: '_'}) {
List<String> words = this._words.map((word) => word.toUpperCase()).toList();
return words.join(separator);
}
String _getPascalCase({String separator: ''}) {
List<String> words = this._words.map(_upperCaseFirstLetter).toList();
return words.join(separator);
}
String _getSentenceCase({String separator: ' '}) {
List<String> words = this._words.map((word) => word.toLowerCase()).toList();
if (_words.isNotEmpty) {
words[0] = _upperCaseFirstLetter(words[0]);
}
return words.join(separator);
}
String _getSnakeCase({String separator: '_'}) {
List<String> words = this._words.map((word) => word.toLowerCase()).toList();
return words.join(separator);
}
String _upperCaseFirstLetter(String word) {
return '${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}';
}
}
extension StringReCase on String {
String get camelCase => ReCase(this).camelCase;
String get constantCase => ReCase(this).constantCase;
String get sentenceCase => ReCase(this).sentenceCase;
String get snakeCase => ReCase(this).snakeCase;
String get dotCase => ReCase(this).dotCase;
String get paramCase => ReCase(this).paramCase;
String get pathCase => ReCase(this).pathCase;
String get pascalCase => ReCase(this).pascalCase;
String get headerCase => ReCase(this).headerCase;
String get titleCase => ReCase(this).titleCase;
}

View File

@@ -1,21 +1,20 @@
name: icomoon_selection_gen
description: A code generator for flutter that reads icomoon selection.json and generate the icon classes
version: 0.0.2
version: 0.0.3
author: Gabriel Rohden
homepage:
environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.17.1 <3.0.0"
dependencies:
build: ^1.1.4
build_config: ^0.4.0
build: ^2.3.0
build_config: ^1.0.0
dev_dependencies:
meta: ^1.1.7
recase: ^3.0.0
dart_style: ^1.0.0
code_builder: ^3.3.0
build_runner: ^1.5.0
build_test: ^0.10.7+3
# recase: ^4.0.0
dart_style: ^2.2.3
code_builder: ^4.1.0
build_runner: ^2.1.11
build_test: ^2.1.5