本文整理汇总了C#中Microsoft.Build.BuildEngine.Project.AddNewItem方法的典型用法代码示例。如果您正苦于以下问题:C# Project.AddNewItem方法的具体用法?C# Project.AddNewItem怎么用?C# Project.AddNewItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Build.BuildEngine.Project
的用法示例。
在下文中一共展示了Project.AddNewItem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
/// <summary>
/// Executes this instance.
/// </summary>
public override bool Execute() {
foreach (var projectTaskItem in this.Projects) {
var project = new Project();
project.Load(projectTaskItem.ItemSpec);
foreach (var projectItem in this.Items) {
string itemType = projectItem.GetMetadata("ItemType");
if (string.IsNullOrEmpty(itemType)) {
itemType = "None";
}
BuildItem newItem = project.AddNewItem(itemType, projectItem.ItemSpec, false);
var customMetadata = projectItem.CloneCustomMetadata();
foreach (DictionaryEntry entry in customMetadata) {
string value = (string)entry.Value;
if (value.Length > 0) {
newItem.SetMetadata((string)entry.Key, value);
}
}
}
project.Save(projectTaskItem.ItemSpec);
}
return !this.Log.HasLoggedErrors;
}
示例2: Execute
public override bool Execute() {
if (this.ProjectReferences.Length != this.References.Length) {
this.Log.LogError("ProjectReferences and References arrays do not have matching lengths.");
}
foreach (var project in Projects) {
Project doc = new Project();
doc.Load(project.ItemSpec);
var projectReferences = doc.EvaluatedItems.OfType<BuildItem>().Where(item => item.Name == "ProjectReference");
var matchingReferences = from reference in projectReferences
join refToRemove in this.ProjectReferences on reference.Include equals refToRemove.ItemSpec
let addIndex = Array.IndexOf(this.ProjectReferences, refToRemove)
select new { Remove = reference, Add = this.References[addIndex] };
foreach (var matchingReference in matchingReferences) {
this.Log.LogMessage("Removing project reference to \"{0}\" from \"{1}\".", matchingReference.Remove.Include, project.ItemSpec);
doc.RemoveItem(matchingReference.Remove);
if (matchingReference.Add.ItemSpec != "REMOVE") {
this.Log.LogMessage("Adding assembly reference to \"{0}\" to \"{1}\".", matchingReference.Add.ItemSpec, project.ItemSpec);
var newReference = doc.AddNewItem("Reference", Path.GetFileNameWithoutExtension(matchingReference.Add.ItemSpec), true);
newReference.SetMetadata("HintPath", matchingReference.Add.ItemSpec);
}
}
doc.Save(project.ItemSpec);
}
return true;
}
示例3: Execute
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns></returns>
public override bool Execute() {
foreach (ITaskItem projectTaskItem in this.Projects) {
this.Log.LogMessage("Fixing up the {0} sample for shipping as source code.", Path.GetFileNameWithoutExtension(projectTaskItem.ItemSpec));
var project = new Project();
Uri projectUri = new Uri(projectTaskItem.GetMetadata("FullPath"));
project.Load(projectTaskItem.ItemSpec, ProjectLoadSettings.IgnoreMissingImports);
if (this.RemoveImportsStartingWith != null && this.RemoveImportsStartingWith.Length > 0) {
project.Imports.Cast<Import>()
.Where(import => this.RemoveImportsStartingWith.Any(start => import.ProjectPath.StartsWith(start, StringComparison.OrdinalIgnoreCase)))
.ToList()
.ForEach(import => project.Imports.RemoveImport(import));
}
if (this.AddReferences != null) {
foreach (var reference in this.AddReferences) {
BuildItem item = project.AddNewItem("Reference", reference.ItemSpec);
foreach (DictionaryEntry metadata in reference.CloneCustomMetadata()) {
item.SetMetadata((string)metadata.Key, (string)metadata.Value);
}
}
}
project.Save(projectTaskItem.ItemSpec);
}
return !this.Log.HasLoggedErrors;
}
示例4: Execute
/// <summary>
/// Executes this instance.
/// </summary>
public override bool Execute() {
foreach (ITaskItem taskItem in this.Projects) {
switch (GetClassification(taskItem)) {
case ProjectClassification.VS2010Project:
this.Log.LogMessage(MessageImportance.Low, "Downgrading project \"{0}\".", taskItem.ItemSpec);
Project project = new Project();
project.Load(taskItem.ItemSpec);
project.DefaultToolsVersion = "3.5";
if (this.DowngradeMvc2ToMvc1) {
string projectTypeGuids = project.GetEvaluatedProperty("ProjectTypeGuids");
if (!string.IsNullOrEmpty(projectTypeGuids)) {
projectTypeGuids = projectTypeGuids.Replace("{F85E285D-A4E0-4152-9332-AB1D724D3325}", "{603c0e0b-db56-11dc-be95-000d561079b0}");
project.SetProperty("ProjectTypeGuids", projectTypeGuids);
}
}
// Web projects usually have an import that includes these substrings
foreach (Import import in project.Imports) {
import.ProjectPath = import.ProjectPath
.Replace("$(MSBuildExtensionsPath32)", "$(MSBuildExtensionsPath)")
.Replace("VisualStudio\\v10.0", "VisualStudio\\v9.0");
}
// VS2010 won't let you have a System.Core reference, but VS2008 requires it.
BuildItemGroup references = project.GetEvaluatedItemsByName("Reference");
if (!references.Cast<BuildItem>().Any(item => item.FinalItemSpec.StartsWith("System.Core", StringComparison.OrdinalIgnoreCase))) {
project.AddNewItem("Reference", "System.Core");
}
project.Save(taskItem.ItemSpec);
break;
case ProjectClassification.VS2010Solution:
this.Log.LogMessage(MessageImportance.Low, "Downgrading solution \"{0}\".", taskItem.ItemSpec);
string[] contents = File.ReadAllLines(taskItem.ItemSpec);
if (contents[1] != "Microsoft Visual Studio Solution File, Format Version 11.00" ||
contents[2] != "# Visual Studio 2010") {
this.Log.LogError("Unrecognized solution file header in \"{0}\".", taskItem.ItemSpec);
break;
}
contents[1] = "Microsoft Visual Studio Solution File, Format Version 10.00";
contents[2] = "# Visual Studio 2008";
for (int i = 3; i < contents.Length; i++) {
contents[i] = contents[i].Replace("TargetFrameworkMoniker = \".NETFramework,Version%3Dv", "TargetFramework = \"");
}
File.WriteAllLines(taskItem.ItemSpec, contents);
break;
default:
this.Log.LogWarning("Unrecognized project type for \"{0}\".", taskItem.ItemSpec);
break;
}
}
return !this.Log.HasLoggedErrors;
}
示例5: Execute
public override bool Execute() {
foreach (var project in Projects) {
Project doc = new Project();
doc.Load(project.ItemSpec);
var projectReference = doc.EvaluatedItems.OfType<BuildItem>().Where(
item => item.Name == "ProjectReference" && item.Include == ProjectReference).Single();
doc.RemoveItem(projectReference);
var newReference = doc.AddNewItem("Reference", Path.GetFileNameWithoutExtension(Reference));
newReference.SetMetadata("HintPath", Reference);
doc.Save(project.ItemSpec);
}
return true;
}
示例6: Execute
public override bool Execute() {
if (this.ProjectReferences.Length != this.References.Length) {
this.Log.LogError("ProjectReferences and References arrays do not have matching lengths.");
this.Log.LogError("ProjectReferences contents ({0} elements): {1}", this.ProjectReferences.Length, String.Join<ITaskItem>(";", this.ProjectReferences));
this.Log.LogError("References contents ({0} elements): {1}", this.References.Length, String.Join<ITaskItem>(";", this.References));
return false;
}
foreach (var project in Projects) {
Project doc = new Project();
doc.Load(project.ItemSpec, ProjectLoadSettings.IgnoreMissingImports);
var projectReferences = doc.EvaluatedItems.OfType<BuildItem>().Where(item => item.Name == "ProjectReference");
var matchingReferences = from reference in projectReferences
join refToRemove in this.ProjectReferences on reference.Include equals refToRemove.ItemSpec
let addIndex = Array.IndexOf(this.ProjectReferences, refToRemove)
select new { Remove = reference, Add = this.References[addIndex] };
foreach (var matchingReference in matchingReferences) {
this.Log.LogMessage("Removing project reference to \"{0}\" from \"{1}\".", matchingReference.Remove.Include, project.ItemSpec);
doc.RemoveItem(matchingReference.Remove);
if (matchingReference.Add.ItemSpec != "REMOVE") {
this.Log.LogMessage("Adding assembly reference to \"{0}\" to \"{1}\".", matchingReference.Add.ItemSpec, project.ItemSpec);
string newItemSpec = Path.GetFileNameWithoutExtension(matchingReference.Add.ItemSpec);
if (!doc.GetEvaluatedItemsByName("Reference").OfType<BuildItem>().Any(bi => String.Equals(bi.Include, newItemSpec, StringComparison.OrdinalIgnoreCase))) {
var newReference = doc.AddNewItem("Reference", newItemSpec, true);
newReference.SetMetadata("HintPath", matchingReference.Add.ItemSpec);
}
}
}
doc.Save(project.ItemSpec);
}
return true;
}
示例7: AddNewItemInclude_Empty
public void AddNewItemInclude_Empty()
{
Project p = new Project();
p.AddNewItem("include", String.Empty);
}
示例8: AddNewItem
public override void AddNewItem(Project project, string name, string include)
{
project.AddNewItem(name, include);
}
示例9: Execute
/// <summary>
/// Executes this instance.
/// </summary>
public override bool Execute() {
var newProjectToOldProjectMapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var createdProjectFiles = new List<TaskItem>();
foreach (ITaskItem taskItem in this.Projects) {
switch (GetClassification(taskItem)) {
case ProjectClassification.VS2010Project:
case ProjectClassification.VS2010Solution:
string projectNameForVS2008 = InPlaceDowngrade
? taskItem.ItemSpec
: Path.Combine(
Path.GetDirectoryName(taskItem.ItemSpec),
Path.GetFileNameWithoutExtension(taskItem.ItemSpec) + "-vs2008" +
Path.GetExtension(taskItem.ItemSpec));
newProjectToOldProjectMapping[taskItem.ItemSpec] = projectNameForVS2008;
break;
}
}
foreach (ITaskItem taskItem in this.Projects) {
switch (GetClassification(taskItem)) {
case ProjectClassification.VS2010Project:
this.Log.LogMessage(MessageImportance.Low, "Downgrading project \"{0}\".", taskItem.ItemSpec);
var project = new Project();
project.Load(taskItem.ItemSpec, ProjectLoadSettings.IgnoreMissingImports);
project.DefaultToolsVersion = "3.5";
if (this.DowngradeMvc2ToMvc1) {
string projectTypeGuids = project.GetEvaluatedProperty("ProjectTypeGuids");
if (!string.IsNullOrEmpty(projectTypeGuids)) {
projectTypeGuids = projectTypeGuids.Replace("{F85E285D-A4E0-4152-9332-AB1D724D3325}", "{603c0e0b-db56-11dc-be95-000d561079b0}");
project.SetProperty("ProjectTypeGuids", projectTypeGuids);
}
}
// MSBuild v3.5 doesn't support the GetDirectoryNameOfFileAbove function
var enlistmentInfoImports = project.Imports.Cast<Import>().Where(i => i.ProjectPath.IndexOf("[MSBuild]::GetDirectoryNameOfFileAbove", StringComparison.OrdinalIgnoreCase) >= 0);
enlistmentInfoImports.ToList().ForEach(i => project.Imports.RemoveImport(i));
// Web projects usually have an import that includes these substrings));)
foreach (Import import in project.Imports) {
import.ProjectPath = import.ProjectPath
.Replace("$(MSBuildExtensionsPath32)", "$(MSBuildExtensionsPath)")
.Replace("VisualStudio\\v10.0", "VisualStudio\\v9.0");
}
// VS2010 won't let you have a System.Core reference, but VS2008 requires it.
BuildItemGroup references = project.GetEvaluatedItemsByName("Reference");
if (!references.Cast<BuildItem>().Any(item => item.FinalItemSpec.StartsWith("System.Core", StringComparison.OrdinalIgnoreCase))) {
project.AddNewItem("Reference", "System.Core");
}
// Rewrite ProjectReferences to other renamed projects.
BuildItemGroup projectReferences = project.GetEvaluatedItemsByName("ProjectReference");
foreach (var mapping in newProjectToOldProjectMapping) {
string oldName = Path.GetFileName(mapping.Key);
string newName = Path.GetFileName(mapping.Value);
foreach (BuildItem projectReference in projectReferences) {
projectReference.Include = Regex.Replace(projectReference.Include, oldName, newName, RegexOptions.IgnoreCase);
}
}
project.Save(newProjectToOldProjectMapping[taskItem.ItemSpec]);
createdProjectFiles.Add(new TaskItem(taskItem) { ItemSpec = newProjectToOldProjectMapping[taskItem.ItemSpec] });
break;
case ProjectClassification.VS2010Solution:
this.Log.LogMessage(MessageImportance.Low, "Downgrading solution \"{0}\".", taskItem.ItemSpec);
string[] contents = File.ReadAllLines(taskItem.ItemSpec);
if (contents[1] != "Microsoft Visual Studio Solution File, Format Version 11.00" ||
contents[2] != "# Visual Studio 2010") {
this.Log.LogError("Unrecognized solution file header in \"{0}\".", taskItem.ItemSpec);
break;
}
contents[1] = "Microsoft Visual Studio Solution File, Format Version 10.00";
contents[2] = "# Visual Studio 2008";
for (int i = 3; i < contents.Length; i++) {
contents[i] = contents[i].Replace("TargetFrameworkMoniker = \".NETFramework,Version%3Dv", "TargetFramework = \"");
}
foreach (var mapping in newProjectToOldProjectMapping) {
string oldName = Path.GetFileName(mapping.Key);
string newName = Path.GetFileName(mapping.Value);
for (int i = 0; i < contents.Length; i++) {
contents[i] = Regex.Replace(contents[i], oldName, newName, RegexOptions.IgnoreCase);
}
}
File.WriteAllLines(newProjectToOldProjectMapping[taskItem.ItemSpec], contents);
createdProjectFiles.Add(new TaskItem(taskItem) { ItemSpec = newProjectToOldProjectMapping[taskItem.ItemSpec] });
break;
default:
this.Log.LogWarning("Unrecognized project type for \"{0}\".", taskItem.ItemSpec);
break;
}
}
//.........这里部分代码省略.........
示例10: CreateBuildProject
/// <summary>
/// Creates a temporary MSBuild content project in memory.
/// </summary>
void CreateBuildProject()
{
string projectPath = Path.Combine(buildDirectory, "content.contentproj");
string outputPath = Path.Combine(buildDirectory, "bin");
// Create the build engine.
msBuildEngine = new Engine(RuntimeEnvironment.GetRuntimeDirectory());
// Hook up our custom error logger.
errorLogger = new ErrorLogger();
msBuildEngine.RegisterLogger(errorLogger);
// Create the build project.
msBuildProject = new Project(msBuildEngine);
msBuildProject.FullFileName = projectPath;
msBuildProject.SetProperty("XnaPlatform", "Windows");
msBuildProject.SetProperty("XnaFrameworkVersion", "v2.0");
msBuildProject.SetProperty("Configuration", "Release");
msBuildProject.SetProperty("OutputPath", outputPath);
// Register any custom importers or processors.
foreach (string pipelineAssembly in pipelineAssemblies)
{
msBuildProject.AddNewItem("Reference", pipelineAssembly);
}
// Include the standard targets file that defines
// how to build XNA Framework content.
msBuildProject.AddNewImport("$(MSBuildExtensionsPath)\\Microsoft\\XNA " +
"Game Studio\\v3.1\\Microsoft.Xna.GameStudio" +
".ContentPipeline.targets", null);
}
示例11: RemoveEvaluatedItemAfterExpansionFails
public void RemoveEvaluatedItemAfterExpansionFails()
{
try
{
CompatibilityTestHelpers.CreateFiles(2, "foo", "foo", ObjectModelHelpers.TempProjectDir);
Project p = new Project();
object o = p.EvaluatedItems; // this causes failure
p.AddNewItem("foos", Path.Combine(ObjectModelHelpers.TempProjectDir, "*.foo"));
p.RemoveItem(p.EvaluatedItems[0]); // Exception thrown here
Assertion.Fail("success as failure"); // should not get here due to exception above
}
catch (Exception e)
{
// ExpectedException cannot be asserted as InternalErrorExceptions are internally scoped.
Assertion.AssertEquals(true, e.GetType().ToString().Contains("InternalErrorException"));
}
finally
{
CompatibilityTestHelpers.CleanupDirectory(ObjectModelHelpers.TempProjectDir);
}
}
示例12: AddNewItemName_Null
public void AddNewItemName_Null()
{
Project p = new Project();
p.AddNewItem(null, "include");
}
示例13: RemoveItem_IsNotImported
public void RemoveItem_IsNotImported()
{
Project p = new Project();
Project i = new Project(p.ParentEngine);
BuildItem buildItem = i.AddNewItem("n", "i");
p.RemoveItem(buildItem);
}
示例14: RemoveItemDirtyAfterRemove
public void RemoveItemDirtyAfterRemove()
{
string projectPath = ObjectModelHelpers.CreateFileInTempProjectDirectory("save.proj", String.Empty);
try
{
Project p = new Project();
BuildItem buildItem = p.AddNewItem("n", "i");
p.Save(projectPath);
Assertion.AssertEquals(false, p.IsDirty);
p.RemoveItem(buildItem);
Assertion.AssertEquals(true, p.IsDirty);
}
finally
{
CompatibilityTestHelpers.RemoveFile(projectPath);
}
}
示例15: RemoveItem
public void RemoveItem()
{
Project p = new Project();
BuildItem buildItem1 = p.AddNewItem("n", "i", true);
Assertion.AssertNotNull(CompatibilityTestHelpers.FindBuildItem(p, "n"));
p.RemoveItem(buildItem1);
Assertion.AssertNull(CompatibilityTestHelpers.FindBuildItem(p, "n"));
}