Skip to content

Commit

Permalink
Code metrics integration & Fix sort strings package issue (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirland authored Nov 3, 2022
1 parent 3f357c9 commit 13299df
Show file tree
Hide file tree
Showing 37 changed files with 633 additions and 351 deletions.
2 changes: 1 addition & 1 deletion .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"flutterSdkVersion": "3.0.5",
"flutterSdkVersion": "3.3.6",
"flavors": {}
}
30 changes: 30 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,41 @@ linter:
- curly_braces_in_flow_control_structures
- slash_for_doc_comments

dart_code_metrics:
anti-patterns:
- long-method
- long-parameter-list
metrics:
cyclomatic-complexity: 20
maximum-nesting-level: 5
number-of-parameters: 4
source-lines-of-code: 50
metrics-exclude:
- test/**
rules:
- avoid-nested-conditional-expressions
- avoid-passing-async-when-sync-expected
- avoid-redundant-async
- no-boolean-literal-compare
- no-empty-block
- no-equal-then-else
- no-object-declaration
- prefer-conditional-expressions
- prefer-first
- prefer-immediate-return
- prefer-last
- prefer-moving-to-variable:
allowed-duplicated-chains: 3
- prefer-trailing-comma



# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
analyzer:
exclude:
- '**/*.g.dart'
- '**/*.gen.dart'
- '**/*.freezed.dart'
errors:
invalid_annotation_target: ignore
8 changes: 8 additions & 0 deletions android/fastlane/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ For _fastlane_ installation instructions, see [Installing _fastlane_](https://do

**Lint: Analyze code**

### lint_code_metrics

```sh
[bundle exec] fastlane lint_code_metrics
```

**Lint: Code metrics**

### lints

```sh
Expand Down
25 changes: 25 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,40 @@ lane :lint_format do
flutter_command(command: "format --set-exit-if-changed .")
end

desc "**Lint: Check code format**"
lane :lint_check_language_sorting do
current_content = sh_on_root(command: "cat lib/l10n/intl_en.arb")
flutter_command(command: "pub run arb_utils sort lib/l10n/intl_en.arb")
new_content = sh_on_root(command: "cat lib/l10n/intl_en.arb")
unless current_content == new_content
UI.user_error!("Language file is not sorted")
end
end

desc "**Lint: Analyze code**"
lane :lint_analyze do
flutter_command(command: "analyze .")
end

desc "**Lint: Code metrics**"
lane :lint_code_metrics do
result = flutter_command(command: "pub run dart_code_metrics:metrics analyze lib --fatal-style --fatal-performance --fatal-warnings")
UI.message(result)
unless result.include? "✔ no issues found!"
UI.user_error!("Code metrics error happened")
end

flutter_command(command: "pub run dart_code_metrics:metrics check-unused-code lib --fatal-unused")
flutter_command(command: "pub run dart_code_metrics:metrics check-unused-files lib --fatal-unused")
end


desc "**Run linters**"
lane :lints do
lint_format
lint_check_language_sorting
lint_analyze
lint_code_metrics
end

desc "**Check generated code is fine**"
Expand Down
16 changes: 16 additions & 0 deletions fastlane/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ For _fastlane_ installation instructions, see [Installing _fastlane_](https://do

**Lint: Check code format**

### lint_check_language_sorting

```sh
[bundle exec] fastlane lint_check_language_sorting
```

**Lint: Check code format**

### lint_analyze

```sh
Expand All @@ -45,6 +53,14 @@ For _fastlane_ installation instructions, see [Installing _fastlane_](https://do

**Lint: Analyze code**

### lint_code_metrics

```sh
[bundle exec] fastlane lint_code_metrics
```

**Lint: Code metrics**

### lints

```sh
Expand Down
8 changes: 8 additions & 0 deletions ios/fastlane/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ For _fastlane_ installation instructions, see [Installing _fastlane_](https://do

**Lint: Analyze code**

### lint_code_metrics

```sh
[bundle exec] fastlane lint_code_metrics
```

**Lint: Code metrics**

### lints

```sh
Expand Down
4 changes: 3 additions & 1 deletion lib/core/common/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ abstract class Config {
static String gitHubTipsNameFolder = 'tipsandtricks/';

static final _environment = enumFromString(
Environments.values, const String.fromEnvironment('ENV')) ??
Environments.values,
const String.fromEnvironment('ENV'),
) ??
Environments.development;

static Future<void> initialize() async {
Expand Down
3 changes: 3 additions & 0 deletions lib/core/common/crash_report_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ abstract class CrashReportTool {

class NoOpsCrashReportTool extends CrashReportTool {
@override
// ignore: no-empty-block, avoid-redundant-async
Future init() async {}

@override
// ignore: no-empty-block, avoid-redundant-async
Future logFatal(error, StackTrace? stackTrace) async {}

@override
// ignore: no-empty-block, avoid-redundant-async
Future logNonFatal(LogEvent event) async {}
}
31 changes: 18 additions & 13 deletions lib/core/common/helper/env_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ import 'dart:async';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_template/core/common/logger.dart';

Future<Map<String, String>> loadEnvs(String path,
[bool ignoreErrors = true]) async {
Future<Map<String, String>> loadEnvs(
String path, [
bool ignoreErrors = true,
]) async {
final dotEnv = DotEnv();
final completer = Completer<Map<String, String>>();
// ignore: unawaited_futures
runZonedGuarded(() async {
await dotEnv.load(fileName: path);
completer.complete(dotEnv.env);
}, (e, s) {
Logger.d('$path file is not valid.', e);
if (ignoreErrors) {
completer.complete({});
} else {
completer.completeError(e, s);
}
});
runZonedGuarded(
() async {
await dotEnv.load(fileName: path);
completer.complete(dotEnv.env);
},
(e, s) {
Logger.d('$path file is not valid.', e);
if (ignoreErrors) {
completer.complete({});
} else {
completer.completeError(e, s);
}
},
);
return completer.future;
}
1 change: 0 additions & 1 deletion lib/core/common/helper/future_helpers.dart

This file was deleted.

14 changes: 11 additions & 3 deletions lib/core/common/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ class _CrashReportWrappedPrinter extends LogPrinter {
var trace = Trace.current(4);
var frames = trace.frames;
final newFrames = frames.map(
(frame) => Frame(frame.uri, frame.line, frame.column, frame.member));
(frame) => Frame(
frame.uri,
frame.line,
frame.column,
frame.member,
),
);
return _PrintableTrace(newFrames);
}

Expand Down Expand Up @@ -103,8 +109,10 @@ class _PrintableTrace extends Trace {
return frames.map((frame) {
var number = '#${i++}'.padRight(8);
var member = frame.member!
.replaceAllMapped(RegExp(r'[^.]+\.<async>'),
(match) => '${match[1]}.<${match[1]}_async_body>')
.replaceAllMapped(
RegExp(r'[^.]+\.<async>'),
(match) => '${match[1]}.<${match[1]}_async_body>',
)
.replaceAll('<fn>', '<anonymous closure>');
var line = frame.line ?? 0;
var column = frame.column ?? 0;
Expand Down
82 changes: 48 additions & 34 deletions lib/core/common/network_exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,45 +94,59 @@ class NetworkException extends Object with _$NetworkException {
return const NetworkException.unexpectedError();
}
} else {
if (error.toString().contains('is not a subtype of')) {
return const NetworkException.unableToProcess();
} else {
return const NetworkException.unexpectedError();
}
return error.toString().contains('is not a subtype of')
? const NetworkException.unableToProcess()
: const NetworkException.unexpectedError();
}
}

static String getErrorMessage(NetworkException networkExceptions) {
var errorMessage = '';
networkExceptions.when(notImplemented: () {
errorMessage = 'Not Implemented';
}, internalServerError: () {
errorMessage = 'Internal Server Error';
}, notFound: (String reason) {
errorMessage = reason;
}, serviceUnavailable: () {
errorMessage = 'Service unavailable';
}, methodNotAllowed: () {
errorMessage = 'Method Allowed';
}, badRequest: () {
errorMessage = 'Bad request';
}, unauthorizedRequest: (body) {
errorMessage = 'Unauthorized request - $body';
}, unexpectedError: () {
errorMessage = 'Unexpected error occurred';
}, noInternetConnection: () {
errorMessage = 'No internet connection';
}, conflict: () {
errorMessage = 'Error due to a conflict';
}, unableToProcess: () {
errorMessage = 'Unable to process the data';
}, defaultError: (int? code, String? error) {
errorMessage = error ?? 'Unexpected error occurred';
}, formatException: () {
errorMessage = 'Unexpected error occurred';
}, notAcceptable: () {
errorMessage = 'Not acceptable';
});
networkExceptions.when(
notImplemented: () {
errorMessage = 'Not Implemented';
},
internalServerError: () {
errorMessage = 'Internal Server Error';
},
notFound: (String reason) {
errorMessage = reason;
},
serviceUnavailable: () {
errorMessage = 'Service unavailable';
},
methodNotAllowed: () {
errorMessage = 'Method Allowed';
},
badRequest: () {
errorMessage = 'Bad request';
},
unauthorizedRequest: (body) {
errorMessage = 'Unauthorized request - $body';
},
unexpectedError: () {
errorMessage = 'Unexpected error occurred';
},
noInternetConnection: () {
errorMessage = 'No internet connection';
},
conflict: () {
errorMessage = 'Error due to a conflict';
},
unableToProcess: () {
errorMessage = 'Unable to process the data';
},
defaultError: (int? code, String? error) {
errorMessage = error ?? 'Unexpected error occurred';
},
formatException: () {
errorMessage = 'Unexpected error occurred';
},
notAcceptable: () {
errorMessage = 'Not acceptable';
},
);

return errorMessage;
}
}
Loading

0 comments on commit 13299df

Please sign in to comment.