Skip to content

Instantly share code, notes, and snippets.

@hafuu
Created October 18, 2017 05:38
Show Gist options
  • Save hafuu/822816157a4ab020199b36a54fd511d7 to your computer and use it in GitHub Desktop.
Save hafuu/822816157a4ab020199b36a54fd511d7 to your computer and use it in GitHub Desktop.
プロジェクトファイルにnugetの設定を追加するスクリプト
#r "system.xml.linq"
open System.IO
open System.Xml.Linq
let projectFiles =
[
for dir in Directory.GetDirectories(".") do
yield! Directory.GetFiles(dir, "*.fsproj")
yield! Directory.GetFiles(dir, "*.csproj")
]
let xname (x: string) = XName.Get(x)
let element (name: string) (elem: XElement) =
elem.Elements()
|> Seq.find (fun child -> child.Name.LocalName = name)
let add (xml: string) (elem: XElement) =
let xml = sprintf """<root xmlns="http://schemas.microsoft.com/developer/msbuild/2003">%s</root>""" xml
let toAdd = XElement.Parse(xml)
elem.Add(toAdd.Elements() |> Seq.head)
projectFiles
|> List.iter (fun proj ->
let xml = XDocument.Load(proj)
xml.Root |> element "PropertyGroup" |> add """<RestorePackages>true</RestorePackages>"""
xml.Root |> add """<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />"""
xml.Root |> add """<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>このプロジェクトは、このコンピューターにはない NuGet パッケージを参照しています。これらをダウンロードするには、NuGet パッケージの復元を有効にしてください。詳細については、http://go.microsoft.com/fwlink/?LinkID=322105 を参照してください。不足しているファイルは {0} です。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>"""
xml.Save(proj)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment