Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modularizing and distributing bash script via Homebrew

Context

I have some functions defined in my ~/.bashrc which I'd like to turn into a Homebrew package. Currently, these functions act as custom commands on my command line:

# .bashrc
function foo() {
    # do something interesting
}

# at terminal
$ foo
# => does the interesting thing

Approach

I've created a homebrew formula using brew create. My current approach is as follows:

  1. Move the function definitions into a separate file, script, within a directory, brew-script
  2. Make brew-script downloadable as a tarball, brew-script.tar.gz
  3. Have my brew formula append text to the end of ~/.bash_profile to include script when terminal session starts

Concerns

  1. Is modifying .bash_profile in a brew formula bad practice? (eg. when uninstalling with brew uninstall script, brew should somehow remove the text that was appended to .bash_profile... Parsing .bash_profile doesn't seem very fun.)

  2. Is there a convention already established for including functions in bash scripts so that they are available from the command line?

  3. Is it common to simply ask the user to add some text to their .bash_profile or .bashrc?

Desired result

Should be able to install cleanly with brew and then run foo as a command:

$ brew install script
$ foo
# => does the interesting thing

(Assume the brew formula is already installed locally. I'll worry about auditing and pushing the formula to homebrew later)

like image 858
Pedro Cattori Avatar asked Mar 28 '15 16:03

Pedro Cattori


1 Answers

Refer https://github.com/Homebrew/homebrew/issues/50232 and https://github.com/Homebrew/homebrew/issues/50231.

I have a script that safely‡ modifies ~/.bash_profile as part of a homebrew install process. https://github.com/paul-hammant/homebrew-tap/blob/master/switchjdk.rb

‡ allegedly

like image 129
paul_h Avatar answered Nov 02 '22 17:11

paul_h