Commit 082f1366 authored by adam j hartz's avatar adam j hartz

allow plugins to add aliases

parent e589a3bf
......@@ -254,10 +254,11 @@ def which(args, stdin=None, stdout=None, stderr=None):
exts = None
failures = []
settings = builtins.__tako_env__['TAKO_SETTINGS']
aliases = settings.aliases
for arg in pargs.args:
nmatches = 0
# skip alias check if user asks to skip
aliases = builtins.__tako_env__['TAKO_SETTINGS'].aliases
if (arg in aliases and not pargs.skip):
if pargs.plain or not pargs.verbose:
if isinstance(aliases[arg], list):
......@@ -265,10 +266,24 @@ def which(args, stdin=None, stdout=None, stderr=None):
else:
print(arg, file=stdout)
else:
print("aliases['{}'] = {}".format(arg, aliases[arg]), file=stdout)
print("$TAKO_SETTINGS.aliases['{}'] = {}".format(arg, aliases[arg]), file=stdout)
nmatches += 1
if not pargs.all:
continue
else:
for plugin in settings.plugins:
local_aliases = settings.plugins[plugin].aliases
if arg in local_aliases and not pargs.skip:
if pargs.plain or not pargs.verbose:
if isinstance(local_aliases[arg], list):
print(' '.join(local_aliases[arg]), file=stdout)
else:
print(arg, file=stdout)
else:
print("$TAKO_SETTINGS.plugins.%s.aliases['{}'] = {}".format(repr(plugin.lower()), arg, local_aliases[arg]), file=stdout)
nmatches += 1
if not pargs.all:
continue
# which.whichgen gives the nicest 'verbose' output if PATH is taken
# from os.environ so we temporarily override it with
# __xosnh_env__['PATH']
......@@ -291,7 +306,7 @@ def which(args, stdin=None, stdout=None, stderr=None):
else:
print('{} not in $PATH'.format(', '.join(failures)), file=stderr, end='')
if not pargs.skip:
print(' or $TAKO_SETTINGS.aliases', file=stderr, end='')
print(' or aliases', file=stderr, end='')
print('', end='\n')
return len(failures)
......
......@@ -439,6 +439,12 @@ def run_subproc(cmds, captured=False):
else:
alias = aliases.get(cmd[0], None)
if alias is None: # look in plugins for the alias
for plugin in settings.plugins:
alias = settings.plugins[plugin].aliases.get(cmd[0], None)
if alias is not None:
break
procinfo['alias'] = alias
if alias is None:
binary_loc = locate_binary(cmd[0])
......
......@@ -1222,7 +1222,9 @@ class CommandsCache(abc.Mapping):
self._path_checksum = path_hash
# did aliases change?
settings = builtins.__tako_env__['TAKO_SETTINGS']
alss = getattr(settings, 'aliases', set())
alss = set(getattr(settings, 'aliases', set()))
for i in settings.plugins:
alss |= set(settings.plugins[i].aliases.keys())
al_hash = hash(frozenset(alss))
cache_valid = cache_valid and al_hash == self._alias_checksum
self._alias_checksum = al_hash
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment