public static SpecFlowProject LoadSpecFlowProjectFromMsBuild(string projectFile) { projectFile = Path.GetFullPath(projectFile); Project project = new Project(); project.Load(projectFile, ProjectLoadSettings.IgnoreMissingImports); string projectFolder = Path.GetDirectoryName(projectFile); SpecFlowProject specFlowProject = new SpecFlowProject(); specFlowProject.ProjectFolder = projectFolder; specFlowProject.ProjectName = Path.GetFileNameWithoutExtension(projectFile); specFlowProject.AssemblyName = project.GetEvaluatedProperty("AssemblyName"); specFlowProject.DefaultNamespace = project.GetEvaluatedProperty("RootNamespace"); var items = project.GetEvaluatedItemsByName("None").Cast<BuildItem>() .Concat(project.GetEvaluatedItemsByName("Content").Cast<BuildItem>()); foreach (BuildItem item in items) { var extension = Path.GetExtension(item.FinalItemSpec); if (extension.Equals(".feature", StringComparison.InvariantCultureIgnoreCase)) { var featureFile = new SpecFlowFeatureFile(item.FinalItemSpec); var ns = item.GetEvaluatedMetadata("CustomToolNamespace"); if (!String.IsNullOrEmpty(ns)) featureFile.CustomNamespace = ns; specFlowProject.FeatureFiles.Add(featureFile); } if (Path.GetFileName(item.FinalItemSpec).Equals("app.config", StringComparison.InvariantCultureIgnoreCase)) { GeneratorConfigurationReader.UpdateConfigFromFile(specFlowProject.GeneratorConfiguration, Path.Combine(projectFolder, item.FinalItemSpec)); } } return specFlowProject; }
public Assembly CompileProject(string projectFileName) { Assembly existing; if (Compilations.TryGetValue(Path.GetFullPath(projectFileName), out existing)) { return existing; } var project = new Microsoft.Build.BuildEngine.Project(); project.Load(projectFileName); var projectName = Environment.NameTable.GetNameFor(project.EvaluatedProperties["AssemblyName"].Value); var projectPath = project.FullFileName; var compilerOptions = new SpecSharpOptions(); var assemblyReferences = new List<IAssemblyReference>(); var moduleReferences = new List<IModuleReference>(); var programSources = new List<SpecSharpSourceDocument>(); var assembly = new SpecSharpAssembly(projectName, projectPath, Environment, compilerOptions, assemblyReferences, moduleReferences, programSources); var helper = new SpecSharpCompilationHelper(assembly.Compilation); Compilations[Path.GetFullPath(projectFileName)] = assembly; assemblyReferences.Add(Environment.LoadAssembly(Environment.CoreAssemblySymbolicIdentity)); project.Build("ResolveAssemblyReferences"); foreach (BuildItem item in project.GetEvaluatedItemsByName("ReferencePath")) { var assemblyName = new System.Reflection.AssemblyName(item.GetEvaluatedMetadata("FusionName")); var name = Environment.NameTable.GetNameFor(assemblyName.Name); var culture = assemblyName.CultureInfo != null ? assemblyName.CultureInfo.Name : ""; var version = assemblyName.Version == null ? new Version(0, 0) : assemblyName.Version; var token = assemblyName.GetPublicKeyToken(); if (token == null) token = new byte[0]; var location = item.FinalItemSpec; var identity = new AssemblyIdentity(name, culture, version, token, location); var reference = Environment.LoadAssembly(identity); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("ProjectReference")) { var name = Environment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(item.FinalItemSpec)); var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var reference = CompileProject(location); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("Compile")) { var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var name = Environment.NameTable.GetNameFor(location); var programSource = new SpecSharpSourceDocument(helper, name, location, File.ReadAllText(location)); programSources.Add(programSource); } return assembly; }
public void ResolveBinary_FancyStuff () { engine = new Engine (Consts.BinPath); project = engine.CreateNewProject (); project.LoadXml (ResolveAssembly (null, @"Test\resources\binary\FancyStuff.dll")); Assert.IsTrue (project.Build ("A"), "A1"); big = project.GetEvaluatedItemsByName ("ResolvedFiles"); Assert.AreEqual (1, big.Count, "A2"); Assert.IsTrue (big[0].Include.EndsWith ("FancyStuff.dll"), "A3"); big = project.GetEvaluatedItemsByName ("ResolvedDependencyFiles"); Assert.AreEqual (1, big.Count, "A4"); Assert.IsTrue (big.Cast<BuildItem> ().Any (item => item.Include.EndsWith ("SimpleWrite.dll")), "A5"); }
/// <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; }
public void TestHintPath1 () { engine = new Engine (Consts.BinPath); project = engine.CreateNewProject (); project.LoadXml (ResolveAssembly (null, @"Test\resources\test.dll")); Assert.IsTrue (project.Build ("A"), "A1"); big = project.GetEvaluatedItemsByName ("ResolvedFiles"); Assert.AreEqual (1, big.Count, "A2"); Assert.IsTrue (big [0].Include.EndsWith (".dll"), "A3"); }
public override bool Execute() { foreach (var project in Projects) { Project doc = new Project(); doc.Load(project.ItemSpec); var reference = doc.GetEvaluatedItemsByName("Reference").OfType<BuildItem>(). Where(item => string.Equals(item.GetMetadata("HintPath"), OldReference, StringComparison.OrdinalIgnoreCase)).Single(); reference.SetMetadata("HintPath", NewReference); doc.Save(project.ItemSpec); } return true; }
public void TestGac1 () { var gacDir = GetGacDir (); if (gacDir == null || !System.IO.Directory.Exists (gacDir)) Assert.Ignore ("GAC not found."); engine = new Engine (Consts.BinPath); project = engine.CreateNewProject (); project.LoadXml (ResolveAssembly ("System", null)); Assert.IsTrue (project.Build ("A"), "A1"); big = project.GetEvaluatedItemsByName ("ResolvedFiles"); Assert.AreEqual (1, big.Count, "A2"); Assert.IsTrue (big [0].Include.EndsWith (".dll"), "A3"); }
private string GetItems (Project proj, string name) { BuildItemGroup big = proj.GetEvaluatedItemsByName (name); string str = String.Empty; if (big == null) return str; foreach (BuildItem bi in big) { if (str == String.Empty) str = bi.FinalItemSpec; else str += ";" + bi.FinalItemSpec; } return str; }
/// <summary> /// Adds references to this container from a MSBuild project. /// </summary> public void LoadReferencesFromBuildProject(MSBuild.Project buildProject) { foreach (string referenceType in SupportedReferenceTypes) { MSBuild.BuildItemGroup refererncesGroup = buildProject.GetEvaluatedItemsByName(referenceType); ReferenceNode node; bool isComReference = referenceType == ProjectFileConstants.COMReference; bool isAssemblyReference = referenceType == ProjectFileConstants.Reference; bool isProjectReference = referenceType == ProjectFileConstants.ProjectReference; if (isAssemblyReference && this.ProjectMgr.Build(MsBuildTarget.ResolveAssemblyReferences) != MSBuildResult.Sucessful) { continue; } foreach (MSBuild.BuildItem item in refererncesGroup) { ProjectElement element = new ProjectElement(this.ProjectMgr, item, false); if (isComReference) { node = this.CreateComReferenceNode(element); } else if (isAssemblyReference) { node = this.CreateAssemblyReferenceNode(element); } else if (isProjectReference) { node = this.CreateProjectReferenceNode(element); } else { // JRock: Added support for other references node = this.CreateOtherReferenceNode(element); } if (node != null) { this.AddChild(node); } } } }
/// <summary> /// Adds references to this container from a MSBuild project. /// </summary> public void LoadReferencesFromBuildProject(MSBuild.Project buildProject) { foreach (string referenceType in SupportedReferenceTypes) { MSBuild.BuildItemGroup refererncesGroup = buildProject.GetEvaluatedItemsByName(referenceType); bool isAssemblyReference = referenceType == ProjectFileConstants.Reference; // If the project was loaded for browsing we should still create the nodes but as not resolved. if (this.ProjectMgr.HasPassedSecurityChecks && isAssemblyReference && this.ProjectMgr.Build(MsBuildTarget.ResolveAssemblyReferences) != MSBuildResult.Successful) { continue; } foreach (MSBuild.BuildItem item in refererncesGroup) { ProjectElement element = new ProjectElement(this.ProjectMgr, item, false); ReferenceNode node = CreateReferenceNode(referenceType, element); if (node != null) { // Make sure that we do not want to add the item twice to the ui hierarchy // We are using here the UI representation of the Node namely the Caption to find that out, in order to // avoid different representation problems. // Example :<Reference Include="EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> // <Reference Include="EnvDTE80" /> bool found = false; for (HierarchyNode n = this.FirstChild; n != null && !found; n = n.NextSibling) { if (String.Compare(n.Caption, node.Caption, StringComparison.OrdinalIgnoreCase) == 0) { found = true; } } if (!found) { this.AddChild(node); } } } } }
/// <summary> /// Executes this instance. /// </summary> public override bool Execute() { if (this.References.Length == 0 || this.Projects.Length == 0) { this.Log.LogMessage(MessageImportance.Low, "Skipping reference hintpath fixup because no projects or no references were supplied."); return !this.Log.HasLoggedErrors; } // Figure out what the assembly names are of the references that are available. AssemblyName[] availableReferences = new AssemblyName[this.References.Length]; for (int i = 0; i < this.References.Length; i++) { if (File.Exists(this.References[i].ItemSpec)) { availableReferences[i] = AssemblyName.GetAssemblyName(this.References[i].ItemSpec); } else { availableReferences[i] = new AssemblyName(Path.GetFileNameWithoutExtension(this.References[i].ItemSpec)) { CodeBase = this.References[i].GetMetadata("FullPath"), }; } } foreach (var projectTaskItem in this.Projects) { var project = new Project(); Uri projectUri = new Uri(projectTaskItem.GetMetadata("FullPath")); project.Load(projectTaskItem.ItemSpec); foreach (BuildItem referenceItem in project.GetEvaluatedItemsByName("Reference")) { var referenceAssemblyName = new AssemblyName(referenceItem.Include); var matchingReference = availableReferences.FirstOrDefault(r => string.Equals(r.Name, referenceAssemblyName.Name, StringComparison.OrdinalIgnoreCase)); if (matchingReference != null) { var originalSuppliedReferenceItem = this.References[Array.IndexOf(availableReferences, matchingReference)]; string hintPath = originalSuppliedReferenceItem.GetMetadata("HintPath"); if (string.IsNullOrEmpty(hintPath)) { hintPath = projectUri.MakeRelativeUri(new Uri(matchingReference.CodeBase)).OriginalString.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); } this.Log.LogMessage("Fixing up HintPath to \"{0}\" in project \"{1}\".", referenceAssemblyName.Name, projectTaskItem.ItemSpec); referenceItem.SetMetadata("HintPath", hintPath); } } project.Save(projectTaskItem.ItemSpec); } return !this.Log.HasLoggedErrors; }
public void TestGac1 () { string documentString = @" <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""> <ItemGroup> <Reference Include='System' /> </ItemGroup> <PropertyGroup> <SearchPaths> {CandidateAssemblyFiles}; $(ReferencePath); {HintPathFromItem}; {TargetFrameworkDirectory}; {AssemblyFolders}; {GAC}; {RawFileName}; $(OutputPath) </SearchPaths> </PropertyGroup> <Target Name='A'> <ResolveAssemblyReference Assemblies='@(Reference)' SearchPaths='$(SearchPaths)' > <Output TaskParameter='ResolvedFiles' ItemName='ResolvedFiles'/> </ResolveAssemblyReference> </Target> </Project> "; engine = new Engine (Consts.BinPath); project = engine.CreateNewProject (); project.LoadXml (documentString); Assert.IsTrue (project.Build ("A"), "A1"); big = project.GetEvaluatedItemsByName ("ResolvedFiles"); Assert.AreEqual (1, big.Count, "A2"); Assert.IsTrue (big [0].Include.EndsWith (".dll"), "A3"); }
private void CheckItems (Project proj, string name, string prefix, params string [] values) { BuildItemGroup big = proj.GetEvaluatedItemsByName (name); if (big == null) { Assert.Fail ("{0}: Item corresponding '{1}' not found.", prefix, name); return; } if (values.Length != big.Count) { Console.Write ("Expected> "); foreach (string s in values) Console.Write ("{0}|", s); Console.WriteLine (); Console.Write ("Actual> "); foreach (BuildItem item in big) Console.Write ("{0}|", item.FinalItemSpec); Console.WriteLine (); Assert.AreEqual (values.Length, big.Count, String.Format ("{0}: Number of items", prefix)); } for (int i = 0; i < values.Length; i ++) Assert.AreEqual (values [i], big [i].FinalItemSpec, String.Format ("{0}: Item named {1}, numbered {2}", prefix, name, i)); }
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; }
public void GetEvaluatedItemsByName_Null() { Project p = new Project(); string name = null; BuildItemGroup emptyGroup = p.GetEvaluatedItemsByName(name); }
public void AddFileToContentProject(String filename, bool shouldCompile, bool copyToOuputFolder, String processor, String importer) { try { foreach (var item in _currentProject.VSProj.EvaluatedItems) { if (item is Microsoft.Build.BuildEngine.BuildItem) { BuildItem buildItem = (BuildItem)item; if (buildItem.Name == "NestedContentProject") { string _name = buildItem.Include; _name = Path.Combine(_currentProject.Path, _name); Project p = new Project(); p.Load(_name); string _root = Path.GetDirectoryName(buildItem.Include); String action = "None"; if (shouldCompile == true) { action = "Compile"; } BuildItemGroup grp = p.GetEvaluatedItemsByName(action); string include = filename.Replace(_root, "").Replace("/", "\\"); if (include.StartsWith("\\")) { include = include.Substring(1); } bool found = false; foreach (BuildItem item2 in grp) { if (item2.Include == include) { found = true; break; } } if (found == false) { BuildItem item1 = p.AddNewItem(action, include); item1.SetMetadata("Name", Path.GetFileNameWithoutExtension(filename)); if (copyToOuputFolder == true) { item1.SetMetadata("CopyToOutputDirectory", "PreserveNewest"); } if (String.IsNullOrEmpty(importer) == false) { item1.SetMetadata("Importer", importer); } if (String.IsNullOrEmpty(processor) == false) { item1.SetMetadata("Processor", processor); } p.Save(_name); } p = null; } } } } catch (Exception err) { ShowErrorMessage(err); } }
public void GetEvaluatedItemsByNameTwoItems() { Project p = new Project(); string name = "new"; p.SetProperty("condition", "false"); BuildItem buildItem = p.AddNewItem(name, "i1"); buildItem.Condition = "$(condition)"; p.AddNewItem(name, "i2"); BuildItemGroup foundGroup = p.GetEvaluatedItemsByName(name); Assertion.AssertEquals(1, foundGroup.Count); }
public void GetEvaluatedItemsByNameDoesNotExistItem() { Project p = new Project(); string name = "notFound"; BuildItemGroup emptyGroup = p.GetEvaluatedItemsByName(name); Assertion.AssertEquals(0, emptyGroup.Count); }
void CheckEvaluatedMetadata (Project p, string itemname, string metadataname, string[] values, string prefix) { BuildItemGroup group = p.GetEvaluatedItemsByName (itemname); Assert.AreEqual (values.Length, group.Count, "Number of items for itemname " + itemname); for (int i = 0; i < values.Length; i++) { Assert.AreEqual (values[i], group [i].GetEvaluatedMetadata (metadataname), prefix + "#" + i.ToString ()); } }
public Assembly CompileProject(string projectFileName) { Assembly existing; if (Compilations.TryGetValue(Path.GetFullPath(projectFileName), out existing)) { return(existing); } var project = new Microsoft.Build.BuildEngine.Project(); project.Load(projectFileName); var projectName = Environment.NameTable.GetNameFor(project.EvaluatedProperties["AssemblyName"].Value); var projectPath = project.FullFileName; var compilerOptions = new SpecSharpOptions(); var assemblyReferences = new List <IAssemblyReference>(); var moduleReferences = new List <IModuleReference>(); var programSources = new List <SpecSharpSourceDocument>(); var assembly = new SpecSharpAssembly(projectName, projectPath, Environment, compilerOptions, assemblyReferences, moduleReferences, programSources); var helper = new SpecSharpCompilationHelper(assembly.Compilation); Compilations[Path.GetFullPath(projectFileName)] = assembly; assemblyReferences.Add(Environment.LoadAssembly(Environment.CoreAssemblySymbolicIdentity)); project.Build("ResolveAssemblyReferences"); foreach (BuildItem item in project.GetEvaluatedItemsByName("ReferencePath")) { var assemblyName = new System.Reflection.AssemblyName(item.GetEvaluatedMetadata("FusionName")); var name = Environment.NameTable.GetNameFor(assemblyName.Name); var culture = assemblyName.CultureInfo != null ? assemblyName.CultureInfo.Name : ""; var version = assemblyName.Version == null ? new Version(0, 0) : assemblyName.Version; var token = assemblyName.GetPublicKeyToken(); if (token == null) { token = new byte[0]; } var location = item.FinalItemSpec; var identity = new AssemblyIdentity(name, culture, version, token, location); var reference = Environment.LoadAssembly(identity); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("ProjectReference")) { var name = Environment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(item.FinalItemSpec)); var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var reference = CompileProject(location); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("Compile")) { var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var name = Environment.NameTable.GetNameFor(location); var programSource = new SpecSharpSourceDocument(helper, name, location, File.ReadAllText(location)); programSources.Add(programSource); } return(assembly); }
/// <summary> /// Loads each MSBuild project in this solution and looks for its project-to-project references so that /// we know what build order we should use when building the solution. /// </summary> /// <owner>LukaszG</owner> static private void ScanProjectDependencies(SolutionParser solution, Engine parentEngine, string childProjectToolsVersion, string fullSolutionConfigurationName, BuildEventContext projectBuildEventContext) { string message = null; // Don't bother with all this if the solution configuration doesn't even exist. if (fullSolutionConfigurationName == null) { return; } foreach (ProjectInSolution project in solution.ProjectsInOrder) { // Skip the project if we don't have its configuration in this solution configuration if (!project.ProjectConfigurations.ContainsKey(fullSolutionConfigurationName)) { continue; } if ((project.ProjectType == SolutionProjectType.ManagedProject) || ((project.ProjectType == SolutionProjectType.Unknown) && (project.CanBeMSBuildProjectFile(out message)))) { try { //Will fail to load a throw an error if the tools version is incorrect. Project msbuildProject = new Project(parentEngine, childProjectToolsVersion); msbuildProject.IsLoadedByHost = false; // this is before building the solution wrapper project, so the current directory may be not set to // the one containing the solution file, and we'd get the relative path wrong msbuildProject.Load(project.AbsolutePath); // Project references for MSBuild projects could be affected by the active configuration, // so set it before retrieving references. msbuildProject.GlobalProperties.SetProperty("Configuration", project.ProjectConfigurations[fullSolutionConfigurationName].ConfigurationName, true /* treat as literal */); msbuildProject.GlobalProperties.SetProperty("Platform", project.ProjectConfigurations[fullSolutionConfigurationName].PlatformName, true /* treat as literal */); BuildItemGroup references = msbuildProject.GetEvaluatedItemsByName("ProjectReference"); foreach (BuildItem reference in references) { string referencedProjectGuid = reference.GetEvaluatedMetadata("Project"); // Need unescaped data here. AddDependencyByGuid(solution, project, parentEngine, projectBuildEventContext, referencedProjectGuid); } // // ProjectDependency items work exactly like ProjectReference items from the point of // view of determining that project B depends on project A. This item must cause // project A to be built prior to project B. // references = msbuildProject.GetEvaluatedItemsByName("ProjectDependency"); foreach (BuildItem reference in references) { string referencedProjectGuid = reference.GetEvaluatedMetadata("Project"); // Need unescaped data here. AddDependencyByGuid(solution, project, parentEngine, projectBuildEventContext, referencedProjectGuid); } // // If this is a web deployment project, we have a reference specified as a property // "SourceWebProject" rather than as a ProjectReference item. This has the format // {GUID}|PATH_TO_CSPROJ // where // GUID is the project guid for the "source" project // PATH_TO_CSPROJ is the solution-relative path to the csproj file. // // NOTE: This is obsolete and is intended only for backward compatability with // Whidbey-generated web deployment projects. New projects should use the // ProjectDependency item above. // string referencedWebProjectGuid = msbuildProject.GetEvaluatedProperty("SourceWebProject"); if (!string.IsNullOrEmpty(referencedWebProjectGuid)) { // Grab the guid with its curly braces... referencedWebProjectGuid = referencedWebProjectGuid.Substring(0, 38); AddDependencyByGuid(solution, project, parentEngine, projectBuildEventContext, referencedWebProjectGuid); } } // We don't want any problems scanning the project file to result in aborting the build. catch (Exception e) { if (ExceptionHandling.IsCriticalException(e)) throw; parentEngine.LoggingServices.LogWarning(projectBuildEventContext, "SubCategoryForSolutionParsingErrors", new BuildEventFileInfo(project.RelativePath), "SolutionScanProjectDependenciesFailed", project.RelativePath, e.Message); } } else if (project.ProjectType == SolutionProjectType.VCProject) { try { XmlDocument doc = new XmlDocument(); doc.Load(project.AbsolutePath); project.IsStaticLibrary = VCProjectParser.IsStaticLibrary(doc, project.ProjectConfigurations[fullSolutionConfigurationName].FullName); // this is before building the solution wrapper project, so the current directory may be not set to // the one containing the solution file, and we'd get the relative path wrong List<string> referencedProjectGuids = VCProjectParser.GetReferencedProjectGuids(doc); foreach (string referencedProjectGuid in referencedProjectGuids) { if (!string.IsNullOrEmpty(referencedProjectGuid)) { if (solution.ProjectsByGuid.ContainsKey(referencedProjectGuid)) { project.Dependencies.Add(referencedProjectGuid); project.ProjectReferences.Add(referencedProjectGuid); } else { parentEngine.LoggingServices.LogWarning(projectBuildEventContext, "SubCategoryForSolutionParsingErrors", new BuildEventFileInfo(solution.SolutionFile), "SolutionParseProjectDepNotFoundError", project.ProjectGuid, referencedProjectGuid); } } } } // We don't want any problems scanning the project file to result in aborting the build. catch (Exception e) { if (ExceptionHandling.IsCriticalException(e)) throw; parentEngine.LoggingServices.LogWarning(projectBuildEventContext, "SubCategoryForSolutionParsingErrors", new BuildEventFileInfo(project.RelativePath), "SolutionScanProjectDependenciesFailed", project.RelativePath, e.Message); } } } }
void CheckCopyBuildItems (Project project, string [] source_files, string destination_folder, string prefix) { int num = source_files.Length; for (int i = 0; i < num; i ++) Assert.IsTrue (File.Exists (source_files [i]), prefix + " C1"); BuildItemGroup big = project.GetEvaluatedItemsByName ("I0"); Assert.AreEqual (num, big.Count, prefix + " C2"); for (int i = 0; i < num; i++) { string suffix = (i + 1).ToString (); BuildItem bi = big [i]; Assert.AreEqual (Path.Combine (destination_folder, Path.GetFileName (source_files [i])), bi.FinalItemSpec, prefix + " C3 #" + suffix); Assert.AreEqual (suffix, bi.GetMetadata ("Md"), prefix + " C4 #" + suffix); } big = project.GetEvaluatedItemsByName ("I1"); Assert.AreEqual (num, big.Count, prefix + " C6"); for (int i = 0; i < num; i++) { string suffix = (i + 1).ToString (); BuildItem bi = big [i]; Assert.AreEqual (Path.Combine (destination_folder, Path.GetFileName (source_files [i])), bi.FinalItemSpec, prefix + " C7 #" + suffix); Assert.AreEqual (suffix, bi.GetMetadata ("Md"), prefix + " C8 #" + suffix); } }
/// <summary> /// Given a cache loaded into a project, determines whether it is up to date with respect to the projects and the solution file listed /// with it, and was created with the same configuration/platform and tools version values as the ones currently in use. /// </summary> private static bool IsCacheUpToDate(Engine parentEngine, string solutionFile, string solutionFileDirectory, Project msbuildProject, BuildEventContext projectBuildEventContext, string fullSolutionConfigurationName, string wrapperProjectToolsVersion) { // Check the full solution configuration matches, eg "Debug|AnyCPU" string cacheSolutionConfigurationName = msbuildProject.GetEvaluatedProperty(cacheSolutionConfigurationPropertyName); string cacheToolsVersion = msbuildProject.GetEvaluatedProperty(cacheToolsVersionPropertyName); string cacheVersion = msbuildProject.GetEvaluatedProperty(cacheVersionNumber); if (cacheSolutionConfigurationName == null || cacheToolsVersion == null) { // Unexpected cache format; we can't use it return false; } if (!String.Equals(fullSolutionConfigurationName, cacheSolutionConfigurationName, StringComparison.OrdinalIgnoreCase)) { parentEngine.LoggingServices.LogComment(projectBuildEventContext, "SolutionCacheNotApplicable", "Configuration", cacheSolutionConfigurationName, fullSolutionConfigurationName); return false; } if (!String.Equals(wrapperProjectToolsVersion, cacheToolsVersion, StringComparison.OrdinalIgnoreCase)) { parentEngine.LoggingServices.LogComment(projectBuildEventContext, "SolutionCacheNotApplicable", "ToolsVersion", cacheToolsVersion, wrapperProjectToolsVersion); return false; } // We also store the version of MSBuild that wrote the file and verify it's the same as ours: that ensures that we // don't read possibly incompatible caches. string thisVersion = Constants.AssemblyVersion; if (!String.Equals(cacheVersion, thisVersion, StringComparison.OrdinalIgnoreCase)) { parentEngine.LoggingServices.LogComment(projectBuildEventContext, "SolutionCacheNotApplicableDueToCacheVersion", cacheVersion, thisVersion); return false; } // Finally check timestamps BuildItemGroup allProjects = msbuildProject.GetEvaluatedItemsByName(cacheProjectListName); List<string> inputs = new List<string>(); foreach (BuildItem item in allProjects.Items) { inputs.Add(item.EvaluatedItemSpec); } if (inputs.Count == 0) { // There's no inputs; either an old-format cache file, or there's really // no projects in this solution. In the former case, we need to regenerate. // In the latter case, we don't really care if we do. So say it's out of date. return false; } // If there are inputs to check, we should also add the solution file, as we need to make sure the // solution file is up to date with respect to the cache file // Get the solution file name because the solution file may be something like myDirectory\mysolution.sln // and since we have already calculated the directory for the solution file, we just need the filename name to // combine with the directory to get the full path to the solution file without having to call GetFullPath again. string solutionFileName = Path.GetFileName(solutionFile); string solutionFileLocation = Path.Combine(solutionFileDirectory, solutionFileName); inputs.Add(solutionFileLocation); List<string> outputs = new List<string>(); outputs.Add(msbuildProject.FullFileName); DependencyAnalysisLogDetail dependencyAnalysisDetail; bool isAnyOutOfDate = TargetDependencyAnalyzer.IsAnyOutOfDate(out dependencyAnalysisDetail, solutionFileDirectory, inputs, outputs); if (isAnyOutOfDate) { string reason = TargetDependencyAnalyzer.GetFullBuildReason(dependencyAnalysisDetail); string message = ResourceUtilities.FormatResourceString("SolutionCacheOutOfDate", reason); parentEngine.LoggingServices.LogCommentFromText(projectBuildEventContext, MessageImportance.Low, message); return false; } else { return true; } }
/// <summary> /// This helper method checks the project to determine whether a particular item of a particular /// name exists in the project, such that the build process (the tasks) would see it. /// </summary> /// <param name="project"></param> /// <param name="itemType"></param> /// <param name="itemSpec"></param> /// <returns></returns> /// <owner>RGoel</owner> private bool ItemExistsInBuildProcessHelper ( Project project, string itemType, string itemSpec ) { BuildItemGroup evaluatedItemsOfParticularType = project.GetEvaluatedItemsByName(itemType); Assertion.AssertNotNull(evaluatedItemsOfParticularType); // Search all the evaluated items for the one with the item spec we want // to remove. foreach (BuildItem evaluatedItem in evaluatedItemsOfParticularType) { if (evaluatedItem.FinalItemSpecEscaped == itemSpec) { return true; } } return false; }
/// <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; } } if (InPlaceDowngrade) { this.DowngradedProjects = new ITaskItem[0]; } else { this.DowngradedProjects = createdProjectFiles.ToArray(); } return !this.Log.HasLoggedErrors; }
/// <summary> /// Gets an item list from the project and assert that it contains /// exactly one item with the supplied name. /// </summary> /// <param name="p"></param> /// <param name="type"></param> /// <param name="itemInclude"></param> /// <owner>JomoF</owner> static internal BuildItem AssertSingleItem(Project p, string type, string itemInclude) { BuildItemGroup items = p.GetEvaluatedItemsByName(type); int count = 0; foreach(BuildItem item in items) { // This was item.Include before, but I believe it really should have been item.FinalItemSpec, which // is what is actually used by tasks, etc. Assertion.AssertEquals(itemInclude.ToUpperInvariant(), item.FinalItemSpec.ToUpperInvariant()); ++count; } Assertion.AssertEquals(1, count); return items[0]; }
/// <summary> /// Asserts that there are no items in the project of the specified type /// </summary> static internal void AssertNoItem(Project p, string type) { BuildItemGroup items = p.GetEvaluatedItemsByName(type); Assertion.AssertEquals(0, items.Count); }