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

Robot #7

Merged
merged 2 commits into from
Apr 20, 2023
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
2 changes: 1 addition & 1 deletion src/Joba.IBM.RPA.Cli/Client/RpaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public async Task CreateOrUpdateAsync(ServerBot bot, CancellationToken cancellat
if (server == null)
_ = await CreateAsync(bot, cancellation);
else
_ = await UpdateAsync(server.Id, bot, cancellation);
_ = await UpdateAsync(server.Id, ServerBot.Copy(bot, server.UniqueId), cancellation);
}

private async Task<ServerBotSearch> UpdateAsync(Guid id, ServerBot bot, CancellationToken cancellation)
Expand Down
17 changes: 7 additions & 10 deletions src/Joba.IBM.RPA.Cli/Robot/RobotCommand.New.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Logging;
using System.Reflection;
using static Joba.IBM.RPA.Cli.ProjectCommand;

namespace Joba.IBM.RPA.Cli
{
Expand All @@ -12,7 +11,7 @@ internal class NewBotCommand : Command
public const string CommandName = "new";
public NewBotCommand() : base(CommandName, "Scaffolds wal files based on the templates.")
{
var name = new Argument<WalFileName>("name", arg => new WalFileName(arg.Tokens[0].Value), description: "The bot name.");
var name = new Argument<string>("name", description: "The bot name.");
var template = new Option<string>("--template", () => "unattended", "The template to use. Depending on the template, different configurations are allowed.")
.FromAmong("attended", "chatbot", "unattended", "excel");

Expand All @@ -24,10 +23,8 @@ public NewBotCommand() : base(CommandName, "Scaffolds wal files based on the tem
Bind.FromServiceProvider<InvocationContext>());
}

private async Task HandleAsync(WalFileName name, string templateName, ILogger<RobotCommand> logger, IProject project, InvocationContext context)
private async Task HandleAsync(string name, string templateName, ILogger<RobotCommand> logger, IProject project, InvocationContext context)
{
var cancellation = context.GetCancellationToken();

var handler = new NewRobotHandler(logger, project);
await handler.HandleAsync(name, templateName, context.GetCancellationToken());
}
Expand All @@ -43,15 +40,15 @@ public NewRobotHandler(ILogger logger, IProject project)
this.project = project;
}

internal async Task HandleAsync(WalFileName name, string templateName, CancellationToken cancellation)
internal async Task HandleAsync(string name, string templateName, CancellationToken cancellation)
{
if (project.Robots.Exists(name.WithoutExtension))
if (project.Robots.Exists(name))
throw new ProjectException($"Robot named '{name}' already exists.");

var template = new TemplateFactory(project.WorkingDirectory, Assembly.GetExecutingAssembly());
_ = await template.CreateAsync(name, templateName, cancellation);
var settings = RobotSettingsFactory.Create(templateName, name.WithoutExtension);
project.Robots.Add(name.WithoutExtension, settings);
_ = await template.CreateAsync(new WalFileName(name), templateName, cancellation);
var settings = RobotSettingsFactory.Create(templateName, name);
project.Robots.Add(name, settings);

await project.SaveAsync(cancellation);
logger.LogInformation("Bot '{Bot}' created successfully based on '{Template}' template", name, templateName);
Expand Down
7 changes: 6 additions & 1 deletion src/Joba.IBM.RPA/IRpaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ public interface IComputerGroupResource
}

public record class ComputerGroup(Guid Id, string Name);
public record class ServerBot(Guid ProjectId, Guid ScriptId, Guid ScriptVersionId, [property: JsonPropertyName("GroupId")] Guid ComputerGroupId, string Name, [property: JsonPropertyName("TechnicalName")] string UniqueId, string Description);
public record class ServerBot(Guid ProjectId, Guid ScriptId, Guid ScriptVersionId, [property: JsonPropertyName("GroupId")] Guid ComputerGroupId, string Name, [property: JsonPropertyName("TechnicalName")] string UniqueId, string Description)
{
public static ServerBot Copy(ServerBot bot, string uniqueId) =>
new(bot.ProjectId, bot.ScriptId, bot.ScriptVersionId, bot.ComputerGroupId, bot.Name, uniqueId, bot.Description);
}

public record class ServerProject(Guid Id, string Name, string Description, [property: JsonPropertyName("TechnicalName")] string UniqueId);
public record class Parameter([property: JsonPropertyName("Id")] string Name, string Value);
public record class PublishScript(Guid? Id, Guid? VersionId, string Name, string? Description, string Content, string ProductVersion,
Expand Down
5 changes: 2 additions & 3 deletions src/Tests/Joba.IBM.RPA.Cli.Tests/NewRobotHandlerShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task ThrowException_WhenBotAlreadyExists()

//assert
_ = await Assert.ThrowsAsync<ProjectException>(
async () => await sut.HandleAsync(new WalFileName("Assistant"), "unattended", CancellationToken.None));
async () => await sut.HandleAsync("Assistant", "unattended", CancellationToken.None));
}

[Fact]
Expand All @@ -37,11 +37,10 @@ public async Task CreateWalAndUpdateProjectFile()
var sut = new NewRobotHandler(logger, project);

//act
await sut.HandleAsync(new WalFileName(nameof(CreateWalAndUpdateProjectFile)), "unattended", CancellationToken.None);
await sut.HandleAsync(nameof(CreateWalAndUpdateProjectFile), "unattended", CancellationToken.None);

//assert
var filePath = Path.Combine(project.WorkingDirectory.FullName, $"{nameof(CreateWalAndUpdateProjectFile)}.wal");
//var filePath = Path.GetFullPath(Path.Combine("assets", $"{nameof(NewRobotHandlerShould)}", nameof(CreateWalAndUpdateProjectFile), $"{nameof(CreateWalAndUpdateProjectFile)}.wal"));
Assert.True(File.Exists(filePath), $"File {filePath} should have been created");
await VerifyFile(Path.Combine(workingDir.FullName, $"{project.Name}.rpa.json"))
.UseDirectory(Path.GetFullPath(Path.Combine("assets", $"{nameof(NewRobotHandlerShould)}", nameof(CreateWalAndUpdateProjectFile))))
Expand Down