|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +import click |
| 7 | + |
| 8 | + |
| 9 | +@click.command() |
| 10 | +@click.option("--repo-path", help="Path to an existing sync_gateway repo") |
| 11 | +@click.option("--git-tag", help="Build tag (will clone/reset a local repo to this tag)") |
| 12 | +def main(repo_path, git_tag): |
| 13 | + """Build Sync Gateway""" |
| 14 | + if bool(repo_path) == bool(git_tag): |
| 15 | + raise click.UsageError( |
| 16 | + "Exactly one of --repo-path or --git-tag must be provided." |
| 17 | + ) |
| 18 | + |
| 19 | + env_local_dir = os.path.dirname(os.path.abspath(__file__)) |
| 20 | + output_bin = os.path.join(env_local_dir, "sync_gateway") |
| 21 | + |
| 22 | + if repo_path: |
| 23 | + repo_dir = os.path.abspath(repo_path) |
| 24 | + if not os.path.isdir(repo_dir): |
| 25 | + click.secho(f"Error: Repository path {repo_dir} does not exist.", fg="red") |
| 26 | + sys.exit(1) |
| 27 | + else: |
| 28 | + # We use a local clone |
| 29 | + repo_dir = os.path.join(env_local_dir, "sync_gateway_clone") |
| 30 | + repo_url = "https://github.com/couchbase/sync_gateway.git" |
| 31 | + |
| 32 | + if not os.path.exists(repo_dir): |
| 33 | + click.echo(f"Cloning {repo_url} into {repo_dir}...") |
| 34 | + subprocess.check_call(["git", "clone", repo_url, repo_dir]) |
| 35 | + |
| 36 | + click.echo(f"Fetching updates and checking out {git_tag}...") |
| 37 | + subprocess.check_call(["git", "fetch", "--all", "--tags"], cwd=repo_dir) |
| 38 | + subprocess.check_call(["git", "reset", "--hard"], cwd=repo_dir) |
| 39 | + subprocess.check_call(["git", "checkout", git_tag], cwd=repo_dir) |
| 40 | + |
| 41 | + click.echo(f"Building sync_gateway in {repo_dir}...") |
| 42 | + build_cmd = ["go", "build", "-tags", "cb_sg_enterprise", "-o", output_bin, "."] |
| 43 | + |
| 44 | + try: |
| 45 | + subprocess.check_call(build_cmd, cwd=repo_dir) |
| 46 | + click.secho(f"Successfully built sync_gateway to {output_bin}", fg="green") |
| 47 | + except subprocess.CalledProcessError as e: |
| 48 | + click.secho(f"Error building sync_gateway: {e}", fg="red") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments