Skip to content

Instantly share code, notes, and snippets.

View MiloszKrajewski's full-sized avatar

Milosz Krajewski MiloszKrajewski

  • Cambridge, UK
View GitHub Profile
@MiloszKrajewski
MiloszKrajewski / ExplainException.cs
Last active March 15, 2023 11:29
Explains exception (including all levels)
public static class ExceptionExtensions
{
public static T Rethrow<T>(this T exception)
where T: Exception
{
ExceptionDispatchInfo.Capture(exception).Throw();
// it never actually gets returned, but allows to do some syntactic trick sometimes
return exception;
}
public static Func<string> Defer(this string message)
{
return () => message ?? "<null>";
}
public static Func<string> Defer(Exception exception)
{
return () => {
if (exception == null)
return "<null>";
curl -L -o 7z.exe http://bit.ly/2o0eAeK
curl -L -o net20-empty.zip http://bit.ly/2y4rWM1
curl -L -o paket.cmd http://bit.ly/2yJ6lWg
curl -L -o fake.cmd http://bit.ly/2nu6TcJ
curl -L -o build.fsx http://bit.ly/2pksTem
curl -L -o protoc.cmd http://bit.ly/2n2h8cI
@MiloszKrajewski
MiloszKrajewski / ProcessHelper.fs
Last active December 6, 2017 08:37
Simple process helper
#r "System.Management.dll"
module Process =
open System
open System.Diagnostics
open System.Threading.Tasks
open System.Management
let fix (proc: Process option) =
match proc with
rem This must be executed as administrator
rem it requires double % if ran as batch file, but single % if copying-pasting lines
assoc .ps1=PowerShell.Script
ftype PowerShell.Script=powershell.exe -ExecutionPolicy RemoteSigned -File %%1 %%*
setx PATHEXT %PATHEXT%;.PS1
rem This is just a test and may require clicking a checkbox (but only first time)
echo Write-Host 'Hello from PS1' > %temp%\test.ps1
private static IObservable<T> Observe<T>(
this NetMQSocket socket,
ISocketPollableCollection poller,
Func<NetMQSocket, T> reader)
{
IEnumerable<T> ReadMany(NetMQSocket sckt)
{
while (sckt.HasIn)
yield return reader(sckt);
}
private static IObservable<string> ObserveConsole() =>
Observable.Create<string>(
observer => {
var input = new StreamReader(System.Console.OpenStandardInput());
return Observable
.FromAsync(() => input.ReadLineAsync())
.Repeat()
.TakeWhile(line => line != null)
.Subscribe(observer!);
}).Publish().RefCount();
@MiloszKrajewski
MiloszKrajewski / protoc.ps1
Last active June 3, 2021 12:27
protoc stub with PowerShell
Param(
[string] $root = "."
)
$protoc_folder = "$PSScriptRoot/.tools/protoc"
$protoc_executable = "$protoc_folder/bin/protoc.exe"
$protoc_includes = "$protoc_folder/include"
$protoc_version = "3.6.0"
$protoc_url = "https://github.com/google/protobuf/releases/download/v$protoc_version/protoc-$protoc_version-win32.zip"
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip([string] $zipfile, [string] $outpath) {
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
function Download([string] $url, [string] $path) {
$temp = [System.IO.Path]::GetTempFileName()
Invoke-WebRequest "$url" -OutFile "$temp"
Unzip "$temp" "$path"
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
<RootNamespace>Proto.Persistence.MySql</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<Version>0.1.14</Version>
<AssemblyVersion>0.1.14</AssemblyVersion>
<FileVersion>0.1.14</FileVersion>