using System.Collections.Generic; using System.Linq; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; namespace BMsBuildTasks { public class RollingVersionCleaner:Task { [Required] public string Path { get; set; } [Required] public string AppName { get; set; } [Output] public ITaskItem[ ] Deleted { get; set; } public override bool Execute( ) { if (System.IO.Directory.Exists(Path)==false) { Log.LogError("Directory does not exist:"+Path); return false; } var files = System.IO.Directory.GetFiles(Path, AppName+"*.zip"); var q= from f in files let fileName=System.IO.Path.GetFileName(f) orderby fileName descending where fileName.Contains(".") select f; if (q.Any( )==false) { Log.LogMessage("No files found "+System.IO.Path.Combine(Path, AppName)); return true; } var toDelete=q.Skip(3); if (toDelete.Any( )==false) { Log.LogMessage("No files to delete "+System.IO.Path.Combine(Path, AppName)); return true; } var deleted = new List<string>( ); foreach (var item in toDelete) { System.IO.File.Delete(item); deleted.Add(item); } Deleted=deleted.Select(x => new TaskItem(x)).ToArray( ); Log.LogMessage("Deleted "+deleted.Count+" file(s)"); return true; } } }
When Abstractions Break
2 weeks ago
No comments:
Post a Comment