Created
September 4, 2024 15:59
-
-
Save dburriss/d2e58f2ebeaadc2a4ba0dceffc2433aa to your computer and use it in GitHub Desktop.
Change the target framework of a csproj, fsproj, or vbproj file
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
open System | |
open System.Xml.Linq | |
let targetVersion = fsi.CommandLineArgs |> Array.tryItem 1 |> Option.defaultValue "net6.0" | |
let xsproj = fsi.CommandLineArgs |> Array.tryItem 2 | |
let isProjFile (filename: string) = | |
match (IO.Path.GetExtension(filename)).ToLower() with | |
| ".csproj" | ".fsproj" | ".vbproj" -> true | |
| _ -> false | |
let tryFindProj () = | |
let curDir = Environment.CurrentDirectory | |
printfn "Finding proj files in %s" curDir | |
IO.Directory.GetFiles(curDir) | |
|> Array.tryFind isProjFile | |
let loadFile (path:string) = | |
XDocument.Load(path) | |
let changeTargetFramework (newTargetFramework: string) (projectFile: XDocument) = | |
// Find the <TargetFramework> element | |
let targetFrameworkElement = | |
projectFile.Descendants() | |
|> Seq.filter (fun elem -> elem.Name.LocalName = "TargetFramework") | |
|> Seq.head | |
// Update the value of the <TargetFramework> element | |
targetFrameworkElement.SetValue(newTargetFramework) | |
projectFile | |
let saveDoc (path:string) (projectFile: XDocument) = | |
// Save the modified project file | |
projectFile.Save(path) | |
let swapTargetFramework target projPath = | |
loadFile projPath | |
|> changeTargetFramework target | |
|> saveDoc projPath | |
let dotvto targetVersion optFile = | |
optFile | |
|> Option.orElseWith tryFindProj | |
|> Option.map (swapTargetFramework targetVersion) | |
|> function | |
| Some _ -> printfn "TargetFramework set to %s" targetVersion | |
| None -> printfn "No proj file found" | |
dotvto targetVersion None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment