Skip to content

Commit

Permalink
Added feature to run scripts on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
MscrmTools committed Oct 31, 2023
1 parent d98b4bd commit 1dfd932
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 24 deletions.
139 changes: 115 additions & 24 deletions XrmToolBox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

using System.Xml;
using XrmToolBox.AppCode;
using XrmToolBox.Extensibility;
using XrmToolBox.Extensibility.Forms;
using XrmToolBox.New;
using XrmToolBox.PluginsStore;

using XrmToolBox.ToolLibrary.AppCode;
using PluginUpdates = XrmToolBox.AppCode.PluginUpdates;

namespace XrmToolBox
Expand Down Expand Up @@ -152,23 +152,6 @@ private static void CopyUpdatedPlugins()
while (tries < 3);
}

private static void TryWaitingForOldProcess(int previousProcessId)
{
try
{
using (var currentProcess = Process.GetCurrentProcess())
using (var oldProcess = Process.GetProcessById(previousProcessId))
{
// process IDs can be reused, so check the name
if (oldProcess.ProcessName == currentProcess.ProcessName)
{
oldProcess.WaitForExit();
}
}
}
catch { }
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
Expand Down Expand Up @@ -318,15 +301,13 @@ private static void Main(string[] args)
InitializePluginsFolder();
CopyUpdatedPlugins();
RemovePlugins();
RunCommandIfAny();

RedirectAssembly("NuGet.Common");
RedirectAssembly("NuGet.Packaging");
RedirectAssembly("NuGet.Protocol");
RedirectAssembly("Newtonsoft.Json");
RedirectAssembly("McTools.Xrm.Connection");
RedirectAssembly("McTools.Xrm.Connection.WinForms");
RedirectAssembly("XrmToolBox.Extensibility");
RedirectAssembly("XrmToolBox.PluginsStore");
RedirectAssembly("XrmToolBox.ToolLibrary");
RedirectAssembly("Microsoft.Xrm.Sdk");
RedirectAssembly("Microsoft.Xrm.Sdk.Workflow");
RedirectAssembly("Microsoft.Crm.Sdk.Proxy");
Expand All @@ -341,6 +322,7 @@ private static void Main(string[] args)
RedirectAssembly("Microsoft.Web.WebView2.WinForms");

OptimizeConnectionSettings();
SetProxy();

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Expand Down Expand Up @@ -418,5 +400,114 @@ private static void RemovePlugins()
}
}
}

private static void RunCommandIfAny()
{
try
{
var xtbExecutionPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

foreach (var file in Directory.GetFiles(xtbExecutionPath, "*.command.xml"))
{
var doc = new XmlDocument();
doc.Load(file);

var commandsNodes = doc.SelectNodes("//Command");
if (commandsNodes?.Count > 0)
{
foreach (XmlNode commandNode in commandsNodes)
{
if (bool.Parse(commandNode.Attributes["Processed"].Value)) continue;

var continueAfterPreCheck = false;
foreach (XmlElement preCheck in commandNode.SelectNodes("//PreCheck"))
{
switch (preCheck.Attributes["type"].Value)
{
case "FileExists":
continueAfterPreCheck = continueAfterPreCheck || File.Exists(preCheck.InnerText);
break;
}
}

if (!continueAfterPreCheck)
{
continue;
}

var description = commandNode.SelectSingleNode("Description").InnerText;
var dr = MessageBox.Show("The following command need to be run before XrmToolBox:\n\n" + description, "XrmToolBox pre run command", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dr == DialogResult.OK)
{
try
{
var actions = commandNode.SelectSingleNode("Actions");
foreach (XmlNode actionNode in actions.SelectNodes("Action"))
{
if (!string.IsNullOrEmpty(actionNode.InnerText))
{
var proc = new Process();
proc.StartInfo.WorkingDirectory = xtbExecutionPath;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c " + actionNode.InnerText.Replace("{{XTBPATH}}", Paths.XrmToolBoxPath);
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.Close();
}
}
}
catch (Exception error)
{
}
finally
{
commandNode.Attributes["Processed"].Value = "true";

if (commandNode.Attributes["ProcessDate"] != null)
{
commandNode.Attributes["ProcessDate"].Value = DateTime.Now.ToString();
}
else
{
var attr = commandNode.OwnerDocument.CreateAttribute("ProcessDate");
attr.Value = DateTime.Now.ToString();
commandNode.Attributes.Append(attr);
}
}
}
}
}

doc.Save(file);
}
}
catch (Exception error)
{
// Do nothing if something went wrong here
}
}

private static void SetProxy()
{
WebProxyHelper.ApplyProxy();
}

private static void TryWaitingForOldProcess(int previousProcessId)
{
try
{
using (var currentProcess = Process.GetCurrentProcess())
using (var oldProcess = Process.GetProcessById(previousProcessId))
{
// process IDs can be reused, so check the name
if (oldProcess.ProcessName == currentProcess.ProcessName)
{
oldProcess.WaitForExit();
}
}
}
catch { }
}
}
}
19 changes: 19 additions & 0 deletions XrmToolBox/Startup/NugetCleanup.Command.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Commands>
<Command Processed="false">
<PreChecks>
<PreCheck type="FileExists">Nuget.Common.dll</PreCheck>
<PreCheck type="FileExists">XrmToolBox.PluginsStore.dll</PreCheck>
</PreChecks>
<Description>This version of XrmToolBox removes the legacy tool library. Nuget assemblies are not needed anymore and will be deleted.</Description>
<Actions>
<Action>IF EXIST "Nuget.Common.dll" (DEL "Nuget.Common.dll")</Action>
<Action>IF EXIST "Nuget.Configuration.dll" (DEL "Nuget.Configuration.dll")</Action>
<Action>IF EXIST "Nuget.Core.dll" (DEL "Nuget.Core.dll")</Action>
<Action>IF EXIST "Nuget.Frameworks.dll" (DEL "Nuget.Frameworks.dll")</Action>
<Action>IF EXIST "Nuget.Packaging.dll" (DEL "Nuget.Packaging.dll")</Action>
<Action>IF EXIST "Nuget.Protocol.dll" (DEL "Nuget.Protocol.dll")</Action>
<Action>IF EXIST "Nuget.Versioning.dll" (DEL "Nuget.Versioning.dll")</Action>
<Action>IF EXIST "XrmToolBox.PluginsStore.dll" (DEL "Nuget.Versioning.dll")</Action>
</Actions>
</Command>
</Commands>

0 comments on commit 1dfd932

Please sign in to comment.