コード例 #1
0
        private bool CompileAndValidate(string[] files, IList <string> fileContent, out EntityToken newEntityToken)
        {
            string tempMasterFile     = GetTempFilePath(files[0]);
            string tempCodeBehindFile = null;

            string tempMasterFileContent = fileContent[0];

            if (files.Length > 1)
            {
                tempCodeBehindFile = GetTempFilePath(files[1]);

                string originalCsFileName = Path.GetFileName(files[1]);
                string newCsFileName      = Path.GetFileName(tempCodeBehindFile);

                // Fixing the refecence to the CS file in the temporary created .master file so it will point
                // to the temporary CS file. Just string.Replace, writing a sofisticated parser would be overkill

                int offset = tempMasterFileContent.IndexOf(originalCsFileName, StringComparison.OrdinalIgnoreCase);

                if (offset > 0)
                {
                    tempMasterFileContent = tempMasterFileContent.Substring(0, offset)
                                            + newCsFileName
                                            + tempMasterFileContent.Substring(offset + originalCsFileName.Length);
                }
            }

            try
            {
                File.WriteAllText(tempMasterFile, tempMasterFileContent);

                if (tempCodeBehindFile != null)
                {
                    File.WriteAllText(tempCodeBehindFile, fileContent[1]);
                }

                string virtualPath = "~" + PathUtil.GetWebsitePath(tempMasterFile);


                MasterPage masterPage;

                try
                {
                    masterPage = CompilationHelper.CompileMasterPage(virtualPath);
                }
                catch (Exception ex)
                {
                    Log.LogWarning(LogTitle, "Failed to compile master page");
                    Log.LogWarning(LogTitle, ex);

                    Exception compilationException = (ex is TargetInvocationException) ? ex.InnerException : ex;

                    // Replacing file path and temp file name from error message as it is irrelevant to the user
                    string masterFileName = Path.GetFileName(files[0]);
                    string errorMessage   = compilationException.Message;

                    if (errorMessage.StartsWith(tempMasterFile, StringComparison.OrdinalIgnoreCase))
                    {
                        errorMessage = masterFileName + errorMessage.Substring(tempMasterFile.Length);
                    }

                    ShowWarning(GetText("EditTemplate.Validation.CompilationFailed")
                                .FormatWith(errorMessage));

                    newEntityToken = null;
                    return(false);
                }

                return(Validate(masterPage, out newEntityToken));
            }
            finally
            {
                // Deleting temporary files
                File.Delete(tempMasterFile);
                if (tempCodeBehindFile != null)
                {
                    File.Delete(tempCodeBehindFile);
                }
            }
        }