Skip to content

Commit

Permalink
fix: use proper orden to get the environments
Browse files Browse the repository at this point in the history
  • Loading branch information
adalessa committed Dec 29, 2023
1 parent 83f282b commit 2fe1157
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
9 changes: 6 additions & 3 deletions lua/laravel/config/environments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ return {
auto_dicover = true,
default = "local",
definitions = {
["sail"] = {
{
name = "sail",
condition = {
file_exists = { "vendor/bin/sail", "docker-compose.yml" },
},
Expand All @@ -15,7 +16,8 @@ return {
},
},
},
["docker-compose"] = {
{
name = "docker-compose",
condition = {
file_exists = { "docker-compose.yml" },
executable = { "docker" },
Expand All @@ -34,7 +36,8 @@ return {
},
},
},
["local"] = {
{
name = "local",
condition = {
executable = { "php" },
},
Expand Down
4 changes: 2 additions & 2 deletions lua/laravel/environment/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ local cache = {}

---@param env table
---@return Environment
function Environment:new(name, env)
function Environment:new(env)
local obj = {
name = name,
name = env.name,
condition = env.condition or nil,
commands = env.commands or {},
}
Expand Down
31 changes: 24 additions & 7 deletions lua/laravel/environment/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,46 @@ local M = {}

M.environment = nil

---@param name string|nil
---@param envs table
---@return table|nil
local function find_env_by_name(name, envs)
if not name then
return nil
end
for _, env in ipairs(envs) do
if env.name == name then
return env
end
end

return nil
end

---@return Environment|nil
local function resolve()
local opts = config.options.environments

if opts.env_variable then
local env_name = get_env(opts.env_variable)
if env_name and opts.definitions[env_name] then
return Environment:new(env_name, opts.definitions[env_name])
local env = find_env_by_name(get_env(opts.env_variable), opts.definitions)
if env then
return env
end
end

if opts.auto_dicover then
for name, opt in pairs(opts.definitions) do
local env = Environment:new(name, opt)
for _, opt in ipairs(opts.definitions) do
local env = Environment:new(opt)
if env:check() then
return env
end
end
end

if opts.default then
if opts.definitions[opts.default] then
return Environment:new(opts.default, opts.definitions[opts.default])
local env = find_env_by_name(opts.default, opts.definitions)
if env then
return env
end
end

Expand Down

0 comments on commit 2fe1157

Please sign in to comment.