public ExitCodes Run(string csproj) { if (csproj == null) { throw new ArgumentNullException(nameof(csproj)); } if (!File.Exists(csproj)) { Console.Error.WriteLine($"Project file {csproj} is not found!"); return(ExitCodes.ProjectNotFound); } var visited = new HashSet <string>(StringComparer.OrdinalIgnoreCase); bool skipPackage(string id) { if (visited.Contains(id)) { return(true); } if (CodeGeneration.SkipPackages.ForRestore(id)) { return(true); } visited.Add(id); return(false); } var projectDir = Path.GetDirectoryName(csproj); var config = GeneratorConfig.LoadFromFile(Path.Combine(projectDir, "sergen.json")); GlobFilter include = null; if (config.Restore?.Include.IsEmptyOrNull() == false) { include = new GlobFilter(config.Restore.Include); } GlobFilter exclude = null; if (config.Restore?.Exclude.IsEmptyOrNull() == false) { exclude = new GlobFilter(config.Restore.Exclude); } var targetRoot = Path.GetDirectoryName(csproj); var restoredFromProjectReference = new HashSet <string>(StringComparer.OrdinalIgnoreCase); void restoreFile(string file, string relative) { relative = PathHelper.ToPath(relative); if (include != null && !include.IsMatch(relative)) { return; } if (exclude != null && exclude.IsMatch(relative)) { return; } if (restoredFromProjectReference.Contains(relative)) { return; } var target = Path.Combine(targetRoot, relative); if (File.Exists(target)) { if (!File.ReadAllBytes(target) .SequenceEqual(File.ReadAllBytes(file))) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Restoring: " + relative); Console.ResetColor(); File.Copy(file, target, true); } } else { if (!Directory.Exists(target)) { Directory.CreateDirectory(Path.GetDirectoryName(target)); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Restoring: " + relative); Console.ResetColor(); File.Copy(file, target, false); } } try { foreach (var reference in EnumerateProjectReferences(csproj, new HashSet <string>(StringComparer.OrdinalIgnoreCase))) { IBuildProject project; try { project = ProjectSystem.LoadProject(reference); } catch { continue; } foreach (var item in project.AllEvaluatedItems .Where(x => string.Equals(x.ItemType, "TypingsToPackage", StringComparison.OrdinalIgnoreCase) || string.Equals(x.ItemType, "Content", StringComparison.OrdinalIgnoreCase) || string.Equals(x.ItemType, "None", StringComparison.OrdinalIgnoreCase) || string.Equals(x.ItemType, "TypeScriptCompile", StringComparison.OrdinalIgnoreCase)) .Where(x => x.EvaluatedInclude?.EndsWith(".d.ts", StringComparison.OrdinalIgnoreCase) == true)) { var sourceFile = Path.Combine(Path.GetDirectoryName(reference), item.EvaluatedInclude); if (!File.Exists(sourceFile)) { continue; } if (!string.Equals(item.ItemType, "TypingsToPackage", StringComparison.OrdinalIgnoreCase) && item.GetMetadataValue("Pack") != "true") { continue; } var packagePath = item.GetMetadataValue("PackagePath")?.Trim(); if (!string.IsNullOrEmpty(packagePath)) { foreach (var path in packagePath.Split(';', StringSplitOptions.RemoveEmptyEntries)) { if (!PathHelper.ToUrl(path).StartsWith("typings/", StringComparison.OrdinalIgnoreCase)) { continue; } restoreFile(sourceFile, path); restoredFromProjectReference.Add(path); } } } } } catch (Exception ex) { Console.Error.WriteLine(ex.Message); } var packagesDir = PackageHelper.DeterminePackagesPath(FileSystem); if (packagesDir == null) { Console.Error.WriteLine("Can't determine NuGet packages directory!"); return(ExitCodes.CantDeterminePackagesDir); } var queue = new Queue <(string ID, string Version)>(); foreach (var x in EnumeratePackageReferences(csproj)) { if (!skipPackage(x.Id) && !string.IsNullOrEmpty(x.Version)) { queue.Enqueue(x); } } ; while (queue.Count > 0) { var dep = queue.Dequeue(); var id = dep.ID; var ver = dep.Version.Trim(); if (ver.EndsWith("-*", StringComparison.Ordinal)) { ver = ver[0..^ 2];
public void Run(string csproj) { var process = Process.Start(new ProcessStartInfo { FileName = "dotnet", WorkingDirectory = Path.GetDirectoryName(csproj), CreateNoWindow = true, Arguments = "restore \"" + csproj + "\"" }); process.WaitForExit(); if (process.ExitCode > 0) { Console.Error.WriteLine("Error executing dotnet restore!"); Environment.Exit(process.ExitCode); } var packagesDir = new PackageHelper().DeterminePackagesPath(); var visited = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var queue = new Queue <Tuple <string, string, string> >(); Func <string, bool> skipPackage = id => { if (visited.Contains(id)) { return(true); } foreach (var skip in skipPackages) { if (id.StartsWith(skip, StringComparison.OrdinalIgnoreCase)) { return(true); } } visited.Add(id); return(false); }; var csprojElement = XElement.Parse(System.IO.File.ReadAllText(csproj)); EnumerateProjectDeps(csprojElement, (fw, id, ver) => { if (!skipPackage(id)) { queue.Enqueue(new Tuple <string, string, string>(fw, id, ver)); } }); while (queue.Count > 0) { var dep = queue.Dequeue(); var id = dep.Item2; var ver = dep.Item3.Trim(); if (ver.EndsWith("-*")) { ver = ver.Substring(0, ver.Length - 2); } else if (ver.StartsWith("[") && ver.EndsWith("]")) { ver = ver.Substring(1, ver.Length - 2).Trim(); } var packageFolder = Path.Combine(Path.Combine(packagesDir, id), ver); if (!Directory.Exists(packageFolder)) { packageFolder = Path.Combine(Path.Combine(packagesDir, id.ToLowerInvariant()), ver); } var nuspecFile = Path.Combine(packageFolder, id + ".nuspec"); if (!File.Exists(nuspecFile)) { nuspecFile = Path.Combine(packageFolder, id.ToLowerInvariant() + ".nuspec"); if (!File.Exists(nuspecFile)) { continue; } } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Processing: " + id); Console.ResetColor(); var contentRoot = Path.Combine(packageFolder, "Content/".Replace('/', Path.DirectorySeparatorChar)); if (!Directory.Exists(contentRoot)) { contentRoot = Path.Combine(packageFolder, "content/".Replace('/', Path.DirectorySeparatorChar)); } if (Directory.Exists(contentRoot)) { var targetRoot = Path.GetDirectoryName(csproj); foreach (var file in Directory.GetFiles(contentRoot, "*.*", SearchOption.AllDirectories)) { var extension = Path.GetExtension(file); if (String.Compare(extension, ".transform", StringComparison.OrdinalIgnoreCase) == 0) { continue; } var relative = file.Substring(contentRoot.Length); if (relative.StartsWith("scripts/typings/".Replace('/', Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase)) { relative = relative.Substring("scripts/".Length); } else { relative = Path.Combine("wwwroot", relative); } var target = Path.Combine(targetRoot, relative); if (File.Exists(target)) { if (!File.ReadAllBytes(target) .SequenceEqual(File.ReadAllBytes(file))) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Restoring: " + relative); Console.ResetColor(); File.Copy(file, target, true); } } else { if (!Directory.Exists(target)) { Directory.CreateDirectory(Path.GetDirectoryName(target)); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Restoring: " + relative); Console.ResetColor(); File.Copy(file, target, false); } } } var nuspec = XElement.Parse(File.ReadAllText(nuspecFile)); XNamespace ns = "http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"; var meta = nuspec.Element(ns + "metadata"); if (meta == null) { continue; } var deps = meta.Element(ns + "dependencies"); if (deps == null) { continue; } var fw = dep.Item1; var groups = deps.Elements(ns + "group"); if (groups.Any()) { foreach (var group in groups) { var target = group.Attribute("targetFramework").Value; if (string.IsNullOrEmpty(target) || String.Compare(target, fw, StringComparison.OrdinalIgnoreCase) == 0 || target.StartsWith(".NETStandard", StringComparison.OrdinalIgnoreCase) || target.StartsWith("netstandard", StringComparison.OrdinalIgnoreCase) || target.StartsWith("netcore", StringComparison.OrdinalIgnoreCase)) { foreach (var dep2 in group.Elements(ns + "dependency")) { var id2 = dep2.Attribute("id").Value; if (!skipPackage(id2)) { queue.Enqueue(new Tuple <string, string, string>(fw, id2, dep2.Attribute("version").Value)); } } } } } else { foreach (var dep2 in deps.Elements(ns + "dependency")) { var id2 = dep2.Attribute("id").Value; if (!skipPackage(id2)) { queue.Enqueue(new Tuple <string, string, string>(fw, id2, dep2.Attribute("version").Value)); } } } } }
public void Run(string projectJson) { var process = Process.Start(new ProcessStartInfo { FileName = "dotnet", WorkingDirectory = Path.GetDirectoryName(projectJson), CreateNoWindow = true, Arguments = "restore project.json" }); process.WaitForExit(); if (process.ExitCode > 0) { Console.Error.WriteLine("Error executing dotnet restore!"); Environment.Exit(process.ExitCode); } var packagesDir = new PackageHelper().DeterminePackagesPath(); var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase); var queue = new Queue<Tuple<string, string, string>>(); Func<string, bool> skipPackage = id => { if (visited.Contains(id)) return true; foreach (var skip in skipPackages) if (id.StartsWith(skip, StringComparison.OrdinalIgnoreCase)) return true; visited.Add(id); return false; }; var proj = JObject.Parse(File.ReadAllText(projectJson)); EnumerateProjectJsonDeps(proj, (fw, id, ver) => { if (!skipPackage(id)) queue.Enqueue(new Tuple<string, string, string>(fw, id, ver)); }); while (queue.Count > 0) { var dep = queue.Dequeue(); var id = dep.Item2; var ver = dep.Item3.Trim(); if (ver.EndsWith("-*")) ver = ver.Substring(0, ver.Length - 2); else if (ver.StartsWith("[") && ver.EndsWith("]")) { ver = ver.Substring(1, ver.Length - 2).Trim(); } var packageFolder = Path.Combine(Path.Combine(packagesDir, id), ver); var nuspecFile = Path.Combine(packageFolder, id + ".nuspec"); if (!File.Exists(nuspecFile)) continue; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Processing: " + id); Console.ResetColor(); var contentRoot = Path.Combine(packageFolder, "content/".Replace('/', Path.DirectorySeparatorChar)); if (Directory.Exists(contentRoot)) { var targetRoot = Path.GetDirectoryName(projectJson); foreach (var file in Directory.GetFiles(contentRoot, "*.*", SearchOption.AllDirectories)) { var extension = Path.GetExtension(file); if (String.Compare(extension, ".transform", StringComparison.OrdinalIgnoreCase) == 0) continue; var relative = file.Substring(contentRoot.Length); if (relative.StartsWith("scripts/typings/".Replace('/', Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase)) { relative = relative.Substring("scripts/".Length); } else { relative = Path.Combine("wwwroot", relative); } var target = Path.Combine(targetRoot, relative); if (File.Exists(target)) { if (!File.ReadAllBytes(target) .SequenceEqual(File.ReadAllBytes(file))) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Restoring: " + relative); Console.ResetColor(); File.Copy(file, target, true); } } else { if (!Directory.Exists(target)) Directory.CreateDirectory(Path.GetDirectoryName(target)); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Restoring: " + relative); Console.ResetColor(); File.Copy(file, target, false); } } } var nuspec = XElement.Parse(File.ReadAllText(nuspecFile)); XNamespace ns = "http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"; var meta = nuspec.Element(ns + "metadata"); if (meta == null) continue; var deps = meta.Element(ns + "dependencies"); if (deps == null) continue; var fw = dep.Item1; var groups = deps.Elements(ns + "group"); if (groups.Any()) { foreach (var group in groups) { var target = group.Attribute("targetFramework").Value; if (string.IsNullOrEmpty(target) || String.Compare(target, fw, StringComparison.OrdinalIgnoreCase) == 0 || target.StartsWith(".NETStandard", StringComparison.OrdinalIgnoreCase) || target.StartsWith("netstandard", StringComparison.OrdinalIgnoreCase) || target.StartsWith("netcore", StringComparison.OrdinalIgnoreCase)) { foreach (var dep2 in group.Elements(ns + "dependency")) { var id2 = dep2.Attribute("id").Value; if (!skipPackage(id2)) queue.Enqueue(new Tuple<string, string, string>(fw, id2, dep2.Attribute("version").Value)); } } } } else { foreach (var dep2 in deps.Elements(ns + "dependency")) { var id2 = dep2.Attribute("id").Value; if (!skipPackage(id2)) queue.Enqueue(new Tuple<string, string, string>(fw, id2, dep2.Attribute("version").Value)); } } } }