public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) { foreach (DirectoryInfo dir in source.GetDirectories()) { DirectoryInfo subDirectory; var subDirectoryPath = Path.Combine(target.FullName, dir.Name); if (Directory.Exists(subDirectoryPath)) { subDirectory = new DirectoryInfo(subDirectoryPath); } else { subDirectory = target.CreateSubdirectory(dir.Name); File.WriteAllBytes(subDirectoryPath + ".meta", Packer.GenerateMeta(target.FullName, target.FullName).Item2); } CopyFilesRecursively(dir, subDirectory); } foreach (FileInfo file in source.GetFiles()) { if (IsIgnoreFile(file.FullName) == false) { var fullpath = Path.Combine(target.FullName, file.Name); if (File.Exists(fullpath) == false) { File.WriteAllBytes(fullpath + ".meta", Packer.GenerateMeta(fullpath, fullpath).Item2); } file.CopyTo(fullpath, true); } } }
public static bool ExtractUnityPackage(string packageFile, string outputDir, string projectId, bool includeExtra, bool includeMerged) { var createProjectFile = false; var tempPath = CreateTemporaryDirectory(); // unzip all files in package into temp dir var fastZip = new FastZip(); fastZip.ExtractZip(packageFile, tempPath, FastZip.Overwrite.Always, overlite => true, null, null, true); var target = Path.Combine("Assets/UnityPackages", projectId); var destPath = Path.Combine(outputDir, target); if (Directory.Exists(destPath) == false) { Directory.CreateDirectory(destPath); File.WriteAllBytes(destPath + ".meta", Packer.GenerateMeta(destPath, destPath).Item2); } var projectFile = Path.Combine(tempPath, $"{projectId}.unitypackage.json"); if (File.Exists(projectFile) == false) { createProjectFile = true; } CopyFilesRecursively(new DirectoryInfo(tempPath), new DirectoryInfo(destPath)); Directory.Delete(tempPath, true); return(createProjectFile); }
public static string ExtractPackage( string packageId, string version, string tarketFrameworkMoniker, string outputDir) { var cacheRootPath = GetPackageCacheRoot(); var packagePath = Path.Combine(cacheRootPath, packageId + "." + version); if (Directory.Exists(packagePath) == false) { throw new InvalidOperationException("Cannot find the package: " + packagePath); } var libPath = Path.Combine(packagePath, "lib", tarketFrameworkMoniker); var target = $"Assets/UnityPackages/{packageId}"; var targetDir = Path.Combine(outputDir, target); if (Directory.Exists(libPath) == false) { if (TryExtractAnalyzersPackage(packageId, version, outputDir, out targetDir)) { return(targetDir); } throw new InvalidOperationException("Cannot find lib directory: " + libPath); } Directory.CreateDirectory(targetDir); File.WriteAllBytes(targetDir + ".meta", Packer.GenerateMeta(".", target).Item2); Packer.GenerateMeta(".", target); foreach (var file in Directory.GetFiles(libPath, "*.dll")) { var fileName = Path.GetFileName(file); var mdbFile = file + ".mdb"; if (File.Exists(mdbFile) == false) { MdbTool.ConvertPdbToMdb(file); } File.Copy(file, Path.Combine(targetDir, fileName), true); File.WriteAllBytes(Path.Combine(targetDir, fileName) + ".meta", Packer.GenerateMeta(file, target + "/" + fileName).Item2); if (File.Exists(mdbFile)) { File.Copy(mdbFile, Path.Combine(targetDir, fileName + ".mdb"), true); File.WriteAllBytes(Path.Combine(targetDir, fileName) + ".mdb.meta", Packer.GenerateMeta(file, target + "/" + fileName + ".mdb").Item2); } } return(targetDir); }
private static bool TryExtractAnalyzersPackage(string packageId, string version, string outputDir, out string targetDir) { var cacheRootPath = GetPackageCacheRoot(); var packagePath = Path.Combine(cacheRootPath, packageId + "." + version); var target = $"Assets/UnityPackages/Analyzers/{packageId}"; targetDir = Path.Combine(outputDir, target); if (Directory.Exists(packagePath) == false) { throw new InvalidOperationException("Cannot find the package: " + packagePath); } Directory.CreateDirectory(targetDir); File.WriteAllBytes(targetDir + ".meta", Packer.GenerateMeta(".", target).Item2); Packer.GenerateMeta(".", target); var analyzerPath = Path.Combine(packagePath, "analyzers", "dotnet", "cs"); if (!Directory.Exists(analyzerPath)) { return(false); } foreach (var file in Directory.GetFiles(analyzerPath, "*.dll")) { var fileName = Path.GetFileName(file); File.Copy(file, Path.Combine(targetDir, fileName), true); File.WriteAllBytes(Path.Combine(targetDir, fileName) + ".meta", Packer.GenerateAnalyzerMeta(file, target + "/" + fileName).Item2); } return(true); }
private static async Task ProcessStep(string projectId, Project.Dependency projectDependency, ProcessContext context) { var versionRange = new SemVer.Range(projectDependency.Version, true); // if already resolved dependency, skip it if (context.PackageMap.ContainsKey(projectId)) { if (versionRange.IsSatisfied(context.PackageMap[projectId]) == false) { throw new InvalidDataException($"Cannot meet version requirement: {projectId} {projectDependency.Version} (but {context.PackageMap[projectId]})"); } return; } // download package Console.WriteLine("Restore: " + projectId); var packageFile = ""; var packageVersion = new SemVer.Version(0, 0, 0); var nugetTargetFrameworkMoniker = string.Empty; var sourceName = projectId; var sourceExtention = ".unitypackage"; if (string.IsNullOrEmpty(projectDependency.SourceType) == false) { var source = projectDependency.SourceType.Split(':'); sourceName = source[0]; if (source.Length > 1) { sourceExtention = source[1]; if (sourceExtention.Contains(".") == false) { sourceExtention = sourceExtention.Insert(0, "."); } } } if (projectDependency.Source != "local" && string.IsNullOrEmpty(context.Options.LocalRepositoryDirectory) == false) { var packages = LocalPackage.GetPackages(context.Options.LocalRepositoryDirectory, projectId); var versionIndex = versionRange.GetSatisfiedVersionIndex(packages.Select(x => x.Item2).ToList()); if (versionIndex != -1) { packageFile = packages[versionIndex].Item1; packageVersion = packages[versionIndex].Item2; } } if (string.IsNullOrEmpty(packageFile) == false) { } else if (projectDependency.Source == "local") { var packages = LocalPackage.GetPackages(context.Options.LocalRepositoryDirectory ?? "", projectId); var versionIndex = versionRange.GetSatisfiedVersionIndex(packages.Select(x => x.Item2).ToList()); if (versionIndex == -1) { throw new InvalidOperationException("Cannot find package from local repository: " + projectId); } packageFile = packages[versionIndex].Item1; packageVersion = packages[versionIndex].Item2; } else if (projectDependency.Source.StartsWith("github:")) { var parts = projectDependency.Source.Substring(7).Split('/'); if (parts.Length != 2) { throw new InvalidDataException("Cannot determine github repo information from url: " + projectDependency.Source); } var r = await GithubPackage.DownloadPackageAsync(parts[0], parts[1], projectId, versionRange, sourceName, sourceExtention, context.Options.Token); packageFile = r.Item1; packageVersion = r.Item2; } else if (projectDependency.Source.StartsWith("nuget:")) { nugetTargetFrameworkMoniker = projectDependency.Source.Substring(6); var r = NugetPackage.DownloadPackage(projectId, projectDependency.Version); packageFile = r.Item1; packageVersion = r.Item2; } else { throw new InvalidOperationException("Cannot recognize source: " + projectDependency.Source); } context.PackageMap.Add(projectId, packageVersion); if (string.IsNullOrEmpty(nugetTargetFrameworkMoniker)) { var projectFile = Path.Combine(context.OutputDir, $"Assets/UnityPackages/{projectId}.unitypackage.json"); // apply package if (sourceExtention == ".zip") { var isCreateProjectFile = SourcePackage.ExtractUnityPackage(packageFile, context.OutputDir, projectId, projectDependency.IncludeExtra, projectDependency.IncludeMerged); if (isCreateProjectFile) { var p = new Project { Id = projectId, Version = packageVersion.ToString() }; p.Description = $"Source package"; p.Files = new List <JToken>() { JToken.FromObject($"Assets/UnityPackages/{projectId}") }; var jsonSettings = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, }; File.WriteAllText(projectFile, JsonConvert.SerializeObject(p, Formatting.Indented, jsonSettings)); File.WriteAllBytes(projectFile + ".meta", Packer.GenerateMeta(projectFile, projectFile).Item2); } } else { Extracter.ExtractUnityPackage(packageFile, context.OutputDir, projectId, projectDependency.IncludeExtra, projectDependency.IncludeMerged); } // deep into dependencies if (File.Exists(projectFile)) { var project = Project.Load(projectFile); if (project.MergedDependencies != null && projectDependency.IncludeMerged) { foreach (var d in project.MergedDependencies) { if (context.PackageMap.ContainsKey(d.Key) == false) { context.PackageMap[d.Key] = new SemVer.Version(d.Value.Version, true); } } } if (project.Dependencies != null) { foreach (var d in project.Dependencies) { context.DepQueue.Enqueue(d); } } } } else { // apply package NugetPackage.ExtractPackage(projectId, packageVersion.ToString(), nugetTargetFrameworkMoniker, context.OutputDir); // create proxy project file var outputDir = Path.Combine(context.OutputDir, $"Assets/UnityPackages/{projectId}"); var projectAssetPath = $"Assets/UnityPackages/{projectId}.unitypackage.json"; var projectFile = Path.Combine(context.OutputDir, projectAssetPath); var p = new Project { Id = projectId, Version = packageVersion.ToString() }; p.Description = $"Nuget package (TFM:{nugetTargetFrameworkMoniker})"; p.Files = Directory.GetFiles(outputDir, "*") .Where(f => Path.GetExtension(f).ToLower() != ".meta") .Select(f => JToken.FromObject(f.Substring(outputDir.Length + 1).Replace("\\", "/"))).ToList(); var jsonSettings = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, }; File.WriteAllText(projectFile, JsonConvert.SerializeObject(p, Formatting.Indented, jsonSettings)); File.WriteAllBytes(projectFile + ".meta", Packer.GenerateMeta(projectFile, projectAssetPath).Item2); } }