Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
189 changes: 188 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,192 @@
include: package:lints/recommended.yaml
Copy link
Owner

Choose a reason for hiding this comment

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

I don't wanna remove this. Can't dart_code_metrics work with this? Please remove metrics if it can't. I don't wanna make this change if it can't. I'd prefer standard recommended rules always.


analyzer:
plugins:
- dart_code_metrics
exclude:
- "build/**"
strong-mode:
implicit-casts: false
errors:
always_use_package_imports: error
argument_type_not_assignable: ignore
avoid_empty_else: error
avoid_relative_lib_imports: error
avoid_returning_null_for_future: error
avoid_slow_async_io: error
avoid_type_to_string: error
avoid_types_as_parameter_names: error
close_sinks: error
empty_statements: error
hash_and_equals: error
invalid_assignment: ignore
invariant_booleans: error
iterable_contains_unrelated_type: error
list_remove_unrelated_type: error
literal_only_boolean_expressions: error
no_adjacent_strings_in_list: error
no_duplicate_case_values: error
no_logic_in_create_state: error
non_bool_condition: ignore
prefer_void_to_null: error
return_of_invalid_type: ignore
test_types_in_equals: error
throw_in_finally: error
unnecessary_statements: error
unrelated_type_equality_checks: error
valid_regexps: error
todo: ignore


dart_code_metrics:
metrics-exclude:
- test/**
rules:
- newline-before-return
- no-boolean-literal-compare
- no-empty-block
- prefer-trailing-comma
- prefer-conditional-expressions
- no-equal-then-else
- always-remove-listener
- avoid-unused-parameters
- avoid-unnecessary-setstate
- avoid-wrapping-in-padding
- double-literal-format
- prefer-const-border-radius
- prefer_extracting_callbacks
- member-ordering-extended
- prefer-extracting-callbacks
- no-object-declaration
- binary-expression-operand-order
- member-ordering-extended:
order:
- public-static-const-fields
- private-static-const-fields
- public-static-final-fields
- private-static-final-fields
- public-static-fields
- private-static-fields
- public-final-fields
- private-final-fields
- public-fields
- public-getters-setters
- private-fields
- private-getters-setters
- constructors
- named-constructors
- factory-constructors
- overridden-methods
- public-static-methods
- public-methods
- protected-methods
- private-static-methods
- private-methods

linter:
rules:
constant_identifier_names: false
# Const rules.
Copy link
Owner

Choose a reason for hiding this comment

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

Some of these rules are already true by default in recommended lints package. We can avoid stating them here.

Copy link
Owner

Choose a reason for hiding this comment

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

@Sanlovty Did you remove rules which are set by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, every one

Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, it seems that a lot of rules explicitly mentioned here are already included by lints package. Here's a list of all the rules included by lints package so that you can compare:

linter:
  rules:
    - avoid_empty_else
    - avoid_relative_lib_imports
    - avoid_shadowing_type_parameters
    - avoid_types_as_parameter_names
    - await_only_futures
    - camel_case_extensions
    - camel_case_types
    - curly_braces_in_flow_control_structures
    - depend_on_referenced_packages
    - empty_catches
    - file_names
    - hash_and_equals
    - iterable_contains_unrelated_type
    - list_remove_unrelated_type
    - no_duplicate_case_values
    - non_constant_identifier_names
    - null_check_on_nullable_type_parameter
    - package_prefixed_library_names
    - prefer_generic_function_type_aliases
    - prefer_is_empty
    - prefer_is_not_empty
    - prefer_iterable_whereType
    - prefer_typing_uninitialized_variables
    - provide_deprecation_message
    - unnecessary_overrides
    - unrelated_type_equality_checks
    - valid_regexps
    - void_checks
    - always_require_non_null_named_parameters
    - annotate_overrides
    - avoid_function_literals_in_foreach_calls
    - avoid_init_to_null
    - avoid_null_checks_in_equality_operators
    - avoid_renaming_method_parameters
    - avoid_return_types_on_setters
    - avoid_returning_null_for_void
    - avoid_single_cascade_in_expression_statements
    - constant_identifier_names
    - control_flow_in_finally
    - empty_constructor_bodies
    - empty_statements
    - exhaustive_cases
    - implementation_imports
    - library_names
    - library_prefixes
    - library_private_types_in_public_api
    - no_leading_underscores_for_library_prefixes
    - no_leading_underscores_for_local_identifiers
    - null_closures
    - overridden_fields
    - package_names
    - prefer_adjacent_string_concatenation
    - prefer_collection_literals
    - prefer_conditional_assignment
    - prefer_contains
    - prefer_equal_for_default_values
    - prefer_final_fields
    - prefer_for_elements_to_map_fromIterable
    - prefer_function_declarations_over_variables
    - prefer_if_null_operators
    - prefer_initializing_formals
    - prefer_inlined_adds
    - prefer_interpolation_to_compose_strings
    - prefer_is_not_operator
    - prefer_null_aware_operators
    - prefer_spread_collections
    - prefer_void_to_null
    - recursive_getters
    - slash_for_doc_comments
    - type_init_formals
    - unnecessary_brace_in_string_interps
    - unnecessary_const
    - unnecessary_constructor_name
    - unnecessary_getters_setters
    - unnecessary_late
    - unnecessary_new
    - unnecessary_null_aware_assignments
    - unnecessary_null_in_if_null_operators
    - unnecessary_nullable_for_final_variable_declarations
    - unnecessary_string_escapes
    - unnecessary_string_interpolations
    - unnecessary_this
    - use_function_type_syntax_for_parameters
    - use_rethrow_when_possible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll check this later

Copy link
Owner

Choose a reason for hiding this comment

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

Thank you!

prefer_const_constructors: true

# Error rules.
always_use_package_imports: true
avoid_empty_else: true
avoid_relative_lib_imports: true
avoid_returning_null_for_future: true
avoid_slow_async_io: true
avoid_type_to_string: true
avoid_types_as_parameter_names: true
cancel_subscriptions: true
close_sinks: true
comment_references: true
eol_at_end_of_file: true
hash_and_equals: true
invariant_booleans: true
iterable_contains_unrelated_type: true
list_remove_unrelated_type: true
literal_only_boolean_expressions: true
no_adjacent_strings_in_list: true
no_duplicate_case_values: true
test_types_in_equals: true
throw_in_finally: true
unnecessary_statements: true
unrelated_type_equality_checks: true
unsafe_html: true
valid_regexps: true

# Style rules.
always_declare_return_types: true
always_put_required_named_parameters_first: true
avoid_equals_and_hash_code_on_mutable_classes: true
avoid_escaping_inner_quotes: true
avoid_field_initializers_in_const_classes: true
avoid_implementing_value_types: true
avoid_js_rounded_ints: true
avoid_multiple_declarations_per_line: true
avoid_private_typedef_functions: true
avoid_redundant_argument_values: true
avoid_returning_null: true
avoid_returning_this: true
avoid_setters_without_getters: true
avoid_shadowing_type_parameters: true
avoid_types_on_closure_parameters: true
avoid_unnecessary_containers: true
avoid_unused_constructor_parameters: true
avoid_void_async: true
await_only_futures: true
camel_case_extensions: true
camel_case_types: true
cascade_invocations: true
curly_braces_in_flow_control_structures: true
deprecated_consistency: true
directives_ordering: true
do_not_use_environment: true
empty_catches: true
file_names: true
leading_newlines_in_multiline_strings: true
missing_whitespace_between_adjacent_strings: true
no_runtimeType_toString: true
noop_primitive_operations: true
non_constant_identifier_names: true
one_member_abstracts: true
only_throw_errors: true
package_api_docs: true
prefer_asserts_in_initializer_lists: true
prefer_constructors_over_static_methods: true
prefer_equal_for_default_values: true
prefer_final_in_for_each: true
prefer_final_locals: true
prefer_generic_function_type_aliases: true
prefer_if_elements_to_conditional_expressions: true
prefer_is_empty: true
prefer_is_not_empty: true
prefer_iterable_whereType: true
prefer_mixin: true
prefer_single_quotes: true
prefer_typing_uninitialized_variables: true
provide_deprecation_message: true
sized_box_for_whitespace: true
type_annotate_public_apis: true
unawaited_futures: true
unnecessary_await_in_return: true
unnecessary_lambdas: true
unnecessary_null_checks: true
unnecessary_overrides: true
unnecessary_parenthesis: true
unnecessary_raw_strings: true
use_if_null_to_convert_nulls_to_bools: true
use_is_even_rather_than_modulo: true
use_late_for_private_fields_and_variables: true
use_named_constants: true
use_raw_strings: true
use_setters_to_change_properties: true
use_string_buffers: true
use_to_and_as_if_applicable: true
void_checks: true
conditional_uri_does_not_exist: true
secure_pubspec_urls: true
sized_box_shrink_expand: true

# Pub rules.
sort_pub_dependencies: true

83 changes: 45 additions & 38 deletions lib/spider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import 'dart:io';

import 'package:path/path.dart' as p;
import 'package:spider/src/cli/command_processor.dart';
import 'package:spider/src/constants.dart';
import 'package:spider/src/dart_class_generator.dart';
import 'package:spider/src/data/json_config.dart';
import 'package:spider/src/data/yaml_config.dart';
import 'package:spider/src/formatter.dart';
import 'package:spider/src/spider_config.dart';
import 'package:spider/src/utils.dart';
import 'package:sprintf/sprintf.dart' show sprintf;

import 'src/constants.dart';
import 'src/dart_class_generator.dart';
import 'src/utils.dart';

/// Entry point of all the command process
/// provides various functions to execute commands
/// Responsible for triggering dart code generation
Expand All @@ -39,21 +38,6 @@ class Spider {

Spider(this.config);

/// Triggers build
void build([List<String> options = const []]) {
if (config.groups.isEmpty) {
exitWith('No groups found in config file.');
}
for (final group in config.groups) {
final generator = DartClassGenerator(group, config.globals);
generator.initAndStart(
options.hasArg('watch', 'w'), options.contains('--smart-watch'));
}
if (config.globals.export) {
exportAsLibrary();
}
}

/// initializes config file (spider.yaml) in the root of the project
static void createConfigs({
bool isJson = false,
Expand All @@ -63,10 +47,12 @@ class Spider {
try {
if (addInPubspec) {
createConfigsInPubspec();

return;
}
if (path != null && path.isNotEmpty) {
createConfigFileAtCustomPath(path, isJson);

return;
}
final filename = isJson ? 'spider.json' : 'spider.yaml';
Expand All @@ -77,36 +63,23 @@ class Spider {
}
dest.writeAsStringSync(content);
success('Configuration file created successfully.');
} on Error catch (e) {
exitWith('Unable to create config file', e.stackTrace);
} catch (error, stacktrace) {
exitWith('Unable to create config file', stacktrace);
}
}

/// Generates library export file for all the generated references files.
void exportAsLibrary() {
final content = getExportContent(
noComments: config.globals.noComments,
usePartOf: config.globals.usePartOf ?? false,
fileNames: config.groups
.map<String>((group) => Formatter.formatFileName(group.fileName))
.toList(),
);
writeToFile(
name: Formatter.formatFileName(config.globals.exportFileName),
path: config.globals.package,
content: DartClassGenerator.formatter.format(content));
}

/// Appends spider config into project's pubspec.yaml file.
static void createConfigsInPubspec() {
final pubspecFile = file('pubspec.yaml') ?? file('pubspec.yml');
if (pubspecFile == null) {
exitWith(ConsoleMessages.pubspecNotFound);

return;
}
final pubspecContent = pubspecFile.readAsStringSync();
if (pubspecContent.contains('spider')) {
exitWith(ConsoleMessages.configExistsInPubspec);

return;
}
try {
Expand All @@ -119,8 +92,8 @@ class Spider {
..write(configContent)
..close();
success(ConsoleMessages.configCreatedInPubspec);
} on Error catch (e) {
exitWith(ConsoleMessages.unableToAddConfigInPubspec, e.stackTrace);
} catch (error, stackTrace) {
exitWith(ConsoleMessages.unableToAddConfigInPubspec, stackTrace);
}
}

Expand All @@ -136,6 +109,7 @@ class Spider {
final String extension = p.extension(path);
if (extension.isNotEmpty) {
exitWith('Provided path is not a valid directory.');

return;
}
Directory(path).createSync(recursive: true);
Expand All @@ -146,9 +120,42 @@ class Spider {
final file = File(filePath);
if (file.existsSync()) {
exitWith('Config file already exists at $filePath.');

return;
}
file.writeAsStringSync(content);
success(sprintf(ConsoleMessages.fileCreatedAtCustomPath, [filePath]));
}

/// Triggers build
void build([List<String> options = const []]) {
if (config.groups.isEmpty) {
exitWith('No groups found in config file.');
}
for (final group in config.groups) {
DartClassGenerator(group, config.globals).initAndStart(
options.hasArg('watch', 'w'),
options.contains('--smart-watch'),
);
}
if (config.globals.export) {
exportAsLibrary();
}
}

/// Generates library export file for all the generated references files.
void exportAsLibrary() {
final content = getExportContent(
noComments: config.globals.noComments,
usePartOf: config.globals.usePartOf ?? false,
fileNames: config.groups
.map<String>((group) => Formatter.formatFileName(group.fileName))
.toList(),
);
writeToFile(
name: Formatter.formatFileName(config.globals.exportFileName),
path: config.globals.package,
content: DartClassGenerator.formatter.format(content),
);
}
}
6 changes: 3 additions & 3 deletions lib/src/asset_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

// Author: Birju Vachhani
// Created Date: February 09, 2020
import 'package:spider/src/formatter.dart';

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

import 'formatter.dart';

/// Holds group information for assets sub directories
class AssetGroup {
late final String className;
Expand Down Expand Up @@ -53,7 +52,8 @@ class AssetGroup {
useConst = true;
types = <String>[];
json['types']?.forEach(
(group) => types.add(formatExtension(group.toString()).toLowerCase()));
(group) => types.add(formatExtension(group.toString()).toLowerCase()),
);
paths = <String>[];
if (json['paths'] != null) {
paths.addAll(List<String>.from(json['paths']));
Expand Down
Loading