| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import sync.pool |
| 5 | import v.help |
| 6 | |
| 7 | struct UpdateSession { |
| 8 | idents []string |
| 9 | } |
| 10 | |
| 11 | pub struct UpdateResult { |
| 12 | mut: |
| 13 | success bool |
| 14 | } |
| 15 | |
| 16 | fn vpm_update(query []string) { |
| 17 | if settings.is_help { |
| 18 | help.print_and_exit('update') |
| 19 | } |
| 20 | idents := if query.len == 0 { get_installed_modules() } else { query.clone() } |
| 21 | mut pp := pool.new_pool_processor(callback: update_module) |
| 22 | ctx := UpdateSession{idents} |
| 23 | pp.set_shared_context(ctx) |
| 24 | pp.work_on_items(idents) |
| 25 | mut errors := 0 |
| 26 | for res in pp.get_results[UpdateResult]() { |
| 27 | if !res.success { |
| 28 | errors++ |
| 29 | continue |
| 30 | } |
| 31 | } |
| 32 | if errors > 0 { |
| 33 | exit(1) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn update_module(mut pp pool.PoolProcessor, idx int, _wid int) &UpdateResult { |
| 38 | ident := pp.get_item[string](idx) |
| 39 | // Usually, the module `ident`ifier. `get_name_from_url` is only relevant for `v update <module_url>`. |
| 40 | name := get_name_from_url(ident) or { ident } |
| 41 | install_path := get_path_of_existing_module(ident) or { |
| 42 | vpm_error('failed to find path for `${name}`.', verbose: true) |
| 43 | return &UpdateResult{} |
| 44 | } |
| 45 | vcs := vcs_used_in_dir(install_path) or { |
| 46 | vpm_error('failed to find version control system for `${name}`.', verbose: true) |
| 47 | return &UpdateResult{} |
| 48 | } |
| 49 | vcs.is_executable() or { |
| 50 | vpm_error(err.msg()) |
| 51 | return &UpdateResult{} |
| 52 | } |
| 53 | args := vcs_info[vcs].args |
| 54 | cmd := [vcs.str(), args.path, os.quoted_path(install_path), args.update].join(' ') |
| 55 | vpm_log(@FILE_LINE, @FN, 'cmd: ${cmd}') |
| 56 | println('Updating module `${name}` in `${fmt_mod_path(install_path)}`...') |
| 57 | res := os.execute_opt(cmd) or { |
| 58 | vpm_error('failed to update module `${name}` in `${install_path}`.', details: err.msg()) |
| 59 | return &UpdateResult{} |
| 60 | } |
| 61 | vpm_log(@FILE_LINE, @FN, 'cmd output: ${res.output.trim_space()}') |
| 62 | if res.output.contains('Already up to date.') { |
| 63 | println('Skipped module `${ident}`. Already up to date.') |
| 64 | } else { |
| 65 | println('Updated module `${ident}`.') |
| 66 | } |
| 67 | // Don't bail if the download count increment has failed. |
| 68 | increment_module_download_count(name, '') or { vpm_error(err.msg(), verbose: true) } |
| 69 | ctx := unsafe { &UpdateSession(pp.get_shared_context()) } |
| 70 | vpm_log(@FILE_LINE, @FN, 'ident: ${ident}; ctx: ${ctx}') |
| 71 | resolve_dependencies(get_manifest(install_path), ctx.idents) |
| 72 | return &UpdateResult{true} |
| 73 | } |
| 74 | |