static string GenerateSteticCodeStructure(DotNetProject project, Stetic.ProjectItemInfo item, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
        {
            // Generate a class which contains fields for all bound widgets of the component

            string name     = item != null ? item.Name : component.Name;
            string fileName = GetBuildCodeFileName(project, name);

            string ns = "";
            int    i  = name.LastIndexOf('.');

            if (i != -1)
            {
                ns   = name.Substring(0, i);
                name = name.Substring(i + 1);
            }

            if (saveToFile && !overwrite && File.Exists(fileName))
            {
                return(fileName);
            }

            if (item != null)
            {
                component = item.Component;
            }

            CodeCompileUnit cu = new CodeCompileUnit();

            if (project.UsePartialTypes)
            {
                CodeNamespace cns = new CodeNamespace(ns);
                cu.Namespaces.Add(cns);

                CodeTypeDeclaration type = new CodeTypeDeclaration(name);
                type.IsPartial      = true;
                type.Attributes     = MemberAttributes.Public;
                type.TypeAttributes = System.Reflection.TypeAttributes.Public;
                cns.Types.Add(type);
                type.Members.Add(
                    new CodeMemberMethod()
                {
                    Name = "Build"
                }
                    );

                foreach (Stetic.ObjectBindInfo binfo in component.GetObjectBindInfo())
                {
                    // When a component is being renamed, we have to generate the
                    // corresponding field using the old name, since it will be renamed
                    // later using refactory
                    string nname = args != null && args.NewName == binfo.Name ? args.OldName : binfo.Name;
                    type.Members.Add(
                        new CodeMemberField(
                            binfo.TypeName,
                            nname
                            )
                        );
                }
            }
            else
            {
                if (!saveToFile)
                {
                    return(fileName);
                }
                CodeNamespace cns = new CodeNamespace();
                cns.Comments.Add(new CodeCommentStatement("Generated code for component " + component.Name));
                cu.Namespaces.Add(cns);
            }

            CodeDomProvider provider = project.LanguageBinding.GetCodeDomProvider();

            if (provider == null)
            {
                throw new UserException("Code generation not supported for language: " + project.LanguageName);
            }

            string text;
            var    pol = project.Policies.Get <TextStylePolicy> ();

            using (var fileStream = new StringWriter()) {
                var options = new CodeGeneratorOptions()
                {
                    IndentString             = pol.TabsToSpaces? new string (' ', pol.TabWidth) : "\t",
                    BlankLinesBetweenMembers = true,
                };
                provider.GenerateCodeFromCompileUnit(cu, fileStream, options);
                text = fileStream.ToString();
                text = FormatGeneratedFile(fileName, text, project, provider);
            }
            if (saveToFile)
            {
                File.WriteAllText(fileName, text);
            }
            TypeSystemService.NotifyFileChange(fileName, text);
//
//			if (ProjectDomService.HasDom (project)) {
//				// Only update the parser database if the project is actually loaded in the IDE.
//				ProjectDomService.Parse (project, fileName, text);
//				if (saveToFile)
//					FileService.NotifyFileChanged (fileName);
//			}

            return(fileName);
        }
Beispiel #2
0
        static void GenerateSteticCodeStructure(DotNetProject project, Stetic.ProjectItemInfo item, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
        {
            // Generate a class which contains fields for all bound widgets of the component

            string name     = item != null ? item.Name : component.Name;
            string fileName = GetBuildCodeFileName(project, name);

            if (fileName == null)
            {
                return;
            }

            string ns = "";
            int    i  = name.LastIndexOf('.');

            if (i != -1)
            {
                ns   = name.Substring(0, i);
                name = name.Substring(i + 1);
            }

            if (saveToFile && !overwrite && File.Exists(fileName))
            {
                return;
            }

            if (item != null)
            {
                component = item.Component;
            }

            CodeCompileUnit cu = new CodeCompileUnit();

            if (project.UsePartialTypes)
            {
                CodeNamespace cns = new CodeNamespace(ns);
                cu.Namespaces.Add(cns);

                CodeTypeDeclaration type = new CodeTypeDeclaration(name);
                type.IsPartial      = true;
                type.Attributes     = MemberAttributes.Public;
                type.TypeAttributes = System.Reflection.TypeAttributes.Public;
                cns.Types.Add(type);

                foreach (Stetic.ObjectBindInfo binfo in component.GetObjectBindInfo())
                {
                    // When a component is being renamed, we have to generate the
                    // corresponding field using the old name, since it will be renamed
                    // later using refactory
                    string nname = args != null && args.NewName == binfo.Name ? args.OldName : binfo.Name;
                    type.Members.Add(
                        new CodeMemberField(
                            binfo.TypeName,
                            nname
                            )
                        );
                }
            }
            else
            {
                if (!saveToFile)
                {
                    return;
                }
                CodeNamespace cns = new CodeNamespace();
                cns.Comments.Add(new CodeCommentStatement("Generated code for component " + component.Name));
                cu.Namespaces.Add(cns);
            }

            CodeDomProvider provider = project.LanguageBinding.GetCodeDomProvider();

            if (provider == null)
            {
                throw new UserException("Code generation not supported for language: " + project.LanguageName);
            }

            TextWriter fileStream;

            if (saveToFile)
            {
                fileStream = new StreamWriter(fileName);
            }
            else
            {
                fileStream = new StringWriter();
            }

            try {
                provider.GenerateCodeFromCompileUnit(cu, fileStream, new CodeGeneratorOptions());
            } finally {
                fileStream.Close();
            }

            if (ProjectDomService.HasDom(project))
            {
                // Only update the parser database if the project is actually loaded in the IDE.
                if (saveToFile)
                {
                    ProjectDomService.Parse(project, fileName, "");
                    FileService.NotifyFileChanged(fileName);
                }
                else
                {
                    ProjectDomService.Parse(project, fileName, ((StringWriter)fileStream).ToString());
                }
            }
        }