protected OperationExecutorBase( string assembly, string startupAssembly, string projectDir, string rootNamespace, string language) { AssemblyFileName = Path.GetFileNameWithoutExtension(assembly); StartupAssemblyFileName = startupAssembly == null ? AssemblyFileName : Path.GetFileNameWithoutExtension(startupAssembly); AppBasePath = Path.GetFullPath( Path.Combine(Directory.GetCurrentDirectory(), Path.GetDirectoryName(startupAssembly ?? assembly))); RootNamespace = rootNamespace ?? AssemblyFileName; ProjectDirectory = projectDir ?? Directory.GetCurrentDirectory(); Language = language; Reporter.WriteVerbose(Resources.UsingAssembly(AssemblyFileName)); Reporter.WriteVerbose(Resources.UsingStartupAssembly(StartupAssemblyFileName)); Reporter.WriteVerbose(Resources.UsingApplicationBase(AppBasePath)); Reporter.WriteVerbose(Resources.UsingWorkingDirectory(Directory.GetCurrentDirectory())); Reporter.WriteVerbose(Resources.UsingRootNamespace(RootNamespace)); Reporter.WriteVerbose(Resources.UsingProjectDir(ProjectDirectory)); }
private static int Main(string[] args) { var app = new CommandLineApplication(throwOnUnexpectedArg: false) { Name = "dotnet ef" }; new RootCommand().Configure(app); try { return(app.Execute(args)); } catch (Exception ex) { if (ex is CommandException || ex is CommandParsingException) { Reporter.WriteVerbose(ex.ToString()); } else { Reporter.WriteInformation(ex.ToString()); } Reporter.WriteError(ex.Message); return(1); } }
protected OperationExecutorBase( string assembly, string?startupAssembly, string?projectDir, string?rootNamespace, string?language, string[] remainingArguments) { AssemblyFileName = Path.GetFileNameWithoutExtension(assembly); StartupAssemblyFileName = startupAssembly == null ? AssemblyFileName : Path.GetFileNameWithoutExtension(startupAssembly); AppBasePath = Path.GetFullPath( Path.Combine(Directory.GetCurrentDirectory(), Path.GetDirectoryName(startupAssembly ?? assembly) !)); RootNamespace = rootNamespace ?? AssemblyFileName; ProjectDirectory = projectDir ?? Directory.GetCurrentDirectory(); Language = language; RemainingArguments = remainingArguments ?? Array.Empty <string>(); Reporter.WriteVerbose(Resources.UsingAssembly(AssemblyFileName)); Reporter.WriteVerbose(Resources.UsingStartupAssembly(StartupAssemblyFileName)); Reporter.WriteVerbose(Resources.UsingApplicationBase(AppBasePath)); Reporter.WriteVerbose(Resources.UsingWorkingDirectory(Directory.GetCurrentDirectory())); Reporter.WriteVerbose(Resources.UsingRootNamespace(RootNamespace)); Reporter.WriteVerbose(Resources.UsingProjectDir(ProjectDirectory)); Reporter.WriteVerbose(Resources.RemainingArguments(string.Join(",", RemainingArguments.Select(s => "'" + s + "'")))); }
public static int Run( string executable, IReadOnlyList <string> args, string?workingDirectory = null, bool interceptOutput = false) { var arguments = ToArguments(args); Reporter.WriteVerbose(executable + " " + arguments); var startInfo = new ProcessStartInfo { FileName = executable, Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = interceptOutput }; if (workingDirectory != null) { startInfo.WorkingDirectory = workingDirectory; } var process = Process.Start(startInfo) !; if (interceptOutput) { string?line; while ((line = process.StandardOutput.ReadLine()) != null) { Reporter.WriteVerbose(line); } } process.WaitForExit(); return(process.ExitCode); }
private static int Main(string[] args) { var app = new CommandLineApplication() { Name = "ef" }; new RootCommand().Configure(app); try { return(app.Execute(args)); } catch (Exception ex) { var wrappedException = ex as WrappedException; if (ex is CommandException || ex is CommandParsingException || (wrappedException != null && wrappedException.Type == "Microsoft.EntityFrameworkCore.Design.OperationException")) { Reporter.WriteVerbose(ex.ToString()); } else { Reporter.WriteInformation(ex.ToString()); } Reporter.WriteError(ex.Message); return(1); } }
public AppDomainOperationExecutor( string assembly, string startupAssembly, string projectDir, string dataDirectory, string rootNamespace, string language, string[] remainingArguments) : base(assembly, startupAssembly, projectDir, rootNamespace, language, remainingArguments) { var info = new AppDomainSetup { ApplicationBase = AppBasePath }; var configurationFile = (startupAssembly ?? assembly) + ".config"; if (File.Exists(configurationFile)) { Reporter.WriteVerbose(Resources.UsingConfigurationFile(configurationFile)); info.ConfigurationFile = configurationFile; } _domain = AppDomain.CreateDomain("EntityFrameworkCore.DesignDomain", null, info); if (dataDirectory != null) { Reporter.WriteVerbose(Resources.UsingDataDir(dataDirectory)); _domain.SetData("DataDirectory", dataDirectory); } var reportHandler = new OperationReportHandler( Reporter.WriteError, Reporter.WriteWarning, Reporter.WriteInformation, Reporter.WriteVerbose); _executor = _domain.CreateInstanceAndUnwrap( DesignAssemblyName, ExecutorTypeName, false, BindingFlags.Default, null, new object[] { reportHandler, new Hashtable { { "targetName", AssemblyFileName }, { "startupTargetName", StartupAssemblyFileName }, { "projectDir", ProjectDirectory }, { "rootNamespace", RootNamespace }, { "language", Language }, { "toolsVersion", ProductInfo.GetVersion() }, { "remainingArguments", RemainingArguments } } }, null, null); }
public AppDomainOperationExecutor( string assembly, string startupAssembly, string projectDir, string contentRootPath, string dataDirectory, string rootNamespace, string environment) : base(assembly, startupAssembly, projectDir, contentRootPath, dataDirectory, rootNamespace, environment) { var info = new AppDomainSetup { ApplicationBase = AppBasePath }; var configurationFile = (startupAssembly ?? assembly) + ".config"; if (File.Exists(configurationFile)) { Reporter.WriteVerbose(string.Format(Resources.UsingConfigurationFile, configurationFile)); info.ConfigurationFile = configurationFile; } _domain = AppDomain.CreateDomain("EntityFrameworkCore.DesignDomain", null, info); if (dataDirectory != null) { _domain.SetData("DataDirectory", dataDirectory); } var reportHandler = new OperationReportHandler(); _executor = _domain.CreateInstanceAndUnwrap( DesignAssemblyName, ExecutorTypeName, false, BindingFlags.Default, null, new object[] { reportHandler, new Hashtable { { "targetName", AssemblyFileName }, { "startupTargetName", StartupAssemblyFileName }, { "projectDir", ProjectDirectory }, { "contentRootPath", ContentRootPath }, { "rootNamespace", RootNamespace }, { "environment", EnvironmentName } } }, null, null); }
public ReflectionOperationExecutor( string assembly, string?startupAssembly, string?projectDir, string?dataDirectory, string?rootNamespace, string?language, bool nullable, string[] remainingArguments) : base(assembly, startupAssembly, projectDir, rootNamespace, language, nullable, remainingArguments) { if (dataDirectory != null) { Reporter.WriteVerbose(Resources.UsingDataDir(dataDirectory)); AppDomain.CurrentDomain.SetData("DataDirectory", dataDirectory); } AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly; _commandsAssembly = Assembly.Load(new AssemblyName { Name = DesignAssemblyName }); var reportHandlerType = _commandsAssembly.GetType(ReportHandlerTypeName, throwOnError: true, ignoreCase: false) !; var reportHandler = Activator.CreateInstance( reportHandlerType, (Action <string>)Reporter.WriteError, (Action <string>)Reporter.WriteWarning, (Action <string>)Reporter.WriteInformation, (Action <string>)Reporter.WriteVerbose) !; _executor = Activator.CreateInstance( _commandsAssembly.GetType(ExecutorTypeName, throwOnError: true, ignoreCase: false) !, reportHandler, new Dictionary <string, object?> { { "targetName", AssemblyFileName }, { "startupTargetName", StartupAssemblyFileName }, { "projectDir", ProjectDirectory }, { "rootNamespace", RootNamespace }, { "language", Language }, { "nullable", Nullable }, { "toolsVersion", ProductInfo.GetVersion() }, { "remainingArguments", RemainingArguments } }) !; _resultHandlerType = _commandsAssembly.GetType(ResultHandlerTypeName, throwOnError: true, ignoreCase: false) !; }
public static int Run(string executable, IReadOnlyList <string> args) { var arguments = ToArguments(args); Reporter.WriteVerbose(executable + " " + arguments); var build = Process.Start( new ProcessStartInfo { FileName = executable, Arguments = arguments, UseShellExecute = false }); build.WaitForExit(); return(build.ExitCode); }
protected OperationExecutorBase( string assembly, string startupAssembly, string projectDir, string contentRootPath, string dataDirectory, string rootNamespace, string environment) { AssemblyFileName = Path.GetFileNameWithoutExtension(assembly); StartupAssemblyFileName = startupAssembly == null ? AssemblyFileName : Path.GetFileNameWithoutExtension(startupAssembly); AppBasePath = Path.GetDirectoryName(startupAssembly ?? assembly); if (!Path.IsPathRooted(AppBasePath)) { AppBasePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), AppBasePath)); } ContentRootPath = contentRootPath ?? AppBasePath; RootNamespace = rootNamespace ?? AssemblyFileName; ProjectDirectory = projectDir ?? Directory.GetCurrentDirectory(); EnvironmentName = environment; Reporter.WriteVerbose(string.Format(Resources.UsingAssembly, AssemblyFileName)); Reporter.WriteVerbose(string.Format(Resources.UsingStartupAssembly, StartupAssemblyFileName)); Reporter.WriteVerbose(string.Format(Resources.UsingApplicationBase, AppBasePath)); Reporter.WriteVerbose(string.Format(Resources.UsingContentRoot, ContentRootPath)); Reporter.WriteVerbose(string.Format(Resources.UsingRootNamespace, RootNamespace)); Reporter.WriteVerbose(string.Format(Resources.UsingProjectDir, ProjectDirectory)); if (dataDirectory != null) { Reporter.WriteVerbose(string.Format(Resources.UsingDataDir, dataDirectory)); Environment.SetEnvironmentVariable(DataDirEnvName, dataDirectory); } }
public static int Run(string executable, IReadOnlyList <string> args, string workingDirectory = null) { var arguments = ToArguments(args); Reporter.WriteVerbose(executable + " " + arguments); var startInfo = new ProcessStartInfo { FileName = executable, Arguments = arguments, UseShellExecute = false }; if (workingDirectory != null) { startInfo.WorkingDirectory = workingDirectory; } var build = Process.Start(startInfo); build.WaitForExit(); return(build.ExitCode); }
public static Project FromFile( string file, string buildExtensionsDir, string framework = null, string configuration = null, string runtime = null) { Debug.Assert(!string.IsNullOrEmpty(file), "file is null or empty."); if (buildExtensionsDir == null) { buildExtensionsDir = Path.Combine(Path.GetDirectoryName(file), "obj"); } Directory.CreateDirectory(buildExtensionsDir); var efTargetsPath = Path.Combine( buildExtensionsDir, Path.GetFileName(file) + ".EntityFrameworkCore.targets"); using (var input = typeof(Resources).GetTypeInfo().Assembly.GetManifestResourceStream( "Microsoft.EntityFrameworkCore.Tools.Resources.EntityFrameworkCore.targets")) using (var output = File.OpenWrite(efTargetsPath)) { // NB: Copy always in case it changes Reporter.WriteVerbose(Resources.WritingFile(efTargetsPath)); input.CopyTo(output); } IDictionary <string, string> metadata; var metadataFile = Path.GetTempFileName(); try { var propertyArg = "/property:EFProjectMetadataFile=" + metadataFile; if (framework != null) { propertyArg += ";TargetFramework=" + framework; } if (configuration != null) { propertyArg += ";Configuration=" + configuration; } if (runtime != null) { propertyArg += ";RuntimeIdentifier=" + runtime; } var args = new List <string> { "msbuild", "/target:GetEFProjectMetadata", propertyArg, "/verbosity:quiet", "/nologo" }; if (file != null) { args.Add(file); } var exitCode = Exe.Run("dotnet", args); if (exitCode != 0) { throw new CommandException(Resources.GetMetadataFailed); } metadata = File.ReadLines(metadataFile).Select(l => l.Split(new[] { ':' }, 2)) .ToDictionary(s => s[0], s => s[1].TrimStart()); } finally { File.Delete(metadataFile); } var platformTarget = metadata["PlatformTarget"]; if (platformTarget.Length == 0) { platformTarget = metadata["Platform"]; } return(new Project(file, framework, configuration, runtime) { AssemblyName = metadata["AssemblyName"], Language = metadata["Language"], OutputPath = metadata["OutputPath"], PlatformTarget = platformTarget, ProjectAssetsFile = metadata["ProjectAssetsFile"], ProjectDir = metadata["ProjectDir"], RootNamespace = metadata["RootNamespace"], RuntimeFrameworkVersion = metadata["RuntimeFrameworkVersion"], TargetFileName = metadata["TargetFileName"], TargetFrameworkMoniker = metadata["TargetFrameworkMoniker"] }); }
protected override int Execute() { var commands = _args.TakeWhile(a => a[0] != '-').ToList(); if (_help.HasValue() || ShouldHelp(commands)) { return(ShowHelp(_help.HasValue(), commands)); } var projectFile = FindProjects( _project.Value(), Resources.NoProject, Resources.MultipleProjects); Reporter.WriteVerbose(Resources.UsingProject(projectFile)); var starupProjectFile = FindProjects( _startupProject.Value(), Resources.NoStartupProject, Resources.MultipleStartupProjects); Reporter.WriteVerbose(Resources.UsingStartupProject(starupProjectFile)); var project = Project.FromFile(projectFile, _msbuildprojectextensionspath.Value()); var startupProject = Project.FromFile( starupProjectFile, _msbuildprojectextensionspath.Value(), _framework.Value(), _configuration.Value(), _runtime.Value()); if (!_noBuild.HasValue()) { startupProject.Build(); } string executable; var args = new List <string>(); var toolsPath = Path.GetFullPath( Path.Combine(Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location), "..", "..")); var targetDir = Path.GetFullPath(Path.Combine(startupProject.ProjectDir, startupProject.OutputPath)); var targetPath = Path.Combine(targetDir, project.TargetFileName); var startupTargetPath = Path.Combine(targetDir, startupProject.TargetFileName); var depsFile = Path.Combine( targetDir, startupProject.AssemblyName + ".deps.json"); var runtimeConfig = Path.Combine( targetDir, startupProject.AssemblyName + ".runtimeconfig.json"); var projectAssetsFile = startupProject.ProjectAssetsFile; var targetFramework = new FrameworkName(startupProject.TargetFrameworkMoniker); if (targetFramework.Identifier == ".NETFramework") { executable = Path.Combine( toolsPath, "net461", startupProject.PlatformTarget == "x86" ? "ef.x86.exe" : "ef.exe"); } else if (targetFramework.Identifier == ".NETCoreApp") { executable = "dotnet"; args.Add("exec"); args.Add("--depsfile"); args.Add(depsFile); if (!string.IsNullOrEmpty(projectAssetsFile)) { using (var reader = new JsonTextReader(File.OpenText(projectAssetsFile))) { var projectAssets = JObject.ReadFrom(reader); var packageFolders = projectAssets["packageFolders"].Children <JProperty>().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 (startupProject.RuntimeFrameworkVersion.Length != 0) { args.Add("--fx-version"); args.Add(startupProject.RuntimeFrameworkVersion); } args.Add(Path.Combine(toolsPath, "netcoreapp2.0", "ef.dll")); } else if (targetFramework.Identifier == ".NETStandard") { throw new CommandException(Resources.NETStandardStartupProject(startupProject.ProjectName)); } else { throw new CommandException( Resources.UnsupportedFramework(startupProject.ProjectName, targetFramework.Identifier)); } args.AddRange(_args); args.Add("--assembly"); args.Add(targetPath); args.Add("--startup-assembly"); args.Add(startupTargetPath); args.Add("--project-dir"); args.Add(project.ProjectDir); args.Add("--language"); args.Add(project.Language); if (Reporter.IsVerbose) { args.Add("--verbose"); } if (Reporter.NoColor) { args.Add("--no-color"); } if (Reporter.PrefixOutput) { args.Add("--prefix-output"); } if (project.RootNamespace.Length != 0) { args.Add("--root-namespace"); args.Add(project.RootNamespace); } return(Exe.Run(executable, args, startupProject.ProjectDir)); }
protected override int Execute(string[] _) { var commands = _args !.TakeWhile(a => a[0] != '-').ToList(); if (_help !.HasValue() || ShouldHelp(commands)) { return(ShowHelp(_help.HasValue(), commands)); } var(projectFile, startupProjectFile) = ResolveProjects( _project !.Value(), _startupProject !.Value()); Reporter.WriteVerbose(Resources.UsingProject(projectFile)); Reporter.WriteVerbose(Resources.UsingStartupProject(startupProjectFile)); var project = Project.FromFile(projectFile, _msbuildprojectextensionspath !.Value()); var startupProject = Project.FromFile( startupProjectFile, _msbuildprojectextensionspath.Value(), _framework !.Value(), _configuration !.Value(), _runtime !.Value()); if (!_noBuild !.HasValue()) { Reporter.WriteInformation(Resources.BuildStarted); startupProject.Build(); Reporter.WriteInformation(Resources.BuildSucceeded); } string executable; var args = new List <string>(); var toolsPath = Path.Combine( Path.GetDirectoryName(typeof(Program).Assembly.Location) !, "tools"); var targetDir = Path.GetFullPath(Path.Combine(startupProject.ProjectDir !, startupProject.OutputPath !)); var targetPath = Path.Combine(targetDir, project.TargetFileName !); var startupTargetPath = Path.Combine(targetDir, startupProject.TargetFileName !); var depsFile = Path.Combine( targetDir, startupProject.AssemblyName + ".deps.json"); var runtimeConfig = Path.Combine( targetDir, startupProject.AssemblyName + ".runtimeconfig.json"); var projectAssetsFile = startupProject.ProjectAssetsFile; var targetFramework = new FrameworkName(startupProject.TargetFrameworkMoniker !); if (targetFramework.Identifier == ".NETFramework") { executable = Path.Combine( toolsPath, "net461", startupProject.PlatformTarget == "x86" ? "win-x86" : "any", "ef.exe"); } else if (targetFramework.Identifier == ".NETCoreApp") { if (targetFramework.Version < new Version(2, 0)) { throw new CommandException( Resources.NETCoreApp1StartupProject(startupProject.ProjectName, targetFramework.Version)); } executable = "dotnet"; args.Add("exec"); args.Add("--depsfile"); args.Add(depsFile); if (!string.IsNullOrEmpty(projectAssetsFile)) { using var file = File.OpenRead(projectAssetsFile); using var reader = JsonDocument.Parse(file); 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 (startupProject.RuntimeFrameworkVersion !.Length != 0) { args.Add("--fx-version"); args.Add(startupProject.RuntimeFrameworkVersion); } args.Add(Path.Combine(toolsPath, "netcoreapp2.0", "any", "ef.dll")); } else if (targetFramework.Identifier == ".NETStandard") { throw new CommandException(Resources.NETStandardStartupProject(startupProject.ProjectName)); } else { throw new CommandException( Resources.UnsupportedFramework(startupProject.ProjectName, targetFramework.Identifier)); } args.AddRange(_args !); args.Add("--assembly"); args.Add(targetPath); args.Add("--project"); args.Add(projectFile); args.Add("--startup-assembly"); args.Add(startupTargetPath); args.Add("--startup-project"); args.Add(startupProjectFile); args.Add("--project-dir"); args.Add(project.ProjectDir !); args.Add("--root-namespace"); args.Add(project.RootNamespace !); args.Add("--language"); args.Add(project.Language !); if (string.Equals(project.Nullable, "enable", StringComparison.OrdinalIgnoreCase) || string.Equals(project.Nullable, "annotations", StringComparison.OrdinalIgnoreCase)) { args.Add("--nullable"); } args.Add("--working-dir"); args.Add(Directory.GetCurrentDirectory()); if (Reporter.IsVerbose) { args.Add("--verbose"); } if (Reporter.NoColor) { args.Add("--no-color"); } if (Reporter.PrefixOutput) { args.Add("--prefix-output"); } if (_applicationArgs !.Any()) { args.Add("--"); args.AddRange(_applicationArgs !); } return(Exe.Run(executable, args, startupProject.ProjectDir)); }