diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5be77215..b438bdbb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -123,9 +123,8 @@ jobs: run: ./scripts/wasm/update_sha.sh sqlc.ci.yaml - name: Codegen Test - run: | - ./scripts/run_codegen_test.sh sqlc.ci.yaml \ - ${{ matrix.file-per-query }} ${{ matrix.generate-csproj }} ${{ matrix.target-framework }} + run: ./scripts/run_codegen_test.sh sqlc.ci.yaml \ + ${{ matrix.file-per-query }} ${{ matrix.generate-csproj }} ${{ matrix.target-framework }} - uses: actions/setup-dotnet@v4 with: @@ -186,7 +185,7 @@ jobs: - name: Bump version and create new tag id: bump_version run: | - set -ex + set -e echo "Extract the latest tag version" LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV diff --git a/Dockerfile b/Dockerfile index 7d43a193..719a3f50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,10 +9,10 @@ COPY Drivers/*.csproj ./Drivers/ COPY EndToEndTests/*.csproj ./EndToEndTests/ COPY Extensions/*.csproj ./Extensions/ COPY GeneratedProtobuf/*.csproj ./GeneratedProtobuf/ +COPY LocalRunner/*.csproj ./LocalRunner/ COPY MySqlConnectorExample/*.csproj ./MySqlConnectorExample/ COPY NpgsqlExample/*.csproj ./NpgsqlExample/ COPY PluginOptions/*.csproj ./PluginOptions/ -COPY ProcessRunner/*.csproj ./ProcessRunner/ COPY SqlcGenCsharp/*.csproj ./SqlcGenCsharp/ COPY WasmRunner/*.csproj ./WasmRunner/ @@ -21,10 +21,10 @@ COPY Drivers/ ./Drivers/ COPY EndToEndTests/ ./EndToEndTests/ COPY Extensions/ ./Extensions/ COPY GeneratedProtobuf/ ./GeneratedProtobuf/ +COPY LocalRunner/ ./LocalRunner/ COPY MySqlConnectorExample/ ./MySqlConnectorExample/ COPY NpgsqlExample/ ./NpgsqlExample/ COPY PluginOptions/ ./PluginOptions/ -COPY ProcessRunner/ ./ProcessRunner/ COPY SqlcGenCsharp/ ./SqlcGenCsharp/ COPY WasmRunner/ ./WasmRunner/ diff --git a/Drivers/DbDriver.cs b/Drivers/DbDriver.cs index 9c3c43ec..ed412357 100644 --- a/Drivers/DbDriver.cs +++ b/Drivers/DbDriver.cs @@ -25,7 +25,7 @@ public virtual UsingDirectiveSyntax[] GetUsingDirectives() public string AddNullableSuffix(string csharpType, bool notNull) { if (notNull) return csharpType; - if (Utils.IsCsharpPrimitive(csharpType)) return $"{csharpType}?"; + if (IsCsharpPrimitive(csharpType)) return $"{csharpType}?"; return DotnetFramework.LatestDotnetSupported() ? $"{csharpType}?" : csharpType; } @@ -73,4 +73,10 @@ public abstract MemberDeclarationSyntax ExecDeclare(string funcName, string text public abstract MemberDeclarationSyntax ExecLastIdDeclare(string funcName, string queryTextConstant, string argInterface, IList parameters); + + public static bool IsCsharpPrimitive(string csharpType) + { + var csharpPrimitives = new HashSet { "long", "double", "int", "float", "bool" }; + return csharpPrimitives.Contains(csharpType.Replace("?", "")); + } } \ No newline at end of file diff --git a/Drivers/Generators/CommonGen.cs b/Drivers/Generators/CommonGen.cs index 97d8e399..6ea523aa 100644 --- a/Drivers/Generators/CommonGen.cs +++ b/Drivers/Generators/CommonGen.cs @@ -55,7 +55,7 @@ string GetNullExpression(Column column) var csharpType = dbDriver.GetColumnType(column); if (csharpType == "string") return "string.Empty"; - return !dbDriver.DotnetFramework.LatestDotnetSupported() && Utils.IsCsharpPrimitive(csharpType) + return !dbDriver.DotnetFramework.LatestDotnetSupported() && DbDriver.IsCsharpPrimitive(csharpType) ? $"({csharpType}) null" : "null"; } diff --git a/Drivers/Utils.cs b/Drivers/Utils.cs deleted file mode 100644 index 578ddd72..00000000 --- a/Drivers/Utils.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; - -namespace SqlcGenCsharp.Drivers; - -public static class Utils -{ - public static bool IsCsharpPrimitive(string csharpType) - { - var csharpPrimitives = new HashSet { "long", "double", "int", "float", "bool" }; - return csharpPrimitives.Contains(csharpType.Replace("?", "")); - } -} \ No newline at end of file diff --git a/ProcessRunner/App.cs b/LocalRunner/App.cs similarity index 100% rename from ProcessRunner/App.cs rename to LocalRunner/App.cs diff --git a/ProcessRunner/ProcessRunner.csproj b/LocalRunner/LocalRunner.csproj similarity index 100% rename from ProcessRunner/ProcessRunner.csproj rename to LocalRunner/LocalRunner.csproj diff --git a/ProcessRunner/Properties/AssemblyInfo.cs b/LocalRunner/Properties/AssemblyInfo.cs similarity index 100% rename from ProcessRunner/Properties/AssemblyInfo.cs rename to LocalRunner/Properties/AssemblyInfo.cs diff --git a/ProcessRunner/nuget.config b/LocalRunner/nuget.config similarity index 100% rename from ProcessRunner/nuget.config rename to LocalRunner/nuget.config diff --git a/Makefile b/Makefile index 72980be8..628471b3 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ dotnet-publish-process: dotnet-build-process dotnet publish ProcessRunner -c release --output dist/ sqlc-generate-process: dotnet-publish-process - sqlc -f sqlc.process.yaml generate + sqlc -f sqlc.local.yaml generate test-process-plugin: sqlc-generate-process dockerfile-generate run-tests diff --git a/scripts/run_codegen_test.sh b/scripts/run_codegen_test.sh index 4b39ce62..d2be349b 100755 --- a/scripts/run_codegen_test.sh +++ b/scripts/run_codegen_test.sh @@ -4,80 +4,80 @@ set -e declare -a examples=("MySqlConnectorExample" "NpgsqlExample") +config_file=$1 +file_per_query=$2 +generate_csproj=$3 +target_framework=$4 + generated_files_cleanup() { - generate_csproj=$1 - for example_dir in "${examples[@]}" - do - echo "Deleting .cs files" && find "${example_dir}/" -type f -name "*.cs" -exec rm -f {} \; - if [ "${generate_csproj}" = "true" ]; then - echo "Deleting .csproj file" && rm "${example_dir}/${example_dir}.csproj" - fi - done + for example_dir in "${examples[@]}" + do + echo "Deleting .cs files in ${example_dir}" + find "${example_dir}/" -type f -name "*.cs" -exec rm -f {} \; + if [ "${generate_csproj}" = "true" ]; then + echo "Deleting .csproj file" && rm "${example_dir}/${example_dir}.csproj" + fi + done +} + +change_config() { + for ((i=0; i<${#examples[@]}; i++)); do + echo "Changing configuration for project ${example_dir}" + yq -i " + .sql[${i}].codegen[0].options.filePerQuery = ${file_per_query} | + .sql[${i}].codegen[0].options.generateCsproj = ${generate_csproj} | + .sql[${i}].codegen[0].options.targetFramework = \"${target_framework}\" + " "${config_file}" + echo "${examples[i]} codegen config:" && yq ".sql[${i}].codegen[0]" "${config_file}" + done } check_cs_file_count() { - file_per_query=$1 - for example_dir in "${examples[@]}" - do - file_count=$(find "${example_dir}/" -maxdepth 1 -name "*.cs" 2>/dev/null | wc -l) - if [[ "${file_per_query}" = "true" && "${file_count}" -le 2 ]]; then - echo "Assertion failed: Not more than 2 .cs files in the directory ${example_dir}." - return 1 - elif [[ "${file_per_query}" = "false" && "${file_count}" -ne 2 ]]; then - echo "Assertion failed: Not exactly 2 .cs files in the directory ${example_dir}." - return 1 - fi - done + for example_dir in "${examples[@]}" + do + echo "Checking C# file count in ${example_dir}/" + file_count=$(find "${example_dir}/" -maxdepth 1 -name "*.cs" 2>/dev/null | wc -l) + if [[ "${file_per_query}" = "true" && "${file_count}" -le 2 ]]; then + echo "Assertion failed: Not more than 2 .cs files in the directory ${example_dir}." + return 1 + elif [[ "${file_per_query}" = "false" && "${file_count}" -ne 2 ]]; then + echo "Assertion failed: Not exactly 2 .cs files in the directory ${example_dir}." + return 1 + fi + done } check_csproj_file() { - for example_dir in "${examples[@]}" - do - if [ ! -f "${example_dir}/${example_dir}.csproj" ]; then - echo "Assertion failed: A .csproj file is not present in the directory ${example_dir}." - return 1 - fi - done + for example_dir in "${examples[@]}" + do + echo "Checking ${example_dir}.csproj file generated" + if [ ! -f "${example_dir}/${example_dir}.csproj" ]; then + echo "Assertion failed: A .csproj file is not present in the directory ${example_dir}." + return 1 + fi + done } check_project_compiles() { + if [ "${generate_csproj}" = "true" ]; then for example_dir in "${examples[@]}" do + echo "Checking ${example_dir} project compiles" dotnet build "${example_dir}/" done + fi } -config_file=$1 -file_per_query=$2 -generate_csproj=$3 -target_framework=$4 - -yq -i " - .sql[0].codegen[0].options.filePerQuery = ${file_per_query} | - .sql[1].codegen[0].options.filePerQuery = ${file_per_query} | - .sql[0].codegen[0].options.generateCsproj = ${generate_csproj} | - .sql[1].codegen[0].options.generateCsproj = ${generate_csproj} | - .sql[0].codegen[0].options.targetFramework = \"${target_framework}\" | - .sql[1].codegen[0].options.targetFramework = \"${target_framework}\" -" "${config_file}" - -generated_files_cleanup "${generate_csproj}" -echo "Using the following codegen config:" && \ - yq '.sql[0].codegen[0]' "${config_file}" && \ - yq '.sql[1].codegen[0]' "${config_file}" +generated_files_cleanup && change_config sqlc -f "${config_file}" generate -status_code=$(check_cs_file_count "${file_per_query}") -if [ "${status_code}" -ne 0 ]; then - exit "${status_code}" -fi -status_code=$(check_csproj_file) -if [ "${status_code}" -ne 0 ]; then +test_functions=("check_cs_file_count" "check_csproj_file" "check_project_compiles") +for test_function in "${test_functions[@]}"; do + ${test_function} + status_code=$? + if [ ${status_code} -ne 0 ]; then + echo "Function ${test_function} failed with status code ${status_code}" exit "${status_code}" -fi -if [ "${generate_csproj}" = "true" ]; then - status_code=$(check_project_compiles "${target_framework}") - if [ "${status_code}" -ne 0 ]; then - exit "${status_code}" - fi -fi \ No newline at end of file + fi + echo "Test ${test_function} passed" +done \ No newline at end of file diff --git a/sqlc-gen-csharp.sln b/sqlc-gen-csharp.sln index 38fa88e2..b3917590 100644 --- a/sqlc-gen-csharp.sln +++ b/sqlc-gen-csharp.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProcessRunner", "ProcessRunner\ProcessRunner.csproj", "{649CFB46-68D1-41F8-BD76-FF4B79B16825}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LocalRunner", "LocalRunner\LocalRunner.csproj", "{649CFB46-68D1-41F8-BD76-FF4B79B16825}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySqlConnectorExample", "MySqlConnectorExample\MySqlConnectorExample.csproj", "{6406B659-77CF-4978-ABD3-BFBB2CBFFCA3}" EndProject diff --git a/sqlc.process.yaml b/sqlc.local.yaml similarity index 100% rename from sqlc.process.yaml rename to sqlc.local.yaml