Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/spider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class Spider {
}
final filename = isJson ? 'spider.json' : 'spider.yaml';
final dest = File(p.join(Directory.current.path, filename));
final content = isJson ? testJsonConfigTemplate : testYamlConfigTemplate;
//isJson ? testJsonConfigTemplate : testYamlConfigTemplate;
final content = getTestConfig(isJson: isJson);
if (dest.existsSync()) {
info('Config file already exists. Overwriting configs...');
}
Expand All @@ -85,12 +86,16 @@ class Spider {

/// Generates library export file for all the generated references files.
void exportAsLibrary() {
final files = <String, String>{};
for (final group in config.groups) {
files[Formatter.formatFileName(group.fileName)] =
toPackageKey(config.globals.package, group.package);
}
final content = getExportContent(
project: config.globals.projectName,
noComments: config.globals.noComments,
usePartOf: config.globals.usePartOf ?? false,
fileNames: config.groups
.map<String>((group) => Formatter.formatFileName(group.fileName))
.toList(),
files: files,
);
writeToFile(
name: Formatter.formatFileName(config.globals.exportFileName),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/asset_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AssetGroup {
late final List<String>? types;
late final List<String>? paths;
late final String? prefix;
late final String package;

AssetGroup({
required this.className,
Expand All @@ -44,6 +45,7 @@ class AssetGroup {

AssetGroup.fromJson(Map<String, dynamic> json) {
className = json['class_name'].toString();
package = json['package']?.toString() ?? '';
fileName =
Formatter.formatFileName(json['file_name']?.toString() ?? className);
paths = json['path'] == null && json['paths'] == null ? null : <String>[];
Expand All @@ -52,8 +54,6 @@ class AssetGroup {
} else if (json['path'] != null) {
paths!.add(json['path'].toString());
}
// If paths are implemented in group scope we don't need
// sub_groups at all.
if (paths != null) {
subgroups = null;
prefix = json['prefix']?.toString() ?? '';
Expand Down
4 changes: 2 additions & 2 deletions lib/src/asset_subgroup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class AssetSubgroup {

AssetSubgroup({
required this.paths,
this.prefix = '',
this.types = const <String>[],
required this.types,
required this.prefix,
});

AssetSubgroup.fromJson(Map<String, dynamic> json) {
Expand Down
21 changes: 13 additions & 8 deletions lib/src/dart_class_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class DartClassGenerator {
for (final path in group.paths!) {
properties.add(
SubgroupProperty(
group.prefix!,
createFileMap(dir: path, types: group.types!),
prefix: group.prefix!,
files: createFileMap(dir: path, types: group.types!),
),
);
}
Expand All @@ -95,8 +95,9 @@ class DartClassGenerator {
for (final path in subgroup.paths) {
properties.add(
SubgroupProperty(
subgroup.prefix,
createFileMap(dir: path, types: group.types ?? subgroup.types),
prefix: subgroup.prefix,
files: createFileMap(
dir: path, types: group.types ?? subgroup.types),
),
);
}
Expand Down Expand Up @@ -215,7 +216,7 @@ class DartClassGenerator {
}

// Can be transformed into lambda function or simplified
List<String> getAssetNames() {
List<String> _getAssetNames() {
final assetNames = <String>[];
for (final property in properties) {
assetNames.addAll(property.files.keys
Expand All @@ -237,12 +238,14 @@ class DartClassGenerator {
final valuesList = globals.useReferencesList
? getListOfReferences(
properties: staticProperty + constProperty,
assetNames: getAssetNames(),
assetNames: _getAssetNames(),
)
: null;

verbose('Constructing dart class for ${group.className}');
final content = getDartClass(
project: globals.projectName,
package: globals.package ?? Constants.DEFAULT_PACKAGE,
ignoredRules: globals.ignoredRules,
className: group.className,
references: references,
Expand All @@ -254,7 +257,7 @@ class DartClassGenerator {
verbose('Writing class ${group.className} to file ${group.fileName}');
writeToFile(
name: Formatter.formatFileName(group.fileName),
path: globals.package,
path: '${globals.package}/${group.package}',
content: formatter.format(content));
}

Expand Down Expand Up @@ -283,7 +286,9 @@ class DartClassGenerator {
final content = getTestClass(
project: globals.projectName,
fileName: fileName,
package: globals.package!,
package: globals.export && globals.usePartOf!
? toPackageKey(globals.package, '')
: toPackageKey(globals.package, group.package),
noComments: globals.noComments,
tests: tests,
testImport: globals.useFlutterTestImports ? 'flutter_test' : 'test',
Expand Down
9 changes: 6 additions & 3 deletions lib/src/data/export_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ String get libraryTemplate => 'library [LIBRARY_NAME];\n\n';
String get ignoreRulesTemplate => '// ignore_for_file: [IGNORED_RULES]\n\n';

/// A template to generate export statements in dart source code.
String get exportFileTemplate => "export '[FILE_NAME]';";
String get exportFileTemplate =>
"export 'package:[PROJECT_NAME]/[PACKAGE]/[FILE_NAME]';";

/// A template to generate `part` directive statement in the dart
/// library source file.
String get partTemplate => "part '[FILE_NAME]';";
String get partTemplate =>
"part 'package:[PROJECT_NAME]/[PACKAGE]/[FILE_NAME]';";

/// A template to generate `part of` directive statements in the dart
/// asset reference files.
String get partOfTemplate => "part of '[FILE_NAME]';";
String get partOfTemplate =>
"part of 'package:[PROJECT_NAME]/[PACKAGE]/[FILE_NAME]';";
Comment on lines 21 to +35
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an oversight or am I not getting this? I have never seen part directive with package: in the path.

159 changes: 84 additions & 75 deletions lib/src/data/test_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import 'package:spider/src/constants.dart';

/// A template for generating file author line.
String get timeStampTemplate => '/// Generated by spider on [TIME]\n\n';

Expand All @@ -33,87 +35,40 @@ void main() {
String get expectTestTemplate =>
'expect(File([CLASS_NAME].[ASSET_NAME]).existsSync(), true);';

/// A template of yaml config for using in tests.
String get testYamlConfigTemplate => '''
# Generated by Spider

# Generates unit tests to verify that the assets exists in assets directory
generate_tests: true

# Use this to remove vcs noise created by the `generated` comments in dart code
no_comments: true

# Exports all the generated file as the one library
export: true

# This allows you to import all the generated references with 1 single import!
use_part_of: true

# Generates a variable that contains a list of all asset values
use_references_list: false

# Location where all the generated references will be stored
package: resources

# List of linter rules that will be ignored in generated files (except for tests)
ignored_rules: [ public_member_api_docs, member-ordering-extended, test_rule ]

groups:
- class_name: Images
path: assets/images
types: [ .png, .jpg, .jpeg, .webp, .webm, .bmp ]

- class_name: Svgs
sub_groups:
- path: assets/svgsMenu
prefix: menu
types: [ .svg ]

- path: assets/svgsOther
prefix: other
types: [ .svg ]

- class_name: Ico
types: [ .ico ]
prefix: ico
sub_groups:
- path: assets/icons
prefix: test1
types: [ .ttf ]

- path: assets/vectors
prefix: test2
types: [ .pdf ]

- class_name: Video
types: [ .mp4 ]
path: assets/moviesOnly
sub_groups:
- path: assets/movies
prefix: common

- path: assets/moviesExtra
prefix: extra
# - path: assets/vectors
# class_name: Svgs
# package: res
# types: [ .svg ]
''';

/// A template of json config for using in tests.
String get testJsonConfigTemplate => '''
/// Returns config for testing with abilty to test with different parameters.
String getTestConfig({
required bool isJson,
bool generateTests = true,
bool export = true,
bool noComments = true,
bool usePartOf = true,
bool useReferencesList = false,

/// TODO(Sanlovty): make ignoredRules injectable into config (not hardcoded).
List<String>? ignoredRules = const [
"public_member_api_docs",
"member-ordering-extended",
"test_rule"
],
String? package = Constants.DEFAULT_PACKAGE,
String? exportFile,
}) {
if (isJson) {
return '''
{
"generate_tests":true,
"no_comments":true,
"export":true,
"use_part_of":true,
"use_references_list": false,
"package":"resources",
"generate_tests": $generateTests,
"no_comments": $noComments,
"export": $export,
"use_part_of": $usePartOf,
"use_references_list": $useReferencesList,
"package": ${package == null ? null : '"$package"'},
"export_file": ${exportFile == null ? null : '"$exportFile"'},
"ignored_rules": [ "public_member_api_docs", "member-ordering-extended", "test_rule" ],
"groups": [
{
"class_name": "Images",
"path": "assets/images",
"package": "images",
"types": [
".png",
".jpg",
Expand All @@ -125,6 +80,7 @@ String get testJsonConfigTemplate => '''
},
{
"class_name": "Svgs",
"package": "svgs",
"sub_groups": [
{
"path": "assets/svgsMenu",
Expand All @@ -144,6 +100,7 @@ String get testJsonConfigTemplate => '''
},
{
"class_name": "Ico",
"package": "ico",
"types": [
".ico"
],
Expand All @@ -167,6 +124,7 @@ String get testJsonConfigTemplate => '''
},
{
"class_name": "Video",
"package": null,
"types": [
".mp4"
],
Expand All @@ -185,3 +143,54 @@ String get testJsonConfigTemplate => '''
]
}
''';
}
return '''
generate_tests: $generateTests
no_comments: $noComments
export: $export
use_part_of: $usePartOf
use_references_list: $useReferencesList
${exportFile == null ? '' : 'export_file: $exportFile'}
${package == null ? '' : 'package: $package'}
ignored_rules: [ public_member_api_docs, member-ordering-extended, test_rule ],
groups:
- class_name: Images
package: images
path: assets/images
types: [ .png, .jpg, .jpeg, .webp, .webm, .bmp ]

- class_name: Svgs
package: svgs
sub_groups:
- path: assets/svgsMenu
prefix: menu
types: [ .svg ]

- path: assets/svgsOther
prefix: other
types: [ .svg ]

- class_name: Ico
package: ico
types: [ .ico ]
prefix: ico
sub_groups:
- path: assets/icons
prefix: test1
types: [ .ttf ]

- path: assets/vectors
prefix: test2
types: [ .pdf ]

- class_name: Video
types: [ .mp4 ]
path: assets/moviesOnly
sub_groups:
- path: assets/movies
prefix: common

- path: assets/moviesExtra
prefix: extra
''';
}
5 changes: 4 additions & 1 deletion lib/src/subgroup_property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ class SubgroupProperty {
final Map<String, String> files;

/// Creates an instance of [SubgroupProperty].
SubgroupProperty(this.prefix, this.files);
SubgroupProperty({
required this.prefix,
required this.files,
});
}
Loading