feat: include versioning

This commit is contained in:
Barrett Ruth 2025-09-13 00:39:53 -05:00
parent 6a7ade554e
commit f13a05c806
6 changed files with 60 additions and 29 deletions

View file

@ -75,7 +75,13 @@ local function check_config()
end
function M.check()
local version = require("cp.version")
vim.health.start("cp.nvim health check")
vim.health.info("Version: " .. version.version)
if version.semver then
vim.health.info("Semantic version: v" .. version.semver.full)
end
check_nvim_version()
check_uv()

View file

@ -54,7 +54,7 @@ local function setup_problem(problem_id, problem_letter)
local ctx = problem.create_context(vim.g.cp_contest, problem_id, problem_letter, config)
local scrape_result = scrape.scrape_problem(vim.g.cp_contest, problem_id, problem_letter)
local scrape_result = scrape.scrape_problem(ctx)
if not scrape_result.success then
logger.log("scraping failed: " .. scrape_result.error, vim.log.levels.WARN)

View file

@ -35,23 +35,14 @@ local function setup_python_env()
return true
end
function M.scrape_problem(contest, problem_id, problem_letter)
---@param ctx ProblemContext
function M.scrape_problem(ctx)
ensure_io_directory()
local cache_problem_id = problem_id:lower()
if contest == "atcoder" or contest == "codeforces" then
if problem_letter then
cache_problem_id = cache_problem_id .. problem_letter:lower()
end
end
local input_file = "io/" .. cache_problem_id .. ".in"
local expected_file = "io/" .. cache_problem_id .. ".expected"
if vim.fn.filereadable(input_file) == 1 and vim.fn.filereadable(expected_file) == 1 then
if vim.fn.filereadable(ctx.input_file) == 1 and vim.fn.filereadable(ctx.expected_file) == 1 then
return {
success = true,
problem_id = cache_problem_id,
problem_id = ctx.problem_name,
test_count = 1,
}
end
@ -64,13 +55,13 @@ function M.scrape_problem(contest, problem_id, problem_letter)
end
local plugin_path = get_plugin_path()
local scraper_path = plugin_path .. "/scrapers/" .. contest .. ".py"
local scraper_path = plugin_path .. "/scrapers/" .. ctx.contest .. ".py"
local args
if contest == "cses" then
args = { "uv", "run", scraper_path, problem_id }
if ctx.contest == "cses" then
args = { "uv", "run", scraper_path, ctx.problem_id }
else
args = { "uv", "run", scraper_path, problem_id, problem_letter }
args = { "uv", "run", scraper_path, ctx.problem_id, ctx.problem_letter }
end
local result = vim.system(args, {
@ -98,9 +89,6 @@ function M.scrape_problem(contest, problem_id, problem_letter)
return data
end
local full_problem_id = data.problem_id:lower()
input_file = "io/" .. full_problem_id .. ".in"
expected_file = "io/" .. full_problem_id .. ".expected"
if #data.test_cases > 0 then
local all_inputs = {}
@ -119,13 +107,13 @@ function M.scrape_problem(contest, problem_id, problem_letter)
end
end
vim.fn.writefile(all_inputs, input_file)
vim.fn.writefile(all_outputs, expected_file)
vim.fn.writefile(all_inputs, ctx.input_file)
vim.fn.writefile(all_outputs, ctx.expected_file)
end
return {
success = true,
problem_id = full_problem_id,
problem_id = ctx.problem_name,
test_count = #data.test_cases,
url = data.url,
}

36
lua/cp/version.lua Normal file
View file

@ -0,0 +1,36 @@
local M = {}
local function get_git_version()
local plugin_path = debug.getinfo(1, "S").source:sub(2)
local plugin_root = vim.fn.fnamemodify(plugin_path, ":h:h:h")
local result = vim.system({"git", "describe", "--tags", "--always", "--dirty"}, {
cwd = plugin_root,
text = true
}):wait()
if result.code == 0 then
return result.stdout:gsub("\n", "")
else
return "unknown"
end
end
local function parse_semver(version_string)
local semver = version_string:match("^v?(%d+%.%d+%.%d+)")
if semver then
local major, minor, patch = semver:match("(%d+)%.(%d+)%.(%d+)")
return {
full = semver,
major = tonumber(major),
minor = tonumber(minor),
patch = tonumber(patch)
}
end
return nil
end
M.version = get_git_version()
M.semver = parse_semver(M.version)
return M