Skip to content

Instantly share code, notes, and snippets.

@ctaggart
Created June 10, 2014 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctaggart/b8412f13165d6b3f7ba2 to your computer and use it in GitHub Desktop.
Save ctaggart/b8412f13165d6b3f7ba2 to your computer and use it in GitHub Desktop.
Uses SLNTools & MSBuild to remove unnecessary Solution Configurations
module RemoveConfigs.App
open System
open System.Collections.Generic
open System.Text.RegularExpressions
// reference Microsoft.Build that requires Microsoft.Build.Framework
open Microsoft.Build.Construction
open Microsoft.Build.Evaluation
// nuget CWDev.SLNTools.Core
open CWDev.SLNTools.Core
let splitPipe (s:string) =
let i = s.IndexOf '|'
s.Substring(0, i), s.Substring(i+1)
let guidCSharp = Guid "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
type Project with
member x.IsCSharp with get() = (Guid x.ProjectTypeGuid).Equals guidCSharp
type PropertyLine with
member x.NameFst = x.Name |> splitPipe |> fst
member x.NameSnd = x.Name |> splitPipe |> snd
// only keep these configs
let validConfigs = HashSet(["Debug";"Release"], StringComparer.OrdinalIgnoreCase)
let removeSlnConfigs (sf:SolutionFile) =
// remove from GlobalSection(SolutionConfigurationPlatforms)
let scps = sf.GlobalSections.["SolutionConfigurationPlatforms"]
let removes = HashSet()
scps.PropertyLines
|> Seq.filter (fun pl -> validConfigs.Contains pl.NameFst = false)
|> Seq.iter (fun pl -> removes.Add pl |> ignore)
removes |> Seq.iter (fun pl -> scps.PropertyLines.Remove pl |> ignore)
// remove from GlobalSection(ProjectConfigurationPlatforms)
sf.Projects |> Seq.iter (fun p ->
let removes = HashSet()
p.ProjectConfigurationPlatformsLines
|> Seq.filter (fun pl -> validConfigs.Contains pl.NameFst = false)
|> Seq.iter (fun pl -> removes.Add pl |> ignore)
removes |> Seq.iter (fun pl -> p.ProjectConfigurationPlatformsLines.Remove pl |> ignore)
)
sf.Save()
let parseConfigPlatform s =
let re = Regex(@"\'\$\(Configuration\)\|\$\(Platform\)\'\ \=\=\ \'(\S+)\|(\S+)\'", RegexOptions.IgnoreCase)
let m = re.Match s
if m.Success then
Some (m.Groups.[1].Value, m.Groups.[2].Value)
else None
let removeProjConfigs f =
use pc = new ProjectCollection()
let p = ProjectRootElement.Open(f, pc)
p.PropertyGroups
|> Seq.filter (fun g ->
match parseConfigPlatform g.Condition with
| None -> false
| Some (config,_) -> validConfigs.Contains config = false
)
|> Seq.iter p.RemoveChild
p.Save()
let removeSlowCheetah f =
use pc = new ProjectCollection()
let p = ProjectRootElement.Open(f, pc)
p.PropertyGroups
|> Seq.filter (fun g ->
if g.Label = "SlowCheetah" then true
else if g.Properties.Count = 1 && (Seq.head g.Properties).Name = "SlowCheetahTargets" then true
else false
)
|> Seq.iter p.RemoveChild
p.Save()
let removeConfigs f =
let sf = SolutionFile.FromFile f
sf.Projects
|> Seq.filter (fun p -> p.IsCSharp)
|> Seq.map (fun p -> p.FullPath)
|> Seq.iter (fun f -> removeProjConfigs f; removeSlowCheetah f)
removeSlnConfigs sf
[<EntryPoint>]
let main argv =
for f in IO.Directory.EnumerateFiles(@"C:\Projects", "*.sln", IO.SearchOption.AllDirectories) do
removeConfigs f
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment