Loading src/Generators/ComponentGenerator.php +23 −11 Original line number Diff line number Diff line Loading @@ -21,6 +21,10 @@ final class ComponentGenerator extends DrupalGenerator { * {@inheritdoc} */ protected ?int $extensionType = self::EXTENSION_TYPE_THEME; /** * {@inheritdoc} */ protected bool $isNewExtension = FALSE; /** * {@inheritdoc} */ Loading Loading @@ -82,7 +86,7 @@ final class ComponentGenerator extends DrupalGenerator { $this->machineNameQuestion = 'Machine name of the ' . $extension_type; $this->collectDefault($vars); $vars['directory'] = $this->ask('Components directory', 'templates/components', '::validateRequired'); $vars['component_machine_name'] = $this->ask('Component machine name', 'my-component', '::validateRequiredKebabMachineName'); $vars['component_machine_name'] = $this->ask('Component machine name', 'my-component', '::validateRequiredMachineName'); $default = \ucwords(\trim(\str_replace([ '_', '-', Loading @@ -104,7 +108,7 @@ final class ComponentGenerator extends DrupalGenerator { $vars['component_variants'] = []; do { // @todo Add validation to the `ask` method. $variant = $this->ask('Component variants (optional). [Example: light]', NULL, '::validateKebabMachineName'); $variant = $this->ask('Component variants (optional). [Example: light]', NULL, '::validateMachineName'); $vars['component_variants'][] = $variant; } while (!empty($variant)); $vars['component_variants'] = \array_filter($vars['component_variants']); Loading Loading @@ -164,7 +168,7 @@ final class ComponentGenerator extends DrupalGenerator { ), $vars['component_variants'] ); $this->addFile($component_path_token . 'metadata.json', 'metadata-json--template.twig'); $this->addFile($component_path_token . '{component_machine_name}.component.yml', 'component-yml--template.twig'); $this->addFile($component_path_token . 'README.md', 'readme-md--template.twig'); $contents = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'thumbnail-placeholder.png'); $thumbnail = new File($component_path_token . 'thumbnail.png'); Loading @@ -188,7 +192,15 @@ final class ComponentGenerator extends DrupalGenerator { ...\array_keys($this->drupalContext->getModules()), ...\array_keys($this->drupalContext->getThemes()), ]; $library_ids = array_reduce($extensions, fn(array $libs, string $extension) => \array_merge($libs, \array_map(fn (string $l) => sprintf('%s/%s', $extension, $l), \array_keys($this->libraryDiscovery->getLibrariesByExtension($extension)))), []); $library_ids = array_reduce( $extensions, fn(array $libs, string $extension) => \array_merge( $libs, \array_map(static fn (string $l) => sprintf('%s/%s', $extension, $l), \array_keys($this->libraryDiscovery->getLibrariesByExtension($extension))) ), [] ); $library_question->setValidator(static::libraryValidator($library_ids)); $library_question->setAutocompleterValues($library_ids); return $this->io->askQuestion($library_question); Loading Loading @@ -237,11 +249,11 @@ final class ComponentGenerator extends DrupalGenerator { * @param array $library_ids * Allowed libraries. * * @return \Closure * @return callable * The validator function. */ public static function libraryValidator(array $library_ids) { return fn($value) => is_null($value) || \in_array($value, $library_ids) ? $value : throw new \UnexpectedValueException('Invalid library ID'); public static function libraryValidator(array $library_ids): callable { return static fn($value) => is_null($value) || \in_array($value, $library_ids, TRUE) ? $value : throw new \UnexpectedValueException('Invalid library ID'); } /** Loading @@ -250,12 +262,12 @@ final class ComponentGenerator extends DrupalGenerator { * @param string|null $value * The input value to validate. */ public static function validateKebabMachineName($value): ?string { public static function validateMachineName(?string $value): string { return static::validate( $value, '^[a-z][a-z0-9-]*[a-z0-9]$', 'The value is not in the correct format. Ex: a-kebab-case-str1ng' ); ) ?? ''; } /** Loading @@ -264,11 +276,11 @@ final class ComponentGenerator extends DrupalGenerator { * @param string|null $value * The input value to validate. */ public static function validateRequiredKebabMachineName($value) { public static function validateRequiredMachineName(?string $value): string { $value = static::validateRequired($value); return static::validate( $value, '^[a-z][a-z0-9-]*[a-z0-9]$', '^[a-z][a-z0-9-_]*[a-z0-9]$', 'The value is not in the correct format. Ex: a-kebab-case-str1ng' ); } Loading src/Generators/component-yml--template.twig 0 → 100644 +40 −0 Original line number Diff line number Diff line '$schema': 'https://git.drupalcode.org/project/cl_components/-/raw/1.x/src/metadata.schema.json' machineName: "{{ component_machine_name }}" name: "{{ component_name }}" componentType: "{{ component_type }}" status: "{{ component_status }}" {% if component_description is not empty %} description: "{{ component_description }}" {% endif %} {% if component_variants is not empty %} variants: {% for variant in component_variants %} - "{{ variant }}" {% endfor %} {% endif %} {% if component_libraries is not empty %} libraryDependencies: {% for library in component_libraries %} - "{{ library }}" {% endfor %} {% endif %} {% if component_props is not empty %} schemas: props: type: object properties: {% for prop in component_props %} "{{ prop.name }}": type: "{{ prop.type }}" title: "{{ prop.title }}" {% if prop.description %} description: "{{ prop.description }}" {% endif %} {% if prop.examples is not empty %} examples: {% for example in prop.examples %} - "{{ example }}" {% endfor %} {% endif %} {% endfor %} {% endif %} src/Generators/metadata-json--template.twigdeleted 100644 → 0 +0 −53 Original line number Diff line number Diff line { "$schema": "https://git.drupalcode.org/project/cl_components/-/raw/1.x/src/metadata.schema.json", "machineName": "{{ component_machine_name }}", "name": "{{ component_name }}", "componentType": "{{ component_type }}", {% if component_description is not empty %} "description": "{{ component_description }}", {% endif %} {% if component_variants is not empty %} "variants": [ {% for variant in component_variants %} "{{ variant }}"{% if not loop.last %},{% endif %} {% endfor %} ], {% endif %} {% if component_libraries is not empty %} "libraryDependencies": [ {% for library in component_libraries %} "{{ library }}"{% if not loop.last %},{% endif %} {% endfor %} ], {% endif %} {% if component_props is not empty %} "schemas": { "props": { "type": "object", "properties": { {% for prop in component_props %} "{{ prop.name }}": { "title": "{{ prop.title }}", {% if prop.description %} "description": "{{ prop.description }}", {% endif %} {% if prop.examples is not empty %} "examples": [ {% for example in prop.examples %} "{{ example }}"{% if not loop.last %},{% endif %} {% endfor %} ], {% endif %} "type": "{{ prop.type }}" }{% if not loop.last %},{% endif %} {% endfor %} } } }, {% endif %} "status": "{{ component_status }}" } src/Generators/thumbnail-placeholder.png +75.4 KiB (76.6 KiB) Loading image diff... Loading
src/Generators/ComponentGenerator.php +23 −11 Original line number Diff line number Diff line Loading @@ -21,6 +21,10 @@ final class ComponentGenerator extends DrupalGenerator { * {@inheritdoc} */ protected ?int $extensionType = self::EXTENSION_TYPE_THEME; /** * {@inheritdoc} */ protected bool $isNewExtension = FALSE; /** * {@inheritdoc} */ Loading Loading @@ -82,7 +86,7 @@ final class ComponentGenerator extends DrupalGenerator { $this->machineNameQuestion = 'Machine name of the ' . $extension_type; $this->collectDefault($vars); $vars['directory'] = $this->ask('Components directory', 'templates/components', '::validateRequired'); $vars['component_machine_name'] = $this->ask('Component machine name', 'my-component', '::validateRequiredKebabMachineName'); $vars['component_machine_name'] = $this->ask('Component machine name', 'my-component', '::validateRequiredMachineName'); $default = \ucwords(\trim(\str_replace([ '_', '-', Loading @@ -104,7 +108,7 @@ final class ComponentGenerator extends DrupalGenerator { $vars['component_variants'] = []; do { // @todo Add validation to the `ask` method. $variant = $this->ask('Component variants (optional). [Example: light]', NULL, '::validateKebabMachineName'); $variant = $this->ask('Component variants (optional). [Example: light]', NULL, '::validateMachineName'); $vars['component_variants'][] = $variant; } while (!empty($variant)); $vars['component_variants'] = \array_filter($vars['component_variants']); Loading Loading @@ -164,7 +168,7 @@ final class ComponentGenerator extends DrupalGenerator { ), $vars['component_variants'] ); $this->addFile($component_path_token . 'metadata.json', 'metadata-json--template.twig'); $this->addFile($component_path_token . '{component_machine_name}.component.yml', 'component-yml--template.twig'); $this->addFile($component_path_token . 'README.md', 'readme-md--template.twig'); $contents = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'thumbnail-placeholder.png'); $thumbnail = new File($component_path_token . 'thumbnail.png'); Loading @@ -188,7 +192,15 @@ final class ComponentGenerator extends DrupalGenerator { ...\array_keys($this->drupalContext->getModules()), ...\array_keys($this->drupalContext->getThemes()), ]; $library_ids = array_reduce($extensions, fn(array $libs, string $extension) => \array_merge($libs, \array_map(fn (string $l) => sprintf('%s/%s', $extension, $l), \array_keys($this->libraryDiscovery->getLibrariesByExtension($extension)))), []); $library_ids = array_reduce( $extensions, fn(array $libs, string $extension) => \array_merge( $libs, \array_map(static fn (string $l) => sprintf('%s/%s', $extension, $l), \array_keys($this->libraryDiscovery->getLibrariesByExtension($extension))) ), [] ); $library_question->setValidator(static::libraryValidator($library_ids)); $library_question->setAutocompleterValues($library_ids); return $this->io->askQuestion($library_question); Loading Loading @@ -237,11 +249,11 @@ final class ComponentGenerator extends DrupalGenerator { * @param array $library_ids * Allowed libraries. * * @return \Closure * @return callable * The validator function. */ public static function libraryValidator(array $library_ids) { return fn($value) => is_null($value) || \in_array($value, $library_ids) ? $value : throw new \UnexpectedValueException('Invalid library ID'); public static function libraryValidator(array $library_ids): callable { return static fn($value) => is_null($value) || \in_array($value, $library_ids, TRUE) ? $value : throw new \UnexpectedValueException('Invalid library ID'); } /** Loading @@ -250,12 +262,12 @@ final class ComponentGenerator extends DrupalGenerator { * @param string|null $value * The input value to validate. */ public static function validateKebabMachineName($value): ?string { public static function validateMachineName(?string $value): string { return static::validate( $value, '^[a-z][a-z0-9-]*[a-z0-9]$', 'The value is not in the correct format. Ex: a-kebab-case-str1ng' ); ) ?? ''; } /** Loading @@ -264,11 +276,11 @@ final class ComponentGenerator extends DrupalGenerator { * @param string|null $value * The input value to validate. */ public static function validateRequiredKebabMachineName($value) { public static function validateRequiredMachineName(?string $value): string { $value = static::validateRequired($value); return static::validate( $value, '^[a-z][a-z0-9-]*[a-z0-9]$', '^[a-z][a-z0-9-_]*[a-z0-9]$', 'The value is not in the correct format. Ex: a-kebab-case-str1ng' ); } Loading
src/Generators/component-yml--template.twig 0 → 100644 +40 −0 Original line number Diff line number Diff line '$schema': 'https://git.drupalcode.org/project/cl_components/-/raw/1.x/src/metadata.schema.json' machineName: "{{ component_machine_name }}" name: "{{ component_name }}" componentType: "{{ component_type }}" status: "{{ component_status }}" {% if component_description is not empty %} description: "{{ component_description }}" {% endif %} {% if component_variants is not empty %} variants: {% for variant in component_variants %} - "{{ variant }}" {% endfor %} {% endif %} {% if component_libraries is not empty %} libraryDependencies: {% for library in component_libraries %} - "{{ library }}" {% endfor %} {% endif %} {% if component_props is not empty %} schemas: props: type: object properties: {% for prop in component_props %} "{{ prop.name }}": type: "{{ prop.type }}" title: "{{ prop.title }}" {% if prop.description %} description: "{{ prop.description }}" {% endif %} {% if prop.examples is not empty %} examples: {% for example in prop.examples %} - "{{ example }}" {% endfor %} {% endif %} {% endfor %} {% endif %}
src/Generators/metadata-json--template.twigdeleted 100644 → 0 +0 −53 Original line number Diff line number Diff line { "$schema": "https://git.drupalcode.org/project/cl_components/-/raw/1.x/src/metadata.schema.json", "machineName": "{{ component_machine_name }}", "name": "{{ component_name }}", "componentType": "{{ component_type }}", {% if component_description is not empty %} "description": "{{ component_description }}", {% endif %} {% if component_variants is not empty %} "variants": [ {% for variant in component_variants %} "{{ variant }}"{% if not loop.last %},{% endif %} {% endfor %} ], {% endif %} {% if component_libraries is not empty %} "libraryDependencies": [ {% for library in component_libraries %} "{{ library }}"{% if not loop.last %},{% endif %} {% endfor %} ], {% endif %} {% if component_props is not empty %} "schemas": { "props": { "type": "object", "properties": { {% for prop in component_props %} "{{ prop.name }}": { "title": "{{ prop.title }}", {% if prop.description %} "description": "{{ prop.description }}", {% endif %} {% if prop.examples is not empty %} "examples": [ {% for example in prop.examples %} "{{ example }}"{% if not loop.last %},{% endif %} {% endfor %} ], {% endif %} "type": "{{ prop.type }}" }{% if not loop.last %},{% endif %} {% endfor %} } } }, {% endif %} "status": "{{ component_status }}" }