-
Notifications
You must be signed in to change notification settings - Fork 0
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
fdbe807
commit 51c0dc0
Showing
19 changed files
with
409 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
MIT License | ||
Copyright (c) 2021-Present Shravan Asati | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
MIT License | ||
|
||
Copyright (c) 2021-Present Shravan Asati | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -1,51 +1,179 @@ | ||
import subprocess | ||
import hashlib | ||
from subprocess import run, CalledProcessError | ||
from multiprocessing import Pool, cpu_count | ||
import shlex | ||
import shutil | ||
import os | ||
from multiprocessing import Process | ||
from typing import List | ||
import json | ||
from pathlib import Path | ||
|
||
def build(appname:str, platform: str) -> None: | ||
try: | ||
goos = platform.split("/")[0] | ||
goarch = platform.split("/")[1] | ||
# build config, would be altered by init_config() | ||
APP_NAME = "atomic" | ||
STRIP = True # whether to strip binaries | ||
VERBOSE = False # go compiler verbose output | ||
FORMAT = True # format code before building binaries | ||
PLATFORMS: list[str] = [] # list of platforms to build for | ||
PANDOC_CONVERSION = True # whether to convert readme.md to plain text using pandoc in distributions | ||
|
||
print(f"==> 🚧 Building executable for `{platform}`...") | ||
os.environ["GOOS"] = goos | ||
os.environ["GOARCH"] = goarch | ||
|
||
outpath = f"./bin/{appname}-{goos}-{goarch}" | ||
def hash_file(filename: str): | ||
h = hashlib.sha256() | ||
|
||
if goos == "windows": | ||
outpath += ".exe" | ||
with open(filename, "rb") as file: | ||
chunk = 0 | ||
while chunk != b"": | ||
chunk = file.read(1024) | ||
h.update(chunk) | ||
|
||
subprocess.check_output(["go", "build", "-v", "-o", outpath]) | ||
return h.hexdigest() | ||
|
||
print(f"==> ✅ Built executable for `{platform}` at `{outpath}`.") | ||
|
||
except Exception as e: | ||
print(e) | ||
print("==> ❌ An error occured! Aborting script execution.") | ||
os._exit(1) | ||
def init_config(): | ||
try: | ||
global APP_NAME, STRIP, VERBOSE, FORMAT, PLATFORMS, PANDOC_CONVERSION | ||
release_config_file = Path(__file__).parent.resolve() / 'release.config.json' | ||
with open(str(release_config_file)) as f: | ||
config = json.load(f) | ||
|
||
if __name__ == "__main__": | ||
# add all platforms to the tuple you want to build | ||
platforms = {"windows/amd64", "linux/amd64", "darwin/amd64"} | ||
appname = "atomic" # name of the executable | ||
multithreaded = True # set to True to enable multithreading | ||
APP_NAME = config["app_name"] | ||
STRIP = config["strip_binaries"] | ||
VERBOSE = config["verbose"] | ||
FORMAT = config["format_code"] | ||
PLATFORMS = config["platforms"] | ||
PANDOC_CONVERSION = config["pandoc_conversion"] | ||
|
||
except Exception as e: | ||
print(f"==> ❌ Some error occured while reading the release config:\n{e}") | ||
exit(1) | ||
|
||
|
||
def init_folders() -> None: | ||
""" | ||
Makes sure that the `temp` and `dist` folders exist. | ||
""" | ||
if not os.path.exists("./dist"): | ||
os.mkdir("./dist") | ||
|
||
if not os.path.exists("./temp"): | ||
os.mkdir("./temp") | ||
|
||
|
||
def pack(dir: str, platform: str) -> None: | ||
""" | ||
Copies README, LICENSE, CHANGELOG and atomic logo to the output directory and creates an archive for the given platform. | ||
""" | ||
project_base = Path(__file__).parent.parent | ||
readme_file = project_base / "temp" / "README.txt" | ||
if not readme_file.exists(): | ||
# if pandoc conversion failed or not enabled | ||
readme_file = project_base / "README.md" | ||
|
||
license_file = project_base / "LICENSE.txt" | ||
icon_file = project_base / "assets" / "icon.png" | ||
# changelog_file = project_base / "CHANGELOG.md" | ||
|
||
shutil.copyfile(str(readme_file), f"{dir}/README.txt") | ||
shutil.copyfile(str(license_file), f"{dir}/LICENSE.txt") | ||
# shutil.copyfile(str(changelog_file), f"{dir}/CHANGELOG.md") | ||
# shutil.copyfile(str(icon_file), f"{dir}/icon.png") | ||
|
||
splitted = platform.split("/") | ||
build_os = splitted[0] | ||
build_arch = splitted[1] | ||
|
||
compression = "zip" if build_os == "windows" else "gztar" | ||
|
||
shutil.make_archive(f"dist/{APP_NAME}_{build_os}_{build_arch}", compression, dir) | ||
|
||
|
||
def build(platform: str) -> None: | ||
""" | ||
Calls the go compiler to build the application for the given platform, and the pack function. | ||
""" | ||
try: | ||
print(f"==> 🚧 Building for {platform}.") | ||
splitted = platform.split("/") | ||
build_os = splitted[0] | ||
build_arch = splitted[1] | ||
|
||
output_dir = f"temp/{APP_NAME}_{build_os}_{build_arch}" | ||
if not os.path.exists(output_dir): | ||
os.makedirs(output_dir, exist_ok=True) | ||
|
||
if multithreaded: | ||
threads: List[Process] = [] | ||
executable_path = f"{output_dir}/{APP_NAME}" | ||
if build_os == "windows": | ||
executable_path += ".exe" | ||
|
||
os.environ["GOOS"] = build_os | ||
os.environ["GOARCH"] = build_arch | ||
|
||
run( | ||
shlex.split( | ||
"go build -o {} {} {}".format( | ||
executable_path, | ||
'-ldflags="-s -w"' if STRIP else "", | ||
"-v" if VERBOSE else "", | ||
) | ||
), | ||
check=True, | ||
) | ||
|
||
print(f"==> ✅ Packing for {platform}.") | ||
pack(output_dir, platform) | ||
|
||
except CalledProcessError: | ||
print(f"==> ❌ Failed to build for {platform}: The Go compiler returned an error.") | ||
|
||
except Exception as e: | ||
print(f"==> ❌ Failed to build for {platform}.") | ||
print(e) | ||
|
||
|
||
def generate_checksums() -> None: | ||
project_base = Path(__file__).parent.parent | ||
dist_folder = project_base / "dist" | ||
checksum = "" | ||
|
||
for item in dist_folder.iterdir(): | ||
checksum += f"{hash_file(str(item.absolute()))} {item.name}\n" | ||
|
||
checksum_file = dist_folder / "checksums.txt" | ||
with open(str(checksum_file), 'w') as f: | ||
f.write(checksum) | ||
|
||
|
||
def cleanup() -> None: | ||
""" | ||
Removes the `temp` folder. | ||
""" | ||
print("==> 👍 Cleaning up.") | ||
shutil.rmtree("./temp") | ||
|
||
|
||
if __name__ == "__main__": | ||
print("==> ⌛ Initialising folders, executing prebuild commands.") | ||
init_config() | ||
init_folders() | ||
if FORMAT: | ||
run(shlex.split("go fmt ./..."), check=True) | ||
|
||
for p in platforms: | ||
threads.append(Process(target=build, args=(appname, p))) | ||
try: | ||
if PANDOC_CONVERSION: | ||
readme_file = Path(__file__).parent.parent / "README.md" | ||
run( | ||
f"pandoc -s {str(readme_file)} -o ./temp/README.txt --to plain", | ||
check=True | ||
) | ||
except CalledProcessError: | ||
print("==> ⚠ Unable to convert README.md to README.txt using pandoc in distributions, make sure you've pandoc installed on your system.") | ||
|
||
for t in threads: | ||
t.start() | ||
max_procs = cpu_count() | ||
print(f"==> 🔥 Starting builds with {max_procs} parallel processes.") | ||
|
||
for t in threads: | ||
t.join() | ||
with Pool(processes=max_procs) as pool: | ||
pool.map(build, PLATFORMS) | ||
|
||
else: | ||
for p in platforms: | ||
build(appname, p) | ||
print("==> #️⃣ Generating checksums.") | ||
generate_checksums() | ||
|
||
print(f"==> 👍 Executables for {len(platforms)} platforms built successfully!") | ||
cleanup() |
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 |
---|---|---|
|
@@ -69,4 +69,4 @@ Set Height 1200 | |
|
||
Type "atomic 'scc' 'tokei'" Sleep 500ms Enter | ||
|
||
Sleep 8s | ||
Sleep 15s |
Oops, something went wrong.