Skip to content

Commit

Permalink
Generate project licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
RauliL committed Dec 6, 2023
1 parent 4aed9e8 commit e60d441
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 45 deletions.
55 changes: 50 additions & 5 deletions cfug/command/new.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import click
import license
import os

from pathvalidate import sanitize_filename
from typing import Optional
from typing import Any, Optional

from ..project import Project
from ..template import ProjectTemplate


@click.command(help="Initializes new project.")
@click.option(
"--template",
"template_name",
type=str,
default="executable",
help="Which project template to use. Available options are: executable, header-only and library.",
Expand All @@ -22,6 +25,25 @@
prompt="Version",
help="Initial version number of the project.",
)
@click.option(
"--license",
"license_name",
type=str,
required=False,
default="",
prompt="License",
help="License of the project, if any.",
)
@click.option(
"--author", type=str, required=False, default="", help="Name of the author."
)
@click.option(
"--email",
type=str,
required=False,
default="",
help="E-mail address of the author.",
)
@click.option(
"--description",
type=str,
Expand All @@ -40,21 +62,44 @@
)
@click.argument("project-name")
def new(
template: str,
template_name: str,
version: Optional[str],
license_name: Optional[str],
author: Optional[str],
email: Optional[str],
description: Optional[str],
homepage_url: Optional[str],
project_name: str,
):
template = ProjectTemplate.find(template_name)
if not template:
raise click.BadParameter(f"Unrecognized template: '{template_name}'")

if sanitize_filename(project_name) != project_name:
raise click.BadParameter(f"Invalid project name: '{project_name}'")

if os.path.exists(project_name):
raise click.BadParameter(f"File/directory '{project_name}' already exists")

Project(
root_directory=os.path.realpath(project_name),
li: Optional[Any] = None
if license_name:
try:
li = license.find(license_name)
except KeyError:
raise click.BadParameter(f"Unrecognized license: '{license_name}'")

if not author:
author = click.prompt("Author's name")

if not email:
email = click.prompt("Author's E-mail address")

Project(root_directory=os.path.realpath(project_name)).create(
template=template,
version=version,
description=description,
homepage_url=homepage_url,
).initialize(template_name=template)
license=li,
author=author,
email=email,
)
10 changes: 0 additions & 10 deletions cfug/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,3 @@ def __init__(self):
super().__init__(
"Project has not been configured yet. Please run `cfug configure` first."
)


class TemplateDoesNotExistError(CFugError):
"""
Exception that is thrown when user attempts to initialize an project with
a template that does not exist.
"""

def __init__(self, template_name: str):
super().__init__(f"Template does not exist: {template_name}")
34 changes: 20 additions & 14 deletions cfug/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from caseconvertor import camelcase
from pathlib import Path
from typing import Optional
from typing import Any, Optional

from .exceptions import ProjectNotConfiguredError, ProjectNotFoundError
from .template import ProjectTemplate
Expand All @@ -23,17 +23,8 @@ def find(cls) -> "Project":

raise ProjectNotFoundError()

def __init__(
self,
root_directory: str,
version: Optional[str] = None,
description: Optional[str] = None,
homepage_url: Optional[str] = None,
):
def __init__(self, root_directory: str):
self.root_directory = root_directory
self.version = version
self.description = description
self.homepage_url = homepage_url

@property
def name(self) -> str:
Expand All @@ -44,10 +35,20 @@ def name(self) -> str:
def build_directory(self) -> str:
return os.path.join(self.root_directory, "build")

def initialize(self, template_name: str):
# Find the template to use for the new project.
template = ProjectTemplate.find(template_name)
def create(
self,
template: ProjectTemplate,
version: Optional[str] = None,
description: Optional[str] = None,
homepage_url: Optional[str] = None,
license: Optional[Any] = None,
author: Optional[str] = "",
email: Optional[str] = "",
):
template.context["project"] = self
template.context["version"] = version
template.context["description"] = description
template.context["homepage_url"] = homepage_url

# Create the project directory.
os.mkdir(self.root_directory)
Expand All @@ -59,6 +60,11 @@ def initialize(self, template_name: str):
# Initialize git repository.
pygit2.init_repository(self.root_directory)

# Render license, if one was given.
if license:
with open(os.path.join(self.root_directory, "LICENSE"), "w") as f:
f.write(license.render(name=author, email=email))

# Install the template.
template.install(self.root_directory)

Expand Down
8 changes: 3 additions & 5 deletions cfug/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

from jinja2 import Template
from pathvalidate import sanitize_filename
from typing import Any, Dict

from ..exceptions import TemplateDoesNotExistError
from typing import Any, Dict, Optional


class ProjectTemplate:
@classmethod
def find(cls, name: str) -> "ProjectTemplate":
def find(cls, name: str) -> Optional["ProjectTemplate"]:
directory = os.path.realpath(
os.path.join(os.path.realpath(__file__), "..", sanitize_filename(name))
)

if not os.path.isdir(directory):
raise TemplateDoesNotExistError(name)
return None

return cls(directory=directory)

Expand Down
6 changes: 3 additions & 3 deletions cfug/template/executable/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.11)

PROJECT(
{{ project.name|lower }}
{% if project.version %} VERSION {{ project.version }}{% endif %}
{% if project.description %} DESCRIPTION "{{ project.description }}"{% endif %}
{% if project.homepage_url %} HOMEPAGE_URL "{{ project.homepage_url }}"{% endif %}
{% if version %} VERSION {{ version }}{% endif %}
{% if description %} DESCRIPTION "{{ description }}"{% endif %}
{% if homepage_url %} HOMEPAGE_URL "{{ homepage_url }}"{% endif %}
LANGUAGES C CXX
)

Expand Down
6 changes: 3 additions & 3 deletions cfug/template/header-only/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.11)

PROJECT(
{{ project.name }}
{% if project.version %} VERSION {{ project.version }}{% endif %}
{% if project.description %} DESCRIPTION "{{ project.description }}"{% endif %}
{% if project.homepage_url %} HOMEPAGE_URL "{{ project.homepage_url }}"{% endif %}
{% if version %} VERSION {{ version }}{% endif %}
{% if description %} DESCRIPTION "{{ description }}"{% endif %}
{% if homepage_url %} HOMEPAGE_URL "{{ homepage_url }}"{% endif %}
LANGUAGES C CXX
)

Expand Down
6 changes: 3 additions & 3 deletions cfug/template/library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.11)

PROJECT(
{{ project.name|lower }}
{% if project.version %} VERSION {{ project.version }}{% endif %}
{% if project.description %} DESCRIPTION "{{ project.description }}"{% endif %}
{% if project.homepage_url %} HOMEPAGE_URL "{{ project.homepage_url }}"{% endif %}
{% if version %} VERSION {{ version }}{% endif %}
{% if description %} DESCRIPTION "{{ description }}"{% endif %}
{% if homepage_url %} HOMEPAGE_URL "{{ homepage_url }}"{% endif %}
LANGUAGES C CXX
)

Expand Down
17 changes: 15 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pathvalidate = "^3.2.0"
pygit2 = "^1.13.3"
jinja2 = "^3.1.2"
case-convertor = "^1.1.2"
license = "^0.1a3"

[tool.poetry.group.dev.dependencies]
black = "^23.11.0"
Expand Down

0 comments on commit e60d441

Please sign in to comment.