From a4c8f8934a53bb38687c311c0fc323a4a632c398 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 9 Feb 2023 13:56:45 +0100 Subject: [PATCH] Implement clj file handler --- file_handlers.py | 42 ++++++++++++++++++++++++++++++++-- project.clj | 46 ++++++++++++++++++++++++++++++++++++++ test/config.clj | 5 +++++ test/test_version_class.py | 19 +++++++++++++++- 4 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 project.clj create mode 100644 test/config.clj diff --git a/file_handlers.py b/file_handlers.py index 8248e8a..6c0813a 100644 --- a/file_handlers.py +++ b/file_handlers.py @@ -12,6 +12,8 @@ class FileHandler(ABC): file_handler = JsonFileHandler() case '.gradle': file_handler = GradleFileHandler() + case '.clj': + file_handler = CljFileHandler() case _: raise Exception(f'The file type "{config_file_type}" is not implemented') @@ -54,13 +56,14 @@ class GradleFileHandler(FileHandler): with open(self.config_file_path, 'r') as gradle_file: contents = gradle_file.read() version_line = re.search("\nversion = .*\n", contents) + exception = Exception("Version not found in gradle file") if version_line is None: - raise Exception("Version not found in gradle file") + raise exception version_line = version_line.group() version_string = re.search('[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line) if version_string is None: - raise Exception("Version not found in gradle file") + raise exception version_string = version_string.group() is_snapshot = False @@ -79,3 +82,38 @@ class GradleFileHandler(FileHandler): gradle_file.seek(0) gradle_file.write(version_substitute) gradle_file.truncate() + +class CljFileHandler(FileHandler): + + def parse(self) -> tuple[list[int], bool]: + with open(self.config_file_path, 'r') as clj_file: + contents = clj_file.read() + version_line = re.search("^\\(defproject .*\n", contents) + exception = Exception("Version not found in clj file") + if version_line is None: + raise exception + + version_line = version_line.group() + version_string = re.search('[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line) + if version_string is None: + raise exception + + version_string = version_string.group() + is_snapshot = False + if '-SNAPSHOT' in version_string: + is_snapshot = True + version_string = version_string.replace('-SNAPSHOT', '') + + version = [int(x) for x in version_string.split('.')] + + return version, is_snapshot + + def write(self, version_string): + with open(self.config_file_path, 'r+') as clj_file: + clj_first = clj_file.readline() + clj_rest = clj_file.read() + version_substitute = re.sub('[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', f'"{version_string}"\n', clj_first) + clj_file.seek(0) + clj_file.write(version_substitute) + clj_file.write(clj_rest) + clj_file.truncate() \ No newline at end of file diff --git a/project.clj b/project.clj new file mode 100644 index 0000000..6e14c1f --- /dev/null +++ b/project.clj @@ -0,0 +1,46 @@ +(defproject org.domaindrivenarchitecture/c4k-website "1.1.3-SNAPSHOT" + :description "website c4k-installation package" + :url "https://domaindrivenarchitecture.org" + :license {:name "Apache License, Version 2.0" + :url "https://www.apache.org/licenses/LICENSE-2.0.html"} + :dependencies [[org.clojure/clojure "1.11.1"] + [org.clojure/tools.reader "1.3.6"] + [org.domaindrivenarchitecture/c4k-common-clj "5.0.1"] + [hickory "0.7.1"]] + :target-path "target/%s/" + :source-paths ["src/main/cljc" + "src/main/clj"] + :resource-paths ["src/main/resources"] + :repositories [["snapshots" :clojars] + ["releases" :clojars]] + :deploy-repositories [["snapshots" {:sign-releases false :url "https://clojars.org/repo"}] + ["releases" {:sign-releases false :url "https://clojars.org/repo"}]] + :profiles {:test {:test-paths ["src/test/cljc"] + :resource-paths ["src/test/resources"] + :dependencies [[dda/data-test "0.1.1"]]} + :dev {:plugins [[lein-shell "0.5.0"]]} + :uberjar {:aot :all + :main dda.c4k-website.uberjar + :uberjar-name "c4k-website-standalone.jar" + :dependencies [[org.clojure/tools.cli "1.0.214"] + [ch.qos.logback/logback-classic "1.4.5" + :exclusions [com.sun.mail/javax.mail]] + [org.slf4j/jcl-over-slf4j "2.0.6"]]}} + :release-tasks [["test"] + ["vcs" "assert-committed"] + ["change" "version" "leiningen.release/bump-version" "release"] + ["vcs" "commit"] + ["vcs" "tag" "v" "--no-sign"] + ["change" "version" "leiningen.release/bump-version"]] + :aliases {"native" ["shell" + "native-image" + "--report-unsupported-elements-at-runtime" + "--initialize-at-build-time" + "-jar" "target/uberjar/c4k-website-standalone.jar" + "-H:ResourceConfigurationFiles=graalvm-resource-config.json" + "-H:Log=registerResource" + "-H:Name=target/graalvm/${:name}"] + "inst" ["shell" + "sh" + "-c" + "lein uberjar && sudo install -m=755 target/uberjar/c4k-website-standalone.jar /usr/local/bin/c4k-website-standalone.jar"]}) diff --git a/test/config.clj b/test/config.clj new file mode 100644 index 0000000..c329c34 --- /dev/null +++ b/test/config.clj @@ -0,0 +1,5 @@ +(defproject org.domaindrivenarchitecture/c4k-website "1.1.3" + :description "website c4k-installation package" + :url "https://domaindrivenarchitecture.org" + :license {:name "Apache License, Version 2.0" + :url "https://www.apache.org/licenses/LICENSE-2.0.html"}) \ No newline at end of file diff --git a/test/test_version_class.py b/test/test_version_class.py index 55b6df2..9811e09 100644 --- a/test/test_version_class.py +++ b/test/test_version_class.py @@ -58,4 +58,21 @@ def test_json(tmp_path): version.to_file() # check - assert '"version": "123.123.456-SNAPSHOT"' in f.read_text() \ No newline at end of file + assert '"version": "123.123.456-SNAPSHOT"' in f.read_text() + +def test_clj(tmp_path): + # init + file_name = 'config.clj' + with open(f'test/{file_name}', 'r') as gradle_file: + contents = gradle_file.read() + + f = tmp_path / file_name + f.write_text(contents) + + # test + version = Version.from_file(f) + version.increment(ReleaseLevel.SNAPSHOT) + version.to_file() + + # check + assert '1.1.3-SNAPSHOT' in f.read_text() \ No newline at end of file