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

filepath: path separator (#3756)

This commit is contained in:
yuyi 2020-02-18 03:31:23 +08:00 committed by GitHub
parent 6079025985
commit 6849a4e770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 105 additions and 99 deletions

View File

@ -42,7 +42,7 @@ pub fn line_to_timestamp_and_commit(line string) (int,string) {
pub fn normalized_workpath_for_commit(workdir string, commit string) string {
nc := 'v_at_' + commit.replace('^', '_').replace('-', '_').replace('/', '_')
return os.realpath(workdir + os.path_separator + nc)
return os.realpath(workdir + filepath.separator + nc)
}
pub fn prepare_vc_source(vcdir string, cdir string, commit string) (string,string) {

View File

@ -165,7 +165,7 @@ fn (c Context) compare_v_performance(label string, commands []string) string {
hyperfine_commands_arguments << " \'cd ${c.a:-34s} ; ./$cmd \' ".replace_each(['@COMPILER@', source_location_a, '@DEBUG@', debug_option_a])
}
// /////////////////////////////////////////////////////////////////////////////
cmd_stats_file := os.realpath([c.workdir, 'v_performance_stats_${label}.json'].join(os.path_separator))
cmd_stats_file := os.realpath([c.workdir, 'v_performance_stats_${label}.json'].join(filepath.separator))
comparison_cmd := 'hyperfine $c.hyperfineopts ' + '--export-json ${cmd_stats_file} ' + '--time-unit millisecond ' + '--style full --warmup $c.warmups ' + hyperfine_commands_arguments.join(' ')
// /////////////////////////////////////////////////////////////////////////////
if c.verbose {

View File

@ -177,7 +177,7 @@ fn vpm_install(module_names []string) {
println('Skipping module "$name", since it uses an unsupported VCS {$vcs} .')
continue
}
final_module_path := os.realpath(filepath.join(settings.vmodules_path,mod.name.replace('.', os.path_separator)))
final_module_path := os.realpath(filepath.join(settings.vmodules_path,mod.name.replace('.', filepath.separator)))
if os.exists(final_module_path) {
vpm_update([name])
continue
@ -287,7 +287,7 @@ fn vpm_remove(module_names []string) {
}
fn valid_final_path_of_existing_module(name string) ?string {
name_of_vmodules_folder := filepath.join(settings.vmodules_path,name.replace('.', os.path_separator))
name_of_vmodules_folder := filepath.join(settings.vmodules_path,name.replace('.', filepath.separator))
final_module_path := os.realpath(name_of_vmodules_folder)
if !os.exists(final_module_path) {
println('No module with name "$name" exists at $name_of_vmodules_folder')

View File

@ -3,6 +3,7 @@ module main
import (
os
os.cmdline
filepath
testing
)
@ -39,7 +40,7 @@ pub fn main() {
}
if os.is_dir(targ) {
// Fetch all tests from the directory
ts.files << os.walk_ext( targ.trim_right(os.path_separator), '_test.v')
ts.files << os.walk_ext( targ.trim_right(filepath.separator), '_test.v')
continue
}
println('Unrecognized test file $targ .')

View File

@ -20,7 +20,7 @@ pub fn new_v(args []string) &compiler.V {
os.mkdir(compiler.v_modules_path)or{
panic(err)
}
os.mkdir('$compiler.v_modules_path${os.path_separator}cache')or{
os.mkdir('$compiler.v_modules_path${filepath.separator}cache')or{
panic(err)
}
}
@ -47,10 +47,10 @@ pub fn new_v(args []string) &compiler.V {
println('use `v -o v cmd/v` instead of `v -o v v.v`')
exit(1)
}
if dir.ends_with(os.path_separator) {
dir = dir.all_before_last(os.path_separator)
if dir.ends_with(filepath.separator) {
dir = dir.all_before_last(filepath.separator)
}
if dir.starts_with('.$os.path_separator') {
if dir.starts_with('.$filepath.separator') {
dir = dir[2..]
}
if args.len < 2 {
@ -65,8 +65,8 @@ pub fn new_v(args []string) &compiler.V {
build_mode = .build_module
os.chdir(vroot)
// v build module ~/v/os => os.o
mod_path := if dir.contains('vlib') { dir.all_after('vlib' + os.path_separator) } else if dir.starts_with('.\\') || dir.starts_with('./') { dir[2..] } else if dir.starts_with(os.path_separator) { dir.all_after(os.path_separator) } else { dir }
mod = mod_path.replace(os.path_separator, '.')
mod_path := if dir.contains('vlib') { dir.all_after('vlib' + filepath.separator) } else if dir.starts_with('.\\') || dir.starts_with('./') { dir[2..] } else if dir.starts_with(filepath.separator) { dir.all_after(filepath.separator) } else { dir }
mod = mod_path.replace(filepath.separator, '.')
println('Building module "${mod}" (dir="$dir")...')
// out_name = '$TmpPath/vlib/${base}.o'
if !out_name.ends_with('.c') {
@ -84,8 +84,8 @@ pub fn new_v(args []string) &compiler.V {
}
// `v -o dir/exec`, create "dir/" if it doesn't exist
if out_name.contains(os.path_separator) {
d := out_name.all_before_last(os.path_separator)
if out_name.contains(filepath.separator) {
d := out_name.all_before_last(filepath.separator)
if !os.is_dir(d) {
println('creating a new directory "$d"')
os.mkdir(d)or{
@ -168,7 +168,7 @@ pub fn new_v(args []string) &compiler.V {
pref.fill_with_defaults()
// v.exe's parent directory should contain vlib
if !os.is_dir(pref.vlib_path) || !os.is_dir(pref.vlib_path + os.path_separator + 'builtin') {
if !os.is_dir(pref.vlib_path) || !os.is_dir(pref.vlib_path + filepath.separator + 'builtin') {
// println('vlib not found, downloading it...')
/*
ret := os.system('git clone --depth=1 https://github.com/vlang/v .')

View File

@ -182,7 +182,7 @@ fn (v mut V) new_parser_from_file(path string) Parser {
p |
file_path:path,
file_path_dir:filepath.dir( path ),
file_name:path.all_after(os.path_separator),
file_name:path.all_after(filepath.separator),
file_platform:path_platform,
file_pcguard:path_pcguard,
is_vh:path.ends_with('.vh'),

View File

@ -152,8 +152,8 @@ fn (v mut V) cc() {
}
if v.pref.build_mode == .build_module {
// Create the modules & out directory if it's not there.
mut out_dir := if v.pref.path.starts_with('vlib') { '$v_modules_path${os.path_separator}cache${os.path_separator}$v.pref.path' } else { '$v_modules_path${os.path_separator}$v.pref.path' }
pdir := out_dir.all_before_last(os.path_separator)
mut out_dir := if v.pref.path.starts_with('vlib') { '$v_modules_path${filepath.separator}cache${filepath.separator}$v.pref.path' } else { '$v_modules_path${filepath.separator}$v.pref.path' }
pdir := out_dir.all_before_last(filepath.separator)
if !os.is_dir(pdir) {
os.mkdir_all(pdir)
}
@ -223,7 +223,7 @@ fn (v mut V) cc() {
}
else {
println('$builtin_o_path not found... building module builtin')
os.system('$vexe build module vlib${os.path_separator}builtin')
os.system('$vexe build module vlib${filepath.separator}builtin')
}
for imp in v.table.imports {
if imp.contains('vweb') {
@ -232,8 +232,8 @@ fn (v mut V) cc() {
if imp == 'webview' {
continue
}
imp_path := imp.replace('.', os.path_separator)
path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}${imp_path}.o'
imp_path := imp.replace('.', filepath.separator)
path := '$v_modules_path${filepath.separator}cache${filepath.separator}vlib${filepath.separator}${imp_path}.o'
// println('adding ${imp_path}.o')
if os.exists(path) {
libs += ' ' + path
@ -250,7 +250,7 @@ fn (v mut V) cc() {
}
}
else {
os.system('$vexe build module vlib${os.path_separator}$imp_path')
os.system('$vexe build module vlib${filepath.separator}$imp_path')
}
}
if path.ends_with('vlib/ui.o') {
@ -554,4 +554,3 @@ fn missing_compiler_info() string {
}
return ''
}

View File

@ -285,7 +285,7 @@ fn (v &V) build_thirdparty_obj_file(path string, moduleflags []CFlag) {
mut cfiles := ''
for file in files {
if file.ends_with('.c') {
cfiles += '"' + os.realpath(parent + os.path_separator + file) + '" '
cfiles += '"' + os.realpath(parent + filepath.separator + file) + '" '
}
}
btarget := moduleflags.c_options_before_target()
@ -503,4 +503,3 @@ fn (v &V) interface_table() string {
}
return sb.str()
}

View File

@ -2,6 +2,7 @@ module compiler
import (
os
filepath
term
)
// ////////////////////////////////////////////////////////////////////////////////////////////////
@ -156,7 +157,7 @@ fn (s &Scanner) get_error_filepath() string {
else {
s.print_rel_paths_on_error}}
if use_relative_paths {
workdir := os.getwd() + os.path_separator
workdir := os.getwd() + filepath.separator
if s.file_path.starts_with(workdir) {
return s.file_path.replace(workdir, '')
}
@ -315,4 +316,3 @@ const (
and_or_error = 'use `()` to make the boolean expression clear\n' + 'for example: `(a && b) || c` instead of `a && b || c`'
err_modify_bitfield = 'to modify a bitfield flag use the methods: set, clear, toggle. and to check for flag use: has'
)

View File

@ -681,8 +681,8 @@ pub fn (v mut V) add_v_files_to_compile() {
}
// use cached built module if exists
if v.pref.vpath != '' && v.pref.build_mode != .build_module && !mod.contains('vweb') {
mod_path := mod.replace('.', os.path_separator)
vh_path := '$v_modules_path${os.path_separator}vlib${os.path_separator}${mod_path}.vh'
mod_path := mod.replace('.', filepath.separator)
vh_path := '$v_modules_path${filepath.separator}vlib${filepath.separator}${mod_path}.vh'
if v.pref.is_cache && os.exists(vh_path) {
eprintln('using cached module `$mod`: $vh_path')
v.cached_mods << mod
@ -924,5 +924,5 @@ pub fn set_vroot_folder(vroot_path string) {
// VEXE env variable is needed so that compiler.vexe_path()
// can return it later to whoever needs it:
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
os.setenv('VEXE', os.realpath([vroot_path, vname].join(os.path_separator)), true)
os.setenv('VEXE', os.realpath([vroot_path, vname].join(filepath.separator)), true)
}

View File

@ -30,9 +30,9 @@ fn generate_vh(mod string) {
println('\n\n\n\nGenerating a V header file for module `$mod`')
vexe := vexe_path()
full_mod_path := filepath.join(filepath.dir(vexe),mod)
dir := if mod.starts_with('vlib') { '$compiler.v_modules_path${os.path_separator}$mod' } else { mod }
dir := if mod.starts_with('vlib') { '$compiler.v_modules_path${filepath.separator}$mod' } else { mod }
path := dir + '.vh'
pdir := dir.all_before_last(os.path_separator)
pdir := dir.all_before_last(filepath.separator)
if !os.is_dir(pdir) {
os.mkdir_all(pdir)
// os.mkdir(os.realpath(dir)) or { panic(err) }
@ -50,7 +50,7 @@ fn generate_vh(mod string) {
// mut vfiles := os.ls(full_mod_path) or {
// exit(1)
// }
filtered := vfiles.filter(it.ends_with('.v') && !it.ends_with('test.v') && !it.ends_with('_windows.v') && !it.ends_with('_win.v') && !it.ends_with('_lin.v') && !it.contains('${os.path_separator}examples') && !it.contains('_js.v') && !it.contains('_bare.v') && !it.contains('${os.path_separator}js')) // TODO merge once filter allows it
filtered := vfiles.filter(it.ends_with('.v') && !it.ends_with('test.v') && !it.ends_with('_windows.v') && !it.ends_with('_win.v') && !it.ends_with('_lin.v') && !it.contains('${filepath.separator}examples') && !it.contains('_js.v') && !it.contains('_bare.v') && !it.contains('${filepath.separator}js')) // TODO merge once filter allows it
// println('f:')
// println(filtered)
mut pref := &pref.Preferences {

View File

@ -25,7 +25,7 @@ fn (table &Table) qualify_module(mod string, file_path string) string {
for m in table.imports {
if m.contains('.') && m.contains(mod) {
m_parts := m.split('.')
m_path := m_parts.join(os.path_separator)
m_path := m_parts.join(filepath.separator)
if mod == m_parts[m_parts.len - 1] && file_path.contains(m_path) {
return m
}
@ -147,7 +147,7 @@ pub fn (graph &DepGraph) imports() []string {
[inline]
fn (v &V) module_path(mod string) string {
// submodule support
return mod.replace('.', os.path_separator)
return mod.replace('.', filepath.separator)
}
// 'strings' => 'VROOT/vlib/strings'
@ -208,4 +208,3 @@ fn mod_gen_name(mod string) string {
fn mod_gen_name_rev(mod string) string {
return mod.replace('_dot_', '.')
}

View File

@ -163,7 +163,7 @@ fn find_msvc() ?MsvcResult {
return error('Unable to find visual studio')
}
return MsvcResult{
full_cl_exe_path: os.realpath(vs.exe_path + os.path_separator + 'cl.exe')
full_cl_exe_path: os.realpath(vs.exe_path + filepath.separator + 'cl.exe')
exe_path: vs.exe_path
um_lib_path: wk.um_lib_path
ucrt_lib_path: wk.ucrt_lib_path
@ -321,7 +321,7 @@ fn build_thirdparty_obj_file_with_msvc(path string, moduleflags []CFlag) {
mut cfiles := ''
for file in files {
if file.ends_with('.c') {
cfiles += '"' + os.realpath(parent + os.path_separator + file) + '" '
cfiles += '"' + os.realpath(parent + filepath.separator + file) + '" '
}
}
include_string := '-I "$msvc.ucrt_include_path" -I "$msvc.vs_include_path" -I "$msvc.um_include_path" -I "$msvc.shared_include_path"'
@ -376,7 +376,7 @@ fn (cflags []CFlag) msvc_string_flags() MsvcStringFlags {
}
else if flag.name == '-L' {
lib_paths << flag.value
lib_paths << flag.value + os.path_separator + 'msvc'
lib_paths << flag.value + filepath.separator + 'msvc'
// The above allows putting msvc specific .lib files in a subfolder msvc/ ,
// where gcc will NOT find them, but cl will do...
// NB: gcc is smart enough to not need .lib files at all in most cases, the .dll is enough.
@ -398,4 +398,3 @@ fn (cflags []CFlag) msvc_string_flags() MsvcStringFlags {
return MsvcStringFlags{
real_libs,inc_paths,lpaths,other_flags}
}

View File

@ -5,6 +5,7 @@ module compiler
import (
os
filepath
// strings
)
@ -850,7 +851,7 @@ fn (s mut Scanner) debug_tokens() {
s.pos = 0
s.started = false
s.debug = true
fname := s.file_path.all_after(os.path_separator)
fname := s.file_path.all_after(filepath.separator)
println('\n===DEBUG TOKENS $fname===')
for {
res := s.scan()
@ -941,4 +942,3 @@ fn (s &Scanner) validate_var_name(name string) {
s.error('bad variable name `$name`\n' + 'looks like you have a multi-word name without separating them with `_`' + '\nfor example, use `registration_date` instead of `registrationdate` ')
}
}

View File

@ -73,7 +73,7 @@ pub fn run_repl_file(wd string, vexec string, file string) ?string {
.replace('>>>', '')
.replace('... ', '')
.all_after('Use Ctrl-C or `exit` to exit\n')
.replace(wd + os.path_separator, '' )
.replace(wd + filepath.separator, '' )
if result != output {
file_result := '${file}.result.txt'
@ -160,4 +160,3 @@ pub fn new_prod_options() RunnerOptions {
files: files
}
}

View File

@ -24,12 +24,12 @@ pub fn join(base string, dirs ...string) string {
for d in dirs {
result << d
}
return result.join(path_separator)
return result.join(separator)
}
// dir returns all but the last element of path, typically the path's directory.
pub fn dir(path string) string {
pos := path.last_index(path_separator) or {
pos := path.last_index(separator) or {
return '.'
}
return path[..pos]
@ -37,7 +37,7 @@ pub fn dir(path string) string {
// basedir returns a directory name from path
pub fn basedir(path string) string {
pos := path.last_index(path_separator) or {
pos := path.last_index(separator) or {
return path
}
// NB: *without* terminating /
@ -46,5 +46,5 @@ pub fn basedir(path string) string {
// filename returns a file name from path
pub fn filename(path string) string {
return path.all_after(path_separator)
return path.all_after(separator)
}

View File

@ -1,6 +1,5 @@
module filepath
const (
path_separator = '/'
pub const (
separator = '/'
)

View File

@ -1,6 +1,5 @@
module filepath
const (
path_separator = '\\'
pub const (
separator = '\\'
)

View File

@ -797,7 +797,7 @@ pub fn home_dir() string {
}
home += homepath
}
home += path_separator
home += filepath.separator
return home
}
@ -1000,7 +1000,7 @@ pub fn walk_ext(path, ext string) []string {
panic(err)
}
mut res := []string
separator := if path.ends_with(path_separator) { '' } else { path_separator }
separator := if path.ends_with(filepath.separator) { '' } else { filepath.separator }
for i, file in files {
if file.starts_with('.') {
continue
@ -1026,7 +1026,7 @@ pub fn walk(path string, f fn(path string)) {
panic(err)
}
for file in files {
p := path + os.path_separator + file
p := path + filepath.separator + file
if os.is_dir(p) && !os.is_link(p) {
walk(p, f)
}
@ -1087,9 +1087,9 @@ pub fn flush_stdout() {
}
pub fn mkdir_all(path string) {
mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' }
for subdir in path.split(os.path_separator) {
p += subdir + os.path_separator
mut p := if path.starts_with(filepath.separator) { filepath.separator } else { '' }
for subdir in path.split(filepath.separator) {
p += subdir + filepath.separator
if !os.is_dir(p) {
os.mkdir(p) or {
panic(err)

View File

@ -7,6 +7,10 @@ import strings
#include <fcntl.h>
pub const (
/**
* This constant is deprecated. Use `filepath.separator` instead.
* FIXME Remove this separator, as it a part of `filepath` module.
*/
path_separator = '/'
)

View File

@ -1,4 +1,5 @@
import os
import filepath
fn test_aaa_setup(){
cleanup_leftovers()
@ -116,14 +117,14 @@ fn walk_callback(file string) {
if file == '.' || file == '..' {
return
}
assert file == 'test_walk'+os.path_separator+'test1'
assert file == 'test_walk' + filepath.separator + 'test1'
}
fn test_walk() {
folder := 'test_walk'
os.mkdir(folder) or { panic(err) }
file1 := folder+os.path_separator+'test1'
file1 := folder + filepath.separator + 'test1'
os.write_file(file1,'test-1')
@ -172,7 +173,7 @@ fn test_tmpdir(){
assert t.len > 0
assert os.is_dir(t)
tfile := t + os.path_separator + 'tmpfile.txt'
tfile := t + filepath.separator + 'tmpfile.txt'
os.rm(tfile) // just in case
@ -256,7 +257,7 @@ fn test_symlink() {
}
fn test_is_executable_writable_readable() {
file_name := os.tmpdir() + os.path_separator + 'rwxfile.exe'
file_name := os.tmpdir() + filepath.separator + 'rwxfile.exe'
mut f := os.create(file_name) or {
eprintln('failed to create file $file_name')

View File

@ -6,6 +6,10 @@ import strings
#include <winsock2.h>
pub const (
/**
* This constant is deprecated. Use `filepath.separator` instead.
* FIXME Remove this separator, as it a part of `filepath` module.
*/
path_separator = '\\'
)

View File

@ -8,7 +8,7 @@ import (
[inline]
fn module_path(mod string) string {
// submodule support
return mod.replace('.', os.path_separator)
return mod.replace('.', filepath.separator)
}
fn (b &Builder) find_module_path(mod string) ?string {

View File

@ -8,6 +8,7 @@ import (
v.table
v.token
os
filepath
)
pub struct Checker {
@ -558,7 +559,7 @@ pub fn (c &Checker) error(s string, pos token.Position) {
print_backtrace()
mut path := c.file_name
// Get relative path
workdir := os.getwd() + os.path_separator
workdir := os.getwd() + filepath.separator
if path.starts_with(workdir) {
path = path.replace(workdir, '')
}

View File

@ -11,6 +11,7 @@ import (
v.pref
term
os
filepath
)
const (
@ -392,7 +393,7 @@ pub fn (p &Parser) error(s string) {
print_backtrace()
mut path := p.file_name
// Get relative path
workdir := os.getwd() + os.path_separator
workdir := os.getwd() + filepath.separator
if path.starts_with(workdir) {
path = path.replace(workdir, '')
}

View File

@ -5,6 +5,7 @@ module scanner
import (
os
filepath
v.token
// strings
)
@ -838,7 +839,7 @@ fn (s mut Scanner) debug_tokens() {
s.pos = 0
s.started = false
s.debug = true
fname := s.file_path.all_after(os.path_separator)
fname := s.file_path.all_after(filepath.separator)
println('\n===DEBUG TOKENS $fname===')
for {
tok := s.scan()