【 Homebrew series 】

  1. HomeBrew regular use tutorial
  2. Homebrew进阶使用教程(一)
  3. Homebrew Advanced Tutorial 2 – Build your own repository with a command line weather client
  4. Install and connect to Homebrew on MAC

I lot address: making address: https://github.com/rangaofei/homebrew-saka

Software runs

Here let me first install a force, promote their own warehouse, although only two packages: Github address

  1. Install the SAKA warehouse
brew tap rangaofei/saka
Copy the code

2. Install the software

brew install sweather
Copy the code

3. Run software

Sweather -v—- Displays the software version

Sweather -setloc Liangxi —- Set the weather address as Liangxi District, Wuxi

As Liangxi is set here, the matching address found in the address database is only Liangxi District of Wuxi, so it is automatically set to Liangxi District of Wuxi.

If there are multiple addresses to choose from when querying, it is as follows:

Here I choose 9:

After the selection, enter the query command, three days of weather

sweather
Copy the code

Welcome to use, at the same time if you have their own library to release can directly leave a message, if you allow me to help you write a good Ruby script released to the warehouse, for all people to use.

Homebrew warehouse

Brew will provide an official warehouse, homebrew-Coregithub address.

Homebrew installed automatically loaded into the warehouse after Homerew/usr/local/Library/Taps/folder, this is the address of a git management, means we can submit your own pullrequest to the warehouse.

Focus on homebrew-core/Formula/. This folder contains all the software installation scripts in our repository, such as curl.

Join a third-party warehouse

Here we take the warehouse created by ourselves as an example:

  • The name of the warehouse

Brew ruby scripts built on Github. All repositories are centralized on Github. All of the repositories demonstrated here are also built on Github.

  1. The warehouse name must behomebrew-anythingFormat,homebrew-The prefix is optional, but if you don’t use it, you have to use the entire URL to install, which is quite cumbersome, so be sure to use it.anythingYou can set it to any name you like. This is the name of my warehouse:
  2. Create a Formula folder to store the installation scripts. The name of the file is the one you used when brew install was created.
  • The command

  1. brew tap
brew tap <user/repo>

Clone your own repository to the local Tap folder, you can brew [UN] Install to manage the installation and uninstallation of software in your repository. When you use BREW Update, you also update the package installation files in your own repository.

For example my warehouse homebrews -saka above, my user name is rangaofei, then I can use brew tap rangaofei/saka to add my own warehouse:

As shown above, my warehouse has been added.

brew tap <user/repo>

Add other non-Github repositories via a URL that is not HTTP, as long as git is supported. This is not demonstrated here.

brew untap user/repo

Delete the added warehouse, and the software under the warehouse can be used again.

brew tap-pin user/repo

Marks the specified repository as the package preferred search repository.

brew tap-unpin user/repo

Cancels the package preference search for the specified repository.

Bags with duplicate names

If you have a package with the same name as homebrew-core in your warehouse, of course you can. Brew has a default search order when using the brew_install command:

  • Pinned TAPS (pinned warehouse)
  • Core Formulae
  • Other TAPS

This means that if we give any warehouse a pinned tag, Homebrew will automatically search the core warehouse first and then the other warehouses. If a warehouse is marked, the marked warehouse is searched first, then the core warehouse is searched, and then the other warehouses are searched. If we have marked a repository, such as saka, then if I have curl packages in my repository and curl packages in core, I will first install curl in saka’s repository.

If you want to install a package from a specific repository you can also do this by adding user/repo. For example, if we want to install Vim, there are two ways:

brew install vim                     Install from Homebrew /core
brew install username/repo/vim       Install from the specified repository
Copy the code

Publish your own package

Brew is installed in Ruby scripts, and the syntax follows ruby syntax.

  1. Distribution source package address

Now I have a c language to get the weather command line client, hosted on Github, file address

https://raw.githubusercontent.com/rangaofei/SimpleWeather/master/sweather-1.1.0.tar.gz
Copy the code

The format is best ended in a compressed file format. Here, you only need to package the necessary files, and keep them small. Focus on the last few characters: sweather-1.0.7.tar.gz

Sweather is the name of the package, which is the name of the folder we installed

1.1.0 indicates the version number. It is best to match the version number of the program you are distributing, such as the version number I set in cmake

set(VERSION_MAJOR 1)
set(VERSION_MINOR 1)
set(VERSION_PATCH 0)
Copy the code
  1. Create a formula
The brew the create https://raw.githubusercontent.com/rangaofei/SimpleWeather/master/sweather-1.0.7.tar.gzCopy the code

Brew automatically creates a sweather. Rb file that looks something like this (I’ve changed some of the contents here, including desc, homepage, cmake dependencies, and using cmake instead when installing) :

# Documentation: https://docs.brew.sh/Formula-Cookbook.html
# http://www.rubydoc.info/github/Homebrew/brew/master/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!

class Sweather < Formula
  desc "A very simple get weather client \n ----by saka"
  homepage "https://juejin.cn/user/3685218704952744"
  url "https://raw.githubusercontent.com/rangaofei/SimpleWeather/master/sweather-1.0.7.tar.gz"
  sha256 "64808c700bd46d837c6b06d7965f4f99d3c91b4c2764c3d02f893b8dfb9e9aa2"

  depends_on "cmake"= >:build
# depends_on "curl"=>:run
  def install
    etc.install Dir["src/*"]
    mkdir "build" do
      system "cmake"."..", *std_cmake_args
      system "make"
      system "make"."install" # if this fails, try separate make/make install steps

    end
  end

  test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! For Homebrew/homebrew-core
    # this will need to be a test that verifies the functionality of the
    # software. Run the test with `brew test sweather`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end
Copy the code

This file is created in the core warehouse, that is, in the/usr/local/Homebrew/Library/Taps/Homebrew/Homebrew – core/Formula/position, As long as the copy of our own warehouse location, and mine is/usr/local/Homebrew/Library/Taps/rangaofei/Homebrew – saka/Formula /. Once you’ve made your changes, commit them to your github via Git.

git add sweather.rb
git commit -m 'add new formula sweather'
git push origin
Copy the code

After committing we perform brew Update to update the repository. Then perform

brew search sweather # search package
brew info sweather # Display package information
brew install sweather # the installation package
Copy the code

Since the installation relies on cmake, it will automatically download and install cmake (which I already installed) and then download sweather installation. At this point you can check the weather from the command line:

sweather -v View the current version
sweather -setloc liangxi Set the weather address to Liangxi-Wuxi
sweather -now Check the weather now
sweather Check the weather for three days
Copy the code