Ejemplo n.º 1
0
def update_projectrc(variables: Dict[str, str]) -> None:

    today = date.today().strftime("%Y-%m-%d")
    annotation = f"# {UPDATE_LABEL} {today}"
    with open(PROJECTRC) as f:
        lines = f.readlines()
        append_additional_lines: List[str] = []

        blanks = get_projectrc_variables_indentation(lines)
        if blanks == 0:  # pragma: no cover
            print_and_exit(
                "Malformed .projectrc file, can't find an env block")

        pref = " " * blanks

        for variable, value in variables.items():
            for index, line in enumerate(lines):
                # If the variable is found in .projectrc, let's update it
                if line.strip().startswith(variable):
                    lines[
                        index] = f'{pref}{variable}: "{value}"  {annotation}\n'
                    break
            # if the variable is not found in .projectrc, let's append as additional
            else:
                append_additional_lines.append(
                    f'{pref}{variable}: "{value}"  {annotation}\n')

    templating = Templating()
    templating.make_backup(PROJECTRC)
    with open(PROJECTRC, "w") as f:
        last_line = ""
        for line in lines + append_additional_lines:
            last_line = line
            f.write(line)
            if not line.endswith("\n"):
                f.write("\n")

        # If last line is not an empty line, let's add a newline at the end of file
        if last_line.strip():
            f.write("\n")

    # Write again the .env file
    Application.get_controller().load_projectrc()
    Application.get_controller().read_specs(read_extended=True)
    Application.get_controller().make_env()
Ejemplo n.º 2
0
def create_service(
    project_scaffold: Project,
    name: str,
    services: List[str],
    auth: str,
    force: bool,
    add_tests: bool,
) -> None:
    path = project_scaffold.p_path("frontend", "app", "services")
    path.mkdir(parents=True, exist_ok=True)

    path = path.joinpath(f"{name}.ts")

    templating = Templating()
    create_template(
        templating,
        "service_template.ts",
        path,
        name,
        services,
        auth,
        force,
        project_scaffold.project,
    )

    log.info("Service created: {}", path)

    module_path = project_scaffold.p_path("frontend", "app",
                                          "custom.module.ts")

    module = None
    with open(module_path) as f:
        module = f.read().splitlines()

    normalized_name = name.title().replace(" ", "")
    SNAME = f"{normalized_name}Service"

    # Add service import
    import_line = f"import {{ {SNAME} }} from '@app/services/{name}';"
    for idx, row in enumerate(module):
        if import_line in row:
            log.info("Import already included in module file")
            break

        if row.strip().startswith("const") or row.strip().startswith("@"):
            module = module[:idx] + [import_line, ""] + module[idx:]
            log.info("Added {} to module file", import_line)
            break

    # Add service declaration
    for idx, row in enumerate(module):
        if row.strip().startswith("declarations"):
            module = module[:idx + 1] + [f"    {SNAME},"] + module[idx + 1:]
            log.info("Added {} to module declarations", SNAME)
            break

    templating.make_backup(module_path)
    # Save new module file
    with open(module_path, "w") as f:
        for row in module:
            f.write(f"{row}\n")
        f.write("\n")

    if add_tests:
        log.warning("Tests for services not implemented yet")
Ejemplo n.º 3
0
def create_component(
    project_scaffold: Project,
    name: str,
    services: List[str],
    auth: str,
    force: bool,
    add_tests: bool,
) -> None:
    path = project_scaffold.p_path("frontend", "app", "components", name)
    path.mkdir(parents=True, exist_ok=True)

    # Used by Frontend during tests
    is_sink_special_case = name == "sink"

    if is_sink_special_case:
        template_ts = "sink.ts"
        template_html = "sink.html"
    else:
        template_ts = "component_template.ts"
        template_html = "component_template.html"

    cpath = path.joinpath(f"{name}.ts")
    templating = Templating()
    create_template(
        templating,
        template_ts,
        cpath,
        name,
        services,
        auth,
        force,
        project_scaffold.project,
    )

    hpath = path.joinpath(f"{name}.html")
    create_template(
        templating,
        template_html,
        hpath,
        name,
        services,
        auth,
        force,
        project_scaffold.project,
    )

    log.info("Component created: {}", path)

    module_path = project_scaffold.p_path("frontend", "app",
                                          "custom.module.ts")

    module = None
    with open(module_path) as f:
        module = f.read().splitlines()

    normalized_name = name.title().replace(" ", "")
    CNAME = f"{normalized_name}Component"

    # Add component import
    import_line = f"import {{ {CNAME} }} from '@app/components/{name}/{name}';"

    for idx, row in enumerate(module):
        if import_line in row:
            log.info("Import already included in module file")
            break

        if row.strip().startswith("const") or row.strip().startswith("@"):
            module = module[:idx] + [import_line, ""] + module[idx:]
            log.info("Added {} to module file", import_line)
            break

    # Add sink route
    if is_sink_special_case:
        for idx, row in enumerate(module):
            if row.strip().startswith("const routes: Routes = ["):
                ROUTE = """
  {
    path: "app/sink",
    component: SinkComponent,
  },

"""
                module = module[:idx + 1] + [ROUTE] + module[idx + 1:]
                log.info("Added route to module declarations")
                break

    # Add component declaration
    for idx, row in enumerate(module):
        if row.strip().startswith("declarations"):
            module = module[:idx + 1] + [f"    {CNAME},"] + module[idx + 1:]
            log.info("Added {} to module declarations", CNAME)
            break

    templating.make_backup(module_path)
    # Save new module file
    with open(module_path, "w") as f:
        for row in module:
            f.write(f"{row}\n")
        f.write("\n")

    if add_tests:
        path = project_scaffold.p_path("frontend", "app", "components", name)
        path = path.joinpath(f"{name}.spec.ts")

        create_template(
            templating,
            "component_test_template.spec.ts",
            path,
            name,
            services,
            auth,
            force,
            project_scaffold.project,
        )

        log.info("Tests scaffold created: {}", path)