private void AddTasksIntermediateFiles(ProjectTargetElement target, CmdType type, string dir) { var tCopy = target.AddTask("Copy"); tCopy.SetParameter("SourceFiles", $"@(DllExportDir{dir})"); tCopy.SetParameter("DestinationFolder", $@"%({GetDependentsTargetDir(type)}.Identity){dir}\"); tCopy.SetParameter("OverwriteReadOnlyFiles", "true"); }
private void AddTaskForX86X64(ProjectTargetElement target, CmdType type, int arch) { var tCopy = target.AddTask("Copy"); tCopy.SetParameter("SourceFiles", $"@(DllExportDirX{arch})"); tCopy.SetParameter("DestinationFolder", $@"%({GetDependentsTargetDir(type)}.Identity)x{arch}\"); tCopy.SetParameter("OverwriteReadOnlyFiles", "true"); }
/// <summary> /// Adds a single task to target. /// </summary> private static ProjectTaskElement AddTask(ProjectTargetElement target, string name, params KeyValuePair <string, string>[] parameters) { var task = target.AddTask(name); foreach (var param in parameters) { task.SetParameter(param.Key, param.Value); } return(task); }
private ProjectTaskElement AddExecTask(ProjectTargetElement target, string cmd, string condition, bool continueOnError) { var tExec = target.AddTask("Exec"); tExec.Condition = condition; tExec.SetParameter("Command", cmd ?? throw new ArgumentNullException(nameof(cmd))); tExec.SetParameter("WorkingDirectory", $"$({MSBuildProperties.PRJ_TARGET_DIR})"); tExec.ContinueOnError = continueOnError.ToString().ToLower(); return(tExec); }
static void ResolveAssemblies(ProjectTargetElement target, string mainAssembly) { var resolveAssemblies = target.AddTask("ResolveAssemblies"); //NOTE: [Export] requires Mono.Android.Export.dll resolveAssemblies.SetParameter("Assemblies", mainAssembly + ";" + XamarinAndroid.FindAssembly("Mono.Android.Export.dll")); resolveAssemblies.SetParameter("LinkMode", LinkMode); resolveAssemblies.SetParameter("ReferenceAssembliesDirectory", "$(TargetFrameworkDirectory)"); resolveAssemblies.AddOutputItem("ResolvedAssemblies", "ResolvedAssemblies"); resolveAssemblies.AddOutputItem("ResolvedUserAssemblies", "ResolvedUserAssemblies"); resolveAssemblies.AddOutputItem("ResolvedFrameworkAssemblies", "ResolvedFrameworkAssemblies"); }
static void ResolveAssemblies(ProjectTargetElement target, List<IKVM.Reflection.Assembly> assemblies) { var resolveAssemblies = target.AddTask("ResolveAssemblies"); var assemblyPaths = assemblies.Select(a => a.Location).ToList(); //NOTE: [Export] requires Mono.Android.Export.dll assemblyPaths.Add(XamarinAndroid.FindAssembly("Mono.Android.Export.dll")); resolveAssemblies.SetParameter("Assemblies", string.Join(";", assemblyPaths)); resolveAssemblies.SetParameter("LinkMode", LinkMode); resolveAssemblies.SetParameter("ReferenceAssembliesDirectory", "$(TargetFrameworkDirectory)"); resolveAssemblies.AddOutputItem("ResolvedAssemblies", "ResolvedAssemblies"); resolveAssemblies.AddOutputItem("ResolvedUserAssemblies", "ResolvedUserAssemblies"); resolveAssemblies.AddOutputItem("ResolvedFrameworkAssemblies", "ResolvedFrameworkAssemblies"); }
private void SetParameter(ProjectRootElement projectXml, string unevaluatedPropertyValue) { ProjectTaskElement?execTask = FindExecTaskInTargets(projectXml); if (execTask != null) { SetExecParameter(execTask, unevaluatedPropertyValue); } else { ProjectTargetElement target = projectXml.AddTarget(TargetName); SetTargetDependencies(target); execTask = target.AddTask(ExecTask); SetExecParameter(execTask, unevaluatedPropertyValue); } }
private void SetParameter(ProjectRootElement projectXml, string unevaluatedPropertyValue) { (bool success, ProjectTaskElement execTask)result = FindExecTaskInTargets(projectXml); if (result.success == true) { SetExecParameter(result.execTask, unevaluatedPropertyValue); } else { ProjectTargetElement target = projectXml.AddTarget(TargetName); SetTargetDependencies(target); ProjectTaskElement execTask = target.AddTask(ExecTask); SetExecParameter(execTask, unevaluatedPropertyValue); } }
static public Project SetupMSBuildProject(IList <string> filesToScan, IList <string> termTableFiles) { Project project = SetupMSBuildProject(); string projectFolder = Path.GetDirectoryName(project.FullPath); if (filesToScan.Count > 0) { project.Xml.AddItem("ItemGroup1", CodeSweep.Utilities.RelativePathFromAbsolute(filesToScan[0], projectFolder)); if (filesToScan.Count > 1) { for (int i = 1; i < filesToScan.Count; ++i) { project.Xml.AddItem("ItemGroup2", CodeSweep.Utilities.RelativePathFromAbsolute(filesToScan[i], projectFolder)); } } } List <string> rootedTermTables = new List <string>(termTableFiles); List <string> relativeTermTables = rootedTermTables.ConvertAll <string>( delegate(string rootedPath) { return(CodeSweep.Utilities.RelativePathFromAbsolute(rootedPath, projectFolder)); }); ProjectTargetElement target = project.Xml.AddTarget("AfterBuild"); ProjectTaskElement newTask = target.AddTask("ScannerTask"); newTask.Condition = "'$(RunCodeSweepAfterBuild)' == 'true'"; newTask.ContinueOnError = "false"; newTask.SetParameter("FilesToScan", "@(ItemGroup1);@(ItemGroup2)"); newTask.SetParameter("TermTables", CodeSweep.Utilities.Concatenate(relativeTermTables, ";")); newTask.SetParameter("Project", "$(MSBuildProjectFullPath)"); string usingTaskAssembly = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetLoadedModules()[0].FullyQualifiedName) + "\\BuildTask.dll"; project.Xml.AddUsingTask("CodeSweep.BuildTask.ScannerTask", usingTaskAssembly, null); ProjectPropertyGroupElement group = project.Xml.AddPropertyGroup(); group.AddProperty("RunCodeSweepAfterBuild", "true"); project.ReevaluateIfNecessary(); return(project); }
/// <summary> /// Create a build file that build the projects in the specified order /// </summary> /// <param name="buildFilePath"></param> /// <param name="projectList"></param> /// <returns></returns> public static bool CreateBuildFile(string buildFilePath, List <string> projectList) { ProjectRootElement proj = ProjectRootElement.Create(buildFilePath); proj.AddProperty("Configuration", "Release"); proj.AddProperty("Platform", "x86"); ProjectTargetElement trgt = proj.AddTarget("Build"); foreach (string p in projectList) { ProjectTaskElement task = trgt.AddTask("MSBuild"); task.SetParameter("Projects", p); } proj.Save(); return(true); }
private static void CreateTaskIfNecessary(IVsProject project) { Project msbuildProject = MSBuildProjectFromIVsProject(project); ProjectTargetElement target = msbuildProject.Xml.Targets.FirstOrDefault(element => element.Name == "AfterBuild"); if (target == null) { target = msbuildProject.Xml.AddTarget("AfterBuild"); } string importFolder = Path.GetDirectoryName(typeof(CodeSweep.BuildTask.ScannerTask).Module.FullyQualifiedName); string importPath = Utilities.EncodeProgramFilesVar(importFolder) + "\\CodeSweep.targets"; string installedCondition = "Exists('" + importPath + "')"; if (GetNamedTask(target, "ScannerTask") == null) { string projectFolder = Path.GetDirectoryName(ProjectUtilities.GetProjectFilePath(project)); ProjectTaskElement newTask = target.AddTask("ScannerTask"); newTask.Condition = installedCondition + " and '$(" + RunWithBuildFlag + ")' == 'true'"; newTask.ContinueOnError = "false"; newTask.SetParameter("FilesToScan", CodeSweep.Utilities.Concatenate(AllItemGroupsInProject(msbuildProject.Xml), ";")); newTask.SetParameter("TermTables", Utilities.RelativePathFromAbsolute(Globals.DefaultTermTablePath, Path.GetDirectoryName(msbuildProject.FullPath))); newTask.SetParameter("Project", "$(MSBuildProjectFullPath)"); } bool found = false; foreach (ProjectImportElement import in msbuildProject.Xml.Imports) { if (import.Project == importPath) { found = true; break; } } if (!found) { ProjectImportElement importElement = msbuildProject.Xml.AddImport(importPath); importElement.Condition = installedCondition; } }
private void AddPreProcAfterTarget(ProjectTargetElement ppTarget, ProjectTaskElement tExec) { var tMove = ppTarget.AddTask("Move"); tMove.SetParameter("SourceFiles", $"$({MSBuildProperties.PRJ_TARGET_DIR})$({MSBuildProperties.PRJ_TARGET_F}){ILMERGE_TMP}.dll"); tMove.SetParameter("DestinationFiles", $"$({MSBuildProperties.PRJ_TARGET_DIR})$({MSBuildProperties.PRJ_TARGET_F})"); tMove.SetParameter("OverwriteReadOnlyFiles", "true"); tMove.ContinueOnError = tExec.ContinueOnError; var target = prj.AddTarget(MSBuildTargets.DXP_PRE_PROC_AFTER); target.AfterTargets = MSBuildTargets.DXP_MAIN; target.Label = Project.METALIB_PK_TOKEN; var tDelete = target.AddTask("Delete"); tDelete.SetParameter("Files", $"$({MSBuildProperties.PRJ_TARGET_DIR})$({MSBuildProperties.PRJ_TARGET_F}){ILMERGE_TMP}.pdb"); tDelete.ContinueOnError = "true"; }
/// <summary> /// Creates a ProjectTargetElement representing this instance. Attaches it to the specified root element. /// </summary> /// <param name="rootElement">The root element to which the new element will belong.</param> /// <returns>The new element.</returns> internal ProjectTargetElement ToProjectTargetElement(ProjectRootElement rootElement) { ProjectTargetElement target = rootElement.CreateTargetElement(Name); rootElement.AppendChild(target); target.Condition = Condition; target.DependsOnTargets = DependsOnTargets; target.Inputs = Inputs; target.Outputs = Outputs; target.Returns = Returns; foreach (ProjectTaskInstance taskInstance in Tasks) { ProjectTaskElement taskElement = target.AddTask(taskInstance.Name); taskElement.Condition = taskInstance.Condition; taskElement.ContinueOnError = taskInstance.ContinueOnError; taskElement.MSBuildArchitecture = taskInstance.MSBuildArchitecture; taskElement.MSBuildRuntime = taskInstance.MSBuildRuntime; foreach (KeyValuePair <string, string> taskParameterEntry in taskInstance.Parameters) { taskElement.SetParameter(taskParameterEntry.Key, taskParameterEntry.Value); } foreach (ProjectTaskInstanceChild outputInstance in taskInstance.Outputs) { if (outputInstance is ProjectTaskOutputItemInstance) { ProjectTaskOutputItemInstance outputItemInstance = outputInstance as ProjectTaskOutputItemInstance; taskElement.AddOutputItem(outputItemInstance.TaskParameter, outputItemInstance.ItemType, outputItemInstance.Condition); } else if (outputInstance is ProjectTaskOutputPropertyInstance) { ProjectTaskOutputPropertyInstance outputPropertyInstance = outputInstance as ProjectTaskOutputPropertyInstance; taskElement.AddOutputItem(outputPropertyInstance.TaskParameter, outputPropertyInstance.PropertyName, outputPropertyInstance.Condition); } } } return(target); }
public void BuildProject(string projectName, IEnumerable <ComponentGeneratorOutput> files, string csprojDir, string packageConfigFilename) { ProjectRootElement root = ProjectRootElement.Create(); AddProperties(projectName, root); // references AddItems(root, "Reference", "System", "System.Core", "System.Drawing", "System.Xml.Linq", "System.Data.DataSetExtensions", "Microsoft.CSharp", "System.Data", "System.Net.Http", "System.Xml"); // items to compile AddItems(root, "Compile", files.Select(file => file.CsFilePath).ToArray()); AddItems(root, "None", packageConfigFilename); ProjectTargetElement target = root.AddTarget("Build"); ProjectTaskElement task = target.AddTask("Csc"); task.SetParameter("Sources", "@(Compile)"); task.SetParameter("OutputAssembly", $"{projectName}.dll"); AddImports(root); root.Save($"{csprojDir}\\{projectName}.csproj"); }
private void AddExec(ProjectTargetElement target, string command) { var task = target.AddTask("Exec"); task.SetParameter("Command", command); }
/// <summary> /// Configures the post-build event of the .NET Standard class library. /// </summary> public static void ConfigurePostBuildEvent(NewProjectSettings settings, bool verbose = false) { try { string csprojPath = Path.Combine(settings.SolutionPath, $"{settings.ProjectName}.ServiceLibrary", $"{settings.ProjectName}.ServiceLibrary.csproj"); string executableWin; string executableMac; if (OperatingSystem.IsWindows()) { executableWin = Path.Combine(AppSettings.Default.AppPath, "flutnet.exe"); executableMac = $"{AppSettings.DefaultBinPath_macOS}/flutnet"; } else { executableWin = $"{AppSettings.DefaultBinPath_Windows}\\flutnet.exe"; executableMac = Path.Combine(AppSettings.Default.AppPath, "flutnet"); } ProjectRootElement prjElement = ProjectRootElement.Open(csprojPath); // Configures the post-build event(s) for Visual Studio for Mac List <string> commandArguments = new List <string> { executableMac.Quoted(), "pack", "-a", "${TargetFile}".Quoted(), "-n", settings.FlutterPackageName, "-o", (settings.CreateFlutterSubfolder ? $"${{SolutionDir}}/{settings.FlutterSubfolderName}" : "${SolutionDir}").Quoted(), "--force" }; foreach (ProjectPropertyGroupElement groupElement in prjElement.PropertyGroups) { if (groupElement.Condition.Contains("$(Configuration)") && groupElement.Condition.Contains("$(Platform)")) { ProjectPropertyElement propElement = groupElement.Properties.FirstOrDefault(p => p.Name == "CustomCommands"); propElement.Value = propElement.Value .Replace("<command></command>", $"<command>{string.Join(' ', commandArguments)}</command>"); } } // Configures the post-build event(s) for Visual Studio List <string> taskArguments = new List <string> { executableWin.Quoted(), "pack", "-a", "$(TargetPath)".Quoted(), "-n", settings.FlutterPackageName, "-o", (settings.CreateFlutterSubfolder ? $"$(SolutionDir)\\{settings.FlutterSubfolderName}" : "$(SolutionDir)").Quoted(), "--force" }; ProjectTargetElement targetElement = prjElement.Targets.FirstOrDefault(t => t.Name == "PostBuild"); ProjectTaskElement taskElement = targetElement.AddTask("Exec"); taskElement.SetParameter("Command", string.Join(' ', taskArguments)); prjElement.Save(); } catch (Exception e) { Log.Ex(e); throw new CommandLineException(CommandLineErrorCode.NewProject_CreateDotNetProjectsFailed, e); } }
internal static ProjectElement GetTargetItem(this ProjectTargetElement target, View view) { // get the member name and data from the view, and create/lookup the item. // return the item. switch (view.MemberName) { case "PropertyGroup": break; case "ItemGroup": break; case "AfterTargets": break; default: // var taskName = view.MemberName; if (TaskClasses.Value.ContainsKey(taskName.ToLower())) { // for tasks we recognize var tskType = TaskClasses.Value[taskName.ToLower()]; var tsk = target.AddTask(taskName); var required = tskType.RequiredInputs.ToList(); foreach (var n in view.GetChildPropertyNames()) { var prop = view.GetProperty(n); if (n == "Condition") { tsk.Condition = prop; continue; } if (required.Contains(n)) { required.Remove(n); } else { if (!tskType.OptionalInputs.Contains(n)) { Event <Warning> .Raise("Unknown Parameter", "Task '{0}' does not appear to have an input parameter '{1}'", taskName, n); // could we set some item collection based on these? // TODO: maybe. } } tsk.SetParameter(n, prop.Values.CollapseToString(";")); } foreach (var r in required) { Event <Warning> .Raise("Missing Parameter", "Task '{0}' is missing required input parameter '{1}'", taskName, r); } var outputs = tskType.Outputs.ToList(); foreach (var n in view.GetIndexedPropertyNames()) { var prop = view.GetProperty(n); // an output paramter. var nam = prop.GetProperty(prop.GetChildPropertyNames().FirstOrDefault()); if (!tskType.Outputs.Contains(nam.Value)) { Event <Warning> .Raise("Unknown Parameter", "Task '{0}' does not appear to have an output parameter '{1}'", taskName, nam.Value); } if (outputs.Contains(nam.Value)) { outputs.Remove(nam.Value); } tsk.AddOutputProperty(nam.Value, nam.MemberName); tsk.AddOutputItem(nam.Value, nam.MemberName); } foreach (var output in outputs) { // add in any unreferenced outputs as themselves. tsk.AddOutputProperty(output, output); tsk.AddOutputItem(output, output); } return(tsk); } // for tasks we don't recognize var tsk2 = target.AddTask(taskName); Event <Warning> .Raise("Unrecognized Task", "Task '{0}' is not recognized.", taskName); foreach (var n in view.GetChildPropertyNames()) { var prop = view.GetProperty(n); if (n == "Condition") { tsk2.Condition = prop; continue; } tsk2.SetParameter(n, prop.Values.CollapseToString(";")); } foreach (var n in view.GetIndexedPropertyNames()) { var prop = view.GetProperty(n); // an output paramter. var nam = prop.GetProperty(prop.GetChildPropertyNames().FirstOrDefault()); tsk2.AddOutputProperty(nam.Value, nam.MemberName); tsk2.AddOutputItem(nam.Value, nam.MemberName); } return(tsk2); } return(null); }