当Item设置为“none”时,动态构建内容项目不会复制

时间:2012-09-19 15:08:14

标签: msbuild xna xna-4.0 content-pipeline

所以我构建了一个工具,可以动态创建一个ContentProject,然后构建它(以便它输出Xnb) 目前的问题是,如果标记为 CopyToOutputDirectory ='always'或'PreserveNewest',则应将未编译的文件复制到输出目录中。如果我们查看.contentProj那个不应该构建但应该被复制的文件的部分看起来像这样

<ItemGroup>
 <None Include="MyFile.file">
   <Name>level1</Name>
   <Importer>XmlImporter</Importer>
   <Processor>PassThroughProcessor</Processor>
   <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
 </None>

但是,我正在动态构建conntent项目,所以我需要以下代码来创建项目并添加项目

   var projectPath = Path.Combine(buildDirectory, "content.contentproj");
_projectRootElement = ProjectRootElement.Create(projectPath);

        _projectRootElement.AddImport("$(MSBuildExtensionsPath)\\Microsoft\\XNA Game Studio\\v4.0\\Microsoft.Xna.GameStudio.ContentPipeline.targets");

        _contentProject = new Project(_projectRootElement);

        _contentProject.SetProperty("XnaPlatform", "Windows");
        _contentProject.SetProperty("XnaProfile", "HiDef");
        _contentProject.SetProperty("XnaFrameworkVersion", "v4.0");
        _contentProject.SetProperty("Configuration", "Debug");
        _contentProject.SetProperty("OutputPath", _outputDirectory);

        // Register any custom importers or processors.
        foreach (string pipelineAssembly in PipelineAssemblies)
        {
            _contentProject.AddItem("Reference", pipelineAssembly);
        }

        // Hook up our custom error logger.
        _errorLogger = new ErrorLogger();
        _buildParameters = new BuildParameters(ProjectCollection.GlobalProjectCollection)
                            {
                                Loggers = new ILogger[] { _errorLogger,  }
                            };
//.... removed code that is not required the following is code that adds each item to the project. In case of items that shoulndt compile I m using None (as in xml from project above)

        var itemType = compile ? "Compile" : "None";

        var items = _contentProject.AddItem(itemType, filename);
        var item = items.SingleOrDefault(x => x.EvaluatedInclude.Equals(filename, StringComparison.InvariantCultureIgnoreCase));

        item.SetMetadataValue("Link", Path.GetFileName(filename));
        item.SetMetadataValue("Name", Path.GetFileNameWithoutExtension(filename));

        if (!compile)
            item.SetMetadataValue("CopyToOutputDirectory", "Always");

最后是构建代码

BuildManager.DefaultBuildManager.BeginBuild(_buildParameters);

        var request = new BuildRequestData(_contentProject.CreateProjectInstance(), new string[0]);

        var submission = BuildManager.DefaultBuildManager.PendBuildRequest(request);

        var execute = Task.Factory.StartNew(() => submission.ExecuteAsync(null, null), cancellationTokenSource.Token);
        var endBuild = execute.ContinueWith(ant => BuildManager.DefaultBuildManager.EndBuild());

        endBuild.Wait();

在BuildRequest中,空数组采用目标参数...我已经经历了许多不同的目标,这些目标做了不同的事情,从构建但没有真正输出文件到将依赖dll复制到主文件夹但不是我需要的

干杯

0 个答案:

没有答案