-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add class generator code with more robust properties name
♻️ refactor code Signed-off-by: birjuvachhani <[email protected]>
- Loading branch information
1 parent
930fe74
commit ada871e
Showing
7 changed files
with
157 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ doc/api/ | |
|
||
.idea/ | ||
|
||
assets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ class Constants { | |
path: assets | ||
class_name: Assets | ||
package: res'''; | ||
static final String LIB_FOLDER = 'lib'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright © $YEAR Birju Vachhani | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Author: Birju Vachhani | ||
// Created Date: February 03, 2020 | ||
|
||
import 'package:meta/meta.dart'; | ||
|
||
class DartClassGenerator { | ||
final String className; | ||
bool useStatic = false; | ||
bool useConst = false; | ||
bool use_underscores = false; | ||
String prefix = ''; | ||
final Map<String, Object> properties; | ||
|
||
static final String _CAPITALIZE_REGEX = r'(_)(\S)'; | ||
static final String _SPECIAL_SYMBOLS = | ||
"[,.\\/;'\\[\\]\\-=<>?:\\\"\\{}_+!@#\$%^&*()\\\\|\\s]+"; | ||
static final Pattern _SPECIAL_SYMBOL_REGEX = RegExp(_SPECIAL_SYMBOLS); | ||
|
||
DartClassGenerator( | ||
{@required this.className, | ||
@required this.properties, | ||
this.use_underscores, | ||
this.useConst, | ||
this.useStatic, | ||
this.prefix}); | ||
|
||
String generate() { | ||
var properties_strings = properties.keys.map<String>((name) { | ||
var str = useStatic ? '\tstatic ' : '\t'; | ||
str += useConst ? 'const ' : ''; | ||
str += | ||
'String ${formatName(name)} = \'${formatPath(properties[name])}\';'; | ||
return str; | ||
}).toList(); | ||
var dart_class = '''// Generated by spider on ${DateTime.now()} | ||
class ${className} { | ||
${properties_strings.join('\n')} | ||
}'''; | ||
return dart_class; | ||
} | ||
|
||
// Formats variable name to be pascal case or with underscores | ||
// if [use_underscores] is true | ||
String formatName(String name) { | ||
// appending prefix if provided any | ||
name = (prefix.isEmpty ? '' : prefix + '_') + name; | ||
name = name | ||
// adds preceding _ for capital letters and lowers them | ||
.replaceAllMapped( | ||
RegExp(r'[A-Z]+'), (match) => '_' + match.group(0).toLowerCase()) | ||
// replaces all the special characters with _ | ||
.replaceAll(_SPECIAL_SYMBOL_REGEX, '_') | ||
// removes _ in the beginning of the name | ||
.replaceFirst(RegExp(r'^_+'), '') | ||
// removes any numbers in the beginning of the name | ||
.replaceFirst(RegExp(r'^[0-9]+'), '') | ||
// lowers the first character of the string | ||
.replaceFirstMapped( | ||
RegExp(r'^[A-Za-z]'), (match) => match.group(0).toLowerCase()); | ||
return use_underscores | ||
? name | ||
: name | ||
// removes _ and capitalize the next character of the _ | ||
.replaceAllMapped(RegExp(_CAPITALIZE_REGEX), | ||
(match) => match.group(2).toUpperCase()); | ||
} | ||
|
||
String formatPath(String value) => value.replaceAll('\\', '/'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# Configuration file for spider | ||
path: assets | ||
# Sample Configuration file for spider | ||
|
||
# where to locate images | ||
path: assets/images | ||
# Name of the generated class | ||
class_name: Assets | ||
# where to generate in the lib folder? | ||
package: res |