Skip to content

Commit

Permalink
vab: introduce and use paths module (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon authored Oct 2, 2024
1 parent 8b18836 commit e351209
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
8 changes: 4 additions & 4 deletions android/screenshot.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module android

import os
import time
import vab.util as vabutil
import vab.paths
import vab.android.env
import vab.android.util

Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn simple_screenshot(opt SimpleScreenshotOptions) ! {
return error('${@MOD}.${@FN}: Taking screenshots requires a device id. Set one via --device or ANDROID_SERIAL')
}
out_dir, out_file := resolve_screenshot_output(opt.path)!
vabutil.ensure_path(out_dir)!
paths.ensure(out_dir)!

output := os.join_path(out_dir, out_file)

Expand Down Expand Up @@ -97,7 +97,7 @@ pub fn screenshot(opt ScreenshotOptions) ! {
}

out_dir, out_file := opt.resolve_output()!
vabutil.ensure_path(out_dir)!
paths.ensure(out_dir)!

output := os.join_path(out_dir, out_file)
if os.exists(output) {
Expand Down Expand Up @@ -153,7 +153,7 @@ pub fn screenshot_on_log_line(opt ScreenshotOptions) ! {
}

out_dir, out_file := opt.resolve_output()!
vabutil.ensure_path(out_dir)!
paths.ensure(out_dir)!

output := os.join_path(out_dir, out_file)
if os.exists(output) {
Expand Down
9 changes: 6 additions & 3 deletions cli/cli.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import os
import flag
import vab.vxt
import vab.java
import vab.paths
import vab.android
import vab.android.sdk
import vab.android.ndk
Expand All @@ -30,8 +31,8 @@ Sub-commands:
`vab install "platforms;android-21"'

pub const exe_git_hash = vab_commit_hash()
pub const work_directory = vab_tmp_work_dir()
pub const cache_directory = vab_cache_dir()
pub const work_directory = paths.tmp_work()
pub const cache_directory = paths.cache()
pub const rip_vflags = ['-autofree', '-gc', '-g', '-cg', '-prod', 'run', '-showcc', '-skip-unused',
'-no-bounds-checking'] // NOTE this can be removed when the deprecated `cli.args_to_options()` is removed
pub const subcmds = ['complete', 'test-all', 'test-cleancode', 'test-runtime']
Expand Down Expand Up @@ -70,7 +71,9 @@ pub const vab_documentation_config = flag.DocConfig{
}
}

// run_vab_sub_command runs and exits a sub-command if found in `args`
// run_vab_sub_command runs (compiles if needed) a sub-command if found in `args`.
// If the command is found this function will call `exit()` with the result
// returned by the executed command.
pub fn run_vab_sub_command(args []string) {
// Indentify sub-commands.
for subcmd in subcmds {
Expand Down
8 changes: 0 additions & 8 deletions cli/utils.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ fn vab_commit_hash() string {
return hash.trim_space()
}

fn vab_tmp_work_dir() string {
return os.join_path(os.temp_dir(), exe_name.replace(' ', '_').replace('.exe', '').to_lower())
}

fn vab_cache_dir() string {
return os.join_path(os.cache_dir(), exe_name.replace(' ', '_').replace('.exe', '').to_lower())
}

fn version_full() string {
return '${exe_version} ${exe_git_hash}'
}
Expand Down
55 changes: 55 additions & 0 deletions paths/paths.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright(C) 2023 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license file distributed with this software package
module paths

import os

pub const vab_namespace = 'vab'

const sanitized_exe_name = os.file_name(os.executable()).replace(' ', '_').replace('.exe',
'').to_lower()

// ensure creates `path` if it does not already exist.
pub fn ensure(path string) ! {
if !os.exists(path) {
os.mkdir_all(path) or {
return error('${@MOD}.${@FN}: error while making directory "${path}":\n${err}')
}
}
}

// config returns a `string` with the path to `vab`'s' configuration directory.
// NOTE: the returned path may not exist on disk. Use `ensure/1` to ensure it exists.
pub fn config() string {
return os.join_path(os.config_dir() or { panic('${@MOD}.${@FN}: ${err}') }, vab_namespace)
}

// tmp_work returns a `string` with the path to `vab`'s' temporary work directory.
// NOTE: the returned path may not exist on disk. Use `ensure/1` to ensure it exists.
pub fn tmp_work() string {
return os.join_path(os.temp_dir(), vab_namespace)
}

// cache returns a `string` with the path to `vab`'s' cache directory.
// NOTE: the returned path may not exist on disk. Use `ensure/1` to ensure it exists.
pub fn cache() string {
return os.join_path(os.cache_dir(), vab_namespace)
}

// exe_config returns a `string` with the path to the executable's configuration directory.
// NOTE: the returned path may not exist on disk. Use `ensure/1` to ensure it exists.
pub fn exe_config() string {
return os.join_path(os.config_dir() or { panic('${@MOD}.${@FN}: ${err}') }, sanitized_exe_name)
}

// exe_tmp_work returns a `string` with the path to the executable's temporary work directory.
// NOTE: the returned path may not exist on disk. Use `ensure/1` to ensure it exists.
pub fn exe_tmp_work() string {
return os.join_path(os.temp_dir(), sanitized_exe_name)
}

// exe_cache returns a `string` with the path to the executable's cache directory.
// NOTE: the returned path may not exist on disk. Use `ensure/1` to ensure it exists.
pub fn exe_cache() string {
return os.join_path(os.cache_dir(), sanitized_exe_name)
}
2 changes: 2 additions & 0 deletions util/util.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub:
}

// ensure_path creates `path` if it does not already exist.
@[deprecated: 'use paths.ensure() instead']
@[deprecated_after: '2025-10-02']
pub fn ensure_path(path string) ! {
if !os.exists(path) {
os.mkdir_all(path) or {
Expand Down

0 comments on commit e351209

Please sign in to comment.