示例#1
0
        public async Task ProjectDependenciesTestAsync()
        {
            var configuration    = "Debug";
            var objDirectoryName = "obj";

            var packageReferences = new PackageReference[]
            {
                new ("StackExchange.Redis", "2.2.4"),
            };

            var project      = new Project(packageReferences, LibraryReferences: null, GenerateSwaggerDoc: false, AddSwaggerUI: false);
            var bootstrapper = new BasicBootstrapper("TestProject", addSwagger: false, addSwaggerUI: false);
            var tempPath     = Path.GetTempPath();
            var nuGetClient  = new NuGetClient(project, configuration, tempPath);

            var packageAssemblies = await nuGetClient
                                    .RestoreProjectDependenciesAsync()
                                    .ConfigureAwait(false);

            var packagePath = Path.Combine(tempPath, objDirectoryName, configuration);
            var packageAssembliesFileName = Path.Combine(packagePath, "packageAssemblies.json");

            Assert.True(File.Exists(packageAssembliesFileName), $"{packageAssembliesFileName} not found");

            var files = packageAssemblies.SelectMany(pa => pa.PackageFiles);

            foreach (var file in files)
            {
                var filePath = Path.Combine(tempPath, file);
                Assert.True(File.Exists(filePath), $"{filePath} not found");
            }
        }
示例#2
0
        public static EmitResult BuildProject(string?projectPath, string?outputPath = null, string?configuration = null, Action <string>?logger = null, bool dumpGeneratedFiles = false)
        {
            var projectFolder = Path.GetFullPath(string.IsNullOrEmpty(projectPath) ?
                                                 Directory.GetCurrentDirectory() :
                                                 projectPath.TrimEnd('\\', '/').Trim());

            var buildConfiguration = string.IsNullOrEmpty(configuration) ? "Debug" : configuration;

            var executableFolder = Path.GetFullPath(string.IsNullOrEmpty(outputPath) ?
                                                    Path.Combine(projectFolder, "bin", buildConfiguration) :
                                                    outputPath);

            var projectFile = SourceFileDiscovery.GetProjectFileInDirectory(projectFolder);
            var projectName = Path.GetFileNameWithoutExtension(projectFile);

            if (string.IsNullOrEmpty(projectFile))
            {
                throw new Exception("No project file found");
            }

            var project = ProjectFileHandler.ReadProjectFile(projectFile);

            if (project is null)
            {
                throw new Exception("Unable to read project file");
            }

            Directory.CreateDirectory(executableFolder);

            var compilation = new ExpressNetCompilation(projectName, projectFolder, executableFolder, buildConfiguration)
                              .SetTargetFrameworks(TargetFrameworks.NetCore50, TargetFrameworks.AspNetCore50, TargetFrameworks.ExpressNet)
                              .SetBootstrapper(new BasicBootstrapper(projectName, project.GenerateSwaggerDoc, project.AddSwaggerUI));

            var packageAssemblies = Enumerable.Empty <PackageAssembly>();

            if (project.PackageReferences != null && project.PackageReferences.Length > 0)
            {
                logger?.Invoke($"Restore NuGet Packages for {projectName}");

                var nugetClient = new NuGetClient(project, buildConfiguration, projectFolder);

                packageAssemblies = nugetClient
                                    .RestoreProjectDependenciesAsync()
                                    .GetAwaiter()
                                    .GetResult();

                compilation = compilation.SetPackageAssemblies(packageAssemblies.ToArray());
            }

            logger?.Invoke($"Build starting for {projectName}");

            var sourceFiles = SourceFileDiscovery.GetSourceFilesInDirectory(projectFolder);

            if (sourceFiles.Length == 0)
            {
                throw new Exception("Unable locate any source files");
            }

            compilation = compilation.SetSyntaxTrees(ParseSourceFiles(sourceFiles, logger));

            var csharpSourceFiles = SourceFileDiscovery.GetCSharpSourceFilesInDirectory(projectFolder);

            if (csharpSourceFiles.Length > 0)
            {
                compilation = compilation.SetCSharpSyntaxTrees(ParseCSharpSourceFiles(csharpSourceFiles, logger));
            }

            logger?.Invoke($"Building Runtime Config");

            RuntimeConfigBuilder.BuildRuntimeConfig(projectName, executableFolder, TargetFrameworks.ExpressNet);

            logger?.Invoke($"Emiting IL");

            var result = compilation.Emit();

            if (result.Success)
            {
                logger?.Invoke("Emiting IL complete");

                logger?.Invoke("Copying express framework assemblies");
                ExpressDependencies.CopyFrameworkAssemblies(project, executableFolder);

                if (packageAssemblies.Any())
                {
                    logger?.Invoke("Copying package assemblies");
                    ExpressDependencies.CopyPackageAssemblies(packageAssemblies, projectFolder, executableFolder);
                }
            }

            if (dumpGeneratedFiles)
            {
                logger?.Invoke("Saving generated C# files");

                SaveGeneratedCSharpFiles(outputPath, projectFolder, result.GeneratedSyntaxTrees);
            }

            return(result);
        }