Updated project type library logic & fixed tests #144
This commit is contained in:
@@ -19,13 +19,10 @@ class Library(ProjectTypeABC):
|
||||
project_file_data: dict,
|
||||
):
|
||||
from project_file import ProjectFile
|
||||
from project_file_appsettings import ProjectFileAppsettings
|
||||
from project_file_code_application import ProjectFileApplication
|
||||
from project_file_code_main import ProjectFileMain
|
||||
from project_file_code_startup import ProjectFileStartup
|
||||
from project_file_readme import ProjectFileReadme
|
||||
from project_file_license import ProjectFileLicense
|
||||
from schematic_init import Init
|
||||
from schematic_class import Class
|
||||
|
||||
ProjectTypeABC.__init__(self, base_path, project_name, workspace, use_application_api, use_startup, use_service_providing, use_async, project_file_data)
|
||||
|
||||
@@ -35,15 +32,6 @@ class Library(ProjectTypeABC):
|
||||
if workspace is None:
|
||||
self.add_template(ProjectFileLicense(''))
|
||||
self.add_template(ProjectFileReadme(''))
|
||||
self.add_template(Init('', 'init', f'{base_path}tests/'))
|
||||
|
||||
self.add_template(Init('', 'init', project_path))
|
||||
self.add_template(ProjectFileAppsettings(project_path))
|
||||
|
||||
if use_application_api:
|
||||
self.add_template(ProjectFileApplication(project_path, use_application_api, use_startup, use_service_providing, use_async))
|
||||
|
||||
if use_startup:
|
||||
self.add_template(ProjectFileStartup(project_path, use_application_api, use_startup, use_service_providing, use_async))
|
||||
|
||||
self.add_template(ProjectFileMain(project_name.split('/')[-1], project_path, use_application_api, use_startup, use_service_providing, use_async))
|
||||
self.add_template(Class('Class1', 'class', project_path))
|
||||
|
@@ -4,8 +4,8 @@ from cpl_core.utils import String
|
||||
|
||||
class Class(GenerateSchematicABC):
|
||||
|
||||
def __init__(self, name: str, path: str, schematic: str):
|
||||
GenerateSchematicABC.__init__(self, name, path, schematic)
|
||||
def __init__(self, name: str, schematic: str, path: str):
|
||||
GenerateSchematicABC.__init__(self, name, schematic, path)
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
|
@@ -34,6 +34,7 @@ class GenerateService(CommandABC):
|
||||
self._config = configuration
|
||||
self._env = self._config.environment
|
||||
self._schematics = {}
|
||||
self._schematic_classes = set()
|
||||
|
||||
for package_name, version in Dependencies.get_cpl_packages():
|
||||
if package_name == 'cpl-cli':
|
||||
@@ -44,16 +45,12 @@ class GenerateService(CommandABC):
|
||||
self._read_custom_schematics_from_path(self._env.working_directory)
|
||||
self._read_custom_schematics_from_path(self._env.runtime_directory)
|
||||
|
||||
if len(GenerateSchematicABC.__subclasses__()) == 0:
|
||||
if len(self._schematic_classes) == 0:
|
||||
Console.error(f'No schematics found in template directory: .cpl')
|
||||
sys.exit()
|
||||
|
||||
known_schematics = []
|
||||
for schematic in GenerateSchematicABC.__subclasses__():
|
||||
if schematic.__name__ in known_schematics:
|
||||
Console.error(f'Duplicate of schematic {schematic.__name__} found!')
|
||||
sys.exit()
|
||||
|
||||
for schematic in self._schematic_classes:
|
||||
known_schematics.append(schematic.__name__)
|
||||
schematic.register()
|
||||
|
||||
@@ -83,6 +80,23 @@ class GenerateService(CommandABC):
|
||||
help_msg += f'\n {schematic}'
|
||||
return help_msg
|
||||
|
||||
def _read_custom_schematics_from_path(self, path: str):
|
||||
if not os.path.exists(os.path.join(path, '.cpl')):
|
||||
return
|
||||
|
||||
sys.path.insert(0, os.path.join(path, '.cpl'))
|
||||
for r, d, f in os.walk(os.path.join(path, '.cpl')):
|
||||
for file in f:
|
||||
if not file.startswith('schematic_') or not file.endswith('.py'):
|
||||
continue
|
||||
|
||||
try:
|
||||
exec(open(os.path.join(r, file), 'r').read())
|
||||
self._schematic_classes.update(GenerateSchematicABC.__subclasses__())
|
||||
except Exception as e:
|
||||
Console.error(str(e), traceback.format_exc())
|
||||
sys.exit(-1)
|
||||
|
||||
@staticmethod
|
||||
def _create_file(file_path: str, value: str):
|
||||
"""
|
||||
@@ -157,23 +171,6 @@ class GenerateService(CommandABC):
|
||||
spinner_foreground_color=ForegroundColorEnum.cyan
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _read_custom_schematics_from_path(path: str):
|
||||
if not os.path.exists(os.path.join(path, '.cpl')):
|
||||
return
|
||||
|
||||
sys.path.insert(0, os.path.join(path, '.cpl'))
|
||||
for r, d, f in os.walk(os.path.join(path, '.cpl')):
|
||||
for file in f:
|
||||
if not file.startswith('schematic_') or not file.endswith('.py'):
|
||||
continue
|
||||
|
||||
try:
|
||||
exec(open(os.path.join(r, file), 'r').read())
|
||||
except Exception as e:
|
||||
Console.error(str(e), traceback.format_exc())
|
||||
sys.exit(-1)
|
||||
|
||||
def _get_schematic_by_alias(self, schematic: str) -> str:
|
||||
for key in self._schematics:
|
||||
if schematic in self._schematics[key]['Aliases']:
|
||||
|
@@ -45,6 +45,8 @@ class NewService(CommandABC):
|
||||
self._build: BuildSettings = BuildSettings()
|
||||
self._build_dict = {}
|
||||
|
||||
self._project_type_classes = set()
|
||||
|
||||
self._name: str = ''
|
||||
self._rel_path: str = ''
|
||||
self._project_type: ProjectTypeEnum = ProjectTypeEnum.console
|
||||
@@ -98,7 +100,7 @@ class NewService(CommandABC):
|
||||
],
|
||||
ProjectSettingsNameEnum.python_version.value: f'>={sys.version.split(" ")[0]}',
|
||||
ProjectSettingsNameEnum.python_path.value: {
|
||||
sys.platform: '../../venv/bin/python' if self._use_venv else ''
|
||||
sys.platform: '../../venv/' if self._use_venv else ''
|
||||
},
|
||||
ProjectSettingsNameEnum.classifiers.value: []
|
||||
}
|
||||
@@ -152,17 +154,22 @@ class NewService(CommandABC):
|
||||
|
||||
return project_path
|
||||
|
||||
def _get_project_information(self, is_unittest=False):
|
||||
def _get_project_information(self, project_type: str):
|
||||
"""
|
||||
Gets project information's from user
|
||||
:return:
|
||||
"""
|
||||
is_unittest = project_type == 'unittest'
|
||||
is_library = project_type == 'library'
|
||||
if is_library:
|
||||
return
|
||||
|
||||
if self._use_application_api or self._use_startup or self._use_service_providing or self._use_async or self._use_nothing:
|
||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
||||
Console.write_line('Skipping question due to given flags')
|
||||
return
|
||||
|
||||
if not is_unittest:
|
||||
if not is_unittest and not is_library:
|
||||
self._use_application_api = Console.read('Do you want to use application base? (y/n) ').lower() == 'y'
|
||||
|
||||
if not is_unittest and self._use_application_api:
|
||||
@@ -194,8 +201,7 @@ class NewService(CommandABC):
|
||||
explicit_path=os.path.join(self._env.working_directory, project, self._project.python_executable.replace('../', ''))
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _read_custom_project_types_from_path(path: str):
|
||||
def _read_custom_project_types_from_path(self, path: str):
|
||||
if not os.path.exists(os.path.join(path, '.cpl')):
|
||||
return
|
||||
|
||||
@@ -207,28 +213,28 @@ class NewService(CommandABC):
|
||||
|
||||
try:
|
||||
exec(open(os.path.join(r, file), 'r').read())
|
||||
self._project_type_classes.update(ProjectTypeABC.__subclasses__())
|
||||
except Exception as e:
|
||||
Console.error(str(e), traceback.format_exc())
|
||||
sys.exit(-1)
|
||||
|
||||
def _create_project(self, project_type: str):
|
||||
for package_name in Dependencies.get_cpl_packages():
|
||||
if package_name == 'cpl-cli':
|
||||
continue
|
||||
package = importlib.import_module(String.convert_to_snake_case(package_name[0]))
|
||||
self._read_custom_project_types_from_path(os.path.dirname(package.__file__))
|
||||
|
||||
self._read_custom_project_types_from_path(self._env.working_directory)
|
||||
self._read_custom_project_types_from_path(self._env.runtime_directory)
|
||||
|
||||
if len(ProjectTypeABC.__subclasses__()) == 0:
|
||||
if len(self._project_type_classes) == 0:
|
||||
Console.error(f'No project types found in template directory: .cpl')
|
||||
sys.exit()
|
||||
|
||||
project_class = None
|
||||
known_project_types = []
|
||||
for p in ProjectTypeABC.__subclasses__():
|
||||
if p.__name__ in known_project_types:
|
||||
Console.error(f'Duplicate of project type {p.__name__} found!')
|
||||
sys.exit()
|
||||
|
||||
for p in self._project_type_classes:
|
||||
known_project_types.append(p.__name__)
|
||||
if p.__name__.lower() != project_type and p.__name__.lower()[0] != project_type[0]:
|
||||
continue
|
||||
@@ -247,7 +253,7 @@ class NewService(CommandABC):
|
||||
if path is None:
|
||||
return
|
||||
|
||||
self._get_project_information()
|
||||
self._get_project_information(project_type)
|
||||
project_name = self._project.name
|
||||
if self._rel_path != '':
|
||||
project_name = f'{self._rel_path}/{project_name}'
|
||||
|
@@ -123,10 +123,16 @@ class ProjectSettings(ConfigurationModelABC):
|
||||
self._python_path = settings[ProjectSettingsNameEnum.python_path.value]
|
||||
|
||||
if ProjectSettingsNameEnum.python_path.value in settings and sys.platform in settings[ProjectSettingsNameEnum.python_path.value]:
|
||||
path = f'{settings[ProjectSettingsNameEnum.python_path.value][sys.platform]}/bin/python'
|
||||
path = f'{settings[ProjectSettingsNameEnum.python_path.value][sys.platform]}'
|
||||
|
||||
if path == '' or path is None:
|
||||
Error.warn(f'{ProjectSettingsNameEnum.python_path.value} not set')
|
||||
path = sys.executable
|
||||
else:
|
||||
if not path.endswith('bin/python'):
|
||||
if not path.endswith('/') and len(path) > 0:
|
||||
path += '/'
|
||||
path += 'bin/python'
|
||||
else:
|
||||
path = sys.executable
|
||||
|
||||
|
@@ -17,7 +17,7 @@ class VenvHelper:
|
||||
if is_virtual:
|
||||
return
|
||||
|
||||
venv_path = os.path.abspath(os.path.join(env.working_directory, project_settings.python_executable))
|
||||
venv_path = os.path.abspath(os.path.join(env.working_directory, project_settings.python_executable, '../../'))
|
||||
|
||||
if explicit_path is not None:
|
||||
venv_path = os.path.abspath(explicit_path)
|
||||
@@ -31,7 +31,7 @@ class VenvHelper:
|
||||
spinner_foreground_color=ForegroundColorEnum.cyan
|
||||
)
|
||||
|
||||
Pip.set_executable(venv_path)
|
||||
Pip.set_executable(project_settings.python_executable)
|
||||
|
||||
@staticmethod
|
||||
def create_venv(path):
|
||||
|
@@ -1,18 +1,11 @@
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
from cpl_cli.command.custom_script_service import CustomScriptService
|
||||
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
|
||||
from cpl_cli.migrations.base.migration_abc import MigrationABC
|
||||
from cpl_cli.migrations.base.migration_service_abc import MigrationServiceABC
|
||||
from cpl_cli.migrations.migration_2022_10 import Migration202210
|
||||
from cpl_cli.migrations.service.migration_service import MigrationService
|
||||
from cpl_core.application.startup_extension_abc import StartupExtensionABC
|
||||
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
|
||||
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC
|
||||
from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC
|
||||
from cpl_core.utils.string import String
|
||||
|
||||
|
||||
class StartupMigrationExtension(StartupExtensionABC):
|
||||
|
@@ -1 +0,0 @@
|
||||
# imports
|
@@ -1 +0,0 @@
|
||||
# imports
|
Reference in New Issue
Block a user