parse & set now works
This commit is contained in:
parent
6a0ed7deef
commit
15bf7e5887
2 changed files with 114 additions and 29 deletions
|
@ -12,11 +12,13 @@ from .version import (
|
|||
Version,
|
||||
)
|
||||
|
||||
|
||||
class BuildFileType(Enum):
|
||||
JS = '.json'
|
||||
JAVA_GRADLE = '.gradle'
|
||||
JS = ".json"
|
||||
JAVA_GRADLE = ".gradle"
|
||||
JAVA_CLOJURE = ".clj"
|
||||
PYTHON = '.py'
|
||||
PYTHON = ".py"
|
||||
|
||||
|
||||
class BuildFile(Validateable):
|
||||
def __init__(self, file_path: Path, content: str):
|
||||
|
@ -36,13 +38,13 @@ class BuildFile(Validateable):
|
|||
return None
|
||||
config_file_type = self.file_path.suffix
|
||||
match config_file_type:
|
||||
case '.json':
|
||||
case ".json":
|
||||
result = BuildFileType.JS
|
||||
case '.gradle':
|
||||
case ".gradle":
|
||||
result = BuildFileType.JAVA_GRADLE
|
||||
case '.clj':
|
||||
case ".clj":
|
||||
result = BuildFileType.JAVA_CLOJURE
|
||||
case '.py':
|
||||
case ".py":
|
||||
result = BuildFileType.PYTHON
|
||||
case _:
|
||||
result = None
|
||||
|
@ -52,28 +54,74 @@ class BuildFile(Validateable):
|
|||
try:
|
||||
match self.build_file_type():
|
||||
case BuildFileType.JS:
|
||||
print(json.loads(self.content))
|
||||
version = json.loads(self.content)["version"]
|
||||
print(version)
|
||||
version_str = json.loads(self.content)["version"]
|
||||
case BuildFileType.JAVA_GRADLE:
|
||||
# TODO: '\nversion = ' will not parse all ?!
|
||||
version_line = re.search("\nversion = .*", self.content)
|
||||
version_line_group = version_line.group()
|
||||
version_string = re.search(
|
||||
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line_group)
|
||||
version = version_string.group()
|
||||
"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group
|
||||
)
|
||||
version_str = version_string.group()
|
||||
case BuildFileType.PYTHON:
|
||||
# TODO: '\nversion = ' will not parse all ?!
|
||||
version_line = re.search("\nversion = .*\n", self.content)
|
||||
version_line_group = version_line.group()
|
||||
version_string = re.search(
|
||||
'[0-9]*\\.[0-9]*\\.[0-9]*(-dev)?[0-9]*', version_line_group)
|
||||
version = version_string.group()
|
||||
"[0-9]*\\.[0-9]*\\.[0-9]*(-dev)?[0-9]*", version_line_group
|
||||
)
|
||||
version_str = version_string.group()
|
||||
case BuildFileType.JAVA_CLOJURE:
|
||||
# TODO: unsure about the trailing '\n' !
|
||||
version_line = re.search("\\(defproject .*\n", self.content)
|
||||
version_line_group = version_line.group()
|
||||
version_string = re.search(
|
||||
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line_group)
|
||||
version = version_string.group()
|
||||
"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group
|
||||
)
|
||||
version_str = version_string.group()
|
||||
except:
|
||||
raise Exception(f"Version not found in file {self.file_path}")
|
||||
|
||||
return Version.from_str(version)
|
||||
result = Version.from_str(version_str)
|
||||
result.throw_if_invalid()
|
||||
|
||||
return result
|
||||
|
||||
def set_version(self, new_version: Version):
|
||||
# TODO: How can we create regex-pattern constants to use them at both places?
|
||||
try:
|
||||
match self.build_file_type():
|
||||
case BuildFileType.JS:
|
||||
json_data = json.loads(self.content)
|
||||
json_data["version"] = new_version.to_string()
|
||||
self.content = json.dumps(json_data, indent=4)
|
||||
case BuildFileType.JAVA_GRADLE:
|
||||
substitute = re.sub(
|
||||
'\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"',
|
||||
f'\nversion = "{new_version.to_string()}"',
|
||||
self.content,
|
||||
)
|
||||
self.content = substitute
|
||||
case BuildFileType.PYTHON:
|
||||
substitute = re.sub(
|
||||
'\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-dev)?[0-9]*"',
|
||||
f'\nversion = "{new_version.to_string()}"',
|
||||
self.content,
|
||||
)
|
||||
self.content = substitute
|
||||
case BuildFileType.JAVA_CLOJURE:
|
||||
version_line = re.search("\\(defproject .*\n", self.content)
|
||||
version_line_group = version_line.group()
|
||||
# TODO: we should stick here on defproject instead of first line!
|
||||
version_string = re.search(
|
||||
"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group
|
||||
)
|
||||
version_str = version_string.group()
|
||||
substitute = re.sub(
|
||||
'"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"',
|
||||
f'"{new_version.to_string()}"',
|
||||
self.content,
|
||||
)
|
||||
self.content = substitute
|
||||
except:
|
||||
raise Exception(f"Version not found in file {self.file_path}")
|
||||
|
|
|
@ -29,7 +29,7 @@ def test_sould_calculate_build_type():
|
|||
assert sut.build_file_type() == BuildFileType.JS
|
||||
|
||||
|
||||
def test_sould_parse_js():
|
||||
def test_sould_parse_and_set_js():
|
||||
sut = BuildFile(
|
||||
Path("./package.json"),
|
||||
"""
|
||||
|
@ -52,20 +52,29 @@ def test_sould_parse_js():
|
|||
"""
|
||||
{
|
||||
"name":"c4k-jira",
|
||||
"description": "Generate c4k yaml for a jira deployment.",
|
||||
"author": "meissa GmbH",
|
||||
"homepage": "https://gitlab.com/domaindrivenarchitecture/c4k-jira#readme",
|
||||
"bin":{
|
||||
"c4k-jira": "./c4k-jira.js"
|
||||
}
|
||||
}
|
||||
""",
|
||||
)
|
||||
with pytest.raises(Exception):
|
||||
sut.get_version()
|
||||
|
||||
sut = BuildFile(
|
||||
Path("./package.json"),
|
||||
"""
|
||||
{
|
||||
"name":"c4k-jira",
|
||||
"version": "1.1.5-SNAPSHOT"
|
||||
}
|
||||
""",
|
||||
)
|
||||
sut.set_version(Version.from_str("1.1.5-SNAPSHOT").create_major())
|
||||
assert """{
|
||||
"name": "c4k-jira",
|
||||
"version": "2.0.0"
|
||||
}""" == sut.content
|
||||
|
||||
def test_sould_parse_gradle():
|
||||
|
||||
def test_sould_parse_and_set_version_for_gradle():
|
||||
sut = BuildFile(
|
||||
Path("./build.gradle"),
|
||||
"""
|
||||
|
@ -75,8 +84,16 @@ version = "1.1.5-SNAPSHOT"
|
|||
)
|
||||
assert sut.get_version() == Version.from_str("1.1.5-SNAPSHOT")
|
||||
|
||||
sut = BuildFile(
|
||||
Path("./build.gradle"),
|
||||
"""
|
||||
version = "1.1.5-SNAPSHOT"
|
||||
""",
|
||||
)
|
||||
sut.set_version(Version.from_str("1.1.5-SNAPSHOT").create_major())
|
||||
assert '\nversion = "2.0.0"\n' == sut.content
|
||||
|
||||
def test_sould_parse_py():
|
||||
def test_sould_parse_and_set_version_for_py():
|
||||
sut = BuildFile(
|
||||
Path("./build.py"),
|
||||
"""
|
||||
|
@ -89,8 +106,17 @@ version = "1.1.5-dev"
|
|||
)
|
||||
assert sut.get_version() == Version.from_str("1.1.5-dev")
|
||||
|
||||
sut = BuildFile(
|
||||
Path("./build.py"),
|
||||
"""
|
||||
version = "1.1.5-dev1"
|
||||
""",
|
||||
)
|
||||
sut.set_version(Version.from_str("1.1.5-dev1").create_major())
|
||||
assert '\nversion = "2.0.0"\n' == sut.content
|
||||
|
||||
def test_sould_parse_clj():
|
||||
|
||||
def test_sould_parse_and_set_version_for_clj():
|
||||
sut = BuildFile(
|
||||
Path("./project.clj"),
|
||||
"""
|
||||
|
@ -101,3 +127,14 @@ def test_sould_parse_clj():
|
|||
""",
|
||||
)
|
||||
assert sut.get_version() == Version.from_str("1.1.5-SNAPSHOT")
|
||||
|
||||
sut = BuildFile(
|
||||
Path("./project.clj"),
|
||||
"""
|
||||
(defproject org.domaindrivenarchitecture/c4k-jira "1.1.5-SNAPSHOT"
|
||||
:description "jira c4k-installation package"
|
||||
)
|
||||
""",
|
||||
)
|
||||
sut.set_version(Version.from_str("1.1.5-SNAPSHOT").create_major())
|
||||
assert '\n(defproject org.domaindrivenarchitecture/c4k-jira "2.0.0"\n :description "jira c4k-installation package"\n)\n' == sut.content
|
||||
|
|
Loading…
Reference in a new issue