#region BSD License /* Copyright (c) 2004 - 2008 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com), C.J. Adams-Collier (cjac@colliertech.org), Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #endregion using System; using System.Collections; using System.Collections.Specialized; using System.IO; using System.Reflection; using System.Text.RegularExpressions; using Prebuild.Core.Attributes; using Prebuild.Core.Interfaces; using Prebuild.Core.Nodes; using Prebuild.Core.Utilities; namespace Prebuild.Core.Targets { /// /// /// [Target("nant")] public class NAntTarget : ITarget { #region Fields private Kernel m_Kernel; #endregion #region Private Methods private static string PrependPath(string path) { string tmpPath = Helper.NormalizePath(path, '/'); Regex regex = new Regex(@"(\w):/(\w+)"); Match match = regex.Match(tmpPath); //if(match.Success || tmpPath[0] == '.' || tmpPath[0] == '/') //{ tmpPath = Helper.NormalizePath(tmpPath); //} // else // { // tmpPath = Helper.NormalizePath("./" + tmpPath); // } return tmpPath; } private static string BuildReference(SolutionNode solution, ProjectNode currentProject, ReferenceNode refr) { if (!String.IsNullOrEmpty(refr.Path)) { return refr.Path; } if (solution.ProjectsTable.ContainsKey(refr.Name)) { ProjectNode projectRef = (ProjectNode) solution.ProjectsTable[refr.Name]; string finalPath = Helper.NormalizePath(refr.Name + GetProjectExtension(projectRef), '/'); return finalPath; } ProjectNode project = (ProjectNode) refr.Parent; // Do we have an explicit file reference? string fileRef = FindFileReference(refr.Name, project); if (fileRef != null) { return fileRef; } // Is there an explicit path in the project ref? if (refr.Path != null) { return Helper.NormalizePath(refr.Path + "/" + refr.Name + GetProjectExtension(project), '/'); } // No, it's an extensionless GAC ref, but nant needs the .dll extension anyway return refr.Name + ".dll"; } public static string GetRefFileName(string refName) { if (ExtensionSpecified(refName)) { return refName; } else { return refName + ".dll"; } } private static bool ExtensionSpecified(string refName) { return refName.EndsWith(".dll") || refName.EndsWith(".exe"); } private static string GetProjectExtension(ProjectNode project) { string extension = ".dll"; if (project.Type == ProjectType.Exe || project.Type == ProjectType.WinExe) { extension = ".exe"; } return extension; } private static string FindFileReference(string refName, ProjectNode project) { foreach (ReferencePathNode refPath in project.ReferencePaths) { string fullPath = Helper.MakeFilePath(refPath.Path, refName); if (File.Exists(fullPath)) { return fullPath; } fullPath = Helper.MakeFilePath(refPath.Path, refName, "dll"); if (File.Exists(fullPath)) { return fullPath; } fullPath = Helper.MakeFilePath(refPath.Path, refName, "exe"); if (File.Exists(fullPath)) { return fullPath; } } return null; } /// /// Gets the XML doc file. /// /// The project. /// The conf. /// public static string GetXmlDocFile(ProjectNode project, ConfigurationNode conf) { if (conf == null) { throw new ArgumentNullException("conf"); } if (project == null) { throw new ArgumentNullException("project"); } string docFile = (string)conf.Options["XmlDocFile"]; // if(docFile != null && docFile.Length == 0)//default to assembly name if not specified // { // return Path.GetFileNameWithoutExtension(project.AssemblyName) + ".xml"; // } return docFile; } private void WriteProject(SolutionNode solution, ProjectNode project) { string projFile = Helper.MakeFilePath(project.FullPath, project.Name + GetProjectExtension(project), "build"); StreamWriter ss = new StreamWriter(projFile); m_Kernel.CurrentWorkingDirectory.Push(); Helper.SetCurrentDir(Path.GetDirectoryName(projFile)); bool hasDoc = false; using (ss) { ss.WriteLine(""); ss.WriteLine("", project.Name); ss.WriteLine(" ", "build"); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); foreach (ReferenceNode refr in project.References) { if (refr.LocalCopy) { ss.WriteLine(" ", '/')); } } ss.WriteLine(" "); ss.WriteLine(" "); if (project.ConfigFile != null && project.ConfigFile.Length!=0) { ss.Write(" "); } // Add the content files to just be copied ss.WriteLine(" {0}", ""); ss.WriteLine(" {0}", ""); foreach (string file in project.Files) { // Ignore if we aren't content if (project.Files.GetBuildAction(file) != BuildAction.Content) continue; // Create a include tag ss.WriteLine(" {0}", ""); } ss.WriteLine(" {0}", ""); ss.WriteLine(" {0}", ""); ss.Write(" "); ss.WriteLine(" ", project.RootNamespace); foreach (string file in project.Files) { switch (project.Files.GetBuildAction(file)) { case BuildAction.EmbeddedResource: ss.WriteLine(" {0}", ""); break; default: if (project.Files.GetSubType(file) != SubType.Code && project.Files.GetSubType(file) != SubType.Settings) { ss.WriteLine(" ", file.Substring(0, file.LastIndexOf('.')) + ".resx"); } break; } } //if (project.Files.GetSubType(file).ToString() != "Code") //{ // ps.WriteLine(" ", file.Substring(0, file.LastIndexOf('.')) + ".resx"); ss.WriteLine(" "); ss.WriteLine(" "); foreach (string file in project.Files) { switch (project.Files.GetBuildAction(file)) { case BuildAction.Compile: ss.WriteLine(" "); break; default: break; } } ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); foreach(ReferencePathNode refPath in project.ReferencePaths) { ss.WriteLine(" "); } ss.WriteLine(" "); foreach (ReferenceNode refr in project.References) { string path = Helper.NormalizePath(Helper.MakePathRelativeTo(project.FullPath, BuildReference(solution, project, refr)), '/'); ss.WriteLine(" "); } ss.WriteLine(" "); ss.WriteLine(" "); foreach (ConfigurationNode conf in project.Configurations) { if (!String.IsNullOrEmpty(conf.Options.OutputPath)) { string targetDir = Helper.NormalizePath(conf.Options.OutputPath, '/'); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); break; } } ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); if (hasDoc) { ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.Write(" "); } else { ss.WriteLine(".exe\" />"); } ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); // foreach(ReferenceNode refr in project.References) // { // string path = Helper.NormalizePath(Helper.MakePathRelativeTo(project.FullPath, BuildReferencePath(solution, refr)), '/'); // if (path != "") // { // ss.WriteLine(" ", path); // } // } ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); } ss.WriteLine(" "); ss.WriteLine(""); } m_Kernel.CurrentWorkingDirectory.Pop(); } private void WriteCombine(SolutionNode solution) { m_Kernel.Log.Write("Creating NAnt build files"); foreach (ProjectNode project in solution.Projects) { if (m_Kernel.AllowProject(project.FilterGroups)) { m_Kernel.Log.Write("...Creating project: {0}", project.Name); WriteProject(solution, project); } } m_Kernel.Log.Write(""); string combFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "build"); StreamWriter ss = new StreamWriter(combFile); m_Kernel.CurrentWorkingDirectory.Push(); Helper.SetCurrentDir(Path.GetDirectoryName(combFile)); using (ss) { ss.WriteLine(""); ss.WriteLine("", solution.Name); ss.WriteLine(" "); ss.WriteLine(); //ss.WriteLine(" "); //ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); // actually use active config out of prebuild.xml ss.WriteLine(" ", solution.ActiveConfig); foreach (ConfigurationNode conf in solution.Configurations) { ss.WriteLine(); ss.WriteLine(" ", conf.Name); ss.WriteLine(" ", conf.Name); ss.WriteLine(" ", conf.Options["DebugInformation"].ToString().ToLower()); ss.WriteLine(" "); ss.WriteLine(); } ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); // sdague - ok, this is an ugly hack, but what it lets // us do is native include of files into the nant // created files from all .nant/*include files. This // lets us keep using prebuild, but allows for // extended nant targets to do build and the like. try { Regex re = new Regex(".include$"); DirectoryInfo nantdir = new DirectoryInfo(".nant"); foreach (FileSystemInfo item in nantdir.GetFileSystemInfos()) { if (item is DirectoryInfo) { } else if (item is FileInfo) { if (re.Match(((FileInfo)item).FullName) != System.Text.RegularExpressions.Match.Empty) { Console.WriteLine("Including file: " + ((FileInfo)item).FullName); using (FileStream fs = new FileStream(((FileInfo)item).FullName, FileMode.Open, FileAccess.Read, FileShare.None)) { using (StreamReader sr = new StreamReader(fs)) { ss.WriteLine("", ((FileInfo)item).FullName); while (sr.Peek() != -1) { ss.WriteLine(sr.ReadLine()); } ss.WriteLine(); } } } } } } catch { } // ss.WriteLine(" "); // ss.WriteLine(" "); // ss.WriteLine(" ", solution.Name, solution.Version); // ss.WriteLine(" "); // ss.WriteLine(" "); // // ss.WriteLine(" "); // ss.WriteLine(" "); // ss.WriteLine(" "); // ss.WriteLine(" "); // ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); //ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(" "); foreach (ProjectNode project in solution.Projects) { string path = Helper.MakePathRelativeTo(solution.FullPath, project.FullPath); ss.Write(" "); } ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); foreach (ProjectNode project in solution.ProjectsTableOrder) { string path = Helper.MakePathRelativeTo(solution.FullPath, project.FullPath); ss.Write(" "); } ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(); //ss.WriteLine(" "); ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(" "); ss.WriteLine(" "); foreach (ProjectNode project in solution.Projects) { string path = Helper.MakePathRelativeTo(solution.FullPath, project.FullPath); ss.Write(" "); } ss.WriteLine(" "); ss.WriteLine(); ss.WriteLine(""); } m_Kernel.CurrentWorkingDirectory.Pop(); } private void CleanProject(ProjectNode project) { m_Kernel.Log.Write("...Cleaning project: {0}", project.Name); string projectFile = Helper.MakeFilePath(project.FullPath, project.Name + GetProjectExtension(project), "build"); Helper.DeleteIfExists(projectFile); } private void CleanSolution(SolutionNode solution) { m_Kernel.Log.Write("Cleaning NAnt build files for", solution.Name); string slnFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "build"); Helper.DeleteIfExists(slnFile); foreach (ProjectNode project in solution.Projects) { CleanProject(project); } m_Kernel.Log.Write(""); } #endregion #region ITarget Members /// /// Writes the specified kern. /// /// The kern. public void Write(Kernel kern) { if (kern == null) { throw new ArgumentNullException("kern"); } m_Kernel = kern; foreach (SolutionNode solution in kern.Solutions) { WriteCombine(solution); } m_Kernel = null; } /// /// Cleans the specified kern. /// /// The kern. public virtual void Clean(Kernel kern) { if (kern == null) { throw new ArgumentNullException("kern"); } m_Kernel = kern; foreach (SolutionNode sol in kern.Solutions) { CleanSolution(sol); } m_Kernel = null; } /// /// Gets the name. /// /// The name. public string Name { get { return "nant"; } } #endregion } }