From 96c7bef55ec768e3534f7b8c950f119ddf66ec63 Mon Sep 17 00:00:00 2001 From: surister Date: Wed, 29 May 2024 13:47:34 +0200 Subject: [PATCH] Replace the version on the target index_files rather instead of appending. --- cratedb_sqlparse_js/cratedb_sqlparse/index.js | 1 + .../cratedb_sqlparse/__init__.py | 2 ++ setup_grammar.py | 19 ++++++++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cratedb_sqlparse_js/cratedb_sqlparse/index.js b/cratedb_sqlparse_js/cratedb_sqlparse/index.js index 344bb4a..692a1f4 100644 --- a/cratedb_sqlparse_js/cratedb_sqlparse/index.js +++ b/cratedb_sqlparse_js/cratedb_sqlparse/index.js @@ -1,2 +1,3 @@ import {sqlparse} from "./parser.js"; export {sqlparse}; +export const __cratedb_version__ = "5.6.4" diff --git a/cratedb_sqlparse_py/cratedb_sqlparse/__init__.py b/cratedb_sqlparse_py/cratedb_sqlparse/__init__.py index c9b943b..11bbf49 100644 --- a/cratedb_sqlparse_py/cratedb_sqlparse/__init__.py +++ b/cratedb_sqlparse_py/cratedb_sqlparse/__init__.py @@ -1,3 +1,5 @@ from .parser import ParsingException, sqlparse __all__ = ["sqlparse", "ParsingException"] +__cratedb_version__ = "5.6.4" + diff --git a/setup_grammar.py b/setup_grammar.py index c834f8c..3ed23eb 100644 --- a/setup_grammar.py +++ b/setup_grammar.py @@ -2,6 +2,7 @@ import logging import subprocess import sys +import re from enum import Enum from pathlib import Path @@ -137,9 +138,18 @@ def set_version(target: Antlr4Target, version: str): index_file = 'index.js' variable = 'export const __cratedb_version__' - # FIXME: Need to apply "replace" instead of "append"? - with open(target_path / index_file, "a") as f: - f.write(f"{variable} = {version}\n") + with open(target_path / index_file, "r+") as f: + content = f.read() + + # Removes the current content on disk. + f.seek(0) + f.truncate() + + updated_content = re.sub(f'({variable} = )"(.*)"', r'\1' + version, content) + + f.write(updated_content) + + logger.info(f'Updated {variable} to {version} in {index_file}') if __name__ == '__main__': @@ -150,14 +160,17 @@ def set_version(target: Antlr4Target, version: str): TODO: Improve efficiency by generating runtime parser for all implemented languages at once. """ setup_logging() + input_target = sys.argv[1] version = '5.6.4' + if input_target.startswith("py"): target = Antlr4Target.python elif input_target.startswith("js") or input_target.startswith("java"): target = Antlr4Target.js else: raise NotImplementedError(f"Parser generator for target {input_target} not implemented") + download_cratedb_grammar(version) compile_grammar(target) patch_lexer(target)