Пример #1
0
        public OperationExecutor(
            [NotNull] CommonOptions options,
            [CanBeNull] string environment)
        {
            var projectFile = Path.Combine(Directory.GetCurrentDirectory(), Project.FileName);
            var project     = ProjectReader.GetProject(projectFile);

            var projectConfiguration = options.Configuration ?? Constants.DefaultConfiguration;
            var projectFramework     = options.Framework;

            var projectContext = ProjectContext.Create(project.ProjectFilePath,
                                                       projectFramework,
                                                       RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers());

            var runtimeOutputPath = projectContext.GetOutputPaths(projectConfiguration)?.RuntimeOutputPath;

            if (!string.IsNullOrEmpty(runtimeOutputPath))
            {
                Reporter.Verbose.WriteLine(
                    ToolsCliStrings.LogDataDirectory(runtimeOutputPath));
                Environment.SetEnvironmentVariable(DataDirEnvName, runtimeOutputPath);
#if NET451
                AppDomain.CurrentDomain.SetData("DataDirectory", runtimeOutputPath);
#endif
            }

            var assemblyName  = project.GetCompilerOptions(projectFramework, projectConfiguration).OutputName;
            var projectDir    = project.ProjectDirectory;
            var rootNamespace = project.Name;

            var assemblyLoader  = new AssemblyLoader(Assembly.Load);
            var projectAssembly = assemblyLoader.Load(assemblyName);

            _contextOperations = new LazyRef <DbContextOperations>(
                () => new DbContextOperations(
                    new LoggerProvider(name => new ConsoleCommandLogger(name)),
                    projectAssembly,
                    projectAssembly,
                    environment,
                    projectDir));
            _databaseOperations = new LazyRef <DatabaseOperations>(
                () => new DatabaseOperations(
                    new LoggerProvider(name => new ConsoleCommandLogger(name)),
                    assemblyLoader,
                    projectAssembly,
                    environment,
                    projectDir,
                    projectDir,
                    rootNamespace));
            _migrationsOperations = new LazyRef <MigrationsOperations>(
                () => new MigrationsOperations(
                    new LoggerProvider(name => new ConsoleCommandLogger(name)),
                    projectAssembly,
                    assemblyLoader,
                    projectAssembly,
                    environment,
                    projectDir,
                    projectDir,
                    rootNamespace));
        }
Пример #2
0
        private ProjectContext GetCompatibleStartupProjectContext(string startupProjectPath, NuGetFramework projectFramework)
        {
            var startupProject = ProjectReader.GetProject(startupProjectPath);
            var frameworks     = startupProject.GetTargetFrameworks()
                                 .Select(f => f.FrameworkName);

            var startupFramework = frameworks.FirstOrDefault(f => f.Equals(projectFramework));

            if (startupFramework == null)
            {
                if (projectFramework.IsDesktop())
                {
                    startupFramework = frameworks.FirstOrDefault(f => f.IsDesktop())
                                       ?? NuGetFrameworkUtility.GetNearest(
                        frameworks,
                        FrameworkConstants.CommonFrameworks.NetStandard15,
                        f => f);
                }
                else
                {
                    startupFramework = NuGetFrameworkUtility.GetNearest(
                        frameworks,
                        FrameworkConstants.CommonFrameworks.NetCoreApp10,
                        f => f)
                                       // TODO remove fallback to dnxcore50
                                       ?? NuGetFrameworkUtility.GetNearest(
                        frameworks,
                        FrameworkConstants.CommonFrameworks.DnxCore50,
                        f => f);
                }
            }

            if (startupFramework == null)
            {
                throw new OperationException(
                          ToolsCliStrings.IncompatibleStartupProject(startupProject.Name, projectFramework.GetShortFolderName()));
            }

            Reporter.Verbose.WriteLine(
                ToolsCliStrings.LogUsingFramework(startupFramework.GetShortFolderName(), startupProject.Name).Bold().Black());

            return(new ProjectContextBuilder()
                   .WithProject(startupProject)
                   .WithTargetFramework(startupFramework)
                   .WithRuntimeIdentifiers(PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers())
                   .Build());
        }
Пример #3
0
        public OperationExecutor(
            [NotNull] CommonOptions options,
            [CanBeNull] string startupProjectPath,
            [CanBeNull] string environment)
        {
            var projectFile = Path.Combine(Directory.GetCurrentDirectory(), Project.FileName);
            var project     = ProjectReader.GetProject(projectFile);

            startupProjectPath = startupProjectPath ?? projectFile;

            var projectConfiguration = options.Configuration ?? Constants.DefaultConfiguration;
            var projectFramework     = options.Framework;

            var startupConfiguration  = options.Configuration ?? Constants.DefaultConfiguration;
            var startupProjectContext = GetCompatibleStartupProjectContext(startupProjectPath, projectFramework);
            var startupFramework      = startupProjectContext.TargetFramework;

            var externalStartup = project.ProjectFilePath != startupProjectContext.ProjectFile.ProjectFilePath;

            if (externalStartup && !options.NoBuild)
            {
                Reporter.Verbose.WriteLine(ToolsCliStrings.LogBuildFailed.Bold().Black());

                var buildExitCode = BuildCommandFactory.Create(startupProjectPath,
                                                               startupConfiguration,
                                                               startupFramework,
                                                               options.BuildBasePath,
                                                               output: null)
                                    .ForwardStdOut()
                                    .ForwardStdErr()
                                    .Execute()
                                    .ExitCode;

                if (buildExitCode != 0)
                {
                    throw new OperationException(ToolsCliStrings.LogBuildFailed);
                }

                Reporter.Verbose.WriteLine(ToolsCliStrings.LogBuildSucceeded.Bold().Black());
            }

            var runtimeOutputPath = startupProjectContext.GetOutputPaths(startupConfiguration)?.RuntimeOutputPath;

            if (!string.IsNullOrEmpty(runtimeOutputPath))
            {
                Reporter.Verbose.WriteLine(
                    ToolsCliStrings.LogDataDirectory(runtimeOutputPath));
                Environment.SetEnvironmentVariable(DataDirEnvName, runtimeOutputPath);
#if NET451
                AppDomain.CurrentDomain.SetData("DataDirectory", runtimeOutputPath);
#endif
            }

            var startupAssemblyName = startupProjectContext.ProjectFile.GetCompilerOptions(startupFramework, startupConfiguration).OutputName;
            var assemblyName        = project.GetCompilerOptions(projectFramework, projectConfiguration).OutputName;
            var projectDir          = project.ProjectDirectory;
            var startupProjectDir   = startupProjectContext.ProjectFile.ProjectDirectory;
            var rootNamespace       = project.Name;

            var projectAssembly = Assembly.Load(new AssemblyName {
                Name = assemblyName
            });
#if NET451
            // TODO use app domains
            var startupAssemblyLoader = new AssemblyLoader(Assembly.Load);
#else
            AssemblyLoader startupAssemblyLoader;
            if (externalStartup)
            {
                var assemblyLoadContext = startupProjectContext.CreateLoadContext(
                    PlatformServices.Default.Runtime.GetRuntimeIdentifier(),
                    Constants.DefaultConfiguration);
                startupAssemblyLoader = new AssemblyLoader(assemblyLoadContext.LoadFromAssemblyName);
            }
            else
            {
                startupAssemblyLoader = new AssemblyLoader(Assembly.Load);
            }
#endif
            var startupAssembly = startupAssemblyLoader.Load(startupAssemblyName);

            _contextOperations = new LazyRef <DbContextOperations>(
                () => new DbContextOperations(
                    new LoggerProvider(name => new ConsoleCommandLogger(name)),
                    projectAssembly,
                    startupAssembly,
                    environment,
                    startupProjectDir));
            _databaseOperations = new LazyRef <DatabaseOperations>(
                () => new DatabaseOperations(
                    new LoggerProvider(name => new ConsoleCommandLogger(name)),
                    startupAssemblyLoader,
                    startupAssembly,
                    environment,
                    projectDir,
                    startupProjectDir,
                    rootNamespace));
            _migrationsOperations = new LazyRef <MigrationsOperations>(
                () => new MigrationsOperations(
                    new LoggerProvider(name => new ConsoleCommandLogger(name)),
                    projectAssembly,
                    startupAssemblyLoader,
                    startupAssembly,
                    environment,
                    projectDir,
                    startupProjectDir,
                    rootNamespace));
        }