Skip to content

State management #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2022
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.0.1-dev.2

- Add support for riverpod.

# 0.0.1-dev.1

- Initial development release.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Out of the box, `Flutter Starter CLI` includes:-

- ✅ **State Management**
- BLoC - BLoC is a popular design/architectural pattern that is used to separate the logic from the UI.
- Riverpod - (Coming Soon... 🥳)
- RiverPod - A state-management library that catches programming errors at compile time and ensures that the code is testable.
- ✅ **API-Services**
- Dio - A powerful HTTP client for Dart, which supports Interceptors, Global configuration, FormData, Request Cancellation, File downloading, Timeout etc.
- Http - A composable, Future-based library for making HTTP requests.
Expand All @@ -32,7 +32,7 @@ Currently, the `Flutter Starter CLI` depends on the following packages:-
| Package | Version | Description |
| ------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------- |
| args | 2.3.1 | Parses raw command-line arguments into a set of options and values. |
| mason_logger | 0.1.3 | Simple logging library for CLI requests. |
| mason_logger | 0.2.1 | Simple logging library for CLI requests. |
| path | 1.8.2 | The path package provides common operations for manipulating paths: joining, splitting, normalizing, etc. |
| pub_updater | 0.2.1 | A Dart package which enables checking whether packages are up to date and supports updating them. |
| flutter_lints | 2.0.1 | This package contains a recommended set of lints for Flutter apps, packages, and plugins to encourage good coding practices. |
Expand Down Expand Up @@ -67,16 +67,11 @@ $ flutter_starter_cli create
```

```sh
# Shorthand to create project with git
$ flutter_starter_cli create <project_name> --api=<api_service> -g

# Shorthand to create project with test
$ flutter_starter_cli create <project_name> --api=<api_service> -t

# Shorthand to create project without git and test
$ flutter_starter_cli create <project_name> --api=<api_service>
# Shorthand to create project
$ flutter_starter_cli create <project_name> --state=<state_management> --api=<api_service> -g -t

# Available API services (dio, http)
# Available State management (bloc, riverpod)
```

## Complete Usage
Expand All @@ -93,6 +88,8 @@ Usage: flutter_starter_cli create <project_name>
(defaults to "A New Flutter Project.")
--org The organization for the project.
(defaults to "com.example")
-s, --state The state management for the project.
[bloc, riverpod]
-a, --api The API service for the project.
[dio, http]
-t, --[no-]test Setup Test Cases.
Expand Down
5 changes: 3 additions & 2 deletions lib/src/cli/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import 'package:flutter_starter_cli/src/cli/cli.dart';
import 'package:flutter_starter_cli/src/utils.dart';

class Actions {
static Future<void> createProject(String path) async {
static Future<void> createProject(String path, String state) async {
Status.start('Project Creating...');
try {
await Cli.cloneProject(path);
await Cli.cloneProject(path, state);
Status.complete('Project Created!!!');
} catch (_) {
Status.fail('Project Creation Failed!!!');
Expand Down Expand Up @@ -39,6 +39,7 @@ class Actions {
final contents = await file.readAsString();
file = await file.writeAsString(contents
.replaceAll('flutter_starter', name)
.replaceAll('Flutter Starter', name)
.replaceAll('A new Flutter project.', desc)
.replaceAll('com.example', org)
.replaceAll(
Expand Down
22 changes: 13 additions & 9 deletions lib/src/cli/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ class Cli {
return result;
}

static Future<void> cloneProject(String path) async {
static _delete(Directory target) async {
await target.delete(recursive: true);
}

static Future<void> cloneProject(String path, String state) async {
await _run(
'git',
[
'clone',
'https://github.com/Geekyants/flutter-starter.git',
'--branch',
'development',
state,
'--single-branch',
path
],
Expand All @@ -35,23 +39,23 @@ class Cli {
static Future<void> removeFiles(String path, String api, bool test) async {
Directory target;
target = Directory(join(path, '.git'));
await target.delete(recursive: true);
await _delete(target);
if (api == 'dio') {
target = Directory(join(path, 'lib', 'api_sdk', 'http'));
await target.delete(recursive: true);
await _delete(target);
target = Directory(join(path, 'lib', 'api_sdk', 'http_api_sdk.dart'));
await target.delete(recursive: true);
await _delete(target);
} else {
target = Directory(join(path, 'lib', 'api_sdk', 'dio'));
await target.delete(recursive: true);
await _delete(target);
target = Directory(join(path, 'lib', 'api_sdk', 'dio_api_sdk.dart'));
await target.delete(recursive: true);
await _delete(target);
}
if (!test) {
target = Directory(join(path, 'integration_test'));
await target.delete(recursive: true);
await _delete(target);
target = Directory(join(path, 'test'));
await target.delete(recursive: true);
await _delete(target);
}
}

Expand Down
36 changes: 31 additions & 5 deletions lib/src/commands/create_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class CreateCommand extends Command<int> {
help: 'The organization for the project.',
defaultsTo: 'com.example',
)
..addOption(
'state',
abbr: 's',
help: 'The state management for the project.',
allowed: [StateManagement.bloc.name, StateManagement.riverpod.name],
)
..addOption(
'api',
abbr: 'a',
Expand Down Expand Up @@ -58,13 +64,15 @@ class CreateCommand extends Command<int> {
final name = _name;
final desc = _desc;
final org = _org;
final state = _state;
final api = _api;
final test = _test;
final test = state == StateManagement.bloc.name ? _test : true;
final git = _git;
final path = '${Directory.current.path}/$name';
final target = Directory(path);
if (!target.existsSync()) await target.create();
await onGenerateComplete(_logger, path, name, desc, org, api, test, git);
await onGenerateComplete(
_logger, path, name, desc, org, state, api, test, git);
_logger.success('Your Project is Ready to Use 🚀');
return ExitCode.success.code;
}
Expand All @@ -80,6 +88,15 @@ class CreateCommand extends Command<int> {
return args.first;
}

String get _state {
return argResults?['state'] ??
_logger.chooseOne(
'Select the State Management',
choices: [StateManagement.bloc.name, StateManagement.riverpod.name],
defaultValue: StateManagement.bloc.name,
);
}

String get _api {
return argResults?['api'] ??
_logger.chooseOne(
Expand Down Expand Up @@ -121,9 +138,18 @@ class CreateCommand extends Command<int> {
);
}

Future<void> onGenerateComplete(Logger logger, String path, String name,
String desc, String org, String api, bool test, bool git) async {
await Actions.createProject(path);
Future<void> onGenerateComplete(
Logger logger,
String path,
String name,
String desc,
String org,
String state,
String api,
bool test,
bool git,
) async {
await Actions.createProject(path, state);
await Actions.generateFiles(path, name, desc, org, api, test);
await Actions.setupPackages(path, api, test);
await Actions.getPackages(path);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ enum APIService {
http,
}

enum StateManagement {
bloc,
riverpod,
}

class Status {
static late Progress progress;
static late Logger logger;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const packageVersion = '0.0.1-dev.1';
const packageVersion = '0.0.1-dev.2';
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_starter_cli
description: A command line tool that provides ease of setting up a Flutter project.
version: 0.0.1-dev.1
version: 0.0.1-dev.2

homepage: https://flutter-starter.geekyants.com
repository: https://github.com/GeekyAnts/flutter-starter-cli
Expand All @@ -10,7 +10,7 @@ environment:

dependencies:
args: ^2.3.1
mason_logger: ^0.2.0
mason_logger: ^0.2.1
path: ^1.8.2
pub_updater: ^0.2.1

Expand Down