|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from argh.decorators import arg |
| 7 | +from jinja2 import Environment, PackageLoader |
| 8 | +from lain_sdk.util import error, info |
| 9 | + |
| 10 | +TEMPLATE_CHOICES = ["go", "java", "python"] |
| 11 | +env = Environment(loader=PackageLoader("lain_cli", "templates")) |
| 12 | + |
| 13 | + |
| 14 | +@arg("appname", help="the name of the new LAIN app") |
| 15 | +@arg("-t", "--template", choices=TEMPLATE_CHOICES, help="template to use") |
| 16 | +def new(appname, template="go"): |
| 17 | + """ |
| 18 | + Create a LAIN project with template |
| 19 | + """ |
| 20 | + if appname == "": |
| 21 | + error("appname should not be \"\".") |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | + try: |
| 25 | + if template == "go": |
| 26 | + new_go_project(appname) |
| 27 | + elif template == "java": |
| 28 | + new_java_project(appname) |
| 29 | + elif template == "python": |
| 30 | + new_python_project(appname) |
| 31 | + else: |
| 32 | + error("template should in {}.".format(TEMPLATE_CHOICES)) |
| 33 | + sys.exit(1) |
| 34 | + except Exception as e: |
| 35 | + error("{}.".format(e)) |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + |
| 39 | +def new_go_project(appname): |
| 40 | + info("Creating {} with go template...".format(appname)) |
| 41 | + GOPATH = os.environ["GOPATH"] |
| 42 | + GO_SRC_PATH = "{}/src/".format(GOPATH) |
| 43 | + CWD = os.getcwd() |
| 44 | + if not CWD.startswith(GO_SRC_PATH): |
| 45 | + raise Exception("currenct working directory: {} is not in GOPATH: {}". |
| 46 | + format(CWD, GOPATH)) |
| 47 | + |
| 48 | + if os.path.exists(appname): |
| 49 | + raise Exception("directory or file: {} already exists".format(appname)) |
| 50 | + |
| 51 | + info("`git init {}` ...".format(appname)) |
| 52 | + subprocess.run(["git", "init", appname], check=True) |
| 53 | + info("`git init {}` done.".format(appname)) |
| 54 | + |
| 55 | + with open("{}/lain.yaml".format(appname), "w+") as f: |
| 56 | + data = env.get_template("go/lain.yaml.j2").render( |
| 57 | + appname=appname, package_path=CWD.replace(GO_SRC_PATH, "")) |
| 58 | + f.write(data) |
| 59 | + |
| 60 | + with open("{}/main.go".format(appname), "w+") as f: |
| 61 | + data = env.get_template("go/main.go").render() |
| 62 | + f.write(data) |
| 63 | + |
| 64 | + with open("{}/Gopkg.lock".format(appname), "w+") as f: |
| 65 | + data = env.get_template("go/Gopkg.lock").render() |
| 66 | + f.write(data) |
| 67 | + |
| 68 | + with open("{}/Gopkg.toml".format(appname), "w+") as f: |
| 69 | + data = env.get_template("go/Gopkg.toml").render() |
| 70 | + f.write(data) |
| 71 | + |
| 72 | + info("`git commit` ...") |
| 73 | + os.chdir(appname) |
| 74 | + subprocess.run(["git", "add", "-A"], check=True) |
| 75 | + subprocess.run([ |
| 76 | + "git", "-c", "user.name=LAIN", "-c", "user.email=\"\"", "commit", "-m", |
| 77 | + "initial commit" |
| 78 | + ], |
| 79 | + check=True) |
| 80 | + os.chdir(CWD) |
| 81 | + info("`git commit` done.") |
| 82 | + |
| 83 | + info("{} has been created with go template.".format(appname)) |
| 84 | + |
| 85 | + |
| 86 | +def new_java_project(appname): |
| 87 | + info("Creating java project...") |
| 88 | + info("Java project has been created.") |
| 89 | + |
| 90 | + |
| 91 | +def new_python_project(appname): |
| 92 | + info("Creating python project...") |
| 93 | + info("Python project has been created.") |
0 commit comments