1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: remove base_dir(); dir() and base() fixes

This commit is contained in:
Alexander Medvednikov 2020-10-01 01:25:52 +02:00
parent 324d547cdb
commit 4879661f5a
4 changed files with 44 additions and 21 deletions

View File

@ -642,21 +642,46 @@ pub fn file_ext(path string) string {
return path[pos..]
}
// dir will return the part of `path` before the last occurence of a `path_separator`.
// dir returns all but the last element of path, typically the path's directory.
// After dropping the final element, trailing slashes are removed.
// If the path is empty, dir returns ".". If the path consists entirely of separators,
// dir returns a single separator.
// The returned path does not end in a separator unless it is the root directory.
pub fn dir(path string) string {
pos := path.last_index(path_separator) or {
if path == '' {
return '.'
}
mut pos := path.last_index(path_separator) or {
return '.'
}
if path.ends_with(path_separator) {
pos--
}
return path[..pos]
}
// base_dir will return the base directory of `path`.
pub fn base_dir(path string) string {
posx := path.last_index(path_separator) or {
// base returns the last element of path.
// Trailing path separators are removed before extracting the last element.
// If the path is empty, base returns ".". If the path consists entirely of separators, base returns a
// single separator.
pub fn base(path string) string {
if path == '' {
return '.'
}
if path == path_separator {
return path_separator
}
if path.ends_with(path_separator) {
path2 := path[..path.len-1]
pos := path2.last_index(path_separator) or {
return path2.clone()
}
return path2[pos+1..]
}
pos := path.last_index(path_separator) or {
return path.clone()
}
// NB: *without* terminating /
return path[..posx]
return path[pos+1..]
}
// file_name will return all characters found after the last occurence of `path_separator`.

View File

@ -446,13 +446,15 @@ fn test_dir() {
assert os.dir('os') == '.'
}
fn test_basedir() {
fn test_base() {
$if windows {
assert os.base_dir('v\\vlib\\os') == 'v\\vlib'
assert os.base('v\\vlib\\os') == 'os'
assert os.base('v\\vlib\\os\\') == 'os'
} $else {
assert os.base_dir('v/vlib/os') == 'v/vlib'
assert os.base('v/vlib/os') == 'os'
assert os.base('v/vlib/os/') == 'os'
}
assert os.base_dir('filename') == 'filename'
assert os.base('filename') == 'filename'
}
fn test_uname() {

View File

@ -27,7 +27,7 @@ fn get_vtmp_filename(base_file_name, postfix string) string {
}
pub fn compile(command string, pref &pref.Preferences) {
odir := os.base_dir(pref.out_name)
odir := os.dir(pref.out_name)
// When pref.out_name is just the name of an executable, i.e. `./v -o executable main.v`
// without a folder component, just use the current folder instead:
mut output_folder := odir
@ -55,9 +55,7 @@ pub fn compile(command string, pref &pref.Preferences) {
println('compilation took: ${util.bold(sw.elapsed().milliseconds().str())} ms')
}
// running does not require the parsers anymore
unsafe {
b.myfree()
}
unsafe {b.myfree()}
if pref.is_test || pref.is_run {
b.run_compiled_executable_and_exit()
}
@ -68,9 +66,7 @@ pub fn compile(command string, pref &pref.Preferences) {
fn (mut b Builder) myfree() {
// for file in b.parsed_files {
// }
unsafe {
b.parsed_files.free()
}
unsafe {b.parsed_files.free()}
}
fn (mut b Builder) run_compiled_executable_and_exit() {
@ -142,7 +138,7 @@ fn (mut v Builder) set_module_lookup_paths() {
// 3.2) search in ~/.vmodules/ (i.e. modules installed with vpm)
v.module_search_paths = []
if v.pref.is_test {
v.module_search_paths << os.base_dir(v.compiled_dir) // pdir of _test.v
v.module_search_paths << os.dir(v.compiled_dir) // pdir of _test.v
}
v.module_search_paths << v.compiled_dir
x := os.join_path(v.compiled_dir, 'modules')
@ -263,7 +259,7 @@ pub fn (v &Builder) get_user_files() []string {
v.log('> That brings in all other ordinary .v files in the same module too .')
}
user_files << single_test_v_file
dir = os.base_dir(single_test_v_file)
dir = os.dir(single_test_v_file)
}
does_exist := os.exists(dir)
if !does_exist {

View File

@ -109,7 +109,7 @@ fn (mut mcache ModFileCacher) traverse(mfolder string) ([]string, ModFileAndFold
if mcache.check_for_stop(cfolder, files) {
break
}
cfolder = os.base_dir(cfolder)
cfolder = os.dir(cfolder)
folders_so_far << cfolder
levels++
}