From 4b35495fbe66a4b7791496278d60d7f354a61b7f Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 8 Nov 2020 08:07:17 +0200 Subject: [PATCH] all: support VMODULES environment variable (defaulting to ~/.vmodules) --- cmd/tools/vpm.v | 2 +- cmd/v/help/build.txt | 3 ++- vlib/os/os.v | 11 ++++++++++- vlib/v/builder/cc.v | 4 ++-- vlib/v/doc/module.v | 2 +- vlib/v/pref/default.v | 6 +----- vlib/v/util/util.v | 2 +- vlib/v/vcache/vcache.v | 2 +- 8 files changed, 19 insertions(+), 13 deletions(-) diff --git a/cmd/tools/vpm.v b/cmd/tools/vpm.v index c7e66a5f2e..ee1e30a638 100644 --- a/cmd/tools/vpm.v +++ b/cmd/tools/vpm.v @@ -552,7 +552,7 @@ fn init_settings() { s.is_help = '-h' in os.args || '--help' in os.args || 'help' in os.args s.is_verbose = '-v' in os.args s.server_urls = cmdline.options(os.args, '-server-url') - s.vmodules_path = os.join_path(os.home_dir(), '.vmodules') + s.vmodules_path = os.vmodules_dir() } fn verbose_println(s string) { diff --git a/cmd/v/help/build.txt b/cmd/v/help/build.txt index 148851a428..aa597e9bcc 100644 --- a/cmd/v/help/build.txt +++ b/cmd/v/help/build.txt @@ -54,7 +54,8 @@ The build flags are shared by the build and run commands: separated by pipes (`|`). In addition to absolute paths, you can also use these special strings too: @vmodules - replaced with the location of the global ~/.vmodules/ folder - (modules installed with `v install` are there). + (modules installed with `v install` are there). You can change + its location by setting the environment variable VMODULES. @vlib - replaced with the location of the v's vlib folder. Using these, you can arrange for very flexible search orders for you project, for example: -path "/v/my_project_private_modules|@vlib|@vmodules" diff --git a/vlib/os/os.v b/vlib/os/os.v index 10b4810452..724f347db0 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -1146,7 +1146,7 @@ pub fn real_path(fpath string) string { } fn normalize_drive_letter(path string) string { - // normalize_drive_letter is needed, because a path like c:\nv\.bin (note the small `c`) + // normalize_drive_letter is needed, because a path like c:\nv\.bin (note the small `c`) // in %PATH is NOT recognized by cmd.exe (and probably other programs too)... // Capital drive letters do work fine. $if !windows { @@ -1355,6 +1355,15 @@ pub fn temp_dir() string { return path } +// vmodules_dir returns the path to a folder, where v stores its global modules. +pub fn vmodules_dir() string { + mut path := os.getenv('VMODULES') + if path == '' { + path = os.join_path(os.home_dir(), '.vmodules') + } + return path +} + // chmod change file access attributes of `path` to `mode`. // Octals like `0o600` can be used. pub fn chmod(path string, mode int) { diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index f23f8858b6..396c82ab28 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -637,11 +637,11 @@ fn (mut v Builder) cc() { } fn (mut b Builder) cc_linux_cross() { - parent_dir := os.join_path(os.home_dir(), '.vmodules') + parent_dir := os.vmodules_dir() if !os.exists(parent_dir) { os.mkdir(parent_dir) } - sysroot := os.join_path(os.home_dir(), '.vmodules', 'linuxroot') + sysroot := os.join_path(os.vmodules_dir(), 'linuxroot') if !os.is_dir(sysroot) { println('Downloading files for Linux cross compilation (~18 MB)...') zip_url := 'https://github.com/vlang/v/releases/download/0.1.27/linuxroot.zip' diff --git a/vlib/v/doc/module.v b/vlib/v/doc/module.v index 3d19f2fe7c..8373c38888 100644 --- a/vlib/v/doc/module.v +++ b/vlib/v/doc/module.v @@ -65,7 +65,7 @@ pub fn lookup_module_with_path(mod string, base_path string) ?string { compile_dir := os.real_path(base_path) modules_dir := os.join_path(compile_dir, 'modules', mod_path) vlib_path := os.join_path(os.dir(@VEXE), 'vlib', mod_path) - vmodules_path := os.join_path(os.home_dir(), '.vmodules', mod_path) + vmodules_path := os.join_path(os.vmodules_dir(), mod_path) paths := [modules_dir, vlib_path, vmodules_path] for path in paths { if !os.exists(path) || os.is_dir_empty(path) { diff --git a/vlib/v/pref/default.v b/vlib/v/pref/default.v index 772ec2fbec..d7905eb5ef 100644 --- a/vlib/v/pref/default.v +++ b/vlib/v/pref/default.v @@ -7,13 +7,9 @@ import os import v.vcache pub const ( - default_module_path = mpath() + default_module_path = os.vmodules_dir() ) -fn mpath() string { - return os.join_path(os.home_dir(), '.vmodules') -} - pub fn new_preferences() Preferences { mut p := Preferences{} p.fill_with_defaults() diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index a8a86956db..1e855c46e0 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -299,7 +299,7 @@ fn non_empty(arg []string) []string { } pub fn check_module_is_installed(modulename string, is_verbose bool) ?bool { - mpath := os.join_path(os.home_dir(), '.vmodules', modulename) + mpath := os.join_path(os.vmodules_dir(), modulename) mod_v_file := os.join_path(mpath, 'v.mod') murl := 'https://github.com/vlang/$modulename' if is_verbose { diff --git a/vlib/v/vcache/vcache.v b/vlib/v/vcache/vcache.v index 6b170dacf9..7b170bee5d 100644 --- a/vlib/v/vcache/vcache.v +++ b/vlib/v/vcache/vcache.v @@ -36,7 +36,7 @@ pub mut: pub fn new_cache_manager(opts []string) CacheManager { mut vcache_basepath := os.getenv('VCACHE') if vcache_basepath == '' { - vcache_basepath = os.join_path(os.home_dir(), '.vmodules', 'cache') + vcache_basepath = os.join_path(os.vmodules_dir(), 'cache') } if !os.is_dir(vcache_basepath) { os.mkdir_all(vcache_basepath)