From fff2970eeb53716918343dc37edd9485722fa847 Mon Sep 17 00:00:00 2001 From: Chelsea Hughes Date: Mon, 1 May 2017 19:33:28 -0700 Subject: [PATCH] Add session options (for #24 and #25) --- README.md | 2 + src/VBASync.WPF/AboutWindow.xaml.cs | 5 +- src/VBASync.WPF/MainViewModel.cs | 8 ++- src/VBASync.WPF/MainWindow.xaml.cs | 4 +- src/VBASync.WPF/SettingsViewModel.cs | 8 +++ src/VBASync.WPF/SettingsWindow.xaml | 9 ++- .../Localization/VBASyncResources.Designer.cs | 9 +++ .../Localization/VBASyncResources.resx | 57 ++++++++++--------- src/VBASync/Model/Lib.cs | 6 ++ src/VBASync/Model/ModuleProcessing.cs | 6 +- src/VBASync/Model/SessionInterfaces.cs | 1 + src/VBASync/Model/Startup.cs | 1 + src/VBASync/Program.cs | 6 ++ 13 files changed, 85 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index dc134c7..b48af65 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,8 @@ Switch | Meaning `-f ` | Specify Office file `-d ` | Specify version-control directory `-r` | Do the selected action, then immediately exit (**required** on Linux/Mac) +`-a` | Add new document modules when publishing (expert option) +`-i` | Ignore empty modules Any other parameter passed to VBA Sync Tool will be read and parsed as a session `.ini` file. diff --git a/src/VBASync.WPF/AboutWindow.xaml.cs b/src/VBASync.WPF/AboutWindow.xaml.cs index f39a41a..3bdf6f4 100644 --- a/src/VBASync.WPF/AboutWindow.xaml.cs +++ b/src/VBASync.WPF/AboutWindow.xaml.cs @@ -22,7 +22,10 @@ public AboutWindow() .Replace("{0}", MainWindow.CopyrightYear.ToString()); CopyrightBlock.Inlines.Clear(); CopyrightBlock.Inlines.Add(TextBefore("{1}", copyrightText)); - var licenseRtfHyperlink = new Hyperlink(); + var licenseRtfHyperlink = new Hyperlink + { + NavigateUri = new Uri("file:///") + }; licenseRtfHyperlink.Inlines.Add("LICENSE.rtf"); licenseRtfHyperlink.RequestNavigate += (s, e) => Process.Start(Path.Combine(exeDir, "LICENSE.rtf")); diff --git a/src/VBASync.WPF/MainViewModel.cs b/src/VBASync.WPF/MainViewModel.cs index a33d053..cb3af39 100644 --- a/src/VBASync.WPF/MainViewModel.cs +++ b/src/VBASync.WPF/MainViewModel.cs @@ -12,6 +12,7 @@ namespace VBASync.WPF { internal class MainViewModel : ViewModelBase { + private readonly Action _onLoadIni; private readonly string _lastSessionPath; private Model.ActiveSession _activeSession; @@ -19,7 +20,7 @@ internal class MainViewModel : ViewModelBase private SessionViewModel _session; private SettingsViewModel _settings; - internal MainViewModel(Model.Startup startup) + internal MainViewModel(Model.Startup startup, Action onLoadIni) { Session = new SessionViewModel { @@ -33,6 +34,7 @@ internal MainViewModel(Model.Startup startup) AddNewDocumentsToFile = startup.AddNewDocumentsToFile, DiffTool = startup.DiffTool, DiffToolParameters = startup.DiffToolParameters, + IgnoreEmpty = startup.IgnoreEmpty, Language = startup.Language, Portable = startup.Portable }; @@ -41,6 +43,8 @@ internal MainViewModel(Model.Startup startup) { RecentFiles.Add(recentFile); } + + _onLoadIni = onLoadIni; _lastSessionPath = startup.LastSessionPath; BrowseForSessionCommand = new WpfCommand(v => BrowseForSession()); @@ -110,6 +114,7 @@ public void LoadIni(Model.AppIniFile ini) Session.FilePath = ini.GetString("General", "FilePath") ?? Session.FilePath; Settings.AddNewDocumentsToFile = ini.GetBool("General", "AddNewDocumentsToFile") ?? Settings.AddNewDocumentsToFile; + _onLoadIni(); } public void RefreshActiveSession() @@ -128,6 +133,7 @@ internal void SaveSession(Stream st, bool saveGlobalSettings, bool saveSessionSe if (saveSessionSettings) { sb.AppendLine($"AddNewDocumentsToFile={iniBool(Settings.AddNewDocumentsToFile)}"); + sb.AppendLine($"IgnoreEmpty={iniBool(Settings.IgnoreEmpty)}"); } if (saveGlobalSettings) { diff --git a/src/VBASync.WPF/MainWindow.xaml.cs b/src/VBASync.WPF/MainWindow.xaml.cs index 23f298f..6622589 100644 --- a/src/VBASync.WPF/MainWindow.xaml.cs +++ b/src/VBASync.WPF/MainWindow.xaml.cs @@ -22,7 +22,7 @@ internal partial class MainWindow { public MainWindow(Startup startup) { InitializeComponent(); - DataContext = _vm = new MainViewModel(startup); + DataContext = _vm = new MainViewModel(startup, QuietRefreshIfInputsOk); DataContextChanged += (s, e) => QuietRefreshIfInputsOk(); _vm.Session.PropertyChanged += (s, e) => QuietRefreshIfInputsOk(); QuietRefreshIfInputsOk(); @@ -49,8 +49,6 @@ private void AboutMenu_Click(object sender, RoutedEventArgs e) private void ApplyButton_Click(object sender, RoutedEventArgs e) { - CheckAndFixErrors(); - var changes = _vm.Changes; var committedChanges = changes?.Where(p => p.Commit).ToList(); if (committedChanges == null || committedChanges.Count == 0) diff --git a/src/VBASync.WPF/SettingsViewModel.cs b/src/VBASync.WPF/SettingsViewModel.cs index a934e41..8abdd29 100644 --- a/src/VBASync.WPF/SettingsViewModel.cs +++ b/src/VBASync.WPF/SettingsViewModel.cs @@ -5,6 +5,7 @@ internal class SettingsViewModel : ViewModelBase, Model.ISessionSettings private bool _addNewDocumentsToFile; private string _diffTool; private string _diffToolParameters; + private bool _ignoreEmpty; private string _language; private bool _portable; @@ -26,6 +27,12 @@ public string DiffToolParameters set => SetField(ref _diffToolParameters, value, nameof(DiffToolParameters)); } + public bool IgnoreEmpty + { + get => _ignoreEmpty; + set => SetField(ref _ignoreEmpty, value, nameof(IgnoreEmpty)); + } + public string Language { get => _language; @@ -43,6 +50,7 @@ public bool Portable _addNewDocumentsToFile = _addNewDocumentsToFile, _diffTool = _diffTool, _diffToolParameters = _diffToolParameters, + _ignoreEmpty = _ignoreEmpty, _language = _language, _portable = _portable }; diff --git a/src/VBASync.WPF/SettingsWindow.xaml b/src/VBASync.WPF/SettingsWindow.xaml index 0240c84..473ea1a 100644 --- a/src/VBASync.WPF/SettingsWindow.xaml +++ b/src/VBASync.WPF/SettingsWindow.xaml @@ -201,13 +201,18 @@ + + + diff --git a/src/VBASync/Localization/VBASyncResources.Designer.cs b/src/VBASync/Localization/VBASyncResources.Designer.cs index b68ebf5..b9424e6 100644 --- a/src/VBASync/Localization/VBASyncResources.Designer.cs +++ b/src/VBASync/Localization/VBASyncResources.Designer.cs @@ -820,6 +820,15 @@ public class VBASyncResources { } } + /// + /// Looks up a localized string similar to Ignore empty modules. + /// + public static string SWIgnoreEmpty { + get { + return ResourceManager.GetString("SWIgnoreEmpty", resourceCulture); + } + } + /// /// Looks up a localized string similar to _Language (requires restart). /// diff --git a/src/VBASync/Localization/VBASyncResources.resx b/src/VBASync/Localization/VBASyncResources.resx index 290f5fc..6e17e1e 100644 --- a/src/VBASync/Localization/VBASyncResources.resx +++ b/src/VBASync/Localization/VBASyncResources.resx @@ -1,17 +1,17 @@  - @@ -407,4 +407,7 @@ File 2: {'{2}'} Session + + Ignore empty modules + \ No newline at end of file diff --git a/src/VBASync/Model/Lib.cs b/src/VBASync/Model/Lib.cs index d8a70d1..2538df1 100644 --- a/src/VBASync/Model/Lib.cs +++ b/src/VBASync/Model/Lib.cs @@ -96,6 +96,12 @@ public static Patch GetLicensesPatch(ISession session, string evfPath) newFolder = session.FolderPath; } + if (sessionSettings.IgnoreEmpty) + { + oldModules = oldModules.Where(kvp => ModuleProcessing.HasCode(kvp.Value.Item1)).ToList(); + newModules = newModules.Where(kvp => ModuleProcessing.HasCode(kvp.Value.Item1)).ToList(); + } + // find modules which aren't in both lists and record them as new/deleted var patches = new List(); patches.AddRange(GetDeletedModuleChanges(oldModules, newModules)); diff --git a/src/VBASync/Model/ModuleProcessing.cs b/src/VBASync/Model/ModuleProcessing.cs index 057a9fc..a7f85ad 100644 --- a/src/VBASync/Model/ModuleProcessing.cs +++ b/src/VBASync/Model/ModuleProcessing.cs @@ -93,10 +93,10 @@ public static string FixCase(string oldString, string newString) return sb.ToString(); } - internal static bool HasContent(string moduleText) + internal static bool HasCode(string moduleText) { _inBlock = false; - return moduleText?.Split(new[] { "\r\n" }, StringSplitOptions.None).Any(LineIsContent) ?? false; + return moduleText?.Split(new[] { "\r\n" }, StringSplitOptions.None).Any(LineIsCode) ?? false; } internal static ModuleType TypeFromText(string moduleText) @@ -120,7 +120,7 @@ internal static ModuleType TypeFromText(string moduleText) return ModuleType.Class; } - private static bool LineIsContent(string line) + private static bool LineIsCode(string line) { // must set _inBlock to false before using this var trimLine = line?.TrimStart(); diff --git a/src/VBASync/Model/SessionInterfaces.cs b/src/VBASync/Model/SessionInterfaces.cs index 3ad4ea5..efe3646 100644 --- a/src/VBASync/Model/SessionInterfaces.cs +++ b/src/VBASync/Model/SessionInterfaces.cs @@ -17,5 +17,6 @@ public interface ISession public interface ISessionSettings { bool AddNewDocumentsToFile { get; set; } + bool IgnoreEmpty { get; set; } } } diff --git a/src/VBASync/Model/Startup.cs b/src/VBASync/Model/Startup.cs index 97527d2..1426163 100644 --- a/src/VBASync/Model/Startup.cs +++ b/src/VBASync/Model/Startup.cs @@ -9,6 +9,7 @@ public class Startup : ISession, ISessionSettings public bool AutoRun { get; set; } public string DiffTool { get; set; } public string DiffToolParameters { get; set; } + public bool IgnoreEmpty { get; set; } public string FilePath { get; set; } public string FolderPath { get; set; } public string Language { get; set; } diff --git a/src/VBASync/Program.cs b/src/VBASync/Program.cs index 07ee663..da14f7f 100644 --- a/src/VBASync/Program.cs +++ b/src/VBASync/Program.cs @@ -42,6 +42,7 @@ private static void Main(string[] args) string filePathSwitch = null; string folderPathSwitch = null; var addNewDocumentsToFileSwitch = false; + var ignoreEmptySwitch = false; for (var i = 1; i < args.Length; ++i) { switch (args[i].ToUpperInvariant()) @@ -70,6 +71,10 @@ private static void Main(string[] args) case "/A": addNewDocumentsToFileSwitch = true; break; + case "-I": + case "/I": + ignoreEmptySwitch = true; + break; default: ini.AddFile(args[i]); break; @@ -83,6 +88,7 @@ private static void Main(string[] args) AutoRun = autoRunSwitch || (ini.GetBool("General", "AutoRun") ?? false), 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"),