With the most current version of nant, the following message is
displayed when you type nant on the command line with no build file.

   Could not find a '*.build' file in '<current directory>'
   Try 'nant -help' for more information

So you type in nant -held and you get the following message...

   Could not find a '*.build' file in '<current directory>'
   Try 'nant -help' for more information

This is because in Nant.cs, GetBuildFileName is called before checking
whether help should be shown.  I've attached how I fixed it in my
version.  It seems to work OK.

Justin Rudd

"Somebody has to do something, and it's just
incredibly pathetic that it has to be us."
  - Jerry Garcia 
// NAnt - A .NET build tool
// Copyright (C) 2001-2002 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

// Gerry Shaw ([EMAIL PROTECTED])

using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.Xsl;
using System.Reflection;
using System.Text.RegularExpressions;

namespace SourceForge.NAnt {

    public class NAnt {
        public static int Main(string[] args) {
            try {
                Project project = new Project();

                const string buildfileOption   = "-buildfile:";
                const string basedirOption     = "-basedir:";
                const string setOption         = "-D:";
                const string helpOption        = "-help";
                const string projectHelpOption = "-projecthelp";
                const string verboseOption     = "-verbose";
                const string findOption        = "-find";

                bool showHelp = false;
                bool showProjectHelp = false;
                bool findInParent = false;

                foreach (string arg in args) {
                    if (arg.StartsWith(buildfileOption)) {
                        project.BuildFileName = arg.Substring(buildfileOption.Length);
                    } else if (arg.StartsWith(basedirOption)) {
                        project.BaseDirectory = arg.Substring(basedirOption.Length);
                    } else if (arg.StartsWith(setOption)) {
                        // Properties from command line cannot be overwritten by
                        // the build file.  Once set they are set for the rest of the 
build.
                        Match match = Regex.Match(arg, @"-D:(\w+.*)=(\w*.*)");
                        if (match.Success) {
                            string name = match.Groups[1].Value;
                            string value = match.Groups[2].Value;
                            project.Properties.AddReadOnly(name, value);
                        }
                    } else if (arg.StartsWith(helpOption)) {
                        showHelp = true;
                    } else if (arg.StartsWith(projectHelpOption)) {
                        showProjectHelp = true;
                    } else if (arg.StartsWith(verboseOption)) {
                        project.Verbose = true;
                    } else if (arg.StartsWith(findOption)) {
                        findInParent = true;
                    } else if (arg.Length > 0) {
                        if (arg.StartsWith("-")) {
                            throw new ApplicationException(String.Format("Unknown 
argument '{0}'", arg));
                        }
                        // must be a target if not an option
                        project.BuildTargets.Add(arg);
                    }
                }

                                /*
                // Get build file name
                project.BuildFileName = GetBuildFileName(Environment.CurrentDirectory, 
project.BuildFileName, findInParent);

                // Get version information directly from assembly.  This takes more
                // work but keeps the version numbers being displayed in sync with
                // what the assembly is marked with.
                FileVersionInfo info = 
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);

                if (showHelp) {
                    const int optionPadding = 23;

                    Console.WriteLine("NAnt version {0} Copyright (C) 2001-{1} Gerry 
Shaw", 
                        info.FileMajorPart + "." + info.FileMinorPart + "." + 
info.FileBuildPart, 
                        DateTime.Now.Year);
                    Console.WriteLine("http://nant.sf.net";);
                    Console.WriteLine();
                    Console.WriteLine("NAnt comes with ABSOLUTELY NO WARRANTY.");
                    Console.WriteLine("This is free software, and you are welcome to 
redistribute it under certain");
                    Console.WriteLine("conditions set out by the GNU General Public 
License.  A copy of the license");
                    Console.WriteLine("is available in the distribution package and 
from the NAnt web site.");
                    Console.WriteLine();
                    Console.WriteLine("usage: nant [options] [target [target2 
[target3] ... ]]");
                    Console.WriteLine();
                    Console.WriteLine("options:");
                    Console.WriteLine("  {0} print this message", 
helpOption.PadRight(optionPadding));
                    Console.WriteLine("  {0} print project help information", 
projectHelpOption.PadRight(optionPadding));
                    Console.WriteLine("  {0} use given buildfile", (buildfileOption + 
"<file>").PadRight(optionPadding));
                    Console.WriteLine("  {0} set project base directory", 
(basedirOption + "<dir>").PadRight(optionPadding));
                    Console.WriteLine("  {0} search parent directories for buildfile", 
findOption.PadRight(optionPadding));
                    Console.WriteLine("  {0} use value for given property", (setOption 
+ "<property>=<value>").PadRight(optionPadding));
                    Console.WriteLine("  {0} displays more information during build 
process", verboseOption.PadRight(optionPadding));
                    Console.WriteLine();
                    Console.WriteLine("A file ending in .build will be used if no 
buildfile is specified.");

                } else if (showProjectHelp ) {
                    ShowProjectHelp(project.BuildFileName);
                
                } else {
                    if (!project.Run()) {
                        throw new ApplicationException("");
                    }
                }
                                */
                                if (showHelp) {
                                        // Get version information directly from 
assembly.  This takes more
                                        // work but keeps the version numbers being 
displayed in sync with
                                        // what the assembly is marked with.
                                        FileVersionInfo info = 
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);

                                        const int optionPadding = 23;

                                        Console.WriteLine("NAnt version {0} Copyright 
(C) 2001-{1} Gerry Shaw", 
                                                info.FileMajorPart + "." + 
info.FileMinorPart + "." + info.FileBuildPart, 
                                                DateTime.Now.Year);
                                        Console.WriteLine("http://nant.sf.net";);
                                        Console.WriteLine();
                                        Console.WriteLine("NAnt comes with ABSOLUTELY 
NO WARRANTY.");
                                        Console.WriteLine("This is free software, and 
you are welcome to redistribute it under certain");
                                        Console.WriteLine("conditions set out by the 
GNU General Public License.  A copy of the license");
                                        Console.WriteLine("is available in the 
distribution package and from the NAnt web site.");
                                        Console.WriteLine();
                                        Console.WriteLine("usage: nant [options] 
[target [target2 [target3] ... ]]");
                                        Console.WriteLine();
                                        Console.WriteLine("options:");
                                        Console.WriteLine("  {0} print this message", 
helpOption.PadRight(optionPadding));
                                        Console.WriteLine("  {0} print project help 
information", projectHelpOption.PadRight(optionPadding));
                                        Console.WriteLine("  {0} use given buildfile", 
(buildfileOption + "<file>").PadRight(optionPadding));
                                        Console.WriteLine("  {0} set project base 
directory", (basedirOption + "<dir>").PadRight(optionPadding));
                                        Console.WriteLine("  {0} search parent 
directories for buildfile", findOption.PadRight(optionPadding));
                                        Console.WriteLine("  {0} use value for given 
property", (setOption + "<property>=<value>").PadRight(optionPadding));
                                        Console.WriteLine("  {0} displays more 
information during build process", verboseOption.PadRight(optionPadding));
                                        Console.WriteLine();
                                        Console.WriteLine("A file ending in .build 
will be used if no buildfile is specified.");

                                } else {
                                        // Get build file name
                                        project.BuildFileName = 
GetBuildFileName(Environment.CurrentDirectory, project.BuildFileName, findInParent);

                                        if (showProjectHelp) {
                                                ShowProjectHelp(project.BuildFileName);
                                        } else {
                                                if (!project.Run()) {
                                                        throw new 
ApplicationException("");
                                                }
                                        }
                                }

                                return 0;

            } catch (ApplicationException e) {
                if (e.Message.Length > 0) {
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine("Try 'nant -help' for more information");
                return 1;

            } catch (Exception e) {
                // all other exceptions should have been caught
                Console.WriteLine("INTERNAL ERROR");
                Console.WriteLine(e.ToString());
                Console.WriteLine();
                Console.WriteLine("Please send bug report to 
[EMAIL PROTECTED]");
                return 2;
            }
        }

        public static void ShowProjectHelp(string buildFileName) {
            
            string resourceDirectory = 
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + 
"\\NAnt";
           
            // load our transform file out of the embedded resources
            Stream xsltStream = 
Assembly.GetExecutingAssembly().GetManifestResourceStream("Nant.xslt.projecthelp.xslt");
            
            // Load the XML documentation into a DOM.
            XmlDocument buildFileDocument = new XmlDocument();
            buildFileDocument.Load(buildFileName);

            XslTransform transform = new XslTransform();           
            XmlTextReader reader = new XmlTextReader( xsltStream, 
XmlNodeType.Document, null );            
            transform.Load(reader);

            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            XsltArgumentList arguments = new XsltArgumentList();

            // Do transform
            transform.Transform( buildFileDocument, arguments, writer );
            string outstr = sb.ToString();
            System.Console.WriteLine( sb.ToString() );
        }

        /// <summary>
        /// Gets the file name for the build file in the specified directory.
        /// </summary>
        /// <param name="directory">The directory to look for a build file.  When in 
doubt use Environment.CurrentDirectory for directory.</param>
        /// <param name="searchPattern">Look for a build file with this pattern or 
name.  If null look for a file that matches the default build pattern 
(*.build).</param>
        /// <param name="findInParent">Whether or not to search the parent directories 
for a build file.</param>
        /// <returns>The path to the build file or <c>null</c> if no build file could 
be found.</returns>
        public static string GetBuildFileName(string directory, string searchPattern, 
bool findInParent) {
            string buildFileName = null;
            if (Path.IsPathRooted(searchPattern)) {
                buildFileName = searchPattern;
            } else {
                if (searchPattern == null) {
                    searchPattern = "*.build";
                }

                // find first file ending in .build
                DirectoryInfo directoryInfo = new DirectoryInfo(directory);
                FileInfo[] files = directoryInfo.GetFiles(searchPattern);
                if (files.Length == 1) {
                    buildFileName = Path.Combine(directory, files[0].Name);
                } else if (files.Length == 0) {
                    DirectoryInfo parentDirectoryInfo = directoryInfo.Parent;
                    if (findInParent && parentDirectoryInfo != null) {
                        buildFileName = GetBuildFileName(parentDirectoryInfo.FullName, 
searchPattern, findInParent);
                    } else {
                        throw new ApplicationException((String.Format("Could not find 
a '{0}' file in '{1}'", searchPattern, directory)));
                    }
                } else { // files.Length > 1
                    throw new ApplicationException(String.Format("More than one '{0}' 
file found in '{1}'.  Use -buildfile:<file> to specify.", searchPattern, directory));
                }
            }
            return buildFileName;
        }

    }
}

Reply via email to