Beispiel #1
0
        protected override async Task <int> ExecuteAsync(CancellationToken cancellationToken)
        {
            string configFilePath;

            if (ConfigSources.HasFlag(ConfigSources.ConfigFile))
            {
                configFilePath = ConfigFilePath ?? GetDefaultConfigFilePath(ProjectDirPath);
            }
            else
            {
                configFilePath = null;
            }

            string compilationBasePath = Path.GetDirectoryName(AssemblyPath);

            IEnumerable <string> assemblyFilePaths;

            if (ConfigSources.HasFlag(ConfigSources.AppAssembly) || ConfigSources.HasFlag(ConfigSources.OutputAssemblies))
            {
                if (ConfigSources.HasFlag(ConfigSources.OutputAssemblies))
                {
                    assemblyFilePaths = Directory.EnumerateFiles(compilationBasePath, "*.dll", SearchOption.TopDirectoryOnly)
                                        .Where(path => !Path.GetFileName(path).StartsWith(BundleBuilderProxy.BundlingAssemblyName));
                }
                else
                {
                    assemblyFilePaths = Enumerable.Empty <string>();
                }

                if (ConfigSources.HasFlag(ConfigSources.AppAssembly))
                {
                    assemblyFilePaths = assemblyFilePaths.Prepend(AssemblyPath);
                }

                assemblyFilePaths = assemblyFilePaths.Distinct();
            }
            else
            {
                assemblyFilePaths = Enumerable.Empty <string>();
            }

            var bundleBuilderProxy = new BundleBuilderProxy(AssemblyLoadContext.Default, ReporterAdapter.Default);

            var settings = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase)
            {
                ["ProjectDirPath"]      = ProjectDirPath,
                ["CompilationBasePath"] = compilationBasePath,
                ["Mode"]   = BundlingMode.ToString(),
                ["Logger"] = new Action <int, string>(Log)
            };

            if (configFilePath != null)
            {
                settings["ConfigFilePath"] = Path.GetFullPath(configFilePath);
            }

            await bundleBuilderProxy.ProcessConfigurationsAsync(assemblyFilePaths, settings, cancellationToken);

            return(0);
        }
Beispiel #2
0
        protected override async Task <int> ExecuteAsync(CancellationToken cancellationToken)
        {
            var     projectPath = ProjectPath ?? Directory.GetCurrentDirectory();
            Project project;

            // project metadata needs to be determined
            if (!Project.IsMetadata(projectPath))
            {
                var projectFile = ResolveProjectFile(projectPath);

                Reporter.WriteVerbose($"Using project '{projectFile}'.");

                project = await Project.FromFileAsync(projectFile, Framework, Configuration, Runtime, cancellationToken);

                if (!NoBuild && (ConfigSources.HasFlag(ConfigSources.AppAssembly) || ConfigSources.HasFlag(ConfigSources.OutputAssemblies)))
                {
                    await project.BuildAsync(cancellationToken);
                }
            }
            // project metadata is available as application is called during build process
            else
            {
                project = Project.FromMetadata(projectPath, Framework, Configuration, Runtime);
            }

            var toolsPath = typeof(Program).GetTypeInfo().Assembly.Location;

            var targetDir  = Path.GetFullPath(Path.Combine(project.ProjectDir, project.OutputPath));
            var targetPath = Path.Combine(targetDir, project.TargetFileName);

            var depsFile          = Path.Combine(targetDir, project.AssemblyName + ".deps.json");
            var runtimeConfig     = Path.Combine(targetDir, project.AssemblyName + ".runtimeconfig.json");
            var projectAssetsFile = project.ProjectAssetsFile;

            var args = new List <string>()
            {
                "exec"
            };

            args.Add("--depsfile");
            args.Add(depsFile);

            if (!string.IsNullOrEmpty(projectAssetsFile))
            {
                using (var reader = JsonDocument.Parse(File.OpenRead(projectAssetsFile)))
                {
                    var projectAssets  = reader.RootElement;
                    var packageFolders = projectAssets.GetProperty("packageFolders").EnumerateObject().Select(p => p.Name);

                    foreach (var packageFolder in packageFolders)
                    {
                        args.Add("--additionalprobingpath");
                        args.Add(packageFolder.TrimEnd(Path.DirectorySeparatorChar));
                    }
                }
            }

            if (File.Exists(runtimeConfig))
            {
                args.Add("--runtimeconfig");
                args.Add(runtimeConfig);
            }
            else if (project.RuntimeFrameworkVersion.Length != 0)
            {
                args.Add("--fx-version");
                args.Add(project.RuntimeFrameworkVersion);
            }

            args.Add(toolsPath);

            args.AddRange(_originalArgs);

            args.Add("--assembly");
            args.Add(targetPath);
            args.Add("--project-dir");
            args.Add(project.ProjectDir);

            var processSpec = new ProcessSpec
            {
                Executable           = "dotnet",
                Arguments            = args,
                EnvironmentVariables = { [Program.NestedRunEnvironmentVariableName] = bool.TrueString },
                WorkingDirectory     = Directory.GetCurrentDirectory()
            };

            return(await ProcessRunner.Default.RunAsync(processSpec, cancellationToken));
        }