【问题标题】:How to add generated files to multiple projects using CodeSmith tools如何使用 CodeSmith 工具将生成的文件添加到多个项目
【发布时间】:2010-06-03 19:15:48
【问题描述】:

我正在我当前的项目中使用 CodeSmith,我正在尝试找出一个问题。对于我的 CodeSmith 项目 (.csp),我可以选择一个选项,让它自动将所有生成的文件添加到当前项目 (.csproj)。但我希望能够将输出添加到多个项目(.csproj)。 CodeSmith 内部是否有允许这样做的选项?还是有一种以编程方式做到这一点的好方法?

谢谢。

【问题讨论】:

    标签: visual-studio-2008 codesmith


    【解决方案1】:

    我无法找到让 CodeSmith 自动处理此问题的方法,因此我最终在 Code Behind 文件中编写了一个自定义方法来处理此问题。

    几点说明: - proj 文件是 XML,因此相当容易编辑,但保存项目中包含的文件列表的实际“ItemGroup”节点实际上并未以任何特殊方式标记。我最终选择了具有“包含”子节点的“ItemGroup”节点,但可能有更好的方法来确定应该使用哪个。 - 我建议一次更改所有 proj 文件,而不是创建/更新每个文件。否则,如果您从 Visual Studio 启动生成,您可能会收到大量“此项目已更改,您想重新加载” - 如果您的文件受源代码控制(它们是,对吗?!),您将需要处理签出文件并将它们添加到源代码控制以及编辑 proj 文件。

    这是(或多或少)我用来向项目添加文件的代码:

    /// <summary>
    /// Adds the given file to the indicated project
    /// </summary>
    /// <param name="project">The path of the proj file</param>
    /// <param name="projectSubDir">The subdirectory of the project that the 
    /// file is located in, otherwise an empty string if it is at the project root</param>
    /// <param name="file">The name of the file to be added to the project</param>
    /// <param name="parent">The name of the parent to group the file to, an 
    /// empty string if there is no parent file</param>
    public static void AddFileToProject(string project, string projectSubDir, 
            string file, string parent)
    {
        XDocument proj = XDocument.Load(project);
    
        XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
        var itemGroup = proj.Descendants(ns + "ItemGroup").FirstOrDefault(x => x.Descendants(ns + "Compile").Count() > 0);
    
        if (itemGroup == null)
            throw new Exception(string.Format("Unable to find an ItemGroup to add the file {1} to the {0} project", project, file));
    
        //If the file is already listed, don't bother adding it again
        if(itemGroup.Descendants(ns + "Compile").Where(x=>x.Attribute("Include").Value.ToString() == file).Count() > 0)
            return; 
    
        XElement item = new XElement(ns + "Compile", 
                        new XAttribute("Include", Path.Combine(projectSubDir,file)));
    
        //This is used to group files together, in this case the file that is 
        //regenerated is grouped as a dependent of the user-editable file that
        //is not changed by the code generator
        if (string.IsNullOrEmpty(parent) == false)
            item.Add(new XElement(ns + "DependentUpon", parent));
    
        itemGroup.Add(item);
    
        proj.Save(project); 
    
    }
    

    【讨论】:

    • 您好,我们一直在考虑是否可以在 CodeSmith 中添加这样的功能。您如何在模板中执行此代码?谢谢 -Blake Niemyjski
    • 在我的模板中我调用了我的助手类,它看起来像这样: if(created == false) { Helper.AddFileToProject(ModelProject, string.Empty, fileName, parentName); }
    【解决方案2】:

    您是否考虑过只编译成一个共享程序集 (DLL),然后您的所有项目都可以引用该程序集?

    我知道这可能不符合您的要求,但我认为这将是实现您的所有项目都可以使用的单一源代码的最佳方法之一,并且只有一个代码库可以根据需要进行维护。

    【讨论】:

    • 不幸的是,这对我的情况不起作用,但不是一个糟糕的潜在解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2015-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多