Skip to content

Commit

Permalink
feat: more tests for alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Nov 24, 2024
1 parent 31019b0 commit ca96adb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/dotnet-exec/Commands/AliasCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ internal sealed class AliasCommand : Command
public AliasCommand() : base("alias", "Alias management")
{
AddCommand(new AliasListCommand());
AddCommand(new AliasSetCommand());
AddCommand(new AliasUnsetCommand());
}
}

file sealed class AliasListCommand : Command
{
public AliasListCommand() : base("list", "List all alias config")
{
AddArgument(AliasCommand.AliasNameArg);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-exec/Contracts/AppConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public sealed class AppConfiguration
// theme settings ...
public Dictionary<string, string> Aliases { get; set; } = new()
{
{ "new-guid", "System.Guid.NewGuid()" },
{ "guid", "System.Guid.NewGuid()" },
{ "now", "System.DateTimeOffset.Now" },
{ "date", "System.DateTimeOffset.Now" },
};
Expand Down
8 changes: 4 additions & 4 deletions src/dotnet-exec/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,14 @@ public static void Initialize(this Command command, IServiceProvider serviceProv
"set" => async (InvocationContext context) =>
{
var aliasName = context.ParseResult.GetValueForArgument(AliasCommand.AliasNameArg);

if (IsValidAliasName(aliasName))
if (!IsValidAliasName(aliasName))
{
Console.WriteLine("Invalid alias name, alias name max length is 64 and only allow characters,numbers and `-`/`_`/`:`/`.` ");
Console.WriteLine("Invalid alias name, alias name max length is 64 and only allow characters,numbers and `-`/`_`/`:`/`.` ");
return;
}

var aliasValue = context.ParseResult.GetValueForArgument(AliasCommand.AliasValueArg);
if (!appConfiguration.Aliases.TryGetValue(aliasName, out var currentValue) || currentValue == aliasValue)
if (appConfiguration.Aliases.TryGetValue(aliasName, out var currentValue) && currentValue == aliasValue)
{
return;
}
Expand All @@ -268,6 +267,7 @@ public static void Initialize(this Command command, IServiceProvider serviceProv
return Task.CompletedTask;
}
};
aliasSubCommand.SetHandler(aliasCommandHandler);
}
break;
}
Expand Down
17 changes: 16 additions & 1 deletion src/dotnet-exec/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,22 @@
"alias-test": {
"commandName": "Project",
"targetFramework": "net8.0",
"commandLineArgs": "new-guid"
"commandLineArgs": "date"
},
"alias-list": {
"commandName": "Project",
"targetFramework": "net8.0",
"commandLineArgs": "alias list"
},
"alias-set": {
"commandName": "Project",
"targetFramework": "net8.0",
"commandLineArgs": "alias set guid \"Guid.NewGuid()\""
},
"alias-unset": {
"commandName": "Project",
"targetFramework": "net8.0",
"commandLineArgs": "alias unset guid"
}
}
}
5 changes: 3 additions & 2 deletions src/dotnet-exec/Services/LocalAppConfigSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public async Task<bool> SaveConfigAsync(AppConfiguration appConfig)
Helper.EnsureFolderCreated(folder);
}

using var fs = File.OpenWrite(DefaultConfigPath);
await JsonSerializer.SerializeAsync(fs, appConfig, JsonHelper.UnsafeEncoderOptions);
var bytes = JsonSerializer.SerializeToUtf8Bytes(appConfig, JsonHelper.UnsafeEncoderOptions);
await File.WriteAllBytesAsync(DefaultConfigPath, bytes);

return true;
}
}

0 comments on commit ca96adb

Please sign in to comment.