[Help wanted] Setting vi/view/vim via alternatives

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I'm currently trying to rewrite the current shell aliases for making
Vi/View/Vim use the correct compiled binary based on which Vim package
is installed. The current aliases have several downsides (don't work
with sudo, runs in subshell) so I got a recommendation for
'alternatives' which should solve all those issues.

But currently I'm stuck and I don't know how to debug - the current
patch (attached) should solve package installation, its upgrade and
removal via %post and %preun scriptlets, but whatever I do, the links
don't exist after package upgrade.

For debugging I used 'ls' in scriptlets, and the links existed at the
time the transaction was leaving the scriptlets. But the links don't
exist after the end of dnf transaction...

Would anyone mind helping me?

Thank you in advance,

Zdenek

-- 
Zdenek Dohnal
Software Engineer
Red Hat Czech - Brno TPB-C

diff --git a/view_wrapper b/view_wrapper
new file mode 100644
index 0000000..f4c9b23
--- /dev/null
+++ b/view_wrapper
@@ -0,0 +1,3 @@
+#!/usr/bin/bash
+
+/usr/bin/vim -R "$@"
diff --git a/vim.csh b/vim.csh
deleted file mode 100644
index 47df221..0000000
--- a/vim.csh
+++ /dev/null
@@ -1,20 +0,0 @@
-# we need to use which twice - first for checking if
-# the command doesn't fail, the use it if doesn't fail
-set vim_cond = `command -v vim`
-set vi_cond = `command -v vi`
-
-switch ( $vim_cond-$vi_cond )
-  case /usr/bin/vim-/usr/bin/vi:
-      # apply only when founded vim and vi are in expected dirs from distro
-      alias vi vim
-      alias view 'vim -R'
-      breaksw
-  case -/usr/bin/vi:
-      # apply only if founded vi is in expected dir from distro
-      alias vim vi
-      breaksw
-endsw
-
-# just in case
-unset vim_cond
-unset vi_cond
diff --git a/vim.fish b/vim.fish
deleted file mode 100644
index a35220d..0000000
--- a/vim.fish
+++ /dev/null
@@ -1,25 +0,0 @@
-# This will avoid user defined aliases and possibly stuff defined earlier in the PATH.
-# Redirecting is for the case when the binary is missing.
-set vim_cond (command -v vim)
-set vi_cond (command -v vi)
-
-switch "$vim_cond-$vi_cond"
-  case /usr/bin/vim-/usr/bin/vi
-      # apply only if founded vim and vi are in the expected dir from distro
-      function vi
-        command vim $argv
-      end
-
-      function view
-        command vim -R $argv
-      end
-  case -/usr/bin/vi
-      # apply only when no vim is installed and founded vi is in the expected dir from distro
-      function vim
-        command vi $argv
-      end
-end
-
-# just in case
-set -e vim_cond
-set -e vi_cond
diff --git a/vim.sh b/vim.sh
deleted file mode 100644
index 2616693..0000000
--- a/vim.sh
+++ /dev/null
@@ -1,32 +0,0 @@
-__vi_internal_vim_alias()
-(
-  # run vim if installed
-  test -f /usr/bin/vim && exec /usr/bin/vim "$@"
-
-  # run vi otherwise
-  test -f /usr/bin/vi && exec /usr/bin/vi "$@"
-)
-
-__view_internal_vim_alias()
-(
-  # run vim -R instead of view if vim installed
-  test -f /usr/bin/vim && exec /usr/bin/vim -R "$@"
-
-  # run view otherwise
-  test -f /usr/bin/view && exec /usr/bin/view "$@"
-)
-
-
-if [ -n "${BASH_VERSION-}" -o -n "${KSH_VERSION-}" -o -n "${ZSH_VERSION-}" ]; then
-  # This will avoid user defined aliases
-  case "$(command -v vim)-$(command -v vi)" in
-    "/usr/bin/vim-/usr/bin/vi" | "-/usr/bin/vi")
-        # apply only when founded vim and vi are in expected dirs from distro
-        # we need to call a shell function to avoid shell restarts when vi/vim
-        # is being installed/uninstalled
-        alias vi=__vi_internal_vim_alias
-        alias view=__view_internal_vim_alias
-        alias vim=__vi_internal_vim_alias
-        ;;
-  esac
-fi
diff --git a/vim.spec b/vim.spec
index ce7d61b..3d02476 100644
--- a/vim.spec
+++ b/vim.spec
@@ -21,26 +21,26 @@ Summary: The VIM editor
 URL:     http://www.vim.org/
 Name: vim
 Version: %{baseversion}.%{patchlevel}
-Release: 2%{?dist}
+Release: 3%{?dist}
 License: Vim and MIT
 Source0: ftp://ftp.vim.org/pub/vim/unix/vim-%{baseversion}-%{patchlevel}.tar.bz2
-Source1: vim.sh
-Source2: vim.csh
-Source4: virc
-Source5: vimrc
-Source7: gvim16.png
-Source8: gvim32.png
-Source9: gvim48.png
-Source10: gvim64.png
+Source1: virc
+Source2: vimrc
+Source3: gvim16.png
+Source4: gvim32.png
+Source5: gvim48.png
+Source6: gvim64.png
+Source7: spec-template.new
+Source8: macros.vim
+Source9: vim-default-editor.sh
+Source10: vim-default-editor.csh
+Source11: vim-default-editor.fish
+Source12: view_wrapper
+
 %if %{withvimspell}
-Source13: vim-spell-files.tar.bz2
+Source100: vim-spell-files.tar.bz2
 %endif
-Source14: spec-template.new
-Source15: macros.vim
-Source16: vim-default-editor.sh
-Source17: vim-default-editor.csh
-Source18: vim-default-editor.fish
-Source19: vim.fish
+
 
 Patch2002: vim-7.0-fixkeys.patch
 Patch2003: vim-7.4-specsyntax.patch
@@ -130,6 +130,8 @@ Conflicts: %{name}-common < %{epoch}:8.1.1-1
 Conflicts: vim-enhanced < 2:8.2.2146-2
 Provides: vi
 Provides: %{_bindir}/vi
+# needs alternatives for post and preun scriptlets
+Requires: alternatives
 
 %description minimal
 VIM (VIsual editor iMproved) is an updated and improved version of the
@@ -151,6 +153,8 @@ Provides: bundled(libvterm)
 Provides: vim
 Provides: %{_bindir}/mergetool
 Provides: %{_bindir}/vim
+# needs alternatives for post and preun scriptlets
+Requires: alternatives
 Requires: vim-common = %{epoch}:%{version}-%{release} which
 # suggest python3, python2, lua, ruby and perl packages because of their 
 # embedded functionality in Vim/GVim
@@ -255,7 +259,7 @@ perl -pi -e "s,bin/nawk,bin/awk,g" runtime/tools/mve.awk
 
 # install spell files
 %if %{withvimspell}
-%{__tar} xjf %{SOURCE13}
+%{__tar} xjf %{SOURCE100}
 %endif
 
 %patch3000 -p1
@@ -402,7 +406,7 @@ cp vim enhanced-vim
 mkdir -p %{buildroot}/%{_bindir}
 mkdir -p %{buildroot}/%{_datadir}/%{name}/vimfiles/{after,autoload,colors,compiler,doc,ftdetect,ftplugin,indent,keymap,lang,plugin,print,spell,syntax,tutor}
 mkdir -p %{buildroot}/%{_datadir}/%{name}/vimfiles/after/{autoload,colors,compiler,doc,ftdetect,ftplugin,indent,keymap,lang,plugin,print,spell,syntax,tutor}
-cp -f %{SOURCE14} %{buildroot}/%{_datadir}/%{name}/vimfiles/template.spec
+cp -f %{SOURCE7} %{buildroot}/%{_datadir}/%{name}/vimfiles/template.spec
 cp runtime/doc/uganda.txt LICENSE
 # Those aren't Linux info files but some binary files for Amiga:
 rm -f README*.info
@@ -413,21 +417,23 @@ cd src
 # and put the stripped files into correct dirs. Build system (koji/brew) 
 # does it for us, so there is no need to do it in Vim
 %make_install BINDIR=%{_bindir} VIMRCLOC=/etc VIMRUNTIMEDIR=/usr/share/vim/%{vimdir} STRIP=/bin/true
+# make install creates vim binary and view symlink, they will be symlinks from alternatives
+# so remove them here
+%{_bindir}/rm -f %{buildroot}%{_bindir}/{vim,view}
 make installgtutorbin  DESTDIR=%{buildroot} BINDIR=%{_bindir} VIMRCLOC=/etc VIMRUNTIMEDIR=/usr/share/vim/%{vimdir}
 mkdir -p %{buildroot}%{_datadir}/icons/hicolor/{16x16,32x32,48x48,64x64}/apps
-install -m755 minimal-vim %{buildroot}%{_bindir}/vi
-install -m755 enhanced-vim %{buildroot}%{_bindir}/vim
+install -m755 minimal-vim %{buildroot}%{_bindir}/vi.origin
+install -m755 enhanced-vim %{buildroot}%{_bindir}/vim.origin
 install -m755 gvim %{buildroot}%{_bindir}/gvim
-install -p -m644 %{SOURCE7} \
+install -m755 %{SOURCE12} %{buildroot}%{_bindir}/view_wrapper
+install -p -m644 %{SOURCE3} \
    %{buildroot}%{_datadir}/icons/hicolor/16x16/apps/gvim.png
-install -p -m644 %{SOURCE8} \
+install -p -m644 %{SOURCE4} \
    %{buildroot}%{_datadir}/icons/hicolor/32x32/apps/gvim.png
-install -p -m644 %{SOURCE9} \
+install -p -m644 %{SOURCE5} \
    %{buildroot}%{_datadir}/icons/hicolor/48x48/apps/gvim.png
-install -p -m644 %{SOURCE10} \
+install -p -m644 %{SOURCE6} \
    %{buildroot}%{_datadir}/icons/hicolor/64x64/apps/gvim.png
-#cp -f %{SOURCE17} %{buildroot}/%{_datadir}/%{name}/%{vimdir}/ftplugin/spec.vim
-#cp -f %{SOURCE18} %{buildroot}/%{_datadir}/%{name}/%{vimdir}/syntax/spec.vim
 
 # Register as an application to be visible in the software center
 #
@@ -473,12 +479,11 @@ SentUpstream: 2014-05-22
 EOF
 
 ( cd %{buildroot}
-  ln -sf vi ./%{_bindir}/rvi
-  ln -sf vi ./%{_bindir}/rview
-  ln -sf vi ./%{_bindir}/view
-  ln -sf vi ./%{_bindir}/ex
-  ln -sf vim ./%{_bindir}/rvim
-  ln -sf vim ./%{_bindir}/vimdiff
+  ln -sf vi.origin ./%{_bindir}/rvi
+  ln -sf vi.origin ./%{_bindir}/rview
+  ln -sf vi.origin ./%{_bindir}/ex
+  ln -sf vim.origin ./%{_bindir}/rvim
+  ln -sf vim.origin ./%{_bindir}/vimdiff
   perl -pi -e "s,%{buildroot},," .%{_mandir}/man1/vim.1 .%{_mandir}/man1/vimtutor.1
   rm -f .%{_mandir}/man1/rvim.1
   cp -p .%{_mandir}/man1/vim.1 .%{_mandir}/man1/vi.1
@@ -539,19 +544,13 @@ chmod 644 %{buildroot}/%{_datadir}/%{name}/%{vimdir}/doc/vim2html.pl \
 chmod 644 ../runtime/doc/vim2html.pl
 
 mkdir -p %{buildroot}/%{_sysconfdir}/profile.d
-install -p -m644 %{SOURCE1} %{buildroot}/%{_sysconfdir}/profile.d/vi.sh
-install -p -m644 %{SOURCE1} %{buildroot}/%{_sysconfdir}/profile.d/vim.sh
-install -p -m644 %{SOURCE2} %{buildroot}/%{_sysconfdir}/profile.d/vi.csh
-install -p -m644 %{SOURCE2} %{buildroot}/%{_sysconfdir}/profile.d/vim.csh
-install -p -m644 %{SOURCE16} %{buildroot}/%{_sysconfdir}/profile.d/vim-default-editor.sh
-install -p -m644 %{SOURCE17} %{buildroot}/%{_sysconfdir}/profile.d/vim-default-editor.csh
+install -p -m644 %{SOURCE9} %{buildroot}/%{_sysconfdir}/profile.d/vim-default-editor.sh
+install -p -m644 %{SOURCE10} %{buildroot}/%{_sysconfdir}/profile.d/vim-default-editor.csh
 mkdir -p %{buildroot}/%{_datadir}/fish/vendor_conf.d/
-install -p -m644 %{SOURCE18} %{buildroot}/%{_datadir}/fish/vendor_conf.d/vim-default-editor.fish
+install -p -m644 %{SOURCE11} %{buildroot}/%{_datadir}/fish/vendor_conf.d/vim-default-editor.fish
 mkdir -p %{buildroot}/%{_datadir}/fish/vendor_functions.d/
-install -p -m644 %{SOURCE19} %{buildroot}/%{_datadir}/fish/vendor_functions.d/vi.fish
-install -p -m644 %{SOURCE19} %{buildroot}/%{_datadir}/fish/vendor_functions.d/vim.fish
-install -p -m644 %{SOURCE4} %{buildroot}/%{_sysconfdir}/virc
-install -p -m644 %{SOURCE5} %{buildroot}/%{_sysconfdir}/vimrc
+install -p -m644 %{SOURCE1} %{buildroot}/%{_sysconfdir}/virc
+install -p -m644 %{SOURCE2} %{buildroot}/%{_sysconfdir}/vimrc
 
 # if Vim isn't built for Fedora, use redhat augroup
 %if 0%{?rhel} >= 7
@@ -560,7 +559,7 @@ sed -i -e "s/augroup fedora/augroup redhat/" %{buildroot}/%{_sysconfdir}/virc
 %endif
 
 mkdir -p %{buildroot}%{_rpmconfigdir}/macros.d/
-install -p -m644 %{SOURCE15} %{buildroot}%{_rpmconfigdir}/macros.d/
+install -p -m644 %{SOURCE8} %{buildroot}%{_rpmconfigdir}/macros.d/
 
 (cd ../runtime; rm -rf doc; ln -svf ../../vim/%{vimdir}/doc docs;) 
 rm -f %{buildroot}/%{_datadir}/vim/%{vimdir}/macros/maze/maze*.c
@@ -591,6 +590,82 @@ echo ".so man1/vim.1" > %{buildroot}/%{_mandir}/man5/vimrc.5
 echo ".so man1/vi.1" > %{buildroot}/%{_mandir}/man5/virc.5
 touch %{buildroot}/%{_datadir}/%{name}/vimfiles/doc/tags
 
+
+%post enhanced
+# sets up symlinks for vi=vim, view=vim -R (via wrapper) during installation
+# we need to copy original binary as vim.origin and ship it, otherwise we lose
+# the original binary
+# it will overwrite vi symlinks if they exist
+
+if [ -f %{_bindir}/vi ] && [ ! -L %{_bindir}/vi ]
+then
+  %{_bindir}/mv %{_bindir}/vi{,.origin}
+fi
+
+if [ -f %{_bindir}/vim ] && [ ! -L %{_bindir}/vim ]
+then
+  %{_bindir}/rm -f %{_bindir}/vim
+fi
+
+%{_sbindir}/alternatives --install %{_bindir}/vim enhanced-vim %{_bindir}/vim.origin 50 \
+--slave %{_bindir}/view enhanced-view %{_bindir}/view_wrapper \
+--slave %{_bindir}/vi enhanced-vi %{_bindir}/vim.origin
+exit 0
+
+%post minimal
+# go out if we are already using alternatives via vim
+[ ! -f %{_bindir}/vim.origin ] || exit 0
+
+if [ -f %{_bindir}/vi ] && [ ! -L %{_bindir}/vi ]
+then
+  %{_bindir}/rm -f %{_bindir}/vi
+fi
+
+if [ -f %{_bindir}/vim ] && [ ! -L %{_bindir}/vim ]
+then
+  %{_bindir}/mv %{_bindir}/vim{,.origin}
+fi
+
+if [ -f %{_bindir}/vim.origin ]
+then
+  %{_sbindir}/alternatives --install %{_bindir}/vim enhanced-vim %{_bindir}/vim.origin 50 \
+  --slave %{_bindir}/view enhanced-view %{_bindir}/view_wrapper \
+  --slave %{_bindir}/vi enhanced-vi %{_bindir}/vim.origin
+else
+  %{_sbindir}/alternatives --install %{_bindir}/vim minimal-vim %{_bindir}/vi.origin 40 \
+  --slave %{_bindir}/view minimal-view %{_bindir}/vi.origin \
+  --slave %{_bindir}/vi minimal-vi %{_bindir}/vi.origin
+fi
+exit 0
+
+%preun enhanced
+# get out if we don't uninstall
+[ $1 = 0 ] || exit 0
+
+# if vim-minimal is installed, set up symlinks to it
+if [ -f %{_bindir}/vi.origin ]
+then
+  %{_sbindir}/alternatives --install %{_bindir}/vim minimal-vim %{_bindir}/vi.origin 40 \
+  --slave %{_bindir}/view minimal-view %{_bindir}/view.origin \
+  --slave %{_bindir}/vi minimal-vi %{_bindir}/vi.origin
+else
+# otherwise remove all vim-enhanced symlinks
+  %{_sbindir}/alternatives --remove enhanced-vim %{_bindir}/vim.origin
+fi
+exit 0
+
+%preun minimal
+# get out if we don't uninstall
+[ $1 = 0 ] || exit 0
+
+# remove symlinks only if vim-enhanced isn't installed. Otherwise
+# the symlinks are set by vim-enhanced and they don't change.
+if [ ! -f %{_bindir}/vim.origin ]
+then
+  %{_sbindir}/alternatives --remove minimal-vi %{_bindir}/vi.origin
+fi
+exit 0
+
 # Refresh documentation helptags
 %transfiletriggerin common -- %{_datadir}/%{name}/vimfiles/doc
 %{_bindir}/vim -c ":helptags %{_datadir}/%{name}/vimfiles/doc" -c :q &> /dev/null || :
@@ -748,13 +823,9 @@ touch %{buildroot}/%{_datadir}/%{name}/vimfiles/doc/tags
 %endif
 
 %files minimal
-%dir %{_datadir}/fish/vendor_functions.d
-%{_datadir}/fish/vendor_functions.d/vi.fish
-%config(noreplace) %{_sysconfdir}/profile.d/vi.*
 %config(noreplace) %{_sysconfdir}/virc
 %{_bindir}/ex
-%{_bindir}/vi
-%{_bindir}/view
+%{_bindir}/vi.origin
 %{_bindir}/rvi
 %{_bindir}/rview
 %{_mandir}/man1/vi.*
@@ -765,11 +836,9 @@ touch %{buildroot}/%{_datadir}/%{name}/vimfiles/doc/tags
 %{_mandir}/man5/virc.*
 
 %files enhanced
-%dir %{_datadir}/fish/vendor_functions.d
-%{_datadir}/fish/vendor_functions.d/vim.fish
-%config(noreplace) %{_sysconfdir}/profile.d/vim.*
-%{_bindir}/vim
 %{_bindir}/rvim
+%{_bindir}/view_wrapper
+%{_bindir}/vim.origin
 %{_bindir}/vimdiff
 %{_bindir}/vimtutor
 
@@ -827,6 +896,9 @@ touch %{buildroot}/%{_datadir}/%{name}/vimfiles/doc/tags
 %config(noreplace) %{_sysconfdir}/profile.d/vim-default-editor.*
 
 %changelog
+* Thu Jan 21 2021 Zdenek Dohnal <zdohnal@xxxxxxxxxx> - 2:8.2.2311-3
+- rebuilt
+
 * Mon Jan 11 2021 Zdenek Dohnal <zdohnal@xxxxxxxxxx> - 2:8.2.2311-2
 - conflicting the version when the change happened doesnt make sense
 

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

_______________________________________________
devel mailing list -- devel@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to devel-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/devel@xxxxxxxxxxxxxxxxxxxxxxx

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Fedora Announce]     [Fedora Users]     [Fedora Kernel]     [Fedora Testing]     [Fedora Formulas]     [Fedora PHP Devel]     [Kernel Development]     [Fedora Legacy]     [Fedora Maintainers]     [Fedora Desktop]     [PAM]     [Red Hat Development]     [Gimp]     [Yosemite News]

  Powered by Linux