/// <summary>
        /// Runs through a project and generates the files in it.
        /// </summary>
        /// <param name="progressHelper">The TaskProgressHelper to use to report progress and cancel the operation.</param>
        /// <param name="projectInfo">The Project we are generating files from.</param>
        /// <param name="folderName">The name of the root folder to generate into. Not the full path, just the relative path to the
        /// current folder.</param>
        /// <param name="folder"></param>
        /// <param name="parentNode"></param>
        /// <param name="thisLevelRootObject"></param>
        /// <returns></returns>
        /// <param name="loader"></param>
        /// <param name="controller"></param>
        public int GenerateAllFiles(ITaskProgressHelper <GenerateFilesProgress> progressHelper, IProjectHelper projectInfo, string folderName, IFolder folder, ProjectFileTreeNode parentNode, IScriptBaseObject thisLevelRootObject, ILoader loader, IController controller)
        {
            if (parentNode is ProjectFileTree)
            {
                ((ProjectFileTree)parentNode).TreeRestructuring = true;
                ((ProjectFileTree)parentNode).Clear();
            }
            int fileCount = 0;

            try
            {
                _Controller       = controller;
                _Loader           = loader;
                _ProgressHelper   = progressHelper;
                project           = projectInfo;
                CurrentRootObject = thisLevelRootObject;
                absoluteBasePath  = controller.GetTempFilePathForComponent(ComponentKey.WorkbenchFileGenerator);


                {
                    Version version         = new Version(loader.GetAssemblyVersionNumber());
                    Version expectedVersion = new Version(1, 1, 9, 49);
                    if (version < expectedVersion)
                    {
                        throw new OldVersionException("The template was compiled with an old version of ArchAngel, and cannot be used in this version of Workbench");
                    }
                }


                foreach (IFolder subFolder in folder.SubFolders)
                {
                    if (progressHelper.IsCancellationPending())
                    {
                        progressHelper.Cancel();
                        return(fileCount);
                    }

                    ProjectFileTreeNode folderNode = null;

                    if (parentNode != null && subFolder.Name != "ROOT")
                    {
                        folderNode          = parentNode.AddChildNode(subFolder.Name);
                        folderNode.IsFolder = true;
                    }

                    if (!string.IsNullOrEmpty(subFolder.IteratorName))
                    {
                        // The folder has an iterator
                        ProviderInfo provider;
                        Type         iteratorType = project.GetTypeFromProviders(subFolder.IteratorName, out provider);

                        if (progressHelper.IsCancellationPending())
                        {
                            progressHelper.Cancel();
                            return(fileCount);
                        }

                        if (iteratorType != null)
                        {
                            IEnumerable <IScriptBaseObject> iteratorObjects;

                            if (thisLevelRootObject == null)
                            {
                                iteratorObjects = provider.GetAllObjectsOfType(iteratorType.FullName);
                            }
                            else if (iteratorType.IsInstanceOfType(thisLevelRootObject))
                            {
                                iteratorObjects = new[] { thisLevelRootObject };
                            }
                            else
                            {
                                iteratorObjects = provider.GetAllObjectsOfType(iteratorType.FullName, thisLevelRootObject);
                            }
                            if (iteratorObjects != null)
                            {
                                foreach (IScriptBaseObject iteratorObject in iteratorObjects)
                                {
                                    if (progressHelper.IsCancellationPending())
                                    {
                                        progressHelper.Cancel();
                                        return(fileCount);
                                    }

                                    CurrentRootObject = iteratorObject;

                                    string subFolderName = UpdateScriptName(iteratorObject, subFolder);

                                    if (folderNode != null)
                                    {
                                        folderNode.Text = subFolderName;
                                    }

                                    subFolderName = Path.Combine(folderName, subFolderName);

                                    //Directory.CreateDirectory(Path.Combine(Controller.Instance.GetTempFilePathForComponent(ComponentKey.WorkbenchFileGenerator), subFolderName));

                                    fileCount += GenerateAllFiles(progressHelper, project, subFolderName, subFolder, folderNode, CurrentRootObject, loader, controller);
                                }
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("The IteratorType could not be found: {0}. Are you missing an assembly?", subFolder.IteratorName));
                        }
                    }
                    else
                    {
                        // The folder doesn't have an iterator
                        if (progressHelper.IsCancellationPending())
                        {
                            progressHelper.Cancel();
                            return(fileCount);
                        }
                        string subFolderName = UpdateScriptName(null, subFolder);

                        if (folderNode != null)
                        {
                            folderNode.Text = subFolderName;
                        }
                        subFolderName = Path.Combine(folderName, subFolderName);
                        //Directory.CreateDirectory(Path.Combine(Controller.Instance.GetTempFilePathForComponent(ComponentKey.WorkbenchFileGenerator), subFolderName));

                        fileCount += GenerateAllFiles(progressHelper, projectInfo, subFolderName, subFolder, folderNode, thisLevelRootObject, loader, controller);
                    }

                    //progressHelper.ReportProgress(20, new GenerateFilesProgress(fileCount));
                }

                //progressHelper.ReportProgress(50, new GenerateFilesProgress(fileCount));

                foreach (IScript script in folder.Scripts)
                {
                    if (progressHelper.IsCancellationPending())
                    {
                        progressHelper.Cancel();
                        return(fileCount);
                    }
                    fileCount += CreateScriptFile(progressHelper, folderName, script, parentNode);
                }

                // progressHelper.ReportProgress(80, new GenerateFilesProgress(fileCount));

                foreach (IFile file in folder.Files)
                {
                    if (progressHelper.IsCancellationPending())
                    {
                        progressHelper.Cancel();
                        return(fileCount);
                    }
                    fileCount += CreateStaticFile(progressHelper, folderName, file, parentNode);
                }

                //progressHelper.ReportProgress(95, new GenerateFilesProgress(fileCount));

                //Application.DoEvents();
            }
            catch (Exception e)
            {
                progressHelper.ReportProgress(100, new GenerateFilesProgress(fileCount, e));
                return(fileCount);
            }
            finally
            {
                if (parentNode is ProjectFileTree)
                {
                    ((ProjectFileTree)parentNode).TreeRestructuring = false;
                    parentNode.RaiseNodeChangedEvent(parentNode, true);
                }
            }
            progressHelper.ReportProgress(50, new GenerateFilesProgress(fileCount));
            return(fileCount);
        }
        /// <summary>
        /// Runs through a project and generates the files in it.
        /// </summary>
        /// <param name="progressHelper">The TaskProgressHelper to use to report progress and cancel the operation.</param>
        /// <param name="projectInfo">The Project we are generating files from.</param>
        /// <param name="folderName">The name of the root folder to generate into. Not the full path, just the relative path to the 
        /// current folder.</param>
        /// <param name="folder"></param>
        /// <param name="parentNode"></param>
        /// <param name="thisLevelRootObject"></param>
        /// <returns></returns>
        /// <param name="loader"></param>
        /// <param name="controller"></param>
        public int GenerateAllFiles(ITaskProgressHelper<GenerateFilesProgress> progressHelper, IProjectHelper projectInfo, string folderName, IFolder folder, ProjectFileTreeNode parentNode, IScriptBaseObject thisLevelRootObject, ILoader loader, IController controller)
        {
            if (parentNode is ProjectFileTree)
            {
                ((ProjectFileTree)parentNode).TreeRestructuring = true;
                ((ProjectFileTree)parentNode).Clear();
            }
            int fileCount = 0;

            try
            {
                _Controller = controller;
                _Loader = loader;
                _ProgressHelper = progressHelper;
                project = projectInfo;
                CurrentRootObject = thisLevelRootObject;
                absoluteBasePath = controller.GetTempFilePathForComponent(ComponentKey.WorkbenchFileGenerator);

                {
                    Version version = new Version(loader.GetAssemblyVersionNumber());
                    Version expectedVersion = new Version(1, 1, 9, 49);
                    if (version < expectedVersion)
                    {
                        throw new OldVersionException("The template was compiled with an old version of ArchAngel, and cannot be used in this version of Workbench");
                    }
                }

                foreach (IFolder subFolder in folder.SubFolders)
                {
                    if (progressHelper.IsCancellationPending())
                    {
                        progressHelper.Cancel();
                        return fileCount;
                    }

                    ProjectFileTreeNode folderNode = null;

                    if (parentNode != null && subFolder.Name != "ROOT")
                    {
                        folderNode = parentNode.AddChildNode(subFolder.Name);
                        folderNode.IsFolder = true;
                    }

                    if (!string.IsNullOrEmpty(subFolder.IteratorName))
                    {
                        // The folder has an iterator
                        ProviderInfo provider;
                        Type iteratorType = project.GetTypeFromProviders(subFolder.IteratorName, out provider);

                        if (progressHelper.IsCancellationPending())
                        {
                            progressHelper.Cancel();
                            return fileCount;
                        }

                        if (iteratorType != null)
                        {
                            IEnumerable<IScriptBaseObject> iteratorObjects;

                            if (thisLevelRootObject == null)
                            {
                                iteratorObjects = provider.GetAllObjectsOfType(iteratorType.FullName);
                            }
                            else if (iteratorType.IsInstanceOfType(thisLevelRootObject))
                            {
                                iteratorObjects = new[] { thisLevelRootObject };
                            }
                            else
                            {
                                iteratorObjects = provider.GetAllObjectsOfType(iteratorType.FullName, thisLevelRootObject);
                            }
                            if (iteratorObjects != null)
                            {
                                foreach (IScriptBaseObject iteratorObject in iteratorObjects)
                                {
                                    if (progressHelper.IsCancellationPending())
                                    {
                                        progressHelper.Cancel();
                                        return fileCount;
                                    }

                                    CurrentRootObject = iteratorObject;

                                    string subFolderName = UpdateScriptName(iteratorObject, subFolder);

                                    if (folderNode != null)
                                    {
                                        folderNode.Text = subFolderName;
                                    }

                                    subFolderName = Path.Combine(folderName, subFolderName);

                                        //Directory.CreateDirectory(Path.Combine(Controller.Instance.GetTempFilePathForComponent(ComponentKey.WorkbenchFileGenerator), subFolderName));

                                    fileCount += GenerateAllFiles(progressHelper, project, subFolderName, subFolder, folderNode, CurrentRootObject, loader, controller);

                                }
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("The IteratorType could not be found: {0}. Are you missing an assembly?", subFolder.IteratorName));
                        }
                    }
                    else
                    {
                        // The folder doesn't have an iterator
                        if (progressHelper.IsCancellationPending())
                        {
                            progressHelper.Cancel();
                            return fileCount;
                        }
                        string subFolderName = UpdateScriptName(null, subFolder);

                        if (folderNode != null)
                        {
                            folderNode.Text = subFolderName;
                        }
                        subFolderName = Path.Combine(folderName, subFolderName);
                        //Directory.CreateDirectory(Path.Combine(Controller.Instance.GetTempFilePathForComponent(ComponentKey.WorkbenchFileGenerator), subFolderName));

                        fileCount += GenerateAllFiles(progressHelper, projectInfo, subFolderName, subFolder, folderNode, thisLevelRootObject, loader, controller);
                    }

                    //progressHelper.ReportProgress(20, new GenerateFilesProgress(fileCount));
                }

                //progressHelper.ReportProgress(50, new GenerateFilesProgress(fileCount));

                foreach (IScript script in folder.Scripts)
                {
                    if (progressHelper.IsCancellationPending())
                    {
                        progressHelper.Cancel();
                        return fileCount;
                    }
                    fileCount += CreateScriptFile(progressHelper, folderName, script, parentNode);
                }

                // progressHelper.ReportProgress(80, new GenerateFilesProgress(fileCount));

                foreach (IFile file in folder.Files)
                {
                    if (progressHelper.IsCancellationPending())
                    {
                        progressHelper.Cancel();
                        return fileCount;
                    }
                    fileCount += CreateStaticFile(progressHelper, folderName, file, parentNode);
                }

                //progressHelper.ReportProgress(95, new GenerateFilesProgress(fileCount));

                //Application.DoEvents();
            }
            catch(Exception e)
            {
                progressHelper.ReportProgress(100, new GenerateFilesProgress(fileCount, e));
                return fileCount;
            }
            finally
            {
                if (parentNode is ProjectFileTree)
                {
                    ((ProjectFileTree) parentNode).TreeRestructuring = false;
                    parentNode.RaiseNodeChangedEvent(parentNode, true);
                }
            }
            progressHelper.ReportProgress(50, new GenerateFilesProgress(fileCount));
            return fileCount;
        }
        private bool ProcessScriptObject(ITaskProgressHelper <GenerateFilesProgress> progressHelper, object scriptObject, string folderName, IScript script, ProjectFileTreeNode parentNode)
        {
            bool   success    = true;
            string scriptName = UpdateScriptName(scriptObject, script);
            string fileName   = Path.Combine(folderName, scriptName);

            if (scriptName.IndexOf("#") >= 0)
            {
                success = false;
            }
            if (success)
            {
                TextFileInformation fileInfo = new TextFileInformation();
                fileInfo.RelativeFilePath = fileName;

                try
                {
                    object[] parameters = new object[0];

                    if (project.FileSkippingIsImplemented)
                    {
                        try
                        {
                            // Reset the SkipCurrentFile variable
                            _Loader.CallTemplateFunction("InternalFunctions.ResetSkipCurrentFile", ref parameters);
                        }
                        catch
                        {
                            project.FileSkippingIsImplemented = false;
                        }
                    }
                    // Call the script file function to get the file text body
                    parameters = new[] { scriptObject };
                    // Check whether we must skip the current file
                    string str = (string)_Loader.CallTemplateFunction(script.ScriptName, ref parameters);

                    if (progressHelper.IsCancellationPending())
                    {
                        return(false);
                    }

                    parameters = new object[0];

                    bool skipCurrentFile = false;

                    if (project.FileSkippingIsImplemented)
                    {
                        skipCurrentFile = (bool)_Loader.CallTemplateFunction("InternalFunctions.MustSkipCurrentFile", ref parameters);
                    }
                    if (!skipCurrentFile)
                    {
                        str = Utility.StandardizeLineBreaks(str, Utility.LineBreaks.Windows);
                        string fullPath = Path.Combine(
                            _Controller.GetTempFilePathForComponent(ComponentKey.WorkbenchFileGenerator),
                            fileName);

                        _Loader.WriteScriptToFile(str, fullPath);

                        fileInfo.NewGenFile = new TextFile(fullPath, false);

                        string        versionNumberString = _Loader.GetAssemblyVersionNumber();
                        VersionNumber versionNumber;
                        if (VersionNumber.TryParse(versionNumberString, out versionNumber))
                        {
                            // Get the template language from the template function.
                            string templateLanguageString = _Loader.GetTemplateFunctionLanguage(script.ScriptName);
                            try
                            {
                                fileInfo.TemplateLanguage = SyntaxEditorHelper.LanguageEnumFromName(templateLanguageString);
                            } catch (NotImplementedException)
                            {
                                fileInfo.TemplateLanguage = null;
                            }
                        }

                        parentNode.AddChildNode(fileInfo, scriptName);
                        AddFileCountToPreviousEventAndRefire(progressHelper, 1);
                    }
                }
                catch (Slyce.Loader.Exceptions.TemplateFunctionException ex)
                {
                    success = false;
                    string message = "<span class='error'>" + ex.Message + "</span>";

                    if (ex.InnerException != null)
                    {
                        message += ":<br/>" + Environment.NewLine + "<b>" + ex.InnerException.Message + "</b>" +
                                   Environment.NewLine + GetCleanTemplateFunctionErrorStackTrace(ex) +
                                   Environment.NewLine + "Target Site: " + ex.InnerException.TargetSite;
                    }
                    RaiseTemplateFunctionCallErrorEvent(ex);
                    // Do nothing, just skip the file because the error will get reported to the user.
                    parentNode.AddChildNode(fileInfo, scriptName).GenerationError = new GenerationError(fileName, message);
                }
                catch (Exception ex)
                {
                    string message = "<span class='error'>" + ex.Message + "</span>";

                    if (ex.InnerException != null)
                    {
                        message += ":<br/>" + Environment.NewLine + "<b>" + ex.InnerException.Message + "</b>" +
                                   Environment.NewLine + GetCleanTemplateFunctionErrorStackTrace(ex) +
                                   Environment.NewLine + "Target Site: " + ex.InnerException.TargetSite;
                    }

                    parentNode.AddChildNode(fileInfo, scriptName).GenerationError = new GenerationError(fileName, message);
                    // Make sure any other type of exception gets thrown
                    throw;
                }
            }
            return(success);
        }