private void SetupLoader(IScript script)
        {
            SetupLoader();

            object[] p = new object[] { script };
            Expect
            .Call(loader.CallTemplateFunction("Class", ref p))
            .Return("public class SomeClass { }")
            .Repeat.Any();
            Expect
            .Call(loader.GetTemplateFunctionLanguage(null))
            .Return("Plain Text")
            .IgnoreArguments()
            .Repeat.Any();
        }
Exemplo n.º 2
0
        private bool ProcessScriptObject(object scriptObject, string folderName, IScript script, ProjectFileTreeNode parentNode)
        {
            bool   success    = true;
            string scriptName = UpdateScriptName(scriptObject, script);
            string fileName   = Path.Combine(folderName, scriptName);

            _Loader.SetGeneratedFileNameOnTemplate(Path.Combine(_Project.ProjectSettings.OutputPath, fileName));

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

                try
                {
                    // Reset the SkipCurrentFile and CurrentFileName variable
                    _Loader.CallTemplateFunction(TemplateHelper.ResetSkipCurrentFileFunctionName);
                    _Loader.CallTemplateFunction(TemplateHelper.ResetCurrentFileNameFunctionName);
                    // Call the script file function to get the file text body
                    object[] parameters = new[] { scriptObject };
                    // Check whether we must skip the current file
                    string templateOutput = (string)_Loader.CallTemplateFunction(script.ScriptName, ref parameters);

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

                    bool skipCurrentFile = (bool)_Loader.CallTemplateFunction(TemplateHelper.GetSkipCurrentFileFunctionName);

                    if (!skipCurrentFile)
                    {
                        templateOutput = Utility.StandardizeLineBreaks(templateOutput, Utility.LineBreaks.Windows);

                        string codeInsertionFilename =
                            (string)_Loader.CallTemplateFunction(TemplateHelper.GetCurrentFileNameFunctionName);

                        if (string.IsNullOrEmpty(codeInsertionFilename))
                        {
                            string fullPath = Path.Combine(absoluteBasePath, fileName);
                            _FileController.WriteAllText(fullPath, templateOutput);

                            // The file has successfully been written - add it to the GeneratedFiles
                            _Project.AddGeneratedFile(new GeneratedFile(script.FileName, fullPath, fileName, script.ScriptName, script.IteratorName));

                            // Set the NewGen text to point to that file.
                            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;
                                }
                            }

                            if (addToProjectFileTree)
                            {
                                parentNode.AddChildNode(fileInfo, scriptName);
                            }
                        }
                        else
                        {
                            // Code insertions were performed. Need to update a node in the tree.
                            // expand the path
                            if (Path.IsPathRooted(codeInsertionFilename) == false)
                            {
                                codeInsertionFilename = RelativePaths.RelativeToAbsolutePath(absoluteBasePath, codeInsertionFilename);
                            }
                            codeInsertionFilename = _FileController.GetFullPath(codeInsertionFilename);

                            // Get the relative path
                            string relativeCodeInsertionFilename = RelativePaths.GetRelativePath(_Project.ProjectSettings.OutputPath, codeInsertionFilename);

                            // If the file is not under the output path, then reset it to the full path
                            if (relativeCodeInsertionFilename.StartsWith(".."))
                            {
                                relativeCodeInsertionFilename = codeInsertionFilename;

                                // We need to add its folder as a root node of the tree
                                if (!parentNode.IsTreeRoot)
                                {
                                    parentNode = parentNode.ParentTree;
                                }
                            }

                            fileInfo.RelativeFilePath = relativeCodeInsertionFilename;
                            string relPathStart = "." + Path.DirectorySeparatorChar;

                            if (fileInfo.RelativeFilePath.StartsWith(relPathStart))
                            {
                                fileInfo.RelativeFilePath = fileInfo.RelativeFilePath.Substring(2);
                            }

                            if (addToProjectFileTree)
                            {
                                ProjectFileTree tree;

                                if (!parentNode.IsTreeRoot)
                                {
                                    tree = parentNode.ParentTree;
                                }
                                else
                                {
                                    tree = (ProjectFileTree)parentNode;
                                }

                                var possibleNode = tree.GetNodeAtPath(codeInsertionFilename);

                                if (possibleNode == null)
                                {
                                    // Need to create this node.


                                    // Create the node and it's parent folders if need be
                                    var node = tree.CreateFileNodeForPath(relativeCodeInsertionFilename);
                                    node.AssociatedFile = fileInfo;
                                }
                                else
                                {
                                    // Update the NewGen text, don't add it to the generated files list.
                                    fileInfo = possibleNode.AssociatedFile as TextFileInformation;
                                    if (fileInfo == null)
                                    {
                                        throw new NotSupportedException("Cannot code merge a binary file");
                                    }
                                }
                            }
                            else
                            {
                                // Write the file to disk
                                _FileController.WriteAllText(codeInsertionFilename, templateOutput);
                            }

                            fileInfo.NewGenFile = new TextFile(templateOutput);
                        }

                        AddFileCountToPreviousEventAndRefire(_ProgressHelper, 1);
                    }
                }
                catch (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.
                    if (addToProjectFileTree)
                    {
                        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;
                    }

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