Skip to content

Commit

Permalink
removing package dependencies check (#1660)
Browse files Browse the repository at this point in the history
* removing package checks for VS scenarios

* cleanup
  • Loading branch information
deepchoudhery committed Oct 5, 2021
1 parent 7dc6e49 commit 50e3d30
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ private DbContext TryCreateContextUsingAppCode(Type dbContextType, Type startupT

private void ValidateEFSqlServerDependency()
{
if (_projectContext.GetPackage(EFSqlServerPackageName) == null)
if (_projectContext.GetPackage(EFSqlServerPackageName) == null && CalledFromCommandline)
{
throw new InvalidOperationException(MessageStrings.EFSqlServerPackageNotAvailable);
}
Expand Down Expand Up @@ -589,5 +589,8 @@ private string GetPathForNewContext(string contextShortTypeName, string areaName

return outputPath;
}

//IFileSystem is DefaultFileSystem in commandline scenarios and SimulationModeFileSystem in VS scenarios.
private bool CalledFromCommandline => !(_fileSystem is SimulationModeFileSystem);
}
}
4 changes: 2 additions & 2 deletions src/Scaffolding/VS.Web.CG.Mvc/Common/EFValidationUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ internal static class EFValidationUtil
const string EfDesignPackageName = "Microsoft.EntityFrameworkCore.Design";
const string SqlServerPackageName = "Microsoft.EntityFrameworkCore.SqlServer";

internal static void ValidateEFDependencies(IEnumerable<DependencyDescription> dependencies, bool useSqlite)
internal static void ValidateEFDependencies(IEnumerable<DependencyDescription> dependencies, bool useSqlite, bool calledFromCommandline)
{
var isEFDesignPackagePresent = dependencies
.Any(package => package.Name.Equals(EfDesignPackageName, StringComparison.OrdinalIgnoreCase));

if (!isEFDesignPackagePresent)
if (!isEFDesignPackagePresent && calledFromCommandline)
{
throw new InvalidOperationException(
string.Format(MessageStrings.InstallEfPackages, $"{EfDesignPackageName}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public ControllerWithContextGenerator(
IEntityFrameworkService entityFrameworkService,
ICodeGeneratorActionsService codeGeneratorActionsService,
IServiceProvider serviceProvider,
ILogger logger)
ILogger logger,
IFileSystem fileSystem)
: base(projectContext, applicationInfo, codeGeneratorActionsService, serviceProvider, logger)
{

Expand All @@ -49,7 +50,7 @@ public ControllerWithContextGenerator(
{
throw new ArgumentNullException(nameof(entityFrameworkService));
}

FileSystem = fileSystem;
ModelTypesLocator = modelTypesLocator;
EntityFrameworkService = entityFrameworkService;
}
Expand All @@ -58,7 +59,7 @@ public override async Task Generate(CommandLineGeneratorModel controllerGenerato
{
Contract.Assert(!String.IsNullOrEmpty(controllerGeneratorModel.ModelClass));
ValidateNameSpaceName(controllerGeneratorModel);
EFValidationUtil.ValidateEFDependencies(ProjectContext.PackageDependencies, controllerGeneratorModel.UseSqlite);
EFValidationUtil.ValidateEFDependencies(ProjectContext.PackageDependencies, controllerGeneratorModel.UseSqlite, CalledFromCommandline);
string outputPath = ValidateAndGetOutputPath(controllerGeneratorModel);
_areaName = GetAreaName(ApplicationInfo.ApplicationBasePath, outputPath);

Expand Down Expand Up @@ -138,15 +139,11 @@ protected override string GetTemplateName(CommandLineGeneratorModel generatorMod
return generatorModel.IsRestController ? Constants.ApiControllerWithContextTemplate : Constants.MvcControllerWithContextTemplate;
}

protected IModelTypesLocator ModelTypesLocator
{
get;
private set;
}
protected IEntityFrameworkService EntityFrameworkService
{
get;
private set;
}
//IFileSystem is DefaultFileSystem in commandline scenarios and SimulationModeFileSystem in VS scenarios.
private bool CalledFromCommandline => !(FileSystem is SimulationModeFileSystem);

protected IFileSystem FileSystem { get; private set; }
protected IModelTypesLocator ModelTypesLocator { get; private set; }
protected IEntityFrameworkService EntityFrameworkService { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ private void ValidateCommandLine(IdentityGeneratorCommandLineModel model)

private void ValidateRequiredDependencies(bool useSqlite)
{

var dependencies = new HashSet<string>()
{
"Microsoft.AspNetCore.Identity.UI",
Expand All @@ -732,12 +731,14 @@ private void ValidateRequiredDependencies(bool useSqlite)
}

var missingPackages = dependencies.Where(d => !_projectContext.PackageDependencies.Any(p => p.Name.Equals(d, StringComparison.OrdinalIgnoreCase)));

if (missingPackages.Any())
if (CalledFromCommandline && missingPackages.Any())
{
throw new InvalidOperationException(
string.Format(MessageStrings.InstallPackagesForScaffoldingIdentity, string.Join(",", missingPackages)));
}
}

//IFileSystem is DefaultFileSystem in commandline scenarios and SimulationModeFileSystem in VS scenarios.
private bool CalledFromCommandline => !(_fileSystem is SimulationModeFileSystem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Razor
public class EFModelBasedRazorPageScaffolder : RazorPageScaffolderBase
{
private IEntityFrameworkService _entityFrameworkService;
private IFileSystem _fileSystem;
private IModelTypesLocator _modelTypesLocator;
private static readonly IReadOnlyList<string> Views = new List<string>()
{
Expand All @@ -34,7 +35,8 @@ public EFModelBasedRazorPageScaffolder(
IEntityFrameworkService entityFrameworkService,
ICodeGeneratorActionsService codeGeneratorActionsService,
IServiceProvider serviceProvider,
ILogger logger)
ILogger logger,
IFileSystem fileSystem)
: base(projectContext, applicationInfo, codeGeneratorActionsService, serviceProvider, logger)
{
if (modelTypesLocator == null)
Expand All @@ -47,6 +49,7 @@ public EFModelBasedRazorPageScaffolder(
throw new ArgumentNullException(nameof(entityFrameworkService));
}

_fileSystem = fileSystem;
_modelTypesLocator = modelTypesLocator;
_entityFrameworkService = entityFrameworkService;
}
Expand Down Expand Up @@ -76,7 +79,7 @@ public override async Task GenerateCode(RazorPageGeneratorModel razorGeneratorMo

var outputPath = ValidateAndGetOutputPath(razorGeneratorModel, outputFileName: razorGeneratorModel.RazorPageName + Constants.ViewExtension);

EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies, razorGeneratorModel.UseSqlite);
EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies, razorGeneratorModel.UseSqlite, CalledFromCommandline);

ModelTypeAndContextModel modelTypeAndContextModel = await ModelMetadataUtilities.ValidateModelAndGetEFMetadata(
razorGeneratorModel,
Expand Down Expand Up @@ -131,7 +134,7 @@ internal async Task GenerateViews(RazorPageGeneratorModel razorPageGeneratorMode
ModelTypeAndContextModel modelTypeAndContextModel = null;
string outputPath = ValidateAndGetOutputPath(razorPageGeneratorModel, string.Empty);

EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies, razorPageGeneratorModel.UseSqlite);
EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies, razorPageGeneratorModel.UseSqlite, CalledFromCommandline);

modelTypeAndContextModel = await ModelMetadataUtilities.ValidateModelAndGetEFMetadata(
razorPageGeneratorModel,
Expand Down Expand Up @@ -191,5 +194,8 @@ internal async Task BaseGenerateViews(IDictionary<string, string> viewsAndTempla

await AddRequiredFiles(razorPageGeneratorModel);
}

//IFileSystem is DefaultFileSystem in commandline scenarios and SimulationModeFileSystem in VS scenarios.
private bool CalledFromCommandline => !(_fileSystem is SimulationModeFileSystem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override async Task GenerateCode(ViewGeneratorModel viewGeneratorModel)
ModelTypeAndContextModel modelTypeAndContextModel = null;
var outputPath = ValidateAndGetOutputPath(viewGeneratorModel, outputFileName: viewGeneratorModel.ViewName + Constants.ViewExtension);

EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies, viewGeneratorModel.UseSqlite);
EFValidationUtil.ValidateEFDependencies(_projectContext.PackageDependencies, viewGeneratorModel.UseSqlite, !string.IsNullOrEmpty(_projectContext.TargetFrameworkMoniker));

modelTypeAndContextModel = await ModelMetadataUtilities.ValidateModelAndGetEFMetadata(
viewGeneratorModel,
Expand Down

0 comments on commit 50e3d30

Please sign in to comment.