binhonglee/GlobeTrotte

View on GitHub
build_defs/sh/sh.build_defs

Summary

Maintainability
Test Coverage
subinclude("//build_defs/gen")

def sh_exec(
  name:str,
  cmd:str,
  outs:list,
  set_home_path:bool=False,
  binary:bool=False,
  deps:list=[],
  tools:dict|list=[],
  visibility:list=[],
  requires:list=["shell"],
):
  if set_home_path:
    cmd = _home_path() + " && " + cmd

  return build_rule(
    name = name,
    cmd = cmd,
    outs = outs,
    binary = binary,
    tools = tools,
    deps = deps,
    visibility = visibility,
    requires = requires,
  )

def sh_tools_cmd(
  name:str,
  srcs:list=[],
  cmd:str,
  set_home_path:bool=False,
  deps:list=[],
  tools:dict|list=[],
  test_only:bool=False,
  visibility:list=[],
  requires:list=["shell"],
):
  if set_home_path:
    cmd = _home_path() + " && " + cmd
  cmd = "\n".join(cmd.split(" && "))
  cmd = _scriptify(cmd)

  return build_rule(
    name = name,
    srcs = srcs,
    cmd = "touch $OUT && " + cmd,
    outs = [name + ".sh"],
    binary = True,
    tools = tools,
    deps = deps,
    test_only = test_only,
    visibility = visibility,
    requires = requires,
  )

def sh_copy_file(
  name:str,
  srcs:list=[],
  out:str,
  deps:list=[],
  visibility:list=[],
  requires:list=["shell"],
):
  return build_rule(
    name = name,
    srcs = srcs,
    cmd = " && ".join([
      _go_to_top_level(),
      "cd $PKG_DIR",
      "if [ -e \"" + out + "\" ]; then rm -rf " + out + "; fi",
      "cd $TMP_DIR",
      "cp -R $SRCS " + _top_level_dir() + "$PKG_DIR",
      "cp -R $SRCS $TMP_DIR || echo 'File already exist.'",
    ]),
    outs = [out],
    deps = deps,
    visibility = visibility,
    requires = requires,
  )

def _scriptify(cmd:str):
  return f'{{ tee -a > "$OUT" << EOF\n#!{CONFIG.SHELL_TOOL}\n{cmd}\nEOF\n}}'

CONFIG.setdefault("SHELL_TOOL", "/bin/sh")