|
| 1 | +#!/usr/bin/env dotnet |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for more information. |
| 5 | + |
| 6 | +using System.Xml.Linq; |
| 7 | + |
| 8 | +// Verifies the shared source files under `src/Features/CSharp/Portable/SyncedSource/FileBasedPrograms` |
| 9 | +// exactly match the copies shipped in the `Microsoft.DotNet.FileBasedPrograms` NuGet package |
| 10 | +// If any file is missing or differs, the local file is regenerated from the package content |
| 11 | +// and the script fails listing the changes. |
| 12 | +// |
| 13 | +// We do this instead of including the source package directly as `PackageReference` |
| 14 | +// because that would not work in source build (which requires roslyn to build before sdk). |
| 15 | + |
| 16 | +var root = Path.Join(AppContext.GetData("EntryPointFileDirectoryPath") as string, ".."); |
| 17 | +if (!Directory.Exists(root)) throw new InvalidOperationException($"Could not locate repo root: {root}"); |
| 18 | + |
| 19 | +var versionDetailsProps = Path.Combine(root, "eng", "Version.Details.props"); |
| 20 | +if (!File.Exists(versionDetailsProps)) throw new InvalidOperationException($"'{versionDetailsProps}' not found."); |
| 21 | + |
| 22 | +var packageVersion = GetPackageVersion(versionDetailsProps, "MicrosoftDotNetFileBasedProgramsPackageVersion"); |
| 23 | + |
| 24 | +var globalPackagesFolder = GetGlobalPackagesFolder(); |
| 25 | +if (!Directory.Exists(globalPackagesFolder)) throw new InvalidOperationException($"Global packages folder not found: {globalPackagesFolder}"); |
| 26 | + |
| 27 | +var packageRoot = Path.Combine(globalPackagesFolder, "microsoft.dotnet.filebasedprograms", packageVersion); |
| 28 | +if (!Directory.Exists(packageRoot)) throw new InvalidOperationException($"Package folder not found: {packageRoot}"); |
| 29 | + |
| 30 | +var contentFilesDir1 = Path.Combine(packageRoot, "contentFiles", "cs", "any"); |
| 31 | +if (!Directory.Exists(contentFilesDir1)) throw new InvalidOperationException($"contentFiles directory not found: {contentFilesDir1}"); |
| 32 | + |
| 33 | +var contentFilesDir2 = Path.Combine(packageRoot, "contentFiles", "cs", "netstandard2.0"); |
| 34 | +if (!Directory.Exists(contentFilesDir2)) throw new InvalidOperationException($"contentFiles directory not found: {contentFilesDir2}"); |
| 35 | + |
| 36 | +var localSourceDir = Path.Combine(root, "src", "Features", "CSharp", "Portable", "SyncedSource", "FileBasedPrograms"); |
| 37 | +if (!Directory.Exists(Path.GetDirectoryName(localSourceDir))) throw new InvalidOperationException($"Local source directory not found: {localSourceDir}"); |
| 38 | + |
| 39 | +var extensions = new[] { ".cs", ".resx", ".editorconfig" }; |
| 40 | + |
| 41 | +var packageFiles = Directory.GetFiles(contentFilesDir1, "*", SearchOption.TopDirectoryOnly) |
| 42 | + .Concat(Directory.GetFiles(contentFilesDir2, "*", SearchOption.TopDirectoryOnly)) |
| 43 | + .Where(f => extensions.Any(ext => f.EndsWith(ext, StringComparison.OrdinalIgnoreCase))) |
| 44 | + .ToList(); |
| 45 | +if (packageFiles.Count == 0) throw new InvalidOperationException("No package files found."); |
| 46 | + |
| 47 | +var updateSnapshots = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI")); |
| 48 | + |
| 49 | +if (updateSnapshots) Directory.CreateDirectory(localSourceDir); |
| 50 | + |
| 51 | +var mismatches = new List<string>(); |
| 52 | +foreach (var pkgFile in packageFiles) |
| 53 | +{ |
| 54 | + var fileName = Path.GetFileName(pkgFile); |
| 55 | + var localFile = Path.Combine(localSourceDir, fileName); |
| 56 | + var pkgContent = File.ReadAllText(pkgFile); |
| 57 | + |
| 58 | + if (!File.Exists(localFile)) |
| 59 | + { |
| 60 | + // Create missing file from package content. |
| 61 | + if (updateSnapshots) File.WriteAllText(localFile, pkgContent); |
| 62 | + mismatches.Add($"Added missing file: {fileName}"); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + var localContent = File.ReadAllText(localFile); |
| 67 | + if (!string.Equals(localContent.ReplaceLineEndings(), pkgContent.ReplaceLineEndings(), StringComparison.Ordinal)) |
| 68 | + { |
| 69 | + // Regenerate local file to match package. |
| 70 | + if (updateSnapshots) File.WriteAllText(localFile, pkgContent); |
| 71 | + mismatches.Add($"Updated file: {fileName}"); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// If there are extra local files that are expected to mirror package files, report them but do not delete. |
| 76 | +var localMirrorFiles = Directory.GetFiles(localSourceDir, "*", SearchOption.TopDirectoryOnly) |
| 77 | + .Where(f => extensions.Any(ext => f.EndsWith(ext, StringComparison.OrdinalIgnoreCase))) |
| 78 | + .Select(Path.GetFileName) |
| 79 | + .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| 80 | +foreach (var pkgName in packageFiles.Select(Path.GetFileName)) |
| 81 | + localMirrorFiles.Remove(pkgName); |
| 82 | +if (localMirrorFiles.Count > 0) |
| 83 | +{ |
| 84 | + mismatches.Add("Extra local files (not in package): " + string.Join(", ", localMirrorFiles)); |
| 85 | +} |
| 86 | + |
| 87 | +if (mismatches.Count > 0) |
| 88 | +{ |
| 89 | + var action = updateSnapshots ? "Regenerated" : "Not regenerated in CI"; |
| 90 | + throw new InvalidOperationException($"Shared source for FileBasedPrograms is out of sync with package. {action}. Changes:\n" + string.Join("\n", mismatches)); |
| 91 | +} |
| 92 | + |
| 93 | +Console.WriteLine("OK"); |
| 94 | + |
| 95 | +static string GetPackageVersion(string xmlFilePath, string propertyName) |
| 96 | +{ |
| 97 | + var doc = XDocument.Load(xmlFilePath); |
| 98 | + // Look for <{propertyName}>{version}</{propertyName}> |
| 99 | + var packageVersionElement = doc.Descendants().FirstOrDefault(e => |
| 100 | + string.Equals(e.Name.LocalName, propertyName, StringComparison.OrdinalIgnoreCase)) |
| 101 | + ?? throw new InvalidOperationException($"'{propertyName}' not found in '{xmlFilePath}'"); |
| 102 | + return packageVersionElement.Value; |
| 103 | +} |
| 104 | + |
| 105 | +static string GetGlobalPackagesFolder() |
| 106 | +{ |
| 107 | + // Respect NUGET_PACKAGES if set; otherwise default location under user profile. |
| 108 | + var envOverride = Environment.GetEnvironmentVariable("NUGET_PACKAGES"); |
| 109 | + if (!string.IsNullOrWhiteSpace(envOverride)) |
| 110 | + return envOverride!; |
| 111 | + |
| 112 | + var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| 113 | + if (string.IsNullOrWhiteSpace(userProfile)) |
| 114 | + throw new InvalidOperationException("Cannot determine user profile path for global packages folder."); |
| 115 | + return Path.Combine(userProfile, ".nuget", "packages"); |
| 116 | +} |
0 commit comments