Skip to content

Commit

Permalink
add @VMODHASH
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 25, 2024
1 parent 7fb7a48 commit b2b0215
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
5 changes: 3 additions & 2 deletions vlib/v/token/token.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down

0 comments on commit b2b0215

Please sign in to comment.