private SwrFile LoadSwrFile(FileInfo file) { SwrFile swr = new SwrFile(file.FullName); string fileContent = File.ReadAllText(swr.Path); foreach (Group matchedGroup in _payloadPattern.Matches(fileContent).Select(m => m.Groups[1])) { string variableInput = matchedGroup.Value; // Replace known variable with actual value variableInput = variableInput.Replace("!(bindpath.sources)", "src"); Match variableMatch = _variablePattern.Match(variableInput); if (!variableMatch.Success) { // This payload path contains no variables. Skip continue; } Group variableGroup = variableMatch.Groups[1]; string variableName = variableGroup.Value; string variableValuePattern = variableInput.Substring(0, variableGroup.Index - 2) + "(.*)" + variableInput.Substring(variableGroup.Index + variableGroup.Length + 1); // Backslashes should be considered as literals in regex variableValuePattern = variableValuePattern.Replace("\\", "\\\\"); swr.PayloadPaths.Add(new PayloadPath(new Regex(variableValuePattern), variableName)); } return(swr); }
public void TestLoadSwrContent() { string swrPath = Path.Combine(Environment.CurrentDirectory, "Assets", "msi.swr"); SwrFileReader swrFileReader = new SwrFileReader(4); SwrFile[] loadedSwrFiles = swrFileReader.LoadSwrFiles(Environment.CurrentDirectory); SwrFile swrFile = loadedSwrFiles.First(s => s.Path == swrPath); Assert.IsNotNull(swrFile.PayloadPaths, "Swr payload list is null"); Assert.IsTrue(swrFile.PayloadPaths.Count > 0, "Loaded swr has no payload"); Assert.IsTrue(swrFile.PayloadPaths.Any(p => p.VariableName == "NetCoreAppHostPack31Version")); }
/// <summary> /// Loads all swr files under the given root. /// </summary> /// <param name="rootSearchDirectory">Directory that will be searched for swr files. /// Contained folders will automatically be included in the search. /// Path shouldn't be null, empty or whitespace.</param> /// <returns>A list of <see cref="SwrFile"/>s found under the root.</returns> public SwrFile[] LoadSwrFiles(string rootSearchDirectory) { DirectoryInfo rootDirectory = new DirectoryInfo(rootSearchDirectory); if (!rootDirectory.Exists) { return(new SwrFile[0]); } FileInfo[] files = rootDirectory.GetFiles("*.swr", SearchOption.AllDirectories); SwrFile[] swrFiles = new SwrFile[files.Length]; ParallelOptions options = new ParallelOptions() { MaxDegreeOfParallelism = _maxConcurrency }; _ = Parallel.For(0, files.Length, options, i => swrFiles[i] = LoadSwrFile(files[i])); return(swrFiles); }
public void TestDownloadTimeout() { // Generate test default.config string testConfig = @"<?xml version=""1.0"" encoding=""us-ascii""?> <corext> <packages> <package id=""runtime.win-x64.Microsoft.NETCore.DotNetAppHost"" version=""unimportant"" link=""path\to\extract"" /> </packages> </corext>"; string defaultConfigPath = Path.GetTempFileName(); File.WriteAllText(defaultConfigPath, testConfig); // Load default.config DefaultConfigUpdater defaultConfigUpdater = new DefaultConfigUpdater(); bool configLoadResult = defaultConfigUpdater.TryLoad(defaultConfigPath, out string error); Assert.IsTrue(configLoadResult, "Loading default.config failed."); // Load msi.swr string swrPath = Path.Combine(Environment.CurrentDirectory, "Assets", "msi.swr"); SwrFileReader swrFileReader = new SwrFileReader(4); SwrFile[] loadedSwrFiles = swrFileReader.LoadSwrFiles(Environment.CurrentDirectory); SwrFile swrFile = loadedSwrFiles.First(s => s.Path == swrPath); PropsVariableDeducer variableDeducer = new PropsVariableDeducer("https://api.nuget.org/v3/index.json", null); bool operationResult = variableDeducer.DeduceVariableValues(defaultConfigUpdater, new[] { new PackageUpdateResult("runtime.win-x64.Microsoft.NETCore.DotNetAppHost", "unimportant", "3.1.3") }, new SwrFile[] { swrFile }, out List <PropsFileVariableReference> results, out string details, // Timeout in zero seconds TimeSpan.FromSeconds(0)); Assert.IsFalse(operationResult, "Operation should have timed out, but it succeeded."); Assert.IsTrue(details.Contains("timed out")); Assert.IsTrue(results == null || results.Count == 0, $"No props file should have been updated, {results?.Count} were updated"); }
public void TestVariableValueDeduce(int maxWaitMilliseconds) { // Generate test default.config string testConfig = @"<?xml version=""1.0"" encoding=""us-ascii""?> <corext> <packages> <package id=""runtime.win-x64.Microsoft.NETCore.DotNetAppHost"" version=""unimportant"" link=""path\to\extract"" /> </packages> </corext>"; string defaultConfigPath = Path.GetTempFileName(); File.WriteAllText(defaultConfigPath, testConfig); // Load default.config DefaultConfigUpdater defaultConfigUpdater = new DefaultConfigUpdater(); bool configLoadResult = defaultConfigUpdater.TryLoad(defaultConfigPath, out string error); Assert.IsTrue(configLoadResult, "Loading default.config failed."); // Load msi.swr string swrPath = Path.Combine(Environment.CurrentDirectory, "Assets", "msi.swr"); SwrFileReader swrFileReader = new SwrFileReader(4); SwrFile[] loadedSwrFiles = swrFileReader.LoadSwrFiles(Environment.CurrentDirectory); SwrFile swrFile = loadedSwrFiles.First(s => s.Path == swrPath); PropsVariableDeducer variableDeducer = new PropsVariableDeducer("https://api.nuget.org/v3/index.json", null); bool operationResult = variableDeducer.DeduceVariableValues(defaultConfigUpdater, new[] { new PackageUpdateResult("runtime.win-x64.Microsoft.NETCore.DotNetAppHost", "unimportant", "3.1.3") }, new SwrFile[] { swrFile }, out List <PropsFileVariableReference> results, out string details, TimeSpan.FromMilliseconds(maxWaitMilliseconds)); Assert.IsTrue(operationResult, $"Operation failed with message: {details}"); Assert.IsNotNull(results); Assert.IsTrue(results.Any(r => r.ReferencedFilePath == swrPath), "Cannot find the value of variable in given swr."); Assert.IsTrue(results.Any(r => r.ReferencedFilePath == swrPath && r.Name == "AspNetCoreTargetingPack30Version"), "Cannot find the correct variable."); Assert.IsTrue(results.Any(r => r.ReferencedFilePath == swrPath && r.Name == "AspNetCoreTargetingPack30Version" && r.Value == "apphost"), "Wrong value was found for the variable."); }