.desktop - run RBENV shell - Run app with ruby version - ruby

Using RBENV on 16.04 sudo environment.
How to use RBENV shell $rubyversion or similar solution to run a ruby app from a .desktop file for the menu with predefined ruby version?
EDIT:
Heres how i did it with the solution posted by Ḱathryin:
I added interactive shell options to bash, not sure how exactly it works but it helped.
desktop file:
[Desktop Entry]
Name=app
Encoding=UTF-8
Exec=bash -ic "/path/app.sh;${SHELL:-bash}"
Type=Application
The sh exec which runs the app
echo '#!/bin/bash -i
cd /...../appdir
source ~/.bashrc
eval "$(rbenv init -)"
rbenv shell 2.4.1
./app $*
' > app.sh
sudo ln -s /path/app.sh /usr/local/bin/app
running apps with sudo also appears to work!

For the details of setting up a .desktop file, please see: Creating a .desktop file for a new application.
Since rbenv (when installed properly) is a function and not an executable, it won't be as simple as pointing to the right file. One solution would be to create a shell script that sets up the environment:
#!/usr/bin/env bash # Execute as a bash script
eval "$(rbenv init -)" # Initialize rbenv
ruby $* # Run the ruby script specified the .desktop file
Then make sure the script is executable and put it in your .desktop Exec key:
Exec=/path/to/script/ruby_stub.sh script_you_want_to_run.rb ARGS

Related

How to set path correctly from RBENV shims on Mac M1 with zsh

I'm having trouble setting up the rbenv paths
I follow the instructions as specified here: rbenv installation page
I run the command on a zsh terminal:
rbenv init
The terminal gives me the instruction to run
eval "$(rbenv init - zsh)"
I then close the restart the terminal and check to see if all is configured correctly by running:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash
and I get the following failure:
Checking for rbenv shims in PATH: not found
If I try open the fils .zshrc I find only the following path written to it
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
but no shims. Can I add the shims manually? I don't know what it should look like? Or is there any reason the command eval "$(rbenv init - zsh)" is not working properly?
Got it working: I need to run echo 'eval "$(rbenv init -)"' >> ~/.zshrc

Failed to activate virtualenv via shellscript

I'm trying to activate my pyenv-virtualenv environment through very simple bash script like this.
set -e
pyenv activate myenv
But I can't activate my env with below error:
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
I can activate it in command line, so I think it's due to my shell environment.
I use fish shell, but it's launched after $PATH settings read in bash shell.
I show you my settings.
.bash_profile:
export LANG="ja_JP.UTF-8"
export XDG_CONFIG_HOME="$HOME/.config"
export PATH="/sbin"
export PATH="/usr/sbin:$PATH"
export PATH="/bin:$PATH"
export PATH="/usr/bin:$PATH"
export PATH="/usr/local/bin:$PATH"
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
exec fish
And my config.fish:
status --is-interactive; and source (pyenv init -|psub)
status --is-interactive; and source (pyenv virtualenv-init -|psub)
What's wrong?
I'm using:
OS: OS X 10.13.5
fish: v2.7.1
pyenv: v1.2.5
pyenv-virtualenv: v1.1.3
I believe the cause is that you are initializing PyEnv in the Fish shell environment but then calling PyEnv in the Bash shell environment (via your Bash script). To solve this problem, you could try either of the following potential solutions:
Initialize PyEnv in your Bash environment (in addition to your Fish environment)
Write your script in Fish instead of Bash
Since PyEnv seems so have better support for Bash than Fish, let's focus on option #1. Step 3 of the PyEnv installation guide suggests the following command for adding PyEnv initialization to your ~/.bash_profile:
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
After running that command, I suspect your script will function as you expect. If it does not, you could try adding the same PyEnv initialization snippet (that you just added to your ~/.bash_profile) to the top of your Bash script.

Archlinux + MATE Terminal - `.bash_profile` is not being sourced

I am using Arch Linux with MATE as desktop environment. So terminal emulator is MATE Terminal. Recently I installed Jekyll with gem install jekyll. But when I ran jekyll -v it says bash: jekyll: command not found. So I tried to add path of Jekyll to PATH variable.
I ran PATH=$PATH/$HOME/.gem/ruby/2.2.0/bin and it worked perfectly. Now I can run jekyll commands. To add it permanently to PATH variable I edited the ~/.bash_profile file like following. It is not working after reboot. But
source ~/.bash_profile works perfectly.
#
# ~/.bash_profile
#
[[ -f ~/.bashrc ]] && . ~/.bashrc
export PATH="${PATH}:/home/heisenberg/.gem/ruby/2.2.0/bin"
According to ArchWiki this is the proper way to concat something permanantly to PATH. But it isn't working. Can somebody figure me out where the wrong is?
[N. B. : Adding the same line in ~/.bashrc is doing okay.]
Depending on the option it is given, bash can be run as an interactive shell or login shell. The default interactive shell mode does not read ~/.bash_profile. login shell bash do.
See:
First, some setup:
% cat ~/.bashrc
…
export BASHRC="yes"
…
% cat ~/.bash_profile
…
export BASH_PROFILE="yes"
…
Now run a regular (interactive) bash:
% bash
[galaux#magenta ~]$ echo $BASHRC
yes
[galaux#magenta ~]$ echo $BASH_PROFILE
Notice we did not get yes with this last one.
Now with a login shell:
% bash --login
[galaux#magenta ~]$ echo $BASHRC
yes
[galaux#magenta ~]$ echo $BASH_PROFILE
yes
See paragraph INVOCATION from man bash.

Rbenv not working

I installed ruby 2.0 into ~/.rbenv/versions last and now nothing but that is available
$ rbenv versions
system
*ruby-1.9.3-p392 (set by /apps/test_app/.ruby-version)
ruby-2.0.0-p0
$ ruby -v
ruby 2.0.0.p0
$ env | grep PATH
PATH=/home/cbron/.rbenv/shims:/home/cbron/.rbenv/bin
$cat ~/.bash_profile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
$rbenv global
ruby-1.9.3-p392
$rbenv local
ruby-1.9.3-p392
$rbenv shell
rbenv: no shell-specific version configured
edit: now set the shell, still nothing.
$rbenv shell
ruby-1.9.3-p392
ruby -v still getting
ruby 2.0.0p0
I already sourced my bash_profile, even restarted the computer.
I had the same issue using zsh and this fixed it:
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshenv
$ echo 'eval "$(rbenv init -)"' >> ~/.zshenv
$ echo 'source $HOME/.zshenv' >> ~/.zshrc
$ exec $SHELL
So basically moving the lines from profile to env!
Extracted from rbenv readme:
rbenv shell
Sets a shell-specific Ruby version by setting the RBENV_VERSION
environment variable in your shell. This version overrides
application-specific versions and the global version.
$ rbenv shell jruby-1.7.1
When run without a version number, rbenv shell reports the current
value of RBENV_VERSION. You can also unset the shell version:
$ rbenv shell --unset
Note that you'll need rbenv's shell integration enabled (step 3 of the
installation instructions) in order to use this command. If you prefer
not to use shell integration, you may simply set the RBENV_VERSION
variable yourself:
$ export RBENV_VERSION=jruby-1.7.1
So in order to use it you need to specify the ruby version as rbenv shell argument (f.e. rbenv shell 2.0.0.p0, or set RBENV_VERSION (f.e. export RBENV_VERSION=2.0.0.p0)
TL;DR: just reinstall rbenv, unless you are as stubborn as me...
I had messed up permissions in the .rbenv folder, so the shims weren't loading because they didn't have execute permissions
chmod u+x ~/.rbenv/shims/*
Disclaimer
I had deeper permissions problems, rbenv wasn't even working, so I did other chmod u+x ... previously
chmod u+x ~/.rbenv/libexec/*
chmod u+x ~/.rbenv/**/bin/*
Probably easier and safer to just reinstall rbenv. But this was fun!

Initialize rbenv and run ruby script from shell script

Initialize rbenv and run ruby script from shell script
I want svnserve to run pre-commit hook, written on ruby. As the svnserve is run as root user, it knows nothing about user rbenv installation.
I set a soft link /usr/bin/ruby -> /home/admin/.rbenv/shims/ruby .
As result, when i try
#!/usr/bin/ruby
puts "Pre-commit hook!"
It shows error:
Transmitting file data .svn: Commit failed (details follow):
svn: Commit blocked by pre-commit hook (exit code 255) with no output.
When i run manually on Server:
admin $ sudo ./pre-commit
/usr/bin/ruby: line 4: exec: rbenv: not found
So, i suppose, that rbenv initialization is needed, but how?
In hooks path:
pre-commit:
#!/bin/bash
export HOME=/home/user
if [ -d $HOME/.rbenv ]; then
export PATH="$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
fi
$0.rb $*
pre-commit.rb:
#!/usr/bin/env ruby
ARGV.each_with_index { |arg, index| puts "Index #{index}: Argument #{arg}" }
you should initialize rbenv before using it.
/path/to/user/home/.rbenv/bin/rbenv init
then set ruby version globally:
rbenv global DESIRED-RUBY-VERSION
or localy:
rbenv local DESIRED-RUBY-VERSION
or per shell:
rbenv shell DESIRED-RUBY-VERSION
though not sure shell setting will work without a tty.
so you could create a shell script, pre-commit.sh and register it as a svn hook.
inside it initialize rbenv and call your pre-commit.rb file

Resources