Skip to content

Commit

Permalink
Generate & install completion scripts in build system
Browse files Browse the repository at this point in the history
The previous commit added a means to generating the completion scripts
and this one plugs that into the build system.

A new build option 'install_completions' has been introduced. Set to
'True' by default.

Completions for bash and fish use pkg-config for getting the preferred
install locations for the completions. If the packages are not
available, fallbacks are in-place.

The 'completion' subdir has been kept to work around the ideology of
Meson that does not allow creating/outputing files in subdirectories nor
using the output of custom_target() in install_data().

containers#840
  • Loading branch information
HarryMichal committed Feb 21, 2022
1 parent d69ce67 commit bafbbe8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 107 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ database, etc..
See our guides on
[installing & getting started](https://containertoolbx.org/install/) with
Toolbox and [Linux distro support](https://containertoolbx.org/distros/).

100 changes: 0 additions & 100 deletions completion/bash/toolbox

This file was deleted.

41 changes: 41 additions & 0 deletions completion/generate_completions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
#
# Copyright © 2022 Ondřej Míchal
# Copyright © 2022 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import subprocess
import sys

if len(sys.argv) != 3:
print('{}: wrong arguments'.format(sys.argv[0]), file=sys.stderr)
print('Usage: {} [SOURCE DIR] [COMPLETION TYPE]'.format(sys.argv[0]), file=sys.stderr)
print()
print("SOURCE DIR is path to the Toolbox Go source code")
print("COMPLETION TYPE is either 'bash', 'zsh' or 'fish'")
sys.exit(1)

source_dir = sys.argv[1]
completion_type = sys.argv[2]

try:
os.chdir(source_dir)
output = subprocess.run(['go', 'run', '.', 'completion', completion_type], check=True)
except subprocess.CalledProcessError as e:
print('{}: go run returned non-zero exit status {}'.format(sys.argv[0], e.returncode), file=sys.stderr)
sys.exit(e.returncode)

sys.exit(0)
36 changes: 36 additions & 0 deletions completion/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
generate_completions_program = find_program('generate_completions.py')

if bash_completion.found()
bash_comp_dir = bash_completion.get_pkgconfig_variable('completionsdir')
else
bash_comp_dir = get_option('datadir') / 'bash-completion' / 'completions'
message('bash-completion not found: using', get_option('prefix') / bash_comp_dir, 'as a falback install directory')
endif

if fish.found()
fish_comp_dir = fish.get_pkgconfig_variable('completionsdir')
else
fish_comp_dir = get_option('datadir') / 'fish' / 'completions'
message('fish not found: using', get_option('prefix') / fish_comp_dir, 'as a fallback install directory')
endif

completion_bash = custom_target('bash-completion',
capture: true,
command: [generate_completions_program, meson.global_source_root() / 'src', 'bash'],
install: true,
install_dir: bash_comp_dir,
output: 'toolbox')

completion_zsh = custom_target('zsh-completion',
capture: true,
command: [generate_completions_program, meson.global_source_root() / 'src', 'zsh'],
install: true,
install_dir: get_option('datadir') / 'zsh' / 'site_functions',
output: '_toolbox')

completion_fish = custom_target('fish-completion',
capture: true,
command: [generate_completions_program, meson.global_source_root() / 'src', 'fish'],
install: true,
install_dir: fish_comp_dir,
output: 'toolbox.fish')
11 changes: 4 additions & 7 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ shellcheck = find_program('shellcheck', required: false)
skopeo = find_program('skopeo', required: false)

bash_completion = dependency('bash-completion', required: false)
fish = dependency('fish', required: false)

migration_path_for_coreos_toolbox = get_option('migration_path_for_coreos_toolbox')
profiledir = get_option('profile_dir')
Expand All @@ -32,13 +33,6 @@ if tmpfilesdir == '' or not fs.exists('/run/.containerenv')
endif
endif

if bash_completion.found()
install_data(
'completion/bash/toolbox',
install_dir: bash_completion.get_variable(pkgconfig: 'completionsdir')
)
endif

if not skopeo.found()
message('Running system tests requires Skopeo for OCI image manipulation.')
endif
Expand Down Expand Up @@ -70,5 +64,8 @@ subdir('data')
subdir('doc')
subdir('profile.d')
subdir('src')
if get_option('install_completions')
subdir('completion')
endif

meson.add_install_script('meson_post_install.py')
7 changes: 7 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ option(
description: 'Directory for system-wide tmpfiles.d(5) files',
type: 'string',
)

option(
'install_completions',
description: 'Install bash, zsh and fish command completions',
type: 'boolean',
value: true,
)

0 comments on commit bafbbe8

Please sign in to comment.