Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Nov 24, 2023
1 parent be9953a commit 250aa0b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 7 deletions.
23 changes: 23 additions & 0 deletions CliWrap.Magic/FormattedToString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Globalization;

namespace CliWrap.Magic;

public readonly partial struct FormattedToString
{
public string Value { get; }

public FormattedToString(string value) => Value = value;
}

public partial struct FormattedToString
{
public static implicit operator FormattedToString(string value) => new(value);

public static implicit operator FormattedToString(bool value) => new(value.ToString());

public static implicit operator FormattedToString(IFormattable value) =>

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed

Check failure on line 19 in CliWrap.Magic/FormattedToString.cs

View workflow job for this annotation

GitHub Actions / main / test (windows-latest)

'FormattedToString.implicit operator FormattedToString(IFormattable)': user-defined conversions to or from an interface are not allowed
new(value.ToString(null, CultureInfo.InvariantCulture));

public static implicit operator string(FormattedToString value) => value.Value;
}
58 changes: 51 additions & 7 deletions CliWrap.Magic/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,60 @@

## Usage

**CliWrap.Magic** provides a static `Shell` class that contains a various methods for creating and executing commands.
The recommended way to use it is by statically importing the class:
### Quick overview

Add `using static CliWrap.Magic.Shell;` to your file and start writing scripts like this:

```csharp
using CliWrap.Magic;
using static CliWrap.Magic.Shell;

// ...
// Create commands using the _() method, execute them simply by awaiting.
// Check for exit code directly in if statements.
if (!await _("git"))
{
WriteErrorLine("Git is not installed");
Exit(1);
return;
}

// Executing a command returns an object which has implicit conversions to:
// - int (exit code)
// - bool (exit code == 0)
// - string (standard output)
string version = await _("git", "--version"); // git version 2.43.0.windows.1
WriteLine($"Git version: {version}");

// Just like with regular CliWrap, arguments are automatically
// escaped to form a well-formed command line string.
// Non-string arguments of many different types can also be passed directly.
await _("git", "clone", "https://github.com/Tyrrrz/CliWrap", "--depth", 0);

// Resolve environment variables easily with the Environment() method.
var commit = Environment("HEAD_SHA");

// Prompt the user for additional input with the Prompt() method.
if (string.IsNullOrWhiteSpace(commit))
commit = Prompt("Enter commit hash");

// Just like with regular CliWrap, arguments are automatically
// escaped to form a well-formed command line string.
await _("git", "checkout", commit);

// Set environment variables using the Environment() method.
// This returns an object that you can dispose to restore the original value.
using (Environment("HEAD_SHA", "deadbeef"))
await _("/bin/sh", "-c", "echo $HEAD_SHA"); // deadbeef
// Same with the WorkingDirectory() method.
using (WorkingDirectory("/tmp/my-script/"))
// Get the current working directory using the same method.
var cwd = WorkingDirectory();

// Magic also supports CliWrap's piping syntax.
var commits = new List<string>(); // this will contain commit hashes
await (
_("git", "log", "--pretty=format:%H") | commits.Add
);
```

### Executing commands
Expand All @@ -37,6 +83,4 @@ Piping works the same way as it does in regular **CliWrap**:

```csharp
await ("standard input" | _("dotnet", "run"));
```

### Tools
```
49 changes: 49 additions & 0 deletions CliWrap.Magic/Shell.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CliWrap.Magic.Utils;

namespace CliWrap.Magic;
Expand Down Expand Up @@ -48,6 +49,18 @@ public static Command _(string targetFilePath, IEnumerable<string> arguments) =>
/// </summary>
public static Command _(string targetFilePath, params string[] arguments) =>
_(targetFilePath, (IEnumerable<string>)arguments);

/// <summary>
/// Creates a new command with the specified target file path and command-line arguments.
/// </summary>
public static Command _(string targetFilePath, IEnumerable<FormattedToString> arguments) =>
_(targetFilePath).WithArguments(a => a.Add(arguments.Select(x => x.Value)));

/// <summary>
/// Creates a new command with the specified target file path and command-line arguments.
/// </summary>
public static Command _(string targetFilePath, params FormattedToString[] arguments) =>
_(targetFilePath, (IEnumerable<FormattedToString>)arguments);

/// <summary>
/// Gets the current working directory.
Expand Down Expand Up @@ -87,4 +100,40 @@ public static IDisposable Environment(string name, string? value)

return Disposable.Create(() => System.Environment.SetEnvironmentVariable(name, lastValue));
}

/// <summary>
/// Terminates the current process with the specified exit code.
/// </summary>
public static void Exit(int exitCode = 0) => System.Environment.Exit(exitCode);

/// <summary>
/// Prompt the user for input, with an optional message.
/// </summary>
public static string? Prompt(string? message = null)
{
if (!string.IsNullOrWhiteSpace(message))
Console.Write(message);

return Console.ReadLine();
}

/// <summary>
/// Writes the specified text to the standard output stream.
/// </summary>
public static void Write(string text) => Console.Write(text);

/// <summary>
/// Writes the specified text to the standard output stream, followed by a line terminator.
/// </summary>
public static void WriteLine(string text) => Write(text + System.Environment.NewLine);

/// <summary>
/// Writes the specified text to the standard error stream.
/// </summary>
public static void WriteError(string text) => Console.Error.Write(text);

/// <summary>
/// Writes the specified text to the standard error stream, followed by a line terminator.
/// </summary>
public static void WriteErrorLine(string text) => WriteError(text + System.Environment.NewLine);
}

0 comments on commit 250aa0b

Please sign in to comment.