Last active
December 6, 2022 04:31
-
-
Save bogdanRada/4994955 to your computer and use it in GitHub Desktop.
RubyMine and rvm gemset detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| #Put this in .bashrc file | |
| # i am using in the project's folder a file named .config-rvm which contains on each line the branches that | |
| # we want for to have different gemsets. | |
| #The branches that are not in the fle will usee the project's gemset. | |
| # If you don't need this functionality you will not need to create the file . | |
| #Please be careful that the branch name is escaped and only small letters and numbers are allowed. | |
| #So if you have a branch like | |
| #feature/rails_upgrade_23 it will have to put like this "featurerailsupgrade23" inside the file. | |
| find_current_branch_and_escape(){ | |
| # find out what branch we're on... | |
| current_branch="`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' -e 's/[^a-z|0-9]//g;'`" | |
| #git_branch="$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' -e 's/\//-/g;' -e 's/[^a-z|0-9|_.-]//g;')" | |
| [[ -z "${current_branch}" ]] && current_branch="master" | |
| echo "$current_branch" | |
| } | |
| find_correct_ruby_version(){ | |
| ruby_version="`echo $RUBY_VERSION`" | |
| # read version from ruby-version file if exists | |
| if [ -f ./.ruby-version ]; then | |
| ruby_version="$(cat .ruby-version)" | |
| # otherwise read version from rbenv-version file | |
| elif [ -f ./.rbenv-version ]; then | |
| ruby_version="$(cat .rbenv-version)" | |
| fi | |
| echo "${ruby_version%%@*}" | |
| } | |
| find_correct_gemset_name(){ | |
| # find the name of the project where are on right now | |
| current_project="`git config --get remote.origin.url`" | |
| [[ -z "${current_project}" ]] && current_project="${PWD}" | |
| current_project="`basename ${current_project%.git}| cut -d':' -f2`" | |
| if [ -f ./.ruby-gemset ]; then | |
| project_gemset="$(cat .ruby-gemset)" | |
| else | |
| # otherwise set it up with the project's name | |
| project_gemset="${current_project}" | |
| fi | |
| if [[ -f .config-rvm ]]; then | |
| if [[ -n $(grep -w "$(find_current_branch_and_escape)" .config-rvm) ]]; then | |
| # define the name of our environment | |
| gemset_name="@${project_gemset}_$(find_current_branch_and_escape)"; | |
| else | |
| gemset_name="@${project_gemset}" | |
| fi | |
| else | |
| gemset_name="@${project_gemset}" | |
| fi | |
| echo "$gemset_name" | |
| } | |
| mk_rvmrc() { | |
| if [[ -f Gemfile ]]; | |
| then | |
| #clear rvmrc contents | |
| > .rvmrc | |
| #add bash header to file | |
| echo "#!/usr/bin/env bash" >> .rvmrc | |
| # trust rvmrc content when is changed | |
| echo "export rvm_trust_rvmrcs_flag=1" >> .rvmrc | |
| echo "export rvm_project_rvmrc_default=1" >> .rvmrc | |
| echo "rvm use '$(find_correct_ruby_version)$(find_correct_gemset_name)' --create" >> .rvmrc | |
| #reload rvmrc file so changes will take effect | |
| . .rvmrc | |
| fi | |
| } | |
| rm_dynamic_gemset(){ | |
| if [[ -f Gemfile ]]; then | |
| if [[ -f .config-rvm ]]; then | |
| if [[ -n $(grep -w "$(find_current_branch_and_escape)" .config-rvm) ]]; then | |
| rvm_ruby_gemset ="$(find_correct_ruby_version)$(find_correct_gemset_name)" | |
| if git diff --quiet 2>/dev/null >&2 | |
| then | |
| command rvm --force gemset delete "$rvm_ruby_gemset"; | |
| else | |
| echo "you have unstanged changes. Cannot remove gemset $rvm_ruby_gemset" | |
| fi; | |
| fi | |
| fi; | |
| fi; | |
| } | |
| # automatically reload your .rvmrc after git checkouts | |
| git() { if [[ "$@" == *checkout* || "$@" == *start* ]]; then command git "$@" ; mk_rvmrc; | |
| elif [[ "$@" == *finish* ]]; then rm_dynamic_gemset ; command git "$@" ; mk_rvmrc; | |
| else command git "$@"; [[ -s .rvmrc ]] && . .rvmrc; fi } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment