Beispiel #1
0
        private bool IsAfterARename(ProjectItem projectItem)
        {
            var hier = SiteServiceProvider.GetService(typeof(IVsHierarchy)) as IVsHierarchy;

            if (hier != null)
            {
                // check if there is an item in our oldInputNames cache corresponding to the itemID.
                string oldInputName;
                var    itemId = VsUtils.GetProjectItemId(hier, projectItem);
                if (_oldInputNames.TryGetValue(itemId, out oldInputName))
                {
                    // double-check that the new filename is not equal to the old filename. If it's not,
                    // then this is a rename
                    var newName = projectItem.get_FileNames(1);
                    if (!String.IsNullOrEmpty(newName) &&
                        !newName.Equals(oldInputName))
                    {
                        _oldInputNames.Remove(itemId);
                        return(true);
                    }
                }
            }
            return(false);
        }
 /// <summary>
 /// Method to get a service by its Type
 /// </summary>
 /// <param name="serviceType">Type of service to retrieve</param>
 /// <returns>An object that implements the requested service</returns>
 protected object GetService(Type serviceType)
 {
     return(SiteServiceProvider.GetService(serviceType));
 }
 /// <summary>
 /// Method to get a service by its GUID
 /// </summary>
 /// <param name="serviceGuid">GUID of service to retrieve</param>
 /// <returns>An object that implements the requested service</returns>
 protected object GetService(Guid serviceGuid)
 {
     return(SiteServiceProvider.GetService(serviceGuid));
 }
Beispiel #4
0
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent, string defaultNamespace)
        {
            byte[] generatedBytes = null;
            var    generatedCode  = string.Empty;

            var projectItem = SiteServiceProvider.GetService(typeof(ProjectItem)) as ProjectItem;

            // Check if this is a rename operation here. The SFG gets called during a rename on website projects
            // and attempts to rollback the code-behind file if the SFG fails, but before the rename of the
            // code-behind file. We don't want to hose the existing code-behind file so we return back the bytes
            // of the previous code-behind file.
            if (projectItem != null &&
                IsAfterARename(projectItem))
            {
                var generatedCodeFile = GetCodeGenFilePathFromInputFile(inputFileName);
                if (File.Exists(generatedCodeFile))
                {
                    return(GetBytesOfExistingCodeGenFile(generatedCodeFile));
                }
            }

            // init Language
            var languageOption = LanguageOption.GenerateCSharpCode;

            try
            {
                var defaultExtension = DefaultExtensionString.ToUpperInvariant();
                if (defaultExtension.Contains(".CS"))
                {
                    languageOption = LanguageOption.GenerateCSharpCode;
                }
                else if (defaultExtension.Contains(".VB"))
                {
                    languageOption = LanguageOption.GenerateVBCode;
                }
                else
                {
                    throw new NotSupportedException(Strings.UnknownLanguage);
                }
            }
            catch (Exception e)
            {
                string commentString;
                if (languageOption == LanguageOption.GenerateVBCode)
                {
                    commentString = "' " + e.Message;
                }
                else
                {
                    commentString = "// " + e.Message;
                }
                generatedBytes = Encoding.UTF8.GetBytes(commentString);
                return(generatedBytes);
            }

            // ensure that our package is loaded
            try
            {
                PackageManager.LoadEDMPackage(SiteServiceProvider);
            }
            catch (Exception)
            {
                // It would be nice to add an error in the error list, but you need an IServiceProvider to do this
                // We use our package usually for this, but here, our pacakge failed to load.  Raise a message box
                VsUtils.ShowErrorDialog(Resources.LoadOurPackageError);

                string commentString;
                if (languageOption == LanguageOption.GenerateVBCode)
                {
                    commentString = "' " + Resources.LoadOurPackageError;
                }
                else
                {
                    commentString = "// " + Resources.LoadOurPackageError;
                }
                generatedBytes = Encoding.UTF8.GetBytes(commentString);
                return(generatedBytes);
            }

            if (InputFileHasContent(inputFileContent))
            {
                var projectItemUri = Utils.FileName2Uri(inputFileName);

                ModelManager modelManager     = PackageManager.Package.ModelManager;
                ModelManager tempModelManager = null;

                try
                {
                    // First check the ModelManager to see if the artifact was loaded by the designer or if it was persisted from an earlier code-gen attempt
                    var artifact = modelManager.GetArtifact(projectItemUri);
                    if (artifact == null)
                    {
                        tempModelManager = new EntityDesignModelManager(new EFArtifactFactory(), new VSArtifactSetFactory());
                        artifact         = tempModelManager.GetNewOrExistingArtifact(
                            projectItemUri, new StandaloneXmlModelProvider(PackageManager.Package));
                        Debug.Assert(artifact != null, "We should have created the artifact from the temporary model manager");

                        // Since we created the artifact purely for code-gen, we need to mark it as such so the designer knows not to re-use this
                        // artifact later.
                        foreach (var codeGenArtifact in artifact.ArtifactSet.Artifacts)
                        {
                            codeGenArtifact.IsCodeGenArtifact = true;
                        }
                    }

                    // First check if the 'CodeGenerationStrategy' option is set to 'Default'.
                    // If there's no or empty value, we assume 'Default'.
                    var codeGenStrategy = ModelHelper.GetDesignerPropertyValueFromArtifact(
                        OptionsDesignerInfo.ElementName, OptionsDesignerInfo.AttributeCodeGenerationStrategy, artifact);
                    if (String.IsNullOrEmpty(codeGenStrategy) ||
                        codeGenStrategy.Equals(Resources.Default))
                    {
                        // navigate to the conceptual element
                        var cModel = artifact.ConceptualModel();
                        if (cModel != null)
                        {
                            IList <EdmSchemaError> generatorErrors;

                            using (var output = new StringWriter(CultureInfo.InvariantCulture))
                            {
                                // set up namespace to use for code-gen. defaultNamespace is computed by VS to account for
                                // folder path in the project, custom tool namespace, and differences between C#, VB, and web-site projects.
                                defaultNamespace = GetCodeNamespace(defaultNamespace, artifact);

                                // generate code
                                generatorErrors =
                                    new LegacyCodeGenerationDriver(languageOption, artifact.SchemaVersion)
                                    .GenerateCode(artifact, defaultNamespace, output);

                                generatedCode = output.ToString();
                            }

                            // TODO: pass on validation to our designer so that we validate the MSL and SSDL in addition to the CSDL
                            // Insert new errors into the ErrorList window
                            ProcessErrors(generatorErrors, projectItemUri, artifact);

                            if (generatorErrors.Count > 0 ||
                                string.IsNullOrEmpty(generatedCode))
                            {
                                // We do not want to bring the error list to the front if there are code generation errors
                                // since this could interrupt the editing of the model.
                                // in case of errors generate a comment in the code which will indicate
                                // that the file failed to generate properly
                                generatedCode = GetCodeGenerationErrorComment(languageOption, inputFileName);
                            }

                            generatedBytes = Utils.StringToBytes(generatedCode, Encoding.UTF8);
                        }
                    }
                    else
                    {
                        generatedCode  = GetCodeGenerationDisabledComment(languageOption, inputFileName);
                        generatedBytes = Utils.StringToBytes(generatedCode, Encoding.UTF8);
                    }
                }
                catch (Exception e)
                {
                    GeneratorErrorCallback(false, 0, e.Message, 0, 0);
                    ErrorList.BringToFront();
                }
                finally
                {
                    if (tempModelManager != null)
                    {
                        tempModelManager.Dispose();
                        tempModelManager = null;
                    }
                }
            }
            return(generatedBytes);
        }