Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge command and tests #5

Merged
merged 7 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
sudo apt-get install gcc-i686-linux-gnu gcc-x86-64-linux-gnu gcc-aarch64-linux-gnu llvm-14 clang-14

- name: "Setup .NET"
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'

Expand All @@ -44,7 +44,7 @@ jobs:
dotnet test '${{ github.workspace }}/src/cs/tests/c2ffi.Tests.EndToEnd.Extract/c2ffi.Tests.EndToEnd.Extract.csproj' --nologo --verbosity minimal --configuration Release

- name: "Upload generated FFI files"
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: "ffi-${{ matrix.platform.name }}"
path: |
Expand All @@ -63,26 +63,26 @@ jobs:
uses: actions/checkout@v4

- name: "Setup .NET"
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'

- name: "Download generated FFI files: windows"
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: "ffi-windows"
path: |
${{ github.workspace }}/src/c/tests

- name: "Download generated FFI files: linux"
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: "ffi-linux"
path: |
${{ github.workspace }}/src/c/tests

- name: "Download generated FFI files: macos"
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: "ffi-macos"
path: |
Expand All @@ -92,3 +92,10 @@ jobs:
run: |
dotnet test '${{ github.workspace }}/src/cs/tests/c2ffi.Tests.EndToEnd.Merge/c2ffi.Tests.EndToEnd.Merge.csproj' --nologo --verbosity minimal --configuration Release

- name: "Upload generated FFI files"
uses: actions/upload-artifact@v4
with:
name: "ffi-x"
path: |
${{ github.workspace }}/src/c/tests/**/ffi-x/*.json

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ bin/
obj/
/artifacts/
src/c/tests/**/ffi/*.json
src/c/tests/**/ffi-x/*.json
File renamed without changes.
10 changes: 10 additions & 0 deletions src/cs/production/c2ffi.Data/Nodes/CMacroObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public override bool Equals(CNode? other)
return TypeInfo.Equals(other2.TypeInfo) && Value == other2.Value;
}

public bool EqualsWithoutValue(CMacroObject other)
{
if (!base.Equals(other))
{
return false;
}

return TypeInfo.Equals(other.TypeInfo);
}

/// <inheritdoc />
public override int GetHashCode()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Bottlenose Labs Inc. (https://github.com/bottlenoselabs). All rights reserved.
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

using System.Collections.Immutable;
using System.IO.Abstractions;
using bottlenoselabs.Common.Tools;
using c2ffi.Tool.Commands.Merge.Input.Sanitized;
using c2ffi.Tool.Commands.Merge.Input.Unsanitized;

namespace c2ffi.Tool.Commands.Merge.Input;

public sealed class MergeInputSanitizer
{
private readonly IFileSystem _fileSystem;

public MergeInputSanitizer(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}

public MergeOptions Sanitize(UnsanitizedMergeOptions unsanitizedOptions)
{
var directoryPath = _fileSystem.Path.GetFullPath(unsanitizedOptions.InputDirectoryPath);
if (!_fileSystem.Directory.Exists(directoryPath))
{
throw new ToolInputSanitizationException($"The directory '{directoryPath}' does not exist.");
}

var filePaths = _fileSystem.Directory.GetFiles(directoryPath, "*.json").ToImmutableArray();

if (filePaths.IsDefaultOrEmpty)
{
throw new ToolInputSanitizationException($"The directory '{directoryPath}' does not contain any abstract syntax tree `.json` files.");
}

var outputFilePath = _fileSystem.Path.GetFullPath(unsanitizedOptions.OutputFilePath);

var result = new MergeOptions
{
OutputFilePath = outputFilePath,
InputFilePaths = filePaths
};

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Bottlenose Labs Inc. (https://github.com/bottlenoselabs). All rights reserved.
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

using System.Collections.Immutable;

namespace c2ffi.Tool.Commands.Merge.Input.Sanitized;

public class MergeOptions
{
public ImmutableArray<string> InputFilePaths { get; set; } = ImmutableArray<string>.Empty;

public string OutputFilePath { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Bottlenose Labs Inc. (https://github.com/bottlenoselabs). All rights reserved.
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

namespace c2ffi.Tool.Commands.Merge.Input.Unsanitized;

// NOTE: This class is considered un-sanitized input; all strings and other types could be null.
public class UnsanitizedMergeOptions
{
public string InputDirectoryPath { get; set; } = string.Empty;

public string OutputFilePath { get; set; } = string.Empty;
}
19 changes: 11 additions & 8 deletions src/cs/production/c2ffi.Tool/Commands/Merge/MergeFfisCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

using System.CommandLine;
using JetBrains.Annotations;
using c2ffi.Tool.Commands.Merge.Input.Unsanitized;

namespace c2ffi.Tool.Commands.Merge;

[UsedImplicitly]
public class MergeFfisCommand : Command
public sealed class MergeFfisCommand : Command
{
public MergeFfisCommand()
private readonly MergeFfisTool _tool;

public MergeFfisCommand(MergeFfisTool tool)
: base(
"merge",
"Merge multiple target platform FFI (foreign function interface) `.json` files into a cross-platform FFI `.json` file.")
{
_tool = tool;

var directoryOption = new Option<string>(
"--inputDirectoryPath", "The input directory where the multiple target platform FFI (foreign function interface) `.json` files are located.")
{
IsRequired = true
};
{
IsRequired = true
};
AddOption(directoryOption);

var fileOption = new Option<string>(
Expand All @@ -30,6 +33,6 @@ public MergeFfisCommand()

private void Main(string inputDirectoryPath, string outputFilePath)
{
Console.WriteLine("Merge!");
_tool.Run(inputDirectoryPath, outputFilePath);
}
}
Loading