v2 / vlib / v / util / vmod_value.v
20 lines · 18 sloc · 820 bytes · 1957162c2a4545ec88c75f59d5c27c689d974a57
Raw
1// Copyright (c) 2025 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module util
5
6import os
7import v.vmod
8
9// resolve_vmodroot replaces all occurrences of `@VMODROOT` in `str`, with an absolute path,
10// formed by resolving, where the nearest `v.mod` is, given the folder `dir`.
11pub fn resolve_vmodroot(str string, dir string) !string {
12 mut mcache := vmod.get_cache()
13 vmod_file_location := mcache.get_by_folder(dir)
14 if vmod_file_location.vmod_file.len == 0 {
15 // There was no actual v.mod file found.
16 return error('To use @VMODROOT, you need to have a "v.mod" file in ${dir}, or in one of its parent folders.')
17 }
18 vmod_path := vmod_file_location.vmod_folder
19 return str.replace('@VMODROOT', os.real_path(vmod_path))
20}
21