Skip to content

Commit 2747992

Browse files
committed
Tools: Remove --environment
Use ASPNETCORE_ENVIRONMENT instead. Part of #8164
1 parent a664dd1 commit 2747992

16 files changed

+52
-225
lines changed

src/EFCore.Design/Design/Internal/DatabaseOperations.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class DatabaseOperations
2424
public DatabaseOperations(
2525
[NotNull] IOperationReporter reporter,
2626
[NotNull] Assembly startupAssembly,
27-
[CanBeNull] string environment,
2827
[NotNull] string projectDir,
2928
[NotNull] string rootNamespace)
3029
{
@@ -37,7 +36,7 @@ public DatabaseOperations(
3736
_projectDir = projectDir;
3837
_rootNamespace = rootNamespace;
3938

40-
var startup = new StartupInvoker(reporter, startupAssembly, environment);
39+
var startup = new StartupInvoker(reporter, startupAssembly);
4140
_servicesBuilder = new DesignTimeServicesBuilder(startup);
4241
}
4342

src/EFCore.Design/Design/Internal/DbContextOperations.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class DbContextOperations
2222
private readonly IOperationReporter _reporter;
2323
private readonly Assembly _assembly;
2424
private readonly Assembly _startupAssembly;
25-
private readonly string _environment;
2625
private readonly IServiceProvider _runtimeServices;
2726

2827
// This obsolete constructor maintains compatibility with Scaffolding
@@ -32,15 +31,14 @@ public DbContextOperations(
3231
[NotNull] Assembly startupAssembly,
3332
[CanBeNull] string environment,
3433
[CanBeNull] string contentRootPath)
35-
: this(reporter, assembly, startupAssembly, environment)
34+
: this(reporter, assembly, startupAssembly)
3635
{
3736
}
3837

3938
public DbContextOperations(
4039
[NotNull] IOperationReporter reporter,
4140
[NotNull] Assembly assembly,
42-
[NotNull] Assembly startupAssembly,
43-
[CanBeNull] string environment)
41+
[NotNull] Assembly startupAssembly)
4442
{
4543
Check.NotNull(reporter, nameof(reporter));
4644
Check.NotNull(assembly, nameof(assembly));
@@ -49,9 +47,8 @@ public DbContextOperations(
4947
_reporter = reporter;
5048
_assembly = assembly;
5149
_startupAssembly = startupAssembly;
52-
_environment = environment;
5350

54-
var startup = new StartupInvoker(reporter, startupAssembly, environment);
51+
var startup = new StartupInvoker(reporter, startupAssembly);
5552
_runtimeServices = startup.ConfigureServices();
5653
}
5754

@@ -258,9 +255,8 @@ private DbContextFactoryOptions CreateFactoryOptions()
258255
{
259256
ApplicationBasePath = AppContext.BaseDirectory,
260257
ContentRootPath = Directory.GetCurrentDirectory(),
261-
EnvironmentName = !string.IsNullOrEmpty(_environment)
262-
? _environment
263-
: "Development"
258+
EnvironmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
259+
?? "Development"
264260
};
265261
}
266262
}

src/EFCore.Design/Design/Internal/MigrationsOperations.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public MigrationsOperations(
3030
[NotNull] IOperationReporter reporter,
3131
[NotNull] Assembly assembly,
3232
[NotNull] Assembly startupAssembly,
33-
[CanBeNull] string environment,
3433
[NotNull] string projectDir,
3534
[NotNull] string rootNamespace)
3635
{
@@ -47,10 +46,9 @@ public MigrationsOperations(
4746
_contextOperations = new DbContextOperations(
4847
reporter,
4948
assembly,
50-
startupAssembly,
51-
environment);
49+
startupAssembly);
5250

53-
var startup = new StartupInvoker(reporter, startupAssembly, environment);
51+
var startup = new StartupInvoker(reporter, startupAssembly);
5452
_servicesBuilder = new DesignTimeServicesBuilder(startup);
5553
}
5654

src/EFCore.Design/Design/Internal/StartupInvoker.cs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class StartupInvoker
2222
{
2323
private readonly Type _startupType;
2424
private readonly Type _designTimeServicesType;
25-
private readonly string _environment;
2625
private readonly string _startupAssemblyName;
2726
private readonly IOperationReporter _reporter;
2827

@@ -32,22 +31,16 @@ public class StartupInvoker
3231
/// </summary>
3332
public StartupInvoker(
3433
[NotNull] IOperationReporter reporter,
35-
[NotNull] Assembly startupAssembly,
36-
[CanBeNull] string environment)
34+
[NotNull] Assembly startupAssembly)
3735
{
3836
Check.NotNull(reporter, nameof(reporter));
3937
Check.NotNull(startupAssembly, nameof(startupAssembly));
4038

4139
_reporter = reporter;
4240

43-
_environment = !string.IsNullOrEmpty(environment)
44-
? environment
45-
: "Development";
46-
4741
_startupAssemblyName = startupAssembly.GetName().Name;
4842

4943
_startupType = startupAssembly.GetLoadableDefinedTypes().Where(t => typeof(IStartup).IsAssignableFrom(t.AsType()))
50-
.Concat(startupAssembly.GetLoadableDefinedTypes().Where(t => t.Name == "Startup" + _environment))
5144
.Concat(startupAssembly.GetLoadableDefinedTypes().Where(t => t.Name == "Startup"))
5245
.Select(t => t.AsType())
5346
.FirstOrDefault();
@@ -68,7 +61,7 @@ public virtual IServiceProvider ConfigureServices()
6861

6962
return Invoke(
7063
_startupType,
71-
new[] { "Configure" + _environment + "Services", "ConfigureServices" },
64+
"ConfigureServices",
7265
services) as IServiceProvider
7366
?? services.BuildServiceProvider();
7467
}
@@ -86,28 +79,18 @@ public virtual IServiceCollection ConfigureDesignTimeServices([NotNull] IService
8679
/// </summary>
8780
public virtual IServiceCollection ConfigureDesignTimeServices([CanBeNull] Type type, [NotNull] IServiceCollection services)
8881
{
89-
Invoke(type, new[] { "ConfigureDesignTimeServices" }, services);
82+
Invoke(type, "ConfigureDesignTimeServices", services);
9083
return services;
9184
}
9285

93-
private object Invoke(Type type, string[] methodNames, IServiceCollection services)
86+
private object Invoke(Type type, string methodName, IServiceCollection services)
9487
{
9588
if (type == null)
9689
{
9790
return null;
9891
}
9992

100-
MethodInfo method = null;
101-
// ReSharper disable once ForCanBeConvertedToForeach
102-
for (var i = 0; i < methodNames.Length; i++)
103-
{
104-
method = type.GetTypeInfo().GetDeclaredMethod(methodNames[i]);
105-
if (method != null)
106-
{
107-
break;
108-
}
109-
}
110-
93+
var method = type.GetTypeInfo().GetDeclaredMethod(methodName);
11194
if (method == null)
11295
{
11396
return null;
@@ -156,7 +139,7 @@ protected virtual IServiceCollection ConfigureHostServices([NotNull] IServiceCol
156139
new HostingEnvironment
157140
{
158141
ContentRootPath = Directory.GetCurrentDirectory(),
159-
EnvironmentName = _environment,
142+
EnvironmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development",
160143
ApplicationName = _startupAssemblyName
161144
});
162145

src/EFCore.Design/Design/OperationExecutor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public OperationExecutor([NotNull] object reportHandler, [NotNull] IDictionary a
3434

3535
var targetName = (string)args["targetName"];
3636
var startupTargetName = (string)args["startupTargetName"];
37-
var environment = (string)args["environment"];
3837
_projectDir = (string)args["projectDir"];
3938
var rootNamespace = (string)args["rootNamespace"];
4039

@@ -59,21 +58,18 @@ public OperationExecutor([NotNull] object reportHandler, [NotNull] IDictionary a
5958
() => new DbContextOperations(
6059
reporter,
6160
assembly.Value,
62-
startupAssembly.Value,
63-
environment));
61+
startupAssembly.Value));
6462
_databaseOperations = new LazyRef<DatabaseOperations>(
6563
() => new DatabaseOperations(
6664
reporter,
6765
startupAssembly.Value,
68-
environment,
6966
_projectDir,
7067
rootNamespace));
7168
_migrationsOperations = new LazyRef<MigrationsOperations>(
7269
() => new MigrationsOperations(
7370
reporter,
7471
assembly.Value,
7572
startupAssembly.Value,
76-
environment,
7773
_projectDir,
7874
rootNamespace));
7975
}

src/dotnet-ef/Commands/ProjectCommandBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.EntityFrameworkCore.Tools.Commands
77
{
8-
internal class ProjectCommandBase : EnvironmentCommandBase
8+
internal class ProjectCommandBase : EFCommandBase
99
{
1010
public override void Configure(CommandLineApplication command)
1111
{

src/dotnet-ef/dotnet-ef.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<Compile Include="..\ef\Commands\DatabaseCommand.cs" />
1818
<Compile Include="..\ef\Commands\DbContextCommand.cs" />
1919
<Compile Include="..\ef\Commands\EFCommandBase.cs" />
20-
<Compile Include="..\ef\Commands\EnvironmentCommandBase.cs" />
2120
<Compile Include="..\ef\Commands\HelpCommandBase.cs" />
2221
<Compile Include="..\ef\Commands\MigrationsCommand.cs" />
2322
<Compile Include="..\ef\Commands\RootCommand.cs" />

src/ef/AppDomainOperationExecutor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ public AppDomainOperationExecutor(
2323
string startupAssembly,
2424
string projectDir,
2525
string dataDirectory,
26-
string rootNamespace,
27-
string environment)
28-
: base(assembly, startupAssembly, projectDir, dataDirectory, rootNamespace, environment)
26+
string rootNamespace)
27+
: base(assembly, startupAssembly, projectDir, dataDirectory, rootNamespace)
2928
{
3029
var info = new AppDomainSetup { ApplicationBase = AppBasePath };
3130

@@ -64,8 +63,7 @@ public AppDomainOperationExecutor(
6463
{ "targetName", AssemblyFileName },
6564
{ "startupTargetName", StartupAssemblyFileName },
6665
{ "projectDir", ProjectDirectory },
67-
{ "rootNamespace", RootNamespace },
68-
{ "environment", EnvironmentName }
66+
{ "rootNamespace", RootNamespace }
6967
}
7068
},
7169
null,

src/ef/Commands/EnvironmentCommandBase.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/ef/Commands/ProjectCommandBase.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Microsoft.EntityFrameworkCore.Tools.Commands
88
{
9-
internal abstract class ProjectCommandBase : EnvironmentCommandBase
9+
internal abstract class ProjectCommandBase : EFCommandBase
1010
{
1111
private CommandOption _assembly;
1212
private CommandOption _startupAssembly;
@@ -48,8 +48,7 @@ protected IOperationExecutor CreateExecutor()
4848
_startupAssembly.Value(),
4949
_projectDir.Value(),
5050
_dataDir.Value(),
51-
_rootNamespace.Value(),
52-
Environment.Value());
51+
_rootNamespace.Value());
5352
}
5453
#elif NETCOREAPP1_0
5554
#else
@@ -60,8 +59,7 @@ protected IOperationExecutor CreateExecutor()
6059
_startupAssembly.Value(),
6160
_projectDir.Value(),
6261
_dataDir.Value(),
63-
_rootNamespace.Value(),
64-
Environment.Value());
62+
_rootNamespace.Value());
6563
}
6664
}
6765
}

src/ef/OperationExecutorBase.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Collections;
65
using System.Collections.Generic;
76
using System.IO;
@@ -21,15 +20,13 @@ internal abstract class OperationExecutorBase : IOperationExecutor
2120
protected string StartupAssemblyFileName { get; set; }
2221
protected string ProjectDirectory { get; }
2322
protected string RootNamespace { get; }
24-
protected string EnvironmentName { get; }
2523

2624
protected OperationExecutorBase(
2725
string assembly,
2826
string startupAssembly,
2927
string projectDir,
3028
string dataDirectory,
31-
string rootNamespace,
32-
string environment)
29+
string rootNamespace)
3330
{
3431
AssemblyFileName = Path.GetFileNameWithoutExtension(assembly);
3532
StartupAssemblyFileName = startupAssembly == null
@@ -44,7 +41,6 @@ protected OperationExecutorBase(
4441

4542
RootNamespace = rootNamespace ?? AssemblyFileName;
4643
ProjectDirectory = projectDir ?? Directory.GetCurrentDirectory();
47-
EnvironmentName = environment;
4844

4945
Reporter.WriteVerbose(Resources.UsingAssembly(AssemblyFileName));
5046
Reporter.WriteVerbose(Resources.UsingStartupAssembly(StartupAssemblyFileName));

src/ef/ReflectionOperationExecutor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ public ReflectionOperationExecutor(
2525
string startupAssembly,
2626
string projectDir,
2727
string dataDirectory,
28-
string rootNamespace,
29-
string environment)
30-
: base(assembly, startupAssembly, projectDir, dataDirectory, rootNamespace, environment)
28+
string rootNamespace)
29+
: base(assembly, startupAssembly, projectDir, dataDirectory, rootNamespace)
3130
{
3231
#if NET46
3332
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
@@ -54,8 +53,7 @@ public ReflectionOperationExecutor(
5453
{ "targetName", AssemblyFileName },
5554
{ "startupTargetName", StartupAssemblyFileName },
5655
{ "projectDir", ProjectDirectory },
57-
{ "rootNamespace", RootNamespace },
58-
{ "environment", EnvironmentName }
56+
{ "rootNamespace", RootNamespace }
5957
});
6058

6159
_resultHandlerType = _commandsAssembly.GetType(ResultHandlerTypeName, throwOnError: true, ignoreCase: false);

test/EFCore.Design.Tests/Design/Internal/DbContextOperationsTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public void CreateContext_get_service()
1717
var operations = new DbContextOperations(
1818
new TestOperationReporter(),
1919
assembly,
20-
assembly,
21-
"Environment1");
20+
assembly);
2221

2322
operations.CreateContext(typeof(TestContext).FullName);
2423
}

0 commit comments

Comments
 (0)