-
Notifications
You must be signed in to change notification settings - Fork 2
/
vxt.v
133 lines (125 loc) · 3.2 KB
/
vxt.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license file distributed with this software package
module vxt
import os
import regex
// vexe returns the path to the `v` compiler if found
// on the host platform, otherwise a blank `string`.
pub fn vexe() string {
mut exe := os.getenv('VEXE')
$if !windows {
if os.is_executable(exe) {
return os.real_path(exe)
}
possible_symlink := os.find_abs_path_of_executable('v') or { '' }
if os.is_executable(possible_symlink) {
return os.real_path(possible_symlink)
}
} $else {
if os.exists(exe) {
return exe
}
system_path := os.find_abs_path_of_executable('v') or { '' }
if os.exists(system_path) {
exe = system_path
}
if !os.exists(exe) {
res := os.execute('where.exe v')
if res.exit_code != 0 {
exe = ''
} else {
return res.output.trim('\n\r')
}
}
}
// Try the compiler that built this
if !os.exists(exe) {
if os.exists(@VEXE) {
return @VEXE
}
}
return exe
}
// vmodules returns the path to the `.vmodules` folder if found
pub fn vmodules() !string {
mut vmodules_path := os.getenv('VMODULES')
if !os.is_dir(vmodules_path) {
vmodules_path = os.join_path(os.home_dir(), '.vmodules')
}
if !os.is_dir(vmodules_path) {
return error('${@MOD}.${@FN}: No valid v modules path found at "${vmodules_path}"')
}
return vmodules_path
}
pub fn found() bool {
return home() != ''
}
pub fn home() string {
// credits to @spytheman:
// https://discord.com/channels/592103645835821068/592294828432424960/746040606358503484
mut exe := vexe()
$if !windows {
if os.is_executable(exe) {
return os.dir(exe)
}
} $else {
if os.exists(exe) {
exe = exe.replace('/', os.path_separator)
// Skip the `.bin\` dir
if os.dir(exe).ends_with('.bin') {
exe = os.dir(exe)
}
return os.dir(exe)
}
}
return ''
}
pub fn version() string {
mut version := ''
v := vexe()
if v != '' {
v_version := os.execute(v + ' -version')
if v_version.exit_code != 0 {
return version
}
output := v_version.output
mut re := regex.regex_opt(r'.*(\d+\.?\d*\.?\d*)') or { panic(err) }
start, _ := re.match_string(output)
if start >= 0 && re.groups.len > 0 {
version = output[re.groups[0]..re.groups[1]]
}
return version
}
return '0.0.0'
}
pub fn version_commit_hash() string {
mut hash := ''
v := vexe()
if v != '' {
v_version := os.execute(v + ' -version')
if v_version.exit_code != 0 {
return ''
}
output := v_version.output
mut re := regex.regex_opt(r'.*\d+\.?\d*\.?\d* ([a-fA-F0-9]{7,})') or { panic(err) }
start, _ := re.match_string(output)
if start >= 0 && re.groups.len > 0 {
hash = output[re.groups[0]..re.groups[1]]
}
return hash
}
return 'deadbeef'
}
// v_mod_path returns the path to the `v.mod` file next to `v_file_or_dir_path` if found, an empty string otherwise.
pub fn v_mod_path(v_file_or_dir_path string) string {
if os.is_dir(v_file_or_dir_path) {
if os.is_file(os.join_path(v_file_or_dir_path, 'v.mod')) {
return os.join_path(v_file_or_dir_path, 'v.mod')
}
} else {
if os.is_file(os.join_path(os.dir(v_file_or_dir_path), 'v.mod')) {
return os.join_path(os.dir(v_file_or_dir_path), 'v.mod')
}
}
return ''
}