From b2b021550921fda5d31967f0fb83b830d6aa5e23 Mon Sep 17 00:00:00 2001 From: Turiiya Date: Mon, 25 Mar 2024 03:45:06 +0100 Subject: [PATCH] add `@VMODHASH` --- vlib/v/checker/checker.v | 13 +++++++++++++ vlib/v/parser/comptime.v | 1 + vlib/v/token/token.v | 5 +++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b1cf460ce62ee4..1e95795d774aca 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3500,6 +3500,19 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type { vmod_file_location := mcache.get_by_file(c.file.path) node.val = os.dir(vmod_file_location.vmod_file) } + .vmod_hash { + mut mcache := vmod.get_cache() + vmod_file_location := mcache.get_by_file(c.file.path) + if vmod_file_location.vmod_file.len == 0 { + c.error('@VMODHASH can only be used in projects that have a v.mod file', + node.pos) + } + hash := version.githash(os.dir(vmod_file_location.vmod_file), false) + if hash == '' { + c.error('failed to find commit hash', node.pos) + } + node.val = hash + } .unknown { c.error('unknown @ identifier: ${node.name}. Available identifiers: ${token.valid_at_tokens}', node.pos) diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 7f3079b6b9e76d..b78218b66ac26d 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -403,6 +403,7 @@ fn (mut p Parser) at() ast.AtExpr { '@VEXE' { token.AtKind.vexe_path } '@VEXEROOT' { token.AtKind.vexeroot_path } '@VMODROOT' { token.AtKind.vmodroot_path } + '@VMODHASH' { token.AtKind.vmod_hash } '@VROOT' { token.AtKind.vroot_path } // deprecated, use @VEXEROOT or @VMODROOT else { token.AtKind.unknown } } diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 9e666578fd59c4..b654aac57c13c5 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -176,6 +176,7 @@ pub enum AtKind { v_current_hash vmod_file vmodroot_path + vmod_hash vroot_path // obsolete vexeroot_path file_path_line_nr @@ -187,8 +188,8 @@ pub const assign_tokens = [Kind.assign, .plus_assign, .minus_assign, .mult_assig .unsigned_right_shift_assign] pub const valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT', - '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@FILE_LINE', - '@LOCATION'] + '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@VMODHASH', + '@FILE_LINE', '@LOCATION'] pub const token_str = build_token_str()