Skip to content

Commit e13349d

Browse files
committed
feat: adds support for flavors
1 parent 5444290 commit e13349d

17 files changed

+518
-30
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# dart_define changelog
22

3+
## 1.0.4
4+
5+
- feat: adds support for flavors
6+
37
## 1.0.3
48

59
- fix: fixes issues with analyzer

README.md

+90-7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ When the generator is run, `dart_define` reads the values from
6161
```sh
6262
# add the tool to dependencies
6363
flutter pub add --dev dart_define
64+
65+
# If you chooce this method, you'll have to add `pub run`
66+
# in from of all of the `dart_define` commands.
67+
# Eg. `pub run dart_define generate`
6468
```
6569

6670
2. Define your variables in `pubspec.yaml`
@@ -76,6 +80,18 @@ When the generator is run, `dart_define` reads the values from
7680
# Defaults to true
7781
json: true
7882
83+
# OPTIONAL: The path to the json file to generate.
84+
# Defaults to dart_define.json
85+
json_path: dart_define.json
86+
87+
# OPTIONAL: The path to the dart file to generate.
88+
# Defaults to lib/dart_define.gen.dart
89+
dart_path: lib/dart_define.gen.dart
90+
91+
# OPTIONAL: The name of the generated class.
92+
# Defaults to DartDefine
93+
class_name: DartDefine
94+
7995
# REQUIRED: The variables to add to the generated config files
8096
variables:
8197
@@ -102,6 +118,19 @@ When the generator is run, `dart_define` reads the values from
102118
- name: BOOL_VALUE
103119
description: An example bool value
104120
required: true
121+
122+
# OPTIONAL: The flavors to use within the app
123+
flavors:
124+
125+
# REQUIRED: The name of the flavor
126+
- name: production
127+
128+
# REQUIRED: The description of the flavor
129+
description: The production flavor
130+
- name: staging
131+
description: The staging flavor
132+
- name: development
133+
description: The development flavor
105134
```
106135

107136
3. Generate boilerplate
@@ -111,13 +140,6 @@ When the generator is run, `dart_define` reads the values from
111140
dart_define generate
112141
```
113142

114-
OR
115-
116-
```sh
117-
# If you added the tool to dependencies
118-
dart run dart_define generate
119-
```
120-
121143
> *NOTE: You can override values and variables from pubspec.yaml
122144
> config with CLI arguments*
123145
>
@@ -222,4 +244,65 @@ When the generator is run, `dart_define` reads the values from
222244
flutter build ios --dart-define-from-file=dart_define.json
223245
```
224246

247+
## Setting up flavors 🍬
248+
249+
`dart_define` allows you to create multiple configurations, for different
250+
environments or white labels of your app. This means you can easily generate
251+
config files for all of the environments or white labels and launch your app
252+
with the configurations. To set these up
253+
254+
1. Add flavors to `dart_define` config in `pubspec.yaml`
255+
256+
```yaml
257+
dart_define:
258+
variables:
259+
- name: APP_NAME
260+
description: The apps name
261+
default: My App
262+
required: false
263+
- name: APP_ID
264+
description: The apps unique id
265+
default: com.my.app
266+
required: false
267+
flavors:
268+
- name: production
269+
description: The production flavor
270+
- name: staging
271+
description: The staging flavor
272+
- name: development
273+
description: The development flavor
274+
```
275+
276+
2. Generate the config file for all of your flavors
277+
278+
```sh
279+
dart_define generate --json_path=config/production.json --FLAVOR=production
280+
dart_define generate --json_path=config/staging.json --FLAVOR=staging
281+
dart_define generate --json_path=config/development.json --FLAVOR=development
282+
```
283+
284+
3. Use the flavor in your app
285+
286+
```dart
287+
/// `dart_define` generates this enum based on the flavors in your configurations
288+
/// You can access the current flavor used to launch the application by calling
289+
/// DartDefine.flavor
290+
enum Flavor {
291+
/// The production flavor
292+
production,
293+
294+
/// The staging flavor
295+
staging,
296+
297+
/// The development flavor
298+
development,
299+
}
300+
```
301+
302+
4. Run application from specific flavor
303+
304+
```sh
305+
flutter run --dart-define-from-file=config/development.json
306+
```
307+
225308
[1]: https://itnext.io/flutter-3-7-and-a-new-way-of-defining-compile-time-variables-f63db8a4f6e2

example/dart_define.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"STRING_VALUE": "hello world!",
33
"INT_VALUE": 3,
4-
"BOOL_VALUE": true
4+
"BOOL_VALUE": true,
5+
"FLAVOR": "production"
56
}
67

example/lib/dart_define.gen.dart

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
class DartDefine {
33
DartDefine._();
44

5+
/// The flavor the app was launched with
6+
static Flavor get flavor => FlavorExtension.fromString(
7+
const String.fromEnvironment('FLAVOR'),
8+
);
9+
510
/// An example String value
611
static const stringValue = String.fromEnvironment('STRING_VALUE');
712

@@ -11,3 +16,30 @@ class DartDefine {
1116
/// An example bool value
1217
static const boolValue = bool.fromEnvironment('BOOL_VALUE');
1318
}
19+
20+
/// The flavors supported by the application
21+
enum Flavor {
22+
/// The production flavor
23+
production,
24+
25+
/// The staging flavor
26+
staging,
27+
28+
/// The development flavor
29+
development,
30+
}
31+
32+
extension FlavorExtension on Flavor {
33+
static Flavor fromString(String value) {
34+
switch (value) {
35+
case 'production':
36+
return Flavor.production;
37+
case 'staging':
38+
return Flavor.staging;
39+
case 'development':
40+
return Flavor.development;
41+
default:
42+
throw throw Exception('Invalid flavor');
43+
}
44+
}
45+
}

example/pubspec.yaml

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ flutter:
2020
uses-material-design: true
2121

2222
dart_define:
23-
dart: true # Whether to generate the dart boilerplate or not. Defaults to true
24-
json: true # Whether to generate the json boilerplate or not. Defaults to true
23+
dart: true # OPTIONAL: Whether to generate the dart boilerplate or not. Defaults to true
24+
json: true # OPTIONAL: Whether to generate the json boilerplate or not. Defaults to true
25+
json_path: dart_define.json # OPTIONAL: The path to the json file to generate. Defaults to dart_define.json
26+
dart_path: lib/dart_define.gen.dart # OPTIONAL: The path to the dart file to generate. Defaults to lib/dart_define.gen.dart
27+
class_name: DartDefine # OPTIONAL: The name of the generated class. Defaults to DartDefine
28+
2529
variables: # The variables to add to the generated config files
2630
- name: STRING_VALUE # REQUIRED: The name of the variable
2731
description: An example String value # REQUIRED: The description of the variable
@@ -34,3 +38,10 @@ dart_define:
3438
- name: BOOL_VALUE
3539
description: An example bool value
3640
required: true
41+
flavors: # OPTIONAL: The flavors to use within the app
42+
- name: production # REQUIRED: The name of the flavor
43+
description: The production flavor # REQUIRED: The description of the flavor
44+
- name: staging
45+
description: The staging flavor
46+
- name: development
47+
description: The development flavor

lib/src/commands/src/generate_command.dart

+30-2
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,40 @@ class GenerateCommand extends Command<int> {
6161
variable.name,
6262
help:
6363
'${variable.required ? 'required' : 'optional'}: ${variable.description}}',
64-
defaultsTo:
65-
variable.required ? null : variable.defaultValue?.toString(),
64+
defaultsTo: variable.defaultValue?.toString(),
6665
valueHelp: variable.defaultValue?.toString(),
6766
);
6867
}
6968

69+
final flavors = config.flavors;
70+
71+
if (flavors != null) {
72+
if (config.variables.any((v) => v.name == kFlavorArg)) {
73+
throw ArgumentError(
74+
'You cannot specify a variable named "$kFlavorArg" when you have flavors in your config file. This name is reserved for the flavor argument.',
75+
);
76+
}
77+
78+
config = config.copyWith(
79+
variables: [
80+
...config.variables,
81+
VariableConfiguration(
82+
name: kFlavorArg,
83+
description: '',
84+
defaultValue: flavors.first.name,
85+
),
86+
],
87+
);
88+
89+
argParser.addOption(
90+
kFlavorArg,
91+
help: 'optional: Flavor to generate the code for',
92+
allowed: flavors.map((e) => e.name),
93+
valueHelp: flavors.first.name,
94+
defaultsTo: flavors.first.name,
95+
);
96+
}
97+
7098
readingConfig.complete();
7199
}
72100

lib/src/model/model.dart

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export 'src/argument_variable.dart';
22
export 'src/dart_define_configuration.dart';
3+
export 'src/flavor_configuration.dart';
34
export 'src/variable_configuration.dart';

lib/src/model/src/dart_define_configuration.dart

+10-6
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,41 @@ class DartDefineConfiguration with _$DartDefineConfiguration {
2828
name: kGenerateDartArg,
2929
defaultValue: kGenerateDartArgDefault,
3030
)
31-
bool generateDart,
31+
bool generateDart,
3232
@Default(kGenerateJsonArgDefault)
3333
@JsonKey(
3434
name: kGenerateJsonArg,
3535
defaultValue: kGenerateJsonArgDefault,
3636
)
37-
bool generateJson,
37+
bool generateJson,
3838
@Default(kDartPathArgDefault)
3939
@JsonKey(
4040
name: kDartPathArg,
4141
defaultValue: kDartPathArgDefault,
4242
)
43-
String dartPath,
43+
String dartPath,
4444
@Default(kJsonPathArgDefault)
4545
@JsonKey(
4646
name: kJsonPathArg,
4747
defaultValue: kJsonPathArgDefault,
4848
)
49-
String jsonPath,
49+
String jsonPath,
5050
@Default(kClassNameArgDefault)
5151
@JsonKey(
5252
name: kClassNameArg,
5353
defaultValue: kClassNameArgDefault,
5454
)
55-
String className,
55+
String className,
5656
@Default([])
5757
@JsonKey(
5858
name: kVariablesKey,
5959
defaultValue: [],
6060
)
61-
List<VariableConfiguration> variables,
61+
List<VariableConfiguration> variables,
62+
@JsonKey(
63+
name: kFlavorsKey,
64+
)
65+
List<FlavorConfiguration>? flavors,
6266
}) = _DartDefineConfiguration;
6367

6468
const DartDefineConfiguration._();

lib/src/model/src/dart_define_configuration.g.dart

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)