Skip to content

Commit

Permalink
Move functionality from update-config.csx to binderator. (#927)
Browse files Browse the repository at this point in the history
As the scope of `update-config.csx` has grown, it has become more and more painful that it isn't in an actual project editable in an IDE, making it harder to update or debug.  As it shares code with `binderator`, like reading and writing `config.json`, move the functionality into `binderator`.

To help facilitate running this functionality, add new targets to `cake` for the various operations:

*   `update-config`

    Updates config.json to the latest versions found in Maven.
    
*   `bump-config`

    Increments the NuGet patch version of all packages in config.json.
    
*   `sort-config`

    Sorts config.json file using the canonical sort.
    
*   `published-config`

    Shows which NuGet package versions in config.json have been published to NuGet.org.

Note these run the in-tree version of `binderator`, there is no need to install the .NET Global tool version to run these targets.
  • Loading branch information
jpobst committed Aug 1, 2024
1 parent 08ee35f commit 6acd20e
Show file tree
Hide file tree
Showing 28 changed files with 813 additions and 208 deletions.
16 changes: 16 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ dotnet cake -t=clean && dotnet cake -t=ci && dotnet cake nuget-diff.cake && dotn
* `ci`

Builds projects on CI (`libs`, `nuget`, `samples`).

* `update-config`

Updates config.json to the latest versions found in Maven.

* `bump-config`

Increments the NuGet patch version of all packages in config.json.

* `sort-config`

Sorts config.json file using the canonical sort.

* `published-config`

Shows which NuGet package versions in config.json have been published to NuGet.org.

#### Nuget API diff (`nuget-diff.cake`)

Expand Down
2 changes: 2 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// #addin nuget:?package=NuGet.Versioning&loaddependencies=true&version=5.6.0
// #addin nuget:?package=Microsoft.Extensions.Logging&loaddependencies=true&version=3.0.0

#load "build/cake/update-config.cake"

using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
Expand Down
54 changes: 54 additions & 0 deletions build/cake/update-config.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Contains tasks for updating/modifying the config.json used by binderator

var binderator_project = "util/Xamarin.AndroidBinderator/Xamarin.AndroidBinderator.Tool/Xamarin.AndroidBinderator.Tool.csproj";
var gps_config = "https://raw.githubusercontent.com/xamarin/GooglePlayServicesComponents/main/config.json";

// Updates config.json to the latest versions found in Maven
Task ("update-config")
.Does (() =>
{
var args = new ProcessArgumentBuilder ()
.Append ("update")
.Append ("--config-file")
.Append ("config.json")
.Append ("--dependency-file")
.Append (gps_config);
DotNetRun (binderator_project, args);
});

// Increments the NuGet patch version of all packages in config.json
Task ("bump-config")
.Does (() =>
{
var args = new ProcessArgumentBuilder ()
.Append ("bump")
.Append ("--config-file")
.Append ("config.json");
DotNetRun (binderator_project, args);
});

// Sorts config.json file using the canonical sort
Task ("sort-config")
.Does (() =>
{
var args = new ProcessArgumentBuilder ()
.Append ("sort")
.Append ("--config-file")
.Append ("config.json");
DotNetRun (binderator_project, args);
});

// Shows which NuGet package versions in config.json have been published to NuGet.org
Task ("published-config")
.Does (() =>
{
var args = new ProcessArgumentBuilder ()
.Append ("published")
.Append ("--config-file")
.Append ("config.json");
DotNetRun (binderator_project, args);
});
4 changes: 4 additions & 0 deletions util/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Project>
<!-- This Directory.Build.props is to prevent the one in the root from being used,
which is tuned for building bindings packages. -->
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,14 @@ public Task MetadataIsAppendedToDependencies()
</Project>");
}

public async Task ProcessAndAssertTemplate(string input, BindingConfig config, string output, Dictionary<string, string> metadata = null)
async Task ProcessAndAssertTemplate(string input, BindingConfig config, string output, Dictionary<string, string> metadata = null)
{
var generated = Path.Combine(RootDirectory, "generated");
var outputFile = Path.Combine(generated, "Generated.csproj");
var templateFile = CreateTemplate(input);

config.BasePath = RootDirectory;
config.Templates.Add(new TemplateConfig(templateFile, outputFile) { Metadata = metadata });
config.Templates.Add(new TemplateConfig(templateFile, outputFile) { Metadata = metadata ?? [] });

await Engine.BinderateAsync(config);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<PreserveCompilationContext>true</PreserveCompilationContext>
<Nullable>disable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using AndroidBinderator;
using Newtonsoft.Json;

namespace Xamarin.AndroidBinderator.Tool;

static class BinderateCommand
{
public static Command Build ()
{
var binderate_command = new Command ("binderate", "Run the binderator process.");

var config_file_option = new Option<string []> (["--config-file", "-c", "-config="], "JSON config file(s)") { AllowMultipleArgumentsPerToken = true, IsRequired = true };
var base_dir_option = new Option<string?> (["--base-path", "-b", "-basepath="], "Default base path");

binderate_command.Add (config_file_option);
binderate_command.Add (base_dir_option);

binderate_command.SetHandler (
RunBinderateVerb,
config_file_option, base_dir_option
);

return binderate_command;
}

public static async Task RunBinderateVerb (string [] configFiles, string? basePath)
{
Console.WriteLine ("Arguments:");

foreach (var c in configFiles)
Console.WriteLine ($"- Config File: {c}");
Console.WriteLine ($"- Default Base Path: {basePath}");
Console.WriteLine ();

basePath ??= "AppDomain.CurrentDomain.BaseDirectory";

try {
foreach (var config in configFiles) {
var cfgs = JsonConvert.DeserializeObject<List<BindingConfig>> (File.ReadAllText (config));

foreach (var c in cfgs ?? []) {
if (string.IsNullOrEmpty (c.BasePath))
c.BasePath = basePath;

await Engine.BinderateAsync (c);
}
}
} catch (AggregateException exc_a) {
var sb = new StringBuilder ();

sb.AppendLine ();
sb.AppendLine ($"Dependency errors : {exc_a.InnerExceptions.Count}");

var i = 1;

foreach (var exc in exc_a.InnerExceptions) {
sb.AppendLine ($"{i}");
sb.AppendLine ($" {exc.ToString ()}");
i++;
}

Trace.WriteLine (sb.ToString ());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.CommandLine;
using System.Linq;
using System.Threading.Tasks;
using AndroidBinderator;

namespace Xamarin.AndroidBinderator.Tool;

static class BumpCommand
{
public static Command Build ()
{
var update_command = new Command ("bump", "Increments the NuGet patch version of all packages in a config.json file.");

var config_file_option = new Option<string> (["--config-file", "-c", "-config="], "JSON config file") { IsRequired = true };

update_command.Add (config_file_option);

update_command.SetHandler (
RunBumpCommand,
config_file_option
);

return update_command;
}

static async Task RunBumpCommand (string configFile)
{
var config = await BindingConfig.Load (configFile);

foreach (var art in config.MavenArtifacts) {
var version = "";
var release = "";
var revision = 0;

var str = art.NugetVersion ?? string.Empty;

if (str.Contains ('-')) {
release = str.Substring (str.IndexOf ('-'));
str = str.Substring (0, str.LastIndexOf ('-'));
}

var period_count = str.Count (c => c == '.');

if (period_count == 2) {
version = str;
revision = 1;
} else if (period_count == 3) {
version = str.Substring (0, str.LastIndexOf ('.'));
revision = int.Parse (str.Substring (str.LastIndexOf ('.') + 1));
revision++;
}

art.NugetVersion = $"{version}.{revision}{release}";
}

config.Save (configFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Mono.Options;

namespace Xamarin.AndroidBinderator.Tool;

class LegacyCommandLine
{
public static async Task Run (string [] args)
{
var configs = new List<string> ();
string? basePath = null;
var shouldShowHelp = false;

// thses are the available options, not that they set the variables
var options = new OptionSet {
{ "c|config=", "JSON Config File.", configs.Add },
{ "b|basepath=", "Default Base Path.", (string b) => basePath= b },
{ "h|help", "show this message and exit", h => shouldShowHelp = h != null },
};

List<string> extra;

try {
// parse the command line
extra = options.Parse (args);
} catch (OptionException e) {
// output some error message
Console.Write ("android-binderator: ");
Console.WriteLine (e.Message);
Console.WriteLine ("Try `android-binderator --help' for more information.");
return;
}

if (shouldShowHelp) {
options.WriteOptionDescriptions (Console.Out);
return;
}

await BinderateCommand.RunBinderateVerb (configs.ToArray (), basePath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.CommandLine;
using System.Linq;
using System.Threading.Tasks;
using AndroidBinderator;

namespace Xamarin.AndroidBinderator.Tool;

static class PublishedCommand
{
public static Command Build ()
{
var update_command = new Command ("published", "Shows which NuGet package versions in a config.json have been published to NuGet.org.");

var config_file_option = new Option<string> (["--config-file", "-c", "-config="], "JSON config file") { IsRequired = true };

update_command.Add (config_file_option);

update_command.SetHandler (
RunPublishedCommand,
config_file_option
);

return update_command;
}

static async Task RunPublishedCommand (string configFile)
{
var config = await BindingConfig.Load (configFile);

var column1 = "Package".PadRight (58);
var column2 = "Version".PadRight (17);
var column3 = "Status".PadRight (15);

Console.WriteLine ($"| {column1} | {column2} | {column3} |");
Console.WriteLine ("|------------------------------------------------------------|-------------------|-----------------|");

foreach (var art in config.MavenArtifacts.Where (a => !a.DependencyOnly)) {
if (art.NugetPackageId is null || art.NugetVersion is null)
continue;

var status = (await ConfigUpdater.DoesNuGetPackageAlreadyExist (art.NugetPackageId, art.NugetVersion)) ? "Published" : "Not Published";
Console.WriteLine ($"| {art.NugetPackageId.PadRight (58)} | {art.NugetVersion.PadRight (17)} | {status.PadRight (15)} |");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.CommandLine;
using System.Threading.Tasks;
using AndroidBinderator;

namespace Xamarin.AndroidBinderator.Tool;

static class SortCommand
{
public static Command Build ()
{
var update_command = new Command ("sort", "Sorts a config.json file using the canonical sort.");

var config_file_option = new Option<string> (["--config-file", "-c", "-config="], "JSON config file") { IsRequired = true };

update_command.Add (config_file_option);

update_command.SetHandler (
RunSortCommand,
config_file_option
);

return update_command;
}

static async Task RunSortCommand (string configFile)
{
// BindingConfig.Load automatically sorts the file, so we just need to write the file back to disk
var config = await BindingConfig.Load (configFile);

config.Save (configFile);
}
}
Loading

0 comments on commit 6acd20e

Please sign in to comment.