{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/cli/pulls/comments/48581017","id":48581017,"diff_hunk":"@@ -14,26 +19,148 @@ public static int Main(string[] args)\n         {\n             DebugHelper.HandleDebugSwitch(ref args);\n \n-            var app = new CommandLineApplication();\n+            var app = new CommandLineApplication(throwOnUnexpectedArg: false);\n             app.Name = \"dotnet repl csi\";\n             app.FullName = \"C# REPL\";\n             app.Description = \"C# REPL for the .NET platform\";\n             app.HelpOption(\"-h|--help\");\n+\n             var script = app.Argument(\"<SCRIPT>\", \"The .csx file to run. Defaults to interactive mode.\");\n+            var framework = app.Option(\"-f|--framework <FRAMEWORK>\", \"Compile a specific framework\", CommandOptionType.MultipleValue);\n+            var configuration = app.Option(\"-c|--configuration <CONFIGURATION>\", \"Configuration under which to build\", CommandOptionType.SingleValue);\n+            var preserveTemporary = app.Option(\"-t|--preserve-temporary\", \"Preserve the temporary directory containing the compiled project.\", CommandOptionType.NoValue);\n+            var project = app.Option(\"-p|--project <PROJECT>\", \"The path to the project to run. Can be a path to a project.json or a project directory\", CommandOptionType.SingleValue);\n \n-            app.OnExecute(() => Run(script.Value));\n+            app.OnExecute(() => Run(script.Value, framework.Values, configuration.Value(), preserveTemporary.HasValue(), project.Value(), app.RemainingArguments));\n             return app.Execute(args);\n         }\n \n-        private static int Run(string scriptOpt)\n+        private static ProjectContext GetProjectContext(IEnumerable<string> targetFrameworks, string projectPath)\n+        {\n+            var contexts = ProjectContext.CreateContextForEachFramework(projectPath);\n+            var context = contexts.First();\n+\n+            if (targetFrameworks.Any())\n+            {\n+                var framework = NuGetFramework.Parse(targetFrameworks.First());\n+                context = contexts.FirstOrDefault(c => c.TargetFramework.Equals(framework));\n+            }\n+\n+            return context;\n+        }\n+\n+        private static CommandResult CompileProject(ProjectContext projectContext, string configuration, out string tempOutputDir)\n+        {\n+            tempOutputDir = Path.Combine(projectContext.ProjectDirectory, \"bin\", \".dotnetrepl\", Guid.NewGuid().ToString(\"N\"));\n+\n+            Reporter.Output.WriteLine($\"Compiling {projectContext.RootProject.Identity.Name.Yellow()} for {projectContext.TargetFramework.DotNetFrameworkName.Yellow()} to use with the {\"C# REPL\".Yellow()} environment.\");\n+\n+            return Command.Create($\"dotnet-compile\", $\"--output \\\"{tempOutputDir}\\\" --temp-output \\\"{tempOutputDir}\\\" --framework \\\"{projectContext.TargetFramework}\\\" --configuration \\\"{configuration}\\\" \\\"{projectContext.ProjectFile.ProjectDirectory}\\\"\")\n+                                .ForwardStdOut(onlyIfVerbose: true)\n+                                .ForwardStdErr()\n+                                .Execute();\n+        }\n+\n+        private static IEnumerable<string> GetRuntimeDependencies(ProjectContext projectContext, string buildConfiguration)\n+        {\n+            var runtimeDependencies = new HashSet<string>();\n+\n+            var projectExporter = projectContext.CreateExporter(buildConfiguration);\n+            var projectDependencies = projectExporter.GetDependencies();\n+\n+            foreach (var projectDependency in projectDependencies)\n+            {\n+                var runtimeAssemblies = projectDependency.RuntimeAssemblies;\n+\n+                foreach (var runtimeAssembly in runtimeAssemblies)\n+                {\n+                    var runtimeAssemblyPath = runtimeAssembly.ResolvedPath;\n+                    runtimeDependencies.Add(runtimeAssemblyPath);\n+                }\n+            }\n+\n+            return runtimeDependencies;\n+        }\n+\n+        private static string CreateResponseFile(ProjectContext projectContext, string buildConfiguration, string tempOutputDir)\n+        {\n+            var outputFileName = projectContext.ProjectFile.Name;\n+            var outputFilePath = Path.Combine(tempOutputDir, $\"{outputFileName}{Constants.DynamicLibSuffix}\");\n+            var projectResponseFilePath = Path.Combine(tempOutputDir, $\"dotnet-repl.{outputFileName}{Constants.ResponseFileSuffix}\");\n+\n+            var runtimeDependencies = GetRuntimeDependencies(projectContext, buildConfiguration);\n+\n+            var fileStream = new FileStream(projectResponseFilePath, FileMode.Create);\n+            using (var streamWriter = new StreamWriter(fileStream))\n+            {\n+                streamWriter.WriteLine($\"/r:\\\"{outputFilePath}\\\"\");\n+\n+                foreach (var projectDependency in runtimeDependencies)\n+                {\n+                    streamWriter.WriteLine($\"/r:\\\"{projectDependency}\\\"\");\n+                }\n+            }\n+\n+            return projectResponseFilePath;\n+        }\n+\n+        private static int Run(string script, IEnumerable<string> targetFrameworks, string buildConfiguration, bool preserveTemporaryOutput, string projectPath, IEnumerable<string> remainingArguments)\n         {\n             var corerun = Path.Combine(AppContext.BaseDirectory, Constants.HostExecutableName);\n             var csiExe = Path.Combine(AppContext.BaseDirectory, \"csi.exe\");\n-            var csiArgs = string.IsNullOrEmpty(scriptOpt) ? \"-i\" : scriptOpt;\n-            var result = Command.Create(csiExe, csiArgs)\n+            var csiArgs = new StringBuilder();\n+\n+            if (buildConfiguration == null)\n+            {\n+                buildConfiguration = Constants.DefaultConfiguration;\n+            }\n+\n+            string tempOutputDir = null;\n+\n+            if (!string.IsNullOrWhiteSpace(projectPath))\n+            {\n+                var projectContext = GetProjectContext(targetFrameworks, projectPath);\n+\n+                if (projectContext == null)\n+                {\n+                    Reporter.Error.WriteLine($\"Unrecognized framework: {targetFrameworks.First()}\".Red());\n+                }\n+\n+                var compileResult = CompileProject(projectContext, buildConfiguration, out tempOutputDir);\n+\n+                if (compileResult.ExitCode != 0)\n+                {\n+                    return compileResult.ExitCode;\n+                }\n+\n+                string responseFile = CreateResponseFile(projectContext, buildConfiguration, tempOutputDir);\n+                csiArgs.Append($\"@\\\"{responseFile}\\\" \");\n+            }\n+\n+            if (string.IsNullOrEmpty(script) && !remainingArguments.Any())\n+            {\n+                csiArgs.Append(\"-i\");\n+            }\n+            else\n+            {\n+                csiArgs.Append(script);\n+            }\n+\n+            foreach (string remainingArgument in remainingArguments)\n+            {\n+                csiArgs.Append($\" {remainingArgument}\");\n+            }\n+\n+            var result = Command.Create(csiExe, csiArgs.ToString())\n                 .ForwardStdOut()\n                 .ForwardStdErr()\n                 .Execute();\n+\n+            if ((tempOutputDir != null) && !preserveTemporaryOutput)\n+            {\n+                Directory.Delete(tempOutputDir, recursive: true);\n+            }","path":"src/Microsoft.DotNet.Tools.Repl.Csi/Program.cs","position":164,"original_position":164,"commit_id":"df6610166bb11ddfab7ab3fcdf81191cb23c2566","original_commit_id":"df6610166bb11ddfab7ab3fcdf81191cb23c2566","user":{"login":"cston","id":10732005,"avatar_url":"https://avatars.githubusercontent.com/u/10732005?v=3","gravatar_id":"","url":"https://api.github.com/users/cston","html_url":"https://github.com/cston","followers_url":"https://api.github.com/users/cston/followers","following_url":"https://api.github.com/users/cston/following{/other_user}","gists_url":"https://api.github.com/users/cston/gists{/gist_id}","starred_url":"https://api.github.com/users/cston/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cston/subscriptions","organizations_url":"https://api.github.com/users/cston/orgs","repos_url":"https://api.github.com/users/cston/repos","events_url":"https://api.github.com/users/cston/events{/privacy}","received_events_url":"https://api.github.com/users/cston/received_events","type":"User","site_admin":false},"body":"Deleting the directory should be in a `finally` block.","created_at":"2015-12-30T00:36:53Z","updated_at":"2015-12-30T00:36:53Z","html_url":"https://github.com/dotnet/cli/pull/662#discussion_r48581017","pull_request_url":"https://api.github.com/repos/dotnet/cli/pulls/662","_links":{"self":{"href":"https://api.github.com/repos/dotnet/cli/pulls/comments/48581017"},"html":{"href":"https://github.com/dotnet/cli/pull/662#discussion_r48581017"},"pull_request":{"href":"https://api.github.com/repos/dotnet/cli/pulls/662"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/cli/pulls/662","id":54755551,"html_url":"https://github.com/dotnet/cli/pull/662","diff_url":"https://github.com/dotnet/cli/pull/662.diff","patch_url":"https://github.com/dotnet/cli/pull/662.patch","issue_url":"https://api.github.com/repos/dotnet/cli/issues/662","number":662,"state":"open","locked":false,"title":"Adding support for seeding the repl with a project context.","user":{"login":"tannergooding","id":10487869,"avatar_url":"https://avatars.githubusercontent.com/u/10487869?v=3","gravatar_id":"","url":"https://api.github.com/users/tannergooding","html_url":"https://github.com/tannergooding","followers_url":"https://api.github.com/users/tannergooding/followers","following_url":"https://api.github.com/users/tannergooding/following{/other_user}","gists_url":"https://api.github.com/users/tannergooding/gists{/gist_id}","starred_url":"https://api.github.com/users/tannergooding/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tannergooding/subscriptions","organizations_url":"https://api.github.com/users/tannergooding/orgs","repos_url":"https://api.github.com/users/tannergooding/repos","events_url":"https://api.github.com/users/tannergooding/events{/privacy}","received_events_url":"https://api.github.com/users/tannergooding/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-12-29T20:30:10Z","updated_at":"2015-12-30T00:36:53Z","closed_at":null,"merged_at":null,"merge_commit_sha":"44293c471c0720a23bbeb310524cb06547f1c2ab","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/cli/pulls/662/commits","review_comments_url":"https://api.github.com/repos/dotnet/cli/pulls/662/comments","review_comment_url":"https://api.github.com/repos/dotnet/cli/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/cli/issues/662/comments","statuses_url":"https://api.github.com/repos/dotnet/cli/statuses/df6610166bb11ddfab7ab3fcdf81191cb23c2566","head":{"label":"tannergooding:ReplSeedProjectContext","ref":"ReplSeedProjectContext","sha":"df6610166bb11ddfab7ab3fcdf81191cb23c2566","user":{"login":"tannergooding","id":10487869,"avatar_url":"https://avatars.githubusercontent.com/u/10487869?v=3","gravatar_id":"","url":"https://api.github.com/users/tannergooding","html_url":"https://github.com/tannergooding","followers_url":"https://api.github.com/users/tannergooding/followers","following_url":"https://api.github.com/users/tannergooding/following{/other_user}","gists_url":"https://api.github.com/users/tannergooding/gists{/gist_id}","starred_url":"https://api.github.com/users/tannergooding/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tannergooding/subscriptions","organizations_url":"https://api.github.com/users/tannergooding/orgs","repos_url":"https://api.github.com/users/tannergooding/repos","events_url":"https://api.github.com/users/tannergooding/events{/privacy}","received_events_url":"https://api.github.com/users/tannergooding/received_events","type":"User","site_admin":false},"repo":{"id":48397334,"name":"cli","full_name":"tannergooding/cli","owner":{"login":"tannergooding","id":10487869,"avatar_url":"https://avatars.githubusercontent.com/u/10487869?v=3","gravatar_id":"","url":"https://api.github.com/users/tannergooding","html_url":"https://github.com/tannergooding","followers_url":"https://api.github.com/users/tannergooding/followers","following_url":"https://api.github.com/users/tannergooding/following{/other_user}","gists_url":"https://api.github.com/users/tannergooding/gists{/gist_id}","starred_url":"https://api.github.com/users/tannergooding/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tannergooding/subscriptions","organizations_url":"https://api.github.com/users/tannergooding/orgs","repos_url":"https://api.github.com/users/tannergooding/repos","events_url":"https://api.github.com/users/tannergooding/events{/privacy}","received_events_url":"https://api.github.com/users/tannergooding/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tannergooding/cli","description":"This repo contains the .NET Core command-line (CLI) tools, used for building .NET Core apps and libraries through your development flow (compiling, NuGet package management, running, testing, ...).","fork":true,"url":"https://api.github.com/repos/tannergooding/cli","forks_url":"https://api.github.com/repos/tannergooding/cli/forks","keys_url":"https://api.github.com/repos/tannergooding/cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tannergooding/cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tannergooding/cli/teams","hooks_url":"https://api.github.com/repos/tannergooding/cli/hooks","issue_events_url":"https://api.github.com/repos/tannergooding/cli/issues/events{/number}","events_url":"https://api.github.com/repos/tannergooding/cli/events","assignees_url":"https://api.github.com/repos/tannergooding/cli/assignees{/user}","branches_url":"https://api.github.com/repos/tannergooding/cli/branches{/branch}","tags_url":"https://api.github.com/repos/tannergooding/cli/tags","blobs_url":"https://api.github.com/repos/tannergooding/cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tannergooding/cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tannergooding/cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/tannergooding/cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tannergooding/cli/statuses/{sha}","languages_url":"https://api.github.com/repos/tannergooding/cli/languages","stargazers_url":"https://api.github.com/repos/tannergooding/cli/stargazers","contributors_url":"https://api.github.com/repos/tannergooding/cli/contributors","subscribers_url":"https://api.github.com/repos/tannergooding/cli/subscribers","subscription_url":"https://api.github.com/repos/tannergooding/cli/subscription","commits_url":"https://api.github.com/repos/tannergooding/cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/tannergooding/cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/tannergooding/cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/tannergooding/cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/tannergooding/cli/contents/{+path}","compare_url":"https://api.github.com/repos/tannergooding/cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tannergooding/cli/merges","archive_url":"https://api.github.com/repos/tannergooding/cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tannergooding/cli/downloads","issues_url":"https://api.github.com/repos/tannergooding/cli/issues{/number}","pulls_url":"https://api.github.com/repos/tannergooding/cli/pulls{/number}","milestones_url":"https://api.github.com/repos/tannergooding/cli/milestones{/number}","notifications_url":"https://api.github.com/repos/tannergooding/cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tannergooding/cli/labels{/name}","releases_url":"https://api.github.com/repos/tannergooding/cli/releases{/id}","created_at":"2015-12-21T22:24:59Z","updated_at":"2015-12-21T22:25:06Z","pushed_at":"2015-12-30T00:00:30Z","git_url":"git://github.com/tannergooding/cli.git","ssh_url":"git@github.com:tannergooding/cli.git","clone_url":"https://github.com/tannergooding/cli.git","svn_url":"https://github.com/tannergooding/cli","homepage":"http://dotnet.github.io/","size":14297,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"8bccd0b93bf0dadb65594590d10675717f910451","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":43568107,"name":"cli","full_name":"dotnet/cli","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/cli","description":"This repo contains the .NET Core command-line (CLI) tools, used for building .NET Core apps and libraries through your development flow (compiling, NuGet package management, running, testing, ...).","fork":false,"url":"https://api.github.com/repos/dotnet/cli","forks_url":"https://api.github.com/repos/dotnet/cli/forks","keys_url":"https://api.github.com/repos/dotnet/cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/cli/teams","hooks_url":"https://api.github.com/repos/dotnet/cli/hooks","issue_events_url":"https://api.github.com/repos/dotnet/cli/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/cli/events","assignees_url":"https://api.github.com/repos/dotnet/cli/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/cli/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/cli/tags","blobs_url":"https://api.github.com/repos/dotnet/cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/cli/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/cli/languages","stargazers_url":"https://api.github.com/repos/dotnet/cli/stargazers","contributors_url":"https://api.github.com/repos/dotnet/cli/contributors","subscribers_url":"https://api.github.com/repos/dotnet/cli/subscribers","subscription_url":"https://api.github.com/repos/dotnet/cli/subscription","commits_url":"https://api.github.com/repos/dotnet/cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/cli/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/cli/merges","archive_url":"https://api.github.com/repos/dotnet/cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/cli/downloads","issues_url":"https://api.github.com/repos/dotnet/cli/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/cli/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/cli/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/cli/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/cli/releases{/id}","created_at":"2015-10-02T18:03:15Z","updated_at":"2015-12-29T23:49:01Z","pushed_at":"2015-12-30T00:27:33Z","git_url":"git://github.com/dotnet/cli.git","ssh_url":"git@github.com:dotnet/cli.git","clone_url":"https://github.com/dotnet/cli.git","svn_url":"https://github.com/dotnet/cli","homepage":"http://dotnet.github.io/","size":16277,"stargazers_count":235,"watchers_count":235,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":43,"mirror_url":null,"open_issues_count":278,"forks":43,"open_issues":278,"watchers":235,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/cli/pulls/662"},"html":{"href":"https://github.com/dotnet/cli/pull/662"},"issue":{"href":"https://api.github.com/repos/dotnet/cli/issues/662"},"comments":{"href":"https://api.github.com/repos/dotnet/cli/issues/662/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/cli/pulls/662/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/cli/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/cli/pulls/662/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/cli/statuses/df6610166bb11ddfab7ab3fcdf81191cb23c2566"}}}}