private void SetupLoader()
        {
            object[] p = new object[0];
            Expect
            .Call(loader.CallTemplateFunction("InternalFunctions.MustSkipCurrentFile", ref p))
            .Return(false)
            .Repeat.Any();
            Expect
            .Call(loader.CallTemplateFunction("InternalFunctions.MustSkipCurrentFile"))
            .Return(false)
            .Repeat.Any();

            Expect
            .Call(loader.GetAssemblyVersionNumber())
            .Return("99.99.99.99")
            .Repeat.Any();

            p = new object[] { scriptObject };
            Expect
            .Call(loader.CallTemplateFunction("Class", ref p))
            .Return("public class SomeClass { }")
            .Repeat.Any();
        }
Example #2
0
        public int GenerateAllFiles(string folderName, IFolder folder, ProjectFileTreeNode parentNode, IScriptBaseObject thisLevelRootObject, string basePath, bool isTopLevel)
        {
            if (isTopLevel)
            {
                SharedData.IsBusyGenerating = true;
                _Project.StartNewFileGenerationRun();

                // Reset the Template before the File name validation run.
                _Loader.CallTemplateFunction(TemplateHelper.ClearTemplateCacheFunctionName);

                // Run the pre generation template function.

                //var data = new PreGenerationData { OutputFolder = _Project.ProjectSettings.OutputPath };

                //foreach (var uo in _Project.Options.Where(o => o.IsVirtualProperty == false))
                //{
                //    var optionValue = _Loader.GetUserOption(uo.VariableName);
                //    data.UserOptions.Add(uo.VariableName, optionValue);
                //}

                //foreach (var provider in _Project.Providers)
                //{
                //    ArchAngel.Interfaces.ProviderInfo[] otherProviders = new ProviderInfo[_Project.Providers.Count];
                //    _Project.Providers.CopyTo(otherProviders);
                //    data.OtherProviderInfos = otherProviders.ToList();
                //    data.OtherProviderInfos.Remove(provider);
                //    provider.InitialisePreGeneration(data);
                //    //_Loader.CallPreGenerationInitialisationFunction(provider, data);
                //}

                IEnumerable <FilenameInfo> duplicates;
                DuplicateFileNameChecker   checker = new DuplicateFileNameChecker(this, _Project, _Project.ProjectSettings.OutputPath);
                bool validates = checker.ValidateFileNames(folderName, folder, thisLevelRootObject, out duplicates);

                if (validates == false)
                {
                    _ProgressHelper.ReportProgress(100, new GenerateFilesProgress(0, new DuplicateFilesException(duplicates)));
                    return(0);
                }

                // Reset the Template again before the real template run.
                object[] parameters = new object[0];
                _Loader.CallTemplateFunction(TemplateHelper.ClearTemplateCacheFunctionName, ref parameters);

                //foreach (var provider in _Project.Providers)
                //{
                //    _Loader.CallPreGenerationInitialisationFunction(provider, data);
                //}
            }

            if (parentNode == null && isTopLevel)
            {
                addToProjectFileTree = false;
            }

            if (_Loader == null)
            {
                return(0);
            }

            if (addToProjectFileTree && parentNode is ProjectFileTree)
            {
                ((ProjectFileTree)parentNode).TreeRestructuring = true;
                ((ProjectFileTree)parentNode).Clear();
            }
            int fileCount = 0;

            try
            {
                CurrentRootObject = thisLevelRootObject;
                absoluteBasePath  = basePath;

                {
                    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 (addToProjectFileTree && 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.GetIteratorTypeFromProviders(subFolder.IteratorName, out provider);

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

                        object[] iteratorObjects;

                        if (thisLevelRootObject == null)
                        {
                            iteratorObjects = provider.GetAllObjectsOfType(iteratorType.FullName).ToArray();
                        }
                        else if (iteratorType.IsInstanceOfType(thisLevelRootObject))
                        {
                            iteratorObjects = new[] { thisLevelRootObject };
                        }
                        else
                        {
                            iteratorObjects = provider.GetAllObjectsOfType(iteratorType.FullName, thisLevelRootObject).ToArray();
                        }

                        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);

                                fileCount += GenerateAllFiles(subFolderName, subFolder, folderNode, CurrentRootObject, basePath, false);
                            }
                        }
                    }
                    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);

                        fileCount += GenerateAllFiles(subFolderName, subFolder, folderNode, thisLevelRootObject, basePath, false);
                    }
                }

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

                foreach (IFile file in folder.Files)
                {
                    if (_ProgressHelper.IsCancellationPending())
                    {
                        _ProgressHelper.Cancel();
                        return(fileCount);
                    }
                    fileCount += CreateStaticFile(folderName, file, parentNode);
                }
            }
            catch (Exception e)
            {
                _ProgressHelper.ReportProgress(100, new GenerateFilesProgress(fileCount, e));
                return(fileCount);
            }
            finally
            {
                if (addToProjectFileTree && parentNode is ProjectFileTree)
                {
                    ((ProjectFileTree)parentNode).TreeRestructuring = false;
                    parentNode.RaiseNodeChangedEvent(parentNode, true);
                }
            }

            if (isTopLevel)
            {
                SharedData.IsBusyGenerating = false;
            }

            _ProgressHelper.ReportProgress(50, new GenerateFilesProgress(fileCount));
            return(fileCount);
        }