Skip to content

Commit

Permalink
✨ Awesome release management
Browse files Browse the repository at this point in the history
Improve the release.py script with the following changes:

- Add colorful ASCII art banner for visual appeal
- Implement color-coded output for better readability
- Enhance error handling and reporting
- Update file paths and build process
- Improve GitHub release creation
- Refactor code structure for better organization
  • Loading branch information
hyperb1iss committed Aug 14, 2024
1 parent 53f98fd commit 9cab141
Showing 1 changed file with 117 additions and 39 deletions.
156 changes: 117 additions & 39 deletions scripts/release.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,162 @@
#!/usr/bin/env python3

import json
import os
import re
import subprocess
import sys
from datetime import datetime

import json
import semver
from github import Github
from colorama import Fore, Style, init

# Initialize colorama
init(autoreset=True)

# Colorful ASCII Art Banner
LOGO = f"""
{Fore.CYAN} ・ 。 ☆ ∴。  ・゚*。★・ ∴。  ・゚*。☆ ・ 。 ☆ ∴。  
{Fore.YELLOW} ╭──────────────────────────────────────────────╮
{Fore.MAGENTA} │ ██╗ ██╗██╗ ██╗██████╗ ███████╗██████╗ │
{Fore.MAGENTA} │ ██║ ██║╚██╗ ██╔╝██╔══██╗██╔════╝██╔══██╗ │
{Fore.MAGENTA} │ ███████║ ╚████╔╝ ██████╔╝█████╗ ██████╔╝ │
{Fore.MAGENTA} │ ██╔══██║ ╚██╔╝ ██╔═══╝ ██╔══╝ ██╔══██╗ │
{Fore.MAGENTA} │ ██║ ██║ ██║ ██║ ███████╗██║ ██║ │
{Fore.MAGENTA} │ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ │
{Fore.CYAN} │ ██╗ ██╗ ██████╗ ██╗ ██╗████████╗ │
{Fore.CYAN} │ ██║ ██║██╔════╝ ██║ ██║╚══██╔══╝ │
{Fore.CYAN} │ ██║ ██║██║ ███╗███████║ ██║ │
{Fore.CYAN} │ ██║ ██║██║ ██║██╔══██║ ██║ │
{Fore.CYAN} │ ███████╗██║╚██████╔╝██║ ██║ ██║ │
{Fore.CYAN} │ ╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ │
{Fore.GREEN} │ ██████╗ █████╗ ██████╗ ██████╗ │
{Fore.GREEN} │ ██╔════╝██╔══██╗██╔══██╗██╔══██╗ │
{Fore.GREEN} │ ██║ ███████║██████╔╝██║ ██║ │
{Fore.GREEN} │ ██║ ██╔══██║██╔══██╗██║ ██║ │
{Fore.GREEN} │ ╚██████╗██║ ██║██║ ██║██████╔╝ │
{Fore.GREEN} │ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ │
{Fore.YELLOW} ╰──────────────────────────────────────────────╯
{Fore.CYAN} ∴。  ・゚*。☆ Release Manager ☆。*゚・  。∴
{Fore.YELLOW} ・ 。 ☆ ∴。  ・゚*。★・ ∴。  ・゚*。☆ ・ 。 ☆ ∴。  
"""

# Configuration
REPO_NAME = "hyperb1iss/hyper-light-card"
MAIN_FILE = "src/hyper-light-card.ts"
FILE = "hyper-light-card.js"
DIST = "dist/" + FILE
TARGET = "target/" + FILE
CHANGELOG_FILE = "CHANGELOG.md"
HACS_MANIFEST = "hacs.json"


def print_logo():
print(LOGO)


def print_step(step):
print(Fore.BLUE + f"\n{step}" + Style.RESET_ALL)


def print_error(message):
print(Fore.RED + f"❌ Error: {message}" + Style.RESET_ALL)


def get_current_version():
with open("package.json", "r") as file:
package_json = json.load(file)
return package_json.get("version")
try:
with open("package.json", "r") as file:
package_json = json.load(file)
return package_json.get("version")
except FileNotFoundError:
print_error("package.json not found.")
sys.exit(1)
except json.JSONDecodeError:
print_error("Invalid JSON in package.json.")
sys.exit(1)


def update_version(new_version):
with open("package.json", "r+") as file:
package_json = json.load(file)
package_json["version"] = new_version
file.seek(0)
json.dump(package_json, file, indent=2)
file.truncate()
try:
with open("package.json", "r+") as file:
package_json = json.load(file)
package_json["version"] = new_version
file.seek(0)
json.dump(package_json, file, indent=2)
file.truncate()
print(
Fore.YELLOW
+ f"✅ Updated version in package.json to {new_version}"
+ Style.RESET_ALL
)
except Exception as e:
print_error(f"Failed to update version: {str(e)}")
sys.exit(1)


def build():
print_step("Rebuilding")
try:
subprocess.run(["npm", "run", "build"], check=True)
print(Fore.YELLOW + "✅ Build completed successfully" + Style.RESET_ALL)
except subprocess.CalledProcessError as e:
print_error(f"Build process failed: {str(e)}")
sys.exit(1)


def commit_and_push(version):
subprocess.run(["git", "add", MAIN_FILE, CHANGELOG_FILE, HACS_MANIFEST])
subprocess.run(["git", "commit", "-m", f":bookmark: Bump version to {version}"])
subprocess.run(["git", "push"])
subprocess.run(["git", "tag", f"v{version}"])
subprocess.run(["git", "push", "--tags"])


def create_github_release(version, changelog_entry):
g = Github(os.environ.get("GITHUB_TOKEN"))
repo = g.get_repo(REPO_NAME)
repo.create_git_release(
f"v{version}",
f"Release {version}",
changelog_entry,
draft=False,
prerelease=False,
)
print_step("Committing and pushing changes")
try:
subprocess.run(["cp", "-f", TARGET, DIST], check=True)
subprocess.run(["git", "add", DIST, HACS_MANIFEST], check=True)
subprocess.run(
["git", "commit", "-m", f":sparkles: Release version {version}"], check=True
)
subprocess.run(["git", "push"], check=True)
subprocess.run(["git", "tag", f"v{version}"], check=True)
subprocess.run(["git", "push", "--tags"], check=True)
print(
Fore.YELLOW
+ f"✅ Changes committed and pushed for version {version}"
+ Style.RESET_ALL
)
except subprocess.CalledProcessError as e:
print_error(f"Git operations failed: {str(e)}")
sys.exit(1)


def main():
print_logo()
print_step("Starting release process")

current_version = get_current_version()
if not current_version:
print("Error: Couldn't find current version.")
print_error("Couldn't find current version.")
sys.exit(1)

print(f"Current version: {current_version}")
new_version = input("Enter new version: ")
print(Fore.CYAN + f"Current version: {current_version}" + Style.RESET_ALL)
new_version = input(Fore.MAGENTA + "Enter new version: " + Style.RESET_ALL)

try:
semver.parse(new_version)
except ValueError:
print("Error: Invalid semantic version.")
print_error("Invalid semantic version.")
sys.exit(1)

update_version(new_version)

build()

print(
"Changes made. Please review and press Enter to commit and push, or Ctrl+C to abort."
Fore.MAGENTA
+ "\n✨ Changes made. Please review and press Enter to commit and push, or Ctrl+C to abort."
+ Style.RESET_ALL
)
input()

commit_and_push(new_version)

create_github_release(new_version, changelog_entry)

print(f"Version {new_version} has been released!")
print(
Fore.GREEN
+ f"\n🎉✨ hyper-light-card v{new_version} has been successfully released! ✨🎉"
+ Style.RESET_ALL
)


if __name__ == "__main__":
Expand Down

0 comments on commit 9cab141

Please sign in to comment.