public void ReplaceTextModule(string name, ModuleType type, VbaFolder source, string fallbackText) { var sourcePath = source.FindModulePath(name, type); var destinationPath = FindModulePath(name, type); if (string.IsNullOrEmpty(destinationPath)) { destinationPath = _so.PathCombine(FolderPath, name + ModuleProcessing.ExtensionFromType(type)); } if (_so.FileExists(sourcePath)) { if (source.ProjectEncoding.Equals(ProjectEncoding)) { _so.FileCopy(sourcePath, destinationPath, true); } else { _so.FileWriteAllText(destinationPath, _so.FileReadAllText(sourcePath, source.ProjectEncoding), ProjectEncoding); } } else { _so.FileWriteAllText(destinationPath, fallbackText, ProjectEncoding); } }
private static IEnumerable <Patch> GetDeletedModuleChanges( IList <KeyValuePair <string, Tuple <string, ModuleType> > > oldModules, IList <KeyValuePair <string, Tuple <string, ModuleType> > > newModules, ActionType action, bool deleteDocumentsFromFile) { var deleted = new HashSet <string>(oldModules.Select(kvp => kvp.Key).Except(newModules.Select(kvp => kvp.Key))); var deletedModulesKvp = oldModules.Where(kvp => deleted.Contains(kvp.Key)).ToList(); if (action == ActionType.Extract || deleteDocumentsFromFile) { return(deletedModulesKvp.Select(kvp => Patch.MakeDeletion(kvp.Key, kvp.Value.Item2, kvp.Value.Item1))); } return(deletedModulesKvp.Where(kvp => kvp.Value.Item2 != ModuleType.StaticClass) .Select(kvp => Patch.MakeDeletion(kvp.Key, kvp.Value.Item2, kvp.Value.Item1)) .Concat(deletedModulesKvp.Where(kvp => kvp.Value.Item2 == ModuleType.StaticClass).SelectMany(GetStubModulePatches))); IEnumerable <Patch> GetStubModulePatches(KeyValuePair <string, Tuple <string, ModuleType> > kvp) { var oldText = kvp.Value.Item1; var newText = ModuleProcessing.StubOut(oldText); if (oldText?.TrimEnd('\r', '\n') == newText?.TrimEnd('\r', '\n')) { return(new Patch[0]); } return(Patch.CompareSideBySide(new SideBySideArgs { Name = kvp.Key, NewText = newText, NewType = kvp.Value.Item2, OldText = oldText, OldType = kvp.Value.Item2 })); } }
public static IEnumerable <Patch> GetModulePatches(ISession session, ISessionSettings sessionSettings, string vbaFolderPath, IList <KeyValuePair <string, Tuple <string, ModuleType> > > folderModules, IList <KeyValuePair <string, Tuple <string, ModuleType> > > fileModules) { IList <KeyValuePair <string, Tuple <string, ModuleType> > > oldModules; IList <KeyValuePair <string, Tuple <string, ModuleType> > > newModules; string oldFolder; string newFolder; if (session.Action == ActionType.Extract) { oldModules = folderModules; newModules = fileModules; oldFolder = session.FolderPath; newFolder = vbaFolderPath; } else { oldModules = fileModules; newModules = folderModules; oldFolder = vbaFolderPath; 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 <Patch>(); patches.AddRange(GetDeletedModuleChanges(oldModules, newModules)); patches.AddRange(GetNewModuleChanges(oldModules, newModules, session.Action, sessionSettings.AddNewDocumentsToFile)); // this also filters the new/deleted modules from the last step var sideBySide = (from o in oldModules join n in newModules on o.Key equals n.Key select new SideBySideArgs { Name = o.Key, OldType = o.Value.Item2, NewType = n.Value.Item2, OldText = o.Value.Item1, NewText = n.Value.Item1 }).ToArray(); patches.AddRange(GetFrxChanges(oldFolder, newFolder, sideBySide.Where(x => x.NewType == ModuleType.Form).Select(x => x.Name))); sideBySide = sideBySide.Where(sxs => sxs.OldText != sxs.NewText).ToArray(); foreach (var sxs in sideBySide) { patches.AddRange(Patch.CompareSideBySide(sxs)); } return(patches); }
public void AddModule(string name, ModuleType type, VbaFolder source) { var path = _so.PathCombine(FolderPath, name + ModuleProcessing.ExtensionFromType(type)); _so.FileCopy(source.FindModulePath(name, type), path); if (type == ModuleType.Form) { var frxPath = path.Substring(0, path.Length - 4) + ".frx"; _so.FileCopy(source.FindFrxPath(name), frxPath); } ModuleFilePaths.Add(path); }
public static IList <KeyValuePair <string, Tuple <string, ModuleType> > > GetFolderModules(string folderPath) { var modulesText = new Dictionary <string, Tuple <string, ModuleType> >(); var extensions = new[] { ".bas", ".cls", ".frm" }; var projIni = new ProjectIni(Path.Combine(folderPath, "Project.INI")); if (File.Exists(Path.Combine(folderPath, "Project.INI.local"))) { projIni.AddFile(Path.Combine(folderPath, "Project.INI.local")); } var projEncoding = Encoding.GetEncoding(projIni.GetInt("General", "CodePage") ?? Encoding.Default.CodePage); foreach (var filePath in Directory.GetFiles(folderPath, "*.*").Where(s => extensions.Any(s.EndsWith)).Select(s => Path.Combine(folderPath, Path.GetFileName(s)))) { var moduleText = File.ReadAllText(filePath, projEncoding).TrimEnd('\r', '\n') + "\r\n"; modulesText[Path.GetFileNameWithoutExtension(filePath)] = Tuple.Create(moduleText, ModuleProcessing.TypeFromText(moduleText)); } return(modulesText.ToList()); }
public IList <KeyValuePair <string, Tuple <string, ModuleType> > > GetCodeModules() { var modulesText = new Dictionary <string, Tuple <string, ModuleType> >(); var projIni = new ProjectIni(_so.PathCombine(FolderPath, "Project.ini")); projIni.AddFile(_so.PathCombine(FolderPath, "Project.ini.local")); ProjectEncoding = Encoding.GetEncoding(projIni.GetInt("General", "CodePage") ?? Encoding.Default.CodePage); // no need to re-read projIni, since we only wanted to get the encoding off of it anyway! ModuleFilePaths = GetModuleFilePaths(); foreach (var filePath in ModuleFilePaths) { var ext = _so.PathGetExtension(filePath).ToUpperInvariant(); if (ext == ".BAS" || ext == ".CLS" || ext == ".FRM") { var moduleText = _so.FileReadAllText(filePath, ProjectEncoding).TrimEnd('\r', '\n') + "\r\n"; modulesText[_so.PathGetFileNameWithoutExtension(filePath)] = Tuple.Create(moduleText, ModuleProcessing.TypeFromText(moduleText)); } } return(modulesText.ToList()); }
protected string FindModulePath(string name, ModuleType type) => ModuleFilePaths.Find(s => string.Equals(_so.PathGetFileName(s), name + ModuleProcessing.ExtensionFromType(type), StringComparison.OrdinalIgnoreCase));
public void Apply(IEnumerable <Patch> changes) { if (_session.Action == ActionType.Extract) { foreach (var p in changes) { var fileName = p.ModuleName + ModuleProcessing.ExtensionFromType(p.ModuleType); switch (p.ChangeType) { case ChangeType.DeleteFile: File.Delete(Path.Combine(_session.FolderPath, fileName)); if (p.ModuleType == ModuleType.Form) { File.Delete(Path.Combine(_session.FolderPath, p.ModuleName + ".frx")); } break; case ChangeType.ChangeFormControls: File.Copy(Path.Combine(_vf.FolderPath, p.ModuleName + ".frx"), Path.Combine(_session.FolderPath, p.ModuleName + ".frx"), true); break; case ChangeType.Licenses: if (!File.Exists(Path.Combine(_vf.FolderPath, fileName))) { File.Delete(Path.Combine(_session.FolderPath, fileName)); } else { File.Copy(Path.Combine(_vf.FolderPath, fileName), Path.Combine(_session.FolderPath, fileName), true); } break; default: File.Copy(Path.Combine(_vf.FolderPath, fileName), Path.Combine(_session.FolderPath, fileName), true); if (p.ChangeType == ChangeType.AddFile && p.ModuleType == ModuleType.Form) { File.Copy(Path.Combine(_vf.FolderPath, p.ModuleName + ".frx"), Path.Combine(_session.FolderPath, p.ModuleName + ".frx"), true); } break; } } _sessionSettings.AfterExtractHook.Execute(_session.FolderPath); } else { foreach (var p in changes) { var fileName = p.ModuleName + ModuleProcessing.ExtensionFromType(p.ModuleType); switch (p.ChangeType) { case ChangeType.DeleteFile: File.Delete(Path.Combine(_vf.FolderPath, fileName)); if (p.ModuleType == ModuleType.Form) { File.Delete(Path.Combine(_vf.FolderPath, p.ModuleName + ".frx")); } break; case ChangeType.ChangeFormControls: File.Copy(Path.Combine(_session.FolderPath, p.ModuleName + ".frx"), Path.Combine(_vf.FolderPath, p.ModuleName + ".frx"), true); break; case ChangeType.Licenses: if (!File.Exists(Path.Combine(_session.FolderPath, fileName))) { File.Delete(Path.Combine(_vf.FolderPath, fileName)); } else { File.Copy(Path.Combine(_session.FolderPath, fileName), Path.Combine(_vf.FolderPath, fileName), true); } break; default: File.Copy(Path.Combine(_session.FolderPath, fileName), Path.Combine(_vf.FolderPath, fileName), true); if (p.ChangeType == ChangeType.AddFile && p.ModuleType == ModuleType.Form) { File.Copy(Path.Combine(_session.FolderPath, p.ModuleName + ".frx"), Path.Combine(_vf.FolderPath, p.ModuleName + ".frx"), true); } break; } } _sessionSettings.BeforePublishHook.Execute(_vf.FolderPath); _vf.Write(_session.FilePath); } }