Skip to content

Commit

Permalink
Add feedback per device when deploying. Closes #22, closes #23, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
versx committed May 7, 2021
1 parent 1925f15 commit dbcf21c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 53 deletions.
73 changes: 37 additions & 36 deletions src/Commands/AppDeployment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public AppDeployment(Dependencies dep)

[
Command("resign"),
Description("Download IPA, resign, and deploy to device(s)."),
Description("Download IPA, resign, and deploy to device(s). If no devices are provided, it will only resign and not deploy."),
]
public async Task ResignPoGoAsync(CommandContext ctx,
[Description("Mega download link")] string megaLink,
Expand All @@ -52,19 +52,29 @@ public async Task ResignPoGoAsync(CommandContext ctx,
{
ResignApp = true,
};
deployer.DeployCompleted += async (object sender, DeployEventArgs e) =>
{
var response = e.Success
? $"Successfully deployed app to: {e.Device.Name}"
: $"Failed to deploy app to: {e.Device.Name}";
await ctx.RespondAsync(response);
};
await ctx.RespondAsync("Starting resign...");
if (!deployer.Resign(megaLink, version))
{
await ctx.RespondAsync($"Failed to resign IPA");
return;
}
await ctx.RespondAsync($"Resign complete, starting deployment to {phoneNames}...");

var result = deployer.Deploy(deployer.SignedReleaseFileName, phoneNames);
var successful = result.Item1.Count > 0 ? $"Successfully deployed app to:\n{string.Join(", ", result.Item1)}" : null;
var failed = result.Item2.Count > 0 ? $"Failed to deploy app to:\n{string.Join(", ", result.Item2)}" : null;
// TODO: Check for content length over 2048 and split messages if so
await ctx.RespondAsync($"{successful}\n{failed}");
var response = $"Resign complete, saved to {deployer.SignedReleaseFileName}.";
if (!string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"{response} Starting deployment to {phoneNames}...");
deployer.Deploy(deployer.SignedReleaseFileName, phoneNames);
return;
}

await ctx.RespondAsync(response);
}

[
Expand All @@ -87,6 +97,12 @@ public async Task DeployPoGoAsync(CommandContext ctx,
if (!ctx.Channel.Id.IsValidChannel(_dep.Config.Servers.Values.ToList()))
return;

if (string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"No devices provided, command not executed.");
return;
}

var devices = Device.GetAll();
var deployAppDevices = new List<string>(phoneNames.RemoveSpaces());
if (string.Compare(phoneNames, Strings.All, true) == 0)
Expand All @@ -99,36 +115,15 @@ public async Task DeployPoGoAsync(CommandContext ctx,
}

var deployer = new AppDeployer(_dep.Config.Developer, _dep.Config.ProvisioningProfile);
_logger.Debug($"Using app {appPath} for deployment.");
//deployer.Deploy(appPath);
Parallel.ForEach(deployAppDevices, async deviceName =>
deployer.DeployCompleted += async (object sender, DeployEventArgs e) =>
{
if (!devices.ContainsKey(deviceName))
{
_logger.Warn($"{deviceName} does not exist in device list, skipping deploy pogo.");
}
else
{
var device = devices[deviceName];
var args = $"--id {device.Uuid} --bundle {appPath}";
_logger.Info($"Deploying to device {device.Name} ({device.Uuid})...");
var output = Shell.Execute("ios-deploy", args, out var exitCode, true);
_logger.Debug($"{device.Name} ({device.Uuid}) Deployment output: {output}");
var success = output.ToLower().Contains($"[100%] installed package {appPath}");
if (success)
{
await ctx.RespondAsync($"Deployed {appPath} to {device.Name} ({device.Uuid}) successfully.");
}
else
{
if (output.Length > 2000)
{
output = string.Join("", output.TakeLast(1900));
}
await ctx.RespondAsync($"Failed to deploy {appPath} to {device.Name} ({device.Uuid})\nOutput: {output}");
}
}
});
var message = e.Success
? $"Successfully deployed app to: {e.Device.Name}"
: $"Failed to deploy app to: {e.Device.Name}";
await ctx.RespondAsync(message);
};
_logger.Debug($"Using app {appPath} for deployment.");
deployer.Deploy(appPath, phoneNames);
}

[
Expand All @@ -151,6 +146,12 @@ public async Task RemovePoGoAsync(CommandContext ctx,
if (!ctx.Channel.Id.IsValidChannel(_dep.Config.Servers.Values.ToList()))
return;

if (string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"No devices provided, command not executed.");
return;
}

var devices = Device.GetAll();
var removeAppDevices = phoneNames.RemoveSpaces();
if (string.Compare(phoneNames, Strings.All, true) == 0)
Expand Down
34 changes: 32 additions & 2 deletions src/Commands/PhoneControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ public async Task ScreenshotAsync(CommandContext ctx,
return;
}

if (string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"No devices provided, command not executed.");
return;
}

var devices = Device.GetAll();
var screenDevices = phoneNames.RemoveSpaces();
if (string.Compare(phoneNames, Strings.All, true) == 0)
Expand Down Expand Up @@ -232,6 +238,12 @@ public async Task SingleAppModeAsync(CommandContext ctx,
if (!ctx.Channel.Id.IsValidChannel(_dep.Config.Servers.Values.ToList()))
return;

if (string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"No devices provided, command not executed.");
return;
}

var devices = Device.GetAll();
var samDevices = phoneNames.RemoveSpaces();
if (string.Compare(phoneNames, Strings.All, true) == 0)
Expand Down Expand Up @@ -283,6 +295,12 @@ public async Task ReopenAsync(CommandContext ctx,
if (!ctx.Channel.Id.IsValidChannel(_dep.Config.Servers.Values.ToList()))
return;

if (string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"No devices provided, command not executed.");
return;
}

var devices = Device.GetAll();
var reopenDevices = phoneNames.RemoveSpaces();
if (string.Compare(phoneNames, Strings.All, true) == 0)
Expand Down Expand Up @@ -340,6 +358,12 @@ public async Task RebootAsync(CommandContext ctx,
return;
}

if (string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"No devices provided, command not executed.");
return;
}

var devices = Device.GetAll();
var rebootDevices = phoneNames.RemoveSpaces();
if (string.Compare(phoneNames, Strings.All, true) == 0)
Expand Down Expand Up @@ -379,6 +403,12 @@ public async Task ShutdownAsync(CommandContext ctx,
if (!ctx.Channel.Id.IsValidChannel(_dep.Config.Servers.Values.ToList()))
return;

if (string.IsNullOrEmpty(phoneNames))
{
await ctx.RespondAsync($"No devices provided, command not executed.");
return;
}

//TODO: Check if idevicediagnostics is installed.
var devices = Device.GetAll();
var shutdownDevices = phoneNames.RemoveSpaces();
Expand Down Expand Up @@ -434,14 +464,14 @@ public async Task KillAsync(CommandContext ctx,

#region Private Methods

private List<string> SplitDevicePages()
private static List<string> SplitDevicePages()
{
var devices = Device.GetAll();
var keys = devices.Keys.ToList();
var pages = new List<string>();
var maxDevicePerPage = 20;
var sb = new System.Text.StringBuilder();
for (int i = 0; i < keys.Count; i++)
for (var i = 0; i < keys.Count; i++)
{
if (i % maxDevicePerPage == 0 && i != 0)
{
Expand Down
42 changes: 27 additions & 15 deletions src/Deployment/AppDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public class AppDeployer

#endregion

#region Events

internal event EventHandler<DeployEventArgs> DeployCompleted;
private void OnDeployCompleted(Device device, bool success)
{
DeployCompleted?.Invoke(this, new DeployEventArgs(device, success));
}

#endregion

#region Constructor

public AppDeployer(string developer, string provisioningProfile)
Expand Down Expand Up @@ -93,7 +103,7 @@ public bool Resign(string megaLink, string version)
return result;
}

public (List<string>, List<string>) Deploy(string appPath, string deviceNames = Strings.All)
public void Deploy(string appPath, string deviceNames = Strings.All)
{
var successful = new List<string>();
var failed = new List<string>();
Expand All @@ -104,7 +114,6 @@ public bool Resign(string megaLink, string version)
deployAppDevices = devices.Keys.ToList();
}
_logger.Info($"Deploying app {appPath} to {string.Join(", ", deployAppDevices)}");
// TODO: Provide better feedback of successful and failed deployments
Parallel.ForEach(deployAppDevices, deviceName =>
{
if (!devices.ContainsKey(deviceName))
Expand All @@ -117,21 +126,11 @@ public bool Resign(string megaLink, string version)
var args = $"--id {device.Uuid} --bundle {appPath}";
_logger.Info($"Deploying to device {device.Name} ({device.Uuid})...");
var output = Shell.Execute("ios-deploy", args, out var exitCode, true);
var success = output.ToLower().Contains($"[100%] installed package {appPath}");
if (success)
{
successful.Add(device.Name);
_logger.Info($"Deployed {appPath} to {device.Name} ({device.Uuid}) successfully.");
}
else
{
failed.Add(device.Name);
_logger.Warn($"Failed to deploy {appPath} to {device.Name} ({device.Uuid})\nOutput: {output}");
}
// TODO: OnDeployCompleted event
var success = output.ToLower().Contains($"[100%] installed package {appPath}") ||
output.ToLower().Contains("100%");
OnDeployCompleted(device, success);
}
});
return (successful, failed);
}

public static string GetLatestAppPath()
Expand Down Expand Up @@ -321,4 +320,17 @@ private void CreateTempDirectory(string tempDir)

#endregion
}

internal class DeployEventArgs : EventArgs
{
public Device Device { get; set; }

public bool Success { get; set; }

internal DeployEventArgs(Device device, bool success)
{
Device = device;
Success = success;
}
}
}

0 comments on commit dbcf21c

Please sign in to comment.