├── .gitignore ├── Dotnet.Script.AspNet.sln ├── LICENSE ├── README.md ├── fixtures ├── app.csx └── project.json ├── global.json └── src └── Dotnet.Script.AspNet ├── AspNetScriptCompiler.cs ├── DictionaryExtensions.cs ├── Dotnet.Script.AspNet.xproj ├── Program.cs ├── Properties ├── AssemblyInfo.cs └── launchSettings.json ├── ScriptAssemblyExtensions.cs ├── ScriptControllerFeatureProvider.cs ├── ScriptingHost.cs ├── Startup.cs └── project.json /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /Dotnet.Script.AspNet.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F273FA2B-1A09-4D20-AFB3-A601DD80A6E6}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3278CF0A-3627-486C-A39C-134CC8C83F06}" 9 | ProjectSection(SolutionItems) = preProject 10 | global.json = global.json 11 | EndProjectSection 12 | EndProject 13 | Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Dotnet.Script.AspNet", "src\Dotnet.Script.AspNet\Dotnet.Script.AspNet.xproj", "{1DA38E03-8184-4D6C-9131-5175813C05C7}" 14 | EndProject 15 | Global 16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 17 | Debug|Any CPU = Debug|Any CPU 18 | Release|Any CPU = Release|Any CPU 19 | EndGlobalSection 20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 21 | {1DA38E03-8184-4D6C-9131-5175813C05C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {1DA38E03-8184-4D6C-9131-5175813C05C7}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {1DA38E03-8184-4D6C-9131-5175813C05C7}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {1DA38E03-8184-4D6C-9131-5175813C05C7}.Release|Any CPU.Build.0 = Release|Any CPU 25 | EndGlobalSection 26 | GlobalSection(SolutionProperties) = preSolution 27 | HideSolutionNode = FALSE 28 | EndGlobalSection 29 | GlobalSection(NestedProjects) = preSolution 30 | {1DA38E03-8184-4D6C-9131-5175813C05C7} = {F273FA2B-1A09-4D20-AFB3-A601DD80A6E6} 31 | EndGlobalSection 32 | EndGlobal 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Filip W 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Experimental playground for Scripting ASP.NET Core apps 2 | 3 | ## What is this? 4 | 5 | Quickly hacked together at MVP Summit. The idea is, you can write an ASP.NET Core app as a script, with lowest possible ceremony, and then run them using .NET CLI. 6 | 7 | It's based on Roslyn scripting and [dotnet-script](https://github.com/filipw/dotnet-script) project. 8 | 9 | ![](http://i.imgur.com/5bv2oqt.jpg) 10 | 11 | ## Getting up and running 12 | 13 | 1) Create a `project.json` with your dependencies. You can reference anything that works on .NET Core. `Microsoft.AspNetCore.Server.Kestrel` and `Microsoft.AspNetCore.Mvc` are implicitly available to you. You also need to reference the `Dotnet.Script.AspNet` as a tool - as we will be using it for running the app. 14 | 15 | Sample `project.json` 16 | 17 | ``` 18 | { 19 | "frameworks": { 20 | "netcoreapp1.0": { 21 | "dependencies": { 22 | "GenFu": "1.2.1" 23 | } 24 | } 25 | }, 26 | "tools": { 27 | "Dotnet.Script.AspNet": { 28 | "version": "0.0.1-beta", 29 | "imports": [ 30 | "portable-net45+win8", 31 | "dnxcore50" 32 | ] 33 | } 34 | } 35 | } 36 | ``` 37 | 38 | In the above case, `GenFu` is a package we wanna use in our ASP.NET Core app and `Dotnet.Script.AspNet` is mandatory (it's a runner). Again, no need to reference `Microsoft.AspNetCore.Server.Kestrel` and `Microsoft.AspNetCore.Mvc` - but you will need to pull in any other package you want. 39 | 40 | 2) `dotnet restore` 41 | 42 | 3) Create a script file (CSX) where you can interact with ASP.NET core pipeline, set up services, configure MVC, add controllers, models, use NuGet packages etc. The hooks from the `Startup` class are surfaced as `Confgure` and `Services` global methods. Sample is shown below. 43 | 44 | ``` 45 | using GenFu; 46 | 47 | [Route("api/[controller]")] 48 | public class PersonController : Controller 49 | { 50 | [HttpGet] 51 | public Person Get() 52 | { 53 | var person = A.New(); 54 | return person; 55 | } 56 | } 57 | 58 | public class Person 59 | { 60 | public string FirstName { get; set; } 61 | public string LastName { get; set; } 62 | } 63 | 64 | Services(s => s.AddMvc()); 65 | Configure((app, env) => app.UseMvc()); 66 | ``` 67 | 68 | 3) Run `dotnet script-aspnet {script_name}.csx`. This will launch Kestrel and start your app. 69 | 70 | 4) Profit. 71 | 72 | ![](http://g.recordit.co/rbJvrQe47u.gif) 73 | -------------------------------------------------------------------------------- /fixtures/app.csx: -------------------------------------------------------------------------------- 1 | using GenFu; 2 | 3 | [Route("api/[controller]")] 4 | public class PersonController : Controller 5 | { 6 | [HttpGet] 7 | public Person Get() 8 | { 9 | var person = A.New(); 10 | return person; 11 | } 12 | } 13 | 14 | public class Person 15 | { 16 | public string FirstName { get; set; } 17 | public string LastName { get; set; } 18 | } 19 | 20 | Services(s => s.AddMvc()); 21 | Configure((app, env) => app.UseMvc()); -------------------------------------------------------------------------------- /fixtures/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "frameworks": { 3 | "netcoreapp1.0": { 4 | "dependencies": { 5 | "GenFu": "1.2.1" 6 | } 7 | } 8 | }, 9 | "tools": { 10 | "Dotnet.Script.AspNet": { 11 | "version": "0.0.1-beta", 12 | "imports": [ 13 | "portable-net45+win8", 14 | "dnxcore50" 15 | ] 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "projects": [ "src", "test" ], 3 | "sdk": { 4 | "version": "1.0.0-preview2-003131" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/AspNetScriptCompiler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Dotnet.Script.Core; 4 | using Microsoft.CodeAnalysis.Scripting; 5 | using Microsoft.DotNet.InternalAbstractions; 6 | using Microsoft.Extensions.DependencyModel; 7 | 8 | namespace Dotnet.Script.AspNet 9 | { 10 | public class AspNetScriptCompiler : ScriptCompiler 11 | { 12 | private IEnumerable _namespaces = new[] 13 | { 14 | "System", 15 | "System.IO", 16 | "System.Linq", 17 | "System.Collections.Generic", 18 | "Microsoft.Extensions.DependencyInjection", 19 | "Microsoft.AspNetCore.Builder", 20 | "Microsoft.AspNetCore.Mvc" 21 | }; 22 | 23 | public AspNetScriptCompiler(ScriptLogger logger) : base(logger) 24 | { 25 | } 26 | 27 | public override ScriptOptions CreateScriptOptions(ScriptContext context) 28 | { 29 | var opts = base.CreateScriptOptions(context).AddImports(_namespaces); 30 | 31 | var runtimeId = RuntimeEnvironment.GetRuntimeIdentifier(); 32 | var inheritedAssemblyNames = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId); 33 | 34 | foreach (var inheritedAssemblyName in inheritedAssemblyNames) 35 | { 36 | var assembly = Assembly.Load(inheritedAssemblyName); 37 | opts = opts.AddReferences(assembly); 38 | } 39 | 40 | return opts; 41 | } 42 | 43 | public override ScriptCompilationContext CreateCompilationContext(ScriptContext context) 44 | { 45 | var result = base.CreateCompilationContext(context); 46 | Context = result; 47 | return result; 48 | } 49 | 50 | public object Context { get; private set; } 51 | } 52 | } -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/DictionaryExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace Dotnet.Script.AspNet 6 | { 7 | static class DictionaryExtensions 8 | { 9 | public static IEnumerable GetEntries(this IDictionary dictionary) 10 | { 11 | if (dictionary == null) throw new ArgumentNullException(nameof(dictionary)); 12 | return GetEntriesIterator(dictionary); 13 | } 14 | 15 | static IEnumerable GetEntriesIterator(IDictionary dictionary) 16 | { 17 | var e = dictionary.GetEnumerator(); 18 | while (e.MoveNext()) 19 | yield return e.Entry; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/Dotnet.Script.AspNet.xproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 14.0 5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 6 | 7 | 8 | 9 | 10 | 1da38e03-8184-4d6c-9131-5175813c05c7 11 | Dotnet.Script.AspNet 12 | .\obj 13 | .\bin\ 14 | v4.5 15 | 16 | 17 | 18 | 2.0 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.CommandLineUtils; 4 | 5 | namespace Dotnet.Script.AspNet 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | var app = new CommandLineApplication(throwOnUnexpectedArg: false); 12 | var file = app.Argument("aspnet-script", "Path to CSX script"); 13 | 14 | app.OnExecute(() => 15 | { 16 | 17 | string workingDirectory; 18 | 19 | if (Path.IsPathRooted(file.Value)) 20 | { 21 | workingDirectory = Path.GetDirectoryName(file.Value); 22 | Startup.ScriptFullPath = file.Value; 23 | } 24 | else 25 | { 26 | var absolutePath = Path.Combine(Directory.GetCurrentDirectory(), file.Value); 27 | workingDirectory = Path.GetDirectoryName(absolutePath); 28 | Startup.ScriptFullPath = absolutePath; 29 | } 30 | 31 | var host = new WebHostBuilder() 32 | .UseKestrel() 33 | .UseContentRoot(workingDirectory) 34 | .UseStartup() 35 | .Build(); 36 | 37 | host.Run(); 38 | return 0; 39 | }); 40 | 41 | app.Execute(args); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("Dotnet.Script.AspNet")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("1da38e03-8184-4d6c-9131-5175813c05c7")] 20 | -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Dotnet.Script.AspNet": { 4 | "commandName": "Project", 5 | "commandLineArgs": "..\\..\\fixtures\\app.csx" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/ScriptAssemblyExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Reflection; 4 | using System.Linq; 5 | using Dotnet.Script.Core; 6 | 7 | namespace Dotnet.Script.AspNet 8 | { 9 | public static class ScriptAssemblyExtensions 10 | { 11 | public static Assembly GetScriptAssembly(this ScriptCompilationContext scriptCompilationContext) 12 | { 13 | // force creation of lazy evaluator 14 | // this does not execute the script code but it will emit the assembly 15 | // and load it via InteractiveAssemblyLoader 16 | var scriptDelegate = scriptCompilationContext.Script.CreateDelegate(); 17 | 18 | // https://github.com/dotnet/roslyn/blob/version-2.0.0-beta4/src/Scripting/Core/Hosting/AssemblyLoader/InteractiveAssemblyLoader.cs#L52 19 | var loadedAssembliesBySimpleNameProperty = scriptCompilationContext.Loader.GetType().GetField("_loadedAssembliesBySimpleName", BindingFlags.NonPublic | BindingFlags.Instance); 20 | var loadedAssembliesBySimpleName = loadedAssembliesBySimpleNameProperty?.GetValue(scriptCompilationContext.Loader) as IDictionary; 21 | if (loadedAssembliesBySimpleName == null) 22 | return null; 23 | 24 | var assemblies = 25 | from entry in loadedAssembliesBySimpleName.GetEntries() 26 | select entry.Value as IList into infoList 27 | where infoList != null 28 | from object infoObject in infoList 29 | // https://github.com/dotnet/roslyn/blob/version-2.0.0-beta4/src/Scripting/Core/Hosting/AssemblyLoader/InteractiveAssemblyLoader.cs#L77 30 | select (Assembly)infoObject.GetType().GetField("Assembly")?.GetValue(infoObject) into asm 31 | where asm != null 32 | select asm; 33 | 34 | return assemblies.FirstOrDefault(asm => asm.FullName.StartsWith("\u211B*", StringComparison.Ordinal)); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/ScriptControllerFeatureProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Microsoft.AspNetCore.Mvc.Controllers; 5 | 6 | namespace Dotnet.Script.AspNet 7 | { 8 | public class ScriptControllerFeatureProvider : ControllerFeatureProvider 9 | { 10 | protected override bool IsController(TypeInfo typeInfo) 11 | { 12 | if (!typeInfo.IsClass) 13 | { 14 | return false; 15 | } 16 | 17 | if (typeInfo.IsAbstract) 18 | { 19 | return false; 20 | } 21 | 22 | if (typeInfo.ContainsGenericParameters) 23 | { 24 | return false; 25 | } 26 | 27 | if (typeInfo.IsDefined(typeof(NonControllerAttribute))) 28 | { 29 | return false; 30 | } 31 | 32 | if (!typeInfo.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) && 33 | !typeInfo.IsDefined(typeof(ControllerAttribute))) 34 | { 35 | return false; 36 | } 37 | 38 | return true; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/ScriptingHost.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Dotnet.Script.AspNet 7 | { 8 | public class ScriptingHost 9 | { 10 | internal Func ConfigureServices; 11 | internal Action ConfigureApp; 12 | 13 | public void Services(Func configureServices) 14 | { 15 | ConfigureServices = configureServices; 16 | } 17 | 18 | public void Configure(Action configureApp) 19 | { 20 | ConfigureApp = configureApp; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Reflection; 5 | using System.Text; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.AspNetCore.Mvc.ApplicationParts; 9 | using Microsoft.CodeAnalysis.Text; 10 | using Microsoft.Extensions.DependencyInjection; 11 | using Dotnet.Script.Core; 12 | 13 | namespace Dotnet.Script.AspNet 14 | { 15 | public class Startup 16 | { 17 | public static string ScriptFullPath; 18 | private readonly ScriptingHost _scriptHost = new ScriptingHost(); 19 | private readonly Assembly _scriptAssembly; 20 | 21 | public Startup() 22 | { 23 | var code = File.ReadAllText(ScriptFullPath); 24 | var sourceText = SourceText.From(code, Encoding.UTF8); 25 | var context = new ScriptContext(sourceText, Path.GetDirectoryName(ScriptFullPath), "Release", new List(), ScriptFullPath); 26 | 27 | var logger = new ScriptLogger(Console.Out, false); 28 | var compiler = new AspNetScriptCompiler(logger); 29 | var runner = new ScriptRunner(compiler, logger); 30 | 31 | var result = runner.Execute(context, _scriptHost).GetAwaiter().GetResult(); 32 | 33 | var compilationContext = compiler.Context as ScriptCompilationContext; 34 | _scriptAssembly = compilationContext.GetScriptAssembly(); 35 | } 36 | 37 | public void ConfigureServices(IServiceCollection services) 38 | { 39 | var manager = new ApplicationPartManager(); 40 | manager.ApplicationParts.Add(new AssemblyPart(_scriptAssembly)); 41 | manager.FeatureProviders.Add(new ScriptControllerFeatureProvider()); 42 | services.AddSingleton(manager); 43 | 44 | var mvcBuilder = _scriptHost.ConfigureServices?.Invoke(services)?.AddControllersAsServices(); 45 | } 46 | 47 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 48 | { 49 | _scriptHost.ConfigureApp?.Invoke(app, env); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /src/Dotnet.Script.AspNet/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.0.1-*", 3 | "description": "Dotnet CLI tool allowing you to run C# ASP.NET Core apps as (CSX) scripts.", 4 | "authors": [ "filipw" ], 5 | "packOptions": { 6 | "summary": "Dotnet CLI tool allowing you to run C# ASP.NET Core apps as (CSX) scripts.", 7 | "owners": [ "filipw" ], 8 | "title": "Dotnet.Script.AspNet", 9 | "iconUrl": "https://raw.githubusercontent.com/filipw/Strathweb.TypedRouting.AspNetCore/master/strathweb.png", 10 | "projectUrl": "https://github.com/filipw/dotnet-script-aspnett", 11 | "licenseUrl": "https://github.com/filipw/dotnet-script-aspnet/blob/master/LICENSE", 12 | "requireLicenseAcceptance": false, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/filipw/dotnet-script-aspnet.git" 16 | } 17 | }, 18 | 19 | "buildOptions": { 20 | "emitEntryPoint": true, 21 | "outputName": "dotnet-script-aspnet", 22 | "debugType": "portable" 23 | }, 24 | 25 | "dependencies": { 26 | "Microsoft.AspNetCore.Mvc": "1.0.1", 27 | "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", 28 | "Microsoft.Extensions.CommandLineUtils": "1.0.0", 29 | "Dotnet.Script.Core": "0.7.0-beta", 30 | "Microsoft.NETCore.App": { 31 | "type": "platform", 32 | "version": "1.0.1" 33 | } 34 | }, 35 | 36 | "frameworks": { 37 | "netcoreapp1.0": { 38 | "imports": "dnxcore50" 39 | } 40 | } 41 | } 42 | --------------------------------------------------------------------------------