Example #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);
        }
Example #2
0
        private async Task <int> RunCoreAsync(CommandLineOptions options)
        {
            string projectFilePath;

            try { projectFilePath = MsBuildProjectFinder.FindMsBuildProject(_workingDir, options.Project); }
            catch (FileNotFoundException ex)
            {
                _reporter.Error(ex.Message);
                return(1);
            }

            var projectDirPath = Path.GetDirectoryName(projectFilePath);

            string configFilePath;

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

            string compilationBasePath;
            IEnumerable <string> assemblyFilePaths;

            if (options.ConfigSources.HasFlag(ConfigSources.AppAssembly) || options.ConfigSources.HasFlag(ConfigSources.OutputAssemblies))
            {
                var targetFilePath = options.BuildTargetPath;
                if (targetFilePath == null)
                {
                    _reporter.Output($"Building project {projectFilePath}...");

                    targetFilePath = await new MsBuildEnsureBuildTarget(_reporter, projectFilePath).ExecuteAsync(options.BuildConfiguration, _cts.Token);

                    // we need the project to be built if configuration is specified by code
                    if (targetFilePath == null)
                    {
                        return(1);
                    }
                }

                compilationBasePath = Path.GetDirectoryName(targetFilePath);

                if (options.ConfigSources.HasFlag(ConfigSources.OutputAssemblies))
                {
                    assemblyFilePaths = Directory.EnumerateFiles(compilationBasePath, "*.dll", SearchOption.TopDirectoryOnly);
                }
                else
                {
                    assemblyFilePaths = Enumerable.Empty <string>();
                }

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

                assemblyFilePaths = assemblyFilePaths.Distinct();
            }
            else
            {
                if (options.BuildTargetPath == null)
                {
                    // TODO: prefer release build?
                    compilationBasePath = Directory.EnumerateFiles(projectDirPath, BundleBuilderProxy.BundlingAssemblyName + ".dll", SearchOption.AllDirectories).FirstOrDefault();
                    if (compilationBasePath != null)
                    {
                        compilationBasePath = Path.GetDirectoryName(compilationBasePath);
                    }
                }
                else
                {
                    compilationBasePath = Path.GetDirectoryName(options.BuildTargetPath);
                }

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

            // we load the application into a separate AssemblyLoadContext (to avoid assembly version mismatches),
            // then we pass the discovered bundling configurations to the design-time bundle builder implementation residing in the main assembly

            var assemblyLoader = new AssemblyLoader(compilationBasePath);

            var bundleBuilderProxy = new BundleBuilderProxy(assemblyLoader, _reporter);

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

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

            await bundleBuilderProxy.ProcessConfigurationsAsync(assemblyFilePaths, settings, _cts.Token);

            return(0);
        }