diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b76071f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,13 @@ +# shell scripts +*.sh text eol=lf + +# linux Pg dist links, libs +pg-dist/pgsql-*-linux-binaries/**/lib*.so[\.\d]* binary +pg-dist/pgsql-*-linux-binaries/bin/** binary +pg-dist/pgsql-*-linux-binaries/lib/** binary +pg-dist/pgsql-*-linux-binaries/share/** binary + +# windows Pg dist links, libs +pg-dist/pgsql-*-windows64-binaries/bin/** binary +pg-dist/pgsql-*-windows64-binaries/lib/** binary +pg-dist/pgsql-*-windows64-binaries/share/** binary \ No newline at end of file diff --git a/.gitignore b/.gitignore index abce8c2..20cfc3a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.user *.userosscache *.sln.docstates +*.vscode # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/GitVersion.yml b/GitVersion.yml index f2b14d8..a989ca0 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -3,6 +3,10 @@ mode: ContinuousDelivery branches: dev(elop)?(ment)?$: mode: ContinuousDeployment + release/*$: + mode: ContinuousDeployment + hotfix/*$: + mode: ContinuousDeployment master: mode: ContinuousDelivery (pull|pull\-requests|pr)[/-]: {} diff --git a/README.md b/README.md index 82e38a7..78f0fe5 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,63 @@ Postgres2Go =========== > Postgres2Go - PostgreSQL for integration tests -## Description -# Installation -# Usage +[![GitHub release][github-release-img]][github-release-url] [![Master build status][appveyor-master-status]][appveyor-project-url] [![Develop build status][appveyor-develop-status]][appveyor-project-url] [![GitHub license][license-img]][license-url] +[![Stable nuget][nuget-img]][nuget-url] [![Downloads][nuget-stats-img]][nuget-url] + +### Description +>Inspired by __[https://github.com/Mongo2Go/Mongo2Go](https://github.com/Mongo2Go/Mongo2Go)__ + +Easily spin up PostgreSQL instances for integration tests. It targets .NET Standard 2.0, .NET 4.6, .NET 4.7. This Nuget package contains the executables of PostgreSQL for Windows, Linux. + +## Installation + +Using __nuget.exe__ and __powershell__: +``` +Install-Package Postgres2Go +``` + +or using __dotnet cli__: +``` +dotnet add package Postgres2Go +``` + +or using __paket.exe__: +``` +paket add Postgres2Go +``` + +## Usage + +Execute static factory method __`PostgresRunner.Start()`__ to setup new instance of disposable `PostgresRunner` and isolated instance of PostgreSQL. + +`PostgresRunner.Start()` accepts parameters: +- `DataDirectory` directory where new instance of PostgreSQL cluster will keep its data; if not provided then special directory will be created inside of `TEMP` folder +- `BinariesSearchPattern` path part where PostgreSQL distribution should be located +- `Port` TCP port used by PostgreSQL instance; if not provided then first free TCP port above 15433 will be used + +To cleanup environment execute method __`Dispose()`__ + +Example usage can be found under __`src/Postgres2Go.Samples`__ ## Credits +Copyright (c) 2017 Johannes Hoppe Copyright (c) 2018 [Skyrise](http://skyrise.tech) Special thanks to all [Contributors](CREDITS.md) - # License This software is distributed under [MIT License](LICENSE.md). -It contains third-party files located in the pg-dist folder that are distributed under [PostgreSQL License](tools/LICENSE.md) \ No newline at end of file +It contains third-party files located in the pg-dist folder that are distributed under [PostgreSQL License](tools/LICENSE.md) + +[appveyor-master-status]: https://ci.appveyor.com/api/projects/status/github/bt-skyrise/Postgres2Go?svg=true&branch=master&passingText=master%20pass&failingText=master%20failed +[appveyor-develop-status]: https://ci.appveyor.com/api/projects/status/github/bt-skyrise/Postgres2Go?svg=true&branch=develop&passingText=develop%20pass&failingText=develop%20failed +[appveyor-project-url]: https://ci.appveyor.com/project/skyrisetech/postgres2go +[github-release-img]: https://img.shields.io/github/release/bt-skyrise/Postgres2Go.svg +[github-release-url]: https://github.com/bt-skyrise/Postgres2Go/releases +[license-img]: https://img.shields.io/badge/License-MIT-green.svg +[license-url]: https://raw.githubusercontent.com/bt-skyrise/Postgres2Go/master/LICENSE.md +[nuget-img]: https://img.shields.io/nuget/v/Postgres2Go.svg?label=stable%20nuget +[nuget-stats-img]: https://img.shields.io/nuget/dt/Postgres2Go.svg?label=downloads +[nuget-url]:https://www.nuget.org/packages/Postgres2Go/ diff --git a/appveyor.yml b/appveyor.yml index 7eed7f1..9eea570 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,6 +14,7 @@ environment: DOTNET_CLI_TELEMETRY_OPTOUT: true VERSION_INFO_SEMVER: null VERSION_INFO: null + VERSION_INFO_NUGET: null nuget: project_feed: true disable_publish_on_pr: true @@ -27,6 +28,8 @@ before_build: $env:VERSION_INFO = $version_info; $env:VERSION_INFO_SEMVER = $version_info.SemVer; + + $env:VERSION_INFO_NUGET = $version_info.NuGetVersion Update-AppveyorBuild -Version "$env:VERSION_INFO_SEMVER-$env:APPVEYOR_BUILD_NUMBER"; @@ -61,3 +64,29 @@ deploy: artifact: NuGet on: branch: master +- provider: BinTray + username: zabrowarnyrafal + api_key: + secure: XjrCjUTd9QwNy4cUUMuFkzKj9yjWIqBc7ry9Qbqosi7y8OY4NogNFGaog6dIj98D + subject: skyrise + repo: Postgres2Go + package: Postgres2Go + version: ${VERSION_INFO_NUGET} + artifact: NuGet + publish: true + override: true + on: + branch: /hotfix\/*/ +- provider: BinTray + username: zabrowarnyrafal + api_key: + secure: XjrCjUTd9QwNy4cUUMuFkzKj9yjWIqBc7ry9Qbqosi7y8OY4NogNFGaog6dIj98D + subject: skyrise + repo: Postgres2Go + package: Postgres2Go + version: ${VERSION_INFO_NUGET} + artifact: NuGet + publish: true + override: true + on: + branch: /release\/*/ diff --git a/pg-dist/pgsql-10.1-linux-binaries/scripts/fix_linux_pg_dist_lib_links.sh b/pg-dist/pgsql-10.1-linux-binaries/scripts/fix_linux_pg_dist_lib_links.sh new file mode 100644 index 0000000..88c33d2 --- /dev/null +++ b/pg-dist/pgsql-10.1-linux-binaries/scripts/fix_linux_pg_dist_lib_links.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +declare -A PG_LIB_LINKS + +PG_LIB_LINKS=( + ["libpq.so"]="libpq.so.5.10" \ + ["libpq.so.5"]="libpq.so.5.10" \ + ["libpgtypes.so"]="libpgtypes.so.3.10" \ + ["libpgtypes.so.3"]="libpgtypes.so.3.10" \ + ["libicuuc.so"]="libicuuc.so.53.1" \ + ["libicuuc.so.53"]="libicuuc.so.53.1" \ + ["libicutu.so"]="libicutu.so.53.1" \ + ["libicutu.so.53"]="libicutu.so.53.1" \ + ["libicutest.so"]="libicutest.so.53.1" \ + ["libicutest.so.53"]="libicutest.so.53.1" \ + ["libiculx.so"]="libiculx.so.53.1" \ + ["libiculx.so.53"]="libiculx.so.53.1" \ + ["libicule.so"]="libicule.so.53.1" \ + ["libicule.so.53"]="libicule.so.53.1" \ + ["libicuio.so"]="libicuio.so.53.1" \ + ["libicuio.so.53"]="libicuio.so.53.1" \ + ["libicui18n.so"]="libicui18n.so.53.1" \ + ["libicui18n.so.53"]="libicui18n.so.53.1" \ + ["libicudata.so"]="libicudata.so.53.1" \ + ["libicudata.so.53"]="libicudata.so.53.1" \ + ["libecpg.so"]="libecpg.so.6.10" \ + ["libecpg.so.6"]="libecpg.so.6.10" \ + ["libecpg_compat.so"]="libecpg_compat.so.3.10" \ + ["libecpg_compat.so.3"]="libecpg_compat.so.3.10" \ + ["libcurl.so"]="libcurl.so.4.4.0" \ + ["libcurl.so.4"]="libcurl.so.4.4.0" \ +) + +PG_BINARIES="$1" + +cd $PG_BINARIES +cd ./../lib + +echo "Changed working_dir to $PWD" + +for link in ${!PG_LIB_LINKS[@]}; +do + if [[ -e $link ]] || [[ -h $link ]] + then + echo "Removing $link" + rm $link + else + echo "Not found $link" + fi + + target="${PG_LIB_LINKS[$link]}" + echo "Create link $link targeting $target" + ln -s $target $link +done diff --git a/src/Postgres2Go.Samples/when_using_as_class_fixture.cs b/src/Postgres2Go.Samples/when_using_as_class_fixture.cs index 2af7d26..3e75d49 100644 --- a/src/Postgres2Go.Samples/when_using_as_class_fixture.cs +++ b/src/Postgres2Go.Samples/when_using_as_class_fixture.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using Npgsql; @@ -13,23 +12,12 @@ public class PgFixture : IDisposable public PgFixture() { - _pgRunner = PostgresRunner - .Start(new PostgresRunnerOptions{ BinariesSearchPattern = GetPgBinariesRelativePath()}); + _pgRunner = PostgresRunner.Start(); } public void Dispose() => _pgRunner?.Dispose(); public string ConnectionString => _pgRunner.GetConnectionString(); - - private string GetPgBinariesRelativePath() - { - if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - return "\\pg-dist\\pgsql-10.1-windows64-binaries\\bin"; - else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - return "/pg-dist/pgsql-10.1-linux-binaries/bin"; - else - throw new NotSupportedException("OSX is not yet supported"); - } } public class when_using_as_class_fixture : IClassFixture diff --git a/src/Postgres2Go.Tests/Helpers/FolderSearch/when_invoke_FindFolderUpwards.cs b/src/Postgres2Go.Tests/Helpers/FolderSearch/when_invoke_FindFolderUpwards.cs new file mode 100644 index 0000000..98ca30e --- /dev/null +++ b/src/Postgres2Go.Tests/Helpers/FolderSearch/when_invoke_FindFolderUpwards.cs @@ -0,0 +1,130 @@ +using System; +using System.IO; +using Xunit; +using Postgres2Go.Helper.FileSystem; +using Postgres2Go.Helper.Postgres; + +namespace Postgres2Go.Tests.Helpers.FolderSearch +{ + public class when_invoke_FindFolderUpwards : IDisposable + { + private readonly Fixture _fixture; + + public when_invoke_FindFolderUpwards() + { + _fixture = new Fixture(); + } + + public void Dispose() + { + _fixture?.Dispose(); + } + + [Theory] + [InlineData("a")] + [InlineData(@"a\b")] + [InlineData(@"a\b\c")] + [InlineData(@"a\b\c\d")] + [InlineData(@"a\b\c\d\e")] + public void should_locate_directory_with_default_search_pattern(string executionPath) + { + // PREPARE + _fixture.CreateSubDirectoryIfNotExists( + Path.Combine(@"packages\Postgres2Go-0.3.0", @"pg-dist\pgsql-10.1-windows64-binaries\bin") + ); + + string executionAbsPath = _fixture + .CreateSubDirectoryIfNotExists(executionPath); + + string searchLocation = Path.Combine( + PostgresBinaryLocator.NugetPackagesDirectoryPrefix, + PostgresBinaryLocator.DefaultWindowsSearchPattern + ); + + // RUN + string foundLocation = executionAbsPath + .FindFolderUpwards(searchLocation); + + // ASSERT + Assert.NotNull(foundLocation); + } + + [Theory] + [InlineData("a", @"packa*\Postgres2*\pg-dist\pgsql-*\bin")] + [InlineData(@"a\b", @"packa*\Postgres2*\pg-dist\pgsql-*\bin")] + [InlineData(@"a\b\c\d\e", @"packa*\Postgres2*\pg-dist\pgsql-*\bin")] + public void should_locate_directory_using_custom_search_pattern(string executionPath, string searchPattern) + { + // PREPARE + _fixture.CreateSubDirectoryIfNotExists( + Path.Combine(@"packages\Postgres2Go-0.3.0", @"pg-dist\pgsql-10.1-windows64-binaries\bin") + ); + + string executionAbsPath = _fixture + .CreateSubDirectoryIfNotExists(executionPath); + + // RUN + string foundLocation = executionAbsPath + .FindFolderUpwards(searchPattern); + + // ASSERT + Assert.NotNull(foundLocation); + } + + [Theory] + [InlineData(@"a\b\c\d\e\f")] + [InlineData(@"a\b\c\d\e\f\g")] + public void cannot_locate_directory_when_nest_level_greater_than_5(string executionPath) + { + // PREPARE + _fixture.CreateSubDirectoryIfNotExists( + Path.Combine(@"packages\Postgres2Go-0.3.0", @"pg-dist\pgsql-10.1-windows64-binaries\bin") + ); + + string executionAbsPath = _fixture + .CreateSubDirectoryIfNotExists(executionPath); + + string searchLocation = Path.Combine( + PostgresBinaryLocator.NugetPackagesDirectoryPrefix, + PostgresBinaryLocator.DefaultWindowsSearchPattern + ); + + // RUN + string foundLocation = executionAbsPath + .FindFolderUpwards(searchLocation); + + // ASSERT + Assert.Null(foundLocation); + + } + + class Fixture : IDisposable + { + internal string Root { get; private set; } + + public Fixture() + { + Root = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + + if (!Directory.Exists(Root)) + Directory.CreateDirectory(Root); + } + + public string CreateSubDirectoryIfNotExists(string path) + { + var subDirPath = Path.Combine(Root, path); + + if (!Directory.Exists(subDirPath)) + Directory.CreateDirectory(subDirPath); + + return subDirPath; + } + + public void Dispose() + { + if (!Directory.Exists(Root)) + Directory.Delete(Root); + } + } + } +} diff --git a/src/Postgres2Go.Tests/Helpers/ProcesOutput/procesoutput_toString.cs b/src/Postgres2Go.Tests/Helpers/ProcesOutput/procesoutput_toString.cs new file mode 100644 index 0000000..ef0121d --- /dev/null +++ b/src/Postgres2Go.Tests/Helpers/ProcesOutput/procesoutput_toString.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using FluentAssertions; +using Xunit; + +namespace Postgres2Go.Tests.Helpers.ProcesOutput +{ + public class procesoutput_toString + { + [Theory] + [InlineData("only stand output",null)] + [InlineData(null,"only err output")] + [InlineData("stand output","err output")] + public void should_return_concatenation_of_outputs(string standardOutput, string errorOutput) + { + // PREPARE + var collectionOfstandardOutputs = new List{"STD OUTPUT:", standardOutput}; + var collectionOfErrorOutputs = new List{"ERR OUTPUT:", errorOutput}; + + var sut = new Helper.Process.ProcessOutput(collectionOfstandardOutputs, collectionOfErrorOutputs, 0); + + // RUN + string wholeFormatedOutput = sut.ToString(); + + // ASSERT + + string expectation = $"{Environment.NewLine}" + + $"==={Environment.NewLine}" + + $"STD OUTPUT:{Environment.NewLine}{standardOutput}{Environment.NewLine}" + + $"{Environment.NewLine}" + + $"ERR OUTPUT:{Environment.NewLine}{errorOutput}{Environment.NewLine}"; + + wholeFormatedOutput.Should().Be(expectation); + + } + } +} diff --git a/src/Postgres2Go.Tests/Postgres2Go.Tests.csproj b/src/Postgres2Go.Tests/Postgres2Go.Tests.csproj index 420afd6..79365fb 100644 --- a/src/Postgres2Go.Tests/Postgres2Go.Tests.csproj +++ b/src/Postgres2Go.Tests/Postgres2Go.Tests.csproj @@ -3,7 +3,8 @@ netcoreapp2.0 - + + diff --git a/src/Postgres2Go.Tests/dummy_test.cs b/src/Postgres2Go.Tests/dummy_test.cs deleted file mode 100644 index 2f8f29e..0000000 --- a/src/Postgres2Go.Tests/dummy_test.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Xunit; - -namespace Postgres2Go.Tests -{ - public class dummy_test - { - [Fact] - public void should_pass() - { - Assert.True(true); - } - } -} diff --git a/src/Postgres2Go.Tests/postgres_binary_locator_tests.cs b/src/Postgres2Go.Tests/postgres_binary_locator_tests.cs new file mode 100644 index 0000000..32d2498 --- /dev/null +++ b/src/Postgres2Go.Tests/postgres_binary_locator_tests.cs @@ -0,0 +1,20 @@ +using Xunit; +using Postgres2Go.Helper.Postgres; +namespace Postgres2Go.Tests +{ + public class postgres_binary_locator_tests + { + [Fact] + public void can_find_postgres_binaries_with_default_settings() + { + //arrange + var postgresBinaryLocator = new PostgresBinaryLocator(null); + + //act + var foundDirectory = postgresBinaryLocator.Directory; + + //assert + Assert.True(!string.IsNullOrEmpty(foundDirectory)); + } + } +} diff --git a/src/Postgres2Go.sln b/src/Postgres2Go.sln index ee84004..5a7ebe0 100644 --- a/src/Postgres2Go.sln +++ b/src/Postgres2Go.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27130.2020 +VisualStudioVersion = 15.0.27130.2027 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Postgres2Go", "Postgres2Go\Postgres2Go.csproj", "{DC273622-0954-432E-A722-8E0D7C78E21B}" EndProject @@ -11,7 +11,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution GCop.json = GCop.json EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Postgres2Go.Samples", "Postgres2Go.Samples\Postgres2Go.Samples.csproj", "{DB99F089-0B3C-4963-A194-6BFAAA619D2E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Postgres2Go.Samples", "Postgres2Go.Samples\Postgres2Go.Samples.csproj", "{DB99F089-0B3C-4963-A194-6BFAAA619D2E}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".build", ".build", "{33268A1B-C8B6-441F-9678-08BD0C6B724C}" + ProjectSection(SolutionItems) = preProject + ..\build.ps1 = ..\build.ps1 + ..\build.sh = ..\build.sh + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".git", ".git", "{30B32B31-7E8A-464F-B3C3-FF9B924D4D84}" + ProjectSection(SolutionItems) = preProject + ..\.gitignore = ..\.gitignore + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Postgres2Go/Common/RecognizedOSPlatform.cs b/src/Postgres2Go/Common/RecognizedOSPlatform.cs index 3039c3d..9492a95 100644 --- a/src/Postgres2Go/Common/RecognizedOSPlatform.cs +++ b/src/Postgres2Go/Common/RecognizedOSPlatform.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; +using System.Runtime.InteropServices; namespace Postgres2Go.Common { @@ -17,6 +14,7 @@ internal static class RecognizedOSPlatform { internal static RecognizedOSPlatformEnum Determine() { +#if NETSTANDARD2_0 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return RecognizedOSPlatformEnum.Windows; @@ -31,6 +29,9 @@ internal static RecognizedOSPlatformEnum Determine() } return RecognizedOSPlatformEnum.Unknown; +#else + return RecognizedOSPlatformEnum.Windows; +#endif } } } diff --git a/src/Postgres2Go/Helper/FileSystem/FolderSearch.cs b/src/Postgres2Go/Helper/FileSystem/FolderSearch.cs index b1314f9..77490c6 100644 --- a/src/Postgres2Go/Helper/FileSystem/FolderSearch.cs +++ b/src/Postgres2Go/Helper/FileSystem/FolderSearch.cs @@ -3,8 +3,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Text; -using System.Text.RegularExpressions; namespace Postgres2Go.Helper.FileSystem { @@ -14,8 +12,22 @@ internal static class FolderSearch internal static string CurrentExecutingDirectory() { - string filePath = new Uri(typeof(FolderSearch).GetTypeInfo().Assembly.CodeBase).LocalPath; - return Path.GetDirectoryName(filePath); + Assembly pg2goAssembly = typeof(FolderSearch).GetTypeInfo().Assembly;; + string pathToPostgres2GoAssembly = null; + + // + // first try read location (abs path) of Postgres2Go assembly + // if framework doesn't implemented it then use code base value to get uri of Postgres2Go assembly + try + { + pathToPostgres2GoAssembly = pg2goAssembly.Location; + } + catch (NotImplementedException) + { + pathToPostgres2GoAssembly = new Uri(pg2goAssembly.EscapedCodeBase).AbsolutePath; + } + + return Path.GetDirectoryName(pathToPostgres2GoAssembly); } internal static string FindFolder(this string startPath, string searchPattern) diff --git a/src/Postgres2Go/Helper/Postgres/Platform/Linux/PostgresLibLinksFixerProcess.cs b/src/Postgres2Go/Helper/Postgres/Platform/Linux/PostgresLibLinksFixerProcess.cs new file mode 100644 index 0000000..cf0e996 --- /dev/null +++ b/src/Postgres2Go/Helper/Postgres/Platform/Linux/PostgresLibLinksFixerProcess.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Linq; +using System.Text; + +namespace Postgres2Go.Helper.Postgres +{ + internal static class PostgresLibLinksFixerProcess + { + internal static void Exec(string binariesDirectory) + { + string pgLibLinksFixingScriptPath = Path.Combine(binariesDirectory, "./../scripts/") + "fix_linux_pg_dist_lib_links.sh"; + + FileSystem.FileSystem + .GrantExecutablePermission(pgLibLinksFixingScriptPath); + + string bashExecutable = "/bin/bash"; + string arguments = $"-c \"{pgLibLinksFixingScriptPath} {binariesDirectory}\""; + + System.Diagnostics.Process serverInitializatorProcess = Process.ProcessController + .CreateProcess(bashExecutable, arguments); + + Process.ProcessOutput output = Process.ProcessController + .StartAndWaitForExit(serverInitializatorProcess, $"Execute fix_linux_pg_dist_lib_links.sh"); + + if(output.ExitCode != 0) + throw new PostgresProcessFinishedWithErrorsException("Cannot prepare Pg lib symbolic links." + output.ToString()); + + Console.Write(output.ToString()); + } + } +} diff --git a/src/Postgres2Go/Helper/Postgres/Platform/Linux/PostgresLinuxSetup.cs b/src/Postgres2Go/Helper/Postgres/Platform/Linux/PostgresLinuxSetup.cs new file mode 100644 index 0000000..426de48 --- /dev/null +++ b/src/Postgres2Go/Helper/Postgres/Platform/Linux/PostgresLinuxSetup.cs @@ -0,0 +1,25 @@ +using System.IO; + +namespace Postgres2Go.Helper.Postgres.Platform.Linux +{ + internal class PostgresLinuxSetup + { + internal void Start(string pgBinDirectoryPath) + { + GrantPostgresExecutablePermission(pgBinDirectoryPath); + PreparePostgresLibrarySymbolicLinks(pgBinDirectoryPath); + } + + private void GrantPostgresExecutablePermission(string pgBinDirectoryPath) + { + FileSystem.FileSystem.GrantExecutablePermission(Path.Combine(pgBinDirectoryPath, PostgresDefaults.ServerControllerExecutable)); + FileSystem.FileSystem.GrantExecutablePermission(Path.Combine(pgBinDirectoryPath, PostgresDefaults.ServerExecutable)); + FileSystem.FileSystem.GrantExecutablePermission(Path.Combine(pgBinDirectoryPath, PostgresDefaults.ServerInitializatorExecutable)); + } + + private void PreparePostgresLibrarySymbolicLinks(string pgBinDirectoryPath) + { + PostgresLibLinksFixerProcess.Exec(pgBinDirectoryPath); + } + } +} diff --git a/src/Postgres2Go/Helper/Postgres/Platform/PostgresPlatformSetup.cs b/src/Postgres2Go/Helper/Postgres/Platform/PostgresPlatformSetup.cs new file mode 100644 index 0000000..2e8571b --- /dev/null +++ b/src/Postgres2Go/Helper/Postgres/Platform/PostgresPlatformSetup.cs @@ -0,0 +1,26 @@ +using Postgres2Go.Common; +using Postgres2Go.Helper.Postgres.Platform.Linux; + +namespace Postgres2Go.Helper.Postgres.Platform +{ + internal class PostgresPlatformSetup + { + internal static void Start(string pgBinDirectoryPath) + { + switch (RecognizedOSPlatform.Determine()) + { + case RecognizedOSPlatformEnum.Linux: + new PostgresLinuxSetup().Start(pgBinDirectoryPath); + break; + case RecognizedOSPlatformEnum.OSX: + throw new UnsupportedPlatformException($"Cannot grant executable right to Postgres binaries when running on OSX platform. OSX platform is not supported."); + case RecognizedOSPlatformEnum.Windows: + default: + break; + + + } + } + + } +} \ No newline at end of file diff --git a/src/Postgres2Go/Helper/Postgres/PostgresBinaries.cs b/src/Postgres2Go/Helper/Postgres/PostgresBinaries.cs deleted file mode 100644 index b072af6..0000000 --- a/src/Postgres2Go/Helper/Postgres/PostgresBinaries.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.IO; -using Postgres2Go.Common; - -namespace Postgres2Go.Helper.Postgres -{ - internal static class PostgresBinaries - { - internal static void AssertCanExecute(string pgBinDirectoryPath) - { - switch (RecognizedOSPlatform.Determine()) - { - case RecognizedOSPlatformEnum.Linux: - FileSystem.FileSystem.GrantExecutablePermission(Path.Combine(pgBinDirectoryPath, PostgresDefaults.ServerControllerExecutable)); - FileSystem.FileSystem.GrantExecutablePermission(Path.Combine(pgBinDirectoryPath, PostgresDefaults.ServerExecutable)); - FileSystem.FileSystem.GrantExecutablePermission(Path.Combine(pgBinDirectoryPath, PostgresDefaults.ServerInitializatorExecutable)); - break; - case RecognizedOSPlatformEnum.OSX: - throw new UnsupportedPlatformException($"Cannot grant executable right to Postgres binaries when running on OSX platform. OSX platform is not supported."); - case RecognizedOSPlatformEnum.Windows: - default: - break; - - - } - } - } -} diff --git a/src/Postgres2Go/Helper/Postgres/PostgresBinaryLocator.cs b/src/Postgres2Go/Helper/Postgres/PostgresBinaryLocator.cs index d4305f1..c4f2d75 100644 --- a/src/Postgres2Go/Helper/Postgres/PostgresBinaryLocator.cs +++ b/src/Postgres2Go/Helper/Postgres/PostgresBinaryLocator.cs @@ -9,10 +9,11 @@ namespace Postgres2Go.Helper.Postgres internal class PostgresBinaryLocator { - internal const string DefaultWindowsSearchPattern = @"pg-dist\pqsql-win64*\bin"; - internal const string DefaultLinuxSearchPattern = "pg-dist/pqsql-linux*/bin"; + internal const string DefaultWindowsSearchPattern = @"pg-dist\pgsql-*-windows64-binaries\bin"; + internal const string DefaultLinuxSearchPattern = "pg-dist/pgsql-*-linux-binaries/bin"; + + internal static string NugetPackagesDirectoryPrefix = Path.Combine("packages", "Postgres2Go*"); - private readonly string _nugetPrefix = Path.Combine("packages", "Postgres2Go*"); private string _binFolder = string.Empty; private readonly string _searchPattern; @@ -26,13 +27,15 @@ public PostgresBinaryLocator(string searchPatternOverride) _searchPattern = DefaultLinuxSearchPattern; break; case RecognizedOSPlatformEnum.OSX: - throw new UnsupportedPlatformException($"Cannot locate Postgres binaries when running on OSX platform. OSX platform is not supported."); - break; + throw new UnsupportedPlatformException("Cannot locate Postgres binaries when running on OSX platform. OSX platform is not supported."); case RecognizedOSPlatformEnum.Windows: _searchPattern = DefaultWindowsSearchPattern; break; default: +#if NETSTANDARD2_0 throw new PostgresBinariesNotFoundException($"Unknow OS:{RuntimeInformation.OSDescription}"); +#endif + throw new PostgresBinariesNotFoundException("Unknow OS. This library is only compatible with Microsoft Windows"); } } @@ -44,14 +47,16 @@ public PostgresBinaryLocator(string searchPatternOverride) internal string Directory => String.IsNullOrEmpty(_binFolder) - ? _binFolder = ResolveBinariesDirectory () + ? _binFolder = ResolveBinariesDirectory() : _binFolder ; private string ResolveBinariesDirectory() { + string searchPatternWithPackagesRootFolder = Path.Combine(NugetPackagesDirectoryPrefix, _searchPattern); + var binariesFolder = - FolderSearch.CurrentExecutingDirectory().FindFolderUpwards(Path.Combine(_nugetPrefix, _searchPattern)) + FolderSearch.CurrentExecutingDirectory().FindFolderUpwards(searchPatternWithPackagesRootFolder) ?? FolderSearch.CurrentExecutingDirectory().FindFolderUpwards(_searchPattern); diff --git a/src/Postgres2Go/Helper/Postgres/PostgresInitializatorProcess.cs b/src/Postgres2Go/Helper/Postgres/PostgresInitializatorProcess.cs index 387abf7..aac432a 100644 --- a/src/Postgres2Go/Helper/Postgres/PostgresInitializatorProcess.cs +++ b/src/Postgres2Go/Helper/Postgres/PostgresInitializatorProcess.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Text; namespace Postgres2Go.Helper.Postgres { @@ -14,10 +15,10 @@ internal static void Exec(string binariesDirectory, string dataDirectory, string .CreateProcess(pgControllerExecutablePath, arguments); Process.ProcessOutput output = Process.ProcessController - .StartAndWaitForExit(serverInitializatorProcess, $"Postgres cluster initializing"); + .StartAndWaitForExit(serverInitializatorProcess, "Postgres cluster initializing"); if(output.ExitCode != 0) - throw new PostgresProcessFinishedWithErrorsException("Cannot initialize Postgres cluster.\n" + System.String.Join("\n",output.ErrorOutput)); + throw new PostgresProcessFinishedWithErrorsException("Cannot initialize Postgres cluster." + output.ToString()); } } } diff --git a/src/Postgres2Go/Helper/Postgres/PostgresStarterProcess.cs b/src/Postgres2Go/Helper/Postgres/PostgresStarterProcess.cs index 3813cda..8d595f0 100644 --- a/src/Postgres2Go/Helper/Postgres/PostgresStarterProcess.cs +++ b/src/Postgres2Go/Helper/Postgres/PostgresStarterProcess.cs @@ -21,7 +21,7 @@ internal static void Exec(string binariesDirectory, string dataDirectory, int po .StartAndWaitForReady(serverStarterProcess, ProcessTimeoutInSeconds, ProcessIdentifier, $"Postgres starting | port: {port}"); if (output.ExitCode != 0) - throw new PostgresProcessFinishedWithErrorsException("Cannot start Postmaster.\n" + String.Join("\n", output.ErrorOutput)); + throw new PostgresProcessFinishedWithErrorsException("Cannot start Postmaster." + output.ToString()); } } } diff --git a/src/Postgres2Go/Helper/Postgres/PostgresStoperProcess.cs b/src/Postgres2Go/Helper/Postgres/PostgresStoperProcess.cs index dce9d69..eca069e 100644 --- a/src/Postgres2Go/Helper/Postgres/PostgresStoperProcess.cs +++ b/src/Postgres2Go/Helper/Postgres/PostgresStoperProcess.cs @@ -17,8 +17,8 @@ internal static void Exec(string binariesDirectory, string dataDirectory) System.Diagnostics.Process serverStopperProcess = Process.ProcessController .CreateProcess(pgControllerExecutablePath, arguments); - ProcessOutput output = Process.ProcessController - .StartAndWaitForReady(serverStopperProcess, ProcessTimeoutInSeconds, ProcessIdentifier, $"Postgres stopping"); + ProcessOutput output = ProcessController + .StartAndWaitForReady(serverStopperProcess, ProcessTimeoutInSeconds, ProcessIdentifier, "Postgres stopping"); } } } diff --git a/src/Postgres2Go/Helper/Process/ProcessOutput.cs b/src/Postgres2Go/Helper/Process/ProcessOutput.cs index cb8e991..325f337 100644 --- a/src/Postgres2Go/Helper/Process/ProcessOutput.cs +++ b/src/Postgres2Go/Helper/Process/ProcessOutput.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.Linq; +using System.Text; namespace Postgres2Go.Helper.Process { @@ -14,5 +16,21 @@ internal ProcessOutput(IEnumerable standardOutput, IEnumerable e internal IEnumerable StandardOutput { get; private set; } internal IEnumerable ErrorOutput { get; private set; } internal int ExitCode{get;private set;} + + internal new string ToString() + { + var sb = new StringBuilder(); + + sb.AppendLine(); + sb.AppendLine("==="); + + StandardOutput.ToList().ForEach(str => sb.AppendLine(str)); + + sb.AppendLine(); + + ErrorOutput.ToList().ForEach(str => sb.AppendLine(str)); + + return sb.ToString(); + } } } diff --git a/src/Postgres2Go/Postgres2Go.csproj b/src/Postgres2Go/Postgres2Go.csproj index 909dd1d..d43159c 100644 --- a/src/Postgres2Go/Postgres2Go.csproj +++ b/src/Postgres2Go/Postgres2Go.csproj @@ -1,12 +1,14 @@ - + - netstandard2.0 + netstandard2.0;net46;net47 PackageReference true true + false $(SemVer) Postgres2Go.nuspec - + false + diff --git a/src/Postgres2Go/PostgresRunner.cs b/src/Postgres2Go/PostgresRunner.cs index cc721cc..762f71a 100644 --- a/src/Postgres2Go/PostgresRunner.cs +++ b/src/Postgres2Go/PostgresRunner.cs @@ -3,6 +3,7 @@ using Postgres2Go.Helper.Net; using Postgres2Go.Helper.Postgres; using Postgres2Go.Helper.FileSystem; +using Postgres2Go.Helper.Postgres.Platform; namespace Postgres2Go { @@ -52,8 +53,8 @@ private PostgresRunner Run() _binDirectory = new PostgresBinaryLocator(_options.BinariesSearchPattern).Directory; - PostgresBinaries - .AssertCanExecute(_binDirectory); + PostgresPlatformSetup + .Start(_binDirectory); PostgresInitializatorProcess .Exec(_binDirectory, _instanceDirectory, PostgresDefaults.User); diff --git a/src/Postgres2Go/Properties/AssemblyInfo.cs b/src/Postgres2Go/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..51553ff --- /dev/null +++ b/src/Postgres2Go/Properties/AssemblyInfo.cs @@ -0,0 +1,12 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Postgres2Go.Tests")] + +[assembly: AssemblyTitle("Postgres2Go")] +[assembly: AssemblyProduct("Postgres2Go")] +[assembly: AssemblyCompany("Skyrise.tech")] + +[assembly: AssemblyVersion("0.3.0.0")] +[assembly: AssemblyInformationalVersion("0.3.0-35-print-whole-pg-output.1+1.Branch.feature/35-print-whole-pg-output.Sha.8453203d506ec354091bd2c9d85695ff5691704b")] +[assembly: AssemblyFileVersion("0.3.0.0")]