v2 / cmd / tools / vpm / settings.v
113 lines · 102 sloc · 3.68 KB · d4290f0d2a11defd671999322c4a8f6b40ad9f92
Raw
1module main
2
3import os
4import os.cmdline
5import log
6import v.vmod
7
8struct VpmSettings {
9mut:
10 is_help bool
11 is_once bool
12 is_verbose bool
13 is_force bool
14 is_local bool
15 server_urls []string
16 mirror_urls []string
17 vmodules_path string
18 tmp_path string
19 no_dl_count_increment bool
20 // To ensure that some test scenarios with conflicting module directory names do not get stuck in prompts.
21 // It is intended that VPM does not display a prompt when `VPM_FAIL_ON_PROMPT` is set.
22 fail_on_prompt bool
23 // git is used by default. URL installations can specify `--hg`. For already installed modules
24 // and VPM modules that specify a different VCS in their `v.mod`, the VCS is validated separately.
25 vcs VCS
26 logger &log.Logger
27}
28
29fn init_settings() VpmSettings {
30 args := os.args[1..]
31 opts := cmdline.only_options(args)
32 cmds := cmdline.only_non_options(args)
33
34 global_vmodules_path := os.vmodules_dir()
35 mut vmodules_path := global_vmodules_path.clone()
36 is_local := '-l' in opts || '--local' in opts
37 if is_local {
38 wrkdir := os.getwd()
39 mut mcache := vmod.get_cache()
40 vmod_file_location := mcache.get_by_folder(wrkdir)
41 project_root_dir := if vmod_file_location.vmod_file.len == 0 {
42 wrkdir
43 } else {
44 vmod_file_location.vmod_folder
45 }
46 vmodules_path = os.join_path(project_root_dir, 'modules')
47 verbose_println('init_settings, local installation, wrkdir: ${wrkdir} | project_root_dir: ${project_root_dir} | vmodules_path: ${vmodules_path}')
48 }
49 verbose_println('init_settings, final is_local: ${is_local} | vmodules_path: `${vmodules_path}`')
50
51 is_no_inc := os.getenv('VPM_NO_INCREMENT') != ''
52 is_dbg := os.getenv('VPM_DEBUG') != ''
53 is_ci := os.getenv('CI') != ''
54
55 mut logger := &log.Log{}
56 logger.set_output_stream(os.stderr())
57 if is_dbg {
58 logger.set_level(.debug)
59 }
60 if !is_ci && !is_dbg {
61 // Log by default, but only in the global location, no matter if --local was passed:
62 cache_path := os.join_path(global_vmodules_path, '.cache')
63 os.mkdir_all(cache_path, mode: 0o700) or { panic(err) }
64 logger.set_output_path(os.join_path(cache_path, 'vpm.log'))
65 }
66
67 return VpmSettings{
68 is_help: '-h' in opts || '--help' in opts || 'help' in cmds
69 is_once: '--once' in opts
70 is_verbose: '-v' in opts || '--verbose' in opts
71 is_force: '-f' in opts || '--force' in opts
72 is_local: is_local
73 server_urls: get_server_urls_from_args(args)
74 mirror_urls: get_mirror_urls_from_args(args)
75 vcs: if '--hg' in opts { .hg } else { .git }
76 vmodules_path: vmodules_path
77 tmp_path: os.join_path(os.vtmp_dir(), 'vpm_modules')
78 no_dl_count_increment: is_ci || is_no_inc
79 fail_on_prompt: os.getenv('VPM_FAIL_ON_PROMPT') != ''
80 logger: logger
81 }
82}
83
84fn get_server_urls_from_args(args []string) []string {
85 mut server_urls := []string{}
86 server_urls << cmdline.options(args, '-server-url')
87 server_urls << cmdline.options(args, '--server-url')
88 server_urls << cmdline.options(args, '--server-urls')
89 return unique_server_urls(server_urls)
90}
91
92fn get_mirror_urls_from_args(args []string) []string {
93 mut mirror_urls := []string{}
94 mirror_urls << cmdline.options(args, '-m')
95 mirror_urls << cmdline.options(args, '--mirror')
96 return unique_server_urls(mirror_urls)
97}
98
99fn unique_server_urls(urls []string) []string {
100 mut unique_urls := []string{}
101 for raw_url in urls {
102 url := normalize_server_url(raw_url)
103 if url == '' || url in unique_urls {
104 continue
105 }
106 unique_urls << url
107 }
108 return unique_urls
109}
110
111fn normalize_server_url(url string) string {
112 return url.trim_space().trim_string_right('/')
113}
114