Skip to content

Commit

Permalink
Update build project with Configuration and TargetFramework in `B…
Browse files Browse the repository at this point in the history
…uild` and `Rebuild`
  • Loading branch information
ricaun committed Nov 20, 2024
1 parent 9493a01 commit ea84d56
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `SymbolPackageFormat` and `IncludeSymbols` in the package.
- Update release `*.nupkg` to `*.*nupkg` to copy symbol package format. (Fix: #75)
- Add `WarningSignFile` to show warning when sign file is empty. (Fix: #74)
- Update build project with `Configuration` and `TargetFramework` in `Build` and `Rebuild`. (Fix: $73)

## [1.8.1] / 2024-05-13
### Updated
Expand Down
232 changes: 175 additions & 57 deletions ricaun.Nuke/Extensions/BuildExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,132 @@ public static class BuildExtension
private const string CONFIGURATION_DEBUG = "Debug";
#endregion

#region ConfigurationTargetPlatform
/// <summary>
/// Configuration Target Platform
/// </summary>
public class ConfigurationTargetPlatform
{
/// <summary>
/// Configuration
/// </summary>
public string Configuration { get; set; }
/// <summary>
/// TargetPlatform
/// </summary>
public string TargetPlatform { get; set; }
/// <summary>
/// ToString
/// </summary>
/// <returns></returns>
public override string ToString() => $"{Configuration}|{TargetPlatform}";
/// <summary>
/// ConfigurationTargetPlatform
/// </summary>
/// <param name="value"></param>
public static implicit operator ConfigurationTargetPlatform(string value)
{
var values = value.Split('|');
return new ConfigurationTargetPlatform
{
Configuration = values.FirstOrDefault(),
TargetPlatform = values.LastOrDefault()
};
}
}

/// <summary>
/// Gets the release target platforms for the specified project.
/// </summary>
/// <param name="project">The project.</param>
/// <returns>The release target platforms.</returns>
public static IEnumerable<ConfigurationTargetPlatform> GetReleaseTargetPlatforms(this Project project)
{
return project.GetConfigurationsTargetPlatform(CONFIGURATION_DEBUG, true);
}

/// <summary>
/// Gets the configuration target platforms for the specified project.
/// </summary>
/// <param name="project">The project.</param>
/// <returns>The configuration target platforms.</returns>
public static IEnumerable<ConfigurationTargetPlatform> GetConfigurationTargetPlatforms(this Project project)
{
return project.Configurations.Values.Distinct().Select(e => (ConfigurationTargetPlatform)e);
}

/// <summary>
/// Gets the configurations target platform for the specified project.
/// </summary>
/// <param name="project">The project.</param>
/// <param name="contain">The string to contain in the configuration.</param>
/// <param name="notContain">A flag indicating whether the configuration should not contain the specified string.</param>
/// <returns>The configurations target platform.</returns>
public static IEnumerable<ConfigurationTargetPlatform> GetConfigurationsTargetPlatform(this Project project, string contain, bool notContain = false)
{
return project.GetConfigurationTargetPlatforms().Where(e => e.Configuration.Contains(contain, StringComparison.OrdinalIgnoreCase) != notContain);
}

/// <summary>
/// Builds the project with the specified configuration target platform.
/// </summary>
/// <param name="project">The project.</param>
/// <param name="value">The configuration target platform.</param>
/// <returns>The outputs of the build.</returns>
public static IReadOnlyCollection<Output> Build(this Project project, ConfigurationTargetPlatform value)
{
return project.Build(value.Configuration, value.TargetPlatform);
}

/// <summary>
/// Rebuilds the project with the specified configuration target platform.
/// </summary>
/// <param name="project">The project.</param>
/// <param name="value">The configuration target platform.</param>
/// <returns>The outputs of the rebuild.</returns>
public static IReadOnlyCollection<Output> Rebuild(this Project project, ConfigurationTargetPlatform value)
{
return project.Rebuild(value.Configuration, value.TargetPlatform);
}

#endregion

#region Solution
/// <summary>
/// Get all Configuration With no Debug
/// Gets all the release configurations for the specified solution.
/// </summary>
/// <param name="Solution"></param>
/// <returns></returns>
public static IEnumerable<string> GetReleases(this Solution Solution)
/// <param name="solution">The solution.</param>
/// <returns>The release configurations.</returns>
[Obsolete($"Use {nameof(BuildExtension)}.{nameof(BuildExtension.GetReleaseTargetPlatforms)}")]
public static IEnumerable<string> GetReleases(this Solution solution)
{
return Solution.GetConfigurations(CONFIGURATION_DEBUG, true);
return solution.GetConfigurations(CONFIGURATION_DEBUG, true);
}

/// <summary>
/// Get Configurations
/// Gets the configurations for the specified solution.
/// </summary>
/// <param name="Solution"></param>
/// <param name="contain"></param>
/// <param name="notContain"></param>
/// <returns></returns>
public static IEnumerable<string> GetConfigurations(this Solution Solution, string contain, bool notContain = false)
/// <param name="solution">The solution.</param>
/// <param name="contain">The string to contain in the configuration.</param>
/// <param name="notContain">A flag indicating whether the configuration should not contain the specified string.</param>
/// <returns>The configurations.</returns>
[Obsolete($"Use {nameof(BuildExtension)}.{nameof(BuildExtension.GetConfigurationsTargetPlatform)}")]
public static IEnumerable<string> GetConfigurations(this Solution solution, string contain, bool notContain = false)
{
var configurations = Solution.GetConfigurations()
var configurations = solution.GetConfigurations()
.Where(s => s.Contains(contain, StringComparison.OrdinalIgnoreCase) != notContain);
return configurations;
}

/// <summary>
/// Get Configurations
/// Gets the configurations for the specified solution.
/// </summary>
/// <param name="Solution"></param>
/// <returns></returns>
public static IEnumerable<string> GetConfigurations(this Solution Solution)
/// <param name="solution">The solution.</param>
/// <returns>The configurations.</returns>
[Obsolete($"Use {nameof(BuildExtension)}.{nameof(BuildExtension.GetConfigurationTargetPlatforms)}")]
public static IEnumerable<string> GetConfigurations(this Solution solution)
{
var configurations = Solution.Configurations
var configurations = solution.Configurations
.Select(pair => pair.Value.Split("|").First())
.Distinct();
return configurations;
Expand All @@ -63,16 +156,16 @@ public static IEnumerable<string> GetConfigurations(this Solution Solution)

#region BuildMainProject
/// <summary>
/// Build Project
/// Builds the specified project.
/// </summary>
/// <param name="Solution"></param>
/// <param name="project"></param>
/// <param name="afterBuild"></param>
public static void BuildProject(this Solution Solution, Project project, Action<Project> afterBuild = null)
/// <param name="solution">The solution.</param>
/// <param name="project">The project.</param>
/// <param name="afterBuild">The action to perform after the build.</param>
public static void BuildProject(this Solution solution, Project project, Action<Project> afterBuild = null)
{
if (project is Project)
{
foreach (var configuration in project.GetReleases())
foreach (var configuration in project.GetReleaseTargetPlatforms())
{
project.Build(configuration);
}
Expand All @@ -81,16 +174,16 @@ public static void BuildProject(this Solution Solution, Project project, Action<
}

/// <summary>
/// Rebuild Project
/// Rebuilds the specified project.
/// </summary>
/// <param name="Solution"></param>
/// <param name="project"></param>
/// <param name="afterBuild"></param>
public static void RebuildProject(this Solution Solution, Project project, Action<Project> afterBuild = null)
/// <param name="solution">The solution.</param>
/// <param name="project">The project.</param>
/// <param name="afterBuild">The action to perform after the rebuild.</param>
public static void RebuildProject(this Solution solution, Project project, Action<Project> afterBuild = null)
{
if (project is Project)
{
foreach (var configuration in project.GetReleases())
foreach (var configuration in project.GetReleaseTargetPlatforms())
{
project.Rebuild(configuration);
}
Expand All @@ -99,14 +192,14 @@ public static void RebuildProject(this Solution Solution, Project project, Actio
}

/// <summary>
/// Delete All bin / obj folder
/// Deletes all the bin/obj folders except for the ones in the specified build project directory.
/// </summary>
/// <param name="Solution"></param>
/// <param name="BuildProjectDirectory"></param>
public static void ClearSolution(this Solution Solution, AbsolutePath BuildProjectDirectory)
/// <param name="solution">The solution.</param>
/// <param name="buildProjectDirectory">The build project directory.</param>
public static void ClearSolution(this Solution solution, AbsolutePath buildProjectDirectory)
{
Globbing.GlobDirectories(Solution.Directory, "**/bin", "**/obj")
.Where(x => !PathConstruction.IsDescendantPath(BuildProjectDirectory, x))
Globbing.GlobDirectories(solution.Directory, "**/bin", "**/obj")
.Where(x => !PathConstruction.IsDescendantPath(buildProjectDirectory, x))
.ForEach((file) =>
{
try
Expand All @@ -123,22 +216,22 @@ public static void ClearSolution(this Solution Solution, AbsolutePath BuildProje

#region Project
/// <summary>
/// Get Project Configurations with no Debug
/// Gets the release configurations for the specified project.
/// </summary>
/// <param name="project"></param>
/// <returns></returns>
/// <param name="project">The project.</param>
/// <returns>The release configurations.</returns>
public static IEnumerable<string> GetReleases(this Project project)
{
return project.GetConfigurations(CONFIGURATION_DEBUG, true);
}

/// <summary>
/// Get Project Configurations
/// Gets the configurations for the specified project.
/// </summary>
/// <param name="project"></param>
/// <param name="contain"></param>
/// <param name="notContain"></param>
/// <returns></returns>
/// <param name="project">The project.</param>
/// <param name="contain">The string to contain in the configuration.</param>
/// <param name="notContain">A flag indicating whether the configuration should not contain the specified string.</param>
/// <returns>The configurations.</returns>
public static IEnumerable<string> GetConfigurations(this Project project, string contain, bool notContain = false)
{
var configurations = project.GetConfigurations()
Expand All @@ -147,10 +240,10 @@ public static IEnumerable<string> GetConfigurations(this Project project, string
}

/// <summary>
/// Get Project Configurations
/// Gets the configurations for the specified project.
/// </summary>
/// <param name="project"></param>
/// <returns></returns>
/// <param name="project">The project.</param>
/// <returns>The configurations.</returns>
public static IEnumerable<string> GetConfigurations(this Project project)
{
var configurations = project.Configurations
Expand All @@ -160,16 +253,19 @@ public static IEnumerable<string> GetConfigurations(this Project project)
}

/// <summary>
/// Rebuild Project
/// Rebuilds the project with the specified configuration and target platform.
/// </summary>
/// <param name="project"></param>
/// <param name="configuration"></param>
public static IReadOnlyCollection<Output> Rebuild(this Project project, string configuration)
/// <param name="project">The project.</param>
/// <param name="configuration">The configuration.</param>
/// <param name="targetPlatform">The target platform.</param>
/// <returns>The outputs of the rebuild.</returns>
public static IReadOnlyCollection<Output> Rebuild(this Project project, string configuration, string targetPlatform = null)
{
return MSBuildTasks.MSBuild(s => s
.SetTargets("Rebuild")
.SetTargetPath(project)
.SetConfiguration(configuration)
.TrySetTargetPlatform(targetPlatform)
.SetVerbosity(MSBuildVerbosity.Minimal)
.SetMaxCpuCount(Environment.ProcessorCount)
.DisableNodeReuse()
Expand All @@ -178,30 +274,52 @@ public static IReadOnlyCollection<Output> Rebuild(this Project project, string c
}

/// <summary>
/// Build Project
/// Builds the project with the specified configuration and target platform.
/// </summary>
/// <param name="project"></param>
/// <param name="configuration"></param>
public static IReadOnlyCollection<Output> Build(this Project project, string configuration)
/// <param name="project">The project.</param>
/// <param name="configuration">The configuration.</param>
/// <param name="targetPlatform">The target platform.</param>
/// <returns>The outputs of the build.</returns>
public static IReadOnlyCollection<Output> Build(this Project project, string configuration, string targetPlatform = null)
{
return MSBuildTasks.MSBuild(s => s
.SetTargets("Build")
.SetTargetPath(project)
.SetConfiguration(configuration)
.TrySetTargetPlatform(targetPlatform)
.SetVerbosity(MSBuildVerbosity.Minimal)
.SetMaxCpuCount(Environment.ProcessorCount)
.DisableNodeReuse()
.EnableRestore()
);
}

private static MSBuildSettings TrySetTargetPlatform(this MSBuildSettings settings, MSBuildTargetPlatform targetPlatform)
{
if (string.IsNullOrWhiteSpace(targetPlatform)) return settings;

var validPlatforms = new[] {
MSBuildTargetPlatform.MSIL,
MSBuildTargetPlatform.x86,
MSBuildTargetPlatform.x64,
MSBuildTargetPlatform.arm,
MSBuildTargetPlatform.Win32
};
if (validPlatforms.Contains(targetPlatform))
{
return settings.SetTargetPlatform(targetPlatform);
}

return settings;
}
#endregion

#region String
/// <summary>
/// Return false if <paramref name="str"/> empty or null
/// Returns false if the specified string is empty or null.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
/// <param name="str">The string.</param>
/// <returns><c>true</c> if the string is not empty or null; otherwise, <c>false</c>.</returns>
public static bool SkipEmpty(this string str)
{
if (str == null) return false;
Expand Down
2 changes: 1 addition & 1 deletion ricaun.Nuke/ricaun.Nuke.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<PropertyGroup>
<PackageId>ricaun.Nuke</PackageId>
<Version>1.8.2-alpha</Version>
<Version>1.8.2-beta</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit ea84d56

Please sign in to comment.