public GeneratorResult Generate(MetadataGeneratorOptions options)
        {
            var generatorResult = new GeneratorResult();
            var targetFramework = new FrameworkName(options.TargetFramework);
            var package         = _packageManager.InstallPackage(options.Package, options.PackageVersion, targetFramework);

            if (package == null)
            {
                Logger.Error("Unable to find package {0} {1}", options.Package, options.PackageVersion ?? string.Empty);
                return(null);
            }

            if (!string.IsNullOrWhiteSpace(options.OutputFolder) && !_fileSystem.DirectoryExists(options.OutputFolder))
            {
                _fileSystem.CreateDirectory(options.OutputFolder);
            }

            var packages   = _dependencyResolver.GetDependenciesAndSelf(package, targetFramework).ToList();
            var assemblies = _packageAssemblyResolver.ResolveAssemblies(package, targetFramework);

            foreach (var assembly in assemblies)
            {
                var compilationUnit = _cakeSourceGenerator.Generate(assembly);
                var rewrittenNode   = _cakeSyntaxRewriterService.Rewrite(compilationUnit, assembly);

                var emitedAssemblyName = $"{assembly.GetName().Name}.{MetadataClassSuffix}";
                var compilation        = _compilationProvider.Get(
                    emitedAssemblyName,
                    new[] { SyntaxFactory.ParseSyntaxTree(rewrittenNode.NormalizeWhitespace().ToFullString()) },
                    _metadataReferenceLoader.CreateFromPackages(packages, targetFramework),
                    new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

                var result = _compiler.Compile(compilation, Path.Combine(options.OutputFolder ?? string.Empty, $"{emitedAssemblyName}.dll"));
                generatorResult.EmitedAssemblies.Add(result);
                generatorResult.SourceAssemblies.Add(assembly);
            }

            return(generatorResult);
        }
예제 #2
0
        public IList <PortableExecutableReference> CreateFromPackages(IList <IPackage> packages, FrameworkName targetFramework)
        {
            packages = packages.ToList();

            if (!_packagesIntegrityValidator.IsValid(packages))
            {
                var cakeCore = _packageManager.InstallPackage(Constants.Packages.CakeCore, null, targetFramework);
                packages.Add(cakeCore);
            }

            var packageAssemblies = packages.SelectMany(package => _packageAssemblyResolver.ResolveAssemblies(package, targetFramework)).ToList();

            var referencedAssemblyReferences =
                packageAssemblies
                .SelectMany(packageFile => _assemblyLoader.LoadReferencedAssemblies(packageFile))
                .Select(assembly => CreateFromFile(assembly.Location))
                .ToList();

            var physicalPackagesReferences = packageAssemblies.Select(val => CreateFromFile(val.Location));

            return(referencedAssemblyReferences.Union(physicalPackagesReferences).DistinctBy(reference => Path.GetFileName(reference.FilePath)).ToList());
        }