Create Homebrew Formula from scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's build a home brew formula in this video we'll create a new program and then package its content as an executable and in the third step we'll create a home group formula out of it and then test the formula locally we'll publish that formula in our own personal tab as a GitHub repository and then we'll test the formula from the tab itself and we are going to do every step in this video so let's get started the inspiration behind making this video is the project that I did last day which is called git commit in the future is a utility function that helps you do git commit with the future time and I happened to create a Homebrew formal out of it and that's why I thought of recording the steps that I did to create the formula it is in the form of article as well that you can read and follow and we also have the repository for the project itself and the home root tab that I'm serving the formula from so we'll do refer these projects in our today's exercise to just copy paste and the code so that's why I want want to mention these two GitHub repositories so let's start with our project and create a new directory so let's create a reaction for the same and CD into the directory and this is going to be a node-based program so I'm gonna edit the npm and let's open it and the editor this is our editor let's create a new file called index.js and this is what our package.json looks like as of now our program will present a random useless fact using this particular API which if we hit on the slash random endpoint it will give a random useless fact and will print that useless fact in the console that's all our utility will do let's get to the fetch code which a useless fact first of all we need the node Fetch and then we'll create a function called Fetch useless fact which will print a useless fact on the console and then exit if it is a success with a success code or exit with one if there was an error that's all our code do but if we try to run this now say node index oh we didn't save let me save it first we should get one error which says note which is not found so let's install couple of dependencies and but first we need to install node fetch which is version 2 because it is a common JS version 3 or latest is esm which we don't want to use here because of environment constraints and let's install a couple of more packages so if I go to my repository and go to package.json we need to use Virtual NCC and a package so these virtual NCC is used to compile your whole project into a single file we do have a single file as of now but it's for future references and the other package is used to make your whole node program into a single executable it package everything required from node environment to dependencies and your source code everything into a single executable file that you can run okay let's install these two packages and then npm install package now we have got two packages installed let's copy the configuration from the package.json into our project so we have only one test script let's give it two more scripts which is to build the project this will build our index in to the build directory and then package command will package targeting the Linux environment and will place the output into the dist folder with the name we should change the name uh useless fact I'll copy it because I need it and then the next set of configuration we want is this package it is if we have another JS files coming it will package those as well from the build directory which is not the case for this particular project and then this is the command that we will execute from the console okay and we should give it a name useless fact which we'll call our index.js file now what we will do is to clear the console and try to build the project success and then let's run package command on this so this will introduce a dist folder here and will package our program into this useless fact executable now if I try to run this useless fact it prints a useless fact awesome so we have got our executable program running and next step is to create a Homebrew formula of it the command we use is Brew create and this command takes a URL for the artifact like you see in the suggestion here so we need to convert this executable and package it into a tar.gz file and then upload it somewhere so that we can get the download URL right so let's create a release GitHub release for this particular executable and before that let's create the entire file and for tar file the command is simple you give it a name the destination and the file to be zipped so this has created the tar let me list down so we have our tar next we'll create a GitHub release for this for releasing the tar file to the GitHub let's create a new repository for that [Music] fill in the details and let's create a repository [Music] to create a release we need to push some source code and to do that let's add the origin to our git repo we need to initialize git here in our directory first and then let's add the remote to our Repository we'll add everything to the stage and commit it let's push it to the Repository [Music] as we can see the code has been pushed to the repository let's create a new release we'll create a new tag [Music] let's attach our artifact to the release let's create the release [Music] and now we can copy the URL to our artifact and we'll utilize the same URL to create our Brew formula and once I hit the enter I will be presented with an Editor to edit my formula which is written in Ruby [Music] this is what the code looks like foreign we should delete all the commented lines which are not required now fill in the details brew has already calculated the Shah 256 but if you want to calculate it yourself you can use this utility and point it to your tar file this will give you the same sha code that you have already in your file now let's finish the testing and installation code I'm gonna copy it from my Repository [Music] and I'll be changing the command name save it and close the editor this is for another file I don't know where it is coming from now we are good to go the next step is to test the code locally so we have created the formula to test the code locally what we should do is Brew install build from source and then the command name so let's try that Brew install build from Source useless Pact [Music] [Music] so installation is done let's try to run our program and it's working the next step is to publish this formula into a tab and I already have a tab repository for that what you need to do is you should create a repository into your GitHub account with the name starting with Homebrew Dash and you can give any name after that the only requirement is that it should start with Homebrew Dash so I'm going to push that formula into this repository which only have currently one formula so we should have another formula here then we'll install the formula and test it from this tab let's push the formula into this tab for that you need the file and to know where the file currently is residing what you can do is you can fire the create command again which will give you error but the file location as well so copy it and move it to the Repository [Music] so I'm gonna move it to my Homebrew tab directory and now I will move it to the directory of the tab let's push this formula to the Repository [Music] we have the new formula here now let's try to install this formula from the tab itself let's get back to the terminal first we need to uninstall the one we created locally now as you can see I don't have this command next let's add the tab to our local installation of Homebrew for that you need to type Brew Tab and then your username GitHub username and your home brew tab name I already have added but I'm still running this command so it says I have a new formula so now let's try to install the useless fact from the tab it is downloading the release from my GitHub repository now it is installing from this particular Tab and we have the installation finished now let's try to run this [Music] and voila this is working so we installed the Brew formula from the tab and that's all I wanted to cover so we started by creating a new program and packaged it as an executable then we created a new Homebrew formula out of it and tested the formula locally then we pushed the formula to a GitHub repository acting as a home root Tab and then install the formula from the tab itself and tested it locally hope you learned something out of this video and wish you create your own new home brew formula please do share in the comments once you have your formula created and happy coding happy hacking till next time take care
Info
Channel: theRDnotes
Views: 997
Rating: undefined out of 5
Keywords: create homebrew formula, homebrew, formula, node executable
Id: Oe0DOpG0GpQ
Channel Id: undefined
Length: 13min 12sec (792 seconds)
Published: Wed Oct 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.