private void Resync(CommandLineParams o, StreamWriter swLog, StringBuilder refds, GenXDataDataSet ds, CustomizationDS nds, CustomScriptManager csmR)
        {
            int    start = csmR.CustomCodeAll.IndexOf("// ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! **");
            int    end   = csmR.CustomCodeAll.Length - start;
            string allCode;
            string script;

            allCode = csmR.CustomCodeAll.Replace(csmR.CustomCodeAll.Substring(start, end), "}").Replace("public class Script", "public partial class Script").Replace("public static class Script", "public static partial class Script");
            script  = csmR.Script.Replace("public class Script", "public partial class Script");
            script  = script.Replace("public static class Script", "public static partial class Script");
            swLog.WriteLine("Write Project");
            string projectFile = Resource.BasicProjc;

            projectFile = projectFile.Replace("<CUSTID>", o.Key1);
            projectFile = projectFile.Replace("<!--<ReferencesHere>-->", refds.ToString());
            o.Version   = ds.XXXDef[0].SysRevID;

            swLog.WriteLine("Create Folder");
            if (string.IsNullOrEmpty(o.ProjectFolder))
            {
                string origFolderName = ($@"{o.Folder}\{o.Key2}_{ o.Key1}").Replace('.', '_');
                string newFolderName  = origFolderName;
                int    ct             = 0;
                while (Directory.Exists(newFolderName))
                {
                    newFolderName = ($"{origFolderName}_{++ct}").Replace('.', '_');
                }
                o.ProjectFolder = newFolderName;
                Directory.CreateDirectory(o.ProjectFolder);
            }


            using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\{o.Key1}.csproj"))
            {
                sw.Write(projectFile);
                sw.Close();
            }
            swLog.WriteLine("Write Script");
            using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\Script.cs"))
            {
                sw.Write(script);
                sw.Close();
            }

            swLog.WriteLine("Write ScriptRO");
            using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\ScriptReadOnly.cs"))
            {
                sw.Write(allCode);
                sw.Close();
            }

            swLog.WriteLine("Write Command");
            string command = Newtonsoft.Json.JsonConvert.SerializeObject(o);

            using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\CustomizationInfo.json"))
            {
                sw.Write(command);
                sw.Close();
            }

            File.SetAttributes($@"{o.ProjectFolder}\ScriptReadOnly.cs", File.GetAttributes($@"{o.ProjectFolder}\ScriptReadOnly.cs") & ~FileAttributes.ReadOnly);
            swLog.WriteLine("Write Customization");
            nds.WriteXml($@"{o.ProjectFolder}\{o.Key2}_Customization_{o.Key1}_CustomExport.xml", XmlWriteMode.WriteSchema);
        }
        private static void DownloadAndSync(Session epiSession, CommandLineParams o)
        {
            string file = Path.GetTempFileName();

            using (StreamWriter swLog = new StreamWriter(file))
            {
                swLog.WriteLine("Got in the Function");
                try
                {
                    epiSession["Customizing"] = false;
                    var oTrans             = new ILauncher(epiSession);
                    CustomizationVerify cm = new CustomizationVerify(epiSession);
                    swLog.WriteLine("Customization Verify");
                    string dll = cm.getDllName(o.Key2);
                    swLog.WriteLine("Got Epicor DLL");
                    StringBuilder refds          = new StringBuilder();
                    dynamic       epiBaseForm    = null;
                    dynamic       epiTransaction = null;
                    if (string.IsNullOrEmpty(dll))
                    {
                        dll = "*.UI.*.dll";
                    }


                    Assembly assy = ClientAssemblyRetriever.ForILaunch(oTrans).RetrieveAssembly(dll);
                    swLog.WriteLine("Finding File");
                    string s = "";
                    if (assy == null)
                    {
                        foreach (string x in Directory.GetFiles(o.EpicorClientFolder, dll))
                        {
                            assy = Assembly.LoadFile(x);
                            if (assy.DefinedTypes.Where(r => r.FullName.ToUpper().Contains(o.Key2.ToUpper())).Any())
                            {
                                s = x;
                                break;
                            }
                        }
                    }
                    s = assy.Location;
                    var typeE = assy.DefinedTypes.Where(r => r.FullName.ToUpper().Contains(o.Key2.ToUpper())).FirstOrDefault();

                    var typeTList = assy.DefinedTypes.Where(r => r.BaseType.Name.Equals("EpiTransaction") || r.BaseType.Name.Equals("EpiMultiViewTransaction") || r.BaseType.Name.Equals("EpiSingleViewTransaction") || r.BaseType.Name.Equals("UDMultiViewTransaction") || r.BaseType.Name.Equals("UDSingleViewTransaction")).ToList();
                    if (typeTList != null && typeTList.Count > 0)
                    {
                        foreach (var typeT in typeTList)
                        {
                            try
                            {
                                if (typeT != null)
                                {
                                    epiTransaction = Activator.CreateInstance(typeT, new object[] { oTrans });
                                }
                                else
                                {
                                    epiTransaction = new EpiTransaction(oTrans);
                                }

                                epiBaseForm = Activator.CreateInstance(typeE, new object[] { epiTransaction });
                                break;
                            }
                            catch (Exception e)
                            { }
                        }
                    }
                    else
                    {
                        epiTransaction = new EpiTransaction(oTrans);
                        epiBaseForm    = Activator.CreateInstance(typeE, new object[] { epiTransaction });
                    }

                    bool dashboard = false;
                    try
                    {
                        epiBaseForm.IsVerificationMode = true;
                        epiBaseForm.CustomizationName  = o.Key1;
                    }
                    catch (Exception) {
                        //Dashboard
                        dashboard = true;
                    }

                    refds.AppendLine($"<Reference Include=\"{typeE.Assembly.FullName}\">");
                    refds.AppendLine($"<HintPath>{s}</HintPath>");
                    refds.AppendLine($"</Reference>");
                    if (dashboard)
                    {
                        //epiBaseForm.GetType().GetMethod("InitializeLaunch", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(epiBaseForm);
                        var     typ      = assy.DefinedTypes.Where(r => r.Name == "Launch").FirstOrDefault();
                        dynamic launcher = Activator.CreateInstance(typ);
                        launcher.Session = epiSession;
                        launcher.GetType().GetMethod("InitializeLaunch", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(launcher, null);
                    }

                    swLog.WriteLine("Initialize EpiUI Utils");
                    EpiUIUtils eu = new EpiUIUtils(epiBaseForm, epiTransaction, epiBaseForm.MainToolManager, null);
                    eu.GetType().GetField("currentSession", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(eu, epiTransaction.Session);
                    eu.GetType().GetField("customizeName", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(eu, o.Key1);
                    eu.GetType().GetField("baseExtentionName", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(eu, o.Key3.Replace("BaseExtension^", string.Empty));

                    swLog.WriteLine("Get composite Customize Data Set");
                    var  mi        = eu.GetType().GetMethod("getCompositeCustomizeDataSet", BindingFlags.Instance | BindingFlags.NonPublic);
                    bool customize = false;
                    mi.Invoke(eu, new object[] { o.Key2, customize, customize, customize });
                    Ice.Adapters.GenXDataAdapter ad = new Ice.Adapters.GenXDataAdapter(epiTransaction);
                    ad.BOConnect();
                    GenXDataImpl i = (GenXDataImpl)ad.BusinessObject;
                    swLog.WriteLine("Customization Get By ID");
                    var                         ds     = i.GetByID(o.Company, o.ProductType, o.LayerType, o.CSGCode, o.Key1, o.Key2, o.Key3);
                    string                      beName = o.Key3.Replace("BaseExtension^", string.Empty);
                    string                      exName = (string)eu.GetType().GetField("extensionName", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(eu);
                    CustomizationDS             nds    = new CustomizationDS();
                    PersonalizeCustomizeManager csm    = new PersonalizeCustomizeManager(epiBaseForm, epiTransaction, o.ProductType, o.Company, beName, exName, o.Key1, eu.CustLayerMan, DeveloperLicenseType.Partner, LayerType.Customization);
                    swLog.WriteLine("Init Custom Controls");
                    csm.InitCustomControlsAndProperties(ds, LayerName.CompositeBase, true);
                    CustomScriptManager csmR = csm.CurrentCustomScriptManager;
                    swLog.WriteLine("Generate Refs");

                    /*List<string> aliases = new List<string>();
                     * Match match =Regex.Match(csmR.CustomCodeAll, "((?<=extern alias )(.*)*(?=;))");
                     * while(match.Success)
                     * {
                     *  aliases.Add(match.Value.Replace("_",".").ToUpper());
                     *  match =match.NextMatch();
                     * }*/

                    GenerateRefs(refds, csmR, o, null);
                    ExportCustmization(nds, ad, o);
                    int    start = csmR.CustomCodeAll.IndexOf("// ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! **");
                    int    end   = csmR.CustomCodeAll.Length - start;
                    string allCode;
                    string script;
                    allCode = csmR.CustomCodeAll.Replace(csmR.CustomCodeAll.Substring(start, end), "}").Replace("public class Script", "public partial class Script").Replace("public static class Script", "public static partial class Script");
                    script  = csmR.Script.Replace("public class Script", "public partial class Script");
                    script  = script.Replace("public static class Script", "public static partial class Script");
                    swLog.WriteLine("Write Project");
                    string projectFile = Resource.BasicProjc;
                    projectFile = projectFile.Replace("<CUSTID>", o.Key1);
                    projectFile = projectFile.Replace("<!--<ReferencesHere>-->", refds.ToString());
                    o.Version   = ds.XXXDef[0].SysRevID;

                    swLog.WriteLine("Create Folder");
                    if (string.IsNullOrEmpty(o.ProjectFolder))
                    {
                        string origFolderName = ($@"{o.Folder}\{o.Key2}_{ o.Key1}").Replace('.', '_');
                        string newFolderName  = origFolderName;
                        int    ct             = 0;
                        while (Directory.Exists(newFolderName))
                        {
                            newFolderName = ($"{origFolderName}_{++ct}").Replace('.', '_');
                        }
                        o.ProjectFolder = newFolderName;
                        Directory.CreateDirectory(o.ProjectFolder);
                    }


                    using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\{o.Key1}.csproj"))
                    {
                        sw.Write(projectFile);
                        sw.Close();
                    }
                    swLog.WriteLine("Write Script");
                    using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\Script.cs"))
                    {
                        sw.Write(script);
                        sw.Close();
                    }

                    swLog.WriteLine("Write ScriptRO");
                    using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\ScriptReadOnly.cs"))
                    {
                        sw.Write(allCode);
                        sw.Close();
                    }

                    swLog.WriteLine("Write Command");
                    string command = Newtonsoft.Json.JsonConvert.SerializeObject(o);
                    using (StreamWriter sw = new StreamWriter($@"{o.ProjectFolder}\CustomizationInfo.json"))
                    {
                        sw.Write(command);
                        sw.Close();
                    }

                    File.SetAttributes($@"{o.ProjectFolder}\ScriptReadOnly.cs", File.GetAttributes($@"{o.ProjectFolder}\ScriptReadOnly.cs") & ~FileAttributes.ReadOnly);
                    swLog.WriteLine("Write Customization");
                    nds.WriteXml($@"{o.ProjectFolder}\{o.Key2}_Customization_{o.Key1}_CustomExport.xml", XmlWriteMode.WriteSchema);



                    epiBaseForm.Dispose();


                    ad.Dispose();
                    cm = null;

                    eu.Dispose();
                }
                catch (Exception ee)
                {
                    swLog.WriteLine(ee.ToString());
                }
            }

            Console.WriteLine(o.ProjectFolder);
            //MessageBox.Show(file);
        }