public override bool Execute() { if (!ValidateInputs()) { return false; } try { ProjectEx.Task = this; ProjectEx.Terminals = terminalTargets; IEnumerator<KeyValuePair<string, ProjectToTransform>> ienum; Version version = new Version(major, minor, build, revision); BuildGuidDictionary(); //Load all of the specified projects. for (int i = 0; i < srcProjects.Length; i++) { ITaskItem srcProject = srcProjects[i]; ITaskItem destProject = destProjects[i]; if (RunSDKTransform) { string preTransform = srcProject.GetMetadata("PreTransform"); if (!string.IsNullOrEmpty(preTransform)) { if (!File.Exists(preTransform)) { Log.LogWarning("PreTransform \"{0}\" does not exist", preTransform); preTransform = null; } } string postTransform = srcProject.GetMetadata("PostTransform"); if (string.IsNullOrEmpty(postTransform)) { postTransform = null; } else if (!File.Exists(postTransform)) { Log.LogWarning("PostTransform \"{0}\" does not exist", postTransform); postTransform = null; } string transformMetadataValue = srcProject.GetMetadata("Transform"); string transformType = "Client"; if (transformMetadataValue != null && transformMetadataValue.Equals("Server", StringComparison.OrdinalIgnoreCase)) { transformType = "Server"; } TransFormProject transformProj = new TransFormProject(transformType, version, srcProject.ItemSpec, destProject.ItemSpec, TargetToolsVersion); transformProj.Transform(preTransform, postTransform); } else if(srcProject.GetMetadata("FullPath") != destProject.GetMetadata("FullPath")) { File.Copy(srcProject.GetMetadata("FullPath"), destProject.GetMetadata("FullPath")); } ProjectEx project = new ProjectEx(destProject, version, srcProject.GetMetadata("FullPath")); project.TargetToolsVersion = TargetToolsVersion; // only load after transform project.Load(); RemoveSourceControlAnnotations(project); string assemblyName = project.GetEvaluatedProperty("AssemblyName"); projects.Add(assemblyName, new ProjectToTransform { Project = project, DestinationFile = destProject.ItemSpec }); //Transforms all paths into relative paths... This probably isn't necessary, as all paths should be relative already... //and a warning is spit out in GetProjectChildFiles now. project.NormalizePaths(true); } if (RunSDKTransform) { ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { ProjectEx project = ienum.Current.Value.Project; List<Build.Construction.ProjectElement> groupsToRemove = new List<Build.Construction.ProjectElement>(); foreach (Build.Construction.ProjectPropertyGroupElement bpg in project.MsBuildProject.Xml.PropertyGroups) { if (bpg != null && !string.IsNullOrEmpty(bpg.Condition) && bpg.Condition.ToLower().Contains("$(spoclient)")) { Console.WriteLine("------ remove property group --- " + bpg.Condition); groupsToRemove.Add(bpg); } } foreach (Build.Construction.ProjectImportElement imp in project.MsBuildProject.Xml.Imports) { if (!string.IsNullOrEmpty(imp.Condition) && imp.Condition.ToLower().Contains("$(spoclient)")) { groupsToRemove.Add(imp); } } foreach (Build.Construction.ProjectElement propertyG in groupsToRemove) { project.MsBuildProject.Xml.RemoveChild(propertyG); } } } if (RunTemplateTransform) { ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { ProjectEx project = ienum.Current.Value.Project; List<_BE.ProjectProperty> toRemove = new List<_BE.ProjectProperty>(); foreach (_BE.ProjectProperty property in project.MsBuildProject.AllEvaluatedProperties) { if (property.Name == "TemplateNameString" || property.Name == "TemplateNamePackage" || property.Name == "TemplateNameResID" || property.Name == "TemplateDescriptionString" || property.Name == "TemplateDescriptionPackage" || property.Name == "TeplateDescriptionResID" || property.Name == "TemplateIconFile" || property.Name == "TemplateIconPackage" || property.Name == "TemplateIconResID" || property.Name == "TemplateID" || property.Name == "TemplateDefaultName" || property.Name == "TemplateProjectType" || property.Name == "TemplateProjectSubType" || property.Name == "TemplateRequiredFrameworkVersion" || property.Name == "TemplateSortOrder") { toRemove.Add(property); } } foreach (_BE.ProjectProperty prop in toRemove) { project.MsBuildProject.RemoveProperty(prop); } ReplaceGuidPropertyIfExists(project, "ProjectGuid"); ReplaceGuidPropertyIfExists(project, "EmulatorId"); foreach (_BE.ProjectItem item in project.MsBuildProject.GetItemsIgnoringCondition("ProjectReference")) { ReplaceGuidMetadataIfExists(item, "Project"); } string rootNamepace = project.GetEvaluatedProperty("RootNamespace"); TransmorgificationUtilities.MakeTemplateReplacements(true, true, rootNamepace, project.FullFileName, project.MsBuildProject); } } ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { /* Projects cannot be saved until multiple passes have been performed over all the projects to allow * template transforms to replace all GUIDs with autogenerated GUID identifiers, and ProjectReferences * to be updated to reflect those modified GUIDs */ ProjectEx project = ienum.Current.Value.Project; //project.DefaultToolsVersion = TargetToolsVersion; project.TargetToolsVersion = TargetToolsVersion; string assemblyName = project.GetEvaluatedProperty("AssemblyName"); project.MsBuildProject.Save(ienum.Current.Value.DestinationFile, Encoding.UTF8); } if (!string.IsNullOrEmpty(solutionFile)) { WiXSolution sln = new WiXSolution(); sln.Shortcut = shortcutType; sln.ParentDirectoryRef = parentDirectoryRef; sln.FragmentIncludeFiles = fragmentIncludeFiles; sln.ComponentIncludeFiles = componentIncludeFiles; ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { sln.Add(new SolutionObject(ienum.Current.Value.Project)); } if (extraSlnFiles != null && extraSlnFiles.Length > 0) { foreach (ITaskItem extraSlnFile in extraSlnFiles) { sln.Add(new SolutionObject(new SolutionFile(extraSlnFile))); } } if (string.IsNullOrEmpty(wxsFile)) { sln.Save(solutionFile); } else { if (componentGuid == default(Guid)) { Log.LogError("ComponentGuid is required when a WiX output is specified"); } else { sln.Save(solutionFile, wxsFile, componentGuid); } } } else { //Warn that certain properties are ignored if a solution isn't specified. if(!string.IsNullOrEmpty(wxsFile)) { Log.LogWarning("Not generating WiX source {0}; WiX files are not generated unless a Visual Studio Solution is also generated.", wxsFile); } } return true; } catch (Exception e) { this.Log.LogErrorFromException(e, true, true, null); return false; } }
public override bool Execute() { if (!ValidateInputs()) { return(false); } try { ProjectEx.Task = this; ProjectEx.Terminals = terminalTargets; IEnumerator <KeyValuePair <string, ProjectToTransform> > ienum; Version version = new Version(major, minor, build, revision); BuildGuidDictionary(); //Load all of the specified projects. for (int i = 0; i < srcProjects.Length; i++) { ITaskItem srcProject = srcProjects[i]; ITaskItem destProject = destProjects[i]; if (RunSDKTransform) { string preTransform = srcProject.GetMetadata("PreTransform"); if (!string.IsNullOrEmpty(preTransform)) { if (!File.Exists(preTransform)) { Log.LogWarning("PreTransform \"{0}\" does not exist", preTransform); preTransform = null; } } string postTransform = srcProject.GetMetadata("PostTransform"); if (string.IsNullOrEmpty(postTransform)) { postTransform = null; } else if (!File.Exists(postTransform)) { Log.LogWarning("PostTransform \"{0}\" does not exist", postTransform); postTransform = null; } string transformMetadataValue = srcProject.GetMetadata("Transform"); string transformType = "Client"; if (transformMetadataValue != null && transformMetadataValue.Equals("Server", StringComparison.OrdinalIgnoreCase)) { transformType = "Server"; } TransFormProject transformProj = new TransFormProject(transformType, version, srcProject.ItemSpec, destProject.ItemSpec, TargetToolsVersion); transformProj.Transform(preTransform, postTransform); } else if (srcProject.GetMetadata("FullPath") != destProject.GetMetadata("FullPath")) { File.Copy(srcProject.GetMetadata("FullPath"), destProject.GetMetadata("FullPath")); } ProjectEx project = new ProjectEx(destProject, version, srcProject.GetMetadata("FullPath")); project.TargetToolsVersion = TargetToolsVersion; // only load after transform project.Load(); RemoveSourceControlAnnotations(project); string assemblyName = project.GetEvaluatedProperty("AssemblyName"); projects.Add(assemblyName, new ProjectToTransform { Project = project, DestinationFile = destProject.ItemSpec }); //Transforms all paths into relative paths... This probably isn't necessary, as all paths should be relative already... //and a warning is spit out in GetProjectChildFiles now. project.NormalizePaths(true); } if (RunSDKTransform) { ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { ProjectEx project = ienum.Current.Value.Project; List <Build.Construction.ProjectElement> groupsToRemove = new List <Build.Construction.ProjectElement>(); foreach (Build.Construction.ProjectPropertyGroupElement bpg in project.MsBuildProject.Xml.PropertyGroups) { if (bpg != null && !string.IsNullOrEmpty(bpg.Condition) && bpg.Condition.ToLower().Contains("$(spoclient)")) { Console.WriteLine("------ remove property group --- " + bpg.Condition); groupsToRemove.Add(bpg); } } foreach (Build.Construction.ProjectImportElement imp in project.MsBuildProject.Xml.Imports) { if (!string.IsNullOrEmpty(imp.Condition) && imp.Condition.ToLower().Contains("$(spoclient)")) { groupsToRemove.Add(imp); } } foreach (Build.Construction.ProjectElement propertyG in groupsToRemove) { project.MsBuildProject.Xml.RemoveChild(propertyG); } } } if (RunTemplateTransform) { ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { ProjectEx project = ienum.Current.Value.Project; List <_BE.ProjectProperty> toRemove = new List <_BE.ProjectProperty>(); foreach (_BE.ProjectProperty property in project.MsBuildProject.AllEvaluatedProperties) { if (property.Name == "TemplateNameString" || property.Name == "TemplateNamePackage" || property.Name == "TemplateNameResID" || property.Name == "TemplateDescriptionString" || property.Name == "TemplateDescriptionPackage" || property.Name == "TeplateDescriptionResID" || property.Name == "TemplateIconFile" || property.Name == "TemplateIconPackage" || property.Name == "TemplateIconResID" || property.Name == "TemplateID" || property.Name == "TemplateDefaultName" || property.Name == "TemplateProjectType" || property.Name == "TemplateProjectSubType" || property.Name == "TemplateRequiredFrameworkVersion" || property.Name == "TemplateSortOrder") { toRemove.Add(property); } } foreach (_BE.ProjectProperty prop in toRemove) { project.MsBuildProject.RemoveProperty(prop); } ReplaceGuidPropertyIfExists(project, "ProjectGuid"); ReplaceGuidPropertyIfExists(project, "EmulatorId"); foreach (_BE.ProjectItem item in project.MsBuildProject.GetItemsIgnoringCondition("ProjectReference")) { ReplaceGuidMetadataIfExists(item, "Project"); } string rootNamepace = project.GetEvaluatedProperty("RootNamespace"); TransmorgificationUtilities.MakeTemplateReplacements(true, true, rootNamepace, project.FullFileName, project.MsBuildProject); } } ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { /* Projects cannot be saved until multiple passes have been performed over all the projects to allow * template transforms to replace all GUIDs with autogenerated GUID identifiers, and ProjectReferences * to be updated to reflect those modified GUIDs */ ProjectEx project = ienum.Current.Value.Project; //project.DefaultToolsVersion = TargetToolsVersion; project.TargetToolsVersion = TargetToolsVersion; string assemblyName = project.GetEvaluatedProperty("AssemblyName"); project.MsBuildProject.Save(ienum.Current.Value.DestinationFile, Encoding.UTF8); } if (!string.IsNullOrEmpty(solutionFile)) { WiXSolution sln = new WiXSolution(); sln.Shortcut = shortcutType; sln.ParentDirectoryRef = parentDirectoryRef; sln.FragmentIncludeFiles = fragmentIncludeFiles; sln.ComponentIncludeFiles = componentIncludeFiles; ienum = projects.GetEnumerator(); while (ienum.MoveNext()) { sln.Add(new SolutionObject(ienum.Current.Value.Project)); } if (extraSlnFiles != null && extraSlnFiles.Length > 0) { foreach (ITaskItem extraSlnFile in extraSlnFiles) { sln.Add(new SolutionObject(new SolutionFile(extraSlnFile))); } } if (string.IsNullOrEmpty(wxsFile)) { sln.Save(solutionFile); } else { if (componentGuid == default(Guid)) { Log.LogError("ComponentGuid is required when a WiX output is specified"); } else { sln.Save(solutionFile, wxsFile, componentGuid); } } } else { //Warn that certain properties are ignored if a solution isn't specified. if (!string.IsNullOrEmpty(wxsFile)) { Log.LogWarning("Not generating WiX source {0}; WiX files are not generated unless a Visual Studio Solution is also generated.", wxsFile); } } return(true); } catch (Exception e) { this.Log.LogErrorFromException(e, true, true, null); return(false); } }