Initial commit
This commit is contained in:
124
lua/cave/task.lua
Normal file
124
lua/cave/task.lua
Normal file
@@ -0,0 +1,124 @@
|
||||
local Path = require "cave.path"
|
||||
local Meta = require "cave.meta"
|
||||
local Enum = require "cave.enum"
|
||||
|
||||
local List = Meta.List
|
||||
local Map = Meta.Map
|
||||
local Optional = Meta.Optional
|
||||
local Str = Meta.String
|
||||
local validate = Meta.validate
|
||||
|
||||
---@enum cave.Task.Tag
|
||||
local Tag = {
|
||||
Run = "Run",
|
||||
Debug = "Debug",
|
||||
}
|
||||
Enum.new(Tag, "Task.Tag")
|
||||
|
||||
---@class cave.Task
|
||||
---@field id string
|
||||
---@field args string[]
|
||||
---@field cwd cave.Path
|
||||
---@field env table<string, string>
|
||||
local Task = Meta.derive "Task"
|
||||
|
||||
Task.Tag = Tag
|
||||
|
||||
---@return string
|
||||
function Task:valid_cwd()
|
||||
assert(self.cwd:is_dir())
|
||||
return self.cwd:tostring()
|
||||
end
|
||||
|
||||
---@class cave.Task.Factory
|
||||
---@field id_ string?
|
||||
---@field args_ string[]
|
||||
---@field cwd_ cave.Path?
|
||||
---@field env_ table<string, string>
|
||||
---@field context_ cave.Context
|
||||
local Factory = Meta.derive "Task.Factory"
|
||||
|
||||
---@param context cave.Context
|
||||
function Factory:init(context)
|
||||
self.args_ = {}
|
||||
self.env_ = {}
|
||||
self.context_ = context
|
||||
end
|
||||
|
||||
---@param other cave.Task.Factory
|
||||
function Factory:copy_from(other)
|
||||
self.id_ = other.id_
|
||||
self.args_ = vim.deepcopy(other.args_)
|
||||
self.cwd_ = other.cwd_ and other.cwd_:copy()
|
||||
self.env_ = vim.deepcopy(other.env_)
|
||||
self.context_ = other.context_
|
||||
end
|
||||
|
||||
---@return string
|
||||
function Factory:get_id()
|
||||
assert(self.id_)
|
||||
return self.id_
|
||||
end
|
||||
|
||||
---@return string[]
|
||||
function Factory:get_args() return self.args_ end
|
||||
|
||||
---@return cave.Path
|
||||
function Factory:get_cwd() return self.cwd_ or Path.new(self.context_.dir) end
|
||||
|
||||
---@return table<string, string>
|
||||
function Factory:get_env() return self.env_ end
|
||||
|
||||
---@param id string
|
||||
---@return cave.Task.Factory
|
||||
function Factory:id(id)
|
||||
validate { id = { id, Str } }
|
||||
self.id_ = id
|
||||
return self
|
||||
end
|
||||
|
||||
---@param args string[]
|
||||
---@return cave.Task.Factory
|
||||
function Factory:args(args)
|
||||
validate { args = { args, List(Str) } }
|
||||
vim.list_extend(self.args_, args)
|
||||
return self
|
||||
end
|
||||
|
||||
---@param arg string
|
||||
---@return cave.Task.Factory
|
||||
function Factory:arg(arg) return self:args { arg } end
|
||||
|
||||
---@param cwd_path_like cave.PathLike
|
||||
---@return cave.Task.Factory
|
||||
function Factory:cwd(cwd_path_like)
|
||||
validate { cwd_path_like = { cwd_path_like, Optional(Path.Like) } }
|
||||
self.cwd_ = Path.like(cwd_path_like)
|
||||
return self
|
||||
end
|
||||
|
||||
---@param env table<string, string>
|
||||
---@return cave.Task.Factory
|
||||
function Factory:env(env)
|
||||
validate { env = { env, Map(Str, Str) } }
|
||||
vim.tbl_extend("error", self.env_, env)
|
||||
return self
|
||||
end
|
||||
|
||||
---@param task cave.Task
|
||||
function Factory:init_task(task)
|
||||
task.id = self:get_id()
|
||||
task.args = self:get_args()
|
||||
task.cwd = self:get_cwd()
|
||||
task.env = self:get_env()
|
||||
end
|
||||
|
||||
---@return cave.Task
|
||||
function Factory:build() error "Not implemented" end
|
||||
|
||||
---@return cave.Task
|
||||
function Factory:copy() error "Not implemented" end
|
||||
|
||||
Task.Factory = Factory
|
||||
|
||||
return Task
|
||||
Reference in New Issue
Block a user