def homebrew_formula(name, src, binary, out, visibility = None): """ Generates a Homebrew formula with the SHA256 of the binary injected. Args: name: The name of the rule. src: The template file (.rb.tpl). binary: The binary target to hash. out: The output filename (.rb). visibility: Visibility of the rule. """ native.genrule( name = name, srcs = [src, binary], outs = [out], cmd = """ TEMPLATE=$(location %s) BINARY=$(location %s) # Calculate SHA256 if command -v sha256sum >/dev/null 2>&1; then HASH=$$(sha256sum $$BINARY | cut -d' ' -f1) else HASH=$$(shasum -a 256 $$BINARY | cut -d' ' -f1) fi # Substitute into template sed "s/{SHA256}/$$HASH/g" $$TEMPLATE > $@ """ % (src, binary), visibility = visibility, )