Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installer defaults to .bash_profile for linux distributions #62

Open
gjstreicher opened this issue Jan 30, 2022 · 0 comments
Open

Installer defaults to .bash_profile for linux distributions #62

gjstreicher opened this issue Jan 30, 2022 · 0 comments

Comments

@gjstreicher
Copy link

gjstreicher commented Jan 30, 2022

Issue Overview

In the install.sh file the following code is currently used to deal with shells:

case $SHELL in
/bin/zsh) shell_profile=".zshrc" ;;
*) shell_profile=".bash_profile" ;;
esac

The default case sets the shell_profile variable to ".bash_profile" without checking if this is a linux distribution.

I personally checked out the Linux distribution I use, which is Pop!_OS by System76, and it only ever sources the .bashrc file when I open a bash terminal. This is true even if I logout and in. This distribution is an Ubuntu-based distribution and most other Linux distributions only ever source ".bashrc" since they treat terminals run on the fly as non-login shells as opposed to Mac that treats these as login shells.

Simple Solution

To fix this issue the above code can be extended as follows to accommodate for bash on most Linux distributions:

Add the line of DENO_OS="linux" in the default case of the switch statement below:

if [ "$OS" = "Windows_NT" ]; then
	target="x86_64-pc-windows-msvc"
else
	case $(uname -s) in
	Darwin) target="x86_64-apple-darwin" ;;
	*)
		target="x86_64-unknown-linux-gnu"
		DENO_OS="linux"                              <-------[modified line]
	;;
	esac
fi

Next, prepend the default case of the switch statement below with [ "$DENO_OS" = "linux" ] && shell_profile=".bashrc"

case $SHELL in
/bin/zsh) shell_profile=".zshrc" ;;
*) [ "$DENO_OS" = "linux" ] && shell_profile=".bashrc" || shell_profile=".bash_profile" ;;    <-------[modified line]
esac

NOTE: It is important for the "$DENO_OS" part to be encapsulated in quotes, otherwise it won't work on Mac due to DENO_OS not being defined in that case leaving no left parameter for the binary operator of = to work with.

Advanced Solution

An even better solution can be found by looking at the install script for the n Node version manager that uses the
following code:

# SYNOPSIS
#   getShellInitFile
# DESCRIPTION
#   Returns the full path of the initalization file of the shell identified
#   via (environment variable) $SHELL, the user's default shell.
#   If $SHELL refers to an *unsupported* shell, the empty string is returned.
getShellInitFile() {
  local initFile=''

  # IMPORTANT:
  #   This STATEMENT MUST BE KEPT IN SYNC with cleanUpShellInitFile() in n-uninstall.
  case "$(basename -- "$SHELL")" in
    'bash')
      # !! Sadly, bash ONLY reads ~/.bash_profile in LOGIN shells, and on macOS (Darwin) ALL shells are login shells, so on macOS we must target ~/.bash_profile.
      [[ $(uname) == 'Darwin' ]] && initFile=~/.bash_profile || initFile=~/.bashrc
      ;;
    'ksh')
      initFile=~/.kshrc
      ;;
    'zsh')
      initFile=${ZDOTDIR:-~}/.zshrc
      ;;
    'fish')
      initFile=${XDG_CONFIG_HOME:-~/.config}/fish/config.fish
      ;;
    'pwsh') # PowerShell
      initFile=${XDG_CONFIG_HOME:-~/.config}/powershell/Microsoft.PowerShell_profile.ps1
      ;;
  esac

  printf %s "$initFile"

}

Link: https://github.com/mklement0/n-install/blob/master/bin/n-install
Line: 262

The above solution caters for quite a few different shells which is a plus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant