RULEAPI-744 automatically fill the template source tags with the current language
This commit is contained in:
parent
9633e90ee4
commit
9ca204f1c9
@ -5,7 +5,6 @@ FIXME: add a description
|
||||
|
||||
== Noncompliant Code Example
|
||||
|
||||
// Replace text with the language you are using if relevant
|
||||
[source,text]
|
||||
----
|
||||
FIXME
|
||||
@ -13,7 +12,6 @@ FIXME
|
||||
|
||||
== Compliant Solution
|
||||
|
||||
// Replace text with the language you are using if relevant
|
||||
[source,text]
|
||||
----
|
||||
FIXME
|
||||
|
@ -5,7 +5,6 @@ FIXME: add a description
|
||||
|
||||
== Noncompliant Code Example
|
||||
|
||||
// Replace text with the language you are using if relevant
|
||||
[source,text]
|
||||
----
|
||||
FIXME
|
||||
@ -13,7 +12,6 @@ FIXME
|
||||
|
||||
== Compliant Solution
|
||||
|
||||
// Replace text with the language you are using if relevant
|
||||
[source,text]
|
||||
----
|
||||
FIXME
|
||||
|
@ -11,7 +11,7 @@ from typing import Final, Iterable, Optional, Callable
|
||||
from contextlib import contextmanager
|
||||
from rspec_tools.utils import parse_and_validate_language_list, get_labels_for_languages, validate_language, get_label_for_language, resolve_rule, swap_metadata_files, is_empty_metadata
|
||||
|
||||
from rspec_tools.utils import copy_directory_content
|
||||
from rspec_tools.utils import copy_directory_content, LANG_TO_SOURCE
|
||||
|
||||
def build_github_repository_url(token: str, user: Optional[str]):
|
||||
'Builds the rspec repository url'
|
||||
@ -120,6 +120,7 @@ class RuleCreator:
|
||||
lang_specific_template = self.TEMPLATE_PATH.joinpath('multi_language', 'language_specific')
|
||||
copy_directory_content(lang_specific_template, lang_dir)
|
||||
self._fill_in_the_blanks_in_the_template(lang_dir, rule_number)
|
||||
self._fill_language_name_in_the_template(lang_dir, language)
|
||||
self.repository.git.add('--all')
|
||||
self.repository.index.commit(f'Add {language} to rule S{rule_number}')
|
||||
self.repository.git.push('origin', branch_name)
|
||||
@ -150,6 +151,14 @@ class RuleCreator:
|
||||
final_content = template_content.replace('${RSPEC_ID}', str(rule_number))
|
||||
rule_item.write_text(final_content)
|
||||
|
||||
def _fill_language_name_in_the_template(self, lang_dir: Path, language: str):
|
||||
for rule_item in lang_dir.glob('*.adoc'):
|
||||
if rule_item.is_file():
|
||||
template_content = rule_item.read_text()
|
||||
lang = LANG_TO_SOURCE[language]
|
||||
final_content = template_content.replace('[source,text]', f'[source,{lang}]')
|
||||
rule_item.write_text(final_content)
|
||||
|
||||
def _fill_multi_lang_template_files(self, rule_dir: Path, rule_number: int, languages: Iterable[str]):
|
||||
common_template = self.TEMPLATE_PATH.joinpath('multi_language', 'common')
|
||||
lang_specific_template = self.TEMPLATE_PATH.joinpath('multi_language', 'language_specific')
|
||||
@ -159,6 +168,7 @@ class RuleCreator:
|
||||
lang_dir = rule_dir.joinpath(lang)
|
||||
lang_dir.mkdir()
|
||||
copy_directory_content(lang_specific_template, lang_dir)
|
||||
self._fill_language_name_in_the_template(lang_dir, lang)
|
||||
|
||||
self._fill_in_the_blanks_in_the_template(rule_dir, rule_number)
|
||||
|
||||
@ -172,6 +182,7 @@ class RuleCreator:
|
||||
copy_directory_content(lang_specific_template, lang_dir)
|
||||
|
||||
self._fill_in_the_blanks_in_the_template(rule_dir, rule_number)
|
||||
self._fill_language_name_in_the_template(lang_dir, language)
|
||||
|
||||
def _create_pull_request(self, github_api: Callable[[Optional[str]], Github], branch_name: str, title: str, body: str, labels: Iterable[str], user: Optional[str]):
|
||||
repository_url = extract_repository_name(self.origin_url)
|
||||
|
@ -40,6 +40,42 @@ LANG_TO_LABEL = {'abap': 'abap',
|
||||
'xml': 'xml',
|
||||
}
|
||||
|
||||
LANG_TO_SOURCE = {
|
||||
# languages with syntax coloring in highlight.js
|
||||
'abap': 'abap',
|
||||
'cfamily': 'cpp',
|
||||
'csharp': 'csharp',
|
||||
'css': 'css',
|
||||
'go': 'go',
|
||||
'html': 'html',
|
||||
'java': 'java',
|
||||
'javascript': 'javascript',
|
||||
'kotlin': 'kotlin',
|
||||
'php': 'php',
|
||||
'plsql': 'sql',
|
||||
'python': 'python',
|
||||
'ruby': 'ruby',
|
||||
'rust': 'rust',
|
||||
'scala': 'scala',
|
||||
'swift': 'swift',
|
||||
'terraform': 'terraform',
|
||||
'tsql': 'sql',
|
||||
'vbnet': 'vbnet',
|
||||
'xml': 'xml',
|
||||
'c': 'c',
|
||||
'objectivec': 'objectivec',
|
||||
'vb': 'vb',
|
||||
# these languages are not supported by highlight.js as the moment:
|
||||
'apex': 'apex',
|
||||
'cloudformation': 'cloudformation',
|
||||
'cobol': 'cobol',
|
||||
'flex': 'flex',
|
||||
'pli': 'pli',
|
||||
'rpg': 'rpg',
|
||||
'text': 'text',
|
||||
'vb6': 'vb6'
|
||||
}
|
||||
|
||||
METADATA_FILE = 'metadata.json'
|
||||
|
||||
def copy_directory_content(src:Path, dest:Path):
|
||||
|
@ -4,6 +4,7 @@ from typing import Final
|
||||
|
||||
from rspec_tools.errors import RuleValidationError
|
||||
from rspec_tools.rules import LanguageSpecificRule
|
||||
from rspec_tools.utils import LANG_TO_SOURCE
|
||||
|
||||
# The list of all the sections currently accepted by the script.
|
||||
# The list includes multiple variants for each title because they all occur
|
||||
@ -47,50 +48,13 @@ def validate_parameters(rule_language: LanguageSpecificRule):
|
||||
continue
|
||||
validate_one_parameter(child, rule_language.id)
|
||||
|
||||
|
||||
HIGHLIGHTED_LANGUAGES = {
|
||||
# languages with syntax coloring in highlight.js
|
||||
'abap': 'abap',
|
||||
'cfamily': 'cpp',
|
||||
'csharp': 'csharp',
|
||||
'css': 'css',
|
||||
'go': 'go',
|
||||
'html': 'html',
|
||||
'java': 'java',
|
||||
'javascript': 'javascript',
|
||||
'kotlin': 'kotlin',
|
||||
'php': 'php',
|
||||
'plsql': 'sql',
|
||||
'python': 'python',
|
||||
'ruby': 'ruby',
|
||||
'rust': 'rust',
|
||||
'scala': 'scala',
|
||||
'swift': 'swift',
|
||||
'terraform': 'terraform',
|
||||
'tsql': 'sql',
|
||||
'vbnet': 'vbnet',
|
||||
'xml': 'xml',
|
||||
'c': 'c',
|
||||
'objectivec': 'objectivec',
|
||||
'vb': 'vb',
|
||||
# these languages are not supported by highlight.js as the moment:
|
||||
'apex': 'apex',
|
||||
'cloudformation': 'cloudformation',
|
||||
'cobol': 'cobol',
|
||||
'flex': 'flex',
|
||||
'pli': 'pli',
|
||||
'rpg': 'rpg',
|
||||
'text': 'text',
|
||||
'vb6': 'vb6'
|
||||
}
|
||||
|
||||
def highlight_name(rule_language: LanguageSpecificRule):
|
||||
if (rule_language.language in HIGHLIGHTED_LANGUAGES):
|
||||
return HIGHLIGHTED_LANGUAGES[rule_language.language]
|
||||
if (rule_language.language in LANG_TO_SOURCE):
|
||||
return LANG_TO_SOURCE[rule_language.language]
|
||||
return rule_language.language
|
||||
|
||||
def known_highlight(language):
|
||||
return language in HIGHLIGHTED_LANGUAGES.values()
|
||||
return language in LANG_TO_SOURCE.values()
|
||||
|
||||
def validate_source_language(rule_language: LanguageSpecificRule):
|
||||
descr = rule_language.description
|
||||
|
@ -5,9 +5,10 @@ from typing import Optional
|
||||
from unittest.mock import Mock, patch
|
||||
import pytest
|
||||
import shutil
|
||||
import os
|
||||
|
||||
from rspec_tools.create_rule import RuleCreator, create_new_rule, add_language_to_rule
|
||||
from rspec_tools.utils import is_empty_metadata
|
||||
from rspec_tools.utils import is_empty_metadata, LANG_TO_SOURCE
|
||||
|
||||
@pytest.fixture
|
||||
def git_config():
|
||||
@ -105,6 +106,7 @@ def test_create_new_multi_lang_rule_branch(rule_creator: RuleCreator, mock_rspec
|
||||
for lang_item in lang_root.glob('**/*'):
|
||||
if lang_item.is_file():
|
||||
expected_content = lang_item.read_text().replace('${RSPEC_ID}', str(rule_number))
|
||||
expected_content = expected_content.replace('[source,text]', f'[source,{LANG_TO_SOURCE[os.path.basename(lang)]}]')
|
||||
relative_path = lang_item.relative_to(lang_root)
|
||||
actual_content = rule_dir.joinpath(lang, relative_path).read_text()
|
||||
assert actual_content == expected_content
|
||||
@ -134,6 +136,8 @@ def test_create_new_single_lang_rule_branch(rule_creator: RuleCreator, mock_rspe
|
||||
for lang_item in lang_root.glob('**/*'):
|
||||
if lang_item.is_file():
|
||||
expected_content = lang_item.read_text().replace('${RSPEC_ID}', str(rule_number))
|
||||
dir_name = os.path.basename(lang)
|
||||
expected_content = expected_content.replace('[source,text]', f'[source,{LANG_TO_SOURCE[dir_name]}]')
|
||||
relative_path = lang_item.relative_to(lang_root)
|
||||
actual_content = rule_dir.joinpath(lang, relative_path).read_text()
|
||||
assert actual_content == expected_content
|
||||
@ -204,6 +208,7 @@ def test_add_lang_singlelang_nonconventional_rule_create_branch(rule_creator: Ru
|
||||
for lang_item in lang_root.glob('**/*'):
|
||||
if lang_item.is_file():
|
||||
expected_content = lang_item.read_text().replace('${RSPEC_ID}', str(rule_number))
|
||||
expected_content = expected_content.replace('[source,text]', f'[source,{LANG_TO_SOURCE[language]}]')
|
||||
relative_path = lang_item.relative_to(lang_root)
|
||||
actual_content = rule_dir.joinpath(language, relative_path).read_text()
|
||||
assert actual_content == expected_content
|
||||
@ -249,6 +254,7 @@ def test_add_lang_multilang_rule_create_branch(rule_creator: RuleCreator, mock_r
|
||||
for lang_item in lang_root.glob('**/*'):
|
||||
if lang_item.is_file():
|
||||
expected_content = lang_item.read_text().replace('${RSPEC_ID}', str(rule_number))
|
||||
expected_content = expected_content.replace('[source,text]', f'[source,{LANG_TO_SOURCE[language]}]')
|
||||
relative_path = lang_item.relative_to(lang_root)
|
||||
actual_content = rule_dir.joinpath(language, relative_path).read_text()
|
||||
assert actual_content == expected_content
|
||||
|
Loading…
x
Reference in New Issue
Block a user