简体   繁体   English

以编程方式生成Visual Studio解决方案

[英]Programmatically generate Visual Studio Solution

I want to write a wizard in .net which programmatically generates Visual Studio Solution with some projects. 我想在.net中编写一个向导,该向导以编程方式生成一些项目的Visual Studio解决方案。 I will have a XML file with details of files need to be included in the project with their respective paths to fetch them and a list of project names. 我将有一个XML文件,其中包含文件的详细信息,这些文件的详细信息需要包含在项目中,并带有它们各自的路径来获取它们以及项目名称列表。 Is there any way I can achieve this 有什么办法可以做到这一点

Check out the Text Template Transformation Toolkit (T4) . 查看文本模板转换工具包(T4) It's pretty sweet, and powers some of the file generation in ASP.NET MVC. 它非常好用,并为ASP.NET MVC中的某些文件生成提供了支持。 Scott Hanselman's got a great introductory article on the topic. 斯科特·汉瑟曼(Scott Hanselman) 撰写了一篇有关该主题的精彩介绍文章

Well the Visual Studio SDK would be your path to go. 那么Visual Studio SDK将是您的理想之路。 There are methods where you can create files and I think also projects based on project templates. 有一些方法可以创建文件,我想也可以基于项目模板进行项目。 So for instance you can create a new "asp.net mvc" project or a class library. 因此,例如,您可以创建一个新的“ asp.net mvc”项目或一个类库。 You could even create your own template. 您甚至可以创建自己的模板。

I am quite sure that you can do this only inside VS. 我很确定您只能在VS内执行此操作。

Otherwise, there is nothing wrong to generate the project and sln files be generating the xml files, thats one of the good things that they are plain xml in the end. 否则,生成项目并没有问题,并且sln文件将生成xml文件,这就是它们的最终目的是纯xml的一件好事。 You just need to generate some Projectd Guids, but I think this would be fairly simple and totally "legal". 您只需要生成一些Projected Guid,但是我认为这将非常简单并且完全“合法”。

good progress! 进步不错!

You can do this with a short bit of code or script. 您可以使用少量的代码或脚本来完成此操作。 Visual Studio will fill in most of the GUIDs for you... Visual Studio将为您填充大多数GUID。

I knocked something like this together as a one-off to collate projects. 我一次性完成了这样的事情来整理项目。 It's not 100% perfect, you might end up with duplicates from the way the code handles project names. 它不是100%完美的,您最终可能会从代码处理项目名称的方式中得到重复。 Hopefully it will show you the way. 希望它能为您指明道路。

What we do here is set up the preamble to the Solution file, then insert each solution (you need the Project type guid, seen here beginning FAE, but not the Project's own GUID, which VS will insert on saving the Solution file). 我们在这里要做的是设置解决方案文件的序言,然后插入每个解决方案(您需要Project类型guid(在此处以FAE开头,而不是Project自己的GUID,VS会在保存Solution文件时插入该GUID)。 There's a bit more boilerplate then we insert the build configuration for each project. 还有更多样板,然后我们为每个项目插入构建配置。 I had about 12 configurations for each project (different Release and Debug settings) but I've condensed it here to two. 对于每个项目,我大约有12种配置(不同的Release和Debug设置),但在这里将其简化为两个。

static void Main(string[] args)
     {
        if(args.Count() != 2)
        {
            Usage();
            return;
        }

        var rootDir = args[0];
        var output = args[1];

        var files = Directory.EnumerateFiles(rootDir, 
                                             "*.*proj", 
                                             SearchOption.AllDirectories);

        var configs = new StringBuilder();

        var configDefs = new string[]{
        ".Debug|Any CPU.ActiveCfg = Debug|Any CPU",
        ".Release|Any CPU.ActiveCfg = Release|Any CPU",
        "Other_configurations_see_solution_for_how"
        };

        using(var sw = new StreamWriter(output))
        {
            sw.WriteLine(Resources.Head);
            foreach(var file in files)
            {
                var relpath = file.Substring(rootDir.Length + 1);
                var split= relpath.Split('\\');
                var name = split[0];
                var path = relpath;
                sw.WriteLine("Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{0}\", \"{1}\", \"{0}\"", name, path);
                sw.WriteLine("EndProject");

                foreach(var configDef in configDefs)
                {
                    configs.AppendLine(string.Format("{0}{1}", file, configDef));
                }
            }

            sw.WriteLine(@"Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
    Debug|Any CPU = Debug|Any CPU       
    Release|Any CPU = Release|Any CPU
    ...Other_configurations_see_solution_for_how...
EndGlobalSection 
    GlobalSection(ProjectConfigurationPlatforms) = postSolution");

            sw.WriteLine(configs.ToString());
            sw.WriteLine(Resources.Tail);
        }
    }

Head looks a bit like: 头看起来有点像:

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010

But I think there are control characters in the first line - beware! 但是我认为第一行中有控制字符-当心!

Tail looks like 尾巴看起来像

    GlobalSection(SolutionProperties) = preSolution
    HideSolutionNode = FALSE
    EndGlobalSection
EndGlobal

We built a tool called Tree Surgeon that does exactly this. 我们构建了一个名为Tree Surgeon的工具来完成此任务。 Things are under review and discussion to revieve (or ditch it) as times have changed since it was originally created but it does what you're asking here (and is open source). 自从它最初创建以来,随着时间的推移,事情正在审查和讨论,以审查(或放弃它),但是它可以满足您在这里的要求(并且是开源的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual Studio中以编程方式重新加载解决方案 - Programmatically reload solution in Visual Studio 以编程方式升级Visual Studio解决方案和项目文件 - Upgrade Visual Studio solution and project files programmatically 如何以编程方式保存Visual Studio解决方案? - How to programmatically save Visual Studio solution? 以编程方式将多个项目添加到Visual Studio解决方案中? - Add multiple projects programmatically into Visual Studio solution? 如何以编程方式在Visual Studio项目中生成新的Visual Studio项目? - How to Generate a New Visual Studio Project in a Visual Studio Project Programmatically? 如何以编程方式在Visual Studio解决方案/程序集中加载文件? - How do I load a file in a Visual Studio solution/assembly programmatically? 以编程方式获取与Visual Studio解决方案文件(* .sln)中的GUID相匹配的Assembly GUID - Getting Assembly GUID programmatically that matches that in Visual studio solution file (*.sln) 序列化和反序列化Visual Studio解决方案文件 - 或以编程方式编辑? - Serialize and Deserialize Visual Studio Solution files - or programmatically edit? 从外接程序以编程方式获取当前Visual Studio IDE解决方案目录 - Programmatically getting the current Visual Studio IDE solution directory from addins 如何在已经运行的Visual Studio中以编程方式打开项目/解决方案? - How to programmatically open project/solution in already running Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM