Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Feb 8, 2025
1 parent 080a866 commit 8c21502
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
Empty file.
7 changes: 4 additions & 3 deletions lua/fittencode/zipflow/engines/cli/gzip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ local M = {
name = 'gzip',
category = 'cli',
priority = 80,
features = {
async = true,
performance = 0.6
performance = {
speed = 0.6,
compression_ratio = 0.5
},
async = true,
capabilities = {
compress = {
format = { 'gzip', 'gz' },
Expand Down
20 changes: 11 additions & 9 deletions lua/fittencode/zipflow/engines/cli/tar.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
local spawn = require('zipflow.process.spawn')
local Spawn = require('fittencode.process.spawn')
local Promise = require('fittencode.concurrency.promise')
local uv = vim.uv or vim.loop

local M = {
name = 'tar',
type = 'cli',
priority = 80,
performance = {
speed = 0.6,
compression_ratio = 0.5
},
async = true,
capabilities = {
compress = {
input_types = { 'directory' },
output_types = { 'file' },
formats = { 'tar' },
compression = { 'none', 'gzip', 'bzip2', 'xz' }
formats = { 'tar', 'tar.gz', 'tar.bz2', 'tar.xz' },
},
decompress = {
input_types = { 'file' },
output_types = { 'directory' },
formats = { 'tar', 'tar.gz', 'tar.bz2', 'tar.xz' }
}
}
Expand Down Expand Up @@ -43,7 +45,7 @@ function M.compress(input_dir, opts)
'.'
}

local p = spawn('tar', args)
local p = Spawn.spawn('tar', args)

p:on('exit', function(code)
if code == 0 then
Expand Down Expand Up @@ -77,9 +79,9 @@ function M.decompress(input_file, opts)
'-C', output_dir
}

uv.fs_mkdir(output_dir, 493) -- 0755 in decimal
vim.uv.fs_mkdir(output_dir, 493) -- 0755 in decimal

local p = spawn('tar', args)
local p = Spawn.spawn('tar', args)

p:on('exit', function(code)
if code == 0 then
Expand Down
6 changes: 1 addition & 5 deletions lua/fittencode/zipflow/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ local function process(op_type, input, opts)
if not engine then return reject(_) end

-- 执行操作
local processor = op_type == 'compress'
and engine.compress
or engine.decompress

processor(input, opts)
engine[opts.operation](input, opts)
:forward(resolve)
:catch(reject)
end)
Expand Down
33 changes: 32 additions & 1 deletion lua/fittencode/zipflow/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,43 @@ local scorer = require('zipflow.scorer')

local M = {}

local function is_supported(engine, opts)
if not engine then
-- error("Engine is not provided")
return false
end

if not engine.capabilities then
-- error("Engine does not have capabilities")
return false
end

local capabilities = engine.capabilities[opts.operation]
if not capabilities then
return false
end

local input_types = capabilities.input_types
local formats = capabilities.formats

if not input_types or not formats then
-- error(("Engine capabilities for %s operation are incomplete"):format(opts.operation))
return false
end

local match_input_type = vim.tbl_contains(input_types, '*') or vim.tbl_contains(input_types, opts.input_type)
local match_format = vim.tbl_contains(formats, '*') or vim.tbl_contains(formats, opts.format)

-- 解压可以不用匹配格式
return match_input_type and (opts.operation == 'compress' or (opts.operation == 'decompress' and match_format))
end

function M.select_engine(opts)
local candidates = {}

-- 获取所有支持当前操作的引擎
for _, engine in ipairs(registry.list_engines()) do
if engine:supports(opts.operation, opts.format) then
if is_supported(engine, opts) then
table.insert(candidates, engine)
end
end
Expand Down
6 changes: 5 additions & 1 deletion lua/fittencode/zipflow/scorer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ function M.calculate(engine, opts)
score = score + engine.priority * 100

-- 参数匹配度
score = score + #engine:match_params(opts) * 10
if engine['match_params'] then
score = score + #engine:match_params(opts) * 10
end

-- 资源偏好
if opts.prefer == 'speed' then
score = score + engine.performance.speed * 2
elseif opts.prefer == 'compression_ratio' then
score = score + engine.performance.compression_ratio * 2
end

return score
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions lua/fittencode/zipflow/validator.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
local M = {}

local function validate_common(opts)
-- 操作类型检查
if not opts.operation then
return false, 'Missing required parameter: operation'
elseif not vim.tbl_contains({ 'compress', 'decompress' }, opts.operation) then
return false, 'Invalid operation: ' .. opts.operation
end

-- 必须参数检查
if not opts.input then
return false, 'Missing required parameter: input'
Expand All @@ -21,6 +28,8 @@ local function validate_common(opts)
return false, 'Output path already exists: ' .. opts.output_path
end
end

return true, nil
end

local function validate_compress(opts)
Expand Down

0 comments on commit 8c21502

Please sign in to comment.