// Copyright (c) 2007 Omer Rauchwerger (a.k.a rauchy) (omer@rauchy.net) // All rights reserved. // // This file is part of Regionerate. // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 3 of the License, // or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System.Collections.Generic; using System.Globalization; using NAnt.Core; using NAnt.Core.Attributes; using NAnt.Core.Types; using Rauchy.Regionerate.ServiceLayer.Components.Services; using Rauchy.Regionerate.Shared.Components; namespace Rauchy.Regionerate.Presentation.Addins.NAnt { /// /// An NAnt which Regionerates files, projects and solutions. /// [ TaskName( "rgn" ) ] public class RegionerateTask : Task { #region Fields (6)  private FileSet _ignoredFiles; private FileSet _ignoredProjects; private string _layout; private readonly LogWriter _logWriter; private FileSet _projects; private FileSet _solutions; #endregion Fields  #region Constructors (1)  /// /// Initializes a new instance of the class. /// public RegionerateTask() { _logWriter = new LogWriter( this, Level.Info, CultureInfo.InvariantCulture ); } #endregion Constructors  #region Properties (7)  /// /// Gets or sets the files within projects that will not be Regionerated. /// [ BuildElement( "ignoredFiles", Required = false ) ] public FileSet IgnoredFiles { get { return _ignoredFiles; } set { _ignoredFiles = value; } } /// /// Gets or sets the projects within solutions that will not be Regionerated. /// [ BuildElement( "ignoredProjects", Required = false ) ] public FileSet IgnoredProjects { get { return _ignoredProjects; } set { _ignoredProjects = value; } } /// /// Gets or sets the Code Layout to Regionerate by. /// [ TaskAttribute( "layout", Required = false ) ] public string Layout { get { return _layout; } set { _layout = value; } } /// /// Gets or sets a value indicating whether information should be prompted or not. /// /// true if [no info]; otherwise, false. [ TaskAttribute( "noInfo", Required = false ) ] public bool NoInfo { get; set; } /// /// Gets or sets a value indicating whether warnings should be prompted or not. /// /// true if [no warn]; otherwise, false. [ TaskAttribute( "noWarn", Required = false ) ] public bool NoWarn { get; set; } /// /// Gets or sets the projects which will be Regionerated. /// [ BuildElement( "projects", Required = false ) ] public FileSet Projects { get { return _projects; } set { _projects = value; } } /// /// Gets or sets the solutions which will be Regionerated. /// [ BuildElement( "solutions", Required = false ) ] public FileSet Solutions { get { return _solutions; } set { _solutions = value; } } #endregion Properties  #region Methods (6)  // Protected Methods (1)  // [rgn] Protected Methods (1) /// /// Executes the task. /// protected override void ExecuteTask() { // Register for information only if NoInfo is not set. if ( !NoInfo ) { Output.OnInformationSubmitted += Output_OnInformationSubmitted; } // Always register to warnings. (if NoWarn is set, NonCritical warnings will not be displayed) Output.OnWarningSubmitted += Output_OnWarningSubmitted; string[] ignoredProjects = GetIncludes( _ignoredProjects ); string[] ignoredFiles = GetIncludes( _ignoredFiles ); #region Handle Solutions string[] includedSolutions = GetIncludes( _solutions ); if ( includedSolutions.Length > 0 ) { SolutionService service = GetSolutionService( ignoredProjects, ignoredFiles, _layout ); foreach ( string solution in includedSolutions ) { service.Process( solution ); } } #endregion #region Handle Projects string[] includedProjects = GetIncludes( _projects ); if ( includedProjects.Length > 0 ) { ProjectService service = GetProjectService( ignoredFiles, _layout ); foreach ( string project in includedProjects ) { service.Process( project ); } } #endregion } // Private Methods (5)  // [rgn] Private Methods (5) /// /// Gets the included files in a . /// /// The file set. private static string[] GetIncludes( FileSet fileSet ) { if ( fileSet == null ) { return new string[] {}; } List includes = new List(); foreach ( string include in fileSet.Includes ) { includes.Add( include ); } return includes.ToArray(); } /// /// Instanciates the correct . /// /// The ignored files. /// The Code Layout path. /// private static ProjectService GetProjectService( string[] ignoredFiles, string layout ) { ProjectService service; if ( layout == null ) { // Use the default Code Layout service = new CSharpProjectService( ignoredFiles ); } else { service = new CSharpProjectService( layout, ignoredFiles ); } return service; } /// /// Instanciates the correct . /// /// The ignored files. /// The ignored projects. /// The Code Layout path. /// private static SolutionService GetSolutionService( string[] ignoredProjects, string[] ignoredFiles, string layout ) { SolutionService service; if ( layout == null ) { // Use the default Code Layout service = new SolutionService( ignoredProjects, ignoredFiles ); } else { service = new SolutionService( layout, ignoredProjects, ignoredFiles ); } return service; } private void Output_OnInformationSubmitted( string text ) { string formattedText = string.Format( "Information: {0}.", text ); _logWriter.WriteLine( formattedText ); } private void Output_OnWarningSubmitted( string text, Output.WarningLevel level ) { if ( ( NoWarn ) && ( level == Output.WarningLevel.NonCritical ) ) { return; } string formattedText = string.Format( "{0} warning: {1}.", level, text ); _logWriter.WriteLine( formattedText ); } #endregion Methods  } }