diff --git a/.github/workflows/test-dotnet.yaml b/.github/workflows/test-dotnet.yaml index 233cb2bdcf80..c7a7a0119b40 100644 --- a/.github/workflows/test-dotnet.yaml +++ b/.github/workflows/test-dotnet.yaml @@ -6,20 +6,54 @@ on: jobs: test: - runs-on: ubuntu-latest + runs-on: windows-latest steps: - name: ๐Ÿ“‚ Checkout code uses: actions/checkout@v4 - name: ๐Ÿ— Build prqlc-clib - run: cargo build --package prqlc-clib + run: cargo build --package prqlc-clib --release + - name: ๐Ÿ”ข Set version + shell: bash + run: ./.github/workflows/scripts/set_version.sh + - name: ๐Ÿœ Debug CI/CD (version + target/release) + shell: pwsh + run: | + Write-Host "Version: $env:version" + Get-ChildItem ./target/release/ - name: ๐Ÿ”ง Setup dotnet uses: actions/setup-dotnet@v3 with: - dotnet-version: 7 - - name: ๐Ÿงช Build and test - working-directory: prqlc/bindings - run: | - dotnet build dotnet - cp ../../target/debug/libprqlc_lib.* dotnet/PrqlCompiler/bin/Debug/net*/ - cp ../../target/debug/libprqlc_lib.* dotnet/PrqlCompiler.Tests/bin/Debug/net*/ - dotnet test dotnet + dotnet-version: | + 6 + 7 + - name: ๐Ÿ› ๏ธ Build + working-directory: prqlc/bindings/dotnet + shell: cmd + run: + dotnet build prql-net.sln -p:version="%version%" -c Release + /p:ContinuousIntegrationBuild=true --nologo + - name: ๐Ÿงช Test + working-directory: prqlc/bindings/dotnet + shell: cmd + run: + dotnet test PrqlCompiler.tests\PrqlCompiler.Tests.csproj -c release + --runtime win-x64 --logger GitHubActions /p:CI=true --nologo + - name: ๐Ÿ“ฆ Package + working-directory: prqlc/bindings/dotnet + shell: cmd + run: + dotnet pack PrqlCompiler -p:version="%version%" -c Release + --include-symbols /p:CI=true --no-build --nologo + - name: ๐Ÿฑ Archive artifacts + uses: actions/upload-artifact@v3 + with: + name: nuget-packages + path: | + prqlc/bindings/dotnet/PrqlCompiler/bin/Release/*.nupkg + prqlc/bindings/dotnet/PrqlCompiler/bin/Release/*.snupkg + # - name: ๐Ÿšš Publish + # working-directory: prqlc/bindings/dotnet + # run: | + # dotnet nuget push PrqlCompiler/bin/Release/*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json + # env: + # NUGET_AUTH_TOKEN: ${{ secrets.NUGET_TOKEN }} diff --git a/prqlc/bindings/dotnet/.editorconfig b/prqlc/bindings/dotnet/.editorconfig new file mode 100644 index 000000000000..ccb0b8371b8e --- /dev/null +++ b/prqlc/bindings/dotnet/.editorconfig @@ -0,0 +1,175 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +root = true + +# Default settings: +# A newline ending every file +# Use 4 spaces as indentation +[*] +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +dotnet_style_operator_placement_when_wrapping = beginning_of_line +tab_width = 4 +end_of_line = lf + +# C# files +[*.cs] +# New line preferences +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true + +# Indentation preferences +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents = true +csharp_indent_case_contents_when_block = true +csharp_indent_switch_labels = true +csharp_indent_labels = one_less_than_current + +# Modifier preferences +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion + +# avoid this. unless absolutely necessary +dotnet_style_qualification_for_field = false:suggestion +dotnet_style_qualification_for_property = false:suggestion +dotnet_style_qualification_for_method = false:suggestion +dotnet_style_qualification_for_event = false:suggestion + +# Types: use keywords instead of BCL types, and permit var only when the type is clear +csharp_style_var_for_built_in_types = true:none +csharp_style_var_when_type_is_apparent = true:suggestion +csharp_style_var_elsewhere = true:suggestion +dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion +dotnet_style_predefined_type_for_member_access = true:suggestion + +# name all constant fields using PascalCase +dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style +dotnet_naming_symbols.constant_fields.applicable_kinds = field +dotnet_naming_symbols.constant_fields.required_modifiers = const +dotnet_naming_style.pascal_case_style.capitalization = pascal_case + +# static fields should have s_ prefix +dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion +dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields +dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style +dotnet_naming_symbols.static_fields.applicable_kinds = none +dotnet_naming_symbols.static_fields.required_modifiers = static +dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected +dotnet_naming_style.static_prefix_style.required_prefix = s_ +dotnet_naming_style.static_prefix_style.capitalization = camel_case + +# internal and private fields should be _camelCase +dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion +dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields +dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_style +dotnet_naming_symbols.private_internal_fields.applicable_kinds = field +dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal +dotnet_naming_style.camel_case_style.capitalization = camel_case + +# Code style defaults +csharp_using_directive_placement = outside_namespace:suggestion +dotnet_sort_system_directives_first = true +csharp_prefer_braces = true:silent +csharp_preserve_single_line_blocks = true:none +csharp_preserve_single_line_statements = false:none +csharp_prefer_static_local_function = true:suggestion +csharp_prefer_simple_using_statement = false:none +csharp_style_prefer_switch_expression = true:suggestion + +# Code quality +dotnet_style_readonly_field = true:suggestion +dotnet_code_quality_unused_parameters = non_public:suggestion + +# Expression-level preferences +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_auto_properties = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:refactoring +dotnet_style_prefer_conditional_expression_over_return = true:refactoring +csharp_prefer_simple_default_expression = true:suggestion + +# Expression-bodied members +csharp_style_expression_bodied_methods = true:silent +csharp_style_expression_bodied_constructors = true:silent +csharp_style_expression_bodied_operators = true:silent +csharp_style_expression_bodied_properties = true:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_accessors = true:silent +csharp_style_expression_bodied_lambdas = true:refactoring +csharp_style_expression_bodied_local_functions = true:refactoring + +# Pattern matching +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion + +# Null checking preferences +csharp_style_throw_expression = true:suggestion +csharp_style_conditional_delegate_call = true:suggestion + +# Other features +csharp_style_prefer_index_operator = false:none +csharp_style_prefer_range_operator = false:none +csharp_style_pattern_local_over_anonymous_function = false:none + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_after_comma = true +csharp_space_after_dot = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_after_semicolon_in_for_statement = true +csharp_space_around_binary_operators = before_and_after +csharp_space_around_declaration_statements = do_not_ignore +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_before_comma = false +csharp_space_before_dot = false +csharp_space_before_open_square_brackets = false +csharp_space_before_semicolon_in_for_statement = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_between_square_brackets = false +csharp_style_namespace_declarations = block_scoped:silent +csharp_style_prefer_method_group_conversion = true:silent +csharp_style_prefer_top_level_statements = true:silent +csharp_style_prefer_primary_constructors = false:suggestion + +# Xml project files +[*.{csproj,props,targets}] +indent_size = 2 +charset = utf-8 diff --git a/prqlc/bindings/dotnet/.gitignore b/prqlc/bindings/dotnet/.gitignore index 1746e3269ed0..f1e7fbc4887c 100644 --- a/prqlc/bindings/dotnet/.gitignore +++ b/prqlc/bindings/dotnet/.gitignore @@ -1,2 +1,354 @@ -bin -obj +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Codecod +codecov.exe +.coverage/ diff --git a/prqlc/bindings/dotnet/Directory.Build.props b/prqlc/bindings/dotnet/Directory.Build.props new file mode 100644 index 000000000000..9abd58d9224d --- /dev/null +++ b/prqlc/bindings/dotnet/Directory.Build.props @@ -0,0 +1,71 @@ + + + + true + false + portable + + + + net6.0;net7.0 + AnyCPU + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + Prql.Compiler + prql-logo.png + PRQL Compiler + PRQL + Copyright ยฉ The PRQL Project 2023 + .NET bindings for the PRQL compiler. PRQL is a modern language for transforming data + Apache-2.0 + https://prql-lang.org/ + README.md + prql;sql + https://github.com/PRQL/prql + git + false + snupkg + + + + + True + \ + + + True + \ + + + + + + $(NoWarn);CS1591 + + + + false + true + + + + 5 + preview + + + + 5 + preview + + + diff --git a/prqlc/bindings/dotnet/Directory.Build.targets b/prqlc/bindings/dotnet/Directory.Build.targets new file mode 100644 index 000000000000..bedf09ca8c08 --- /dev/null +++ b/prqlc/bindings/dotnet/Directory.Build.targets @@ -0,0 +1,22 @@ + + + + + + + + + + + <_Parameter1>%(InternalsVisibleTo.Identity) + + + + + + + <_Parameter1>$(AssemblyName)%(InternalsVisibleToSuffix.Identity) + + + + diff --git a/prqlc/bindings/dotnet/PrqlCompiler.Tests/CompilerTest.cs b/prqlc/bindings/dotnet/PrqlCompiler.Tests/CompilerTest.cs index 5d7fdf1e1e91..1a97b6cadc26 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler.Tests/CompilerTest.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler.Tests/CompilerTest.cs @@ -2,7 +2,7 @@ namespace Prql.Compiler.Tests; -sealed public class CompilerTest +public sealed class CompilerTest { [Fact] public void ToCompile_Works() diff --git a/prqlc/bindings/dotnet/PrqlCompiler.Tests/PrqlCompiler.Tests.csproj b/prqlc/bindings/dotnet/PrqlCompiler.Tests/PrqlCompiler.Tests.csproj index eeddc9523e9a..de0fc968f4e3 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler.Tests/PrqlCompiler.Tests.csproj +++ b/prqlc/bindings/dotnet/PrqlCompiler.Tests/PrqlCompiler.Tests.csproj @@ -1,14 +1,10 @@ - - net7.0 - enable - enable - - false - - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/prqlc/bindings/dotnet/PrqlCompiler/Message.cs b/prqlc/bindings/dotnet/PrqlCompiler/Message.cs index d580dd823000..20b07e7c0e96 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/Message.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/Message.cs @@ -1,46 +1,45 @@ using System.Runtime.InteropServices; -namespace Prql.Compiler +namespace Prql.Compiler; + +/// +/// Compile result message. +/// +[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] +public struct Message { /// - /// Compile result message. + /// Message kind. Currently only Error is implemented. + /// + public MessageKind Kind { get; set; } + + /// + /// Machine-readable identifier of the error. + /// + public string Code { get; set; } + + /// + /// Plain text of the error. + /// + public string Reason { get; set; } + + /// + /// A list of suggestions of how to fix the error. + /// + public string Hint { get; set; } + + /// + /// Character offset of error origin within a source file. + /// + public Span Span { get; set; } + + /// + /// Annotated code, containing cause and hints. + /// + public string Display { get; set; } + + /// + /// Line and column number of error origin within a source file. /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct Message - { - /// - /// Message kind. Currently only Error is implemented. - /// - public MessageKind Kind { get; set; } - - /// - /// Machine-readable identifier of the error. - /// - public string Code { get; set; } - - /// - /// Plain text of the error. - /// - public string Reason { get; set; } - - /// - /// A list of suggestions of how to fix the error. - /// - public string Hint { get; set; } - - /// - /// Character offset of error origin within a source file. - /// - public Span Span { get; set; } - - /// - /// Annotated code, containing cause and hints. - /// - public string Display { get; set; } - - /// - /// Line and column number of error origin within a source file. - /// - public SourceLocation Location { get; set; } - } + public SourceLocation Location { get; set; } } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/MessageKind.cs b/prqlc/bindings/dotnet/PrqlCompiler/MessageKind.cs index efb0a4cb015f..2958666d88b2 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/MessageKind.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/MessageKind.cs @@ -1,24 +1,23 @@ using System; -namespace Prql.Compiler +namespace Prql.Compiler; + +/// +/// Compile message kind. Currently only Error is implemented. +/// +[Serializable] +public enum MessageKind { /// - /// Compile message kind. Currently only Error is implemented. + /// Error message. + /// + Error, + /// + /// Warning message. + /// + Warning, + /// + /// Lint message. /// - [Serializable] - public enum MessageKind - { - /// - /// Error message. - /// - Error, - /// - /// Warning message. - /// - Warning, - /// - /// Lint message. - /// - Lint - } + Lint } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/NativePrqlCompilerOptions.cs b/prqlc/bindings/dotnet/PrqlCompiler/NativePrqlCompilerOptions.cs index c267e4eeed1e..69dcb25a8a30 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/NativePrqlCompilerOptions.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/NativePrqlCompilerOptions.cs @@ -1,16 +1,15 @@ -namespace Prql.Compiler +namespace Prql.Compiler; + +internal struct NativePrqlCompilerOptions { - internal struct NativePrqlCompilerOptions - { - public bool Format; - public string Target; - public bool SignatureComment; + public bool Format; + public string Target; + public bool SignatureComment; - public NativePrqlCompilerOptions(PrqlCompilerOptions options) - { - Format = options.Format; - Target = options.Target; - SignatureComment = options.SignatureComment; - } + public NativePrqlCompilerOptions(PrqlCompilerOptions options) + { + Format = options.Format; + Target = options.Target; + SignatureComment = options.SignatureComment; } } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/NativeResult.cs b/prqlc/bindings/dotnet/PrqlCompiler/NativeResult.cs index 8902e1ed44e5..b15940ed14c6 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/NativeResult.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/NativeResult.cs @@ -1,13 +1,12 @@ using System; -namespace Prql.Compiler +namespace Prql.Compiler; + +internal struct NativeResult { - internal struct NativeResult - { #pragma warning disable CS0649 // Field is never assigned to - public string Output; - public IntPtr Messages; - public int MessagesLen; + public string Output; + public IntPtr Messages; + public int MessagesLen; #pragma warning restore CS0649 // Field is never assigned to - } } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.cs b/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.cs index 2221c9afb352..163dab3d46f0 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.cs @@ -1,141 +1,119 @@ using System; using System.Runtime.InteropServices; -namespace Prql.Compiler +namespace Prql.Compiler; + +/// +/// The PRQL compiler transpiles RPQL queries. +/// +public static class PrqlCompiler { /// - /// The PRQL compiler transpiles RPQL queries. + /// Compile a PRQL string into a SQL string. + /// + /// A PRQL query. + /// SQL query. + /// is null or empty. + /// cannot be compiled. + public static Result Compile(string prqlQuery) + { + if (string.IsNullOrEmpty(prqlQuery)) + throw new ArgumentException("PRQL query cannot be null or empty.", nameof(prqlQuery)); + + var options = new PrqlCompilerOptions(); + + return Compile(prqlQuery, options); + } + + /// + /// Compile a PRQL string into a SQL string. + /// + /// A PRQL query. + /// PRQL compiler options. + /// SQL query. + /// is null or empty. + /// cannot be compiled. + public static Result Compile(string prqlQuery, PrqlCompilerOptions options) + { + if (string.IsNullOrEmpty(prqlQuery)) + throw new ArgumentException("PRQL query cannot be null or empty.", nameof(prqlQuery)); + + var nativeOptions = new NativePrqlCompilerOptions(options); + var nativeResult = CompileExtern(prqlQuery, ref nativeOptions); + var result = new Result(nativeResult); + + return result; + } + + /// + /// Build PL AST from a PRQL string. /// - public static class PrqlCompiler + /// A PRQL query. + /// JSON. + /// is null or empty. + /// cannot be compiled. + /// https://docs.rs/prql-compiler/latest/prql_compiler/ir/pl + public static Result PrqlToPl(string prqlQuery) { - /// - /// Compile a PRQL string into a SQL string. - /// - /// A PRQL query. - /// SQL query. - /// is null or empty. - /// cannot be compiled. - public static Result Compile(string prqlQuery) - { - if (string.IsNullOrEmpty(prqlQuery)) - { - throw new ArgumentException(nameof(prqlQuery)); - } - - var options = new PrqlCompilerOptions(); - - return Compile(prqlQuery, options); - } - - /// - /// Compile a PRQL string into a SQL string. - /// - /// A PRQL query. - /// PRQL compiler options. - /// SQL query. - /// is null or empty. - /// is null. - /// cannot be compiled. - public static Result Compile(string prqlQuery, PrqlCompilerOptions options) - { - if (string.IsNullOrEmpty(prqlQuery)) - { - throw new ArgumentException(nameof(prqlQuery)); - } - - if (options is null) - { - throw new ArgumentException(nameof(options)); - } - - var nativeOptions = new NativePrqlCompilerOptions(options); - var nativeResult = CompileExtern(prqlQuery, ref nativeOptions); - var result = new Result(nativeResult); - - return result; - } - - /// - /// Build PL AST from a PRQL string. - /// - /// A PRQL query. - /// JSON. - /// is null or empty. - /// cannot be compiled. - /// https://docs.rs/prql-compiler/latest/prql_compiler/ir/pl - public static Result PrqlToPl(string prqlQuery) - { - if (string.IsNullOrEmpty(prqlQuery)) - { - throw new ArgumentException(nameof(prqlQuery)); - } - - var nativeResult = PrqlToPlExtern(prqlQuery); - var result = new Result(nativeResult); - - return result; - } - - /// - /// Finds variable references, validates functions calls, determines frames and converts PL to RQ. - /// - /// A PRQL query. - /// JSON. - /// is null or empty. - /// cannot be compiled. - /// https://docs.rs/prql-compiler/latest/prql_compiler/ast - public static Result PlToRq(string plJson) - { - if (string.IsNullOrEmpty(plJson)) - { - throw new ArgumentException(nameof(plJson)); - } - - var nativeResult = PlToRqExtern(plJson); - var result = new Result(nativeResult); - - return result; - } - - /// - /// Convert RQ AST into an SQL string. - /// - /// RQ string in JSON format. - /// PRQL compiler options. - /// JSON. - /// is null or empty. - /// is null. - /// cannot be compiled. - /// https://docs.rs/prql-compiler/latest/prql_compiler/ir/rq - public static Result RqToSql(string rqJson, PrqlCompilerOptions options) - { - if (string.IsNullOrEmpty(rqJson)) - { - throw new ArgumentException(nameof(rqJson)); - } - - if (options is null) - { - throw new ArgumentException(nameof(options)); - } - - var nativeOptions = new NativePrqlCompilerOptions(options); - var nativeResult = RqToSqlExtern(rqJson, ref nativeOptions); - var result = new Result(nativeResult); - - return result; - } - - [DllImport("libprqlc_lib", EntryPoint = "compile")] - private static extern NativeResult CompileExtern(string prqlQuery, ref NativePrqlCompilerOptions options); - - [DllImport("libprqlc_lib", EntryPoint = "prql_to_pl")] - private static extern NativeResult PrqlToPlExtern(string prqlQuery); - - [DllImport("libprqlc_lib", EntryPoint = "pl_to_rq")] - private static extern NativeResult PlToRqExtern(string plJson); - - [DllImport("libprqlc_lib", EntryPoint = "rq_to_sql")] - private static extern NativeResult RqToSqlExtern(string rqJson, ref NativePrqlCompilerOptions options); + if (string.IsNullOrEmpty(prqlQuery)) + throw new ArgumentException("PRQL query cannot be null or empty.", nameof(prqlQuery)); + + var nativeResult = PrqlToPlExtern(prqlQuery); + var result = new Result(nativeResult); + + return result; } + + /// + /// Finds variable references, validates functions calls, determines frames and converts PL to RQ. + /// + /// A PL string in JSON format. + /// JSON. + /// is null or empty. + /// cannot be compiled. + /// https://docs.rs/prql-compiler/latest/prql_compiler/ast + public static Result PlToRq(string plJson) + { + if (string.IsNullOrEmpty(plJson)) + throw new ArgumentException("PL json cannot be null or empty.", nameof(plJson)); + + var nativeResult = PlToRqExtern(plJson); + var result = new Result(nativeResult); + + return result; + } + + /// + /// Convert RQ AST into an SQL string. + /// + /// RQ string in JSON format. + /// PRQL compiler options. + /// JSON. + /// is null or empty. + /// is null. + /// cannot be compiled. + /// https://docs.rs/prql-compiler/latest/prql_compiler/ir/rq + public static Result RqToSql(string rqJson, PrqlCompilerOptions options) + { + if (string.IsNullOrEmpty(rqJson)) + throw new ArgumentException("PL json cannot be null or empty.", nameof(rqJson)); + + var nativeOptions = new NativePrqlCompilerOptions(options); + var nativeResult = RqToSqlExtern(rqJson, ref nativeOptions); + var result = new Result(nativeResult); + + return result; + } + + [DllImport("prqlc_lib", EntryPoint = "compile", CharSet = CharSet.Ansi)] + private static extern NativeResult CompileExtern(string prqlQuery, ref NativePrqlCompilerOptions options); + + [DllImport("prqlc_lib", EntryPoint = "prql_to_pl", CharSet = CharSet.Ansi)] + private static extern NativeResult PrqlToPlExtern(string prqlQuery); + + [DllImport("prqlc_lib", EntryPoint = "pl_to_rq", CharSet = CharSet.Ansi)] + private static extern NativeResult PlToRqExtern(string plJson); + + [DllImport("prqlc_lib", EntryPoint = "rq_to_sql", CharSet = CharSet.Ansi)] + private static extern NativeResult RqToSqlExtern(string rqJson, ref NativePrqlCompilerOptions options); } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.csproj b/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.csproj index 2bc0d5c9e7c3..cd140020247a 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.csproj +++ b/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompiler.csproj @@ -1,39 +1,17 @@ - - netstandard2.0 - - - - Prql.Compiler - 0.1.0 - PRQL Compiler - PRQL - Copyright ยฉ The PRQL Project 2023 - .NET bindings for the PRQL compiler. PRQL is a modern language for transforming data - Apache-2.0 - https://prql-lang.org/ - README.md - prql;sql - https://github.com/PRQL/prql - - - - True - \ - + + - - PreserveNewest - - - PreserveNewest - - + + false PreserveNewest + true + \runtimes\win-x64\native + %(FileName)%(Extension) diff --git a/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompilerOptions.cs b/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompilerOptions.cs index 4b827e91073f..7c9e35f38dcf 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompilerOptions.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/PrqlCompilerOptions.cs @@ -1,29 +1,29 @@ using System.Runtime.InteropServices; -namespace Prql.Compiler -{ +namespace Prql.Compiler; + +/// +/// Compilation options for SQL backend of the compiler. +/// +[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] +public record struct PrqlCompilerOptions +( /// - /// Compilation options for SQL backend of the compiler. + /// Pass generated SQL string trough a formatter that splits it into + /// multiple lines and prettifies indentation and spacing. /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public class PrqlCompilerOptions - { - /// - /// Pass generated SQL string trough a formatter that splits it into - /// multiple lines and prettifies indentation and spacing. - /// - /// Defaults to true. - public bool Format { get; set; } = true; + /// Defaults to true. + bool Format = true, - /// - /// Target and dialect to compile to. - /// - public string Target { get; set; } + /// + /// Target and dialect to compile to. + /// + string Target = "", - /// - /// Emits the compiler signature as a comment after generated SQL. - /// - /// Defaults to true. - public bool SignatureComment { get; set; } = true; - } -} + /// + /// Emits the compiler signature as a comment after generated SQL. + /// + /// Defaults to true. + bool SignatureComment = true +) +{ } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/Result.cs b/prqlc/bindings/dotnet/PrqlCompiler/Result.cs index 60568751d282..51fd19ed2ec0 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/Result.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/Result.cs @@ -2,37 +2,36 @@ using System.Linq; using System.Runtime.InteropServices; -namespace Prql.Compiler -{ - /// - /// Result of compilation. - /// - public class Result - { - private readonly IReadOnlyCollection _messages; +namespace Prql.Compiler; - internal Result(NativeResult result) - { - Output = result.Output; +/// +/// Result of compilation. +/// +public class Result +{ + private readonly IReadOnlyCollection _messages; - var messages = new List(); + internal Result(NativeResult result) + { + Output = result.Output; - for (var i = 0; i < result.MessagesLen; i++) - { - messages.Add(Marshal.PtrToStructure(result.Messages)); - } + var messages = new List(); - _messages = messages.ToList().AsReadOnly(); + for (var i = 0; i < result.MessagesLen; i++) + { + messages.Add(Marshal.PtrToStructure(result.Messages)); } - /// - /// The compiler output. - /// - public string Output { get; } - - /// - /// Error, warning and lint messages. - /// - public IReadOnlyCollection Messages => _messages; + _messages = messages.ToList().AsReadOnly(); } + + /// + /// The compiler output. + /// + public string Output { get; } + + /// + /// Error, warning and lint messages. + /// + public IReadOnlyCollection Messages => _messages; } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/SourceLocation.cs b/prqlc/bindings/dotnet/PrqlCompiler/SourceLocation.cs index 426c65835def..d3b6d1dd001e 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/SourceLocation.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/SourceLocation.cs @@ -1,31 +1,30 @@ using System.Runtime.InteropServices; -namespace Prql.Compiler +namespace Prql.Compiler; + +/// +/// Location within a source file. +/// +[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] +public struct SourceLocation { /// - /// Location within a source file. + /// Start line. /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct SourceLocation - { - /// - /// Start line. - /// - public int StartLine { get; set; } + public int StartLine { get; set; } - /// - /// Start column. - /// - public int StartCol { get; set; } + /// + /// Start column. + /// + public int StartCol { get; set; } - /// - /// End line. - /// - public int EndLine { get; set; } + /// + /// End line. + /// + public int EndLine { get; set; } - /// - /// End column. - /// - public int EndCol { get; set; } - } + /// + /// End column. + /// + public int EndCol { get; set; } } diff --git a/prqlc/bindings/dotnet/PrqlCompiler/Span.cs b/prqlc/bindings/dotnet/PrqlCompiler/Span.cs index 441347a96390..0bd92cc0b1b7 100644 --- a/prqlc/bindings/dotnet/PrqlCompiler/Span.cs +++ b/prqlc/bindings/dotnet/PrqlCompiler/Span.cs @@ -1,22 +1,21 @@ using System.Runtime.InteropServices; -namespace Prql.Compiler +namespace Prql.Compiler; + +/// +/// Identifier of a location in source. +/// Contains offsets in terms of chars. +/// +[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] +public struct Span { /// - /// Identifier of a location in source. - /// Contains offsets in terms of chars. + /// Start offset. /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct Span - { - /// - /// Start offset. - /// - public int Start { get; set; } + public int Start { get; set; } - /// - /// End offset. - /// - public int End { get; set; } - } + /// + /// End offset. + /// + public int End { get; set; } } diff --git a/prqlc/bindings/dotnet/README.md b/prqlc/bindings/dotnet/README.md index a8ab3998a915..3cba4aa3bc63 100644 --- a/prqlc/bindings/dotnet/README.md +++ b/prqlc/bindings/dotnet/README.md @@ -1,21 +1,42 @@ # prql-dotnet -`prql-net` offers PRQL bindings for .NET bindings as a `netstandard2.0` library. +`PrqlCompiler` offers PRQL bindings for .NET bindings as `net6.0` and `net7.0` +libraries. -It provides the `PrqlCompiler` class which contains the `ToJson` and `ToSql` -static methods. +It provides the `PrqlCompiler` class which contains the `Compile`, `PrqlToPl`, +`PlToRq` and `RqToSql` static methods. It's still at an early stage, and isn't published to NuGet. Contributions are welcome. ## Installation -Make sure that `libprqlc_lib.so` (Linux), `libprqlc_lib.dylib` (macOS) or -`libprqlc_lib.dll` (Windows) is in the project's `bin` directory together with -`PrqlCompiler.dll` and the rest of the project's compiled files. I.e. -`{your_project}/bin/Debug/net7.0/`. +Current project and package only handles Windows native library `prqlc_lib.dll`. +Handling of `libprqlc_lib.so` (Linux), `libprqlc_lib.dylib` (macOS) is work in +progress. -The `libprqlc_lib` library gets dynamically imported at runtime. +A package published on NuGet [Work in progress], includes the .NET bindings and +the native libraries. + +For developers consuming this package, when publishing your application, ensure +that the native library described above is in your project's `bin` directory +together with `PrqlCompiler.dll` (.NET bindings to the native library) and the +rest of your project's compiled files. If it's not the case, ensure that you +specified a runtime parameter when publishing your project. I.e. +`dotnet publish YourProject.csproj --runtime win-x64 --framework net6.0 -o Publish\net6.0-win-x64` +. + +If you're using the package `PrqlCompiler` in a test project, identically, don't +forget to specify the runtime when running your tests or native libraries won't +be copied. I.e. +`dotnet test YourProject.Tests.csproj --runtime win-x64 --framework net6.0` . + +The list of currently supported runtimes is: + +- win-x64 + +The `prqlc_lib` library gets dynamically imported at runtime and is not needed +at compiled time. ## Usage @@ -26,6 +47,7 @@ var options = new PrqlCompilerOptions { Format = false, SignatureComment = false, + Target = "sql.mysql" }; var sql = PrqlCompiler.Compile("from employees", options); Console.WriteLine(sql); @@ -33,6 +55,6 @@ Console.WriteLine(sql); ## TODO -This is currently at 0.1.0 because we're waiting to update prqlc-clib for the -latest API. When we've done that, we can match the version here with the broader -PRQL version. +We're currently including the build, test, package and publication of this +package into the GitHub-actions. When we've done that, we can match the version +here with the broader PRQL version. diff --git a/prqlc/bindings/dotnet/prql-logo.png b/prqlc/bindings/dotnet/prql-logo.png new file mode 100644 index 000000000000..226d9d16c711 Binary files /dev/null and b/prqlc/bindings/dotnet/prql-logo.png differ diff --git a/prqlc/bindings/dotnet/prql-net.sln b/prqlc/bindings/dotnet/prql-net.sln index 76842e4e1189..317451eae514 100644 --- a/prqlc/bindings/dotnet/prql-net.sln +++ b/prqlc/bindings/dotnet/prql-net.sln @@ -3,18 +3,25 @@ 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}") = "PrqlCompiler", "PrqlCompiler\PrqlCompiler.csproj", "{339EA2A6-23D2-4938-884F-052431AC0674}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrqlCompiler", "PrqlCompiler\PrqlCompiler.csproj", "{339EA2A6-23D2-4938-884F-052431AC0674}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrqlCompiler.Tests", "PrqlCompiler.Tests\PrqlCompiler.Tests.csproj", "{78C1AD08-6FF5-444E-9298-385887ABAA80}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrqlCompiler.Tests", "PrqlCompiler.Tests\PrqlCompiler.Tests.csproj", "{78C1AD08-6FF5-444E-9298-385887ABAA80}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution items", "Solution items", "{A5DC0D85-61A8-4E32-B5FC-4196CB406F3C}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + .gitignore = .gitignore + Directory.Build.props = Directory.Build.props + Directory.Build.targets = Directory.Build.targets + README.md = README.md + ..\..\..\.github\workflows\test-dotnet.yaml = ..\..\..\.github\workflows\test-dotnet.yaml + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {339EA2A6-23D2-4938-884F-052431AC0674}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {339EA2A6-23D2-4938-884F-052431AC0674}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -25,4 +32,7 @@ Global {78C1AD08-6FF5-444E-9298-385887ABAA80}.Release|Any CPU.ActiveCfg = Release|Any CPU {78C1AD08-6FF5-444E-9298-385887ABAA80}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection EndGlobal