private static Session GetEpiSession(CommandLineParams o) { string password = o.Password; Session ses = null; //Create a copy of the config file so that we can set a temporary cache folder for our instance var newConfig = Path.GetTempFileName().Replace(".tmp", ".sysconfig"); File.Copy(o.ConfigFile, newConfig, true); o.NewConfig = newConfig; //Create a temp directory to store our epicor cache DirectoryInfo di = Directory.CreateDirectory(Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString())); o.Temp = di.FullName; var x = XElement.Load(newConfig); //Set our new Temp Cache location in our new config x.Descendants("appSettings").Elements().Where(r => r.Name == "AlternateCacheFolder").FirstOrDefault().FirstAttribute.Value = di.FullName; x.Save(newConfig); try { password = NeedtoEncrypt(o); ses = new Session(o.Username, password, Session.LicenseType.Default, newConfig); } catch (Exception ex) { ShowProgressBar(false); MessageBox.Show("Failed to Authenticate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); LoginForm frm = new LoginForm(o.EpicorClientFolder); if (frm.ShowDialog() == DialogResult.OK) { ShowProgressBar(); o.Username = Settings.Default.Username; o.Password = Settings.Default.Password; o.ConfigFile = Settings.Default.Environment; o.Encrypted = "true"; password = NeedtoEncrypt(o); try { ses = new Session(o.Username, password, Session.LicenseType.Default, newConfig); } catch (Exception) { MessageBox.Show("Failed to Authenticate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); ses = null; } ShowProgressBar(true); } } if (ses != null) { SecRightsHandler.CacheBOSecSettings(ses); dynamic curMRUList = typeof(SecRightsHandler).GetField("_currMRUList", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); Startup.SetupPlugins(ses); if (!string.IsNullOrEmpty(o.DLLLocation)) { String fineName = Path.GetFileName(o.DLLLocation); string newPath = Path.GetDirectoryName((string)curMRUList.GetType().GetProperty("SavePath", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).GetValue(curMRUList)).Replace("BOSecMRUList", "CustomDLLs"); o.DLLLocation = Path.Combine(newPath, fineName); } epi.Ice.Lib.Configuration c = new epi.Ice.Lib.Configuration(newConfig); c.AlternateCacheFolder = di.FullName; Assembly assy = Assembly.LoadFile(Path.Combine(o.EpicorClientFolder, "Epicor.exe")); TypeInfo ty = assy.DefinedTypes.Where(r => r.Name == "ConfigureForAutoDeployment").FirstOrDefault(); dynamic thing = Activator.CreateInstance(ty); object[] args = { c }; thing.GetType().GetMethod("SetUpAssemblyRetrieversAndPossiblyGetNewConfiguration", BindingFlags.Instance | BindingFlags.Public).Invoke(thing, args); WellKnownAssemblyRetrievers.AutoDeployAssemblyRetriever = (IAssemblyRetriever)thing.GetType().GetProperty("AutoDeployAssemblyRetriever", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetValue(thing); WellKnownAssemblyRetrievers.SessionlessAssemblyRetriever = (IAssemblyRetriever)thing.GetType().GetProperty("SessionlessAssemblyRetriever", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetValue(thing); Startup.PreStart(ses, true); launcher = new EpicorLauncher(); } return(ses); }
static void Main(string[] args) { Application.EnableVisualStyles(); Session epiSession = null; bool reSync = false; Parser.Default.ParseArguments <CommandLineParams>(args) .WithParsed(o => { currAction = o.Action; ShowProgressBar(); switch (o.Action) { case "Launch": { epiSession = GetEpiSession(o); if (epiSession != null) { launcher.LaunchInEpicor(o, epiSession, false, true); } } break; case "Add": { ShowProgressBar(false); LoginForm frm = new LoginForm(o.EpicorClientFolder); if (frm.ShowDialog() == DialogResult.OK) { ShowProgressBar(); o.Username = Settings.Default.Username; o.Password = Settings.Default.Password; o.ConfigFile = Settings.Default.Environment; o.Encrypted = "true"; epiSession = GetEpiSession(o); if (epiSession != null) { DownloadCustomization(o, epiSession); reSync = true; } else { reSync = false; } } else { reSync = false; } } break; case "Update": { epiSession = GetEpiSession(o); if (epiSession != null) { reSync = true; if (!UpdateCustomization(o, epiSession)) { if (MessageBox.Show("You've canceled the sync operation, would you like to download the latest copy of the customization from Epicor? This will over-write any changes you have made to the custom script since the last sync.", "Re-Download?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { reSync = false; } } } else { reSync = false; } } break; case "Download": { epiSession = GetEpiSession(o); if (epiSession != null) { reSync = true; } else { reSync = false; } } break; case "Edit": { epiSession = GetEpiSession(o); if (epiSession != null) { launcher.LaunchInEpicor(o, epiSession, true); reSync = true; } else { reSync = false; } } break; case "Debug": { epiSession = GetEpiSession(o); if (epiSession != null) { if (!string.IsNullOrEmpty(o.DNSpy)) { RunDnSpy(o); } launcher.LaunchInEpicor(o, epiSession, false, true); } } break; case "Toolbox": { epiSession = GetEpiSession(o); if (epiSession != null) { ShowProgressBar(false); NonModalWokIt win = new NonModalWokIt(epiSession, o); win.ShowDialog(); reSync = win.Sync; ShowProgressBar(true); } } break; } if (reSync) { if (o.Key2.Contains("MainController")) //Dashboard { launcher.DownloadAndSyncDashboard(epiSession, o); } else { launcher.DownloadAndSync(epiSession, o); } } ShowProgressBar(false); if (epiSession != null) { epiSession.OnSessionClosing(); epiSession = null; } if (!string.IsNullOrEmpty(o.NewConfig)) { try { File.Delete(o.NewConfig); } catch { } } if (!string.IsNullOrEmpty(o.Temp)) { try { Directory.Delete(o.Temp, true); } catch { } } }); }
//Main Program Launched from VS Code static void Main(string[] args) { Application.EnableVisualStyles(); Session epiSession = null; bool reSync = false; Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.File("CustomizationEditor.log", Serilog.Events.LogEventLevel.Verbose, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 10, shared: true, fileSizeLimitBytes: 10000000) .CreateLogger(); Log.Information("===========================================Application Launch=================================="); Parser.Default.ParseArguments <CommandLineParams>(args) .WithParsed(o => { try { currAction = o.Action; ShowProgressBar(); switch (o.Action) { case "Add": { Log.Information("Action was Add"); ShowProgressBar(false); LoginForm frm = new LoginForm(o.EpicorClientFolder); if (frm.ShowDialog() == DialogResult.OK) { ShowProgressBar(); o.Username = Settings.Default.Username; o.Password = Settings.Default.Password; o.ConfigFile = Settings.Default.Environment; o.Encrypted = "true"; epiSession = GetEpiSession(o); if (epiSession != null) { Log.Information("Got a valid Epicor Session"); if (DownloadCustomization(o, epiSession)) { reSync = true; } } else { reSync = false; } } else { reSync = false; } } break; case "Update": { Log.Information("Action was Update"); epiSession = GetEpiSession(o); if (epiSession != null) { Log.Information("Got a valid Epicor Session"); reSync = true; if (!UpdateCustomization(o, epiSession)) { if (MessageBox.Show("You've canceled the sync operation, would you like to download the latest copy of the customization from Epicor? This will over-write any changes you have made to the custom script since the last sync.", "Re-Download?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { reSync = false; } } } else { reSync = false; } } break; case "Download": { Log.Information("Action was Update"); epiSession = GetEpiSession(o); if (epiSession != null) { Log.Information("Got a valid Epicor Session"); reSync = true; } else { reSync = false; } } break; case "Toolbox": { Log.Information("Action was ToolBox"); epiSession = GetEpiSession(o); if (epiSession != null) { Log.Information("Got a valid Epicor Session"); ShowProgressBar(false); NonModalWokIt win = new NonModalWokIt(epiSession, o); win.ShowDialog(); reSync = win.Sync; ShowProgressBar(true); } } break; } if (reSync) { Log.Information("Sync down is True"); if (o.Key2.Contains("MainController")) { Log.Information("Download and Sync a Deployed Dashboard"); launcher.DownloadAndSyncDashboard(epiSession, o); } else { Log.Information("Download and Sync a regular customization"); launcher.DownloadAndSync(epiSession, o); } } ShowProgressBar(false); if (epiSession != null) { epiSession.OnSessionClosing(); epiSession = null; } if (!string.IsNullOrEmpty(o.NewConfig)) { try { File.Delete(o.NewConfig); } catch { } } if (!string.IsNullOrEmpty(o.Temp)) { try { Directory.Delete(o.Temp, true); } catch { } } } catch (Exception ex) { Log.Error(ex, "Main Application Error"); } finally { Log.Information("========================Application Close====================="); Log.CloseAndFlush(); } }); }