How to call static class methods from msbuild?

How to call a static class method from msbuild and save its results in a list?

EDIT: Alright, let me explain a little further. I am using a file builder using sandcastle to create documentation for my application. One of the requirements is that you must specify the documentation sources as follows:

<DocumentationSources>
    <DocumentationSource sourceFile="$(MSBuildProjectDirectory)\..\src\myApp\bin\Debug\myApp.exe" xmlns="" />
    <DocumentationSource sourceFile="$(MSBuildProjectDirectory)\..\src\myApp\bin\Debug\myApp.xml" xmlns="" />
</DocumentationSources>

The Builder Builder file for Sandcastle comes with the utils assembly, which has a way to get all the dll and xml files from the specified directory. I want to call a method from this assembly and save its result as a list <DocumentationSource>. This is a static method that returnsCollection<string>

+3
source share
3 answers

, MSBuild. , , :

msbuild:

<UsingTask TaskName="FindFiles" AssemblyFile="FindFiles.dll" />

<!-- 
As you'll see below, SearchDirectory and SearchPatterns are input parameters,
MatchingFiles is an output parameter, SourceFiles is an ItemGroup assigned to
the output.
-->
<FindFiles SearchDirectory="$(MyDirectory)" SearchPatterns="*.dll;*.xml">
    <Output ItemName="SourceFiles" TaskParameter="MatchingFiles" />
</FindFiles>

<!-- You can then use the generated ItemGroup output elsewhere. -->
<DocumentationSources>
    <DocumentationSource sourceFile="@(SourceFiles)" xmlns="" />
</DocumentationSources>

FindFiles.cs:

using System;
using System.IO;
using System.Collections.Generic;

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace FindFiles
{
    public class FindFiles : Task
    {
        // input parameter
        [Required]
        public string SearchDirectory { get; set; }

        // output parameter
        [Required]
        public string[] SearchPatterns { get; set; }

        [Output]
        public string[] MatchingFiles { get; private set; }

        private bool ValidateParameters()
        {
            if (String.IsNullOrEmpty(SearchDirectory))
            {
                return false;
            }
            if (!Directory.Exists(SearchDirectory))
            {
                return false;
            }
            if (SearchPatterns == null || SearchPatterns.Length == 0)
            {
                return false;
            }
            return true;
        }

        // MSBuild tasks use the command pattern, this is where the magic happens,
        // refactor as needed
        public override bool Execute()
        {
            if (!ValidateParameters())
            {
                return false;
            }
            List<string> matchingFiles = new List<string>();
            try
            {

                foreach (string searchPattern in SearchPatterns)
                {
                    matchingFiles.AddRange(
                        Directory.GetFiles(SearchDirectory, searchPattern)
                        );
                }
            }
            catch (IOException)
            {
                // it might be smarter to just let this exception fly, depending on
                // how you want the task to behave
                return false;
            }
            MatchingFiles = matchingFiles.ToArray();
            return true;
        }
    }
}
+2

, , - . , MSBuild 4.

( ):

<Today>$([System.DateTime]::Now)</Today>

:

$([Class]:: Property.Method(Parameters))

, , - , .

+15

Create a custom task that calls this static method and returns an ITaskItem array.

or

You can try using MSBuild Extension Pack Assembly.Invoke :

<PropertyGroup>
  <StaticMethodAssemblyPath>path</StaticMethodAssemblyPath>
</PropertyGroup>

<MSBuild.ExtensionPack.Framework.Assembly TaskAction="Invoke" 
                                          NetArguments="@(ArgsM)" 
                                          NetClass="StaticMethodClassName" 
                                          NetMethod="StaticMethodName" 
                                          NetAssembly="${StaticMethodAssemblyPath}">
        <Output TaskParameter="Result" PropertyName="R"/>
</MSBuild.ExtensionPack.Framework.Assembly>
+3
source

Source: https://habr.com/ru/post/1705244/


All Articles