Browse Source

add shut.commands.commons.new and shut.commands.mono.new (adding a "shut mono new" command)

shut-new-model
Niklas Rosenstein 9 months ago
parent
commit
47dc16a816
No known key found for this signature in database GPG Key ID: 6D269B33D25F6C6
4 changed files with 214 additions and 51 deletions
  1. + 85
    - 0
      src/shut/commands/commons/new.py
  2. + 1
    - 0
      src/shut/commands/mono/__init__.py
  3. + 115
    - 0
      src/shut/commands/mono/new.py
  4. + 13
    - 51
      src/shut/commands/pkg/new.py

+ 85
- 0
src/shut/commands/commons/new.py

@ -0,0 +1,85 @@
# -*- coding: utf8 -*-
# Copyright (c) 2020 Niklas Rosenstein
#
# 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.
from shore.util.license import get_license_metadata, wrap_license_text # TODO
import subprocess
from typing import Optional
import jinja2
from shut.model.author import Author
from shut.utils.io.virtual import VirtualFiles
from termcolor import colored
GITIGNORE_TEMPLATE = '''
/.venv*/
/dist
/build
*.py[cod]
*.egg-info
*.egg
'''.lstrip()
README_TEMPLATE = '''
# {{project_name}}
---
<p align="center">Copyright &copy; {{year}} {{author.name}}</p>
'''.lstrip()
def load_author_from_git() -> Optional[str]:
"""
Returns a string formatted as "name <mail>" from the Git `user.name` and `user.email`
configuration values. Returns `None` if Git is not configured.
"""
try:
name = subprocess.getoutput('git config user.name')
email = subprocess.getoutput('git config user.email')
except FileNotFoundError:
return None
if not name and not email:
return None
return Author(name, email)
def get_license_file_text(license: str) -> str:
license_text = 'Copyright (c) {year} {author.name}\n\n'.format(**template_vars)
license_text += wrap_license_text(get_license_metadata(license)['license_text'])
return license_text
def render_template(fp, template_string, template_vars):
for data in jinja2.Template(template_string).stream(**template_vars):
fp.write(data)
fp.write('\n')
def write_files(files: VirtualFiles, target_directory: str, force: bool = False, dry: bool = False):
files.write_all(
target_directory,
on_write=lambda fn: print(colored('Write ' + fn, 'cyan')),
on_skip=lambda fn: print(colored('Skip ' + fn, 'yellow')),
overwrite=force,
dry=dry,
)

+ 1
- 0
src/shut/commands/mono/__init__.py

@ -36,4 +36,5 @@ def load_monorepo_manifest() -> Monorepo:
from . import checks
from . import new
from . import status

+ 115
- 0
src/shut/commands/mono/new.py

@ -0,0 +1,115 @@
# -*- coding: utf8 -*-
# Copyright (c) 2020 Niklas Rosenstein
#
# 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.
import datetime
from typing import Optional
from termcolor import colored
import click
import jinja2
from shut.commands.commons.new import (
GITIGNORE_TEMPLATE,
README_TEMPLATE,
load_author_from_git,
get_license_file_text,
render_template,
write_files,
)
from shut.model import dump
from shut.model.author import Author
from shut.model.monorepo import MonorepoModel
from shut.model.release import MonorepoReleaseConfiguration
from shut.model.version import Version
from shut.utils.io.virtual import VirtualFiles
from . import mono
@mono.command(no_args_is_help=True)
@click.argument('target_directory', required=False)
@click.option('--project-name', '--name', metavar='name', required=True, help='The name of the project.')
@click.option('--author', metavar='"name <mail>"', type=Author.parse, help='The name of the author to write into the configuration file. Defaults to the name and email from the Git config.')
@click.option('--version', metavar='x.y.z', help='The version number to start counting from. Defaults to "0.0.0" (stands for "unreleased").')
@click.option('--license', metavar='name', help='The name of the license to use for the project. A LICENSE.txt file will be created.')
@click.option('--url', metavar='url', help='The URL to the project (e.g. the Git repository website).')
@click.option('--single-version', is_flag=True, help='Enable mono repository single-versioning.')
@click.option('--suffix', type=click.Choice(['yaml', 'yml']), help='The suffix for YAML files. Defaults to "yml".', default='yml')
@click.option('--dry', is_flag=True, help='Do not write files to disk.')
@click.option('-f', '--force', is_flag=True, help='Overwrite files if they already exist.')
def new(
target_directory,
project_name,
author,
version,
license,
url,
single_version,
suffix,
dry,
force,
):
"""
Create files for a new Python monorepository. If the *target_directory* is specified, the files
will be written to that directory. Otherwise the value of the --project-name argument will be
used as the target directory.
The following project layout will be created:
\b
project_name/
.gitignore
LICENSE.txt
monorepo.yml
README.md
"""
if not target_directory:
target_directory = project_name
if not author:
author = load_author_from_git() or Author('Unknown', '<unknown@example.org>')
if not version:
version = version or Version('0.0.0')
package_manifest = MonorepoModel(
name=project_name,
version=version,
author=author,
license=license,
url=url,
release=MonorepoReleaseConfiguration(
single_version=single_version,
),
)
template_vars = {
'project_name': project_name,
'version': version,
'author': author,
'year': datetime.date.today().year,
}
files = VirtualFiles()
files.add_static('.gitignore', GITIGNORE_TEMPLATE)
files.add_dynamic('README.md', render_template, README_TEMPLATE, template_vars)
files.add_dynamic('monorepo.' + suffix, lambda fp: dump(package_manifest, fp))
if license:
files.add_static('LICENSE.txt', get_license_file_text(license))
write_files(files, target_directory, force, dry)

+ 13
- 51
src/shut/commands/pkg/new.py

@ -21,6 +21,13 @@
from shore.util.license import get_license_metadata, wrap_license_text
from shut.commands.commons.new import (
load_author_from_git,
render_template,
write_files,
GITIGNORE_TEMPLATE,
README_TEMPLATE,
)
from shut.model import dump
from shut.model.author import Author
from shut.model.package import PackageModel, PackageData
@ -46,39 +53,6 @@ NAMESPACE_INIT_TEMPLATE = '''
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
'''
GITIGNORE_TEMPLATE = '''
/.venv*/
/dist
/build
*.py[cod]
*.egg-info
*.egg
'''.lstrip()
README_TEMPLATE = '''
# {{project_name}}
---
<p align="center">Copyright &copy; {{year}} {{author.name}}</p>
'''.lstrip()
def load_author_from_git() -> Optional[str]:
"""
Returns a string formatted as "name <mail>" from the Git `user.name` and `user.email`
configuration values. Returns `None` if Git is not configured.
"""
try:
name = subprocess.getoutput('git config user.name')
email = subprocess.getoutput('git config user.email')
except FileNotFoundError:
return None
if not name and not email:
return None
return Author(name, email)
@pkg.command(no_args_is_help=True)
@click.argument('target_directory', required=False)
@ -166,21 +140,17 @@ def new(
'year': datetime.date.today().year,
}
def _render_template(fp, template_string):
for data in jinja2.Template(template_string).stream(**template_vars):
fp.write(data)
fp.write('\n')
files = VirtualFiles()
files.add_static('.gitignore', GITIGNORE_TEMPLATE)
files.add_dynamic('README.md', _render_template, README_TEMPLATE)
files.add_dynamic('README.md', render_template, README_TEMPLATE, template_vars)
files.add_dynamic('package.' + suffix, lambda fp: dump(package_manifest, fp))
files.add_dynamic(
'src/{}/__init__.py'.format(module_name.replace('.', '/')),
_render_template,
render_template,
INIT_TEMPLATE,
template_vars,
)
parts = []
@ -192,14 +162,6 @@ def new(
)
if license:
license_text = 'Copyright (c) {year} {author.name}\n\n'.format(**template_vars)
license_text += wrap_license_text(get_license_metadata(license)['license_text'])
files.add_static('LICENSE.txt', license_text)
files.write_all(
target_directory,
on_write=lambda fn: print(colored('Write ' + fn, 'cyan')),
on_skip=lambda fn: print(colored('Skip ' + fn, 'yellow')),
overwrite=force,
dry=dry,
)
files.add_static('LICENSE.txt', get_license_file_text(license))
write_files(files, target_directory, force, dry)

Loading…
Cancel
Save