Skip to content
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

feat: optional path build.yaml option #83

Merged
merged 9 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions examples/envied_example/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
KEY1=DEBUG1
KEY2=DEBUG2
key3=DEBUG3
key4=1
key5=true
7 changes: 7 additions & 0 deletions examples/envied_example/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
targets:
$default:
builders:
envied_generator|envied:
options:
path: .env.test
override: true
4 changes: 2 additions & 2 deletions examples/envied_example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ packages:
path: "../../packages/envied"
relative: true
source: path
version: "0.5.1"
version: "0.5.2"
envied_generator:
dependency: "direct dev"
description:
path: "../../packages/envied_generator"
relative: true
source: path
version: "0.5.1"
version: "0.5.2"
file:
dependency: transitive
description:
Expand Down
11 changes: 9 additions & 2 deletions packages/envied_generator/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ library envied.builder;

import 'package:build/build.dart';
import 'package:envied_generator/envied_generator.dart';
import 'package:envied_generator/src/build_options.dart';
import 'package:source_gen/source_gen.dart';

/// Primary builder to build the generated code from the `EnviedGenerator`
Builder enviedBuilder(BuilderOptions options) =>
SharedPartBuilder([const EnviedGenerator()], 'envied');
Builder enviedBuilder(BuilderOptions options) => SharedPartBuilder(
[
EnviedGenerator(
BuildOptions.fromMap(options.config),
)
],
'envied',
);
27 changes: 27 additions & 0 deletions packages/envied_generator/lib/src/build_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Represents the options passed to the build runner via build.yaml.
///
/// For example
///
/// ```yaml
/// targets:
/// $default:
/// builders:
/// envied_generator|envied:
/// options:
/// path: .env.test
/// override: true
/// ```
class BuildOptions {
const BuildOptions({
this.override,
this.path,
});

final String? path;
final bool? override;

factory BuildOptions.fromMap(Map<String, dynamic> map) => BuildOptions(
override: map['override'] as bool?,
path: map['path'] as String?,
);
}
10 changes: 8 additions & 2 deletions packages/envied_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';
import 'package:envied/envied.dart';
import 'package:envied_generator/src/build_options.dart';
import 'package:envied_generator/src/generate_field.dart';
import 'package:envied_generator/src/generate_field_encrypted.dart';
import 'package:envied_generator/src/load_envs.dart';
Expand All @@ -19,7 +20,9 @@ import 'package:source_gen/source_gen.dart';
/// Will throw an [InvalidGenerationSourceError] if the annotated
/// element is not a [classElement].
final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
const EnviedGenerator();
const EnviedGenerator(this._buildOptions);

final BuildOptions _buildOptions;

@override
Future<String> generateForAnnotatedElement(
Expand All @@ -36,7 +39,10 @@ final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
}

final Envied config = Envied(
path: annotation.read('path').literalValue as String?,
path: _buildOptions.override == true &&
_buildOptions.path?.isNotEmpty == true
? _buildOptions.path
: annotation.read('path').literalValue as String?,
requireEnvFile:
annotation.read('requireEnvFile').literalValue as bool? ?? false,
name: annotation.read('name').literalValue as String?,
Expand Down
21 changes: 21 additions & 0 deletions packages/envied_generator/test/.env.example_with_path_override
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_string=test_string
testString=testString
TEST_STRING=TEST_STRING
testInt=123
TEST_INT=123
testDouble=1.23
testNum=1.23
TEST_DOUBLE=1.23
TEST_BOOL=true
testBool=true
testDynamic=123abc
TEST_DYNAMIC=123_ABC
testUrl=https://foo.bar/baz
emptyTestUrl=
invalidTestUrl="::Not valid URI::"
testDateTime=2023-11-06T22:32:55.287Z
invalidTestDateTime=2023-11-06X22:32:55.287Z
testDate=2023-11-06
invalidTestDate=2023
testEnum=ipsum
invalidTestEnum=foo
27 changes: 21 additions & 6 deletions packages/envied_generator/test/envy_generator_test.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import 'package:envied_generator/envied_generator.dart';
import 'package:envied_generator/src/build_options.dart';
import 'package:source_gen_test/source_gen_test.dart';

Future<void> main() async {
// for annotated elements
initializeBuildLogTracking();
final reader = await initializeLibraryReaderForDirectory(
'test/src',
'generator_tests.dart',
);

// print(Platform.environment['SYSTEM_VAR']);

testAnnotatedElements(
reader,
EnviedGenerator(),
await initializeLibraryReaderForDirectory(
'test/src',
'generator_tests.dart',
),
EnviedGenerator(
const BuildOptions(),
),
);

testAnnotatedElements(
await initializeLibraryReaderForDirectory(
'test/src',
'generator_tests_with_path_override.dart',
),
EnviedGenerator(
const BuildOptions(
path: 'test/.env.example_with_path_override',
override: true,
),
),
);
}
Loading