FileSetFunctions.cs :  » Build-Systems » NAntContrib » NAnt » Contrib » Functions » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » NAntContrib 
NAntContrib » NAnt » Contrib » Functions » FileSetFunctions.cs
// NAnt - A .NET build tool
// Copyright (C) 2001-2004 Gerry Shaw
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Bill Martin

using System;
using System.Text;

using NAnt.Core;
using NAnt.Core.Types;
using NAnt.Core.Attributes;

namespace NAnt.Contrib.Functions{
    /// <summary>
    /// Provides methods for interrogating Filesets.
    /// </summary>
    [FunctionSet("fileset", "FileSet")]
    public class FileSetFunctions : FunctionSetBase {
        #region Public Instance Constructors

        public FileSetFunctions(Project project, PropertyDictionary properties) : base(project, properties) { }

        #endregion Public Instance Constructors

        #region Public Instance Methods

        /// <summary>
        /// Determines the number of files within a <see cref="FileSet"/>.
        /// </summary>
        /// <param name="fileset">The id of the FileSet to scan.</param>
        /// <returns>The number of files included in the FileSet</returns>
        /// <exception cref="ArgumentException"><paramref name="fileset" /> is not a valid refid to a defined fileset.</exception>
        /// <example>
        ///   <para>
        ///   Define a fileset and check the number of files in it.
        ///   </para>
        ///   <code>
        ///     <![CDATA[
        /// <fileset id="test.fileset">
        ///     <include name="**/*.cs">
        /// </fileset>
        /// <echo message="FileSet contains ${fileset::get-file-count('test.fileset')} files." />
        ///     ]]>
        ///   </code>
        /// </example>
        [Function("get-file-count")]
        public int GetFileCount(string fileset) {
            //Try to retrieve the specified fileset from the Data References on the project
            FileSet fs = base.Project.DataTypeReferences[fileset] as FileSet;
            //If the value was missing or the referenced type was not a fileset, then throw an exception
            if (fs == null) {
                throw new ArgumentException(string.Format("'{0}' is not a valid fileset reference", fileset));
            }
            //Otherwise return the number of files in the fileset.
            return fs.FileNames.Count;
        }

        /// <summary>
        /// Determines whether <see cref="FileSet"/> contains any files.
        /// </summary>
        /// <param name="fileset">The id of the fileset to check.</param>
        /// <returns><see langword="true" /> if the FileSet contains one or more files, otherwise <see langword="false"/>.</returns>
        /// <exception cref="ArgumentException"><paramref name="fileset" /> is not a valid refid to a defined fileset.</exception>
        /// <example>
        ///   <para>
        ///   Perform conditional processing on a fileset if it contains files.
        ///   </para>
        ///   <code>
        ///     <![CDATA[
        /// <fileset id="test.fileset">
        ///     <include name="**/*.cs">
        /// </fileset>
        /// <if test="${fileset::has-files('test.fileset')}">
        ///     <dostuff... />
        /// </if>
        ///     ]]>
        ///   </code>
        /// </example>
        [Function("has-files")]
        public bool HasFiles(string fileset) {
            //Try to retrieve the specified fileset from the Data References on the project
            FileSet fs = base.Project.DataTypeReferences[fileset] as FileSet;
            //If the value was missing or the referenced type was not a fileset, then throw an exception
            if (fs == null) {
                throw new ArgumentException(string.Format("'{0}' is not a valid fileset reference", fileset));
            }
            //Otherwise return whether or not the fileset contains files.
            return (fs.FileNames.Count > 0);
        }

        /// <summary>
        /// Returns a delimited string of all the filenames within a <see cref="FileSet"/> with each filename
        /// separated by the specified delimiter string.
        /// </summary>
        /// <param name="fileset">The id of the fileset to check.</param>
        /// <param name="delimiter">String to separate filenames with.</param>
        /// <returns>A delimited string of the filenames within the specified FileSet.</returns>
        /// <exception cref="ArgumentException"><paramref name="fileset" /> is not a valid refid to a defined fileset.</exception>
        /// <example>
        ///   <para>
        ///   Displays a space-pipe-space separated string fo the files within a defined FileSet.
        ///   </para>
        ///   <code>
        ///     <![CDATA[
        /// <fileset id="test.fileset">
        ///     <include name="**/*.cs">
        /// </fileset>
        /// <echo message="${fileset::to-string('test.fileset', ' | ')}">
        ///     ]]>
        ///   </code>
        /// </example>
        [Function("to-string")]
        public string ToString(string fileset, string delimiter) {
            //Try to retrieve the specified fileset from the Data References on the project
            FileSet fs = base.Project.DataTypeReferences[fileset] as FileSet;
            
            //If the value was missing or the referenced type was not a fileset, then throw an exception
            if (fs == null) {
                throw new ArgumentException(string.Format("'{0}' is not a valid fileset reference", fileset));
            }

            //We've got a valid fileset.  Use a stringbuilder to build the delimiter separated list.
            StringBuilder sb = new StringBuilder();

            //This is a loop as we need to omit the delimiter on the last element, and it's easier 
            //to check the last element with a for loop than in a foreach loop.
            for (int i = 0; i < fs.FileNames.Count; i++) {
                string file = fs.FileNames[i];
                sb.Append(file);
                //if this is not the last element, then append the delimiter
                if (i < fs.FileNames.Count - 1) {
                    sb.Append(delimiter);
                }
            }
            //Now return the string representation of the FileSet.
            return sb.ToString();
        }

        #endregion Public Instance Methods
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.