Exemplo n.º 1
0
        public void parse_into_two_files()
        {
            var template = ResourceLoader.Current.GetEmbeddedResourceString(Assembly.GetExecutingAssembly(), "tempfilename.template.cs");
            var parsed   = RegenFileTemplateEngine.Compile(template);

            parsed.Count.Should().Be(2);
            parsed[0].Path.Should().Contain("INT");
            parsed[0].Content.Should().StartWith("//Generated").And.NotContainAll("__1__", "__2__");

            parsed[1].Path.Should().Contain("FLOAT");
            parsed[1].Content.Should().StartWith("//Generated").And.NotContain("__1__", "__2__");
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void Execute(object sender, EventArgs _)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // open the file in a VS code window and activate the pane
            DTE      dte = Package.GetGlobalService(typeof(DTE)) as DTE;
            Document doc = dte?.ActiveDocument;

            TextDocument templateDoc = doc?.Object() as TextDocument;

            if (dte == null || doc == null || templateDoc == null)
            {
                SystemSounds.Beep.Play();
                return;
            }

            var templatePath    = doc.FullName;
            var templateContent = templateDoc.GetText().Replace("\r", "");

            //compile outputs
            var outputs = RegenFileTemplateEngine.Compile(templateContent, templatePath);

            //find out if these files exist
            var projitem = doc.ProjectItem.ContainingProject;

            Logger.Log(projitem.FullName);
            Logger.Log(projitem.FileName);
            var solution = Package.GetGlobalService(typeof(IVsSolution)) as IVsSolution;
            var projects = GetProjects(solution);

            Project            targetProject = null;
            List <ProjectItem> targetItems   = null;

            //get current project and its files
            foreach (Project project in projects)
            {
                if (!projitem.FullName.Equals(project.FullName))
                {
                    continue;
                }
                targetProject = project;
                Logger.Log(project.FullName);
                targetItems = new List <ProjectItem>(project.ProjectItems.Cast <ProjectItem>());
                foreach (ProjectItem projectItem in targetItems)
                {
                    Logger.Log("    " + projectItem.Name);
                }

                break;
            }

            if (targetProject == null || targetItems == null)
            {
                throw new Exception("ERR #0010");
            }

            //every output either write file or replace contents.
            foreach ((string path, string content) in outputs)
            {
                //we compile regen frames
                var output = RegenEngine.Compile(content);

                //we add the compiled file to solution
                Directory.CreateDirectory(Path.GetDirectoryName(path));
                File.WriteAllText(path, output, Encoding.UTF8);
                AddFile(targetProject, path);
            }

            //now after we successfully outputted these files
            //we need to compile the files



            // now set the cursor to the beginning of the function
            //textSelection.MoveToPoint(function.StartPoint);
            void Message(string msg)
            {
                // Show a message box to prove we were here
                VsShellUtilities.ShowMessageBox(
                    this.package,
                    msg, //$"index: {pt.AbsoluteCharOffset}, path: {doc.FullName}\n+{textSelection.Text.Length}",
                    "Regen",
                    OLEMSGICON.OLEMSGICON_INFO,
                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
        }