Skip to content

Commit

Permalink
## 1.7.0
Browse files Browse the repository at this point in the history
* Change loader format
* Remove asset loader
* Update logo
* Update documentation
* Add flag obfuscate
* Remove flutter from pubspec
  • Loading branch information
Nialixus committed Oct 11, 2023
1 parent 490a638 commit ef9236f
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 191 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@
* Automatically add output into .gitignore

## 1.6.0
* Update pubspec
* Update pubspec

## 1.7.0
* Change loader format
* Remove asset loader
* Update logo
* Update documentation
* Add flag obfuscate
* Remove flutter from pubspec
21 changes: 13 additions & 8 deletions bin/env_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ void main(List<String> arguments) async {
ArgParser runner = ArgParser()
..addOption('input',
abbr: 'i', help: 'Input path of the .env file', mandatory: true)
..addOption('password',
abbr: 'p', help: 'Password for encryption & decryption')
..addOption('output',
abbr: 'o',
help: 'Custom output path for the encrypted .env file',
defaultsTo: 'assets/env/')
..addOption('model', help: 'Generate model.dart file to your desired path')
..addFlag('help', abbr: 'h', help: 'Print this usage information')
abbr: 'o', help: 'Output path for the encrypted .env file')
..addOption('key',
abbr: 's', help: 'Secret key for encryption & decryption')
..addOption('model', help: 'Generate dart model to your desired file path')
..addFlag('null-safety',
defaultsTo: false, negatable: false, help: 'Make the model null safety')
..addFlag('obfuscate',
defaultsTo: true, help: 'Obfuscating generated values of model')
..addFlag('pubspec',
defaultsTo: true, help: 'Inserting asset path to pubspec.yaml')
..addFlag('gitignore',
defaultsTo: true, help: 'Inserting .env input file into .gitignore');
defaultsTo: true,
help: 'Inserting .env input & output file into .gitignore')
..addFlag('help',
defaultsTo: false,
abbr: 'h',
negatable: false,
help: 'Print this usage information');
try {
ArgResults argument = runner.parse(arguments);
if (argument["help"]) {
Expand Down
48 changes: 25 additions & 23 deletions bin/src/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ part of '../env_reader.dart';
/// A function to take the .env file from given [input] into a more secured version inside [directory].
void insertFile({required ArgResults from}) {
String input = from['input']!.toString();
String output = from["output"]!.toString();
String target = output.contains("/")
? output.replaceAll(RegExp(r'/[^/]+$'), "/")
: Directory.current.path;
String data = File(input).readAsStringSync();
Directory directory = Directory(target)..createSync(recursive: true);
String name = output.endsWith("/")
? (input.contains("/") ? input.split("/").last : input)
: output.contains("/")
? output.split("/").last
: output;
String? output = from["output"]?.toString();
if (output != null) {
String target = output.contains("/")
? output.replaceAll(RegExp(r'/[^/]+$'), "/")
: output;
String data = File(input).readAsStringSync();
Directory directory = Directory(target)..createSync(recursive: true);
String name = output.endsWith("/")
? (input.contains("/") ? input.split("/").last : input)
: output.contains("/")
? output.split("/").last
: output;

String? password = from['password']?.toString();
if (password != null) {
File asset = File(directory.path + name)
..writeAsStringSync(Encryptor.encrypt(password, data));
print(
"\x1B[32m$input\x1B[0m successfully encrypted into \x1B[34m${asset.path}\x1B[0m 🚀");
} else {
File asset = File(directory.path + name)..writeAsStringSync(data);
print(
"\x1B[32m$input\x1B[0m successfully duplicated into \x1B[34m${asset.path}\x1B[0m 🚀");
if (asset.path.startsWith("assets/")) {
String? key = from['key']?.toString();
if (key != null) {
File asset = File(directory.path + name)
..writeAsStringSync(Encryptor.encrypt(key, data));
print(
"\x1B[31mWarning:\x1B[0m Avoid openly placing exposed .env variables in assets 🔥");
"\x1B[32m$input\x1B[0m successfully encrypted into \x1B[34m${asset.path}\x1B[0m 🚀");
} else {
File asset = File(directory.path + name)..writeAsStringSync(data);
print(
"\x1B[32m$input\x1B[0m successfully duplicated into \x1B[34m${asset.path}\x1B[0m 🚀");
if (asset.path.startsWith("assets/")) {
print(
"\x1B[31mWarning:\x1B[0m Avoid openly placing exposed .env variables in assets 🔥");
}
}
}
}
14 changes: 8 additions & 6 deletions bin/src/gitignore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void insertGitignore({required ArgResults from}) {
if (insert) {
File gitignore = File('.gitignore');
String input = from["input"]!.toString();
String output = from["output"]!.toString();
String? output = from["output"]?.toString();
List<String> lines = gitignore.readAsLinesSync();
bool inputExisted = false;
bool outputExisted = false;
Expand All @@ -18,10 +18,12 @@ void insertGitignore({required ArgResults from}) {
}
}

for (var item in lines) {
if (item.contains(output)) {
outputExisted = true;
break;
if (output != null) {
for (var item in lines) {
if (item.contains(output)) {
outputExisted = true;
break;
}
}
}

Expand All @@ -36,7 +38,7 @@ void insertGitignore({required ArgResults from}) {
}
}

if (!outputExisted) {
if (!outputExisted && output != null) {
String comment = "# Env Reader related";
int index =
lines.lastIndexWhere((line) => line.trim().startsWith(comment));
Expand Down
28 changes: 18 additions & 10 deletions bin/src/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void insertJson({required ArgResults from}) {
.map((e) => capitalize(e))
.join("");
String input = from["input"]!.toString();
bool obfuscate = from['obfuscate'];
String data = File(input).readAsStringSync();
bool nullSafety = from["null-safety"];

Expand All @@ -26,28 +27,35 @@ void insertJson({required ArgResults from}) {
String cast = json.entries.map((e) {
Type type = e.value.runtimeType;
String name = dartNamed(e.key);
String variable = nullSafety
? "Env.read<$type>('${e.key}') ?? ${type == bool ? 'false' : type == int ? '0' : type == double ? '0.0' : "'${e.key}'"}"
: "Env.read<$type>('${e.key}')";
if (obfuscate) {
String variable = nullSafety
? "Env.read<$type>('${e.key}') ?? ${type == bool ? 'false' : type == int ? '0' : type == double ? '0.0' : "'${e.key}'"}"
: "Env.read<$type>('${e.key}')";

return """
return """
/// Value of `${e.key}` in environment variable. This is equal to
/// ```dart
/// $variable;
/// ```
static $type${nullSafety ? "" : "?"} $name = $variable;
""";
} else {
return """
/// Value of `${e.key}` in environment variable. This is equal to
/// ```dart
/// $type $name = Env.read<$type>('${e.key}') ?? ${type == bool ? 'false' : type == int ? '0' : type == double ? '0.0' : "'${e.key}'"};
/// print($name); // ${e.value}
/// ```
static const $type $name = ${type == String ? "'${e.value}'" : e.value ?? 'e.key'};
""";
}
}).join("\n");
String write = """
// Env Reader Auto-Generated Model File
// Created at ${DateTime.now()}
// 🍔 [Buy me a coffee](https://www.buymeacoffee.com/nialixus) 🚀
import 'package:env_reader/env_reader.dart';
/// Auto-generated environment model class.
///
/// This class represents environment variables parsed from the .env file.
/// Each static variable corresponds to an environment variable,${nullSafety ? "\n/// with default values provided for safety\n/// `false` for [bool], `0` for [int], `0.0` for [double] and `VARIABLE_NAME` for [String]." : ""}
${obfuscate ? "import 'package:env_reader/env_reader.dart';\n" : ''}
${obfuscate ? "/// This class represents environment variables parsed from the .env file.\n/// Each static variable corresponds to an environment variable,${nullSafety ? "\n/// with default values provided for safety\n/// `false` for [bool], `0` for [int], `0.0` for [double] and `VARIABLE_NAME` for [String]." : ""}" : "/// Class wrapper for duplicated values copied directly from env file"}
class $name {
$cast
}
Expand Down
3 changes: 2 additions & 1 deletion bin/src/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ part of '../env_reader.dart';
void insertPubspec({required ArgResults from}) {
bool insert = from["pubspec"] ?? true;
String input = from["input"]!.toString();
if (insert) {
String? output = from['output']?.toString();
if (insert && output != null) {
String output =
from["output"]!.toString().replaceAll(RegExp(r'/[^/]+$'), "/");
if (output.startsWith("assets/")) {
Expand Down
3 changes: 3 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Env Reader related
.env
assets/env/

# Miscellaneous
*.class
Expand Down
2 changes: 1 addition & 1 deletion example/assets/env/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GDE6V1uW1u0Z+LmxdgzW/vHCm1FtH2rCTcfJk+2bZeOXd9t/bRSNbuZXnT8ByDIMflCDX4FYXCzNCf6j27saxCSeN+ezKOvpoURTVY95aKpXfkVJyEChQ0k4ulE3NHMfpzJxOh2zc0neNh8Aw212LcNimPEtm7TlF2odThW6WW+cEFEsD2cu7rlOe3Zc7vdO5WlQaI9xsXj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bB+cNp8vfDHKfYXfN+D4ZW1ci2H8tqMK4T0Z4kMTBayZlIXhWBEwX2w=
5 changes: 3 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'package:env_reader/env_reader.dart';
import 'package:example/src/env_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Future<void> main(List<String> arguments) async {
WidgetsFlutterBinding.ensureInitialized();
await Env.load(
source: EnvLoader.asset('assets/env/.env'),
password: "MyStrongPassword",
EnvLoader.string(await rootBundle.loadString('assets/env/.env')),
"MyOptionalSecretKey",
);
runApp(
const MaterialApp(
Expand Down
4 changes: 1 addition & 3 deletions example/lib/src/env_model.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Env Reader Auto-Generated Model File
// Created at 2023-08-29 10:08:02.009503
// Created at 2023-10-11 15:28:35.710424
// 🍔 [Buy me a coffee](https://www.buymeacoffee.com/nialixus) 🚀
import 'package:env_reader/env_reader.dart';

/// Auto-generated environment model class.
///
/// This class represents environment variables parsed from the .env file.
/// Each static variable corresponds to an environment variable,
/// with default values provided for safety
Expand Down
Loading

0 comments on commit ef9236f

Please sign in to comment.