os: replace dir_exists with is_dir; file_exists() => exists()

pull/2978/head
Alexander Medvednikov 2019-12-04 23:03:12 +03:00
parent fb237b9e53
commit a57e29dfc5
28 changed files with 116 additions and 98 deletions

View File

@ -9,7 +9,7 @@ fn main() {
exe := os.executable()
dir := os.dir(exe)
vdir := os.dir(os.dir(dir))
if !os.file_exists('$vdir/v') && !os.dir_exists('$vdir/vlib') {
if !os.exists('$vdir/v') && !os.is_dir('$vdir/vlib') {
println('fast.html generator needs to be located in `v/tools/fast/`')
}
println('fast.html generator\n')
@ -18,7 +18,7 @@ fn main() {
exec('git pull --rebase')
mut commit_hash := exec('git rev-parse HEAD')
commit_hash = commit_hash[..7]
if !os.file_exists('table.html') {
if !os.exists('table.html') {
os.create('table.html') or { panic(err) }
}
mut table := os.read_file('table.html') or { panic(err) }

View File

@ -114,18 +114,18 @@ fn main() {
fp.version(app_version)
fp.description(app_description)
fp.skip_executable()
show_help:=fp.bool('help', false, 'Show this help screen\n')
flag_options := parse_flags(mut fp)
if( show_help ){ println( fp.usage() ) exit(0) }
fp.finalize() or {
eprintln(err)
println(fp.usage())
return
}
// webhook server mode
if flag_options.serve {
app := WebhookServer{ gen_vc: new_gen_vc(flag_options) }
@ -198,11 +198,11 @@ fn (gen_vc mut GenVC) generate() {
gen_vc.gen_error = false
// check if gen_vc dir exists
if !os.dir_exists(gen_vc.options.work_dir) {
if !os.is_dir(gen_vc.options.work_dir) {
// try create
os.mkdir(gen_vc.options.work_dir) or { panic(err) }
// still dosen't exist... we have a problem
if !os.dir_exists(gen_vc.options.work_dir) {
if !os.is_dir(gen_vc.options.work_dir) {
gen_vc.logger.error('error creating directory: $gen_vc.options.work_dir')
gen_vc.gen_error = true
return
@ -211,12 +211,12 @@ fn (gen_vc mut GenVC) generate() {
// cd to gen_vc dir
os.chdir(gen_vc.options.work_dir)
// if we are not running with the --serve flag (webhook server)
// rather than deleting and re-downloading the repo each time
// first check to see if the local v repo is behind master
// if it isn't behind theres no point continuing further
if !gen_vc.options.serve && os.dir_exists(git_repo_dir_v) {
if !gen_vc.options.serve && os.is_dir(git_repo_dir_v) {
gen_vc.cmd_exec('git -C $git_repo_dir_v checkout master')
// fetch the remote repo just in case there are newer commits there
gen_vc.cmd_exec('git -C $git_repo_dir_v fetch')
@ -229,11 +229,11 @@ fn (gen_vc mut GenVC) generate() {
// delete repos
gen_vc.purge_repos()
// clone repos
gen_vc.cmd_exec('git clone --depth 1 https://$git_repo_v $git_repo_dir_v')
gen_vc.cmd_exec('git clone --depth 1 https://$git_repo_vc $git_repo_dir_vc')
// get output of git log -1 (last commit)
git_log_v := gen_vc.cmd_exec('git -C $git_repo_dir_v log -1 --format="commit %H%nDate: %ci%nDate Unix: %ct"')
git_log_vc := gen_vc.cmd_exec('git -C $git_repo_dir_vc log -1 --format="Commit %H%nDate: %ci%nDate Unix: %ct"')
@ -241,7 +241,7 @@ fn (gen_vc mut GenVC) generate() {
// date of last commit in each repo
ts_v := git_log_v.find_between('Date:', '\n').trim_space()
ts_vc := git_log_vc.find_between('Date:', '\n').trim_space()
// parse time as string to time.Time
last_commit_time_v := time.parse(ts_v)
last_commit_time_vc := time.parse(ts_vc)
@ -259,7 +259,7 @@ fn (gen_vc mut GenVC) generate() {
gen_vc.logger.debug('last commit time ($git_repo_v): ' + last_commit_time_v.format_ss())
gen_vc.logger.debug('last commit time ($git_repo_vc): ' + last_commit_time_vc.format_ss())
gen_vc.logger.debug('last commit hash ($git_repo_v): $last_commit_hash_v')
// if vc repo already has a newer commit than the v repo, assume it's up to date
if t_unix_vc >= t_unix_v {
gen_vc.logger.warn('vc repository is already up to date.')
@ -271,7 +271,7 @@ fn (gen_vc mut GenVC) generate() {
v_exec := '$git_repo_dir_v/v'
// check if make was successful
gen_vc.assert_file_exists_and_is_not_too_short(v_exec, err_msg_make)
// build v.c for each os
for os_name in vc_build_oses {
vc_suffix := if os_name == 'nix' { '' } else { '_${os_name[..3]}' }
@ -292,7 +292,7 @@ fn (gen_vc mut GenVC) generate() {
}
// check if the vc repo actually changed
git_status := gen_vc.cmd_exec('git -C $git_repo_dir_vc status')
git_status := gen_vc.cmd_exec('git -C $git_repo_dir_vc status')
if git_status.contains('nothing to commit') {
gen_vc.logger.error('no changes to vc repo: something went wrong.')
gen_vc.gen_error = true
@ -349,12 +349,12 @@ fn (gen_vc mut GenVC) command_execute_dry(cmd string) string {
fn (gen_vc mut GenVC) purge_repos() {
// delete old repos (better to be fully explicit here, since these are destructive operations)
mut repo_dir := '$gen_vc.options.work_dir/$git_repo_dir_v'
if os.dir_exists(repo_dir) {
if os.is_dir(repo_dir) {
gen_vc.logger.info('purging local repo: "$repo_dir"')
gen_vc.cmd_exec('rm -rf $repo_dir')
}
repo_dir = '$gen_vc.options.work_dir/$git_repo_dir_vc'
if os.dir_exists(repo_dir) {
if os.is_dir(repo_dir) {
gen_vc.logger.info('purging local repo: "$repo_dir"')
gen_vc.cmd_exec('rm -rf $repo_dir')
}
@ -362,7 +362,7 @@ fn (gen_vc mut GenVC) purge_repos() {
// check if file size is too short
fn (gen_vc mut GenVC) assert_file_exists_and_is_not_too_short(f string, emsg string){
if !os.file_exists(f) {
if !os.exists(f) {
gen_vc.logger.error('$err_msg_build: $emsg .')
gen_vc.gen_error = true
return

View File

@ -94,7 +94,7 @@ pub fn (ts mut TestSession) test() {
pub fn vlib_should_be_present( parent_dir string ) {
vlib_dir := filepath.join( parent_dir, 'vlib' )
if !os.dir_exists( vlib_dir ){
if !os.is_dir( vlib_dir ){
eprintln('$vlib_dir is missing, it must be next to the V executable')
exit(1)
}

View File

@ -152,7 +152,7 @@ fn get_vmodules_dir_path() string {
fn ensure_vmodules_dir_exist() {
home_vmodules := get_vmodules_dir_path()
if !os.dir_exists( home_vmodules ) {
if !os.is_dir( home_vmodules ) {
println('Creating $home_vmodules/ ...')
os.mkdir(home_vmodules) or { panic(err) }
}

View File

@ -200,7 +200,7 @@ fn print_output(s os.Result) {
}
fn main() {
if os.args.len < 2 || !os.file_exists(os.args[1]) {
if os.args.len < 2 || !os.exists(os.args[1]) {
println('Usage:')
println(' vrepl vexepath\n')
println(' ... where vexepath is the full path to the v executable file')

View File

@ -26,7 +26,7 @@ fn v_test_compiler(vargs string){
os.chdir( parent_dir )
/*
if !os.file_exists(parent_dir + '/v.v') {
if !os.exists(parent_dir + '/v.v') {
eprintln('v.v is missing, it must be next to the V executable')
exit(1)
}
@ -34,7 +34,7 @@ fn v_test_compiler(vargs string){
// Make sure v.c can be compiled without warnings
$if mac {
if os.file_exists('/v.v') {
if os.exists('/v.v') {
os.system('$vexe -o v.c v.v')
if os.system('cc -Werror v.c') != 0 {
eprintln('cc failed to build v.c without warnings')
@ -63,7 +63,7 @@ fn v_test_compiler(vargs string){
if ret != 0 {
eprintln('failed to run v install')
}
if !os.file_exists(v_modules_path + '/nedpals/args') {
if !os.exists(v_modules_path + '/nedpals/args') {
eprintln('v failed to install a test module')
}
vmark.stop()

View File

@ -32,11 +32,11 @@ pub fn main() {
mut ts := testing.new_test_sesion(args_before)
for targ in args_after.split(' ') {
if os.file_exists(targ) && targ.ends_with('_test.v') {
if os.exists(targ) && targ.ends_with('_test.v') {
ts.files << targ
continue
}
if os.dir_exists(targ) {
if os.is_dir(targ) {
// Fetch all tests from the directory
ts.files << os.walk_ext( targ.trim_right(os.path_separator), '_test.v')
continue

View File

@ -8,7 +8,7 @@ fn main() {
println(s.output)
$if windows {
v_backup_file := '$vroot/v_old.exe'
if os.file_exists( v_backup_file ) {
if os.exists( v_backup_file ) {
os.rm( v_backup_file )
}
os.mv_by_cp('$vroot/v.exe', v_backup_file) or { panic(err) }

View File

@ -8,11 +8,11 @@ fn test_syscallwrappers() {
vdir := os.dir(exe)
if vdir.len > 1 {
dot_checks := vdir + "/.checks"
assert os.dir_exists(dot_checks)
assert os.is_dir(dot_checks)
os.chdir(dot_checks)
checks_v := "checks.v"
assert os.file_exists(checks_v)
assert os.exists(checks_v)
rc := os.exec("v run $checks_v") or { panic(err) }
assert !rc.output.contains("V panic: An assertion failed.")
assert !rc.output.contains("failed")

View File

@ -38,7 +38,7 @@ fn (v mut V) cc() {
$if !js {
if v.out_name.ends_with('.js') {
vjs_path := vexe + 'js'
if !os.file_exists(vjs_path) {
if !os.exists(vjs_path) {
println('V.js compiler not found, building...')
// Build V.js. Specifying `-os js` makes V include
// only _js.v files and ignore _c.v files.
@ -98,7 +98,7 @@ fn (v mut V) cc() {
tcc_3rd := '$vdir/thirdparty/tcc/bin/tcc'
//println('tcc third "$tcc_3rd"')
tcc_path := '/var/tmp/tcc/bin/tcc'
if os.file_exists(tcc_3rd) && !os.file_exists(tcc_path) {
if os.exists(tcc_3rd) && !os.exists(tcc_path) {
//println('moving tcc')
// if there's tcc in thirdparty/, that means this is
// a prebuilt V_linux.zip.
@ -106,7 +106,7 @@ fn (v mut V) cc() {
// it to /var/tmp/
os.system('mv $vdir/thirdparty/tcc /var/tmp/')
}
if v.pref.ccompiler == 'cc' && os.file_exists(tcc_path) {
if v.pref.ccompiler == 'cc' && os.exists(tcc_path) {
// TODO tcc bug, needs an empty libtcc1.a fila
//os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) }
//os.create('/var/tmp/tcc/lib/tcc/libtcc1.a')
@ -136,7 +136,7 @@ fn (v mut V) cc() {
'$v_modules_path${os.path_separator}$v.dir'
}
pdir := out_dir.all_before_last(os.path_separator)
if !os.dir_exists(pdir) {
if !os.is_dir(pdir) {
os.mkdir_all(pdir)
}
v.out_name = '${out_dir}.o' //v.out_name
@ -202,7 +202,7 @@ fn (v mut V) cc() {
else if v.pref.is_cache {
builtin_o_path := filepath.join(v_modules_path, 'cache', 'vlib', 'builtin.o')
a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed
if os.file_exists(builtin_o_path) {
if os.exists(builtin_o_path) {
libs = builtin_o_path
} else {
println('$builtin_o_path not found... building module builtin')
@ -215,7 +215,7 @@ fn (v mut V) cc() {
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'
//println('adding ${imp_path}.o')
if os.file_exists(path) {
if os.exists(path) {
libs += ' ' + path
} else {
println('$path not found... building module $imp')
@ -253,7 +253,7 @@ fn (v mut V) cc() {
//
// Output executable name
a << '-o "$v.out_name"'
if os.dir_exists(v.out_name) {
if os.is_dir(v.out_name) {
verror('\'$v.out_name\' is a directory')
}
// macOS code can include objective C TODO remove once objective C is replaced with C
@ -426,7 +426,7 @@ fn (c mut V) cc_windows_cross() {
mut libs := ''
if c.pref.build_mode == .default_mode {
libs = '"$v_modules_path/vlib/builtin.o"'
if !os.file_exists(libs) {
if !os.exists(libs) {
println('`$libs` not found')
exit(1)
}
@ -442,7 +442,7 @@ fn (c mut V) cc_windows_cross() {
}
println('Cross compiling for Windows...')
winroot := '$v_modules_path/winroot'
if !os.dir_exists(winroot) {
if !os.is_dir(winroot) {
winroot_url := 'https://github.com/vlang/v/releases/download/v0.1.10/winroot.zip'
println('"$winroot" not found.')
println('Download it from $winroot_url and save it in $v_modules_path')
@ -502,7 +502,7 @@ fn find_c_compiler() string {
fn find_c_compiler_default() string {
//fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
//if os.file_exists(fast_clang) {
//if os.exists(fast_clang) {
// return fast_clang
//}
// TODO fix $if after 'string'

View File

@ -255,7 +255,7 @@ fn (g mut CGen) add_to_main(s string) {
fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
obj_path := os.realpath(path)
if os.file_exists(obj_path) {
if os.exists(obj_path) {
return
}
println('$obj_path not found, building it...')

View File

@ -153,12 +153,12 @@ fn (p mut Parser) comp_time() {
if p.pref.is_debug {
println('compiling tmpl $path')
}
if !os.file_exists(path) {
if !os.exists(path) {
// Can't find the template file in current directory,
// try looking next to the vweb program, in case it's run with
// v path/to/vweb_app.v
path = os.dir(p.scanner.file_path) + '/' + path
if !os.file_exists(path) {
if !os.exists(path) {
p.error('vweb HTML template "$path" not found')
}
}

View File

@ -191,7 +191,7 @@ void reload_so() {
sprintf(compile_cmd, "$vexe $msvc -o %s -shared $file", new_so_base);
os__system(tos2(compile_cmd));
if( !os__file_exists(tos2(new_so_name)) ) {
if( !os__exists(tos2(new_so_name)) ) {
fprintf(stderr, "Errors while compiling $file\\n");
continue;
}

View File

@ -587,13 +587,13 @@ pub fn (v V) run_compiled_executable_and_exit() {
pub fn (v &V) v_files_from_dir(dir string) []string {
mut res := []string
if !os.file_exists(dir) {
if dir == 'compiler' && os.dir_exists('vlib') {
if !os.exists(dir) {
if dir == 'compiler' && os.is_dir('vlib') {
println('looks like you are trying to build V with an old command')
println('use `v -o v v.v` instead of `v -o v compiler`')
}
verror("$dir doesn't exist")
} else if !os.dir_exists(dir) {
} else if !os.is_dir(dir) {
verror("$dir isn't a directory")
}
mut files := os.ls(dir) or { panic(err) }
@ -639,7 +639,7 @@ pub fn (v mut V) add_v_files_to_compile() {
}
// Builtin cache exists? Use it.
builtin_vh := '${v.pref.vlib_path}${os.path_separator}builtin.vh'
if v.pref.is_cache && os.file_exists(builtin_vh) {
if v.pref.is_cache && os.exists(builtin_vh) {
v.cached_mods << 'builtin'
builtin_files = [builtin_vh]
}
@ -685,7 +685,7 @@ pub fn (v mut V) add_v_files_to_compile() {
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'
if v.pref.is_cache && os.file_exists(vh_path) {
if v.pref.is_cache && os.exists(vh_path) {
eprintln('using cached module `$mod`: $vh_path')
v.cached_mods << mod
v.files << vh_path
@ -850,7 +850,7 @@ pub fn (v &V) log(s string) {
pub fn new_v(args[]string) &V {
// Create modules dirs if they are missing
if !os.dir_exists(v_modules_path) {
if !os.is_dir(v_modules_path) {
os.mkdir(v_modules_path) or { panic(err) }
os.mkdir('$v_modules_path${os.path_separator}cache') or { panic(err) }
}
@ -923,7 +923,7 @@ pub fn new_v(args[]string) &V {
}
is_test := dir.ends_with('_test.v')
is_script := dir.ends_with('.v') || dir.ends_with('.vsh')
if is_script && !os.file_exists(dir) {
if is_script && !os.exists(dir) {
println('`$dir` does not exist')
exit(1)
}
@ -933,7 +933,7 @@ pub fn new_v(args[]string) &V {
// Building V? Use v2, since we can't overwrite a running
// executable on Windows + the precompiled V is more
// optimized.
if out_name == 'v' && os.dir_exists('vlib/compiler') {
if out_name == 'v' && os.is_dir('vlib/compiler') {
println('Saving the resulting V executable in `./v2`')
println('Use `v -o v v.v` if you want to replace current '+
'V executable.')
@ -948,7 +948,7 @@ pub fn new_v(args[]string) &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 !os.dir_exists(d) {
if !os.is_dir(d) {
println('creating a new directory "$d"')
os.mkdir(d) or { panic(err) }
}
@ -989,7 +989,7 @@ pub fn new_v(args[]string) &V {
}
//println('VROOT=$vroot')
// v.exe's parent directory should contain vlib
if !os.dir_exists(vlib_path) || !os.dir_exists(vlib_path + os.path_separator + 'builtin') {
if !os.is_dir(vlib_path) || !os.is_dir(vlib_path + os.path_separator + 'builtin') {
//println('vlib not found, downloading it...')
/*
ret := os.system('git clone --depth=1 https://github.com/vlang/v .')
@ -1104,7 +1104,7 @@ pub fn env_vflags_and_os_args() []string {
pub fn vfmt(args[]string) {
println('running vfmt...')
file := args.last()
if !os.file_exists(file) {
if !os.exists(file) {
println('"$file" does not exist')
exit(1)
}

View File

@ -34,7 +34,7 @@ fn generate_vh(mod string) {
dir := if mod.starts_with('vlib') { '$compiler.v_modules_path${os.path_separator}$mod' } else { mod }
path := dir + '.vh'
pdir := dir.all_before_last(os.path_separator)
if !os.dir_exists(pdir) {
if !os.is_dir(pdir) {
os.mkdir_all(pdir)
// os.mkdir(os.realpath(dir)) or { panic(err) }
}

View File

@ -170,7 +170,7 @@ fn (v &V) find_module_path(mod string) ?string {
}
for try_path in tried_paths {
if v.pref.is_verbose { println(' >> trying to find $mod in $try_path ...') }
if os.dir_exists(try_path) {
if os.is_dir(try_path) {
return try_path
}
}

View File

@ -166,7 +166,7 @@ fn find_vs(vswhere_dir string, host_arch string) ?VsInstallation {
lib_path := '$res.output\\VC\\Tools\\MSVC\\$v\\lib\\$host_arch'
include_path := '$res.output\\VC\\Tools\\MSVC\\$v\\include'
if os.file_exists('$lib_path\\vcruntime.lib') {
if os.exists('$lib_path\\vcruntime.lib') {
p := '$res.output\\VC\\Tools\\MSVC\\$v\\bin\\Host$host_arch\\$host_arch'
// println('$lib_path $include_path')
@ -274,7 +274,7 @@ pub fn (v mut V) cc_msvc() {
/*
b := os.realpath( '$v_modules_path/vlib/builtin.obj' )
alibs << '"$b"'
if !os.file_exists(b) {
if !os.exists(b) {
println('`builtin.obj` not found')
exit(1)
}
@ -390,7 +390,7 @@ fn build_thirdparty_obj_file_with_msvc(path string, moduleflags []CFlag) {
obj_path = os.realpath(obj_path)
if os.file_exists(obj_path) {
if os.exists(obj_path) {
println('$obj_path already build.')
return
}

View File

@ -48,7 +48,7 @@ mut:
// new scanner from file.
fn new_scanner_file(file_path string) &Scanner {
if !os.file_exists(file_path) {
if !os.exists(file_path) {
verror("$file_path doesn't exist")
}

View File

@ -9,7 +9,7 @@ import filepath
pub fn get_vtmp_folder() string {
vtmp := filepath.join(os.tmpdir(),'v')
if !os.dir_exists( vtmp ) {
if !os.is_dir( vtmp ) {
os.mkdir(vtmp) or { panic(err) }
}
return vtmp

View File

@ -15,7 +15,7 @@ pub fn launch_tool(tname string){
//println('Launching: "$tool_command" ...')
mut tool_should_be_recompiled := false
if !os.file_exists( tool_exe ) {
if !os.exists( tool_exe ) {
// fresh checkout
tool_should_be_recompiled = true
}else{

View File

@ -173,12 +173,12 @@ pub fn new_context(cfg gg.Cfg) &FreeType {
if font_path == '' {
font_path = 'RobotoMono-Regular.ttf'
}
if !os.file_exists(font_path) {
if !os.exists(font_path) {
exe_path := os.executable()
exe_dir := os.basedir(exe_path)
font_path = '$exe_dir/$font_path'
}
if !os.file_exists(font_path) {
if !os.exists(font_path) {
println('failed to load $font_path')
return 0
}

View File

@ -363,7 +363,7 @@ pub fn create_image(file string) u32 {
if file.contains('twitch') {
return u32(0)// TODO
}
if !os.file_exists(file) {
if !os.exists(file) {
println('gg create image no such file "$file"')
return u32(0)
}

View File

@ -164,7 +164,7 @@ pub fn cp(old, new string) ?bool {
pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{
source_path := os.realpath( osource_path )
dest_path := os.realpath( odest_path )
if !os.file_exists(source_path) {
if !os.exists(source_path) {
return error('Source path doesn\'t exist')
}
//single file copy
@ -174,7 +174,7 @@ pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{
} else {
dest_path
}
if os.file_exists(adjasted_path) {
if os.exists(adjasted_path) {
if overwrite {
os.rm(adjasted_path)
}
@ -523,16 +523,21 @@ pub fn unsetenv(name string) int {
}
}
// file_exists returns true if `path` exists.
pub fn file_exists(_path string) bool {
// exists returns true if `path` exists.
pub fn exists(path string) bool {
$if windows {
path := _path.replace('/', '\\')
return C._waccess(path.to_wide(), 0) != -1
p := path.replace('/', '\\')
return C._waccess(p.to_wide(), 0) != -1
} $else {
return C.access(_path.str, 0 ) != -1
return C.access(path.str, 0 ) != -1
}
}
[deprecated]
pub fn file_exists(_path string) bool {
panic('use os.exists(path) instead of os.file_exists(path)')
}
// rm removes file in `path`.
pub fn rm(path string) {
$if windows {
@ -822,13 +827,24 @@ pub fn executable() string {
return os.args[0]
}
[deprecated]
pub fn dir_exists(path string) bool {
panic('use os.is_dir()')
//return false
}
// is_dir returns a boolean indicating whether the given path is a directory.
pub fn is_dir(path string) bool {
$if windows {
return dir_exists(path)
//val := int(C.GetFileAttributes(path.to_wide()))
// Note: this return is broke (wrong). we have dir_exists already how will this differ?
//return (val &FILE_ATTRIBUTE_DIRECTORY) > 0
_path := path.replace('/', '\\')
attr := C.GetFileAttributesW(_path.to_wide())
if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) {
return false
}
if (int(attr) & C.FILE_ATTRIBUTE_DIRECTORY) != 0 {
return true
}
return false
}
$else {
statbuf := C.stat{}
@ -927,7 +943,7 @@ pub fn walk(path string, fnc fn(path string)) {
if os.is_dir(p) {
walk(p, fnc)
}
else if os.file_exists(p) {
else if os.exists(p) {
fnc(p)
}
}
@ -996,7 +1012,7 @@ 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
if !os.dir_exists(p) {
if !os.is_dir(p) {
os.mkdir(p) or { panic(err) }
}
}

View File

@ -47,12 +47,11 @@ pub fn ls(path string) ?[]string {
return res
}
pub fn dir_exists(path string) bool {
/*
$if linux {
C.syscall(4, path.str) // sys_newstat
}
*/
/*
pub fn is_dir(path string) bool {
//$if linux {
//C.syscall(4, path.str) // sys_newstat
//}
dir := C.opendir(path.str)
res := !isnil(dir)
if res {
@ -60,6 +59,7 @@ pub fn dir_exists(path string) bool {
}
return res
}
*/
// mkdir creates a new directory with the specified path.
pub fn mkdir(path string) ?bool {

View File

@ -76,14 +76,14 @@ fn test_write_and_read_bytes() {
fn test_create_and_delete_folder() {
folder := './test1'
os.mkdir(folder) or { panic(err) }
assert os.dir_exists(folder)
assert os.is_dir(folder)
folder_contents := os.ls(folder) or { panic(err) }
assert folder_contents.len == 0
os.rmdir(folder)
folder_exists := os.dir_exists(folder)
folder_exists := os.is_dir(folder)
assert folder_exists == false
}

View File

@ -46,7 +46,7 @@ mut:
}
struct StartupInfo {
mut:
mut:
cb u32
lpReserved &u16
lpDesktop &u16
@ -89,13 +89,13 @@ fn init_os_args(argc int, argv &byteptr) []string {
pub fn ls(path string) ?[]string {
mut find_file_data := Win32finddata{}
mut dir_files := []string
// We can also check if the handle is valid. but using dir_exists instead
// We can also check if the handle is valid. but using is_dir instead
// h_find_dir := C.FindFirstFile(path.str, &find_file_data)
// if (INVALID_HANDLE_VALUE == h_find_dir) {
// return dir_files
// }
// C.FindClose(h_find_dir)
if !dir_exists(path) {
if !is_dir(path) {
return error('ls() couldnt open dir "$path": directory does not exist')
}
// NOTE: Should eventually have path struct & os dependant path seperator (eg os.PATH_SEPERATOR)
@ -118,7 +118,8 @@ pub fn ls(path string) ?[]string {
return dir_files
}
pub fn dir_exists(path string) bool {
/*
pub fn is_dir(path string) bool {
_path := path.replace('/', '\\')
attr := C.GetFileAttributesW(_path.to_wide())
if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) {
@ -129,6 +130,7 @@ pub fn dir_exists(path string) bool {
}
return false
}
*/
@ -250,7 +252,7 @@ pub fn exec(cmd string) ?Result {
panic('exec failed (SetHandleInformation): $error_msg')
}
proc_info := ProcessInformation{}
proc_info := ProcessInformation{}
mut start_info := StartupInfo{}
start_info.cb = sizeof(C.PROCESS_INFORMATION)
start_info.hStdInput = child_stdin
@ -265,7 +267,7 @@ pub fn exec(cmd string) ?Result {
return error('exec failed (CreateProcess): $error_msg')
}
C.CloseHandle(child_stdin)
C.CloseHandle(child_stdout_write)
C.CloseHandle(child_stdout_write)
buf := [1000]byte
mut bytes_read := u32(0)
mut read_data := ''
@ -274,7 +276,7 @@ pub fn exec(cmd string) ?Result {
read_data += tos(buf, int(bytes_read))
if readfile_result == false || int(bytes_read) == 0 {
break
}
}
}
read_data = read_data.trim_space()
exit_code := u32(0)

View File

@ -76,7 +76,7 @@ fn (am mut AssetManager) combine(asset_type string, to_file bool) string {
out_file := '$am.cache_dir/${cache_key}.$asset_type'
mut out := ''
// use cache
if os.file_exists(out_file) {
if os.exists(out_file) {
if to_file {
return out_file
}
@ -102,7 +102,7 @@ fn (am mut AssetManager) combine(asset_type string, to_file bool) string {
if !to_file {
return out
}
if !os.dir_exists(am.cache_dir) {
if !os.is_dir(am.cache_dir) {
os.mkdir(am.cache_dir) or { panic(err) }
}
mut file := os.create(out_file) or {
@ -153,7 +153,7 @@ fn (am mut AssetManager) include(asset_type string, combine bool) string {
// dont return option until size limit is removed
// fn (am mut AssetManager) add(asset_type, file string) ?bool {
fn (am mut AssetManager) add(asset_type, file string) bool {
if !os.file_exists(file) {
if !os.exists(file) {
// return error('vweb.assets: cannot add asset $file, it does not exist')
return false
}

View File

@ -19,7 +19,7 @@ pub fn compile_template(path string) string {
panic('html failed')
}
mut header := ''
if os.file_exists('header.html') {
if os.exists('header.html') {
h := os.read_file('header.html') or {
panic('html failed')
}