|
using Extensibility;
using EnvDTE;
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace WebReports {
/// <summary>Report the newest files in the current Project.</summary>
/// <remarks>This kind of report exists in Frontapge, but not in Visual Studio,
/// and is useful for reviewing the work of the last days.</remarks>
public class ReportNewest : Report {
// these paramters may be changed by a future Options page.
private int topParam = 50; // default number of files to be reported.
private DateTime since = DateTime.MinValue; // report files newer than this datetime
private int found = 0; // number of found files.
private DateTime oldest = DateTime.MinValue; // date-watermark: older files need not to be included into the list
private string [] fNames; // list of filenames
private DateTime [] fDates; // list of last Write Date
/// <summary>Construtor, that initializes the private analysis data.</summary>
public ReportNewest() {
found = 0; // number of found files.
oldest = DateTime.MinValue; // oldest date of any entry in fNames
fNames = new string[topParam];
fDates = new DateTime[topParam];
} // ReportNewest
public override int Arguments {
get { return (1); }
} // Arguments
/// <summary>is set to true, when this report starts on a folder basis.</summary>
public override bool FolderParameter {
get { return (true); }
}
/// <summary>Use the root of the current Project as the default argument.</summary>
public override string DefaultArgument(_DTE applicationObject) {
return(Report.CurrentProjectFolder(applicationObject));
} // DefaultArgument
/// <summary>
/// Do the analysis by walking through the current project and send html to the output.
/// </summary>
/// <param name="applicationObject">reference to the VS.NET</param>
/// <param name="output">ouput stream</param>
public override void Render(_DTE applicationObject, StringWriter output, params string[] args) {
string path = args[0];
// do the work
Walk(path);
// sort all data
Array.Sort(fDates, fNames, 0, found);
string html = ExtractHtmlResource("WebReports.WebReports.ReportNewest.htm");
// write the starting html
output.Write(html.Substring(0, html.IndexOf("[RESULT]")));
// write the result to output table
for (int n = found-1; n >= 0; n--) {
output.Write("<tr><td nowrap><a href='DTE:file?file=" + System.Web.HttpUtility.UrlEncode(fNames[n]) + "'>" + System.Web.HttpUtility.HtmlEncode(fNames[n]) + "</a></td>");
output.Write("<td>" + fDates[n].ToString() + "</td>");
output.WriteLine("<td><a href='dte:backup?file=" + System.Web.HttpUtility.UrlEncode(fNames[n]) + "'>backup</a></td></tr>");
} // for
// write the end html
output.Write(html.Substring(html.IndexOf("[RESULT]")+8));
} // Render
/// <summary>Analyze all sub-folder by recursion and all files of a folder.
/// Do not analyse any folder starting with a undeline character.
/// </summary>
/// <param name="aFolder">Full name of the folder.</param>
private void Walk(string aFolder) {
string [] folders = Directory.GetDirectories (aFolder);
for (int i = 0 ; i < folders.Length ; i++ ) {
string aName = folders[i];
int n = aName.LastIndexOf ('\\');
if ((n+1 < aName.Length) && (aName[n+1] != '_' ))
Walk(aName);
} // for
string [] files = Directory.GetFiles ( aFolder );
for (int i = 0 ; i < files.Length ; i++ ) {
string aName = files[i];
DateTime aDate = File.GetLastWriteTime(aName);
if (aDate < since) {
// forget about that file
} else if (found == 0) {
// first found file
oldest = aDate;
fNames[found] = aName;
fDates[found] = aDate;
found++;
} else if (found < topParam) {
// still space available
if (aDate < oldest)
oldest = aDate;
fNames[found] = aName;
fDates[found] = aDate;
found++;
} else if (aDate > oldest) {
Array.Sort(fDates, fNames, 0, found);
// remove oldest file and add current file.
fDates[0] = aDate;
fNames[0] = aName;
oldest = aDate;
if (fDates[1] < oldest)
oldest = fDates[1];
} // if
} // for
} // Walk
} // class
} // namespace
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here