Last active
December 20, 2015 08:09
-
-
Save bpatra/6098558 to your computer and use it in GitHub Desktop.
F#-ception, building a powershell script with inlined F# from F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let PSUsableAssemblies = | |
AppDomain.CurrentDomain.GetAssemblies() | |
|> Array.filter (fun asm -> match asm.IsDynamic with | |
| true -> false | |
| false -> File.Exists asm.Location) | |
let FsToPs (codeDOMProviderPath:string) (fsCode:string) moduleName returnedVariableName = | |
let sb = new StringBuilder() | |
sb.AppendFormat("Add-Type -Path {0}",codeDOMProviderPath) |> ignore | |
sb.AppendLine("") |> ignore | |
sb.AppendLine("$provider = New-Object Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider") |> ignore | |
sb.AppendLine("$fsharpCode = @\"") |> ignore | |
sb.Append(fsCode) |> ignore | |
sb.Append("\r\n") |> ignore | |
sb.AppendLine("\"@") |> ignore | |
//we will not need extra assemblies in this example, modify this lambda if necessary | |
let referencedAssemblies = PSUsableAssemblies |> Array.filter (fun asm -> false) | |
match Array.isEmpty referencedAssemblies with | |
| false -> for asm in referencedAssemblies do | |
sb.AppendLine(String.Format("$assemblies += \"{0}\"", asm.Location)) |> ignore | |
sb.AppendLine(@"$fsharpType = Add-Type -TypeDefinition $fsharpCode -CodeDomProvider $provider -ReferencedAssemblies $assemblies") |> ignore | |
| true -> sb.AppendLine(@"$fsharpType = Add-Type -TypeDefinition $fsharpCode -CodeDomProvider $provider") |> ignore | |
sb.AppendLine(String.Format("[{0}]::{1}", moduleName, returnedVariableName)) |> ignore | |
sb.ToString() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment