diff --git a/spec/workspace_spec.cr b/spec/workspace_spec.cr index 14b3c160..49d070ee 100644 --- a/spec/workspace_spec.cr +++ b/spec/workspace_spec.cr @@ -13,6 +13,7 @@ describe Mint::Workspace do path: Path[workspace.root_path, "mint.json"].to_s, check: Mint::Check::Environment, include_tests: false, + dot_env: ".env", format: false) results.size.should eq(1) @@ -30,6 +31,7 @@ describe Mint::Workspace do check: Mint::Check::Environment, path: workspace.root_path, include_tests: false, + dot_env: ".env", format: false) results.size.should eq(1) @@ -51,6 +53,7 @@ describe Mint::Workspace do path: Path[workspace.root_path, "mint.json"].to_s, check: Mint::Check::Environment, include_tests: false, + dot_env: ".env", format: false) FileUtils.touch(Path[workspace.root_path, "Main.mint"]) diff --git a/src/command.cr b/src/command.cr index 2a01338c..2315d93b 100644 --- a/src/command.cr +++ b/src/command.cr @@ -31,11 +31,6 @@ module Mint result = nil begin - # Load environment variables. - Env.init(env) do |file| - terminal.puts "#{COG} Loaded environment variables from: #{file}" - end - check_dependencies! if check_dependencies # Measure elapsed time of a command. diff --git a/src/commands/build.cr b/src/commands/build.cr index b465bd5b..39635650 100644 --- a/src/commands/build.cr +++ b/src/commands/build.cr @@ -39,9 +39,10 @@ module Mint def run execute "Building for production", - check_dependencies: true, env: flags.env do + check_dependencies: true do Workspace.new( path: Path[Dir.current, "mint.json"].to_s, + dot_env: flags.env || ".env", check: Check::Environment, include_tests: false, format: false, diff --git a/src/commands/start.cr b/src/commands/start.cr index 9cb58f1b..15d48f33 100644 --- a/src/commands/start.cr +++ b/src/commands/start.cr @@ -32,10 +32,11 @@ module Mint def run execute "Running the development server", - check_dependencies: true, env: flags.env do + check_dependencies: true do Reactor.new( reload: !flags.no_reload, format: flags.format, + dot_env: flags.env, host: flags.host, port: flags.port ) do |type_checker| diff --git a/src/commands/test.cr b/src/commands/test.cr index 47abe17e..b805eebe 100644 --- a/src/commands/test.cr +++ b/src/commands/test.cr @@ -56,7 +56,7 @@ module Mint def run runner = - execute "Running tests", env: flags.env do + execute "Running tests" do TestRunner.new(flags, arguments) end diff --git a/src/commands/tool/docs_server.cr b/src/commands/tool/docs_server.cr index a5d30801..13f32a0c 100644 --- a/src/commands/tool/docs_server.cr +++ b/src/commands/tool/docs_server.cr @@ -20,6 +20,7 @@ module Mint Reactor.new( host: flags.host, port: flags.port, + dot_env: ".env", format: false, reload: true, ) do |type_checker| diff --git a/src/env.cr b/src/env.cr index de86809e..4bd3ca56 100644 --- a/src/env.cr +++ b/src/env.cr @@ -7,15 +7,14 @@ module Mint class_getter env : String? - def load(&) + def load env.try do |value| MINT_ENV.clear MINT_ENV.merge!(Dotenv.load(value)) - yield end end - def init(raw, &) + def init(raw) env = if !raw.presence ".env" if File.exists?(".env") @@ -33,7 +32,7 @@ module Mint end unless File.exists?(env) @@env = env - load { yield env } + load end end end diff --git a/src/ls/sandbox.cr b/src/ls/sandbox.cr index 41d44b5c..9d2d24c1 100644 --- a/src/ls/sandbox.cr +++ b/src/ls/sandbox.cr @@ -24,6 +24,7 @@ module Mint listener: ->build(TypeChecker | Error), check: Check::Unreachable, include_tests: true, + dot_env: ".env", format: false) end diff --git a/src/ls/server.cr b/src/ls/server.cr index 772e2fcb..4e5d508f 100644 --- a/src/ls/server.cr +++ b/src/ls/server.cr @@ -37,6 +37,7 @@ module Mint Workspace.new( check: Check::Unreachable, include_tests: true, + dot_env: ".env", listener: nil, format: false, path: base) diff --git a/src/reactor.cr b/src/reactor.cr index cd528126..e28244c5 100644 --- a/src/reactor.cr +++ b/src/reactor.cr @@ -9,6 +9,7 @@ module Mint def initialize( *, @reload : Bool, + dot_env, format, host, port, @@ -16,6 +17,7 @@ module Mint ) Workspace.new( path: Path[Dir.current, "mint.json"].to_s, + dot_env: dot_env || ".env", check: Check::Environment, include_tests: false, format: format, diff --git a/src/test_runner.cr b/src/test_runner.cr index 637b7fab..64e3cd51 100644 --- a/src/test_runner.cr +++ b/src/test_runner.cr @@ -23,6 +23,7 @@ module Mint Workspace.new( path: Path[Dir.current, "mint.json"].to_s, + dot_env: flags.env || ".env", check: Check::Environment, include_tests: true, format: false, diff --git a/src/utils/source_files.cr b/src/utils/source_files.cr index 56c74ef6..36c71e98 100644 --- a/src/utils/source_files.cr +++ b/src/utils/source_files.cr @@ -14,10 +14,10 @@ module Mint end.map { |dir| glob_pattern(File.dirname(json.path), dir) } end - def everything(json : MintJson, *, include_tests = false) : Array(String) + def everything(json : MintJson, *, include_tests = false, dot_env = ".env") : Array(String) packages(json, include_self: true) .flat_map { |item| globs(item, include_tests: include_tests) + [item.path] } - .push(Path[File.dirname(json.path), ".env"].to_s) + .push(Path[File.dirname(json.path), dot_env].to_s) end def packages(json : MintJson, *, include_self = false) : Array(MintJson) diff --git a/src/workspace.cr b/src/workspace.cr index e2f9bd0d..b9fb5f04 100644 --- a/src/workspace.cr +++ b/src/workspace.cr @@ -20,10 +20,14 @@ module Mint *, @listener : Proc(TypeChecker | Error, Nil) | Nil, @include_tests : Bool, + dot_env : String, @format : Bool, @check : Check, @path : String ) + @dot_env = + File.basename(dot_env) + (@watcher = Watcher.new(&->update(Array(String), Symbol))) .tap { reset } .watch @@ -102,7 +106,8 @@ module Mint @watcher.patterns = SourceFiles.everything( MintJson.parse(@path, search: true), - include_tests: @include_tests) + include_tests: @include_tests, + dot_env: @dot_env) rescue error : Error set(error) end @@ -154,11 +159,12 @@ module Mint # 1. packages could have changed # 2. source directories could have changed # 3. variables in the .env file cloud have changed - case basename = File.basename(file) - when "mint.json", ".env" - Env.init(basename) { } if basename == ".env" - actions << :reset + case File.basename(file) + when @dot_env + Env.init(file) end + + actions << :reset end end end