From e3512092c90e24807fdf2dd8c6197c7f883aebba Mon Sep 17 00:00:00 2001 From: larpon <768942+larpon@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:58:49 +0200 Subject: [PATCH] vab: introduce and use `paths` module (#316) --- android/screenshot.v | 8 +++---- cli/cli.v | 9 +++++--- cli/utils.v | 8 ------- paths/paths.v | 55 ++++++++++++++++++++++++++++++++++++++++++++ util/util.v | 2 ++ 5 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 paths/paths.v diff --git a/android/screenshot.v b/android/screenshot.v index ec0b809c..2683ffae 100644 --- a/android/screenshot.v +++ b/android/screenshot.v @@ -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 @@ -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) @@ -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) { @@ -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) { diff --git a/cli/cli.v b/cli/cli.v index 0b51d29c..157bd90b 100644 --- a/cli/cli.v +++ b/cli/cli.v @@ -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 @@ -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'] @@ -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 { diff --git a/cli/utils.v b/cli/utils.v index ecc26689..829fa5aa 100644 --- a/cli/utils.v +++ b/cli/utils.v @@ -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}' } diff --git a/paths/paths.v b/paths/paths.v new file mode 100644 index 00000000..73919301 --- /dev/null +++ b/paths/paths.v @@ -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) +} diff --git a/util/util.v b/util/util.v index 3c232659..0f809e81 100644 --- a/util/util.v +++ b/util/util.v @@ -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 {