| 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. |
| 4 | module util |
| 5 | |
| 6 | import os |
| 7 | import 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`. |
| 11 | pub 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 | |