Implement clj file handler

This commit is contained in:
erik 2023-02-09 13:56:45 +01:00
parent 86c66dc7f7
commit a4c8f8934a
4 changed files with 109 additions and 3 deletions

View file

@ -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()

46
project.clj Normal file
View file

@ -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"]})

5
test/config.clj Normal file
View file

@ -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"})

View file

@ -59,3 +59,20 @@ def test_json(tmp_path):
# check
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()