Skip to content

Commit

Permalink
feat: rework environment
Browse files Browse the repository at this point in the history
  • Loading branch information
adalessa committed Dec 28, 2023
1 parent 4fd5428 commit a251ca3
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 231 deletions.
29 changes: 28 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Ideas

## To implement
```lua
- [X] implement api
- [X] implement ui
- [X] implements a command history
Expand All @@ -28,4 +27,32 @@
- [X] implement watch
- [X] implement alternate file for livewire components `:A`
- [X] extend api .sync and .async with better respons object. maybe create a new object with method for example `.successful()` and `.failed()`
- [ ] rework environment
- [ ] re-write the readme and translate (not forget fd as dependency) move all info from readme to doc

## Environment
How I want the environment

I have environments, resolver and executables
In environment only have `executables`
I need to get from the configuration per project the executables

I want it to be set on the configuration

So do I need the resolver ?
Is not clear ?
I already have the `environment` term so it's hard to tink into ahother

So I have diferent environments
Executables not found should use try to look for it
```lua
local environments = {
local = {
executables = {
artisan = {"php", "artisan"},
composer = {"composer"},
npm = {"npm"},
}
}
}
```
43 changes: 43 additions & 0 deletions lua/laravel/config/environments.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
return {
env_variable = "NVIM_LARAVEL_ENV",
auto_dicover = true,
default = "local",
definitions = {
["sail"] = {
condition = {
file_exists = { "vendor/bin/sail", "docker-compose.yml" },
},
commands = {
sail = { "vendor/bin/sail" },
{
commands = { "php", "composer", "npm", "yarn" },
prefix = { "vendor/bin/sail" },
},
},
},
["docker-compose"] = {
condition = {
file_exists = { "docker-compose.yml" },
executable = { "docker" },
},
commands = {
compose = { "docker", "compose" },
{
commands = { "php", "composer", "npm" },
docker = {
container = {
env = "APP_SERVICE",
default = "app",
},
exec = { "docker", "compose", "exec", "-it" },
},
},
},
},
["local"] = {
condition = {
executable = { "php" },
},
},
},
}
9 changes: 1 addition & 8 deletions lua/laravel/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ M.defaults = {
["docs"] = { ui = "popup", skip_args = true },
["make:model"] = { options = { "-mf" } },
},
environment = {
resolver = require "laravel.environment.resolver"(true, true, nil),
environments = {
["local"] = require("laravel.environment.native").setup(),
["sail"] = require("laravel.environment.sail").setup(),
["docker-compose"] = require("laravel.environment.docker_compose").setup(),
},
},
environments = require "laravel.config.environments",
resources = require "laravel.config.resources",
}

Expand Down
34 changes: 0 additions & 34 deletions lua/laravel/environment/docker_compose.lua

This file was deleted.

107 changes: 107 additions & 0 deletions lua/laravel/environment/environment.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
local get_env = require("laravel.utils").get_env

---@class Environment
---@field name string
---@field condition table|nil
---@field commands table
local Environment = {}

local cache = {}

---@param env table
---@return Environment
function Environment:new(name, env)
local obj = {
name = name,
condition = env.condition or nil,
commands = env.commands or {},
}

setmetatable(obj, self)
self.__index = self

return obj
end

---@return boolean
function Environment:check()
if not self.condition then
return true
end

for _, file in pairs(self.condition.file_exists or {}) do
if vim.fn.filereadable(file) ~= 1 then
return false
end
end

for _, exec in pairs(self.condition.executable or {}) do
if vim.fn.executable(exec) == 0 then
return false
end
end

return true
end

---@param name string
---@return table|nil
function Environment:executable(name)
if cache[name] then
return cache[name]
end

-- check commands directly by name
if self.commands[name] then
cache[name] = self.commands[name]
return cache[name]
end

for _, value in pairs(self.commands) do
if vim.tbl_contains(value.commands or {}, name) then
-- is on the list have to process it
if value.docker then
-- is set to run from docker
if not value.docker.container then
error(
"Configuration indicates docker but there is no container information, check the configuration",
vim.log.levels.ERROR
)
end

local container = value.docker.container.default
if value.docker.container.env and get_env(value.docker.container.env) then
container = get_env(value.docker.container.env)
end

if not container then
error("Could not resolve container name check the configuration", vim.log.levels.ERROR)
end

if not value.docker.exec then
error("Need to define a docker exec command", vim.log.levels.ERROR)
end

cache[name] = vim.fn.extend(value.docker.exec, { container, name })

return cache[name]
end

if value.prefix then
cache[name] = vim.fn.extend(value.prefix, { name })

return cache[name]
end
end
end

-- if is not define look for the executable in the system
if vim.fn.executable(name) == 1 then
cache[name] = { name }
return { name }
end

return nil
end

return Environment
14 changes: 0 additions & 14 deletions lua/laravel/environment/get_env.lua

This file was deleted.

54 changes: 44 additions & 10 deletions lua/laravel/environment/init.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
local config = require "laravel.config"
local user_commands = require "laravel.user_commands"
local get_env = require("laravel.utils").get_env
local Environment = require "laravel.environment.environment"

local M = {}

M.environment = {}
M.environment = nil

---@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])
end
end

if opts.auto_dicover then
for name, opt in pairs(opts.definitions) do
local env = Environment:new(name, 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])
end
end

return nil
end

function M.setup()
M.environment = {}
M.environment = nil
if vim.fn.filereadable "artisan" == 0 then
return
end

M.environment = config.options.environment.resolver(config.options.environment.environments)
if type(M.environment) == "function" then
M.environment = M.environment()
M.environment = resolve()

if not M.environment then
return
end

user_commands.setup()

if config.options.features.route_info.enable then
require("laravel.route_info").setup()
end
Expand All @@ -29,14 +62,15 @@ end
---@param name string
---@return string[]|nil
function M.get_executable(name)
if vim.tbl_isempty(M.environment) then
if not M.environment then
return nil
end
local executable = M.environment.executables[name]
if executable == nil then
return nil

if name == "artisan" then
return vim.fn.extend(M.environment:executable "php", { "artisan" })
end
return executable

return M.environment:executable(name)
end

return M
24 changes: 0 additions & 24 deletions lua/laravel/environment/native.lua

This file was deleted.

Loading

0 comments on commit a251ca3

Please sign in to comment.