Exemple #1
0
        private static void Main(string[] args)
        {
            try
            {
                var exeDir      = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var exeBaseName = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location);
                var ini         = new Model.AppIniFile(Path.Combine(exeDir, "VBASync.ini"));
                ini.AddFile(Path.Combine(exeDir, exeBaseName + ".ini"));
                var appDataDir      = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                var lastSessionPath = ini.GetBool("General", "Portable") ?? false
                    ? Path.Combine(exeDir, "LastSession.ini")
                    : Path.Combine(appDataDir, "VBA Sync Tool", "LastSession.ini");

                ini.AddFile(lastSessionPath);

                // don't persist these settings
                ini.Delete("General", "ActionType");
                ini.Delete("General", "FolderPath");
                ini.Delete("General", "FilePath");
                ini.Delete("General", "AutoRun");
                ini.Delete("General", "AddNewDocumentsToFile");
                ini.Delete("General", "IgnoreEmpty");
                ini.Delete("Hooks", "BeforePublish");
                ini.Delete("Hooks", "AfterExtract");

                if (exeDir != Environment.CurrentDirectory)
                {
                    ini.AddFile(Path.Combine(Environment.CurrentDirectory, "VBASync.ini"));
                    ini.AddFile(Path.Combine(Environment.CurrentDirectory, exeBaseName + ".ini"));
                }
                var autoRunSwitch                  = false;
                Model.ActionType?actionSwitch      = null;
                string           filePathSwitch    = null;
                string           folderPathSwitch  = null;
                var    addNewDocumentsToFileSwitch = false;
                var    ignoreEmptySwitch           = false;
                string beforePublishHookSwitch     = null;
                string afterExtractHookSwitch      = null;
                for (var i = 1; i < args.Length; ++i)
                {
                    switch (args[i].ToUpperInvariant())
                    {
                    case "-R":
                    case "/R":
                        autoRunSwitch = true;
                        break;

                    case "-X":
                    case "/X":
                        actionSwitch = Model.ActionType.Extract;
                        break;

                    case "-P":
                    case "/P":
                        actionSwitch = Model.ActionType.Publish;
                        break;

                    case "-F":
                    case "/F":
                        filePathSwitch = args[++i];
                        break;

                    case "-D":
                    case "/D":
                        folderPathSwitch = args[++i];
                        break;

                    case "-A":
                    case "/A":
                        addNewDocumentsToFileSwitch = true;
                        break;

                    case "-I":
                    case "/I":
                        ignoreEmptySwitch = true;
                        break;

                    case "-H":
                    case "/H":
                        if (actionSwitch == Model.ActionType.Publish)
                        {
                            beforePublishHookSwitch = args[++i];
                        }
                        else
                        {
                            afterExtractHookSwitch = args[++i];
                        }
                        break;

                    default:
                        ini.AddFile(args[i]);
                        break;
                    }
                }

                var startup = new Model.Startup
                {
                    Action = actionSwitch ?? ini.GetActionType("General", "ActionType") ?? Model.ActionType.Extract,
                    AddNewDocumentsToFile = addNewDocumentsToFileSwitch || (ini.GetBool("General", "AddNewDocumentsToFile") ?? false),
                    AfterExtractHook      = new Model.Hook(afterExtractHookSwitch ?? ini.GetString("Hooks", "AfterExtract")),
                    AutoRun            = autoRunSwitch || (ini.GetBool("General", "AutoRun") ?? false),
                    BeforePublishHook  = new Model.Hook(beforePublishHookSwitch ?? ini.GetString("Hooks", "BeforePublish")),
                    DiffTool           = ini.GetString("DiffTool", "Path"),
                    DiffToolParameters = ini.GetString("DiffTool", "Parameters") ?? "\"{OldFile}\" \"{NewFile}\"",
                    IgnoreEmpty        = ignoreEmptySwitch || (ini.GetBool("General", "IgnoreEmpty") ?? false),
                    FilePath           = filePathSwitch ?? ini.GetString("General", "FilePath"),
                    FolderPath         = folderPathSwitch ?? ini.GetString("General", "FolderPath"),
                    Language           = ini.GetString("General", "Language"),
                    LastSessionPath    = lastSessionPath,
                    Portable           = ini.GetBool("General", "Portable") ?? false
                };
                var j = 1;
                while (j <= 5 && !string.IsNullOrEmpty(ini.GetString("RecentFiles", j.ToString(CultureInfo.InvariantCulture))))
                {
                    startup.RecentFiles.Add(ini.GetString("RecentFiles", j.ToString(CultureInfo.InvariantCulture)));
                    ++j;
                }
                if (!string.IsNullOrEmpty(startup.Language))
                {
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(startup.Language);
                }
                if (startup.AutoRun)
                {
                    using (var actor = new Model.ActiveSession(startup, startup))
                    {
                        actor.Apply(actor.GetPatches().ToList());
                    }
                }
                else
                {
                    try
                    {
                        Assembly.Load("VBASync.WPF")
                        .GetType("VBASync.WPF.WpfManager")
                        .GetMethod("RunWpf", BindingFlags.Public | BindingFlags.Static)
                        .Invoke(null, new object[] { startup, !File.Exists(lastSessionPath) });
                    }
                    catch
                    {
                        throw new ApplicationException(VBASyncResources.ErrorCannotLoadGUI);
                    }
                }
            }
            catch (Exception ex)
            {
                Forms.MessageBox.Show(ex.Message, VBASyncResources.MWTitle,
                                      Forms.MessageBoxButtons.OK, Forms.MessageBoxIcon.Error);
            }
        }
Exemple #2
0
        public void ProcessIni(AppIniFile ini, bool allowSessionSettings)
        {
            var iniAction = ini.GetActionType("General", "ActionType");
            var iniAddNewDocumentsToFile = ini.GetBool("General", "AddNewDocumentsToFile");
            var iniAfterExtractHook      = ini.GetString("Hooks", "AfterExtract");
            var iniAutoRun                 = ini.GetBool("General", "AutoRun");
            var iniBeforePublishHook       = ini.GetString("Hooks", "BeforePublish");
            var iniDeleteDocumentsFromFile = ini.GetBool("General", "DeleteDocumentsFromFile");
            var iniDiffTool                = ini.GetString("DiffTool", "Path");
            var iniDiffToolParameters      = ini.GetString("DiffTool", "Parameters");
            var iniIgnoreEmpty             = ini.GetBool("General", "IgnoreEmpty");
            var iniFilePath                = ini.GetString("General", "FilePath");
            var iniFolderPath              = ini.GetString("General", "FolderPath");
            var iniLanguage                = ini.GetString("General", "Language");
            var iniPortable                = ini.GetBool("General", "Portable");
            var iniSearchSubdirectories    = ini.GetBool("General", "SearchRepositorySubdirectories");

            if (iniAction.HasValue && allowSessionSettings)
            {
                Action = iniAction.Value;
            }

            if (iniAddNewDocumentsToFile.HasValue && allowSessionSettings)
            {
                AddNewDocumentsToFile = iniAddNewDocumentsToFile.Value;
            }

            if (iniAfterExtractHook != null && allowSessionSettings)
            {
                AfterExtractHook = _hookFactory(iniAfterExtractHook);
            }

            if (iniAutoRun.HasValue && allowSessionSettings)
            {
                AutoRun = iniAutoRun.Value;
            }

            if (iniBeforePublishHook != null && allowSessionSettings)
            {
                BeforePublishHook = _hookFactory(iniBeforePublishHook);
            }

            if (iniDeleteDocumentsFromFile.HasValue && allowSessionSettings)
            {
                DeleteDocumentsFromFile = iniDeleteDocumentsFromFile.Value;
            }

            if (iniDiffTool != null)
            {
                DiffTool = iniDiffTool;
            }

            if (iniDiffToolParameters != null)
            {
                DiffToolParameters = iniDiffToolParameters;
            }

            if (iniIgnoreEmpty.HasValue && allowSessionSettings)
            {
                IgnoreEmpty = iniIgnoreEmpty.Value;
            }

            if (iniFilePath != null && allowSessionSettings)
            {
                FilePath = iniFilePath;
            }

            if (iniFolderPath != null && allowSessionSettings)
            {
                FolderPath = iniFolderPath;
            }

            if (iniLanguage != null)
            {
                Language = iniLanguage;
            }

            if (iniPortable.HasValue)
            {
                Portable = iniPortable.Value;
            }

            if (iniSearchSubdirectories.HasValue)
            {
                SearchRepositorySubdirectories = iniSearchSubdirectories.Value;
            }

            if (ini.GetString("RecentFiles", "1") != null)
            {
                RecentFiles.Clear();
                var j = 1;
                while (j <= 5 && !string.IsNullOrEmpty(ini.GetString("RecentFiles", j.ToString(CultureInfo.InvariantCulture))))
                {
                    RecentFiles.Add(ini.GetString("RecentFiles", j.ToString(CultureInfo.InvariantCulture)));
                    ++j;
                }
            }
        }