Skip to content

Commit 92ddbcc

Browse files
committed
add flutter standarts
0 parents  commit 92ddbcc

File tree

6 files changed

+248
-0
lines changed

6 files changed

+248
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code standards
2+
3+
1. [Flutter application](flutter/README.md)

flutter/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Code standarts for Flutter application
2+
3+
On start new project follow the next steps:
4+
5+
1. Copy [analysis_options.yaml](analysis_options.yaml) to your project to root folder.
6+
2. Copy [scripts](scripts) folder to your project to root folder
7+
3. Follow these instructions [pre-commit.md](scripts/pre-commit.md) to install ```pre-commit``` hook

flutter/analysis_options.yaml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
analyzer:
2+
strong-mode:
3+
implicit-casts: false
4+
implicit-dynamic: false
5+
errors:
6+
missing_required_param: warning
7+
missing_return: warning
8+
todo: ignore
9+
invalid_annotation_target: ignore
10+
deprecated_member_use_from_same_package: ignore
11+
sdk_version_async_exported_from_core: ignore
12+
exclude:
13+
- "bin/cache/**"
14+
- "lib/i18n/messages_*.dart"
15+
- "lib/src/http/**"
16+
- "**/*.g.dart"
17+
18+
linter:
19+
rules:
20+
- always_declare_return_types
21+
- always_put_control_body_on_new_line
22+
- always_require_non_null_named_parameters
23+
- always_specify_types
24+
- annotate_overrides
25+
- avoid_bool_literals_in_conditional_expressions
26+
- avoid_classes_with_only_static_members
27+
- avoid_empty_else
28+
- avoid_equals_and_hash_code_on_mutable_classes
29+
- avoid_field_initializers_in_const_classes
30+
- avoid_init_to_null
31+
- avoid_null_checks_in_equality_operators
32+
- avoid_relative_lib_imports
33+
- avoid_renaming_method_parameters
34+
- avoid_return_types_on_setters
35+
- avoid_returning_null_for_void
36+
- avoid_single_cascade_in_expression_statements
37+
- avoid_slow_async_io
38+
- avoid_types_as_parameter_names
39+
- avoid_unused_constructor_parameters
40+
- avoid_void_async
41+
- await_only_futures
42+
- camel_case_extensions
43+
- camel_case_types
44+
- cancel_subscriptions
45+
- control_flow_in_finally
46+
- empty_catches
47+
- empty_constructor_bodies
48+
- empty_statements
49+
- flutter_style_todos
50+
- hash_and_equals
51+
- implementation_imports
52+
- iterable_contains_unrelated_type
53+
- library_names
54+
- library_prefixes
55+
- list_remove_unrelated_type
56+
- no_adjacent_strings_in_list
57+
- no_duplicate_case_values
58+
- non_constant_identifier_names
59+
- overridden_fields
60+
- package_api_docs
61+
- package_names
62+
- package_prefixed_library_names
63+
- prefer_adjacent_string_concatenation
64+
- prefer_asserts_in_initializer_lists
65+
- prefer_collection_literals
66+
- prefer_conditional_assignment
67+
- prefer_const_constructors
68+
- prefer_const_constructors_in_immutables
69+
- prefer_const_declarations
70+
- prefer_const_literals_to_create_immutables
71+
- prefer_contains
72+
- prefer_equal_for_default_values
73+
- prefer_final_fields
74+
- prefer_final_in_for_each
75+
- prefer_final_locals
76+
- prefer_for_elements_to_map_fromIterable
77+
- prefer_foreach
78+
- prefer_generic_function_type_aliases
79+
- prefer_if_null_operators
80+
- prefer_initializing_formals
81+
- prefer_inlined_adds
82+
- prefer_is_empty
83+
- prefer_is_not_empty
84+
- prefer_is_not_operator
85+
- prefer_iterable_whereType
86+
- prefer_single_quotes
87+
- prefer_spread_collections
88+
- prefer_typing_uninitialized_variables
89+
- prefer_void_to_null
90+
- recursive_getters
91+
- slash_for_doc_comments
92+
- sort_constructors_first
93+
- sort_unnamed_constructors_first
94+
- test_types_in_equals
95+
- throw_in_finally
96+
- type_init_formals
97+
- unnecessary_brace_in_string_interps
98+
- unnecessary_const
99+
- unnecessary_getters_setters
100+
- unnecessary_new
101+
- unnecessary_null_aware_assignments
102+
- unnecessary_null_in_if_null_operators
103+
- unnecessary_overrides
104+
- unnecessary_parenthesis
105+
- unnecessary_statements
106+
- unnecessary_string_interpolations
107+
- unnecessary_this
108+
- unrelated_type_equality_checks
109+
- use_super_parameters
110+
- use_full_hex_values_for_flutter_colors
111+
- use_key_in_widget_constructor
112+
- use_rethrow_when_possible
113+
- valid_regexps
114+
- void_checks

flutter/scripts/install-hooks.bash

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# run chmod +x scripts/*.bash && ./scripts/install-hooks.bash
2+
3+
GIT_DIR=$(git rev-parse --git-dir)
4+
5+
echo "Installing hooks..."
6+
# this command creates symlink to our pre-commit script
7+
ln -s ../../scripts/pre-commit.bash $GIT_DIR/hooks/pre-commit
8+
echo "Done!"

flutter/scripts/pre-commit.bash

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# To avoid running this pre-commit simply write your commit like:
2+
# git commit -m "test commit" --no-verify
3+
# with the --no-verify flag being the operative addition
4+
5+
printf "\e[33;1m%s\e[0m\n" 'Pre-Commit'
6+
7+
# Undo the stash of the files
8+
pop_stash_files () {
9+
if [ -n "$hasChanges" ]; then
10+
printf "\e[33;1m%s\e[0m\n" '=== Applying git stash changes ==='
11+
git stash pop
12+
fi
13+
}
14+
15+
# Stash unstaged files
16+
hasChanges=$(git diff)
17+
if [ -n "$hasChanges" ]; then
18+
printf "\e[33;1m%s\e[0m\n" 'Stashing unstaged changes'
19+
git stash push --keep-index
20+
fi
21+
22+
# Flutter import sorter
23+
printf "\e[33;1m%s\e[0m\n" '=== Running Flutter import sorter ==='
24+
flutter pub run import_sorter:main
25+
hasNewFilesSorted=$(git diff)
26+
if [ -n "$hasNewFilesSorted" ]; then
27+
git add .
28+
printf "\e[33;1m%s\e[0m\n" 'Sorted imports added to git stage'
29+
fi
30+
printf "\e[33;1m%s\e[0m\n" 'Finished running Flutter import sorter'
31+
printf '%s\n' "${avar}"
32+
33+
# Flutter formatter
34+
printf "\e[33;1m%s\e[0m\n" '=== Running Flutter formatter ==='
35+
flutter format .
36+
37+
hasNewFilesFormatted=$(git diff)
38+
if [ -n "$hasNewFilesFormatted" ]; then
39+
git add .
40+
printf "\e[33;1m%s\e[0m\n" 'Formatted files added to git stage'
41+
fi
42+
printf "\e[33;1m%s\e[0m\n" 'Finished running Flutter Formatter'
43+
printf '%s\n' "${avar}"
44+
45+
# Flutter Analyzer
46+
printf "\e[33;1m%s\e[0m\n" '=== Running Flutter analyzer'
47+
flutter analyze
48+
49+
if [ $? -ne 0 ]; then
50+
printf "\e[31;1m%s\e[0m\n" '=== Flutter analyzer error ==='
51+
pop_stash_files
52+
exit 1
53+
fi
54+
55+
printf "\e[33;1m%s\e[0m\n" 'Finished running Flutter analyzer'
56+
printf '%s\n' "${avar}"
57+
58+
# Unit tests
59+
printf "\e[33;1m%s\e[0m\n" '=== Running Unit Tests ==='
60+
flutter test -r expanded
61+
62+
if [ $? -ne 0 ]; then
63+
printf "\e[31;1m%s\e[0m\n" '=== Unit tests error ==='
64+
pop_stash_files
65+
exit 1
66+
fi
67+
68+
printf "\e[33;1m%s\e[0m\n" 'Finished running Unit Tests'
69+
printf '%s\n' "${avar}"
70+
71+
pop_stash_files
72+
73+
printf "\e[33;1m%s\e[0m\n" 'Finished all tasks'

flutter/scripts/pre-commit.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Pre Commit Guide
2+
3+
### Introduction
4+
5+
This pre-commit script makes use of a symlink file in the git registry to target the `pre-commit.bash` file in _/scripts_
6+
7+
To register the symlink, run
8+
`chmod +x scripts/*.bash && ./scripts/install-hooks.bash`
9+
in a terminal in the root directory of the repository. Use a bash terminal for this, powershell typically doesn't work.
10+
11+
The `install-hooks.bash` file should look like this
12+
13+
```
14+
GIT_DIR=$(git rev-parse --git-dir)
15+
16+
echo "Installing hooks..."
17+
# this command creates symlink to our pre-commit script
18+
ln -s ../../scripts/pre-commit.bash $GIT_DIR/hooks/pre-commit
19+
echo "Done!"
20+
```
21+
22+
### Sections
23+
24+
1. **Stash**
25+
The stash section stashes any non-staged files tracked by git.
26+
2. **Import Sorter**
27+
The import sorter organizes imports across the dart files tracked by git in the repository. It is run multiple times due to the nested nature of our packages. One execution is for the root package, or the workspace. The script then moves into the child package and executes the sorter once more, before stepping out again to the root workspace and staging the changes.
28+
3. **Formatter**
29+
The formatter lints the project files and stages any changes.
30+
4. **Analyzer**
31+
The analyzer checks the code for errors and breaches of convention. Any warnings cause the commit to exit and must be dealt with before proceeding. If a warning is idiomatic but not desirable, ignore comments may be generated inline for the reported errors.
32+
5. **Unit Tests**
33+
The script also executes unit tests using the dart test library, which looks for files with the convention of \*\_test.dart
34+
We use the convention of \*.\<subextension>.test.dart for test files, which will not be detected by the dart test library. To circumvent this issue, import tests into the /test/widget_test.dart file under an alias and execute the unit test functions in main()
35+
If no tests are found this will fail, it is recommended to place a generic test within the main function of /test/widget_test.dart such as
36+
37+
```
38+
void main() {
39+
test('App starts', () {
40+
expect(true, true);
41+
});
42+
}
43+
```

0 commit comments

Comments
 (0)