@@ -37,6 +37,42 @@ def get_actual_python_version():
3737 return sys .version_info [:2 ]
3838 except Exception :
3939 return sys .version_info [:2 ]
40+
41+ def upgrade (args , core ):
42+ """Handler for the upgrade command."""
43+ package_name = args .package_name [0 ] if args .package_name else 'omnipkg'
44+
45+ # Handle self-upgrade as a special case
46+ if package_name .lower () == 'omnipkg' :
47+ return core .smart_upgrade (
48+ version = args .version ,
49+ force = args .force ,
50+ skip_dev_check = args .force_dev
51+ )
52+
53+ # For non-omnipkg packages: temporarily switch strategy, install, then restore
54+ original_strategy = core .config .get ('install_strategy' , 'stable-main' )
55+
56+ try :
57+ if original_strategy != 'latest-active' :
58+ safe_print (f" - 🔄 Temporarily switching to 'latest-active' strategy..." )
59+ core .config_manager .set ('install_strategy' , 'latest-active' )
60+ # Also update the in-memory config so smart_install sees it
61+ core .config ['install_strategy' ] = 'latest-active'
62+
63+ safe_print (f"🔄 Upgrading '{ package_name } ' to latest version..." )
64+
65+ return core .smart_install (
66+ packages = [package_name ],
67+ force_reinstall = True
68+ )
69+
70+ finally :
71+ # ALWAYS restore the original strategy
72+ if original_strategy != 'latest-active' :
73+ core .config_manager .set ('install_strategy' , original_strategy )
74+ core .config ['install_strategy' ] = original_strategy
75+ safe_print (f" - ✅ Strategy restored to '{ original_strategy } '" )
4076
4177def run_demo_with_enforced_context (
4278 source_script_path : Path ,
@@ -435,10 +471,11 @@ def create_parser():
435471 prune_parser .add_argument ('--keep-latest' , type = int , metavar = 'N' , help = _ ('Keep N most recent bubbled versions' ))
436472 prune_parser .add_argument ('--yes' , '-y' , dest = 'force' , action = 'store_true' , help = _ ('Skip confirmation' ))
437473 upgrade_parser = subparsers .add_parser ('upgrade' , help = _ ('Upgrade omnipkg or other packages to the latest version' ))
438- upgrade_parser .add_argument ('package ' , nargs = '? ' , default = 'omnipkg' , help = _ ( 'Package to upgrade (defaults to omnipkg itself)' ) )
439- upgrade_parser .add_argument ('--version' , help = _ ( 'Specify a version to upgrade/downgrade to' ) )
474+ upgrade_parser .add_argument ('package_name ' , nargs = '* ' , default = [ 'omnipkg' ] , help = 'Package to upgrade (defaults to omnipkg itself)' )
475+ upgrade_parser .add_argument ('--version' , help = '(For omnipkg self-upgrade only) Specify a target version' )
440476 upgrade_parser .add_argument ('--yes' , '-y' , dest = 'force' , action = 'store_true' , help = _ ('Skip confirmation prompt' ))
441477 upgrade_parser .add_argument ('--force-dev' , action = 'store_true' , help = _ ('Force upgrade even in a developer environment (use with caution)' ))
478+ upgrade_parser .set_defaults (func = upgrade ) # CRITICAL: This connects the handler!
442479 return parser
443480
444481def print_header (title ):
@@ -563,6 +600,8 @@ def main():
563600 else :
564601 parser .print_help ()
565602 return 1
603+ elif args .command == 'upgrade' :
604+ return upgrade (args , pkg_instance )
566605 elif args .command == 'swap' :
567606 if not args .target :
568607 safe_print (_ ('❌ Error: You must specify what to swap.' ))
@@ -855,13 +894,23 @@ def run_and_stream(cmd_list):
855894 elif args .command == 'run' :
856895 return execute_run_command (args .script_and_args , cm , verbose = args .verbose )
857896 elif args .command == 'upgrade' :
858- if args .package .lower () == 'omnipkg' :
859- return pkg_instance .smart_upgrade (version = args .version , force = args .force , skip_dev_check = args .force_dev )
897+ package_name = args .package_name [0 ] if args .package_name else 'omnipkg'
860898
861- else :
862- # Upgrading other packages is just a reinstall of the latest
863- safe_print (_ ("Redirecting to smart_install to get the latest version of '{}'..." ).format (args .package ))
864- return pkg_instance .smart_install ([args .package ], force_reinstall = True )
899+ # Handle self-upgrade as a special case
900+ if package_name .lower () == 'omnipkg' :
901+ return pkg_instance .smart_upgrade (
902+ version = args .version ,
903+ force = args .force ,
904+ skip_dev_check = args .force_dev
905+ )
906+
907+ # For all other packages, use smart_install with a temporary strategy override
908+ safe_print (f"🔄 Upgrading '{ package_name } ' to latest version..." )
909+ return pkg_instance .smart_install (
910+ packages = [package_name ],
911+ force_reinstall = True ,
912+ override_strategy = 'latest-active' # Temporarily use this strategy for the upgrade
913+ )
865914 else :
866915 parser .print_help ()
867916 safe_print (_ ("\n 💡 Did you mean 'omnipkg config set language <code>'?" ))
0 commit comments