Open Bug 1670288 Opened 5 years ago Updated 4 months ago

Bootstrap mach completion scripts

Categories

(Firefox Build System :: Bootstrap Configuration, enhancement, P5)

enhancement

Tracking

(Not tracked)

People

(Reporter: ahal, Unassigned)

References

Details

Bug 1519990 introduced new tab completion script generation, it can be used like ./mach mach-completion <shell> -f <path/to/put/file>.

It would be cool if we could automatically detect shell / environment and put the completion script in the correct spot. We could also set up a cron task to periodically re-generate it (as it will become out of date over time).

Not sure if this logic should belong in mozboot or mach core, but filing here for now.

Depends on: 1519990

It would be cool if we could automatically detect shell / environment and put the completion script in the correct spot. We could also set up a cron task to periodically re-generate it (as it will become out of date over time).

Seems like bootstrap is the right place for this. Since bootstrap would typically be run on a semi-regular basis, that would erase the need for periodic re-generation -- if the script gets out of date, just re-run bootstrap as you would to fix any other issue with your machine getting out-of-sync with the mach source.

Yeah, I think it should hook into mach bootstrap no matter what. But mozboot could call into code that lives under mach. See also my vision for bootstrapping in bug 1526021.

Oh, yeah, I don't care about the exact directory it lives under :) Still, IMO the cron thing does not seem necessary.

True, personally I avoid mach bootstrap since it does so much and takes so long.. so I'll personally keep it as a cron task. But agree mach bootstrap can just focus on installing the scripts and not worry about setting up cron.

Priority: P4 → P5

How about a git hook that is generated on mach bootstrap? If wanted, I would implement this.

(In reply to Michael van Straten [:michael] from comment #5)

How about a git hook that is generated on mach bootstrap? If wanted, I would implement this.

Sure.

So I tinkered around a little with this and came up with the following "dynamic" completion script for fish shells. It loads and executes the correct completion script based on the mach binary path you give it or the current tree you are in.

# This is base on the `get_state_dir` in python/mach/mach/utils.py
function __mach_get_state_dir -a source_dir
    set -l dir_basename (path basename "$source_dir")
    set -l dir_hash (string sub -l 12 (sha256 -s "$source_dir"))
    echo "$HOME/.mozbuild/srcdirs/$dir_basename-$dir_hash"
end

function __mach_find_completion_script -a executable_path
    # Prefer the directory hinted by the executable path (./mach, ../mach, etc.)
    set -l search_dir (path dirname $executable_path)
    set -l state_dir (__mach_get_state_dir $search_dir)
    if test -d "$state_dir"
        echo "$state_dir/completions/mach.fish"
        return
    end

    # Otherwise, walk upward from the current working directory
    set search_dir (pwd)
    while test "$search_dir" != /
        set state_dir (__mach_get_state_dir "$search_dir")
        if test -d "$state_dir"
            echo "$state_dir/completions/mach.fish"
            return
        end
        set search_dir (path dirname "$search_dir")
    end

    return 1
end

function __mach_dynamic_completion
    set -l command_name (commandline -opc)[1]
    set -l executable_path (realpath "$(command -v "$command_name")")

    set -l com_script (__mach_find_completion_script $executable_path); or return

    # Execute the completion script found
    fish --no-config --private -c "source $com_script; complete -C \"$(commandline)\""
end

complete -c mach -f -a "(__mach_dynamic_completion)"

Running mach bootstrap would write this completion script to a fish completion directory and would then trigger the hook manually once to generate the completion for the work tree.

For the hook phase I landed on post-checkout, it works nicely and also generates the completion if you add a new work tree (so you only have to call mach bootstrap once for the completion to work in every work tree). The issue is that git only supports one hook per phase, and all work trees share the same hooks. If someone already has a post-checkout hook in their moz-central checkout, we probably just want to skip writing the hook with a warning message being printed to the console.

Besides that, I haven't tried to implement the same type of dynamic completion for bash.

You need to log in before you can comment on or make changes to this bug.