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,
|
Version,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class BuildFileType(Enum):
|
class BuildFileType(Enum):
|
||||||
JS = '.json'
|
JS = ".json"
|
||||||
JAVA_GRADLE = '.gradle'
|
JAVA_GRADLE = ".gradle"
|
||||||
JAVA_CLOJURE = ".clj"
|
JAVA_CLOJURE = ".clj"
|
||||||
PYTHON = '.py'
|
PYTHON = ".py"
|
||||||
|
|
||||||
|
|
||||||
class BuildFile(Validateable):
|
class BuildFile(Validateable):
|
||||||
def __init__(self, file_path: Path, content: str):
|
def __init__(self, file_path: Path, content: str):
|
||||||
|
@ -36,13 +38,13 @@ class BuildFile(Validateable):
|
||||||
return None
|
return None
|
||||||
config_file_type = self.file_path.suffix
|
config_file_type = self.file_path.suffix
|
||||||
match config_file_type:
|
match config_file_type:
|
||||||
case '.json':
|
case ".json":
|
||||||
result = BuildFileType.JS
|
result = BuildFileType.JS
|
||||||
case '.gradle':
|
case ".gradle":
|
||||||
result = BuildFileType.JAVA_GRADLE
|
result = BuildFileType.JAVA_GRADLE
|
||||||
case '.clj':
|
case ".clj":
|
||||||
result = BuildFileType.JAVA_CLOJURE
|
result = BuildFileType.JAVA_CLOJURE
|
||||||
case '.py':
|
case ".py":
|
||||||
result = BuildFileType.PYTHON
|
result = BuildFileType.PYTHON
|
||||||
case _:
|
case _:
|
||||||
result = None
|
result = None
|
||||||
|
@ -52,28 +54,74 @@ class BuildFile(Validateable):
|
||||||
try:
|
try:
|
||||||
match self.build_file_type():
|
match self.build_file_type():
|
||||||
case BuildFileType.JS:
|
case BuildFileType.JS:
|
||||||
print(json.loads(self.content))
|
version_str = json.loads(self.content)["version"]
|
||||||
version = json.loads(self.content)["version"]
|
|
||||||
print(version)
|
|
||||||
case BuildFileType.JAVA_GRADLE:
|
case BuildFileType.JAVA_GRADLE:
|
||||||
|
# TODO: '\nversion = ' will not parse all ?!
|
||||||
version_line = re.search("\nversion = .*", self.content)
|
version_line = re.search("\nversion = .*", self.content)
|
||||||
version_line_group = version_line.group()
|
version_line_group = version_line.group()
|
||||||
version_string = re.search(
|
version_string = re.search(
|
||||||
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line_group)
|
"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group
|
||||||
version = version_string.group()
|
)
|
||||||
|
version_str = version_string.group()
|
||||||
case BuildFileType.PYTHON:
|
case BuildFileType.PYTHON:
|
||||||
|
# TODO: '\nversion = ' will not parse all ?!
|
||||||
version_line = re.search("\nversion = .*\n", self.content)
|
version_line = re.search("\nversion = .*\n", self.content)
|
||||||
version_line_group = version_line.group()
|
version_line_group = version_line.group()
|
||||||
version_string = re.search(
|
version_string = re.search(
|
||||||
'[0-9]*\\.[0-9]*\\.[0-9]*(-dev)?[0-9]*', version_line_group)
|
"[0-9]*\\.[0-9]*\\.[0-9]*(-dev)?[0-9]*", version_line_group
|
||||||
version = version_string.group()
|
)
|
||||||
|
version_str = version_string.group()
|
||||||
case BuildFileType.JAVA_CLOJURE:
|
case BuildFileType.JAVA_CLOJURE:
|
||||||
|
# TODO: unsure about the trailing '\n' !
|
||||||
version_line = re.search("\\(defproject .*\n", self.content)
|
version_line = re.search("\\(defproject .*\n", self.content)
|
||||||
version_line_group = version_line.group()
|
version_line_group = version_line.group()
|
||||||
version_string = re.search(
|
version_string = re.search(
|
||||||
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line_group)
|
"[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?", version_line_group
|
||||||
version = version_string.group()
|
)
|
||||||
|
version_str = version_string.group()
|
||||||
except:
|
except:
|
||||||
raise Exception(f"Version not found in file {self.file_path}")
|
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
|
assert sut.build_file_type() == BuildFileType.JS
|
||||||
|
|
||||||
|
|
||||||
def test_sould_parse_js():
|
def test_sould_parse_and_set_js():
|
||||||
sut = BuildFile(
|
sut = BuildFile(
|
||||||
Path("./package.json"),
|
Path("./package.json"),
|
||||||
"""
|
"""
|
||||||
|
@ -52,20 +52,29 @@ def test_sould_parse_js():
|
||||||
"""
|
"""
|
||||||
{
|
{
|
||||||
"name":"c4k-jira",
|
"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):
|
with pytest.raises(Exception):
|
||||||
sut.get_version()
|
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(
|
sut = BuildFile(
|
||||||
Path("./build.gradle"),
|
Path("./build.gradle"),
|
||||||
"""
|
"""
|
||||||
|
@ -75,8 +84,16 @@ version = "1.1.5-SNAPSHOT"
|
||||||
)
|
)
|
||||||
assert sut.get_version() == Version.from_str("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(
|
sut = BuildFile(
|
||||||
Path("./build.py"),
|
Path("./build.py"),
|
||||||
"""
|
"""
|
||||||
|
@ -89,15 +106,35 @@ version = "1.1.5-dev"
|
||||||
)
|
)
|
||||||
assert sut.get_version() == Version.from_str("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(
|
sut = BuildFile(
|
||||||
Path("./project.clj"),
|
Path("./project.clj"),
|
||||||
"""
|
"""
|
||||||
(defproject org.domaindrivenarchitecture/c4k-jira "1.1.5-SNAPSHOT"
|
(defproject org.domaindrivenarchitecture/c4k-jira "1.1.5-SNAPSHOT"
|
||||||
:description "jira c4k-installation package"
|
:description "jira c4k-installation package"
|
||||||
:url "https://domaindrivenarchitecture.org"
|
:url "https://domaindrivenarchitecture.org"
|
||||||
)
|
)
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
assert sut.get_version() == Version.from_str("1.1.5-SNAPSHOT")
|
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