《powershell 的版本所引起的載入 FSharp 編譯器問題》基本解決

來源:互聯網
上載者:User

標籤:des   style   http   color   使用   os   io   strong   

《powershell 的版本所引起的載入 FSharp 編譯器問題》基本解決


1、FSharp.Core.dll,不光要 Add-Type,還要在編譯中引用。但是,在 VS2012 的環境下,就不需要引用,百思不得其解;

2、理順了大部分系統引用的地址。


Function Compiler-FSharp([string]$fSharpCode,[Array]$References)
{
    # 判斷 Powershell的版本
    $isAMD64= ($env:PROCESSOR_ARCHITECTURE).Contains("AMD64" )
    $is32= $PSHOME.Contains("SysWOW64")
    $ra= New-Object Collections.ArrayList

    # 增加 Path for FSC.exe
    $FscPath= @("C:\ProgramFiles\Microsoft SDKs\F#\2.0\Framework\v4.0";
                 "C:\ProgramFiles\Microsoft SDKs\F#\3.0\Framework\v4.0";
                 "C:\ProgramFiles\Microsoft SDKs\F#\3.1\Framework\v4.0";
                 "C:\ProgramFiles\Microsoft SDKs\F#\4.0\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\4.0\Framework\v4.0";
                 )
    $FscPath| % {if (Test-Path$_) { $env:Path = $env:Path+ ";" + $_  } }

    if ($isAMD64)
    {
        $FSharpCorePath= @(
            "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll"
        )

        if ($is32)
        {
           $mscorlibPath= @(
                "C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll"
            )
        }
        else
        {
            $mscorlibPath= @(
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll"
            )
        }
    }
    else
    {
        if ($is32)
        {
            $FSharpCorePath= @(
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll"
                )
            $mscorlibPath= @(
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll";
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client5\mscorlib.dll"

            )

        }
        else
        {
            $FSharpCorePath= @(
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\FSharp\3.0\Runtime\v4.0\FSharp.Core.dll";
                "C:\ProgramFiles\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll";
                )

            $mscorlibPath= @(           
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v3.0\mscorlib.dll"
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v3.5\mscorlib.dll"
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll"
                "C:\ProgramFiles\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll"
            )
       }
    }


    $FSharpCorePath| % { if(Test-Path($_ )) { Add-Type -Path$_ } }
    $ra.AddRange( @($mscorlibPath  | ? { Test-Path($_ ) } | Select-Object -First1) )
    $ra.AddRange( @($FSharpCorePath | ? {Test-Path($_) } | Select-Object -First1 ) )


    if ($references.Count-ge1)
    {
        $ra.AddRange($References)
        $references|  % { if(Test-Path($_ )) { Add-Type -Path$_} else{Add-Type -AssemblyName$_} }
    }
    # 載入 FSharp.Compiler.CodeDom.dll
    Add-Type -Path C:\FSharpPowerPack-4.0.0.0\bin\FSharp.Compiler.CodeDom.dll
    $provider= New-ObjectMicrosoft.FSharp.Compiler.CodeDom.FSharpCodeProvider
    <#
    如果要使用-CompilerParameters
    與-ReferencedAssemblies 不能同時使用
    $cp= New-ObjectSystem.CodeDom.Compiler.CompilerParameters
    $cp.CompilerOptions= "--platform:x86"
    #>
    $fSharpType= Add-Type-TypeDefinition$fSharpCode -CodeDomProvider$provider -ReferencedAssemblies$ra -ErrorActionIgnore -PassThru | where{ $_.IsPublic}
    $fSharpType
}


============================

######################
# FSharp.Data
######################


$fSharpCode = Get-Content"$env:USERPROFILE\Desktop\Data.fs" |Out-String
$references = @("C:\FSharp.Data\bin\FSharp.Data.dll")

Compiler-FSharp $fSharpCode $references

[Test]::http("http://www.baidu.com").Body.item

============================

Data.fs:

module Test

open FSharp.Data
//open FSharp.Net
// FSharp.Data 新版本中所有的函數全部歸到 Data 命名空間下了。

let http (url:string) = Http.Request url


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.