hi,
 
I sent a mail on the nant users list a few days ago about a new task that would help 
to deploy vs.net projects (eg asp.net projects). It copies somewhere only the files 
that are described inside a vs.net project as "Content" + everthing that has been 
built in the output path.
 
I really needed it so i coded it, and i wish now to share it with the community, so... 
here it is !
 
Could it be part of the NAnt.Contrib project ? Can someone of you put it in CVS ? I 
hope you'll agree with that. The code is simple and commented so i won't be giving 
implementation details here. I talked about why I needed this in the nant users lists.
 
If you find something awful, or if you want to rename the task, let's just talk about 
it... I tried to do my best to integrate my code well within the NAnt framework 
(attributes, log, sub task calls). However, the copy task is quite slow compared to 
direct file manipulations, but I thought it was cleaner this way...
 
enough talk, here's the code (I can send an attachment if u prefer) :
 
VSPExtractTask.cs
-------------------------------------------------------------------------
// NAnt - A .NET build tool
//
// 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
//
// Vincent Labatut (c) 2003

using System;
using System.IO;
using System.Xml;
using NAnt.Core.Util;
using NAnt.Core;
using NAnt.Core.Tasks;
using NAnt.Core.Attributes;

namespace NAnt.Contrib.Tasks {

 /// <summary>Packages a Visual Studio .NET Project after a build by copying only 
files necessary to run the application.</summary>
 /// <remarks>
 ///  The Visual Studio Project file (ie csproj, vbproj etc.) contains the exact 
information
 ///  on what files are needed for the project to run. This is particularly true for 
ASP.NET projects.
 ///  This task is typically used after a build in order to prepare the files that 
must be deployed.
 /// </remarks>
 /// <example>
 ///   <para>Get the files that belong to MyProject.vbproj and puts them in a "deploy" 
directory.</para>
 ///   <code><![CDATA[
 ///      <vspextract
 ///         projectfile="MyProject/MyProject.vbproj"
 ///         config="Debug"
 ///         path="${build.dir}/deploy"
 ///         cleanfirst="true"
 ///      />
 ///   ]]></code>
 /// </example>
 [TaskName("vspextract")]
    public class VSPExtractTask : Task 
    {
  private string _projectFile;
  private string _projectConfig;
  private string _destPath;
  private bool _cleanFirst;
  
  /// <summary>
  /// The name of the VS.NET project file to process.
  /// </summary>
  [TaskAttribute("projectfile", Required=true)]
  public string ProjectFile
  {
   get { return _projectFile; }
   set { _projectFile = value; }
  }

  /// <summary>
  /// The name of the project configuration that is to be packaged.
  /// </summary>
  /// <remarks>
  /// <para>
  /// Case-Sensitive.
  /// Generally <c>Release</c> or <c>Debug</c>.
  /// It is required in order to get the project output path.
  /// </para>
  /// </remarks>
  [TaskAttribute("config", Required=true)]
  public string ProjectConfig
  {
   get { return _projectConfig; }
   set { _projectConfig = value; }
  }
  
  /// <summary>
  /// The directory into which the files must be copied.
  /// </summary>
  [TaskAttribute("path", Required=true)]
  public string DestPath 
  {
   get { return _destPath; }
   set { _destPath = value; }
  }
  
  /// <summary>
  /// Indicates if the target directory will be erased from it contents before copying 
the files. Default is "True".
  /// </summary>
  [TaskAttribute("cleanfirst", Required=false), BooleanValidator()]
  public bool CleanFirst
  {
   get { return _cleanFirst; }
   set { _cleanFirst = value; }
  }
  
  public VSPExtractTask()
  {
   _cleanFirst = true;
  }

  protected override void InitializeTask(XmlNode taskNode) 
  {
   if (ProjectFile != null)
   {
    if (!File.Exists(ProjectFile)) 
    {
     throw new BuildException("Couldn't find project file '" + ProjectFile + "'.", 
Location);
    }
   }

   base.InitializeTask (taskNode);
  }

  protected override void ExecuteTask() 
  {
   string srcPath = System.IO.Path.GetDirectoryName(ProjectFile);

   // Load XML project File
   XmlDocument xmldoc = new XmlDocument();
   Output("Loading XML document: " + ProjectFile);
   xmldoc.Load(ProjectFile);
   
   // Make sure the config is valid before doing anything
   XmlNode configNode = xmldoc.SelectSingleNode(@"//Build/Settings/[EMAIL PROTECTED]'" 
+ ProjectConfig + "']");
   if (configNode == null) throw new BuildException(Name +  ": Config \"" + 
ProjectConfig + "\" not found in project !", Location);
   XmlAttribute outputPathAttribute = configNode.Attributes["OutputPath"];
   if (outputPathAttribute == null || 
StringUtils.IsNullOrEmpty(outputPathAttribute.Value))
    throw new BuildException(Name +  ": OutputPath not defined or not found in project 
configuration.", Location);

   // Optional directory cleaning before copy
   if (CleanFirst && Directory.Exists(DestPath)) 
   {
    Output("Cleaning directory " + DestPath);
    DeleteTask cleanDest = new DeleteTask();
    cleanDest.DirectoryName = DestPath;
    cleanDest.FailOnError = true;
    LaunchTask(cleanDest);
   }

   // Copy the files described in the project as "Content"
   // Use a CopyTask, despite that it is quite slow
   Output("Copying files from " + srcPath + " to " + DestPath);
   XmlNodeList nodes = xmldoc.SelectNodes(@"//Files/Include/[EMAIL 
PROTECTED]'Content']/@RelPath");
   CopyTask copyFiles = new CopyTask();
   copyFiles.CopyFileSet.BaseDirectory = srcPath;
   copyFiles.ToDirectory = DestPath;
   foreach (XmlNode node in nodes) 
   {
    copyFiles.CopyFileSet.Includes.Add(node.Value);
   }
   LaunchTask(copyFiles);

   // Copy the output directory files
   // Copy everything, including subdirectories to support localized assemblies
   CopyTask copyOutput = new CopyTask();
   copyOutput.ToDirectory = DestPath;
   copyOutput.CopyFileSet.Includes.Add(Path.Combine(outputPathAttribute.Value, "**"));
   copyOutput.CopyFileSet.BaseDirectory = srcPath;
   LaunchTask(copyOutput);
  }

  // launches a sub task
  private void LaunchTask(Task task) 
  {
   task.Project = Project;
   task.Verbose = Verbose;
   Project.Indent();
   task.Execute();
   Project.Unindent();
  }

  private void Output(string message) 
  {
   Log(Level.Info, LogPrefix + message);
  }
    }
}

 
 
 
+,~wzf+,좷o$yyzW(h礅zxm&j{]z�f)+-uޖ^b,y+޶m+-.ǟ+-bا~j{]z�

Reply via email to