Esempio n. 1
0
        protected override byte[] GenerateCode(string defaultNamespace, string inputFileName)
        {
            byte[] resultBytes = null;
            BE.Project project = null;

            if (Project != null)
            {
                if (!Project.Saved)
                {
                    //if (!String.IsNullOrEmpty(Project.FileName))
                    Project.Save(String.Empty);
                }

                project = BE.Engine.GlobalEngine.GetLoadedProject(Project.FullName);
                if (project == null)
                {
                    project = new BE.Project(BE.Engine.GlobalEngine);
                    try
                    {
                        project.Load(Project.FullName);
                    }
                    catch (Exception ex)
                    {
                        WriteLine(ex.ToString());
                    }
                }
            }

            if (project == null)
                project = new Microsoft.Build.BuildEngine.Project(BE.Engine.GlobalEngine);

            GeneratorArguments arguments = new GeneratorArguments(false, inputFileName, defaultNamespace,
                                                                  new MsBuildProject(project));
            arguments.OutputMessage += base.WriteLine;

            using (CmdToolBuilder builder = new CmdToolBuilder())
                builder.Generate(arguments);

            GeneratorArguments.OutputFile primaryFile;
            foreach (GeneratorArguments.OutputFile file in arguments.GetOutput(out primaryFile))
            {
                try
                {
                    if (file.AddToProject)
                        AddProjectFile(file.FileName);
                }
                catch (Exception ex)
                {
                    arguments.WriteError(0, ex.ToString());
                }
            }

            if (primaryFile != null)
            {
                string testPrefix = Path.ChangeExtension(Path.GetFullPath(inputFileName), ".");
                _lastGeneratedExtension = primaryFile.FileName.Substring(testPrefix.Length - 1);
                resultBytes = Encoding.UTF8.GetBytes(File.ReadAllText(primaryFile.FileName));
            }

            if (arguments.DisplayHelp)
            {
                string file = Path.Combine(Path.GetTempPath(), "CmdTool - Help.txt");
                File.WriteAllText(file, arguments.Help());
                try
                {
                    DTE.Documents.Open(file, "Auto", true);
                }
                catch
                {
                    System.Diagnostics.Process.Start(file);
                }

                //EnvDTE.Window window = DTE.ItemOperations.NewFile(@"General\Text File", "CmdTool - Help.txt", Guid.Empty.ToString("B"));
                //EnvDTE.TextSelection sel = window.Selection as EnvDTE.TextSelection;
                //if (sel != null)
                //{
                //    sel.Text = arguments.ReplaceVariables("$(help)");
                //}
            }

            return resultBytes;
        }
Esempio n. 2
0
        protected override byte[] GenerateCode(string defaultNamespace, string inputFileName)
        {
            byte[]     resultBytes = null;
            BE.Project project     = null;

            if (Project != null)
            {
                if (!Project.Saved)
                {
                    //if (!String.IsNullOrEmpty(Project.FileName))
                    Project.Save(String.Empty);
                }

                project = BE.Engine.GlobalEngine.GetLoadedProject(Project.FullName);
                if (project == null)
                {
                    project = new BE.Project(BE.Engine.GlobalEngine);
                    try
                    {
                        project.Load(Project.FullName);
                    }
                    catch (Exception ex)
                    {
                        WriteLine(ex.ToString());
                    }
                }
            }

            if (project == null)
            {
                project = new Microsoft.Build.BuildEngine.Project(BE.Engine.GlobalEngine);
            }

            GeneratorArguments arguments = new GeneratorArguments(false, inputFileName, defaultNamespace,
                                                                  new MsBuildProject(project));

            arguments.OutputMessage += base.WriteLine;

            using (CmdToolBuilder builder = new CmdToolBuilder())
                builder.Generate(arguments);

            GeneratorArguments.OutputFile primaryFile;
            foreach (GeneratorArguments.OutputFile file in arguments.GetOutput(out primaryFile))
            {
                try
                {
                    if (file.AddToProject)
                    {
                        AddProjectFile(file.FileName);
                    }
                }
                catch (Exception ex)
                {
                    arguments.WriteError(0, ex.ToString());
                }
            }

            if (primaryFile != null)
            {
                string testPrefix = Path.ChangeExtension(Path.GetFullPath(inputFileName), ".");
                _lastGeneratedExtension = primaryFile.FileName.Substring(testPrefix.Length - 1);
                resultBytes             = Encoding.UTF8.GetBytes(File.ReadAllText(primaryFile.FileName));
            }

            if (arguments.DisplayHelp)
            {
                string file = Path.Combine(Path.GetTempPath(), "CmdTool - Help.txt");
                File.WriteAllText(file, arguments.Help());
                try
                {
                    DTE.Documents.Open(file, "Auto", true);
                }
                catch
                {
                    System.Diagnostics.Process.Start(file);
                }

                //EnvDTE.Window window = DTE.ItemOperations.NewFile(@"General\Text File", "CmdTool - Help.txt", Guid.Empty.ToString("B"));
                //EnvDTE.TextSelection sel = window.Selection as EnvDTE.TextSelection;
                //if (sel != null)
                //{
                //    sel.Text = arguments.ReplaceVariables("$(help)");
                //}
            }

            return(resultBytes);
        }
        protected override byte[] GenerateCode(string defaultNamespace, string inputFileName)
        {
            var Project = this.Project;

            byte[]      resultBytes = null;
            FauxProject project     = null;

            if (Project != null)
            {
                if (!Project.Saved)
                {
                    //if (!String.IsNullOrEmpty(Project.FileName))
                    Project.Save(String.Empty);
                }

                project = new FauxProject(Project.FullName);
            }

            if (project == null)
            {
                project = FauxProject.CreateEmpty(inputFileName);
            }

            GeneratorArguments arguments = new GeneratorArguments(false, inputFileName, defaultNamespace, project);

            arguments.OutputMessage += base.WriteLine;

            if (Project != null)
            {
                try
                {
                    if (File.Exists(DTE.Solution.FileName))
                    {
                        arguments.SolutionDir = Path.GetDirectoryName(DTE.Solution.FileName);
                    }
                }
                catch { }
            }

            using (CmdToolBuilder builder = new CmdToolBuilder())
                builder.Generate(arguments);

            var addToProject = new List <string>();

            GeneratorArguments.OutputFile primaryFile;
            foreach (GeneratorArguments.OutputFile file in arguments.GetOutput(out primaryFile))
            {
                try
                {
                    if (file.AddToProject && File.Exists(file.FileName))
                    {
                        addToProject.Add(file.FileName);
                    }
                }
                catch (Exception ex)
                {
                    arguments.WriteError(0, ex.ToString());
                }
            }

            string primaryFileName = null;

            if (primaryFile != null)
            {
                string testPrefix = Path.ChangeExtension(Path.GetFullPath(inputFileName), ".");
                _lastGeneratedExtension = primaryFile.FileName.Substring(testPrefix.Length - 1);
                resultBytes             = Encoding.UTF8.GetBytes(File.ReadAllText(primaryFile.FileName));
                addToProject.Add(primaryFile.FileName);
                primaryFileName = Path.GetFileName(primaryFile.FileName);
            }

            AddFilesToProject(addToProject.ToArray(), primaryFileName);

            try
            {
                string actualOutFile;
                if (Project != null && primaryFileName != null && !IsLastGenOutputValid(primaryFileName, out actualOutFile))
                {
                    Project.Save(String.Empty);
                    // Complete hack, we don't have the ability to edit these values...
                    string projectText = File.ReadAllText(project.FullFileName);
                    projectText = projectText.Replace(
                        "<LastGenOutput>" + actualOutFile + "</LastGenOutput>",
                        "<LastGenOutput>" + primaryFileName + "</LastGenOutput>");

                    arguments.WriteError(
                        0,
                        "The project item has an incorrect value for LastGenOutput, expected \"{0}\" found \"{1}\".\r\n" +
                        "Press 'Discard' to correct or unload and edit the project.",
                        primaryFileName, actualOutFile
                        );

                    File.WriteAllText(project.FullFileName, projectText);
                }
            }
            catch
            { /* Ignore */ }

            if (arguments.DisplayHelp)
            {
                string file = Path.Combine(Path.GetTempPath(), "CmdTool - Help.txt");
                using (var wtr = new StreamWriter(file))
                {
                    wtr.WriteLine(arguments.Help());
                    wtr.WriteLine("ENVIRONMENT:");
                    foreach (System.Collections.DictionaryEntry variable in Environment.GetEnvironmentVariables())
                    {
                        wtr.WriteLine("{0} = '{1}'", variable.Key, variable.Value);
                    }
                    wtr.Flush();
                }

                try
                {
                    DTE.Documents.Open(file, "Auto", true);
                }
                catch
                {
                    System.Diagnostics.Process.Start(file);
                }

                //EnvDTE.Window window = DTE.ItemOperations.NewFile(@"General\Text File", "CmdTool - Help.txt", Guid.Empty.ToString("B"));
                //EnvDTE.TextSelection sel = window.Selection as EnvDTE.TextSelection;
                //if (sel != null)
                //{
                //    sel.Text = arguments.ReplaceVariables("$(help)");
                //}
            }

            return(resultBytes);
        }
        protected override byte[] GenerateCode(string defaultNamespace, string inputFileName)
        {
            var Project = this.Project;
            byte[] resultBytes = null;
            FauxProject project = null;

            if (Project != null)
            {
                if (!Project.Saved)
                {
                    //if (!String.IsNullOrEmpty(Project.FileName))
                    Project.Save(String.Empty);
                }

                project = new FauxProject(Project.FullName);
            }

            if (project == null)
                project = FauxProject.CreateEmpty(inputFileName);

            GeneratorArguments arguments = new GeneratorArguments(false, inputFileName, defaultNamespace, project);
            arguments.OutputMessage += base.WriteLine;

            if (Project != null)
            {
                try
                {
                    if (File.Exists(DTE.Solution.FileName))
                        arguments.SolutionDir = Path.GetDirectoryName(DTE.Solution.FileName);
                }
                catch { }
            }

            using (CmdToolBuilder builder = new CmdToolBuilder())
                builder.Generate(arguments);

            var addToProject = new List<string>();
            GeneratorArguments.OutputFile primaryFile;
            foreach (GeneratorArguments.OutputFile file in arguments.GetOutput(out primaryFile))
            {
                try
                {
                    if (file.AddToProject && File.Exists(file.FileName))
                        addToProject.Add(file.FileName);
                }
                catch (Exception ex)
                {
                    arguments.WriteError(0, ex.ToString());
                }
            }

            string primaryFileName = null;
            if (primaryFile != null)
            {
                string testPrefix = Path.ChangeExtension(Path.GetFullPath(inputFileName), ".");
                _lastGeneratedExtension = primaryFile.FileName.Substring(testPrefix.Length - 1);
                resultBytes = Encoding.UTF8.GetBytes(File.ReadAllText(primaryFile.FileName));
                addToProject.Add(primaryFile.FileName);
                primaryFileName = Path.GetFileName(primaryFile.FileName);
            }

            AddFilesToProject(addToProject.ToArray(), primaryFileName);

            try
            {
                string actualOutFile;
                if (Project != null && primaryFileName != null && !IsLastGenOutputValid(primaryFileName, out actualOutFile))
                {
                    Project.Save(String.Empty);
                    // Complete hack, we don't have the ability to edit these values...
                    string projectText = File.ReadAllText(project.FullFileName);
                    projectText = projectText.Replace(
                        "<LastGenOutput>" + actualOutFile + "</LastGenOutput>",
                        "<LastGenOutput>" + primaryFileName + "</LastGenOutput>");

                    arguments.WriteError(
                        0,
                        "The project item has an incorrect value for LastGenOutput, expected \"{0}\" found \"{1}\".\r\n" +
                        "Press 'Discard' to correct or unload and edit the project.",
                        primaryFileName, actualOutFile
                        );

                    File.WriteAllText(project.FullFileName, projectText);
                }
            }
            catch
            { /* Ignore */ }

            if (arguments.DisplayHelp)
            {
                string file = Path.Combine(Path.GetTempPath(), "CmdTool - Help.txt");
                using (var wtr = new StreamWriter(file))
                {
                    wtr.WriteLine(arguments.Help());
                    wtr.WriteLine("ENVIRONMENT:");
                    foreach (System.Collections.DictionaryEntry variable in Environment.GetEnvironmentVariables())
                        wtr.WriteLine("{0} = '{1}'", variable.Key, variable.Value);
                    wtr.Flush();
                }

                try
                {
                    DTE.Documents.Open(file, "Auto", true);
                }
                catch
                {
                    System.Diagnostics.Process.Start(file);
                }

                //EnvDTE.Window window = DTE.ItemOperations.NewFile(@"General\Text File", "CmdTool - Help.txt", Guid.Empty.ToString("B"));
                //EnvDTE.TextSelection sel = window.Selection as EnvDTE.TextSelection;
                //if (sel != null)
                //{
                //    sel.Text = arguments.ReplaceVariables("$(help)");
                //}
            }

            return resultBytes;
        }