FrankVDB
FrankVDB

Reputation: 235

TFS 2017 - Get the Solution's name in C#

I'd like to know how can I get the name of the solution that the XAML Build Definition is using.

For instance in the configuration of my Build Defnition I have this path to chose which solution I want to apply this Build Definition: $/test/SolutionName/SolutionName.sln

How can I get "SolutionName" for my Activity Code, using C# code.

EDIT: My variable, what can I put as Default yield? enter image description here

The code:

public InArgument<string> BuildSettings { get; set; }
....
string nameSolutionAuto = System.IO.Path.GetFileNameWithoutExtension(this.BuildSettings.ProjectsToBuild[0]);

Upvotes: 0

Views: 916

Answers (2)

starian chen-MSFT
starian chen-MSFT

Reputation: 33698

The projects/solutions info is stored in ProjectsToBuild variable, so you just need to pass this variable value to your Activity (InArgument) and get the solution or project name by this code (InArgument variable:solutionPaths):

      foreach(string solution in solutionPaths)
        {
            Console.WriteLine(solution.Split(new char[] { '/' }).Last());
        }

An example to display selected solutions/projects in build definition to build log:

  1. Add ForEach active (TypeArgument: String; Values: ProjectsToBuild
  2. Add WriteBuildMessage active to ForEach active body (Importance: Microsoft.TeamFoundation.Build.Client.BuildMessageImportance.High; Message: item
  3. Queue build with this process template

enter image description here

Upvotes: 1

Giulio Vian
Giulio Vian

Reputation: 8343

Your Activity needs a property of type BuildSettings, which you set with the value of the BuildSettings variable.

In the Activity code, examine the content of the ProjectsToBuild property and extract the name of the solution file, e.g. System.IO.Path.GetFileNameWithoutExtension(this.BuildSettings.ProjectsToBuild[0]).

EDIT

The BuildSettings variable is defined globally, no need to re-define it.

The BuildSettings property should be

public InArgument<Microsoft.TeamFoundation.Build.Workflow.Activities.BuildSettings> BuildSettings { get; set; }

and you have to assign the BuildSettings variable to this property in the XAML Property Window.

To use it the code should be more like

var buildSettings = GetValue<Microsoft.TeamFoundation.Build.Workflow.Activities.BuildSettings>(this.BuildSettings);
string nameSolutionAuto = System.IO.Path.Get
FileNameWithoutExtension(buildSettings.ProjectsToBuild[0]);

Upvotes: 2

Related Questions