private void UpgradeProjects(IEnumerable <string> paths, UpgradeType upgradeMode) { var files = paths.Select(s => { var source = File.ReadAllText(s); var file = new Yarn.Compiler.CompilationJob.File() { FileName = s, Source = source, }; return(file); }); AssetDatabase.StartAssetEditing(); try { var upgradeJob = new UpgradeJob(upgradeMode, files); var upgradeResult = LanguageUpgrader.Upgrade(upgradeJob); foreach (var upgradedFile in upgradeResult.Files) { if (upgradedFile.Replacements.Count() == 0 && upgradedFile.IsNewFile == false) { Debug.Log($"No upgrades required for {upgradedFile.Path}"); continue; } // Log some diagnostics about what changes we're making foreach (var diagnostics in upgradedFile.Diagnostics) { Debug.Log($@"{upgradedFile.Path}: {diagnostics}"); } var realPath = upgradedFile.Path.Replace(DirectorySeparatorChar, Path.DirectorySeparatorChar); // Save the text back to disk File.WriteAllText(realPath, upgradedFile.UpgradedSource, System.Text.Encoding.UTF8); // (Re-)import the asset AssetDatabase.ImportAsset(upgradedFile.Path); } } catch (System.Exception e) { Debug.LogError($"Failed to run upgrade job: {e.GetType()} {e.Message}"); return; } AssetDatabase.StopAssetEditing(); }
public void TestUpgradingV1toV2(string directory) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine($"INFO: Loading file {directory}"); storage.Clear(); directory = Path.Combine(TestBase.TestDataPath, directory); var allInputYarnFiles = Directory.EnumerateFiles(directory) .Where(path => path.EndsWith(".yarn")) .Where(path => path.Contains(".upgraded.") == false); var expectedOutputFiles = Directory.EnumerateFiles(directory) .Where(path => path.Contains(".upgraded.")); var testPlanPath = Directory.EnumerateFiles(directory) .Where(path => path.EndsWith(".testplan")) .FirstOrDefault(); var upgradeJob = new UpgradeJob( UpgradeType.Version1to2, allInputYarnFiles.Select(path => new CompilationJob.File { FileName = path, Source = File.ReadAllText(path) })); var upgradeResult = LanguageUpgrader.Upgrade(upgradeJob); // The upgrade result should produce as many files as there are // expected output files Assert.Equal(expectedOutputFiles.Count(), upgradeResult.Files.Count()); // For each file produced by the upgrade job, its content // should match that of the corresponding expected output foreach (var outputFile in upgradeResult.Files) { string extension = Path.GetExtension(outputFile.Path); var expectedOutputFilePath = Path.ChangeExtension(outputFile.Path, ".upgraded" + extension); if (expectedOutputFiles.Contains(expectedOutputFilePath) == false) { // This test case doesn't expect this output (perhaps // it's a test case that isn't expected to succeed.) Ignore it. continue; } Assert.True(File.Exists(expectedOutputFilePath), $"Expected file {expectedOutputFilePath} to exist"); var expectedOutputFileContents = File.ReadAllText(expectedOutputFilePath); Assert.Equal(expectedOutputFileContents, outputFile.UpgradedSource); } // If the test case doesn't contain a test plan file, it's not // expected to compile successfully, so don't do it. Instead, // we'll rely on the fact that the upgraded contents are what // we expected. if (testPlanPath == null) { // Don't compile; just succeed here. return; } // While we're here, correctness-check the upgraded source. (To // be strictly correct, we're using the files on disk, not the // generated source, but we just demonstrated that they're // identical, so that's fine! Saves us having to write them to // a temporary location.) var result = Compiler.Compile(CompilationJob.CreateFromFiles(expectedOutputFiles)); Assert.Empty(result.Diagnostics); stringTable = result.StringTable; // Execute the program and verify thats output matches the test // plan dialogue.SetProgram(result.Program); // Load the test plan LoadTestPlan(testPlanPath); // If this file contains a Start node, run the test case // (otherwise, we're just testing its parsability, which we did // in the last line) if (dialogue.NodeExists("Start")) { RunStandardTestcase(); } }