Skip to content

Instantly share code, notes, and snippets.

@bpatra bpatra/gist:6098558
Last active Dec 20, 2015

Embed
What would you like to do?
F#-ception, building a powershell script with inlined F# from F#
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.