Skip to content

ksokolovskyi/envy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build codecov pub package

Welcome to Envy, code generator for extracting environment variables.

Installation

To use Envy, you will need your typical build_runner/code-generator setup.
First, install build_runner and Envy by adding them to your pubspec.yaml file:

If you are using in a Flutter project:

$ flutter pub add envy_annotation
$ flutter pub add --dev build_runner
$ flutter pub add --dev envy_generator

If you are using in a Dart project:

$ dart pub add envy_annotation
$ dart pub add --dev build_runner
$ dart pub add --dev envy_generator

This installs three packages:

Run the generator

To run the code generator, execute the following command:

dart run build_runner build

For Flutter projects, you can also run:

flutter pub run build_runner build

Note that like most code-generators, Envy will need you to both import the annotation (envy_annotation) and use the part keyword on the top of your files.

As such, a file that wants to use Envy will start with:

import 'package:envy_annotation/envy_annotation.dart';

part 'config.e.dart';

Creating a Config Model using Envy

An example is better than a long abstract explanation, so here's a typical Envy class:

// This file is "config.dart"
import 'package:envy_annotation/envy_annotation.dart';

// required: associates our `config.dart` with the code generated by Envy
part 'config.e.dart';

@envy
class Config {
  @variable
  static String apiBaseUrl = _Envy.apiBaseUrl;

  @variable
  static String get apiUrl => _Envy.apiUrl;

  @variable
  static bool get devMode => _Envy.devMode;

  @variable
  static int get threadsCount => _Envy.threadsCount;

  @variable
  static double get similarityDistanceThreshold =>
      _Envy.similarityDistanceThreshold;

  @variable
  static String? get missingOne => _Envy.missingOne;

  static String get testVar => 'test';
}
# This file is ".env"
API_BASE_URL=https://my.api
API_URL=$API_BASE_URL/guest/json-rpc

DEV_MODE=TRUE

THREADS_COUNT=3

SIMILARITY_DISTANCE_THRESHOLD=0.2

The following dart snippet defines a model named Config:

  • Config has 2 properties: apiBaseUrl and testVar.
  • Config has 6 getters: apiUrl, devMode, threadsCount, similarityDistanceThreshold, missingOne.
  • Because we are using @envy, Envy will try to extract value for each property/getter which is marked by @variable.
  • Getters and properties which are marked with the @variable have links to the _Envy class which holds extracted values from the .env file.

From this example, we can notice a few things:

  • It is necessary to annotate our class with @envy or @Envy.
    This annotation is what tells Envy to generate code for that class.

  • It is necessary to annotate each property/getter of the class with @variable or @Variable for which you need to extract value from .env file.
    This annotation is what tells Envy which variables to look for.

Supported types

Envy supports the following types: int, int?, double, double?, bool, bool?, String, String?.

Releases

No releases published

Packages

No packages published

Languages