public void TestCompacting() { var resolvableFolder = new ResolvablePath(); resolvableFolder.Resolved = "C:\\Program Files (x86)\\Test"; Assert.AreEqual("{{ProgramFilesX86}}\\Test", resolvableFolder.Compacted, "The value must be compacted to ProgramFiles (x86)."); resolvableFolder.Resolved = "C:\\Program Files\\Test"; Assert.AreEqual("{{ProgramFiles}}\\Test", resolvableFolder.Compacted); resolvableFolder.Compacted = "{{ProgramFilesX86}}\\ABC"; Assert.AreEqual("C:\\Program Files (x86)\\ABC", resolvableFolder.Resolved); resolvableFolder.Compacted = "{{ProgramFiles}}\\ABC"; Assert.AreEqual("C:\\Program Files\\ABC", resolvableFolder.Resolved); resolvableFolder.Compacted = null; Assert.AreEqual(resolvableFolder.Compacted, resolvableFolder.Resolved, "If compacted path is null, the resolved path must be also null."); resolvableFolder.Resolved = null; Assert.AreEqual(resolvableFolder.Compacted, resolvableFolder.Resolved, "If resolved path is null, the compacted path must be also null."); resolvableFolder.Compacted = string.Empty; Assert.AreEqual(resolvableFolder.Compacted, resolvableFolder.Resolved, "If compacted path is an empty string, the resolved path must be also an empty string."); resolvableFolder.Resolved = string.Empty; Assert.AreEqual(resolvableFolder.Compacted, resolvableFolder.Resolved, "If resolved path is an empty string, the compacted path must be also an empty string."); resolvableFolder.Compacted = "abc"; Assert.AreEqual("abc", resolvableFolder.Resolved, "If compacted path has no variables, the resolved path must be the same value."); resolvableFolder.Resolved = "abc"; Assert.AreEqual("abc", resolvableFolder.Compacted, "If resolved path does not have any resolvable path, then the compacted path must be the same."); }
public void TestSerialization() { var folder = new ResolvablePath("{{ProgramFiles}}\\ABC"); var serialized = JsonConvert.SerializeObject(folder, new ResolvablePathConverter()); var deserialized = JsonConvert.DeserializeObject <ResolvablePath>(serialized, new ResolvablePathConverter()); Assert.IsTrue(serialized.Contains("\"{{ProgramFiles}}\\\\ABC\"", StringComparison.Ordinal), "Serialized object must be a plain type (compacted)."); Assert.AreEqual(deserialized.Compacted, folder.Compacted, "Value before and after serialization must be the same."); Assert.AreEqual(deserialized.Resolved, folder.Resolved, "Value before and after serialization must be the same."); }
public void TestImplicitConversion() { ResolvablePath folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.AdminTools, Environment.SpecialFolderOption.DoNotVerify), "Test"); Assert.AreEqual("{{AdminTools}}\\Test", folder.Compacted); string resolved = folder; Assert.AreEqual(folder.Resolved, resolved); }
public void Execute(ResolvablePath filePath, bool throwExceptions = false) { ResolvablePath toolPath = null; EditorType? editorType = null; var config = this.configurationService.GetCurrentConfiguration().Editing; if (string.Equals("appxmanifest.xml", Path.GetFileName(filePath.Resolved), StringComparison.OrdinalIgnoreCase)) { toolPath = config?.ManifestEditor; editorType = config?.ManifestEditorType; } else if (string.Equals("config.json", Path.GetFileName(filePath.Resolved), StringComparison.OrdinalIgnoreCase)) { toolPath = config?.PsfEditor; editorType = config?.PsfEditorType; } else { var ext = Path.GetExtension(filePath).ToLowerInvariant(); switch (ext) { case ".msix": case ".appx": toolPath = config?.MsixEditor; editorType = config?.MsixEditorType; break; case ".appinstaller": toolPath = config?.AppInstallerEditor; editorType = config?.AppInstallerEditorType; break; case ".ps1": toolPath = config?.PowerShellEditor; editorType = config?.PowerShellEditorType; break; } } if (editorType.HasValue) { this.Execute(editorType.Value, toolPath, filePath, throwExceptions); } else { this.Execute(EditorType.Default, null, filePath, throwExceptions); } }
public void Execute(EditorType editorType, ResolvablePath toolPath, ResolvablePath filePath, bool throwExceptions = false) { try { switch (editorType) { case EditorType.Default: { var spi = new ProcessStartInfo(filePath.Resolved) { UseShellExecute = true }; Process.Start(spi); break; } case EditorType.Ask: { Process.Start("rundll32.exe", "shell32.dll, OpenAs_RunDLL " + filePath.Resolved); break; } default: { var spi = new ProcessStartInfo(toolPath.Resolved, "\"" + filePath.Resolved + "\"") { UseShellExecute = true }; Process.Start(spi); break; } } } catch (Exception e) { Logger.Error(e, $"Could not open the file {filePath.Resolved}"); if (throwExceptions) { throw; } this.interactionService.ShowError($"Could not open the file {filePath.Resolved}", e, InteractionResult.OK); } }