adachsoft / code-operations-csharp
C# and .NET project operations for the AdachSoft code operations contracts.
Package info
gitlab.com/a.adach/code-operations-csharp
pkg:composer/adachsoft/code-operations-csharp
Requires
- php: ^8.3
- adachsoft/code-operations-contracts: ^0.2
- adachsoft/command-executor-lib: ^2.4
- adachsoft/filesystem: ^1.4
Requires (Dev)
- adachsoft/php-code-style: ^0.9.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A C#/.NET implementation of the adachsoft/code-operations-contracts API. It discovers installed .NET SDKs and provides project validation, test execution, and application run operations through adachsoft/command-executor-lib.
Installation
Install the package with Composer:
composer require adachsoft/code-operations-csharp
Requirements
- PHP 8.3 or newer
- A .NET SDK available as
dotnetinPATH, or an explicit executable path - Composer dependencies declared by this package
The installed SDK must support the target framework used by the project.
Usage
On Linux or Windows, use the default dotnet executable from PATH:
use AdachSoft\CodeOperationsContracts\ExecutionContextDto;
use AdachSoft\CodeOperationsContracts\RuntimeVo;
use AdachSoft\CodeOperationsCsharp\CsharpCodeOperations;
use AdachSoft\CodeOperationsCsharp\DotnetRuntimeResolver;
use AdachSoft\CommandExecutorLib\SimpleCommandRunner;
$runner = new SimpleCommandRunner();
$resolver = new DotnetRuntimeResolver($runner);
$operations = new CsharpCodeOperations($resolver, $runner);
$context = new ExecutionContextDto(
projectPath: '/path/to/project',
runtime: new RuntimeVo('csharp', null),
);
$build = $operations->validate($context);
$tests = $operations->test($context, 'FullyQualifiedName~ParserTest');
$run = $operations->run($context, 'App.csproj', ['--sample-argument']);
When PHP runs in WSL and the .NET SDK is installed on Windows, pass the .exe path explicitly:
$dotnetBinary = '/mnt/c/Program Files/dotnet/dotnet.exe';
$resolver = new DotnetRuntimeResolver($runner, $dotnetBinary);
$operations = new CsharpCodeOperations($resolver, $runner);
$context = new ExecutionContextDto(
projectPath: '/mnt/d/Programing/my-project',
runtime: new RuntimeVo('csharp', null),
);
WSL mode is detected when PHP runs on Linux and the configured executable ends in .exe. In that mode, the library forwards required environment variable names through WSLENV and converts Windows diagnostic paths under drive roots to /mnt/<drive> paths. Projects and working directories used with Windows interop should be on a mounted drive, such as /mnt/d/....
Remote projects
Both DotnetRuntimeResolver and CsharpCodeOperations accept an optional final FileSystemInterface argument for projects stored outside the local machine. Pass a filesystem rooted at the project directory and a CommandRunnerInterface that executes commands in the same remote workspace. The project path is supplied later through ExecutionContextDto; filesystem paths remain relative to its root (. for the project root and global.json for the SDK configuration). Without a filesystem argument, the existing local-disk behavior is unchanged.
$resolver = new DotnetRuntimeResolver($runner, 'dotnet', $remoteFileSystem);
$operations = new CsharpCodeOperations($resolver, $runner, fileSystem: $remoteFileSystem);
SDK selection
DotnetRuntimeResolver reads sdk.version from global.json in the project root. Without that setting it selects the highest installed SDK. Requested versions are matched as prefixes against dotnet --list-sdks; when global.json selects a different SDK, the resolver reports that SDK selection is controlled by global.json.
Integration tests
The default PHPUnit suite includes the csharp integration group. Run that group directly with:
vendor/bin/phpunit --group csharp
The integration test uses DOTNET_BINARY when set, otherwise it looks for dotnet in PATH. If neither is available, PHPUnit marks the integration test skipped. Set CODE_OPERATIONS_TMP to a writable directory when a specific temporary location is required; when using a Windows .exe through WSL, it must be under /mnt/<drive>.