mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: merge with filepath
This commit is contained in:
78
vlib/os/os.v
78
vlib/os/os.v
@@ -3,8 +3,6 @@
|
||||
// that can be found in the LICENSE file.
|
||||
module os
|
||||
|
||||
import filepath
|
||||
|
||||
#include <sys/stat.h> // #include <signal.h>
|
||||
#include <errno.h>
|
||||
|
||||
@@ -183,7 +181,7 @@ pub fn cp_all(osource_path, odest_path string, overwrite bool) ?bool {
|
||||
}
|
||||
// single file copy
|
||||
if !os.is_dir(source_path) {
|
||||
adjasted_path := if os.is_dir(dest_path) { filepath.join(dest_path,filepath.filename(source_path)) } else { dest_path }
|
||||
adjasted_path := if os.is_dir(dest_path) { os.join(dest_path,os.filename(source_path)) } else { dest_path }
|
||||
if os.exists(adjasted_path) {
|
||||
if overwrite {
|
||||
os.rm(adjasted_path)
|
||||
@@ -204,8 +202,8 @@ pub fn cp_all(osource_path, odest_path string, overwrite bool) ?bool {
|
||||
return error(err)
|
||||
}
|
||||
for file in files {
|
||||
sp := filepath.join(source_path,file)
|
||||
dp := filepath.join(dest_path,file)
|
||||
sp := os.join(source_path,file)
|
||||
dp := os.join(dest_path,file)
|
||||
if os.is_dir(sp) {
|
||||
os.mkdir(dp) or {
|
||||
panic(err)
|
||||
@@ -634,10 +632,10 @@ pub fn rmdir_all(path string) {
|
||||
return
|
||||
}
|
||||
for item in items {
|
||||
if os.is_dir(filepath.join(path,item)) {
|
||||
rmdir_all(filepath.join(path,item))
|
||||
if os.is_dir(os.join(path,item)) {
|
||||
rmdir_all(os.join(path,item))
|
||||
}
|
||||
os.rm(filepath.join(path,item))
|
||||
os.rm(os.join(path,item))
|
||||
}
|
||||
os.rmdir(path)
|
||||
}
|
||||
@@ -655,24 +653,30 @@ fn print_c_errno() {
|
||||
println('errno=$e err=$se')
|
||||
}
|
||||
|
||||
[deprecated]
|
||||
pub fn ext(path string) string {
|
||||
panic('Use `filepath.ext` instead of `os.ext`')
|
||||
pos := path.last_index('.') or {
|
||||
return ''
|
||||
}
|
||||
return path[pos..]
|
||||
}
|
||||
|
||||
[deprecated]
|
||||
pub fn dir(path string) string {
|
||||
panic('Use `filepath.dir` instead of `os.dir`')
|
||||
pos := path.last_index(path_separator) or {
|
||||
return '.'
|
||||
}
|
||||
return path[..pos]
|
||||
}
|
||||
|
||||
[deprecated]
|
||||
pub fn basedir(path string) string {
|
||||
panic('Use `filepath.basedir` instead of `os.basedir`')
|
||||
pos := path.last_index(path_separator) or {
|
||||
return path
|
||||
}
|
||||
// NB: *without* terminating /
|
||||
return path[..pos]
|
||||
}
|
||||
|
||||
[deprecated]
|
||||
pub fn filename(path string) string {
|
||||
panic('Use `filepath.filename` instead of `os.filename`')
|
||||
return path.all_after(path_separator)
|
||||
}
|
||||
|
||||
// get_line returns a one-line string from stdin
|
||||
@@ -792,9 +796,9 @@ pub fn user_os() string {
|
||||
// home_dir returns path to user's home directory.
|
||||
pub fn home_dir() string {
|
||||
$if windows {
|
||||
return os.getenv('USERPROFILE') + filepath.separator
|
||||
return os.getenv('USERPROFILE') + os.path_separator
|
||||
} $else {
|
||||
return os.getenv('HOME') + filepath.separator
|
||||
return os.getenv('HOME') + os.path_separator
|
||||
}
|
||||
}
|
||||
|
||||
@@ -988,6 +992,25 @@ pub fn realpath(fpath string) string {
|
||||
return string(fullpath)
|
||||
}
|
||||
|
||||
// is_abs returns true if `path` is absolute.
|
||||
pub fn is_abs(path string) bool {
|
||||
$if windows {
|
||||
return path[0] == `/` || // incase we're in MingGW bash
|
||||
(path[0].is_letter() && path[1] == `:`)
|
||||
}
|
||||
return path[0] == `/`
|
||||
}
|
||||
|
||||
// join returns path as string from string parameter(s).
|
||||
pub fn join(base string, dirs ...string) string {
|
||||
mut result := []string
|
||||
result << base.trim_right('\\/')
|
||||
for d in dirs {
|
||||
result << d
|
||||
}
|
||||
return result.join(path_separator)
|
||||
}
|
||||
|
||||
// walk_ext returns a recursive list of all file paths ending with `ext`.
|
||||
pub fn walk_ext(path, ext string) []string {
|
||||
if !os.is_dir(path) {
|
||||
@@ -997,7 +1020,7 @@ pub fn walk_ext(path, ext string) []string {
|
||||
return []
|
||||
}
|
||||
mut res := []string
|
||||
separator := if path.ends_with(filepath.separator) { '' } else { filepath.separator }
|
||||
separator := if path.ends_with(os.path_separator) { '' } else { os.path_separator }
|
||||
for i, file in files {
|
||||
if file.starts_with('.') {
|
||||
continue
|
||||
@@ -1023,7 +1046,7 @@ pub fn walk(path string, f fn(path string)) {
|
||||
return
|
||||
}
|
||||
for file in files {
|
||||
p := path + filepath.separator + file
|
||||
p := path + os.path_separator + file
|
||||
if os.is_dir(p) && !os.is_link(p) {
|
||||
walk(p, f)
|
||||
}
|
||||
@@ -1089,9 +1112,9 @@ pub fn flush() {
|
||||
}
|
||||
|
||||
pub fn mkdir_all(path string) {
|
||||
mut p := if path.starts_with(filepath.separator) { filepath.separator } else { '' }
|
||||
for subdir in path.split(filepath.separator) {
|
||||
p += subdir + filepath.separator
|
||||
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.is_dir(p) {
|
||||
os.mkdir(p) or {
|
||||
panic(err)
|
||||
@@ -1100,11 +1123,6 @@ pub fn mkdir_all(path string) {
|
||||
}
|
||||
}
|
||||
|
||||
[deprecated]
|
||||
pub fn join(base string, dirs ...string) string {
|
||||
panic('Use `filepath.join` instead of `os.join`')
|
||||
}
|
||||
|
||||
// cachedir returns the path to a *writable* user specific folder, suitable for writing non-essential data.
|
||||
pub fn cachedir() string {
|
||||
// See: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
@@ -1169,10 +1187,10 @@ pub const (
|
||||
// It gives a convenient way to access program resources like images, fonts, sounds and so on,
|
||||
// *no matter* how the program was started, and what is the current working directory.
|
||||
pub fn resource_abs_path(path string) string {
|
||||
mut base_path := os.realpath(filepath.dir(os.executable()))
|
||||
mut base_path := os.realpath(os.dir(os.executable()))
|
||||
vresource := os.getenv('V_RESOURCE_PATH')
|
||||
if vresource.len != 0 {
|
||||
base_path = vresource
|
||||
}
|
||||
return os.realpath( filepath.join( base_path, path ) )
|
||||
return os.realpath( os.join( base_path, path ) )
|
||||
}
|
||||
|
@@ -7,10 +7,6 @@ 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 = '/'
|
||||
)
|
||||
|
||||
|
@@ -1,7 +1,4 @@
|
||||
import (
|
||||
os
|
||||
filepath
|
||||
)
|
||||
import os
|
||||
|
||||
fn testsuite_begin() {
|
||||
cleanup_leftovers()
|
||||
@@ -134,14 +131,14 @@ fn walk_callback(file string) {
|
||||
if file == '.' || file == '..' {
|
||||
return
|
||||
}
|
||||
assert file == 'test_walk' + filepath.separator + 'test1'
|
||||
assert file == 'test_walk' + os.path_separator + 'test1'
|
||||
}
|
||||
|
||||
fn test_walk() {
|
||||
folder := 'test_walk'
|
||||
os.mkdir(folder) or { panic(err) }
|
||||
|
||||
file1 := folder + filepath.separator + 'test1'
|
||||
file1 := folder + os.path_separator + 'test1'
|
||||
|
||||
os.write_file(file1,'test-1')
|
||||
|
||||
@@ -190,7 +187,7 @@ fn test_tmpdir(){
|
||||
assert t.len > 0
|
||||
assert os.is_dir(t)
|
||||
|
||||
tfile := t + filepath.separator + 'tmpfile.txt'
|
||||
tfile := t + os.path_separator + 'tmpfile.txt'
|
||||
|
||||
os.rm(tfile) // just in case
|
||||
|
||||
@@ -269,7 +266,7 @@ fn test_symlink() {
|
||||
}
|
||||
|
||||
fn test_is_executable_writable_readable() {
|
||||
file_name := os.tmpdir() + filepath.separator + 'rwxfile.exe'
|
||||
file_name := os.tmpdir() + os.path_separator + 'rwxfile.exe'
|
||||
|
||||
mut f := os.create(file_name) or {
|
||||
eprintln('failed to create file $file_name')
|
||||
@@ -294,6 +291,48 @@ fn test_is_executable_writable_readable() {
|
||||
os.rm(file_name)
|
||||
}
|
||||
|
||||
fn test_ext() {
|
||||
assert os.ext('file.v') == '.v'
|
||||
assert os.ext('file') == ''
|
||||
}
|
||||
|
||||
fn test_is_abs() {
|
||||
assert os.is_abs('/home/user') == true
|
||||
assert os.is_abs('v/vlib') == false
|
||||
|
||||
$if windows {
|
||||
assert os.is_abs('C:\\Windows\\') == true
|
||||
}
|
||||
}
|
||||
|
||||
fn test_join() {
|
||||
$if windows {
|
||||
assert os.join('v', 'vlib', 'os') == 'v\\vlib\\os'
|
||||
} $else {
|
||||
assert os.join('v', 'vlib', 'os') == 'v/vlib/os'
|
||||
}
|
||||
}
|
||||
|
||||
fn test_dir() {
|
||||
$if windows {
|
||||
assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b'
|
||||
} $else {
|
||||
assert os.dir('/var/tmp/foo') == '/var/tmp'
|
||||
}
|
||||
|
||||
assert os.dir('os') == '.'
|
||||
}
|
||||
|
||||
fn test_basedir() {
|
||||
$if windows {
|
||||
assert os.basedir('v\\vlib\\os') == 'v\\vlib'
|
||||
} $else {
|
||||
assert os.basedir('v/vlib/os') == 'v/vlib'
|
||||
}
|
||||
|
||||
assert os.basedir('filename') == 'filename'
|
||||
}
|
||||
|
||||
// this function is called by both test_aaa_setup & test_zzz_cleanup
|
||||
// it ensures that os tests do not polute the filesystem with leftover
|
||||
// files so that they can be run several times in a row.
|
||||
|
@@ -6,10 +6,6 @@ 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 = '\\'
|
||||
)
|
||||
|
||||
|
Reference in New Issue
Block a user