Exemplo n.º 1
0
        public void Generate(string jsiiFile, string tarballPath, ISymbolMap symbols = null)
        {
            jsiiFile = jsiiFile ?? throw new ArgumentNullException(nameof(jsiiFile));
            symbols  = symbols ?? new SymbolMap();

            string   assemblyJson = _fileSystem.File.ReadAllText(jsiiFile);
            Assembly assembly     = JsonConvert.DeserializeObject <Assembly>(assemblyJson);

            symbols.Add(assembly);

            LoadDependencies(assembly, Path.GetDirectoryName(jsiiFile));

            string packageOutputRoot = Path.Combine(_outputRoot, assembly.GetNativeName());

            if (_fileSystem.Directory.Exists(packageOutputRoot))
            {
                _fileSystem.Directory.Delete(packageOutputRoot, true);
            }
            _fileSystem.Directory.CreateDirectory(packageOutputRoot);

            // On Windows, the full destination path including the filename is required.
            _fileSystem.File.Copy(tarballPath, Path.Combine(packageOutputRoot, Path.GetFileName(tarballPath)));
            _fileSystem.File.Copy(jsiiFile, Path.Combine(packageOutputRoot, Path.GetFileName(jsiiFile)));

            Save(packageOutputRoot, symbols, assembly, new FileInfo(tarballPath).Name, new FileInfo(jsiiFile).Name);

            void LoadDependencies(DependencyRoot dependencyRoot, string packageDirectory)
            {
                if (dependencyRoot.Dependencies == null)
                {
                    return;
                }

                foreach (KeyValuePair <string, PackageVersion> entry in dependencyRoot.Dependencies)
                {
                    string   depRoot     = ResolvePackage(entry.Key, packageDirectory);
                    string   depFile     = Path.Combine(depRoot, ".jsii");
                    string   depJson     = _fileSystem.File.ReadAllText(depFile);
                    Assembly depAssembly = JsonConvert.DeserializeObject <Assembly>(depJson);
                    symbols.Add(depAssembly);
                    LoadDependencies(depAssembly, depRoot);
                }

                string ResolvePackage(string packageName, string directory)
                {
                    if (string.IsNullOrEmpty(directory))
                    {
                        throw new FileNotFoundException($"Could not resolve package named {packageName}");
                    }
                    string candidate = Path.Combine(directory, "node_modules", packageName);

                    if (_fileSystem.Directory.Exists(candidate))
                    {
                        return(candidate);
                    }
                    return(ResolvePackage(packageName, Path.GetDirectoryName(directory)));
                }
            }
        }