commit:     561e6cb5b3fefb6c248056aa4d790276433856b3
Author:     Florian Schmaus <flow <AT> gentoo <DOT> org>
AuthorDate: Fri Jun 24 07:02:14 2022 +0000
Commit:     Florian Schmaus <flow <AT> gentoo <DOT> org>
CommitDate: Fri Jun 24 08:16:33 2022 +0000
URL:        https://gitweb.gentoo.org/proj/eselect-java.git/commit/?id=561e6cb5

Add "eselect java-vm update"

Bug: https://bugs.gentoo.org/853928
Signed-off-by: Florian Schmaus <flow <AT> gentoo.org>

 NEWS                           |  3 +++
 src/modules/java-vm.eselect.in | 55 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/NEWS b/NEWS
index 6c331a2..d8ea978 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,6 @@
+NEXT:
+    * Add "eselect java-vm update" (#853928)
+
 0.4.3:
     * install jpackage symlink
 

diff --git a/src/modules/java-vm.eselect.in b/src/modules/java-vm.eselect.in
index e332c5a..c7d5a82 100644
--- a/src/modules/java-vm.eselect.in
+++ b/src/modules/java-vm.eselect.in
@@ -177,3 +177,58 @@ set_symlink() {
                die -q "Target \"${1}\" doesn't appear to be valid!"
        fi
 }
+
+describe_update() {
+       echo "Automatically update the Java system VM"
+}
+
+do_update() {
+       local targets
+       targets=( $(find_targets) )
+
+       if [[ ${#targets[@]} -eq 0 ]]; then
+               echo "No installed Java VMs found, can not update"
+               return
+       fi
+
+       if [[ -e "${VM_SYSTEM}" ]]; then
+               local current_system_vm_name=$(sym_to_vm "${VM_SYSTEM}")
+               echo "Current Java system VM ${current_system_vm_name} is 
valid, no need to update"
+               return
+       fi
+
+       local new_target old_system_vm_name
+       if [[ -L "${VM_SYSTEM}" ]]; then
+               # There exists a Java system VM symlink that has become stale,
+               # try to find another available VM with the same slot.
+               old_system_vm_name=$(sym_to_vm "${VM_SYSTEM}")
+
+               local old_system_vm_slot="${old_system_vm_name##*-}"
+
+               local target
+               for target in "${targets[@]}"; do
+                       local target_slot="${target##*-}"
+                       if [[ ${target_slot} -eq ${old_system_vm_slot} ]]; then
+                               new_target="${target}"
+                               break
+                       fi
+               done
+       fi
+
+       if [[ -z "${new_target}" ]]; then
+               # There is no Java system VM symlink or we could not find a
+               # slot-matching replacement. But there are potential targets,
+               # simply choose the first.
+               # TODO: We could get more sophisticated here to select the 
"best"
+               # target, but that is far from trivial.
+               new_target="${targets[0]}"
+       fi
+
+       local from_vm_text=""
+       if [[ -n "${old_system_vm_name}" ]]; then
+               from_vm_text="from ${old_system_vm_name} "
+       fi
+
+       echo "Updating Java system VM ${from_vm_text}to ${new_target}"
+       set_symlink "${new_target}" "${VM_SYSTEM}"
+}

Reply via email to