88# MCPP_VERSION — pin a specific version (default: latest)
99# MCPP_PREFIX — install root (default: $HOME/.mcpp)
1010# MCPP_NO_PATH — set to skip shell-rc PATH editing
11+ # MCPP_NO_COMPLETION — set to skip shell-rc completion editing
1112#
1213# Layout afterwards (PREFIX = $HOME/.mcpp by default):
1314# $PREFIX/bin/{mcpp,xlings} ← binary + pinned bundled xlings
15+ # $PREFIX/config/shell/ ← sourceable shell completion scripts
1416# $PREFIX/registry/ ← seeded on first `mcpp` invocation
1517# $PREFIX/... ← mcpp's full self-contained tree
1618#
@@ -21,6 +23,8 @@ set -euo pipefail
2123REPO=" mcpp-community/mcpp"
2224VERSION=" ${MCPP_VERSION:- latest} "
2325PREFIX=" ${MCPP_PREFIX:- $HOME / .mcpp} "
26+ shell_name=" ${SHELL:- } "
27+ shell_name=" ${shell_name##*/ } "
2428
2529# ---- platform detection ---------------------------------------------------
2630# OS+arch → release asset platform tag. Linux reports the arch as aarch64;
@@ -126,22 +130,46 @@ mkdir -p "$PREFIX"
126130echo " :: Extracting to $PREFIX "
127131tar -xzf " $WORK /mcpp.tar.gz" -C " $PREFIX " --strip-components=1
128132
133+ # Older pinned releases predate completion. Probe help without initialization.
134+ completion_available=0
135+ if " $PREFIX /bin/mcpp" self --help 2> /dev/null | grep ' completion' > /dev/null; then
136+ MCPP_HOME=" $PREFIX " " $PREFIX /bin/mcpp" self completion
137+ completion_available=1
138+ fi
139+
129140# ---- PATH integration -----------------------------------------------------
130- if [[ -z " ${MCPP_NO_PATH:- } " ]]; then
131- rc=" "
132- case " ${SHELL##*/ } " in
133- bash) rc=" $HOME /.bashrc" ;;
134- zsh) rc=" ${ZDOTDIR:- $HOME } /.zshrc" ;;
135- fish) rc=" $HOME /.config/fish/config.fish" ;;
141+ rc=" "
142+ case " $shell_name " in
143+ bash) rc=" $HOME /.bashrc" ;;
144+ zsh) rc=" ${ZDOTDIR:- $HOME } /.zshrc" ;;
145+ fish) rc=" ${XDG_CONFIG_HOME:- $HOME / .config} /fish/config.fish" ;;
146+ pwsh)
147+ if command -v pwsh > /dev/null 2>&1 ; then
148+ rc=$( pwsh -NoProfile -Command ' $PROFILE.CurrentUserAllHosts' )
149+ fi ;;
150+ esac
151+ shell_quote () {
152+ local value=" $1 "
153+ case " $shell_name " in
154+ pwsh) value=" ${value// \' / \'\' } " ;;
155+ fish)
156+ value=" ${value// \\ / \\\\ } "
157+ value=" ${value// \' / \\\' } " ;;
158+ * ) value=" ${value// \' / \'\\\'\' } " ;;
136159 esac
160+ printf " '%s'" " $value "
161+ }
162+ if [[ -z " ${MCPP_NO_PATH:- } " ]]; then
137163 if [[ -n " $rc " ]]; then
138- if [[ " ${SHELL##*/ } " == " fish" ]]; then
139- line=" set -gx PATH \" $PREFIX /bin\" \$ PATH"
140- else
141- line=" export PATH=\" $PREFIX /bin:\$ PATH\" "
142- fi
164+ quoted_bin=$( shell_quote " $PREFIX /bin" )
165+ case " $shell_name " in
166+ fish) line=" set -gx PATH $quoted_bin \$ PATH" ;;
167+ pwsh) line=" \$ env:PATH = $quoted_bin + [IO.Path]::PathSeparator + \$ env:PATH" ;;
168+ * ) line=" export PATH=$quoted_bin :\"\$ PATH\" " ;;
169+ esac
143170 mkdir -p " $( dirname " $rc " ) "
144- if ! grep -Fqs " $PREFIX /bin" " $rc " 2> /dev/null; then
171+ if ! grep -Fqx " $line " " $rc " 2> /dev/null \
172+ && ! grep -Fqs " $PREFIX /bin" " $rc " 2> /dev/null; then
145173 printf ' \n# mcpp\n%s\n' " $line " >> " $rc "
146174 echo " :: Added $PREFIX /bin to PATH via $rc "
147175 else
@@ -153,13 +181,39 @@ if [[ -z "${MCPP_NO_PATH:-}" ]]; then
153181 fi
154182fi
155183
184+ # ---- completion integration ----------------------------------------------
185+ if [[ -z " ${MCPP_NO_COMPLETION:- } " && " $completion_available " == 1 ]]; then
186+ if [[ -n " $rc " ]]; then
187+ extension=" $shell_name "
188+ [[ " $shell_name " == pwsh ]] && extension=ps1
189+ script=" $PREFIX /config/shell/mcpp.$extension "
190+ quoted_script=$( shell_quote " $script " )
191+ if [[ " $shell_name " == pwsh ]]; then
192+ line=" if (Test-Path -LiteralPath $quoted_script ) { . $quoted_script }"
193+ elif [[ " $shell_name " == fish ]]; then
194+ line=" test -f $quoted_script ; and source $quoted_script "
195+ else
196+ line=" [ ! -f $quoted_script ] || source $quoted_script "
197+ fi
198+ mkdir -p " $( dirname " $rc " ) "
199+ if ! grep -Fqx " $line " " $rc " 2> /dev/null; then
200+ printf ' \n# mcpp completion\n%s\n' " $line " >> " $rc "
201+ echo " :: Enabled completion via $rc "
202+ fi
203+ else
204+ echo " :: Completion scripts: $PREFIX /config/shell/ (source the script for your shell)"
205+ fi
206+ fi
207+
156208# ---- verify install -------------------------------------------------------
157209echo
158210" $PREFIX /bin/mcpp" --version
159211echo
160212echo " ✓ mcpp installed at $PREFIX "
161- if [[ -n " ${rc:- } " ]]; then
162- echo " Open a new shell (or 'source $rc ') and run: mcpp --help"
213+ if [[ -n " ${rc:- } " && -f " $rc " ]]; then
214+ load_command=" source $( shell_quote " $rc " ) "
215+ [[ " $shell_name " == pwsh ]] && load_command=" . $( shell_quote " $rc " ) "
216+ echo " Open a new shell (or $load_command ) and run: mcpp --help"
163217else
164218 echo " Add $PREFIX /bin to your PATH, then run: mcpp --help"
165219fi
0 commit comments