// PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as // procedural languages (PL) // // // Copyright (c) 2023 Brick Abode // // This code is subject to the terms of the PostgreSQL License. // The full text of the license can be found in the LICENSE file // at the top level of the pldotnet repository. // // DotNetProjectBuilder.cs - pldotnet F# project builder using dotnet // using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text.RegularExpressions; using Npgsql; using NpgsqlTypes; using PlDotNET.Common; namespace PlDotNET { /// /// Represents a builder for creating a .NET project. /// internal partial class DotNetProjectBuilder { /// /// The verbose level for logging detailed information during the build process. /// internal int Verbose = 0; /// /// The path to the destination project. /// internal string DestinationProjectPath = string.Empty; /// /// The path to the project template used as a basis for the new project. /// internal string ProjectTemplatePath; /// /// The root destination path where the project directory will be created. /// internal string DestinationPath = @"./tmp"; /// /// The name of the project to be created. /// internal string ProjectName; /// /// The programming language used for the project. /// internal DotNETLanguage Language; /// /// The target framework for the project, determined at compile time. /// internal string TargetFramework; /// /// Initializes a new instance of the class. /// /// The path to the project template. /// The path where the project will be created. /// The name of the project. /// The programming language of the project. public DotNetProjectBuilder(string projectTemplatePath, string destinationPath, string projectName, DotNETLanguage language) { this.ProjectTemplatePath = projectTemplatePath; this.DestinationPath = destinationPath; this.ProjectName = projectName; this.Language = language; #if NET6_0 this.TargetFramework = "net6.0"; #elif NET7_0 this.TargetFramework = "net7.0"; #elif NET8_0 this.TargetFramework = "net8.0"; #elif NET9_0 this.TargetFramework = "net9.0"; #else throw new NotSupportedException("Unsupported target framework. Please ensure the project is targeting a supported version."); #endif } /// /// Sets the verbose level for the project builder. /// /// The verbose level to set. public void SetVerboseLevel(int v) { this.Verbose = v; } /// /// Sets the content of the program file in the project. /// /// The new content to be written to the program file. /// Returns 0 if the file is successfully written, otherwise returns 1. public int SetProgramContent(string newContent) { string formatFile = Language == DotNETLanguage.CSharp ? "cs" : "fs"; if (this.Verbose > 0) { Elog.Info($"Calling SetProgramContent: saving file in {this.DestinationProjectPath}/Program.{formatFile}"); } try { File.WriteAllText($"{this.DestinationProjectPath}/Program.{formatFile}", newContent); return 0; } catch (Exception e) { Elog.Info(e.ToString()); return 1; } } /// /// Builds the project with the specified new content. /// /// The new content to replace the placeholder program file. /// The result of the build operation. public int Build(string newContent) { if (this.Verbose > 0) { Elog.Info("Calling Build()"); } // Create a new directory at the destination path DestinationProjectPath = Path.Combine(DestinationPath, ProjectName); if (!Directory.Exists(DestinationProjectPath)) { if (this.Verbose > 0) { Elog.Info("Creating new directory for template project"); } Directory.CreateDirectory(DestinationProjectPath); } else if (this.Verbose > 0) { Elog.Warning($"{DestinationProjectPath} already exists!"); } if (this.Verbose > 0) { Elog.Info("Searching for the project template file"); } if (!File.Exists(this.ProjectTemplatePath)) { throw new SystemException($"Template file '{this.ProjectTemplatePath}' not found"); } // Read the project template file and replace the placeholders with the actual DLL paths string templateProject = File.ReadAllText(this.ProjectTemplatePath); templateProject = templateProject.Replace( "$PlDotNET.Common.dll$", typeof(Elog).Assembly.Location.Replace("/", "\\")); templateProject = templateProject.Replace( "$NpgsqlTypes.dll$", typeof(NpgsqlPoint).Assembly.Location.Replace("/", "\\")); templateProject = templateProject.Replace( "$Npgsql.dll$", typeof(NpgsqlCommand).Assembly.Location.Replace("/", "\\")); templateProject = templateProject.Replace( "$TargetFramework$", this.TargetFramework); // Save the project file to the destination path string formatFile = Language == DotNETLanguage.CSharp ? "csproj" : "fsproj"; File.WriteAllText( $"{DestinationProjectPath}/{ProjectName}.{formatFile}", templateProject); if (Verbose > 2) { // Assuming Test is your Folder DirectoryInfo d = new(DestinationProjectPath); // Getting Text files FileInfo[] files = d.GetFiles("*"); var fl = new List(); foreach (FileInfo f in files) { fl.Add(f.Name); } Elog.Info($"New project created from template with these files: {string.Join(", ", fl)}"); } // Replace the placeholder program file with the UserFunction code SetProgramContent(newContent); // Build the project using dotnet build return BuildProject(DestinationProjectPath); } /// /// Builds the project and generates a DLL file. /// /// The new content to be built. /// The path of the generated DLL file. public string BuildAndGenDLL(string newContent) { if (this.Verbose > 0) { Elog.Info("Calling BuildAndGenDLL()"); } int error = this.Build(newContent); if (error != 0) { return string.Empty; } // Construct the release path using the defined target framework string releasePath = Path.GetFullPath(Path.Combine(this.DestinationProjectPath, "bin", "Release", this.TargetFramework)); return Path.Combine(releasePath, $"{this.ProjectName}.dll"); } /// /// Builds the specified project using the dotnet build command. /// /// The path to the project file. /// An integer indicating the success or failure of the build process. public int BuildProject(string projectPath) { int retVal = 0; try { // Run dotnet build command ProcessStartInfo psi = new() { // FileName = "dotnet", FileName = "/usr/bin/dotnet", Arguments = $"build {projectPath} --configuration Release", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, }; psi.EnvironmentVariables["DOTNET_CLI_HOME"] = DestinationPath; using Process process = new() { StartInfo = psi }; process.Start(); process.WaitForExit(); // Read the output and error streams string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); if (Verbose > 1) { Elog.Info("Build Output:"); Elog.Info(output); } else if (output.Contains("build failed", StringComparison.CurrentCultureIgnoreCase) || !string.IsNullOrEmpty(error)) { var errorLines = string.IsNullOrEmpty(error) ? output.Split('\n') .Select(line => MyRegex().Match(line)) .Where(match => match.Success) .Select(match => match.Value) .Distinct() .ToArray() : error.Split('\n'); string language = Language == DotNETLanguage.CSharp ? "C#" : "F#"; string code = File.ReadAllText( $"{this.DestinationProjectPath}/Program.{(Language == DotNETLanguage.CSharp ? "cs" : "fs")}"); Elog.Warning( $"PL.NET could not compile the following {language} generated code:" + $"\n**********\n{code}\n**********\n" + $"Here are the compilation results:\n{string.Join("\n", errorLines)}\n\n" + $"For additional information, please consult the {language} project created at '{this.DestinationProjectPath}'\n"); retVal = 1; } if (Verbose > 0) { Elog.Info("Build completed."); } this.DestinationProjectPath = projectPath; } catch (Exception ex) { Elog.Info($"Error: {ex.Message}"); retVal = 1; } return retVal; } [GeneratedRegex(@"\((\d+,\d+)\): error (\w+): ([^\[]+)")] private static partial Regex MyRegex(); } }