-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c6b2f6
commit bf15b16
Showing
9 changed files
with
253 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Generate a build ID for a release or pre-release version.""" | ||
|
||
import argparse | ||
import datetime | ||
import json | ||
import pathlib | ||
from typing import Tuple, Union | ||
|
||
EXT_ROOT = pathlib.Path(__file__).parent.parent | ||
PACKAGE_JSON_PATH = EXT_ROOT / "package.json" | ||
|
||
|
||
def is_even(v: Union[int, str]) -> bool: | ||
"""Returns `True` if `v` is even.""" | ||
return not int(v) % 2 | ||
|
||
|
||
def micro_build_number() -> str: | ||
"""Generates the micro build number. | ||
The format is `1<Julian day><hour><minute>`. | ||
""" | ||
return f"1{datetime.datetime.now(tz=datetime.timezone.utc).strftime('%j%H%M')}" | ||
|
||
|
||
def parse_version(version: str) -> Tuple[str, str, str, str]: | ||
"""Parse a version string into a tuple of version parts.""" | ||
major, minor, parts = version.split(".", maxsplit=2) | ||
try: | ||
micro, suffix = parts.split("-", maxsplit=1) | ||
except ValueError: | ||
micro = parts | ||
suffix = "" | ||
return major, minor, micro, suffix | ||
|
||
|
||
def main(package_json: pathlib.Path, *, release: bool) -> None: | ||
package = json.loads(package_json.read_text(encoding="utf-8")) | ||
|
||
major, minor, micro, suffix = parse_version(package["version"]) | ||
|
||
if release and not is_even(minor): | ||
raise ValueError( | ||
f"Release version should have EVEN numbered minor version: " | ||
f"{package['version']}" | ||
) | ||
elif not release and is_even(minor): | ||
raise ValueError( | ||
f"Pre-Release version should have ODD numbered minor version: " | ||
f"{package['version']}" | ||
) | ||
|
||
if release: | ||
print(micro) | ||
else: | ||
print(micro_build_number()) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Generate a build ID for a release or pre-release version." | ||
) | ||
parser.add_argument( | ||
"--release", | ||
action="store_true", | ||
help="Treats the current build as a release build.", | ||
) | ||
args = parser.parse_args() | ||
|
||
main(PACKAGE_JSON_PATH, release=args.release) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"""Update the Python extension micro version based on a build ID.""" | ||
|
||
import argparse | ||
import json | ||
import pathlib | ||
from typing import Tuple, Union | ||
|
||
EXT_ROOT = pathlib.Path(__file__).parent.parent | ||
PACKAGE_JSON_PATH = EXT_ROOT / "package.json" | ||
|
||
|
||
def is_even(v: Union[int, str]) -> bool: | ||
"""Returns True if `v` is even.""" | ||
return not int(v) % 2 | ||
|
||
|
||
def parse_version(version: str) -> Tuple[str, str, str, str]: | ||
"""Parse a version string into a tuple of version parts.""" | ||
major, minor, parts = version.split(".", maxsplit=2) | ||
try: | ||
micro, suffix = parts.split("-", maxsplit=1) | ||
except ValueError: | ||
micro = parts | ||
suffix = "" | ||
return major, minor, micro, suffix | ||
|
||
|
||
def main( | ||
package_json: pathlib.Path, *, release: bool, build_id: int, for_publishing: bool | ||
) -> None: | ||
package = json.loads(package_json.read_text(encoding="utf-8")) | ||
|
||
major, minor, micro, suffix = parse_version(package["version"]) | ||
|
||
if release and not is_even(minor): | ||
raise ValueError( | ||
f"Release version should have EVEN numbered minor version: " | ||
f"{package['version']}" | ||
) | ||
elif not release and is_even(minor): | ||
raise ValueError( | ||
f"Pre-Release version should have ODD numbered minor version: " | ||
f"{package['version']}" | ||
) | ||
|
||
# The build ID should fall within the 0-INT32 max range allowed by the Marketplace. | ||
if build_id < 0 or (for_publishing and build_id > ((2**32) - 1)): | ||
raise ValueError(f"Build ID must be within [0, {(2**32) - 1}]") | ||
|
||
print(f"Updating build FROM: {package['version']}") | ||
package["version"] = ".".join((major, minor, str(build_id))) | ||
if not for_publishing and not release and len(suffix): | ||
package["version"] += "-" + suffix | ||
print(f"Updating build TO: {package['version']}") | ||
|
||
# Overwrite package.json with new data. | ||
package_json.write_text( | ||
json.dumps(package, indent=4, ensure_ascii=False) + "\n", encoding="utf-8" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Update the Python extension micro version based on a build ID." | ||
) | ||
parser.add_argument( | ||
"--release", | ||
action="store_true", | ||
help="Treats the current build as a release build.", | ||
) | ||
parser.add_argument( | ||
"--build-id", | ||
action="store", | ||
type=int, | ||
help="Micro version to use.", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--for-publishing", | ||
action="store_true", | ||
help="Removes `-dev` or `-rc` suffix.", | ||
) | ||
args = parser.parse_args() | ||
|
||
main( | ||
PACKAGE_JSON_PATH, | ||
release=args.release, | ||
build_id=args.build_id, | ||
for_publishing=args.for_publishing, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.