The Wayback Machine - https://web.archive.org/web/20200519011556/https://github.com/Bash-it/bash-it/issues/1505
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

A more convenient method for controlling the sourcing order of customized scripts. #1505

Open
hongyi-zhao opened this issue Feb 25, 2020 · 6 comments

Comments

@hongyi-zhao
Copy link
Contributor

@hongyi-zhao hongyi-zhao commented Feb 25, 2020

Hi,

Currently, the sourcing order of the customized scripts are controlled by the following code snippet in https://github.com/Bash-it/bash-it/blob/master/bash_it.sh:

# Custom
CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/**/*.bash"
for _bash_it_config_file in $CUSTOM
do
  if [ -e "${_bash_it_config_file}" ]; then
    # shellcheck disable=SC1090
    source "$_bash_it_config_file"
  fi
done

But this is not so convenient if we wan to have more control on the sourcing order of the customized scripts.

So, I suggest the following codes for doing the job instead of the above ones:

# Custom
# A more convenient method for controlling the sourcing order of customized scripts:
# The scripts in begin folder will be sourced firstly and the ones in the end folder will be 
# sourced lastly.
CUSTOM="${BASH_IT_CUSTOM:=${BASH_IT}/custom}/begin/*.bash \
        ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/*.bash       \
        ${BASH_IT_CUSTOM:=${BASH_IT}/custom}/end/*.bash"
for _bash_it_config_file in $CUSTOM
do
  if [ -e "${_bash_it_config_file}" ]; then
    # shellcheck disable=SC1090
    source "$_bash_it_config_file"
  fi
done

Any comments on this idea? If you agree, I will make a PR.

Regards

@nwinkler
Copy link
Member

@nwinkler nwinkler commented Feb 26, 2020

Couldn't you achieve the same effect by using a naming convention for the custom scripts, with a number prefix, e.g. like this:

  • 00_begin_custom.bash
  • 10_init_custom.bash
  • 99_end_custom.bash

The glob that lists the files (CUSTOM variable in the above snippet) should order them alphabetically by default, I think.

@hongyi-zhao
Copy link
Contributor Author

@hongyi-zhao hongyi-zhao commented Feb 26, 2020

The problem is that the scripts to be sourced are symlinks pointing to somewhere outside of the custom folder. And I have already some other naming specifications for these scripts. In this case, I don't want to change the naming spec for these scripts.

Any hints?

@hongyi-zhao
Copy link
Contributor Author

@hongyi-zhao hongyi-zhao commented Feb 26, 2020

Any I have more scripts in the custom folder which maybe startwith alphabets, in this case, they will be sourced after number-beginning ones.

Regards

@nwinkler
Copy link
Member

@nwinkler nwinkler commented Feb 26, 2020

For your symlinked custom scripts, you could use a different name for the symlink than for the source script in a different location, e.g. like this:

ln -s ~/.bash_it/custom/00_aliases.bash ~/foo/bar/aliases.bash

This is the way the Bash-it plugins, aliases and completions are enabled and then loaded.

I'm not a fan of introducing more complexity in the way these things are handled. Maybe you can look into renaming/linking your custom scripts to work like that.

What are your naming standards for the custom scripts? Maybe there's something there to be reused...

@hongyi-zhao
Copy link
Contributor Author

@hongyi-zhao hongyi-zhao commented Feb 26, 2020

Thanks a lot. This is more graceful then my idea and still can solve my problem. Thanks for you suggestions.

I mainly use some code like the following in the script to obtain the script's pyhisical dirname, basename, and so on:

topdir=$(
cd -P -- "$(dirname -- "$(realpath -e -- "${BASH_SOURCE[0]}")" )" &&
pwd -P
) 

# In the following method, the $DIRNAME is equivalent to $topdir otained above in this script:
#https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
FULLPATH="$( realpath -e -- "${BASH_SOURCE[0]}")"

FILENAME="${FULLPATH##*/}"                      # Strip longest match of */ from start
# Revise, removed the trailling /: 
DIRNAME="${FULLPATH:0:${#FULLPATH} - ${#FILENAME} - 1}" # Substring from 0 thru pos of filename
FILE_BASENAME="${FILENAME%.[^.]*}"                       # Strip shortest match of . plus at least one non-dot char from end
FILE_EXTNAME="${FILENAME:${#FILE_BASENAME} + 1}"                  # Substring from len of base thru end
if [[ -z "$FILE_BASENAME" && -n "$FILE_EXTNAME" ]]; then          # If we have an extension and no base, it's really the base
  FILE_BASENAME=".$FILE_EXTNAME"
  ext=""
fi

echo -e "\tFULLPATH  = \"$FULLPATH\"\n\tDIRNAME  = \"$DIRNAME\"\n\tFILE_BASENAME = \"$FILE_BASENAME\"\n\tFILE_EXTNAME  = \"$FILE_EXTNAME\""

Regards

@hongyi-zhao
Copy link
Contributor Author

@hongyi-zhao hongyi-zhao commented Feb 26, 2020

Couldn't you achieve the same effect by using a naming convention for the custom scripts, with a number prefix, e.g. like this:

* 00_begin_custom.bash

* 10_init_custom.bash

* 99_end_custom.bash

If I want to use this spec for the scripts added by me in the system's /etc/profile.d folder, due to there are some scripts created by system and these scripts are not named after the above spec.

So, in this case, the above spec may not ensure the souring order.

Any hints for this case?

The glob that lists the files (CUSTOM variable in the above snippet) should order them alphabetically by default, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.