blob: aae137b10d2d25b618deebe0f1f6a91823feccd7 [file]
"""
Setup code for setting up binaries for use
"""
load("@bazel_skylib//lib:new_sets.bzl", "sets")
load("@bazel_skylib//lib:types.bzl", "types")
_TOOL_NAMES = ["buildifier", "buildozer"]
_TYPICAL_PLATFORMS = ["darwin", "linux"]
_TYPICAL_ARCHES = ["amd64", "arm64"]
_VALID_TOOL_NAMES = sets.make(_TOOL_NAMES)
def _create_asset(name, platform, arch, version, sha256 = None):
"""Create a `struct` representing a buildtools asset.
Args:
name: The name of the asset (e.g. `buildifier`, `buildozer`) as a
`string`.
platform: The platform as a `string`. (e.g. `linux`, `darwin`)
arch: The arch as a `string`. (e.g. `amd64`, `arm64`)
version: The version as a `string`. (e.g. `4.2.3`)
sha256: Optional. The sha256 as a `string`.
Returns:
A `struct` representing the asset to be downloaded.
"""
if name == None:
fail("Expected a name.")
if platform == None:
fail("Expected a platform.")
if arch == None:
fail("Expected an arch.")
if version == None:
fail("Expected a version.")
if not sets.contains(_VALID_TOOL_NAMES, name):
fail("Invalid asset name. {name}".format(name = name))
return struct(
name = name,
platform = platform,
arch = arch,
version = version,
sha256 = sha256,
)
def _create_unique_name(asset = None, name = None, platform = None, arch = None):
"""Create a unique name from an asset or from a name/platform/arch.
Args:
asset: An asset `struct` as returned by `buildtools.create_asset`.
name: A tool name (e.g. buildifier) as `string`.
platform: A platform as `string`.
arch: An arch as `string`.
Returns:
A `string` suitable for use as identifying an asset.
"""
if asset != None:
name = asset.name
platform = asset.platform
arch = asset.arch
if name == None or platform == None or arch == None:
fail("An asset or name/platform/arch must be specified.")
return "{name}_{platform}_{arch}".format(
name = name,
platform = platform,
arch = arch,
)
def _asset_to_json(asset):
"""Returns the JSON representation for an asset `struct` or a `list` of asset `struct` values.
Args:
asset: An asset `struct` as returned by `buildtools.create_asset`.
Returns:
Returns a JSON `string` representation of the provided value.
"""
return json.encode(asset)
def _asset_from_json(json_str):
"""Returns an asset `struct` or a `list` of asset `struct` values as represented by the JSON `string`.
Args:
json_str: A JSON `string` representing an asset or a list of assets.
Returns:
An asset `struct` or a `list` of asset `struct` values.
"""
result = json.decode(json_str)
if types.is_list(result):
return [_create_asset(**a) for a in result]
elif types.is_dict(result):
return _create_asset(**result)
fail("Unexpected result type decoding JSON string. %s" % (json_str))
def _create_assets(
version,
names = _TOOL_NAMES,
platforms = _TYPICAL_PLATFORMS,
arches = _TYPICAL_ARCHES,
sha256_values = {}):
"""Create a `list` of asset `struct` values.
Args:
version: The buildtools version string.
names: Optional. A `list` of tools to include.
platforms: Optional. A `list` of platforms to include.
arches: Optional. A `list` of arches to include.
sha256_values: Optional. A `dict` of asset name to sha256.
Returns:
A `list` of buildtools assets.
"""
if version == None:
fail("Expected a version.")
if names == None or names == []:
fail("Expected a non-empty list for names.")
if platforms == None or platforms == []:
fail("Expected a non-empty list for platforms.")
if arches == None or arches == []:
fail("Expected a non-empty list for arches.")
if sha256_values == None:
sha256_values = {}
assets = []
for name in names:
for platform in platforms:
for arch in arches:
uniq_name = _create_unique_name(
name = name,
platform = platform,
arch = arch,
)
assets.append(_create_asset(
name = name,
platform = platform,
arch = arch,
version = version,
sha256 = sha256_values.get(uniq_name),
))
return assets
_DEFAULT_ASSETS = _create_assets(
version = "6.0.0",
names = ["buildifier", "buildozer"],
platforms = ["darwin", "linux"],
arches = ["amd64", "arm64"],
sha256_values = {
"buildifier_darwin_amd64": "3f8ab7dd5d5946ce44695f29c3b895ad11a9a6776c247ad5273e9c8480216ae1",
"buildifier_darwin_arm64": "21fa0d48ef0b7251eb6e3521cbe25d1e52404763cd2a43aa29f69b5380559dd1",
"buildifier_linux_amd64": "7ff82176879c0c13bc682b6b0e482d670fbe13bbb20e07915edb0ad11be50502",
"buildifier_linux_arm64": "9ffa62ea1f55f420c36eeef1427f71a34a5d24332cb861753b2b59c66d6343e2",
"buildozer_darwin_amd64": "17c97b23ebf0aa59c3c457800090e5d9b937511bafbe91d22aec972fbdf588d0",
"buildozer_darwin_arm64": "8d5e26446cd5a945588b1e0c72854d2cc367fac98d16ddeccbc59b0c87a9a05e",
"buildozer_linux_amd64": "b46c12c81ab45306d3bbb4b3a6cd795532d1c3036ed126fbc43fde23d6c35f2d",
"buildozer_linux_arm64": "548c3a6c890ef5cc4398d5afeb1399717b43740eb910f7488a36b76440ca0383",
},
)
buildtools = struct(
create_asset = _create_asset,
create_unique_name = _create_unique_name,
asset_to_json = _asset_to_json,
asset_from_json = _asset_from_json,
create_assets = _create_assets,
DEFAULT_ASSETS = _DEFAULT_ASSETS,
)