diff --git a/install_gadget.py b/install_gadget.py index af2794e..5cfc395 100755 --- a/install_gadget.py +++ b/install_gadget.py @@ -163,19 +163,8 @@ for custom_file_name in functools.reduce( operator.add, failed = [] -if args.update_gadget_config: - with open( install.GetGadgetConfigFile( vimspector_base ), 'r' ) as f: - all_adapters = json.load( f ).get( 'adapters', {} ) -else: - all_adapters = {} - -# Include "built-in" adapter for multi-session mode -all_adapters.update( { - 'multi-session': { - 'port': '${port}', - 'host': '${host}' - }, -} ) +all_adapters = installer.ReadAdapters( + read_existing = args.update_gadget_config ) for name, gadget in gadgets.GADGETS.items(): if not gadget.get( 'enabled', True ): @@ -197,17 +186,12 @@ for name, gadget in gadgets.GADGETS.items(): for name, gadget in CUSTOM_GADGETS.items(): installer.InstallGagdet( name, gadget, failed, all_adapters ) -adapter_config = json.dumps ( { 'adapters': all_adapters }, - indent=2, - sort_keys=True ) - if args.no_gadget_config: print( "" ) print( "Would write the following gadgets: " ) - print( adapter_config ) + installer.WriteAdapters( all_adapters, to_file = sys.stdout ) else: - with open( install.GetGadgetConfigFile( vimspector_base ), 'w' ) as f: - f.write( adapter_config ) + installer.WriteAdapters( all_adapters ) if failed: raise RuntimeError( 'Failed to install gadgets: {}'.format( diff --git a/python3/vimspector/installer.py b/python3/vimspector/installer.py index 9e42fee..8bf78b0 100644 --- a/python3/vimspector/installer.py +++ b/python3/vimspector/installer.py @@ -211,6 +211,38 @@ def InstallGagdet( name, gadget, failed, all_adapters ): print( "FAILED installing {}: {}".format( name, e ) ) +def ReadAdapters( read_existing = True ): + if read_existing: + with open( install.GetGadgetConfigFile( options.vimspector_base ), + 'r' ) as f: + all_adapters = json.load( f ).get( 'adapters', {} ) + else: + all_adapters = {} + + # Include "built-in" adapter for multi-session mode + all_adapters.update( { + 'multi-session': { + 'port': '${port}', + 'host': '${host}' + }, + } ) + + return all_adapters + + +def WriteAdapters( all_adapters, to_file=None ): + adapter_config = json.dumps ( { 'adapters': all_adapters }, + indent=2, + sort_keys=True ) + + if to_file: + to_file.write( adapter_config ) + else: + with open( install.GetGadgetConfigFile( options.vimspector_base ), + 'w' ) as f: + f.write( adapter_config ) + + @contextlib.contextmanager def CurrentWorkingDir( d ): cur_d = os.getcwd()