true $(IntermediateOutputPath)Generated $(CompilerGeneratedFilesOutputPath)\Ariadne.Generator\Ariadne.Generator.ArcScriptGenerator\ $([System.String]::Copy('%(Filename)').Replace('.CodeBehind', '')).cs relativeTo = (folder, path) => { string[] from = Path.GetFullPath(folder).TrimEnd('\\').Split('\\'); string[] to = Path.GetFullPath(path).Split('\\'); int shared = 0; while (shared < from.Length && shared < to.Length - 1 && string.Equals(from[shared], to[shared], StringComparison.OrdinalIgnoreCase)) { ++shared; } if (shared == 0) { return path; } var parts = new List(); for (int i = shared; i < from.Length; ++i) { parts.Add(".."); } for (int i = shared; i < to.Length; ++i) { parts.Add(to[i]); } return string.Join("\\", parts); }; // The name the generator adds a script file's code-behind under: CodeBehindWriter.HintNameOf says the same. Func hintNameOf = scriptPath => { string path = scriptPath.Replace('\\', '/'); string root = ProjectDirectory.Replace('\\', '/').TrimEnd('/') + "/"; if (ProjectDirectory.Length > 0 && path.StartsWith(root, StringComparison.OrdinalIgnoreCase)) { path = path.Substring(root.Length); } var segments = new List(); foreach (string segment in path.Split('/')) { if (segment.Length == 0 || segment == ".") { continue; } segments.Add(segment == ".." ? "__" : segment.Replace(':', '_')); } segments[segments.Count - 1] = Path.GetFileNameWithoutExtension(segments[segments.Count - 1]); return string.Join("/", segments) + ".CodeBehind.cs"; }; foreach (ITaskItem script in Scripts) { string scriptPath = script.GetMetadata("FullPath"); string source = Path.Combine(GeneratedFolder, hintNameOf(scriptPath).Replace('/', '\\')); if (!File.Exists(source)) { continue; } string destination = Path.Combine(Path.GetDirectoryName(scriptPath), Path.GetFileNameWithoutExtension(scriptPath) + ".CodeBehind.cs"); string folder = Path.GetDirectoryName(destination); string text = Regex.Replace(File.ReadAllText(source), "^(#line \\d+ \")([^\"]+)(\")", match => match.Groups[1].Value + relativeTo(folder, match.Groups[2].Value) + match.Groups[3].Value, RegexOptions.Multiline); if (!File.Exists(destination) || File.ReadAllText(destination) != text) { File.WriteAllText(destination, text, new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)); } } ]]>