diff --git a/pgxcrown.nimble b/pgxcrown.nimble index 942fbf7..4635e66 100644 --- a/pgxcrown.nimble +++ b/pgxcrown.nimble @@ -1,12 +1,15 @@ # Package -version = "0.2.9" +version = "0.3.0" author = "luisacosta828" description = "Build Postgres extensions in Nim." srcDir = "src" license = "MIT" -bin = @["build_extension"] +bin = @["cli/pgxcrown"] binDir = "src/bin" + +#namedBin["build_extension"] = "pgxcrown" + # Dependencies requires "nim >= 0.12.0" diff --git a/src/bin/cli/pgxcrown b/src/bin/cli/pgxcrown new file mode 100755 index 0000000..8e664d0 Binary files /dev/null and b/src/bin/cli/pgxcrown differ diff --git a/src/build_extension.nim b/src/build_extension.nim deleted file mode 100644 index 85dfd29..0000000 --- a/src/build_extension.nim +++ /dev/null @@ -1,50 +0,0 @@ -from strutils import parseInt, repeat, join, split -from osproc import execCmd, execCmdEx -import tables,os - -var file = paramStr(1) - -discard execCmd "nim c -d:release --hints:off --opt:size --app:lib " & file & ".nim" - -var produced_lib = "lib"&extractFilename(file) - -var postgreslib = execCmdEx("pg_config --pkglibdir").output.split("\n")[0] - -echo "Moving ",produced_lib,".so to ", postgreslib -discard execCmd "sudo mv "&produced_lib&".so "&postgreslib - -var nim_target_function:string - -proc build_pg_function():string = - - nim_target_function = execCmdEx("""grep -i pgv1 """ & file & """.nim | awk '{ print $2}' | tr "()" "\n" | head -n1""").output.split("\n")[0] - - var create_function = "CREATE OR REPLACE FUNCTION "&nim_target_function&"_funtion_template" - - var types = {"Int16": "int", "Int32": "int", "Float4":"real", "Float8": "real", "Varchar": "varchar"}.toTable - - var get_type:tuple[output:string, exitCode:int] - var return_type:tuple[output:string, exitCode:int] - var total_args:int - var param_builder:string = "(" - var returns:string - var args:seq[string] - - for key,value in types.pairs(): - get_type = execCmdEx("grep -c get"&key&" "&file&".nim") - return_type = execCmdEx("grep -c return"&key&" "&file&".nim") - if get_type.exitCode == 0: - total_args = parseInt(get_type.output.split("\n")[0]) - 1 - for i in 0..total_args: - args.add(value) - if return_type.exitCode == 0: - returns = " RETURNS "&value - - param_builder = param_builder & args.join(",") & ")" - - result = create_function & param_builder & returns & " as '"&file&".so' , '"&nim_target_function&"' LANGUAGE C STRICT;" - -discard execCmd """ echo " """ & build_pg_function() & """ " > """ & nim_target_function & ".sql" - -echo "Created file: ",nim_target_function,".sql" - diff --git a/src/cli/build_extension.nim b/src/cli/build_extension.nim new file mode 100644 index 0000000..adac2b0 --- /dev/null +++ b/src/cli/build_extension.nim @@ -0,0 +1,62 @@ +from strutils import parseInt, repeat, join, split +from osproc import execCmd, execCmdEx +import tables,os + +var nim_target_function* :string + +{.push inline .} + +proc compile_library(file:string) = discard execCmdEx "nim c -d:release --hints:off --opt:size --app:lib " & file & ".nim" +proc getLibName(file:string):string = "lib"&extractFilename(file) +proc getPostgresLibDir(): string = execCmdEx("pg_config --pkglibdir").output.split("\n")[0] + +proc moveTo( libname:string, postgreslib:string ) = discard execCmd "sudo mv "&libname&".so "&postgreslib + +proc extractV1Function( file:string ):string = + execCmdEx("""grep -i pgv1 """ & file & """.nim | awk '{ print $2}' | tr "()" "\n" | head -n1""").output.split("\n")[0] + +proc createSQLFunction( file:string, nim_target_function: string): string = + var create_function = "CREATE OR REPLACE FUNCTION "&nim_target_function&"_template" + var types = {"Int16": "int", "Int32": "int", "Float4":"real", "Float8": "real", "Varchar": "varchar"}.toTable + var get_type:tuple[output:string, exitCode:int] + var return_type:tuple[output:string, exitCode:int] + var total_args:int + var param_builder:string = "(" + var returns:string + var args:seq[string] + + for key,value in types.pairs(): + get_type = execCmdEx("grep -c get"&key&" "&file&".nim") + return_type = execCmdEx("grep -c return"&key&" "&file&".nim") + if get_type.exitCode == 0: + total_args = parseInt(get_type.output.split("\n")[0]) - 1 + for i in 0..total_args: + args.add(value) + if return_type.exitCode == 0: + returns = " RETURNS "&value + + param_builder = param_builder & args.join(",") & ")" + result = create_function & param_builder & returns & " as '"&getLibName(file)&".so' , '"&nim_target_function&"' LANGUAGE C STRICT;" + + +proc build_pg_function*( file:string ):string = + + echo "Compiling: ",file, "..." + compile_library(file) + + echo "Searching postgres libdir..." + var postgreslib = getPostgresLibDir() + + var libname = getLibName(file) + + echo "Moving ",libname,".so to ", postgreslib + + libname.moveTo(postgreslib) + + nim_target_function = extractV1Function(file) + + echo "Creating SQL Function..." + + result = createSQLFunction(file,nim_target_function) + +{.pop.} diff --git a/src/cli/pgxcrown.nim b/src/cli/pgxcrown.nim new file mode 100644 index 0000000..f516d22 --- /dev/null +++ b/src/cli/pgxcrown.nim @@ -0,0 +1,11 @@ +import os, osproc + +import build_extension + +var build_opt = paramStr(1) + +if build_opt == "--build-extension": + discard execCmd """ echo " """ & build_pg_function(paramStr(2)) & """ " > """ & nim_target_function & ".sql" + echo "Created file: ",nim_target_function,".sql" +else: + echo "error"