diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index aa8b7c28..00000000 --- a/.gitattributes +++ /dev/null @@ -1,23 +0,0 @@ -# Auto detect text files and perform LF normalization -# http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/ -* text=auto - -# Custom for Visual Studio -*.cs diff=csharp -*.sln merge=union -*.csproj merge=union -*.vbproj merge=union -*.fsproj merge=union -*.dbproj merge=union - -# Standard to msysgit -*.doc diff=astextplain -*.DOC diff=astextplain -*.docx diff=astextplain -*.DOCX diff=astextplain -*.dot diff=astextplain -*.DOT diff=astextplain -*.pdf diff=astextplain -*.PDF diff=astextplain -*.rtf diff=astextplain -*.RTF diff=astextplain \ No newline at end of file diff --git a/.gitignore b/.gitignore deleted file mode 100644 index bc064d5d..00000000 --- a/.gitignore +++ /dev/null @@ -1,317 +0,0 @@ -# Files relevant to ScriptCS project - -artifacts -/scriptcs_packages - -# Diff files -*.orig - -# These are extra items from the original ignore file -# keeping the items here until I submit PR to gitignore.io -*.sln.ide -*.bak -*.ncrunch* -*.Cache -[Ss]tyle[Cc]op.* - -# ----------------------------------------------------------------------- -# Created by https://www.gitignore.io/api/visualstudio - -### VisualStudio ### -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore - -# User-specific files -*.suo -*.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -bld/ -[Bb]in/ -[Oo]bj/ -[Ll]og/ - -# Visual Studio 2015 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUNIT -*.VisualState.xml -TestResult.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ -**/Properties/launchSettings.json - -*_i.c -*_p.c -*_i.h -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# JustCode is a .NET coding add-in -.JustCode - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# TODO: Uncomment the next line to ignore your web deploy settings. -# By default, sensitive information, such as encrypted password -# should be stored in the .pubxml.user file. -#*.pubxml -*.pubxml.user -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# The packages folder can be ignored because of Package Restore -**/packages/* -# except build/, which is used as an MSBuild target. -!**/packages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/packages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Typescript v1 declaration files -typings/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# JetBrains Rider -.idea/ -*.sln.iml - -# CodeRush -.cr/ - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -### VisualStudio Patch ### -# By default, sensitive information, such as encrypted password -# should be stored in the .pubxml.user file. - -# End of https://www.gitignore.io/api/visualstudio -*.DS_Store diff --git a/.mailmap b/.mailmap deleted file mode 100644 index b321039f..00000000 --- a/.mailmap +++ /dev/null @@ -1,6 +0,0 @@ -Filip W -Kristian Hellang -Justin Rusbatch -dschenkelman -Nick Berardi -estoy diff --git a/.nuget/NuGet.exe b/.nuget/NuGet.exe deleted file mode 100644 index a34c3675..00000000 Binary files a/.nuget/NuGet.exe and /dev/null differ diff --git a/.nuget/packages.config b/.nuget/packages.config deleted file mode 100644 index 61ff56f9..00000000 --- a/.nuget/packages.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/.paket/Paket.Restore.targets b/.paket/Paket.Restore.targets deleted file mode 100644 index 305e736c..00000000 --- a/.paket/Paket.Restore.targets +++ /dev/null @@ -1,300 +0,0 @@ - - - - - - - $(MSBuildAllProjects);$(MSBuildThisFileFullPath) - - true - $(MSBuildThisFileDirectory) - $(MSBuildThisFileDirectory)..\ - $(PaketRootPath)paket-files\paket.restore.cached - $(PaketRootPath)paket.lock - /Library/Frameworks/Mono.framework/Commands/mono - mono - - $(PaketRootPath)paket.exe - $(PaketToolsPath)paket.exe - "$(PaketExePath)" - $(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)" - - - <_PaketExeExtension>$([System.IO.Path]::GetExtension("$(PaketExePath)")) - dotnet "$(PaketExePath)" - - - "$(PaketExePath)" - - $(PaketRootPath)paket.bootstrapper.exe - $(PaketToolsPath)paket.bootstrapper.exe - "$(PaketBootStrapperExePath)" - $(MonoPath) --runtime=v4.0.30319 "$(PaketBootStrapperExePath)" - - - - - true - true - - - - - - - true - $(NoWarn);NU1603;NU1604;NU1605;NU1608 - - - - - /usr/bin/shasum "$(PaketRestoreCacheFile)" | /usr/bin/awk '{ print $1 }' - /usr/bin/shasum "$(PaketLockFilePath)" | /usr/bin/awk '{ print $1 }' - - - - - - - - - - - - - - - - $([System.IO.File]::ReadAllText('$(PaketRestoreCacheFile)')) - $([System.IO.File]::ReadAllText('$(PaketLockFilePath)')) - true - false - true - - - - true - - - - - - - - - - - - - - - - $(MSBuildProjectDirectory)\obj\$(MSBuildProjectFile).paket.references.cached - - $(MSBuildProjectFullPath).paket.references - - $(MSBuildProjectDirectory)\$(MSBuildProjectName).paket.references - - $(MSBuildProjectDirectory)\paket.references - - false - true - true - references-file-or-cache-not-found - - - - - $([System.IO.File]::ReadAllText('$(PaketReferencesCachedFilePath)')) - $([System.IO.File]::ReadAllText('$(PaketOriginalReferencesFilePath)')) - references-file - false - - - - - false - - - - - true - target-framework '$(TargetFramework)' or '$(TargetFrameworks)' files @(PaketResolvedFilePaths) - - - - - - - - - - false - true - - - - - - - - - - - $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[0]) - $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[1]) - $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[4]) - $([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[5]) - - - %(PaketReferencesFileLinesInfo.PackageVersion) - All - runtime - true - - - - - $(MSBuildProjectDirectory)/obj/$(MSBuildProjectFile).paket.clitools - - - - - - - - - $([System.String]::Copy('%(PaketCliToolFileLines.Identity)').Split(',')[0]) - $([System.String]::Copy('%(PaketCliToolFileLines.Identity)').Split(',')[1]) - - - %(PaketCliToolFileLinesInfo.PackageVersion) - - - - - - - - - - false - - - - - - <_NuspecFilesNewLocation Include="$(BaseIntermediateOutputPath)$(Configuration)\*.nuspec"/> - - - - $(MSBuildProjectDirectory)/$(MSBuildProjectFile) - true - false - true - $(BaseIntermediateOutputPath)$(Configuration) - $(BaseIntermediateOutputPath) - - - - <_NuspecFiles Include="$(AdjustedNuspecOutputPath)\*.nuspec"/> - - - - - - - - - - - - - - - - diff --git a/.paket/paket.bootstrapper.exe b/.paket/paket.bootstrapper.exe deleted file mode 100644 index b98e000b..00000000 Binary files a/.paket/paket.bootstrapper.exe and /dev/null differ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4f77655f..00000000 --- a/.travis.yml +++ /dev/null @@ -1,20 +0,0 @@ -language: csharp -dotnet: 2.0.0 -sudo: required -dist: trusty -env: - global: - - secure: m2PtYwYOhaK0uFMZ19ZxApZwWZeAIq1dS//jx/5I3txpIWD+TfycQMAWYxycFJ/GJkeVF29P4Zz1uyS2XKKjPJpp2Pds98FNQyDv3OftpLAVa0drsjfhurVlBmSdrV7GH6ncKfvhd+h7KVK5vbZc+NeR4dH7eNvN/jraS//AMJg= - - DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true - - DOTNET_CLI_TELEMETRY_OPTOUT=true -mono: - - 5.8.0 - -os: - - linux - - osx - -script: - - ulimit -n8192 - - chmod +x build.sh - - ./build.sh \ No newline at end of file diff --git a/CNAME b/CNAME new file mode 100644 index 00000000..dd8efa7e --- /dev/null +++ b/CNAME @@ -0,0 +1 @@ +scriptcs.net diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 52671d59..00000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,58 +0,0 @@ -# Contributing - -We've seen some great energy from folks around contributing to scriptcs and WE LOVE IT. scriptcs *welcomes* your contributions! In order to make it easy we've come up with the following model so that you can [Jump in](http://nikcodes.com/2013/05/10/new-contributor-jump-in/). The guidelines will help us all to operate in harmony. - -We definitely want to soak up all that energy you have! - -## The issue tracker and how you can contribute. - -* Bugs - For bugs, if you see one filed, just go fix it. If there is no bug filed, please file one first. You don't need to seek approval from core unless the bug starts to turn into a feature. _Any bug fix which is a significant code contribution (say more than 10 lines of code), which introduces new APIs or which introduces breaking changes will be treated in the same way as we treat a feature (see below)._ -* Features - By default all issues tagged as enhancements which we file will be fixed by the core team. However if you'd like to take it, please comment in the issue that you'd like to implement it so we can discuss before you put in a potentially wasted effort. -* YOU TAKE IT - These are items that are important, but which we'd available for the community to take. One place we use this is for investigations and prototyping for features that would be really awesome, but which the core team just don't have bandwith for. As an example @dschenkelman [investigated](https://github.com/scriptcs/scriptcs/issues/68?source=cc) a VS debugging story. This lead to him ultimately implementing the feature, however don't feel pressure that you have to that. It's extremely valuable if you can just show us how it might be done. - -### Other points - -* If an issue is marked as "Taken" then it is assigned to another member of the community. -* To take an issue, reply in the comments "I'll take it" and wait for confirmation from a member of the core team. - -## Discussing features - -We'd like the design for all features to get socialized either via github issues or our [discussion group](https://groups.google.com/forum/?fromgroups#!forum/scriptcs). Please do this before you implement so that we can all get on the same page of the feature and not waste your time, which we really appreciate. - -## Summary - -* Unassigned bug - Available for you -* Assigned bug / Taken bug - unavailable -* You take it issue - Available for you once it is confirmed. -* Taken issue - Unavailable as someone already took it. - -We really appreciate your help in taking scriptcs forward! - -Also please see the discussion [here](https://github.com/scriptcs/scriptcs/issues/79) on which things are open for contribution. - -## Workflow - -* We use the **dev** branch for active development. All pull requests should be made to this branch. -* The master branch is only updated via pull requests from the dev branch. -* Tests need to be provided for every bug/feature that is completed. -* Please do not submit a PR unless without running all unit tests. If any tests are failing in the PR they will be rejected. - -## Issue Management - -We use some tags to manage issue priority. - -* **P1**: This is a big deal. It's either really awesome, terribly broken, or it's holding other awesome things up. -* **P2**: This is kind of important and we're really looking forward to doing it, but there are more important things that need done first. -* **P3**: We'd like to see these get implemented, but we're just not sure when we'll get to them. Want to take a shot at it? Go for it! - -If the issue isn't marked with one of the above tags it's either still under discussion or not something we think we'll get to. - -## Code Style - -* Indent with spaces (4) instead of tabs. -* Prefix private instance field names with an underscore and be camel-cased. The `this` keyword should be avoided. -* Use the `var` keyword unless the inferred type is not obvious. -* Use the C# type aliases for types that have them (ex. `int` instead of `Int32`). -* Use meaningful names (no hungarian notation). -* Unit tests should follow the conventions detailed in [this blog post](http://haacked.com/archive/2012/01/02/structuring-unit-tests.aspx) by Phil Haack. -* Unit tests assertions should be declared using the [Should Assertion Library](https://github.com/erichexter/Should) by Eric Hexter. diff --git a/Directory.Build.props b/Directory.Build.props deleted file mode 100644 index fcb2be8d..00000000 --- a/Directory.Build.props +++ /dev/null @@ -1,5 +0,0 @@ - - - latest - - \ No newline at end of file diff --git a/Directory.Build.targets b/Directory.Build.targets deleted file mode 100644 index eb7be3ec..00000000 --- a/Directory.Build.targets +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md deleted file mode 100644 index 7261cf31..00000000 --- a/LICENSE.md +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2013 Glenn Block, Justin Rusbatch, Filip Wojcieszyn - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/README.md b/README.md deleted file mode 100644 index 01c0df77..00000000 --- a/README.md +++ /dev/null @@ -1,300 +0,0 @@ -# scriptcs - -[![Chocolatey Version](http://img.shields.io/chocolatey/v/scriptcs.svg?style=flat-square)](http://chocolatey.org/packages/scriptcs) [![Chocolatey Downloads](http://img.shields.io/chocolatey/dt/scriptcs.svg?style=flat-square)](http://chocolatey.org/packages/scriptcs) [![NuGet version (ScriptCs.Hosting)](https://img.shields.io/nuget/v/ScriptCs.Hosting.svg?style=flat-square)](https://www.nuget.org/packages/ScriptCs.Hosting/) - -[![*nix Build Status](http://img.shields.io/travis/scriptcs/scriptcs/dev.svg?style=flat-square&label=linux-build)](https://travis-ci.org/scriptcs/scriptcs) [![Windows Build Status](http://img.shields.io/teamcity/codebetter/Scriptcs_Ci.svg?style=flat-square&label=windows-build)](http://ci.scriptcs.net) [![Coverity Scan Build Status](https://img.shields.io/badge/coverity-passed-brightgreen.svg?style=flat-square)](https://scan.coverity.com/projects/2356) - -[![Issue Stats](http://issuestats.com/github/scriptcs/scriptcs/badge/pr?style=flat-square)](http://issuestats.com/github/scriptcs/scriptcs) [![Issue Stats](http://issuestats.com/github/scriptcs/scriptcs/badge/issue?style=flat-square)](http://issuestats.com/github/scriptcs/scriptcs) - -## What is it? - -scriptcs makes it easy to write and execute C# with a simple text editor. - -While Visual Studio, and other IDEs, are powerful tools, they can sometimes hinder productivity more than they promote it. You don’t always need, or want, the overhead of a creating a new solution or project. Sometimes you want to just type away in your favorite text editor. - -scriptcs frees you from Visual Studio, without sacrificing the advantages of a strongly-typed language. - -* Write C# in your favorite text editor. -* Use NuGet to manage your dependencies. -* The relaxed C# scripting syntax means you can write and execute an application with only one line of code. -* Script Packs allow you to bootstrap the environment for new scripts, further reduces the amount of code necessary to take advantage of your favorite C# frameworks. - - -## Getting scriptcs - -Releases and nightly builds should be installed using [Chocolatey](http://chocolatey.org/). To install Chocolatey, execute the following command in your command prompt: - - @powershell -NoProfile -ExecutionPolicy Unrestricted -Command "iex ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin - -If the above fails with the error indicating that proxy authentication is required (i.e. [HTTP 407](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8)) then try again with the following on the command prompt that uses your default credentials: - - @powershell -NoProfile -ExecutionPolicy Unrestricted -Command "[Net.WebRequest]::DefaultWebProxy.Credentials = [Net.CredentialCache]::DefaultCredentials; iex ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin - -**Note:** If you are using a version of Chocolatey > 0.9.9.0 you can pass the `-y` into the install and upgrade commands to prevent the confirmation that will appear. - -### Installing scriptcs - -Once Chocolatey has been installed, you can install the latest stable version of scriptcs from your command prompt: - - choco install scriptcs - -Chocolatey will install scriptcs to `%LOCALAPPDATA%\scriptcs\` and update your PATH accordingly. - -**Note:** You may need to restart your command prompt after the installation completes. - -### Staying up-to-date - -With Chocolatey, keeping scriptcs updated is just as easy: - - choco upgrade scriptcs - -**Note:** If you are using a version of Chocolatey < 0.9.0.0 you will need to use `choco update scriptcs`, but also think about updating Chocolatey itself. - -### Nightly builds - -Nightly builds are hosted on [MyGet](https://www.myget.org/), and can also be installed through with Chocolatey: - - choco install scriptcs -pre -source https://www.myget.org/F/scriptcsnightly/ - -### Building from source - -#### Windows - -1. Ensure you have .NET Framework 4.6.1 installed. - -2. Execute the build script. - - `build.cmd` - -#### Linux - -1. Ensure you have [Mono 5.12](https://www.mono-project.com/download/stable/) or later installed. - -2. Execute the build script - - `./build.sh` - -## Getting Started - -### Using the REPL -The scriptcs REPL can be started by running scriptcs without any parameters. The REPL allows you to execute C# statements directly from your command prompt. - -```batchfile -C:\> scriptcs -scriptcs (ctrl-c or blank to exit) - -> var message = "Hello, world!"; -> Console.WriteLine(message); -Hello, world! -> - -C:\> -``` - -REPL supports all C# language constructs (i.e. class definition, method definition), as well as multi-line input. For example: - -```batchfile -C:\> scriptcs -scriptcs (ctrl-c or blank to exit) - -> public class Test { - public string Name { get; set; } - } -> var x = new Test { Name = "Hello" }; -> x -{Name: "Hello"} - -C:\> -``` - -### Writing a script - -* In an empty directory, create a new file named `app.csx`: - -```c# -using Raven.Client; -using Raven.Client.Embedded; -using Raven.Client.Indexes; - -Console.WriteLine("Starting RavenDB server..."); - -using (var documentStore = new EmbeddableDocumentStore { UseEmbeddedHttpServer = true }) -{ - documentStore.Initialize(); - Console.WriteLine($"RavenDB started, listening on http://localhost:{documentStore.Configuration.Port}"); - Console.ReadKey(); -} -``` - -* Install the [RavenDB.Embedded](https://nuget.org/packages/RavenDB.Embedded/) package from NuGet using the [install command](https://github.com/scriptcs/scriptcs/wiki/Package-installation). - -```batchfile -scriptcs -install RavenDB.Embedded -``` - -* Execute your script. Note that listening on a port requires that the command prompt be launched using the **Run as Administrator** option. - -```batchfile -> scriptcs app.csx -INFO : Starting to create execution components -INFO : Starting execution -Starting RavenDB server... -.. snip .. -RavenDB started, listening on http://localhost:8080. -``` - -* Navigating to the URL that Raven is listening on will now bring up the RavenDB management studio. - -### Bootstrap scripts with Script Packs - -Script Packs can be used to further reduce the amount of code you need to write when working with common frameworks. - -* In an empty directory, install the [ScriptCs.WebApi](https://nuget.org/packages/ScriptCs.WebApi/) script pack from NuGet. The script pack automatically imports the Web API namespaces and provides a convenient factory method for initializing the Web API host. It also replaces the default `ControllerResolver` with a custom implementation that allows Web API to discover controllers declared in scripts. - -```batchfile -scriptcs -install ScriptCs.WebApi -``` - -* Script packs can be imported into a script by calling `Require()`. Create a file named `server.csx` that contains the following code: - -```c# -public class TestController : ApiController { - public string Get() { - return "Hello world!"; - } -} - -var webApi = Require(); -var server = webApi.CreateServer("http://localhost:8888"); -server.OpenAsync().Wait(); - -Console.WriteLine("Listening..."); -Console.ReadKey(); -server.CloseAsync().Wait(); -``` - -* In a command prompt running as administrator, execute the `server.csx` file. - -```batchfile -scriptcs server.csx -``` - -* Browse to http://localhost:8888/test/ to see the result of the TestController.Get method. - -```xml -Hello world! -``` - -### Referencing scripts - -* Move the TestController class from the previous example into a new file named `controller.csx` with the following content. - -* On the first line of `server.csx`, reference `controller.csx` using the [#load directive](https://github.com/scriptcs/scriptcs/wiki/Writing-a-script#loading-referenced-scripts). **Note:** #load directives must be placed at the top of a script, otherwise they will be ignored. - -```c# -#load "controller.csx" -``` - -* In a command prompt running as administrator, execute the `server.csx` file. - -```batchfile -scriptcs server.csx -``` - -* Browse to http://localhost:8888/test/ to see the result of the TestController.Get method. - -```xml -Hello world! -``` - - -### Referencing assemblies - -You can reference additional assemblies from the GAC or from the `bin` folder in your script's directory using the [#r directive](https://github.com/scriptcs/scriptcs/wiki/Writing-a-script#referencing-assemblies): - -```c# -#r "nunit.core.dll" -#r "nunit.core.interfaces.dll" - -var path = "UnitTests.dll"; -var runner = TestSetup.GetRunner(new[] {path}); -var result = runner.Run(new ConsoleListener(msg => Console.WriteLine(msg)), TestFilter.Empty, true, LoggingThreshold.All); - -Console.ReadKey(); -``` - -### Debugging - -Instructions for debugging scripts using Visual Studio can be found on the [wiki](https://github.com/scriptcs/scriptcs/wiki/Debugging-a-script). - -### Package installation - -You can install any NuGet packages directly from the scriptcs CLI. This will pull the relevant packages from NuGet, and install them in the scriptcs_packages folder. - -Once the packages are installed, you can simply start using them in your script code directly (just import the namespaces - no additional bootstrapping or DLL referencing is needed). - -The `install` command will also create a `scriptcs_packages.config` file if you don't have one - so that you can easily redistribute your script (without having to copy the package binaries). - - - `scriptcs -install {package name}` will install the desired package from NuGet. - - For example: - - scriptcs -install ServiceStack - - - `scriptcs -install` (without package name) will look for the `scriptcs_packages.config` file located in the current execution directory, and install all the packages specified there. You only need to specify **top level** packages. - -For example, you might create the following `scriptcs_packages.config`: - - - - - - - - -And then just call: - - scriptcs -install - -As a result, all packages specified in the `scriptcs_packages.config`, including all dependencies, will be downloaded and installed in the `scriptcs_packages` folder. - - -## Contributing - -* Read our [Contribution Guidelines](https://github.com/scriptcs/scriptcs/blob/master/CONTRIBUTING.md). - - -## Samples and Documentation - -Additional samples can be contributed to our [samples repository](https://github.com/scriptcs/scriptcs-samples). Documentation can be found on our [wiki](https://github.com/scriptcs/scriptcs/wiki). - - -## Community - -Want to chat? In addition to Twitter, you can find us on [Google Groups](https://groups.google.com/forum/?fromgroups#!forum/scriptcs) and [JabbR](https://jabbr.net/#/rooms/scriptcs)! - - -## Coordinators - -* [Glenn Block](https://github.com/glennblock) ([@gblock](https://twitter.com/intent/user?screen_name=gblock)) -* [Justin Rusbatch](https://github.com/jrusbatch) ([@jrusbatch](https://twitter.com/intent/user?screen_name=jrusbatch)) -* [Filip Wojcieszyn](https://github.com/filipw) ([@filip_woj](https://twitter.com/intent/user?screen_name=filip_woj)) - - -## Core Committers - -* [Damian Schenkelman](http://github.com/dschenkelman) ([@dschenkelman](https://twitter.com/intent/user?screen_name=dschenkelman)) -* [Kristian Hellang](http://github.com/khellang) ([@khellang](https://twitter.com/intent/user?screen_name=khellang)) -* [Adam Ralph](http://github.com/adamralph) ([@adamralph](https://twitter.com/intent/user?screen_name=adamralph)) -* [Paul Bouwer](http://github.com/paulbouwer) ([@pbouwer](https://twitter.com/intent/user?screen_name=pbouwer)) - -## Credits - -* Check out the [list of developers](https://github.com/scriptcs/scriptcs/wiki/Contributors) responsible for getting scriptcs to where it is today! -* Special thanks to Filip Wojcieszyn for being the inspiration behind this with his Roslyn Web API posts. -* Thanks to the Roslyn team who helped point me in the right direction. - - -## License - -[Apache 2 License](https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md) diff --git a/ScriptCs.sln b/ScriptCs.sln deleted file mode 100644 index 756ee82f..00000000 --- a/ScriptCs.sln +++ /dev/null @@ -1,99 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29709.97 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs", "src\ScriptCs\ScriptCs.csproj", "{25080671-1A80-4041-B9C7-260578FF4849}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Contracts", "src\ScriptCs.Contracts\ScriptCs.Contracts.csproj", "{6049E205-8B5F-4080-B023-70600E51FD64}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Tests", "test\ScriptCs.Tests\ScriptCs.Tests.csproj", "{4D6A2A55-BB17-40CB-9567-EAFF80BEFDCE}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{A59C6538-62EA-4BF6-AA00-E0E9A2892D47}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Core", "src\ScriptCs.Core\ScriptCs.Core.csproj", "{E590E710-E159-48E6-A3E6-1A83D3FE732C}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Core.Tests", "test\ScriptCs.Core.Tests\ScriptCs.Core.Tests.csproj", "{AC228213-7356-4F0D-BA48-EBA5FB8A7506}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Engine.Roslyn.Tests", "test\ScriptCs.Engine.Roslyn.Tests\ScriptCs.Engine.Roslyn.Tests.csproj", "{28D11DE5-9F98-4E0A-8CCC-9CDC19110451}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Hosting", "src\ScriptCs.Hosting\ScriptCs.Hosting.csproj", "{9AEF2D95-87FB-4829-B384-34BFE076D531}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Hosting.Tests", "test\ScriptCs.Hosting.Tests\ScriptCs.Hosting.Tests.csproj", "{EC03EAA0-94FD-4145-8F78-5F2E30E3FF9A}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{B138045D-DC04-4A04-A2BA-F771173BCC32}" - ProjectSection(SolutionItems) = preProject - .nuget\packages.config = .nuget\packages.config - EndProjectSection -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Engine.Roslyn", "src\ScriptCs.Engine.Roslyn\ScriptCs.Engine.Roslyn.csproj", "{E79EC231-E27D-4057-91C9-2D001A3A8C3B}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCs.Tests.Acceptance", "test\ScriptCs.Tests.Acceptance\ScriptCs.Tests.Acceptance.csproj", "{10684649-2922-41F5-AB9B-20B127CBF92C}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8C5ABCEF-82F9-4850-B2B8-1D7B73D9C5C3}" - ProjectSection(SolutionItems) = preProject - Directory.Build.props = Directory.Build.props - Directory.Build.targets = Directory.Build.targets - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {25080671-1A80-4041-B9C7-260578FF4849}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {25080671-1A80-4041-B9C7-260578FF4849}.Debug|Any CPU.Build.0 = Debug|Any CPU - {25080671-1A80-4041-B9C7-260578FF4849}.Release|Any CPU.ActiveCfg = Release|Any CPU - {25080671-1A80-4041-B9C7-260578FF4849}.Release|Any CPU.Build.0 = Release|Any CPU - {6049E205-8B5F-4080-B023-70600E51FD64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6049E205-8B5F-4080-B023-70600E51FD64}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6049E205-8B5F-4080-B023-70600E51FD64}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6049E205-8B5F-4080-B023-70600E51FD64}.Release|Any CPU.Build.0 = Release|Any CPU - {4D6A2A55-BB17-40CB-9567-EAFF80BEFDCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4D6A2A55-BB17-40CB-9567-EAFF80BEFDCE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4D6A2A55-BB17-40CB-9567-EAFF80BEFDCE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4D6A2A55-BB17-40CB-9567-EAFF80BEFDCE}.Release|Any CPU.Build.0 = Release|Any CPU - {E590E710-E159-48E6-A3E6-1A83D3FE732C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E590E710-E159-48E6-A3E6-1A83D3FE732C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E590E710-E159-48E6-A3E6-1A83D3FE732C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E590E710-E159-48E6-A3E6-1A83D3FE732C}.Release|Any CPU.Build.0 = Release|Any CPU - {AC228213-7356-4F0D-BA48-EBA5FB8A7506}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AC228213-7356-4F0D-BA48-EBA5FB8A7506}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AC228213-7356-4F0D-BA48-EBA5FB8A7506}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AC228213-7356-4F0D-BA48-EBA5FB8A7506}.Release|Any CPU.Build.0 = Release|Any CPU - {28D11DE5-9F98-4E0A-8CCC-9CDC19110451}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {28D11DE5-9F98-4E0A-8CCC-9CDC19110451}.Debug|Any CPU.Build.0 = Debug|Any CPU - {28D11DE5-9F98-4E0A-8CCC-9CDC19110451}.Release|Any CPU.ActiveCfg = Release|Any CPU - {28D11DE5-9F98-4E0A-8CCC-9CDC19110451}.Release|Any CPU.Build.0 = Release|Any CPU - {9AEF2D95-87FB-4829-B384-34BFE076D531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9AEF2D95-87FB-4829-B384-34BFE076D531}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9AEF2D95-87FB-4829-B384-34BFE076D531}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9AEF2D95-87FB-4829-B384-34BFE076D531}.Release|Any CPU.Build.0 = Release|Any CPU - {EC03EAA0-94FD-4145-8F78-5F2E30E3FF9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EC03EAA0-94FD-4145-8F78-5F2E30E3FF9A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EC03EAA0-94FD-4145-8F78-5F2E30E3FF9A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EC03EAA0-94FD-4145-8F78-5F2E30E3FF9A}.Release|Any CPU.Build.0 = Release|Any CPU - {E79EC231-E27D-4057-91C9-2D001A3A8C3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E79EC231-E27D-4057-91C9-2D001A3A8C3B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E79EC231-E27D-4057-91C9-2D001A3A8C3B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E79EC231-E27D-4057-91C9-2D001A3A8C3B}.Release|Any CPU.Build.0 = Release|Any CPU - {10684649-2922-41F5-AB9B-20B127CBF92C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {10684649-2922-41F5-AB9B-20B127CBF92C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {10684649-2922-41F5-AB9B-20B127CBF92C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {10684649-2922-41F5-AB9B-20B127CBF92C}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {4D6A2A55-BB17-40CB-9567-EAFF80BEFDCE} = {A59C6538-62EA-4BF6-AA00-E0E9A2892D47} - {AC228213-7356-4F0D-BA48-EBA5FB8A7506} = {A59C6538-62EA-4BF6-AA00-E0E9A2892D47} - {28D11DE5-9F98-4E0A-8CCC-9CDC19110451} = {A59C6538-62EA-4BF6-AA00-E0E9A2892D47} - {EC03EAA0-94FD-4145-8F78-5F2E30E3FF9A} = {A59C6538-62EA-4BF6-AA00-E0E9A2892D47} - {10684649-2922-41F5-AB9B-20B127CBF92C} = {A59C6538-62EA-4BF6-AA00-E0E9A2892D47} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {AD80B19D-7299-4CBD-B59F-6DB868FF6A9C} - EndGlobalSection -EndGlobal diff --git a/ScriptCs.sln.DotSettings b/ScriptCs.sln.DotSettings deleted file mode 100644 index 993cd6d0..00000000 --- a/ScriptCs.sln.DotSettings +++ /dev/null @@ -1,2 +0,0 @@ - - <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /> \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 55cad9e8..00000000 --- a/appveyor.yml +++ /dev/null @@ -1,16 +0,0 @@ -version: 1.0.{build} - -image: Visual Studio 2017 - -environment: - DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true - DOTNET_CLI_TELEMETRY_OPTOUT: true - -init: -- git config --global core.autocrlf false - -build_script: -- cmd: - build.cmd - -test: off \ No newline at end of file diff --git a/build.cmd b/build.cmd deleted file mode 100644 index 752c9f63..00000000 --- a/build.cmd +++ /dev/null @@ -1,14 +0,0 @@ -@echo off -cls - -.paket\paket.bootstrapper.exe -if errorlevel 1 ( - exit /b %errorlevel% -) - -.paket\paket.exe restore -if errorlevel 1 ( - exit /b %errorlevel% -) - -packages\FAKE\tools\FAKE.exe build.fsx %* \ No newline at end of file diff --git a/build.fsx b/build.fsx deleted file mode 100644 index 51ba6692..00000000 --- a/build.fsx +++ /dev/null @@ -1,70 +0,0 @@ -#r "packages/FAKE/tools/FakeLib.dll" - -open Fake -open Fake.DotNetCli -open Fake.Testing -open System.IO - -Target "Clean" (fun _ -> - !! "artifacts" ++ "src/*/bin" ++ "test/*/bin" ++ "src/*/obj" ++ "test/*/obj" - |> DeleteDirs -) - -Target "Build" (fun _ -> - DotNetCli.Restore id - - "ScriptCs.sln" - |> MSBuildHelper.build (fun p -> - { p with - RestorePackagesFlag = true - Verbosity = Some Minimal - Targets = [ "Build" ] - Properties = - [ - "Optimize", "True" - "Configuration", "Release" - ] - } ) -) - -Target "Test" (fun _ -> -#if MONO - !! "test/**/bin/**/*Tests.Acceptance.dll" - |> xUnit2 (fun c -> - {c with - MaxThreads = CollectionConcurrencyMode.MaxThreads 1 - }) -#else - !! "test/**/*Tests*.csproj" - |> Seq.iter (fun p -> - DotNetCli.Test (fun c -> - {c with - WorkingDir = Path.GetDirectoryName p - AdditionalArgs = ["--no-build"] - }) - ) -#endif -) - -Target "Pack" (fun _ -> - "ScriptCs.sln" - |> MSBuildHelper.build (fun p -> - { p with - RestorePackagesFlag = true - Verbosity = Some Minimal - Targets = [ "Pack" ] - Properties = - [ - "Optimize", "True" - "Configuration", "Release" - "PackageOutputPath", "../../artifacts" - ] - } ) -) - -"Clean" - ==> "Build" - ==> "Test" - ==> "Pack" - -RunTargetOrDefault "Pack" \ No newline at end of file diff --git a/build.sh b/build.sh deleted file mode 100755 index fa37cd79..00000000 --- a/build.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -if test "$OS" = "Windows_NT" -then - # use .Net - - .paket/paket.bootstrapper.exe prerelease - exit_code=$? - if [ $exit_code -ne 0 ]; then - exit $exit_code - fi - - .paket/paket.exe restore - exit_code=$? - if [ $exit_code -ne 0 ]; then - exit $exit_code - fi - - packages/FAKE/tools/FAKE.exe $@ --nocache --fsiargs build.fsx -else - # use mono - mono .paket/paket.bootstrapper.exe prerelease - exit_code=$? - if [ $exit_code -ne 0 ]; then - exit $exit_code - fi - - mono .paket/paket.exe restore - exit_code=$? - if [ $exit_code -ne 0 ]; then - exit $exit_code - fi - mono packages/FAKE/tools/FAKE.exe $@ --nocache --fsiargs -d:MONO build.fsx -fi \ No newline at end of file diff --git a/build_brew.sh b/build_brew.sh deleted file mode 100755 index b74b80e4..00000000 --- a/build_brew.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash -set -e -set -o pipefail -set -x - -# install -mozroots --import --sync --quiet -mono ./.nuget/NuGet.exe restore ./ScriptCs.sln - -# script -mkdir -p artifacts/Release/bin -msbuild ./ScriptCs.sln /property:Configuration=Release /nologo /verbosity:normal -cp src/ScriptCs/bin/Release/net461/* artifacts/Release/bin/ diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 00000000..4dc6f8b2 Binary files /dev/null and b/favicon.ico differ diff --git a/images/logo.png b/images/logo.png new file mode 100644 index 00000000..9c94b816 Binary files /dev/null and b/images/logo.png differ diff --git a/images/logo@2x.png b/images/logo@2x.png new file mode 100644 index 00000000..a10214fc Binary files /dev/null and b/images/logo@2x.png differ diff --git a/images/scriptcs_katacoda.gif b/images/scriptcs_katacoda.gif new file mode 100644 index 00000000..818b468d Binary files /dev/null and b/images/scriptcs_katacoda.gif differ diff --git a/index.html b/index.html new file mode 100644 index 00000000..aff0924c --- /dev/null +++ b/index.html @@ -0,0 +1,268 @@ + + + + + + scriptcs - Write C# scripts in your favorite text editor + + + + + + + + + +
+
+

+ + + +

+

Unleash your C# from Visual Studio.

+ + +
+
+

What is it?

+ +

scriptcs makes it easy to write and execute C# with a simple text editor.

+ +

While Visual Studio, and other IDEs, are powerful tools, they can sometimes hinder productivity more than they promote it. You don’t always need, or want, the overhead of a creating a new solution or project. Sometimes you want to just type away in your favorite text editor.

+ +

scriptcs frees you from Visual Studio, without sacrificing the advantages of a strongly-typed language.

+ +
    +
  • Write C# in your favorite text editor.
  • +
  • Use NuGet to manage your dependencies.
  • +
  • The relaxed C# scripting syntax means you can write and execute an application with only one line of code.
  • +
  • Script Packs allow you to bootstrap the environment for new scripts, further reduces the amount of code necessary to take advantage of your favorite C# frameworks.
  • +
+ +

Try scriptcs

+

You can try scriptcs right now in the browser using Kata<oda. Click the image below to get started! +

+ +scriptcs_katacoda + + +

Getting scriptcs

+ +

Releases and nightly builds should be installed using Chocolatey. Information on installing Chocolatey is available at their website.

+ +

Installing scriptcs

+ +

Once Chocolatey has been installed, you can install the latest stable version of scriptcs from your command prompt:

+ +
cinst scriptcs
+
+ +

Chocolatey will install scriptcs to %APPDATA%\scriptcs\ and update your PATH accordingly.

+ +

Note: You may need to restart your command prompt after the installation completes.

+ +

Staying up-to-date

+ +

With Chocolatey, keeping scriptcs updated is just as easy:

+ +
cup scriptcs
+
+ +

Nightly builds

+ +

Nightly builds are hosted on MyGet, and can also be installed through with Chocolatey:

+ +
cinst scriptcs -pre -source https://www.myget.org/F/scriptcsnightly/ 
+
+ +

Building from source

+ +

Execute build.cmd to start the build script.

+ +

Getting Started

+ +

Using the REPL

+ +

The scriptcs REPL can be started by running scriptcs without any parameters. The REPL allows you to execute C# statements directly from your command prompt.

+ +
C:\> scriptcs
+scriptcs (ctrl-c or blank to exit)
+
+> var message = "Hello, world!";
+> Console.WriteLine(message);
+Hello, world!
+> 
+
+C:\>
+
+ +

Writing a script

+ +
    +
  • In an empty directory, create a new file named app.csx:
  • +
using Raven.Client;
+using Raven.Client.Embedded;
+using Raven.Client.Indexes;
+
+Console.WriteLine("Starting RavenDB server...");
+
+EmbeddableDocumentStore documentStore = null;
+try
+{
+    documentStore = new EmbeddableDocumentStore { UseEmbeddedHttpServer = true };
+    documentStore.Initialize();
+
+    var url = string.Format("http://localhost:{0}", documentStore.Configuration.Port);
+    Console.WriteLine("RavenDB started, listening on {0}.", url);
+
+    Console.ReadKey();
+}
+finally
+{
+    if (documentStore != null)
+        documentStore.Dispose();
+}
+
+ +
scriptcs -install RavenDB.Embedded
+
+ +
    +
  • Execute your script. Note that listening on a port requires that the command prompt be launched using the Run as Administrator option.
  • +
> scriptcs app.csx
+INFO : Starting to create execution components
+INFO : Starting execution
+Starting RavenDB server...
+.. snip ..
+RavenDB started, listening on http://localhost:8080.
+
+ +
    +
  • Navigating to the URL that Raven is listening on will now bring up the RavenDB management studio.
  • +

Bootstrap scripts with Script Packs

+ +

Script Packs can be used to further reduce the amount of code you need to write when working with common frameworks.

+ +
    +
  • In an empty directory, install the ScriptCs.WebApi script pack from NuGet. The script pack will automatically imports the Web API namespaces and provides a convenient factory method for initializing the Web API host. It also replaces the default ControllerResolver with a custom implementation that allows Web API to discover controllers declared in scripts.
  • +
scriptcs -install ScriptCs.WebApi
+
+ +
    +
  • Script packs can be imported into a script by calling Require<TScriptPack>(). Create a file named server.csx that contains the following code:
  • +
public class TestController : ApiController {
+    public string Get() {
+        return "Hello world!";
+    }
+}
+
+var webApi = Require<WebApi>();
+var server = webApi.CreateServer("http://localhost:8888");
+server.OpenAsync().Wait();
+
+Console.WriteLine("Listening...");
+Console.ReadKey();
+server.CloseAsync().Wait();
+
+ +
    +
  • In a command prompt running as administrator, execute the server.csx file.
  • +
scriptcs server.csx 
+
+ +
    +
  • Browse to http://localhost:8888/test/ to see the result of the TestController.Get method.
  • +
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello world!</string>
+
+ +

Referencing scripts

+ +
    +
  • Move the TestController class from the previous example into a new file named controller.csx with the following content.

  • +
  • On the first line of server.csx, reference controller.csx using the #load directive. Note: #load directives must be placed at the top of a script, otherwise they will be ignored.

  • +
#load "controller.csx"
+
+ +
    +
  • In a command prompt running as administrator, execute the server.csx file.
  • +
scriptcs server.csx 
+
+ +
    +
  • Browse to http://localhost:8888/test/ to see the result of the TestController.Get method.
  • +
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello world!</string>
+
+ +

Referencing assemblies

+ +

You can reference additional assemblies from the GAC or from the bin folder in your script's directory using the #r directive:

+ +
#r "nunit.core.dll"
+#r "nunit.core.interfaces.dll"
+
+var path = "UnitTests.dll";
+var runner = TestSetup.GetRunner(new[] {path});
+var result = runner.Run(new ConsoleListener(msg => Console.WriteLine(msg)), TestFilter.Empty, true,     LoggingThreshold.All);
+
+Console.ReadKey();
+
+ +

Contributing

+ +

Samples and Documentation

+ +

Additional samples can be contributed to our samples repository. Documentation can be found on our wiki.

+ +

Community

+ +

Want to chat? In addition to Twitter, you can find us on Google Groups and JabbR!

+ +

Coordinators

+ +

Credits

+ +
    +
  • Check out the list of developers responsible for getting scriptcs to where it is today!
  • +
  • Special thanks to Filip Wojcieszyn for being the inspiration behind this with his Roslyn Web API posts.
  • +
  • Thanks to the Roslyn team who helped point me in the right direction.
  • +

License

+ +

Apache 2 License

+
+
+

This project is maintained by the community.

+

Hosted on GitHub Pages — Theme by orderedlist

+
+
+ + + + diff --git a/javascripts/scale.fix.js b/javascripts/scale.fix.js new file mode 100644 index 00000000..87a40ca7 --- /dev/null +++ b/javascripts/scale.fix.js @@ -0,0 +1,17 @@ +var metas = document.getElementsByTagName('meta'); +var i; +if (navigator.userAgent.match(/iPhone/i)) { + for (i=0; i scriptcs\r\nscriptcs (ctrl-c or blank to exit)\r\n\r\n> var message = \"Hello, world!\";\r\n> Console.WriteLine(message);\r\nHello, world!\r\n> \r\n\r\nC:\\>\r\n```\r\n\r\n### Writing a script\r\n\r\n* In an empty directory, create a new file named `app.csx`:\r\n\r\n```c#\r\nusing Raven.Client;\r\nusing Raven.Client.Embedded;\r\nusing Raven.Client.Indexes;\r\n\r\nConsole.WriteLine(\"Starting RavenDB server...\");\r\n\r\nEmbeddableDocumentStore documentStore = null;\r\ntry\r\n{\r\n documentStore = new EmbeddableDocumentStore { UseEmbeddedHttpServer = true };\r\n documentStore.Initialize();\r\n\r\n var url = string.Format(\"http://localhost:{0}\", documentStore.Configuration.Port);\r\n Console.WriteLine(\"RavenDB started, listening on {0}.\", url);\r\n\r\n Console.ReadKey();\r\n}\r\nfinally\r\n{\r\n if (documentStore != null)\r\n documentStore.Dispose();\r\n}\r\n```\r\n\r\n* Install the [RavenDB.Embedded](https://nuget.org/packages/RavenDB.Embedded/) package from NuGet using the [install command](https://github.com/scriptcs/scriptcs/wiki/Package-installation).\r\n\r\n```batchfile\r\nscriptcs -install RavenDB.Embedded\r\n```\r\n\r\n* Execute your script. Note that listening on a port requires that the command prompt be launched using the **Run as Administrator** option.\r\n\r\n```batchfile\r\n> scriptcs app.csx\r\nINFO : Starting to create execution components\r\nINFO : Starting execution\r\nStarting RavenDB server...\r\n.. snip ..\r\nRavenDB started, listening on http://localhost:8080.\r\n```\r\n\r\n* Navigating to the URL that Raven is listening on will now bring up the RavenDB management studio.\r\n\r\n### Bootstrap scripts with Script Packs\r\n\r\nScript Packs can be used to further reduce the amount of code you need to write when working with common frameworks. \r\n\r\n* In an empty directory, install the [ScriptCs.WebApi](https://nuget.org/packages/ScriptCs.WebApi/) script pack from NuGet. The script pack will automatically imports the Web API namespaces and provides a convenient factory method for initializing the Web API host. It also replaces the default `ControllerResolver` with a custom implementation that allows Web API to discover controllers declared in scripts.\r\n\r\n```batchfile\r\nscriptcs -install ScriptCs.WebApi\r\n```\r\n\r\n* Script packs can be imported into a script by calling `Require()`. Create a file named `server.csx` that contains the following code:\r\n\r\n```c#\r\npublic class TestController : ApiController {\r\n public string Get() {\r\n return \"Hello world!\";\r\n }\r\n}\r\n\r\nvar webApi = Require();\r\nvar server = webApi.CreateServer(\"http://localhost:8888\");\r\nserver.OpenAsync().Wait();\r\n\r\nConsole.WriteLine(\"Listening...\");\r\nConsole.ReadKey();\r\nserver.CloseAsync().Wait();\r\n```\r\n\r\n* In a command prompt running as administrator, execute the `server.csx` file.\r\n\r\n```batchfile\r\nscriptcs server.csx \r\n```\r\n\r\n* Browse to http://localhost:8888/test/ to see the result of the TestController.Get method.\r\n\r\n```xml\r\nHello world!\r\n```\r\n\r\n### Referencing scripts\r\n\r\n* Move the TestController class from the previous example into a new file named `controller.csx` with the following content.\r\n\r\n* On the first line of `server.csx`, reference `controller.csx` using the [#load directive](https://github.com/scriptcs/scriptcs/wiki/Writing-a-script#loading-referenced-scripts). **Note:** #load directives must be placed at the top of a script, otherwise they will be ignored.\r\n\r\n```c#\r\n#load \"controller.csx\"\r\n```\r\n\r\n* In a command prompt running as administrator, execute the `server.csx` file.\r\n\r\n```batchfile\r\nscriptcs server.csx \r\n```\r\n\r\n* Browse to http://localhost:8888/test/ to see the result of the TestController.Get method.\r\n\r\n```xml\r\nHello world!\r\n```\r\n\r\n\r\n### Referencing assemblies\r\n\r\nYou can reference additional assemblies from the GAC or from the bin folder in your script's directory using the [#r directive](https://github.com/scriptcs/scriptcs/wiki/Writing-a-script#referencing-assemblies):\r\n\r\n```c#\r\n#r \"nunit.core.dll\"\r\n#r \"nunit.core.interfaces.dll\"\r\n\r\nvar path = \"UnitTests.dll\";\r\nvar runner = TestSetup.GetRunner(new[] {path});\r\nvar result = runner.Run(new ConsoleListener(msg => Console.WriteLine(msg)), TestFilter.Empty, true, LoggingThreshold.All);\r\n\r\nConsole.ReadKey();\r\n```\r\n\r\n\r\n## Contributing\r\n\r\n* Read our [Contribution Guidelines](https://github.com/scriptcs/scriptcs/blob/master/CONTRIBUTING.md). \r\n\r\n\r\n## Samples and Documentation\r\n\r\nAdditional samples can be contributed to our [samples repository](https://github.com/scriptcs/scriptcs-samples). Documentation can be found on our [wiki](https://github.com/scriptcs/scriptcs/wiki). \r\n\r\n\r\n## Community\r\n\r\nWant to chat? In addition to Twitter, you can find us on [Google Groups](https://groups.google.com/forum/?fromgroups#!forum/scriptcs) and [JabbR](https://jabbr.net/#/rooms/scriptcs)!\r\n\r\n\r\n## Coordinators\r\n\r\n* [Glenn Block](https://github.com/glennblock) ([@gblock](https://twitter.com/intent/user?screen_name=gblock))\r\n* [Justin Rusbatch](https://github.com/jrusbatch) ([@jrusbatch](https://twitter.com/intent/user?screen_name=jrusbatch))\r\n* [Filip Wojcieszyn](https://github.com/filipw) ([@filip_woj](https://twitter.com/intent/user?screen_name=filip_woj))\r\n\r\n\r\n## Credits \r\n\r\n* Check out the [list of developers](https://github.com/scriptcs/scriptcs/wiki/Contributors) responsible for getting scriptcs to where it is today! \r\n* Special thanks to Filip Wojcieszyn for being the inspiration behind this with his Roslyn Web API posts.\r\n* Thanks to the Roslyn team who helped point me in the right direction.\r\n\r\n\r\n## License \r\n\r\n[Apache 2 License](https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md)\r\n","google":"UA-38995388-1","note":"Don't delete this file! It's used internally to help with page regeneration."} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/AssemblyReferences.cs b/src/ScriptCs.Contracts/AssemblyReferences.cs deleted file mode 100644 index 734bcea7..00000000 --- a/src/ScriptCs.Contracts/AssemblyReferences.cs +++ /dev/null @@ -1,110 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System.Collections.Generic; - using System.Linq; - using System.Reflection; - using System; - using System.IO; - - public class AssemblyReferences - { - private readonly Dictionary _assemblies = new Dictionary(); - private readonly Dictionary _paths = new Dictionary(); - - public AssemblyReferences() - : this(Enumerable.Empty()) - { - } - - public AssemblyReferences(IEnumerable assemblies) - : this(assemblies, Enumerable.Empty()) - { - } - - public AssemblyReferences(IEnumerable paths) - : this(Enumerable.Empty(), paths) - { - } - - public AssemblyReferences(IEnumerable assemblies, IEnumerable paths) - { - Guard.AgainstNullArgument("paths", paths); - Guard.AgainstNullArgument("assemblies", assemblies); - - foreach (var assembly in assemblies.Where(assembly => assembly != null)) - { - var name = assembly.GetName().Name; - if (!_assemblies.ContainsKey(name)) - { - _assemblies.Add(name, assembly); - } - } - - foreach (var path in paths) - { - var name = Path.GetFileName(path); - if (name == null) - { - continue; - } - - if (name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || - name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) - { - name = Path.GetFileNameWithoutExtension(name); - } - - if (!_paths.ContainsKey(name) && !_assemblies.ContainsKey(name)) - { - _paths.Add(name, path); - } - } - } - - public IEnumerable Assemblies => _assemblies.Values.ToArray(); - - public IEnumerable Paths => _paths.Values.ToArray(); - - public AssemblyReferences Union(AssemblyReferences references) - { - Guard.AgainstNullArgument("references", references); - - return new AssemblyReferences(Assemblies.Union(references.Assemblies), Paths.Union(references.Paths)); - } - - public AssemblyReferences Union(IEnumerable assemblies) - { - Guard.AgainstNullArgument("assemblies", assemblies); - - return new AssemblyReferences(Assemblies.Union(assemblies), Paths); - } - - public AssemblyReferences Union(IEnumerable paths) - { - Guard.AgainstNullArgument("paths", paths); - - return new AssemblyReferences(Assemblies, Paths.Union(paths)); - } - - public AssemblyReferences Except(AssemblyReferences references) - { - Guard.AgainstNullArgument("references", references); - - return new AssemblyReferences(Assemblies.Except(references.Assemblies), Paths.Except(references.Paths)); - } - - public AssemblyReferences Except(IEnumerable assemblies) - { - Guard.AgainstNullArgument("assemblies", assemblies); - - return new AssemblyReferences(Assemblies.Except(assemblies), Paths); - } - - public AssemblyReferences Except(IEnumerable paths) - { - Guard.AgainstNullArgument("paths", paths); - - return new AssemblyReferences(Assemblies, Paths.Except(paths)); - } - } -} diff --git a/src/ScriptCs.Contracts/BehaviorAfterCode.cs b/src/ScriptCs.Contracts/BehaviorAfterCode.cs deleted file mode 100644 index f8007dbd..00000000 --- a/src/ScriptCs.Contracts/BehaviorAfterCode.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ScriptCs.Contracts -{ - public enum BehaviorAfterCode - { - Allow, - Ignore, - Throw - } -} diff --git a/src/ScriptCs.Contracts/DefaultLogProvider.cs b/src/ScriptCs.Contracts/DefaultLogProvider.cs deleted file mode 100644 index 202dae56..00000000 --- a/src/ScriptCs.Contracts/DefaultLogProvider.cs +++ /dev/null @@ -1,76 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System; - - public class DefaultLogProvider : ILogProvider - { - private readonly ILogProvider _provider = ResolveLogProvider() ?? new NullLogProvider(); - - [Obsolete("Should not be called directly. Instead, call a method on LogProviderExtensions.")] - public Logger GetLogger(string name) => _provider.GetLogger(name); - - /// - /// Opens a nested diagnostics context. Not supported in . - /// - /// The message to add to the diagnostics context. - /// A disposable that when disposed removes the message from the context. - public IDisposable OpenNestedContext(string message) => _provider.OpenNestedContext(message); - - /// - /// Opens a mapped diagnostics context. Not supported in . - /// - /// A key. - /// A value. - /// A disposable that when disposed removes the map from the context. - public IDisposable OpenMappedContext(string key, string value) => _provider.OpenMappedContext(key, value); - - private static ILogProvider ResolveLogProvider() - { - var libLogProvider = LibLog.LogProvider.ResolveLogProvider(); - return libLogProvider == null ? null : new LibLogAdapter(libLogProvider); - } - - private class NullLogProvider : ILogProvider - { - private static readonly Logger logger = (_, __, ___, ____) => false; - private static readonly Disposable disposable = new Disposable(); - - [Obsolete("Should not be called directly. Instead, call a method on LogProviderExtensions.")] - public Logger GetLogger(string name) - { - return logger; - } - - public IDisposable OpenNestedContext(string message) => disposable; - - public IDisposable OpenMappedContext(string key, string value) => disposable; - - private sealed class Disposable : IDisposable - { - public void Dispose() - { - } - } - } - - private class LibLogAdapter : ILogProvider - { - private readonly LibLog.ILogProvider _libLogProvider; - - public LibLogAdapter(LibLog.ILogProvider libLogProvider) - { - _libLogProvider = libLogProvider; - } - - public Logger GetLogger(string name) - { - return (logLevel, messageFunc, exception, formatParameters) => - _libLogProvider.GetLogger(name)((LibLog.LogLevel)logLevel, messageFunc, exception, formatParameters); - } - - public IDisposable OpenNestedContext(string message) => _libLogProvider.OpenNestedContext(message); - - public IDisposable OpenMappedContext(string key, string value) => _libLogProvider.OpenMappedContext(key, value); - } - } -} diff --git a/src/ScriptCs.Contracts/DirectiveLineProcessor.cs b/src/ScriptCs.Contracts/DirectiveLineProcessor.cs deleted file mode 100644 index 9494a54b..00000000 --- a/src/ScriptCs.Contracts/DirectiveLineProcessor.cs +++ /dev/null @@ -1,55 +0,0 @@ -using ScriptCs.Contracts.Exceptions; - -namespace ScriptCs.Contracts -{ - public abstract class DirectiveLineProcessor : IDirectiveLineProcessor - { - protected virtual BehaviorAfterCode BehaviorAfterCode => BehaviorAfterCode.Ignore; - - protected abstract string DirectiveName { get; } - - private string DirectiveString => string.Format("#{0}", DirectiveName); - - public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode) - { - if (!Matches(line)) - { - return false; - } - - if (!isBeforeCode) - { - if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Throw) - { - throw new InvalidDirectiveUseException(string.Format("Encountered directive '{0}' after the start of code. Please move this directive to the beginning of the file.", DirectiveString)); - } - if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Ignore) - { - return true; - } - } - - return ProcessLine(parser, context, line); - } - - protected string GetDirectiveArgument(string line) - { - Guard.AgainstNullArgument("line", line); - - return line.Replace(DirectiveString, string.Empty) - .Trim() - .Replace("\"", string.Empty) - .Replace(";", string.Empty); - } - - protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line); - - public bool Matches(string line) - { - Guard.AgainstNullArgument("line", line); - - var tokens = line.Split(); - return tokens[0] == DirectiveString; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/Exceptions/InvalidDirectiveUseException.cs b/src/ScriptCs.Contracts/Exceptions/InvalidDirectiveUseException.cs deleted file mode 100644 index 06507aaa..00000000 --- a/src/ScriptCs.Contracts/Exceptions/InvalidDirectiveUseException.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace ScriptCs.Contracts.Exceptions -{ - [Serializable] - public class InvalidDirectiveUseException : Exception - { - public InvalidDirectiveUseException(string message) - : base(message) - { - } - - public InvalidDirectiveUseException(string message, Exception innerException) - : base(message, innerException) - { - } - - protected InvalidDirectiveUseException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} diff --git a/src/ScriptCs.Contracts/FileParserContext.cs b/src/ScriptCs.Contracts/FileParserContext.cs deleted file mode 100644 index adc754d8..00000000 --- a/src/ScriptCs.Contracts/FileParserContext.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public class FileParserContext - { - public FileParserContext() - { - Namespaces = new List(); - References = new List(); - LoadedScripts = new List(); - BodyLines = new List(); - } - - public List Namespaces { get; private set; } - - public List References { get; private set; } - - public List LoadedScripts { get; private set; } - - public List BodyLines { get; private set; } - - public string ScriptPath { get; set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/FilePreProcessorResult.cs b/src/ScriptCs.Contracts/FilePreProcessorResult.cs deleted file mode 100644 index 2b664edc..00000000 --- a/src/ScriptCs.Contracts/FilePreProcessorResult.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public class FilePreProcessorResult - { - public FilePreProcessorResult() - { - Namespaces = new List(); - LoadedScripts = new List(); - References = new List(); - } - - public List Namespaces { get; set; } - - public List LoadedScripts { get; set; } - - public List References { get; set; } - - public string ScriptPath { get; set; } - - public string Code { get; set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/FileSystemExtensions.cs b/src/ScriptCs.Contracts/FileSystemExtensions.cs deleted file mode 100644 index 765d8e9f..00000000 --- a/src/ScriptCs.Contracts/FileSystemExtensions.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace ScriptCs.Contracts -{ - public static class FileSystemExtensions - { - public static IEnumerable EnumerateBinaries( - this IFileSystem fileSystem, string path, SearchOption searchOption = SearchOption.AllDirectories) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - - return fileSystem.EnumerateFiles(path, "*.dll", searchOption) - .Union(fileSystem.EnumerateFiles(path, "*.exe", searchOption)) - .Where(f=>!f.Equals("scriptcs.exe", StringComparison.InvariantCultureIgnoreCase)); - } - } -} diff --git a/src/ScriptCs.Contracts/IAppDomainAssemblyResolver.cs b/src/ScriptCs.Contracts/IAppDomainAssemblyResolver.cs deleted file mode 100644 index ff805473..00000000 --- a/src/ScriptCs.Contracts/IAppDomainAssemblyResolver.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IAppDomainAssemblyResolver - { - void Initialize(); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IAssemblyResolver.cs b/src/ScriptCs.Contracts/IAssemblyResolver.cs deleted file mode 100644 index cc3e5320..00000000 --- a/src/ScriptCs.Contracts/IAssemblyResolver.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IAssemblyResolver - { - IEnumerable GetAssemblyPaths(string path, bool binariesOnly = false); - } -} diff --git a/src/ScriptCs.Contracts/IAssemblyUtility.cs b/src/ScriptCs.Contracts/IAssemblyUtility.cs deleted file mode 100644 index 8d545730..00000000 --- a/src/ScriptCs.Contracts/IAssemblyUtility.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Reflection; - -namespace ScriptCs.Contracts -{ - public interface IAssemblyUtility - { - bool IsManagedAssembly(string path); - Assembly LoadFile(string path); - Assembly Load(AssemblyName assemblyRef); - AssemblyName GetAssemblyName(string path); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IConsole.cs b/src/ScriptCs.Contracts/IConsole.cs deleted file mode 100644 index 2ff69cdc..00000000 --- a/src/ScriptCs.Contracts/IConsole.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace ScriptCs.Contracts -{ - public interface IConsole - { - void Write(string value); - - void WriteLine(); - - void WriteLine(string value); - - string ReadLine(string prompt); - - void Clear(); - - void Exit(); - - void ResetColor(); - - ConsoleColor ForegroundColor { get; set; } - - int Width { get; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IDirectiveLineProcessor.cs b/src/ScriptCs.Contracts/IDirectiveLineProcessor.cs deleted file mode 100644 index 41331ec3..00000000 --- a/src/ScriptCs.Contracts/IDirectiveLineProcessor.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IDirectiveLineProcessor : ILineProcessor - { - bool Matches(string line); - } -} diff --git a/src/ScriptCs.Contracts/IFileParser.cs b/src/ScriptCs.Contracts/IFileParser.cs deleted file mode 100644 index 9db65cc0..00000000 --- a/src/ScriptCs.Contracts/IFileParser.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IFileParser - { - void ParseFile(string path, FileParserContext context); - - void ParseScript(List scriptLines, FileParserContext context); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IFilePreProcessor.cs b/src/ScriptCs.Contracts/IFilePreProcessor.cs deleted file mode 100644 index 37ce7b72..00000000 --- a/src/ScriptCs.Contracts/IFilePreProcessor.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IFilePreProcessor : IFileParser - { - FilePreProcessorResult ProcessFile(string path); - - FilePreProcessorResult ProcessScript(string script); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IFileSystem.cs b/src/ScriptCs.Contracts/IFileSystem.cs deleted file mode 100644 index 0f0fb352..00000000 --- a/src/ScriptCs.Contracts/IFileSystem.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace ScriptCs.Contracts -{ - public interface IFileSystem - { - IEnumerable EnumerateFiles( - string dir, string search, SearchOption searchOption = SearchOption.AllDirectories); - - IEnumerable EnumerateDirectories( - string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories); - - IEnumerable EnumerateFilesAndDirectories( - string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories); - - void Copy(string source, string dest, bool overwrite); - - void CopyDirectory(string source, string dest, bool overwrite); - - bool DirectoryExists(string path); - - void CreateDirectory(string path, bool hidden = false); - - void DeleteDirectory(string path); - - string ReadFile(string path); - - string[] ReadFileLines(string path); - - DateTime GetLastWriteTime(string file); - - bool IsPathRooted(string path); - - string GetFullPath(string path); - - string TempPath { get; } - - string CurrentDirectory { get; set; } - - string NewLine { get; } - - string GetWorkingDirectory(string path); - - void Move(string source, string dest); - - void MoveDirectory(string source, string dest); - - bool FileExists(string path); - - void FileDelete(string path); - - IEnumerable SplitLines(string value); - - void WriteToFile(string path, string text); - - Stream CreateFileStream(string filePath, FileMode mode); - - void WriteAllBytes(string filePath, byte[] bytes); - - string GlobalFolder { get; } - - string HostBin { get; } - - string BinFolder { get; } - - string DllCacheFolder { get; } - - string PackagesFile { get; } - - string PackagesFolder { get; } - - string NugetFile { get; } - - string GlobalOptsFile { get; } - } -} diff --git a/src/ScriptCs.Contracts/IInstallationProvider.cs b/src/ScriptCs.Contracts/IInstallationProvider.cs deleted file mode 100644 index db019ba9..00000000 --- a/src/ScriptCs.Contracts/IInstallationProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IInstallationProvider - { - IEnumerable GetRepositorySources(string path); - void Initialize(); - bool IsInstalled(IPackageReference packageId, bool allowPreRelease = false); - void InstallPackage(IPackageReference packageId, bool allowPreRelease = false); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/ILineProcessor.cs b/src/ScriptCs.Contracts/ILineProcessor.cs deleted file mode 100644 index 30e9abbd..00000000 --- a/src/ScriptCs.Contracts/ILineProcessor.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface ILineProcessor - { - bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/ILog.cs b/src/ScriptCs.Contracts/ILog.cs deleted file mode 100644 index 795e61b2..00000000 --- a/src/ScriptCs.Contracts/ILog.cs +++ /dev/null @@ -1,36 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System; - - public interface ILog - { - /// - /// Logs a message for a specified log level, if the is enabled for that log level. - /// - /// The log level. - /// - /// Optional function which creates the message. - /// Specify null to simply check if the logger is enabled for the . - /// - /// An optional exception to include with the message. - /// - /// Optional arguments for formatting the message created by . - /// - /// - /// true if the is enabled for the . - /// Otherwise false. - /// - /// - /// Note to implementers for optimal performance: - /// When the is null, simply return a - /// indicating whether the is enabled for the specified . - /// When the is not null, it should only be invoked - /// if the is enabled for the specified . - /// - bool Log( - LogLevel logLevel, - Func createMessage = null, - Exception exception = null, - params object[] formatArgs); - } -} diff --git a/src/ScriptCs.Contracts/ILogProvider.cs b/src/ScriptCs.Contracts/ILogProvider.cs deleted file mode 100644 index 17946070..00000000 --- a/src/ScriptCs.Contracts/ILogProvider.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System; - - public interface ILogProvider - { - /// - /// Gets the specified named logger. - /// - /// Name of the logger. - /// The logger reference. - /// - /// Do not call this method directly. Instead, call a method on . - /// - Logger GetLogger(string name); - - IDisposable OpenNestedContext(string message); - - IDisposable OpenMappedContext(string key, string value); - } -} diff --git a/src/ScriptCs.Contracts/IModule.cs b/src/ScriptCs.Contracts/IModule.cs deleted file mode 100644 index 8077bf68..00000000 --- a/src/ScriptCs.Contracts/IModule.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IModule - { - void Initialize(IModuleConfiguration config); - } -} diff --git a/src/ScriptCs.Contracts/IModuleConfiguration.cs b/src/ScriptCs.Contracts/IModuleConfiguration.cs deleted file mode 100644 index 49a31364..00000000 --- a/src/ScriptCs.Contracts/IModuleConfiguration.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IModuleConfiguration : IServiceOverrides - { - bool Cache { get; } - - string ScriptName { get; } - - bool IsRepl { get; } - - LogLevel LogLevel { get; } - - bool Debug { get; } - - IDictionary Overrides { get; } - } -} diff --git a/src/ScriptCs.Contracts/IModuleMetadata.cs b/src/ScriptCs.Contracts/IModuleMetadata.cs deleted file mode 100644 index 3ebb5b85..00000000 --- a/src/ScriptCs.Contracts/IModuleMetadata.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IModuleMetadata - { - string Name { get; } - - string Extensions { get; } - - bool Autoload { get; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IObjectSerializer.cs b/src/ScriptCs.Contracts/IObjectSerializer.cs deleted file mode 100644 index 10c3d784..00000000 --- a/src/ScriptCs.Contracts/IObjectSerializer.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IObjectSerializer - { - string Serialize(object value); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IPackageAssemblyResolver.cs b/src/ScriptCs.Contracts/IPackageAssemblyResolver.cs deleted file mode 100644 index a953c4b7..00000000 --- a/src/ScriptCs.Contracts/IPackageAssemblyResolver.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IPackageAssemblyResolver - { - void SavePackages(); - IEnumerable GetPackages(string workingDirectory); - IEnumerable GetAssemblyNames(string workingDirectory); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IPackageContainer.cs b/src/ScriptCs.Contracts/IPackageContainer.cs deleted file mode 100644 index df9202e0..00000000 --- a/src/ScriptCs.Contracts/IPackageContainer.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IPackageContainer - { - void CreatePackageFile(); - - IEnumerable FindReferences(string path); - - IPackageObject FindPackage(string path, IPackageReference packageReference); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IPackageInstaller.cs b/src/ScriptCs.Contracts/IPackageInstaller.cs deleted file mode 100644 index b020a50b..00000000 --- a/src/ScriptCs.Contracts/IPackageInstaller.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IPackageInstaller - { - void InstallPackages(IEnumerable packageIds, bool allowPreRelease = false); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IPackageObject.cs b/src/ScriptCs.Contracts/IPackageObject.cs deleted file mode 100644 index abdb940c..00000000 --- a/src/ScriptCs.Contracts/IPackageObject.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Runtime.Versioning; - -namespace ScriptCs.Contracts -{ - public interface IPackageObject - { - string Id { get; } - - string TextVersion { get; } - - Version Version { get; } - - string FullName { get; } - - IEnumerable GetCompatibleDlls(FrameworkName frameworkName); - - FrameworkName FrameworkName { get; } - - IEnumerable Dependencies { get; } - - IEnumerable FrameworkAssemblies { get; } - - IEnumerable GetContentFiles(); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IPackageReference.cs b/src/ScriptCs.Contracts/IPackageReference.cs deleted file mode 100644 index 70e8e2c2..00000000 --- a/src/ScriptCs.Contracts/IPackageReference.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Runtime.Versioning; - -namespace ScriptCs.Contracts -{ - public interface IPackageReference - { - string PackageId { get; } - - FrameworkName FrameworkName { get; } - - Version Version { get; set; } - - string SpecialVersion { get; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IRepl.cs b/src/ScriptCs.Contracts/IRepl.cs deleted file mode 100644 index 7edcd1b4..00000000 --- a/src/ScriptCs.Contracts/IRepl.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System.Collections.Generic; - - public interface IRepl : IScriptExecutor - { - Dictionary Commands { get; } - - string Buffer { get; } - } -} diff --git a/src/ScriptCs.Contracts/IReplCommand.cs b/src/ScriptCs.Contracts/IReplCommand.cs deleted file mode 100644 index 71028f3e..00000000 --- a/src/ScriptCs.Contracts/IReplCommand.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IReplCommand - { - string Description { get; } - - string CommandName { get; } - - object Execute(IRepl repl, object[] args); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IReplEngine.cs b/src/ScriptCs.Contracts/IReplEngine.cs deleted file mode 100644 index 7363fa72..00000000 --- a/src/ScriptCs.Contracts/IReplEngine.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IReplEngine : IScriptEngine - { - ICollection GetLocalVariables(ScriptPackSession scriptPackSession); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptEngine.cs b/src/ScriptCs.Contracts/IScriptEngine.cs deleted file mode 100644 index f54b44fa..00000000 --- a/src/ScriptCs.Contracts/IScriptEngine.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IScriptEngine - { - string BaseDirectory { get; set; } - - string CacheDirectory { get; set; } - - string FileName { get; set; } - - ScriptResult Execute( - string code, - string[] scriptArgs, - AssemblyReferences references, - IEnumerable namespaces, - ScriptPackSession scriptPackSession); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptEnvironment.cs b/src/ScriptCs.Contracts/IScriptEnvironment.cs deleted file mode 100644 index 60a6c637..00000000 --- a/src/ScriptCs.Contracts/IScriptEnvironment.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -namespace ScriptCs.Contracts -{ - public interface IScriptEnvironment - { - IReadOnlyList ScriptArgs { get; } - void AddCustomPrinter(Func printer); - void Print(T o); - void Print(object o); - string ScriptPath { get; } - string[] LoadedScripts { get; } - Assembly ScriptAssembly { get; } - void Initialize(); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptExecutor.cs b/src/ScriptCs.Contracts/IScriptExecutor.cs deleted file mode 100644 index fe9b992d..00000000 --- a/src/ScriptCs.Contracts/IScriptExecutor.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Collections.Generic; -using System.Reflection; - -namespace ScriptCs.Contracts -{ - public interface IScriptExecutor - { - AssemblyReferences References { get; } - - IReadOnlyCollection Namespaces { get; } - - IScriptEngine ScriptEngine { get; } - - IFileSystem FileSystem { get; } - - ScriptPackSession ScriptPackSession { get; } - - void ImportNamespaces(params string[] namespaces); - - void RemoveNamespaces(params string[] namespaces); - - void AddReferences(params Assembly[] references); - - void RemoveReferences(params Assembly[] references); - - void AddReferences(params string[] references); - - void RemoveReferences(params string[] references); - - void Initialize(IEnumerable paths, IEnumerable scriptPacks, params string[] scriptArgs); - - void Reset(); - - void Terminate(); - - ScriptResult Execute(string script, params string[] scriptArgs); - - ScriptResult ExecuteScript(string script, params string[] scriptArgs); - } -} diff --git a/src/ScriptCs.Contracts/IScriptHost.cs b/src/ScriptCs.Contracts/IScriptHost.cs deleted file mode 100644 index ac54c15f..00000000 --- a/src/ScriptCs.Contracts/IScriptHost.cs +++ /dev/null @@ -1,10 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.Contracts -{ - public interface IScriptHost - { - T Require() where T : IScriptPackContext; - IScriptEnvironment Env { get; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptHostFactory.cs b/src/ScriptCs.Contracts/IScriptHostFactory.cs deleted file mode 100644 index 2ef554f7..00000000 --- a/src/ScriptCs.Contracts/IScriptHostFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IScriptHostFactory - { - IScriptHost CreateScriptHost(IScriptPackManager scriptPackManager, string[] scriptArgs); - } -} diff --git a/src/ScriptCs.Contracts/IScriptInfo.cs b/src/ScriptCs.Contracts/IScriptInfo.cs deleted file mode 100644 index 1686a1af..00000000 --- a/src/ScriptCs.Contracts/IScriptInfo.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ScriptCs.Contracts -{ - public interface IScriptInfo - { - string ScriptPath { get; set; } - IList LoadedScripts { get; } - } -} diff --git a/src/ScriptCs.Contracts/IScriptLibraryComposer.cs b/src/ScriptCs.Contracts/IScriptLibraryComposer.cs deleted file mode 100644 index 7708af47..00000000 --- a/src/ScriptCs.Contracts/IScriptLibraryComposer.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Text; - -namespace ScriptCs.Contracts -{ - public interface IScriptLibraryComposer - { - void Compose(string workingDirectory, StringBuilder builder = null); - - string ScriptLibrariesFile { get; } - } -} diff --git a/src/ScriptCs.Contracts/IScriptPack.cs b/src/ScriptCs.Contracts/IScriptPack.cs deleted file mode 100644 index 8c3b35b2..00000000 --- a/src/ScriptCs.Contracts/IScriptPack.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.ComponentModel.Composition; - -namespace ScriptCs.Contracts -{ - [InheritedExport] - public interface IScriptPack - { - void Initialize(IScriptPackSession session); - - IScriptPackContext GetContext(); - - void Terminate(); - } - - public interface IScriptPack : IScriptPack where TContext : IScriptPackContext - { - [Import] - TContext Context { get; set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptPackContext.cs b/src/ScriptCs.Contracts/IScriptPackContext.cs deleted file mode 100644 index 1892f683..00000000 --- a/src/ScriptCs.Contracts/IScriptPackContext.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IScriptPackContext { } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptPackManager.cs b/src/ScriptCs.Contracts/IScriptPackManager.cs deleted file mode 100644 index 71f3fd8b..00000000 --- a/src/ScriptCs.Contracts/IScriptPackManager.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IScriptPackManager - { - TContext Get() where TContext : IScriptPackContext; - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptPackResolver.cs b/src/ScriptCs.Contracts/IScriptPackResolver.cs deleted file mode 100644 index 66e62b79..00000000 --- a/src/ScriptCs.Contracts/IScriptPackResolver.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IScriptPackResolver - { - IEnumerable GetPacks(); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/IScriptPackSession.cs b/src/ScriptCs.Contracts/IScriptPackSession.cs deleted file mode 100644 index 35e123dc..00000000 --- a/src/ScriptCs.Contracts/IScriptPackSession.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IScriptPackSession - { - string[] ScriptArgs { get; } - - void AddReference(string assemblyDisplayNameOrPath); - - void ImportNamespace(string @namespace); - } -} diff --git a/src/ScriptCs.Contracts/IServiceOverrides.cs b/src/ScriptCs.Contracts/IServiceOverrides.cs deleted file mode 100644 index 3766ed45..00000000 --- a/src/ScriptCs.Contracts/IServiceOverrides.cs +++ /dev/null @@ -1,41 +0,0 @@ -namespace ScriptCs.Contracts -{ - public interface IServiceOverrides { } - - public interface IServiceOverrides : IServiceOverrides where TConfig : IServiceOverrides - { - TConfig ScriptExecutor() where T : IScriptExecutor; - - TConfig Repl() where T : IRepl; - - TConfig ScriptEngine() where T : IScriptEngine; - - TConfig ScriptHostFactory() where T : IScriptHostFactory; - - TConfig ScriptPackManager() where T : IScriptPackManager; - - TConfig ScriptPackResolver() where T : IScriptPackResolver; - - TConfig InstallationProvider() where T : IInstallationProvider; - - TConfig FileSystem() where T : IFileSystem; - - TConfig AssemblyUtility() where T : IAssemblyUtility; - - TConfig ObjectSerializer() where T : IObjectSerializer; - - TConfig PackageContainer() where T : IPackageContainer; - - TConfig PackageInstaller() where T : IPackageInstaller; - - TConfig FilePreProcessor() where T : IFilePreProcessor; - - TConfig PackageAssemblyResolver() where T : IPackageAssemblyResolver; - - TConfig AssemblyResolver() where T : IAssemblyResolver; - - TConfig LineProcessor() where T : ILineProcessor; - - TConfig Console() where T : IConsole; - } -} diff --git a/src/ScriptCs.Contracts/Internal/LibLog.cs b/src/ScriptCs.Contracts/Internal/LibLog.cs deleted file mode 100644 index f5f232ac..00000000 --- a/src/ScriptCs.Contracts/Internal/LibLog.cs +++ /dev/null @@ -1,2050 +0,0 @@ -//=============================================================================== -// LibLog -// -// https://github.com/damianh/LibLog -//=============================================================================== -// Copyright © 2011-2015 Damian Hickey. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -//=============================================================================== - -// ReSharper disable PossibleNullReferenceException - -// Define LIBLOG_PORTABLE conditional compilation symbol for PCL compatibility -// -// Define LIBLOG_PUBLIC to enable ability to GET a logger (LogProvider.For<>() etc) from outside this library. NOTE: -// this can have unintended consequences of consumers of your library using your library to resolve a logger. If the -// reason is because you want to open this functionality to other projects within your solution, -// consider [InternalsVisibleTo] instead. -// -// Define LIBLOG_PROVIDERS_ONLY if your library provides its own logging API and you just want to use the -// LibLog providers internally to provide built in support for popular logging frameworks. - -#pragma warning disable 1591 - -using System.Diagnostics.CodeAnalysis; - -[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "ScriptCs.Contracts.Logging")] -[assembly: SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Scope = "member", Target = "ScriptCs.Contracts.Logging.Logger.#Invoke(ScriptCs.Contracts.Logging.LogLevel,System.Func`1,System.Exception,System.Object[])")] - -// If you copied this file manually, you need to change all "YourRootNameSpace" so not to clash with other libraries -// that use LibLog -#if LIBLOG_PROVIDERS_ONLY -namespace ScriptCs.Contracts.LibLog -#else -namespace ScriptCs.Contracts.Logging -#endif -{ - using System.Collections.Generic; - using System.Diagnostics.CodeAnalysis; -#if LIBLOG_PROVIDERS_ONLY - using ScriptCs.Contracts.LibLog.LogProviders; -#else - using ScriptCs.Contracts.Logging.LogProviders; -#endif - using System; -#if !LIBLOG_PROVIDERS_ONLY - using System.Diagnostics; -#if !LIBLOG_PORTABLE - using System.Runtime.CompilerServices; -#endif -#endif - -#if LIBLOG_PROVIDERS_ONLY - internal -#else - public -#endif - delegate bool Logger(LogLevel logLevel, Func messageFunc, Exception exception = null, params object[] formatParameters); - -#if !LIBLOG_PROVIDERS_ONLY - /// - /// Simple interface that represent a logger. - /// -#if LIBLOG_PUBLIC - public -#else - internal -#endif - interface ILog - { - /// - /// Log a message the specified log level. - /// - /// The log level. - /// The message function. - /// An optional exception. - /// Optional format parameters for the message generated by the messagefunc. - /// true if the message was logged. Otherwise false. - /// - /// Note to implementers: the message func should not be called if the loglevel is not enabled - /// so as not to incur performance penalties. - /// - /// To check IsEnabled call Log with only LogLevel and check the return value, no event will be written. - /// - bool Log(LogLevel logLevel, Func messageFunc, Exception exception = null, params object[] formatParameters ); - } -#endif - - /// - /// The log level. - /// -#if LIBLOG_PROVIDERS_ONLY - internal -#else - public -#endif - enum LogLevel - { - Trace, - Debug, - Info, - Warn, - Error, - Fatal - } - -#if !LIBLOG_PROVIDERS_ONLY -#if LIBLOG_PUBLIC - public -#else - internal -#endif - static partial class LogExtensions - { - public static bool IsDebugEnabled(this ILog logger) - { - GuardAgainstNullLogger(logger); - return logger.Log(LogLevel.Debug, null); - } - - public static bool IsErrorEnabled(this ILog logger) - { - GuardAgainstNullLogger(logger); - return logger.Log(LogLevel.Error, null); - } - - public static bool IsFatalEnabled(this ILog logger) - { - GuardAgainstNullLogger(logger); - return logger.Log(LogLevel.Fatal, null); - } - - public static bool IsInfoEnabled(this ILog logger) - { - GuardAgainstNullLogger(logger); - return logger.Log(LogLevel.Info, null); - } - - public static bool IsTraceEnabled(this ILog logger) - { - GuardAgainstNullLogger(logger); - return logger.Log(LogLevel.Trace, null); - } - - public static bool IsWarnEnabled(this ILog logger) - { - GuardAgainstNullLogger(logger); - return logger.Log(LogLevel.Warn, null); - } - - public static void Debug(this ILog logger, Func messageFunc) - { - GuardAgainstNullLogger(logger); - logger.Log(LogLevel.Debug, messageFunc); - } - - public static void Debug(this ILog logger, string message) - { - if (logger.IsDebugEnabled()) - { - logger.Log(LogLevel.Debug, message.AsFunc()); - } - } - - public static void DebugFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsDebugEnabled()) - { - logger.LogFormat(LogLevel.Debug, message, args); - } - } - - public static void DebugException(this ILog logger, string message, Exception exception) - { - if (logger.IsDebugEnabled()) - { - logger.Log(LogLevel.Debug, message.AsFunc(), exception); - } - } - - public static void DebugException(this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsDebugEnabled()) - { - logger.Log(LogLevel.Debug, message.AsFunc(), exception, formatParams); - } - } - - public static void Error(this ILog logger, Func messageFunc) - { - GuardAgainstNullLogger(logger); - logger.Log(LogLevel.Error, messageFunc); - } - - public static void Error(this ILog logger, string message) - { - if (logger.IsErrorEnabled()) - { - logger.Log(LogLevel.Error, message.AsFunc()); - } - } - - public static void ErrorFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsErrorEnabled()) - { - logger.LogFormat(LogLevel.Error, message, args); - } - } - - public static void ErrorException(this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsErrorEnabled()) - { - logger.Log(LogLevel.Error, message.AsFunc(), exception, formatParams); - } - } - - public static void Fatal(this ILog logger, Func messageFunc) - { - logger.Log(LogLevel.Fatal, messageFunc); - } - - public static void Fatal(this ILog logger, string message) - { - if (logger.IsFatalEnabled()) - { - logger.Log(LogLevel.Fatal, message.AsFunc()); - } - } - - public static void FatalFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsFatalEnabled()) - { - logger.LogFormat(LogLevel.Fatal, message, args); - } - } - - public static void FatalException(this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsFatalEnabled()) - { - logger.Log(LogLevel.Fatal, message.AsFunc(), exception, formatParams); - } - } - - public static void Info(this ILog logger, Func messageFunc) - { - GuardAgainstNullLogger(logger); - logger.Log(LogLevel.Info, messageFunc); - } - - public static void Info(this ILog logger, string message) - { - if (logger.IsInfoEnabled()) - { - logger.Log(LogLevel.Info, message.AsFunc()); - } - } - - public static void InfoFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsInfoEnabled()) - { - logger.LogFormat(LogLevel.Info, message, args); - } - } - - public static void InfoException(this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsInfoEnabled()) - { - logger.Log(LogLevel.Info, message.AsFunc(), exception, formatParams); - } - } - - public static void Trace(this ILog logger, Func messageFunc) - { - GuardAgainstNullLogger(logger); - logger.Log(LogLevel.Trace, messageFunc); - } - - public static void Trace(this ILog logger, string message) - { - if (logger.IsTraceEnabled()) - { - logger.Log(LogLevel.Trace, message.AsFunc()); - } - } - - public static void TraceFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsTraceEnabled()) - { - logger.LogFormat(LogLevel.Trace, message, args); - } - } - - public static void TraceException(this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsTraceEnabled()) - { - logger.Log(LogLevel.Trace, message.AsFunc(), exception, formatParams); - } - } - - public static void Warn(this ILog logger, Func messageFunc) - { - GuardAgainstNullLogger(logger); - logger.Log(LogLevel.Warn, messageFunc); - } - - public static void Warn(this ILog logger, string message) - { - if (logger.IsWarnEnabled()) - { - logger.Log(LogLevel.Warn, message.AsFunc()); - } - } - - public static void WarnFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsWarnEnabled()) - { - logger.LogFormat(LogLevel.Warn, message, args); - } - } - - public static void WarnException(this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsWarnEnabled()) - { - logger.Log(LogLevel.Warn, message.AsFunc(), exception, formatParams); - } - } - - // ReSharper disable once UnusedParameter.Local - private static void GuardAgainstNullLogger(ILog logger) - { - if (logger == null) - { - throw new ArgumentNullException("logger"); - } - } - - private static void LogFormat(this ILog logger, LogLevel logLevel, string message, params object[] args) - { - logger.Log(logLevel, message.AsFunc(), null, args); - } - - // Avoid the closure allocation, see https://gist.github.com/AArnott/d285feef75c18f6ecd2b - private static Func AsFunc(this T value) where T : class - { - return value.Return; - } - - private static T Return(this T value) - { - return value; - } - } -#endif - - /// - /// Represents a way to get a - /// -#if LIBLOG_PROVIDERS_ONLY - internal -#else - public -#endif - interface ILogProvider - { - /// - /// Gets the specified named logger. - /// - /// Name of the logger. - /// The logger reference. - Logger GetLogger(string name); - - /// - /// Opens a nested diagnostics context. Not supported in EntLib logging. - /// - /// The message to add to the diagnostics context. - /// A disposable that when disposed removes the message from the context. - IDisposable OpenNestedContext(string message); - - /// - /// Opens a mapped diagnostics context. Not supported in EntLib logging. - /// - /// A key. - /// A value. - /// A disposable that when disposed removes the map from the context. - IDisposable OpenMappedContext(string key, string value); - } - - /// - /// Provides a mechanism to create instances of objects. - /// -#if LIBLOG_PROVIDERS_ONLY - internal -#else - public -#endif - static class LogProvider - { -#if !LIBLOG_PROVIDERS_ONLY - /// - /// The disable logging environment variable. If the environment variable is set to 'true', then logging - /// will be disabled. - /// - public const string DisableLoggingEnvironmentVariable = "ScriptCs.Contracts_LIBLOG_DISABLE"; - private const string NullLogProvider = "Current Log Provider is not set. Call SetCurrentLogProvider " + - "with a non-null value first."; - private static dynamic s_currentLogProvider; - private static Action s_onCurrentLogProviderSet; - - [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")] - static LogProvider() - { - IsDisabled = false; - } - - /// - /// Sets the current log provider. - /// - /// The log provider. - public static void SetCurrentLogProvider(ILogProvider logProvider) - { - s_currentLogProvider = logProvider; - - RaiseOnCurrentLogProviderSet(); - } - - /// - /// Gets or sets a value indicating whether this is logging is disabled. - /// - /// - /// true if logging is disabled; otherwise, false. - /// - public static bool IsDisabled { get; set; } - - /// - /// Sets an action that is invoked when a consumer of your library has called SetCurrentLogProvider. It is - /// important that hook into this if you are using child libraries (especially ilmerged ones) that are using - /// LibLog (or other logging abstraction) so you adapt and delegate to them. - /// - /// - internal static Action OnCurrentLogProviderSet - { - set - { - s_onCurrentLogProviderSet = value; - RaiseOnCurrentLogProviderSet(); - } - } - - internal static ILogProvider CurrentLogProvider - { - get - { - return s_currentLogProvider; - } - } - - /// - /// Gets a logger for the specified type. - /// - /// The type whose name will be used for the logger. - /// An instance of -#if LIBLOG_PUBLIC - public -#else - internal -#endif - static ILog For() - { - return GetLogger(typeof(T)); - } - -#if !LIBLOG_PORTABLE - /// - /// Gets a logger for the current class. - /// - /// An instance of - [MethodImpl(MethodImplOptions.NoInlining)] -#if LIBLOG_PUBLIC - public -#else - internal -#endif - static ILog GetCurrentClassLogger() - { - var stackFrame = new StackFrame(1, false); - return GetLogger(stackFrame.GetMethod().DeclaringType); - } -#endif - - /// - /// Gets a logger for the specified type. - /// - /// The type whose name will be used for the logger. - /// An instance of -#if LIBLOG_PUBLIC - public -#else - internal -#endif - static ILog GetLogger(Type type) - { - return GetLogger(type.FullName); - } - - /// - /// Gets a logger with the specified name. - /// - /// The name. - /// An instance of -#if LIBLOG_PUBLIC - public -#else - internal -#endif - static ILog GetLogger(string name) - { - ILogProvider logProvider = CurrentLogProvider ?? ResolveLogProvider(); - return logProvider == null - ? NoOpLogger.Instance - : (ILog)new LoggerExecutionWrapper(logProvider.GetLogger(name), () => IsDisabled); - } - - /// - /// Opens a nested diagnostics context. - /// - /// A message. - /// An that closes context when disposed. - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "SetCurrentLogProvider")] -#if LIBLOG_PUBLIC - public -#else - internal -#endif - static IDisposable OpenNestedContext(string message) - { - return CurrentLogProvider == null - ? new DisposableAction(() => {}) - : CurrentLogProvider.OpenNestedContext(message); - } - - /// - /// Opens a mapped diagnostics context. - /// - /// A key. - /// A value. - /// An that closes context when disposed. - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "SetCurrentLogProvider")] -#if LIBLOG_PUBLIC - public -#else - internal -#endif - static IDisposable OpenMappedContext(string key, string value) - { - return CurrentLogProvider == null - ? new DisposableAction(() => { }) - : CurrentLogProvider.OpenMappedContext(key, value); - } -#endif - -#if LIBLOG_PROVIDERS_ONLY - private -#else - internal -#endif - delegate bool IsLoggerAvailable(); - -#if LIBLOG_PROVIDERS_ONLY - private -#else - internal -#endif - delegate ILogProvider CreateLogProvider(); - -#if LIBLOG_PROVIDERS_ONLY - private -#else - internal -#endif - static readonly List> LogProviderResolvers = - new List> - { - new Tuple(SerilogLogProvider.IsLoggerAvailable, () => new SerilogLogProvider()), - new Tuple(NLogLogProvider.IsLoggerAvailable, () => new NLogLogProvider()), - new Tuple(Log4NetLogProvider.IsLoggerAvailable, () => new Log4NetLogProvider()), - new Tuple(EntLibLogProvider.IsLoggerAvailable, () => new EntLibLogProvider()), - new Tuple(LoupeLogProvider.IsLoggerAvailable, () => new LoupeLogProvider()), - }; - -#if !LIBLOG_PROVIDERS_ONLY - private static void RaiseOnCurrentLogProviderSet() - { - if (s_onCurrentLogProviderSet != null) - { - s_onCurrentLogProviderSet(s_currentLogProvider); - } - } -#endif - - [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Console.WriteLine(System.String,System.Object,System.Object)")] - [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] - internal static ILogProvider ResolveLogProvider() - { - try - { - foreach (var providerResolver in LogProviderResolvers) - { - if (providerResolver.Item1()) - { - return providerResolver.Item2(); - } - } - } - catch (Exception ex) - { -#if LIBLOG_PORTABLE - Debug.WriteLine( -#else - Console.WriteLine( -#endif - "Exception occurred resolving a log provider. Logging for this assembly {0} is disabled. {1}", - typeof(LogProvider).GetAssemblyPortable().FullName, - ex); - } - return null; - } - -#if !LIBLOG_PROVIDERS_ONLY - internal class NoOpLogger : ILog - { - internal static readonly NoOpLogger Instance = new NoOpLogger(); - - public bool Log(LogLevel logLevel, Func messageFunc, Exception exception, params object[] formatParameters) - { - return false; - } - } -#endif - } - -#if !LIBLOG_PROVIDERS_ONLY - internal class LoggerExecutionWrapper : ILog - { - private readonly Logger _logger; - private readonly Func _getIsDisabled; - internal const string FailedToGenerateLogMessage = "Failed to generate log message"; - - internal LoggerExecutionWrapper(Logger logger, Func getIsDisabled = null) - { - _logger = logger; - _getIsDisabled = getIsDisabled ?? (() => false); - } - - internal Logger WrappedLogger - { - get { return _logger; } - } - - [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] - public bool Log(LogLevel logLevel, Func messageFunc, Exception exception = null, params object[] formatParameters) - { - if (_getIsDisabled()) - { - return false; - } -#if !LIBLOG_PORTABLE - var envVar = Environment.GetEnvironmentVariable(LogProvider.DisableLoggingEnvironmentVariable); - - if (envVar != null && envVar.Equals("true", StringComparison.OrdinalIgnoreCase)) - { - return false; - } -#endif - - if (messageFunc == null) - { - return _logger(logLevel, null); - } - - Func wrappedMessageFunc = () => - { - try - { - return messageFunc(); - } - catch (Exception ex) - { - Log(LogLevel.Error, () => FailedToGenerateLogMessage, ex); - } - return null; - }; - return _logger(logLevel, wrappedMessageFunc, exception, formatParameters); - } - } -#endif -} - -#if LIBLOG_PROVIDERS_ONLY -namespace ScriptCs.Contracts.LibLog.LogProviders -#else -namespace ScriptCs.Contracts.Logging.LogProviders -#endif -{ - using System; - using System.Collections.Generic; - using System.Diagnostics.CodeAnalysis; -#if !LIBLOG_PORTABLE - using System.Diagnostics; -#endif - using System.Globalization; - using System.Linq; - using System.Linq.Expressions; - using System.Reflection; -#if !LIBLOG_PORTABLE - using System.Text; -#endif - using System.Text.RegularExpressions; - - internal abstract class LogProviderBase : ILogProvider - { - protected delegate IDisposable OpenNdc(string message); - protected delegate IDisposable OpenMdc(string key, string value); - - private readonly Lazy _lazyOpenNdcMethod; - private readonly Lazy _lazyOpenMdcMethod; - private static readonly IDisposable NoopDisposableInstance = new DisposableAction(); - - protected LogProviderBase() - { - _lazyOpenNdcMethod - = new Lazy(GetOpenNdcMethod); - _lazyOpenMdcMethod - = new Lazy(GetOpenMdcMethod); - } - - public abstract Logger GetLogger(string name); - - public IDisposable OpenNestedContext(string message) - { - return _lazyOpenNdcMethod.Value(message); - } - - public IDisposable OpenMappedContext(string key, string value) - { - return _lazyOpenMdcMethod.Value(key, value); - } - - protected virtual OpenNdc GetOpenNdcMethod() - { - return _ => NoopDisposableInstance; - } - - protected virtual OpenMdc GetOpenMdcMethod() - { - return (_, __) => NoopDisposableInstance; - } - } - - internal class NLogLogProvider : LogProviderBase - { - private readonly Func _getLoggerByNameDelegate; - private static bool s_providerIsAvailableOverride = true; - - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "LogManager")] - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "NLog")] - public NLogLogProvider() - { - if (!IsLoggerAvailable()) - { - throw new InvalidOperationException("NLog.LogManager not found"); - } - _getLoggerByNameDelegate = GetGetLoggerMethodCall(); - } - - public static bool ProviderIsAvailableOverride - { - get { return s_providerIsAvailableOverride; } - set { s_providerIsAvailableOverride = value; } - } - - public override Logger GetLogger(string name) - { - return new NLogLogger(_getLoggerByNameDelegate(name)).Log; - } - - public static bool IsLoggerAvailable() - { - return ProviderIsAvailableOverride && GetLogManagerType() != null; - } - - protected override OpenNdc GetOpenNdcMethod() - { - Type ndcContextType = Type.GetType("NLog.NestedDiagnosticsContext, NLog"); - MethodInfo pushMethod = ndcContextType.GetMethodPortable("Push", typeof(string)); - ParameterExpression messageParam = Expression.Parameter(typeof(string), "message"); - MethodCallExpression pushMethodCall = Expression.Call(null, pushMethod, messageParam); - return Expression.Lambda(pushMethodCall, messageParam).Compile(); - } - - protected override OpenMdc GetOpenMdcMethod() - { - Type mdcContextType = Type.GetType("NLog.MappedDiagnosticsContext, NLog"); - - MethodInfo setMethod = mdcContextType.GetMethodPortable("Set", typeof(string), typeof(string)); - MethodInfo removeMethod = mdcContextType.GetMethodPortable("Remove", typeof(string)); - ParameterExpression keyParam = Expression.Parameter(typeof(string), "key"); - ParameterExpression valueParam = Expression.Parameter(typeof(string), "value"); - - MethodCallExpression setMethodCall = Expression.Call(null, setMethod, keyParam, valueParam); - MethodCallExpression removeMethodCall = Expression.Call(null, removeMethod, keyParam); - - Action set = Expression - .Lambda>(setMethodCall, keyParam, valueParam) - .Compile(); - Action remove = Expression - .Lambda>(removeMethodCall, keyParam) - .Compile(); - - return (key, value) => - { - set(key, value); - return new DisposableAction(() => remove(key)); - }; - } - - private static Type GetLogManagerType() - { - return Type.GetType("NLog.LogManager, NLog"); - } - - private static Func GetGetLoggerMethodCall() - { - Type logManagerType = GetLogManagerType(); - MethodInfo method = logManagerType.GetMethodPortable("GetLogger", typeof(string)); - ParameterExpression nameParam = Expression.Parameter(typeof(string), "name"); - MethodCallExpression methodCall = Expression.Call(null, method, nameParam); - return Expression.Lambda>(methodCall, nameParam).Compile(); - } - - internal class NLogLogger - { - private readonly dynamic _logger; - - internal NLogLogger(dynamic logger) - { - _logger = logger; - } - - [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] - public bool Log(LogLevel logLevel, Func messageFunc, Exception exception, params object[] formatParameters) - { - if (messageFunc == null) - { - return IsLogLevelEnable(logLevel); - } - messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters); - - if(exception != null) - { - return LogException(logLevel, messageFunc, exception); - } - switch (logLevel) - { - case LogLevel.Debug: - if (_logger.IsDebugEnabled) - { - _logger.Debug(messageFunc()); - return true; - } - break; - case LogLevel.Info: - if (_logger.IsInfoEnabled) - { - _logger.Info(messageFunc()); - return true; - } - break; - case LogLevel.Warn: - if (_logger.IsWarnEnabled) - { - _logger.Warn(messageFunc()); - return true; - } - break; - case LogLevel.Error: - if (_logger.IsErrorEnabled) - { - _logger.Error(messageFunc()); - return true; - } - break; - case LogLevel.Fatal: - if (_logger.IsFatalEnabled) - { - _logger.Fatal(messageFunc()); - return true; - } - break; - default: - if (_logger.IsTraceEnabled) - { - _logger.Trace(messageFunc()); - return true; - } - break; - } - return false; - } - - [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] - private bool LogException(LogLevel logLevel, Func messageFunc, Exception exception) - { - switch (logLevel) - { - case LogLevel.Debug: - if (_logger.IsDebugEnabled) - { - _logger.DebugException(messageFunc(), exception); - return true; - } - break; - case LogLevel.Info: - if (_logger.IsInfoEnabled) - { - _logger.InfoException(messageFunc(), exception); - return true; - } - break; - case LogLevel.Warn: - if (_logger.IsWarnEnabled) - { - _logger.WarnException(messageFunc(), exception); - return true; - } - break; - case LogLevel.Error: - if (_logger.IsErrorEnabled) - { - _logger.ErrorException(messageFunc(), exception); - return true; - } - break; - case LogLevel.Fatal: - if (_logger.IsFatalEnabled) - { - _logger.FatalException(messageFunc(), exception); - return true; - } - break; - default: - if (_logger.IsTraceEnabled) - { - _logger.TraceException(messageFunc(), exception); - return true; - } - break; - } - return false; - } - - private bool IsLogLevelEnable(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Debug: - return _logger.IsDebugEnabled; - case LogLevel.Info: - return _logger.IsInfoEnabled; - case LogLevel.Warn: - return _logger.IsWarnEnabled; - case LogLevel.Error: - return _logger.IsErrorEnabled; - case LogLevel.Fatal: - return _logger.IsFatalEnabled; - default: - return _logger.IsTraceEnabled; - } - } - } - } - - internal class Log4NetLogProvider : LogProviderBase - { - private readonly Func _getLoggerByNameDelegate; - private static bool s_providerIsAvailableOverride = true; - - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "LogManager")] - public Log4NetLogProvider() - { - if (!IsLoggerAvailable()) - { - throw new InvalidOperationException("log4net.LogManager not found"); - } - _getLoggerByNameDelegate = GetGetLoggerMethodCall(); - } - - public static bool ProviderIsAvailableOverride - { - get { return s_providerIsAvailableOverride; } - set { s_providerIsAvailableOverride = value; } - } - - public override Logger GetLogger(string name) - { - return new Log4NetLogger(_getLoggerByNameDelegate(name)).Log; - } - - internal static bool IsLoggerAvailable() - { - return ProviderIsAvailableOverride && GetLogManagerType() != null; - } - - protected override OpenNdc GetOpenNdcMethod() - { - Type logicalThreadContextType = Type.GetType("log4net.LogicalThreadContext, log4net"); - PropertyInfo stacksProperty = logicalThreadContextType.GetPropertyPortable("Stacks"); - Type logicalThreadContextStacksType = stacksProperty.PropertyType; - PropertyInfo stacksIndexerProperty = logicalThreadContextStacksType.GetPropertyPortable("Item"); - Type stackType = stacksIndexerProperty.PropertyType; - MethodInfo pushMethod = stackType.GetMethodPortable("Push"); - - ParameterExpression messageParameter = - Expression.Parameter(typeof(string), "message"); - - // message => LogicalThreadContext.Stacks.Item["NDC"].Push(message); - MethodCallExpression callPushBody = - Expression.Call( - Expression.Property(Expression.Property(null, stacksProperty), - stacksIndexerProperty, - Expression.Constant("NDC")), - pushMethod, - messageParameter); - - OpenNdc result = - Expression.Lambda(callPushBody, messageParameter) - .Compile(); - - return result; - } - - protected override OpenMdc GetOpenMdcMethod() - { - Type logicalThreadContextType = Type.GetType("log4net.LogicalThreadContext, log4net"); - PropertyInfo propertiesProperty = logicalThreadContextType.GetPropertyPortable("Properties"); - Type logicalThreadContextPropertiesType = propertiesProperty.PropertyType; - PropertyInfo propertiesIndexerProperty = logicalThreadContextPropertiesType.GetPropertyPortable("Item"); - - MethodInfo removeMethod = logicalThreadContextPropertiesType.GetMethodPortable("Remove"); - - ParameterExpression keyParam = Expression.Parameter(typeof(string), "key"); - ParameterExpression valueParam = Expression.Parameter(typeof(string), "value"); - - MemberExpression propertiesExpression = Expression.Property(null, propertiesProperty); - - // (key, value) => LogicalThreadContext.Properties.Item[key] = value; - BinaryExpression setProperties = Expression.Assign(Expression.Property(propertiesExpression, propertiesIndexerProperty, keyParam), valueParam); - - // key => LogicalThreadContext.Properties.Remove(key); - MethodCallExpression removeMethodCall = Expression.Call(propertiesExpression, removeMethod, keyParam); - - Action set = Expression - .Lambda>(setProperties, keyParam, valueParam) - .Compile(); - - Action remove = Expression - .Lambda>(removeMethodCall, keyParam) - .Compile(); - - return (key, value) => - { - set(key, value); - return new DisposableAction(() => remove(key)); - }; - } - - private static Type GetLogManagerType() - { - return Type.GetType("log4net.LogManager, log4net"); - } - - private static Func GetGetLoggerMethodCall() - { - Type logManagerType = GetLogManagerType(); - MethodInfo method = logManagerType.GetMethodPortable("GetLogger", typeof(string)); - ParameterExpression nameParam = Expression.Parameter(typeof(string), "name"); - MethodCallExpression methodCall = Expression.Call(null, method, nameParam); - return Expression.Lambda>(methodCall, nameParam).Compile(); - } - - internal class Log4NetLogger - { - private readonly dynamic _logger; - private static Type s_callerStackBoundaryType; - private static readonly object CallerStackBoundaryTypeSync = new object(); - - private readonly object _levelDebug; - private readonly object _levelInfo; - private readonly object _levelWarn; - private readonly object _levelError; - private readonly object _levelFatal; - private readonly Func _isEnabledForDelegate; - private readonly Action _logDelegate; - - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "ILogger")] - internal Log4NetLogger(dynamic logger) - { - _logger = logger.Logger; - - var logEventLevelType = Type.GetType("log4net.Core.Level, log4net"); - if (logEventLevelType == null) - { - throw new InvalidOperationException("Type log4net.Core.Level was not found."); - } - - var levelFields = logEventLevelType.GetFieldsPortable().ToList(); - _levelDebug = levelFields.First(x => x.Name == "Debug").GetValue(null); - _levelInfo = levelFields.First(x => x.Name == "Info").GetValue(null); - _levelWarn = levelFields.First(x => x.Name == "Warn").GetValue(null); - _levelError = levelFields.First(x => x.Name == "Error").GetValue(null); - _levelFatal = levelFields.First(x => x.Name == "Fatal").GetValue(null); - - // Func isEnabledFor = (logger, level) => { return ((log4net.Core.ILogger)logger).IsEnabled(level); } - var loggerType = Type.GetType("log4net.Core.ILogger, log4net"); - if (loggerType == null) - { - throw new InvalidOperationException("Type log4net.Core.ILogger, was not found."); - } - MethodInfo isEnabledMethodInfo = loggerType.GetMethodPortable("IsEnabledFor", logEventLevelType); - ParameterExpression instanceParam = Expression.Parameter(typeof(object)); - UnaryExpression instanceCast = Expression.Convert(instanceParam, loggerType); - ParameterExpression callerStackBoundaryDeclaringTypeParam = Expression.Parameter(typeof(Type)); - ParameterExpression levelParam = Expression.Parameter(typeof(object)); - ParameterExpression messageParam = Expression.Parameter(typeof(string)); - UnaryExpression levelCast = Expression.Convert(levelParam, logEventLevelType); - MethodCallExpression isEnabledMethodCall = Expression.Call(instanceCast, isEnabledMethodInfo, levelCast); - _isEnabledForDelegate = Expression.Lambda>(isEnabledMethodCall, instanceParam, levelParam).Compile(); - - // Action Log = - // (logger, callerStackBoundaryDeclaringType, level, message, exception) => { ((ILogger)logger).Write(callerStackBoundaryDeclaringType, level, message, exception); } - MethodInfo writeExceptionMethodInfo = loggerType.GetMethodPortable("Log", - typeof(Type), - logEventLevelType, - typeof(string), - typeof(Exception)); - ParameterExpression exceptionParam = Expression.Parameter(typeof(Exception)); - var writeMethodExp = Expression.Call( - instanceCast, - writeExceptionMethodInfo, - callerStackBoundaryDeclaringTypeParam, - levelCast, - messageParam, - exceptionParam); - _logDelegate = Expression.Lambda>( - writeMethodExp, - instanceParam, - callerStackBoundaryDeclaringTypeParam, - levelParam, - messageParam, - exceptionParam).Compile(); - } - - public bool Log(LogLevel logLevel, Func messageFunc, Exception exception, params object[] formatParameters) - { - if (messageFunc == null) - { - return IsLogLevelEnable(logLevel); - } - - if (!IsLogLevelEnable(logLevel)) - { - return false; - } - - messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters); - - // determine correct caller - this might change due to jit optimizations with method inlining - if (s_callerStackBoundaryType == null) - { - lock (CallerStackBoundaryTypeSync) - { -#if !LIBLOG_PORTABLE - StackTrace stack = new StackTrace(); - Type thisType = GetType(); - s_callerStackBoundaryType = Type.GetType("LoggerExecutionWrapper"); - for (var i = 1; i < stack.FrameCount; i++) - { - if (!IsInTypeHierarchy(thisType, stack.GetFrame(i).GetMethod().DeclaringType)) - { - s_callerStackBoundaryType = stack.GetFrame(i - 1).GetMethod().DeclaringType; - break; - } - } -#else - s_callerStackBoundaryType = typeof (LoggerExecutionWrapper); -#endif - } - } - - var translatedLevel = TranslateLevel(logLevel); - _logDelegate(_logger, s_callerStackBoundaryType, translatedLevel, messageFunc(), exception); - return true; - } - - private static bool IsInTypeHierarchy(Type currentType, Type checkType) - { - while (currentType != null && currentType != typeof(object)) - { - if (currentType == checkType) - { - return true; - } - currentType = currentType.GetBaseTypePortable(); - } - return false; - } - - private bool IsLogLevelEnable(LogLevel logLevel) - { - var level = TranslateLevel(logLevel); - return _isEnabledForDelegate(_logger, level); - } - - private object TranslateLevel(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Trace: - case LogLevel.Debug: - return _levelDebug; - case LogLevel.Info: - return _levelInfo; - case LogLevel.Warn: - return _levelWarn; - case LogLevel.Error: - return _levelError; - case LogLevel.Fatal: - return _levelFatal; - default: - throw new ArgumentOutOfRangeException("logLevel", logLevel, null); - } - } - } - } - - internal class EntLibLogProvider : LogProviderBase - { - private const string TypeTemplate = "Microsoft.Practices.EnterpriseLibrary.Logging.{0}, Microsoft.Practices.EnterpriseLibrary.Logging"; - private static bool s_providerIsAvailableOverride = true; - private static readonly Type LogEntryType; - private static readonly Type LoggerType; - private static readonly Type TraceEventTypeType; - private static readonly Action WriteLogEntry; - private static readonly Func ShouldLogEntry; - - [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")] - static EntLibLogProvider() - { - LogEntryType = Type.GetType(string.Format(CultureInfo.InvariantCulture, TypeTemplate, "LogEntry")); - LoggerType = Type.GetType(string.Format(CultureInfo.InvariantCulture, TypeTemplate, "Logger")); - TraceEventTypeType = TraceEventTypeValues.Type; - if (LogEntryType == null - || TraceEventTypeType == null - || LoggerType == null) - { - return; - } - WriteLogEntry = GetWriteLogEntry(); - ShouldLogEntry = GetShouldLogEntry(); - } - - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "EnterpriseLibrary")] - public EntLibLogProvider() - { - if (!IsLoggerAvailable()) - { - throw new InvalidOperationException("Microsoft.Practices.EnterpriseLibrary.Logging.Logger not found"); - } - } - - public static bool ProviderIsAvailableOverride - { - get { return s_providerIsAvailableOverride; } - set { s_providerIsAvailableOverride = value; } - } - - public override Logger GetLogger(string name) - { - return new EntLibLogger(name, WriteLogEntry, ShouldLogEntry).Log; - } - - internal static bool IsLoggerAvailable() - { - return ProviderIsAvailableOverride - && TraceEventTypeType != null - && LogEntryType != null; - } - - private static Action GetWriteLogEntry() - { - // new LogEntry(...) - var logNameParameter = Expression.Parameter(typeof(string), "logName"); - var messageParameter = Expression.Parameter(typeof(string), "message"); - var severityParameter = Expression.Parameter(typeof(int), "severity"); - - MemberInitExpression memberInit = GetWriteLogExpression( - messageParameter, - Expression.Convert(severityParameter, TraceEventTypeType), - logNameParameter); - - //Logger.Write(new LogEntry(....)); - MethodInfo writeLogEntryMethod = LoggerType.GetMethodPortable("Write", LogEntryType); - var writeLogEntryExpression = Expression.Call(writeLogEntryMethod, memberInit); - - return Expression.Lambda>( - writeLogEntryExpression, - logNameParameter, - messageParameter, - severityParameter).Compile(); - } - - private static Func GetShouldLogEntry() - { - // new LogEntry(...) - var logNameParameter = Expression.Parameter(typeof(string), "logName"); - var severityParameter = Expression.Parameter(typeof(int), "severity"); - - MemberInitExpression memberInit = GetWriteLogExpression( - Expression.Constant("***dummy***"), - Expression.Convert(severityParameter, TraceEventTypeType), - logNameParameter); - - //Logger.Write(new LogEntry(....)); - MethodInfo writeLogEntryMethod = LoggerType.GetMethodPortable("ShouldLog", LogEntryType); - var writeLogEntryExpression = Expression.Call(writeLogEntryMethod, memberInit); - - return Expression.Lambda>( - writeLogEntryExpression, - logNameParameter, - severityParameter).Compile(); - } - - private static MemberInitExpression GetWriteLogExpression(Expression message, - Expression severityParameter, ParameterExpression logNameParameter) - { - var entryType = LogEntryType; - MemberInitExpression memberInit = Expression.MemberInit(Expression.New(entryType), - Expression.Bind(entryType.GetPropertyPortable("Message"), message), - Expression.Bind(entryType.GetPropertyPortable("Severity"), severityParameter), - Expression.Bind( - entryType.GetPropertyPortable("TimeStamp"), - Expression.Property(null, typeof (DateTime).GetPropertyPortable("UtcNow"))), - Expression.Bind( - entryType.GetPropertyPortable("Categories"), - Expression.ListInit( - Expression.New(typeof (List)), - typeof (List).GetMethodPortable("Add", typeof (string)), - logNameParameter))); - return memberInit; - } - - internal class EntLibLogger - { - private readonly string _loggerName; - private readonly Action _writeLog; - private readonly Func _shouldLog; - - internal EntLibLogger(string loggerName, Action writeLog, Func shouldLog) - { - _loggerName = loggerName; - _writeLog = writeLog; - _shouldLog = shouldLog; - } - - public bool Log(LogLevel logLevel, Func messageFunc, Exception exception, params object[] formatParameters) - { - var severity = MapSeverity(logLevel); - if (messageFunc == null) - { - return _shouldLog(_loggerName, severity); - } - - - messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters); - if (exception != null) - { - return LogException(logLevel, messageFunc, exception); - } - _writeLog(_loggerName, messageFunc(), severity); - return true; - } - - public bool LogException(LogLevel logLevel, Func messageFunc, Exception exception) - { - var severity = MapSeverity(logLevel); - var message = messageFunc() + Environment.NewLine + exception; - _writeLog(_loggerName, message, severity); - return true; - } - - private static int MapSeverity(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Fatal: - return TraceEventTypeValues.Critical; - case LogLevel.Error: - return TraceEventTypeValues.Error; - case LogLevel.Warn: - return TraceEventTypeValues.Warning; - case LogLevel.Info: - return TraceEventTypeValues.Information; - default: - return TraceEventTypeValues.Verbose; - } - } - } - } - - internal class SerilogLogProvider : LogProviderBase - { - private readonly Func _getLoggerByNameDelegate; - private static bool s_providerIsAvailableOverride = true; - - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "Serilog")] - public SerilogLogProvider() - { - if (!IsLoggerAvailable()) - { - throw new InvalidOperationException("Serilog.Log not found"); - } - _getLoggerByNameDelegate = GetForContextMethodCall(); - } - - public static bool ProviderIsAvailableOverride - { - get { return s_providerIsAvailableOverride; } - set { s_providerIsAvailableOverride = value; } - } - - public override Logger GetLogger(string name) - { - return new SerilogLogger(_getLoggerByNameDelegate(name)).Log; - } - - internal static bool IsLoggerAvailable() - { - return ProviderIsAvailableOverride && GetLogManagerType() != null; - } - - protected override OpenNdc GetOpenNdcMethod() - { - return message => GetPushProperty()("NDC", message); - } - - protected override OpenMdc GetOpenMdcMethod() - { - return (key, value) => GetPushProperty()(key, value); - } - - private static Func GetPushProperty() - { - Type ndcContextType = Type.GetType("Serilog.Context.LogContext, Serilog.FullNetFx"); - MethodInfo pushPropertyMethod = ndcContextType.GetMethodPortable( - "PushProperty", - typeof(string), - typeof(object), - typeof(bool)); - ParameterExpression nameParam = Expression.Parameter(typeof(string), "name"); - ParameterExpression valueParam = Expression.Parameter(typeof(object), "value"); - ParameterExpression destructureObjectParam = Expression.Parameter(typeof(bool), "destructureObjects"); - MethodCallExpression pushPropertyMethodCall = Expression - .Call(null, pushPropertyMethod, nameParam, valueParam, destructureObjectParam); - var pushProperty = Expression - .Lambda>( - pushPropertyMethodCall, - nameParam, - valueParam, - destructureObjectParam) - .Compile(); - - return (key, value) => pushProperty(key, value, false); - } - - private static Type GetLogManagerType() - { - return Type.GetType("Serilog.Log, Serilog"); - } - - private static Func GetForContextMethodCall() - { - Type logManagerType = GetLogManagerType(); - MethodInfo method = logManagerType.GetMethodPortable("ForContext", typeof(string), typeof(object), typeof(bool)); - ParameterExpression propertyNameParam = Expression.Parameter(typeof(string), "propertyName"); - ParameterExpression valueParam = Expression.Parameter(typeof(object), "value"); - ParameterExpression destructureObjectsParam = Expression.Parameter(typeof(bool), "destructureObjects"); - MethodCallExpression methodCall = Expression.Call(null, method, new Expression[] - { - propertyNameParam, - valueParam, - destructureObjectsParam - }); - var func = Expression.Lambda>( - methodCall, - propertyNameParam, - valueParam, - destructureObjectsParam) - .Compile(); - return name => func("Name", name, false); - } - - internal class SerilogLogger - { - private readonly object _logger; - private static readonly object DebugLevel; - private static readonly object ErrorLevel; - private static readonly object FatalLevel; - private static readonly object InformationLevel; - private static readonly object VerboseLevel; - private static readonly object WarningLevel; - private static readonly Func IsEnabled; - private static readonly Action Write; - private static readonly Action WriteException; - - [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")] - [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")] - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "ILogger")] - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "LogEventLevel")] - [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "Serilog")] - static SerilogLogger() - { - var logEventLevelType = Type.GetType("Serilog.Events.LogEventLevel, Serilog"); - if (logEventLevelType == null) - { - throw new InvalidOperationException("Type Serilog.Events.LogEventLevel was not found."); - } - DebugLevel = Enum.Parse(logEventLevelType, "Debug", false); - ErrorLevel = Enum.Parse(logEventLevelType, "Error", false); - FatalLevel = Enum.Parse(logEventLevelType, "Fatal", false); - InformationLevel = Enum.Parse(logEventLevelType, "Information", false); - VerboseLevel = Enum.Parse(logEventLevelType, "Verbose", false); - WarningLevel = Enum.Parse(logEventLevelType, "Warning", false); - - // Func isEnabled = (logger, level) => { return ((SeriLog.ILogger)logger).IsEnabled(level); } - var loggerType = Type.GetType("Serilog.ILogger, Serilog"); - if (loggerType == null) - { - throw new InvalidOperationException("Type Serilog.ILogger was not found."); - } - MethodInfo isEnabledMethodInfo = loggerType.GetMethodPortable("IsEnabled", logEventLevelType); - ParameterExpression instanceParam = Expression.Parameter(typeof(object)); - UnaryExpression instanceCast = Expression.Convert(instanceParam, loggerType); - ParameterExpression levelParam = Expression.Parameter(typeof(object)); - UnaryExpression levelCast = Expression.Convert(levelParam, logEventLevelType); - MethodCallExpression isEnabledMethodCall = Expression.Call(instanceCast, isEnabledMethodInfo, levelCast); - IsEnabled = Expression.Lambda>(isEnabledMethodCall, instanceParam, levelParam).Compile(); - - // Action Write = - // (logger, level, message, params) => { ((SeriLog.ILoggerILogger)logger).Write(level, message, params); } - MethodInfo writeMethodInfo = loggerType.GetMethodPortable("Write", logEventLevelType, typeof(string), typeof(object[])); - ParameterExpression messageParam = Expression.Parameter(typeof(string)); - ParameterExpression propertyValuesParam = Expression.Parameter(typeof(object[])); - MethodCallExpression writeMethodExp = Expression.Call( - instanceCast, - writeMethodInfo, - levelCast, - messageParam, - propertyValuesParam); - var expression = Expression.Lambda>( - writeMethodExp, - instanceParam, - levelParam, - messageParam, - propertyValuesParam); - Write = expression.Compile(); - - // Action WriteException = - // (logger, level, exception, message) => { ((ILogger)logger).Write(level, exception, message, new object[]); } - MethodInfo writeExceptionMethodInfo = loggerType.GetMethodPortable("Write", - logEventLevelType, - typeof(Exception), - typeof(string), - typeof(object[])); - ParameterExpression exceptionParam = Expression.Parameter(typeof(Exception)); - writeMethodExp = Expression.Call( - instanceCast, - writeExceptionMethodInfo, - levelCast, - exceptionParam, - messageParam, - propertyValuesParam); - WriteException = Expression.Lambda>( - writeMethodExp, - instanceParam, - levelParam, - exceptionParam, - messageParam, - propertyValuesParam).Compile(); - } - - internal SerilogLogger(object logger) - { - _logger = logger; - } - - public bool Log(LogLevel logLevel, Func messageFunc, Exception exception, params object[] formatParameters) - { - if (messageFunc == null) - { - return IsEnabled(_logger, logLevel); - } - if (exception != null) - { - return LogException(logLevel, messageFunc, exception, formatParameters); - } - - switch (logLevel) - { - case LogLevel.Debug: - if (IsEnabled(_logger, DebugLevel)) - { - Write(_logger, DebugLevel, messageFunc(), formatParameters); - return true; - } - break; - case LogLevel.Info: - if (IsEnabled(_logger, InformationLevel)) - { - Write(_logger, InformationLevel, messageFunc(), formatParameters); - return true; - } - break; - case LogLevel.Warn: - if (IsEnabled(_logger, WarningLevel)) - { - Write(_logger, WarningLevel, messageFunc(), formatParameters); - return true; - } - break; - case LogLevel.Error: - if (IsEnabled(_logger, ErrorLevel)) - { - Write(_logger, ErrorLevel, messageFunc(), formatParameters); - return true; - } - break; - case LogLevel.Fatal: - if (IsEnabled(_logger, FatalLevel)) - { - Write(_logger, FatalLevel, messageFunc(), formatParameters); - return true; - } - break; - default: - if (IsEnabled(_logger, VerboseLevel)) - { - Write(_logger, VerboseLevel, messageFunc(), formatParameters); - return true; - } - break; - } - return false; - } - - private bool LogException(LogLevel logLevel, Func messageFunc, Exception exception, object[] formatParams) - { - switch (logLevel) - { - case LogLevel.Debug: - if (IsEnabled(_logger, DebugLevel)) - { - WriteException(_logger, DebugLevel, exception, messageFunc(), formatParams); - return true; - } - break; - case LogLevel.Info: - if (IsEnabled(_logger, InformationLevel)) - { - WriteException(_logger, InformationLevel, exception, messageFunc(), formatParams); - return true; - } - break; - case LogLevel.Warn: - if (IsEnabled(_logger, WarningLevel)) - { - WriteException(_logger, WarningLevel, exception, messageFunc(), formatParams); - return true; - } - break; - case LogLevel.Error: - if (IsEnabled(_logger, ErrorLevel)) - { - WriteException(_logger, ErrorLevel, exception, messageFunc(), formatParams); - return true; - } - break; - case LogLevel.Fatal: - if (IsEnabled(_logger, FatalLevel)) - { - WriteException(_logger, FatalLevel, exception, messageFunc(), formatParams); - return true; - } - break; - default: - if (IsEnabled(_logger, VerboseLevel)) - { - WriteException(_logger, VerboseLevel, exception, messageFunc(), formatParams); - return true; - } - break; - } - return false; - } - } - } - - internal class LoupeLogProvider : LogProviderBase - { - /// - /// The form of the Loupe Log.Write method we're using - /// - internal delegate void WriteDelegate( - int severity, - string logSystem, - int skipFrames, - Exception exception, - bool attributeToException, - int writeMode, - string detailsXml, - string category, - string caption, - string description, - params object[] args - ); - - private static bool s_providerIsAvailableOverride = true; - private readonly WriteDelegate _logWriteDelegate; - - public LoupeLogProvider() - { - if (!IsLoggerAvailable()) - { - throw new InvalidOperationException("Gibraltar.Agent.Log (Loupe) not found"); - } - - _logWriteDelegate = GetLogWriteDelegate(); - } - - /// - /// Gets or sets a value indicating whether [provider is available override]. Used in tests. - /// - /// - /// true if [provider is available override]; otherwise, false. - /// - public static bool ProviderIsAvailableOverride - { - get { return s_providerIsAvailableOverride; } - set { s_providerIsAvailableOverride = value; } - } - - public override Logger GetLogger(string name) - { - return new LoupeLogger(name, _logWriteDelegate).Log; - } - - public static bool IsLoggerAvailable() - { - return ProviderIsAvailableOverride && GetLogManagerType() != null; - } - - private static Type GetLogManagerType() - { - return Type.GetType("Gibraltar.Agent.Log, Gibraltar.Agent"); - } - - private static WriteDelegate GetLogWriteDelegate() - { - Type logManagerType = GetLogManagerType(); - Type logMessageSeverityType = Type.GetType("Gibraltar.Agent.LogMessageSeverity, Gibraltar.Agent"); - Type logWriteModeType = Type.GetType("Gibraltar.Agent.LogWriteMode, Gibraltar.Agent"); - - MethodInfo method = logManagerType.GetMethodPortable( - "Write", - logMessageSeverityType, typeof(string), typeof(int), typeof(Exception), typeof(bool), - logWriteModeType, typeof(string), typeof(string), typeof(string), typeof(string), typeof(object[])); - - var callDelegate = (WriteDelegate)method.CreateDelegate(typeof(WriteDelegate)); - return callDelegate; - } - - internal class LoupeLogger - { - private const string LogSystem = "LibLog"; - - private readonly string _category; - private readonly WriteDelegate _logWriteDelegate; - private readonly int _skipLevel; - - internal LoupeLogger(string category, WriteDelegate logWriteDelegate) - { - _category = category; - _logWriteDelegate = logWriteDelegate; -#if DEBUG - _skipLevel = 2; -#else - _skipLevel = 1; -#endif - } - - public bool Log(LogLevel logLevel, Func messageFunc, Exception exception, params object[] formatParameters) - { - if (messageFunc == null) - { - //nothing to log.. - return true; - } - - messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters); - - _logWriteDelegate(ToLogMessageSeverity(logLevel), LogSystem, _skipLevel, exception, true, 0, null, - _category, null, messageFunc.Invoke()); - - return true; - } - - private static int ToLogMessageSeverity(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Trace: - return TraceEventTypeValues.Verbose; - case LogLevel.Debug: - return TraceEventTypeValues.Verbose; - case LogLevel.Info: - return TraceEventTypeValues.Information; - case LogLevel.Warn: - return TraceEventTypeValues.Warning; - case LogLevel.Error: - return TraceEventTypeValues.Error; - case LogLevel.Fatal: - return TraceEventTypeValues.Critical; - default: - throw new ArgumentOutOfRangeException("logLevel"); - } - } - } - } - - internal static class TraceEventTypeValues - { - internal static readonly Type Type; - internal static readonly int Verbose; - internal static readonly int Information; - internal static readonly int Warning; - internal static readonly int Error; - internal static readonly int Critical; - - [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")] - static TraceEventTypeValues() - { - var assembly = typeof(Uri).GetAssemblyPortable(); // This is to get to the System.dll assembly in a PCL compatible way. - if (assembly == null) - { - return; - } - Type = assembly.GetType("System.Diagnostics.TraceEventType"); - if (Type == null) return; - Verbose = (int)Enum.Parse(Type, "Verbose", false); - Information = (int)Enum.Parse(Type, "Information", false); - Warning = (int)Enum.Parse(Type, "Warning", false); - Error = (int)Enum.Parse(Type, "Error", false); - Critical = (int)Enum.Parse(Type, "Critical", false); - } - } - - internal static class LogMessageFormatter - { - private static readonly Regex Pattern = new Regex(@"\{@?\w{1,}\}"); - - /// - /// Some logging frameworks support structured logging, such as serilog. This will allow you to add names to structured data in a format string: - /// For example: Log("Log message to {user}", user). This only works with serilog, but as the user of LibLog, you don't know if serilog is actually - /// used. So, this class simulates that. it will replace any text in {curly braces} with an index number. - /// - /// "Log {message} to {user}" would turn into => "Log {0} to {1}". Then the format parameters are handled using regular .net string.Format. - /// - /// The message builder. - /// The format parameters. - /// - public static Func SimulateStructuredLogging(Func messageBuilder, object[] formatParameters) - { - if (formatParameters == null || formatParameters.Length == 0) - { - return messageBuilder; - } - - return () => - { - string targetMessage = messageBuilder(); - int argumentIndex = 0; - foreach (Match match in Pattern.Matches(targetMessage)) - { - int notUsed; - if (!int.TryParse(match.Value.Substring(1, match.Value.Length -2), out notUsed)) - { - targetMessage = ReplaceFirst(targetMessage, match.Value, - "{" + argumentIndex++ + "}"); - } - } - try - { - return string.Format(CultureInfo.InvariantCulture, targetMessage, formatParameters); - } - catch (FormatException ex) - { - throw new FormatException("The input string '" + targetMessage + "' could not be formatted using string.Format", ex); - } - }; - } - - private static string ReplaceFirst(string text, string search, string replace) - { - int pos = text.IndexOf(search, StringComparison.Ordinal); - if (pos < 0) - { - return text; - } - return text.Substring(0, pos) + replace + text.Substring(pos + search.Length); - } - } - - internal static class TypeExtensions - { - internal static MethodInfo GetMethodPortable(this Type type, string name) - { -#if LIBLOG_PORTABLE - return type.GetRuntimeMethods().SingleOrDefault(m => m.Name == name); -#else - return type.GetMethod(name); -#endif - } - - internal static MethodInfo GetMethodPortable(this Type type, string name, params Type[] types) - { -#if LIBLOG_PORTABLE - return type.GetRuntimeMethod(name, types); -#else - return type.GetMethod(name, types); -#endif - } - - internal static PropertyInfo GetPropertyPortable(this Type type, string name) - { -#if LIBLOG_PORTABLE - return type.GetRuntimeProperty(name); -#else - return type.GetProperty(name); -#endif - } - - internal static IEnumerable GetFieldsPortable(this Type type) - { -#if LIBLOG_PORTABLE - return type.GetRuntimeFields(); -#else - return type.GetFields(); -#endif - } - - internal static Type GetBaseTypePortable(this Type type) - { -#if LIBLOG_PORTABLE - return type.GetTypeInfo().BaseType; -#else - return type.BaseType; -#endif - } - -#if LIBLOG_PORTABLE - internal static MethodInfo GetGetMethod(this PropertyInfo propertyInfo) - { - return propertyInfo.GetMethod; - } - - internal static MethodInfo GetSetMethod(this PropertyInfo propertyInfo) - { - return propertyInfo.SetMethod; - } -#endif - -#if !LIBLOG_PORTABLE - internal static object CreateDelegate(this MethodInfo methodInfo, Type delegateType) - { - return Delegate.CreateDelegate(delegateType, methodInfo); - } -#endif - - internal static Assembly GetAssemblyPortable(this Type type) - { -#if LIBLOG_PORTABLE - return type.GetTypeInfo().Assembly; -#else - return type.Assembly; -#endif - } - } - - internal class DisposableAction : IDisposable - { - private readonly Action _onDispose; - - public DisposableAction(Action onDispose = null) - { - _onDispose = onDispose; - } - - public void Dispose() - { - if(_onDispose != null) - { - _onDispose(); - } - } - } -} diff --git a/src/ScriptCs.Contracts/LogExtensions.cs b/src/ScriptCs.Contracts/LogExtensions.cs deleted file mode 100644 index 59f170c9..00000000 --- a/src/ScriptCs.Contracts/LogExtensions.cs +++ /dev/null @@ -1,251 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System; - - public static class LogExtensions - { - public static bool IsDebugEnabled(this ILog logger) - { - Guard.AgainstNullArgument("logger", logger); - return logger.Log(LogLevel.Debug); - } - - public static bool IsErrorEnabled(this ILog logger) - { - Guard.AgainstNullArgument("logger", logger); - return logger.Log(LogLevel.Error); - } - - public static bool IsFatalEnabled(this ILog logger) - { - Guard.AgainstNullArgument("logger", logger); - return logger.Log(LogLevel.Fatal); - } - - public static bool IsInfoEnabled(this ILog logger) - { - Guard.AgainstNullArgument("logger", logger); - return logger.Log(LogLevel.Info); - } - - public static bool IsTraceEnabled(this ILog logger) - { - Guard.AgainstNullArgument("logger", logger); - return logger.Log(LogLevel.Trace); - } - - public static bool IsWarnEnabled(this ILog logger) - { - Guard.AgainstNullArgument("logger", logger); - return logger.Log(LogLevel.Warn); - } - - public static void Debug(this ILog logger, Func messageFunc) - { - Guard.AgainstNullArgument("logger", logger); - logger.Log(LogLevel.Debug, messageFunc); - } - - public static void Debug(this ILog logger, string message) - { - if (logger.IsDebugEnabled()) - { - logger.Log(LogLevel.Debug, message.AsFunc()); - } - } - - public static void DebugFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsDebugEnabled()) - { - logger.LogFormat(LogLevel.Debug, message, args); - } - } - - public static void DebugException(this ILog logger, string message, Exception exception) - { - if (logger.IsDebugEnabled()) - { - logger.Log(LogLevel.Debug, message.AsFunc(), exception); - } - } - - public static void DebugException( - this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsDebugEnabled()) - { - logger.Log(LogLevel.Debug, message.AsFunc(), exception, formatParams); - } - } - - public static void Error(this ILog logger, Func messageFunc) - { - logger.Log(LogLevel.Error, messageFunc); - } - - public static void Error(this ILog logger, string message) - { - if (logger.IsErrorEnabled()) - { - logger.Log(LogLevel.Error, message.AsFunc()); - } - } - - public static void ErrorFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsErrorEnabled()) - { - logger.LogFormat(LogLevel.Error, message, args); - } - } - - public static void ErrorException( - this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsErrorEnabled()) - { - logger.Log(LogLevel.Error, message.AsFunc(), exception, formatParams); - } - } - - public static void Fatal(this ILog logger, Func messageFunc) - { - logger.Log(LogLevel.Fatal, messageFunc); - } - - public static void Fatal(this ILog logger, string message) - { - if (logger.IsFatalEnabled()) - { - logger.Log(LogLevel.Fatal, message.AsFunc()); - } - } - - public static void FatalFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsFatalEnabled()) - { - logger.LogFormat(LogLevel.Fatal, message, args); - } - } - - public static void FatalException( - this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsFatalEnabled()) - { - logger.Log(LogLevel.Fatal, message.AsFunc(), exception, formatParams); - } - } - - public static void Info(this ILog logger, Func messageFunc) - { - Guard.AgainstNullArgument("logger", logger); - logger.Log(LogLevel.Info, messageFunc); - } - - public static void Info(this ILog logger, string message) - { - if (logger.IsInfoEnabled()) - { - logger.Log(LogLevel.Info, message.AsFunc()); - } - } - - public static void InfoFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsInfoEnabled()) - { - logger.LogFormat(LogLevel.Info, message, args); - } - } - - public static void InfoException( - this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsInfoEnabled()) - { - logger.Log(LogLevel.Info, message.AsFunc(), exception, formatParams); - } - } - - public static void Trace(this ILog logger, Func messageFunc) - { - Guard.AgainstNullArgument("logger", logger); - logger.Log(LogLevel.Trace, messageFunc); - } - - public static void Trace(this ILog logger, string message) - { - if (logger.IsTraceEnabled()) - { - logger.Log(LogLevel.Trace, message.AsFunc()); - } - } - - public static void TraceFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsTraceEnabled()) - { - logger.LogFormat(LogLevel.Trace, message, args); - } - } - - public static void TraceException( - this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsTraceEnabled()) - { - logger.Log(LogLevel.Trace, message.AsFunc(), exception, formatParams); - } - } - - public static void Warn(this ILog logger, Func messageFunc) - { - Guard.AgainstNullArgument("logger", logger); - logger.Log(LogLevel.Warn, messageFunc); - } - - public static void Warn(this ILog logger, string message) - { - if (logger.IsWarnEnabled()) - { - logger.Log(LogLevel.Warn, message.AsFunc()); - } - } - - public static void WarnFormat(this ILog logger, string message, params object[] args) - { - if (logger.IsWarnEnabled()) - { - logger.LogFormat(LogLevel.Warn, message, args); - } - } - - public static void WarnException( - this ILog logger, string message, Exception exception, params object[] formatParams) - { - if (logger.IsWarnEnabled()) - { - logger.Log(LogLevel.Warn, message.AsFunc(), exception, formatParams); - } - } - - private static void LogFormat(this ILog logger, LogLevel logLevel, string message, params object[] args) - { - logger.Log(logLevel, message.AsFunc(), null, args); - } - - // Avoid the closure allocation, see https://gist.github.com/AArnott/d285feef75c18f6ecd2b - private static Func AsFunc(this T value) where T : class - { - return value.Return; - } - - private static T Return(this T value) - { - return value; - } - } -} diff --git a/src/ScriptCs.Contracts/LogLevel.cs b/src/ScriptCs.Contracts/LogLevel.cs deleted file mode 100644 index 0a59e0c5..00000000 --- a/src/ScriptCs.Contracts/LogLevel.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace ScriptCs.Contracts -{ - public enum LogLevel - { - Trace, - Debug, - Info, - Warn, - Error, - Fatal - } -} diff --git a/src/ScriptCs.Contracts/LogProviderExtensions.cs b/src/ScriptCs.Contracts/LogProviderExtensions.cs deleted file mode 100644 index 8bca68bd..00000000 --- a/src/ScriptCs.Contracts/LogProviderExtensions.cs +++ /dev/null @@ -1,67 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System; - using System.Diagnostics; - using System.Runtime.CompilerServices; - - public static class LogProviderExtensions - { - public static ILog For(this ILogProvider provider) - { - return provider.For(typeof(T)); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - public static ILog ForCurrentType(this ILogProvider provider) - { - return provider.For(new StackFrame(1, false).GetMethod().DeclaringType); - } - - public static ILog For(this ILogProvider provider, Type type) - { - return provider.For(type.FullName); - } - - public static ILog For(this ILogProvider provider, string name) - { - return new LoggerExecutionWrapper(provider.GetLogger(name)); - } - - private class LoggerExecutionWrapper : ILog - { - private const string FailedToGenerateLogMessage = "Failed to generate log message"; - - private readonly Logger _logger; - - internal LoggerExecutionWrapper(Logger logger) - { - _logger = logger; - } - - public bool Log( - LogLevel logLevel, Func createMessage, Exception exception = null, params object[] formatArgs) - { - if (createMessage == null) - { - return _logger(logLevel, null); - } - - Func wrappedMessageFunc = () => - { - try - { - return createMessage(); - } - catch (Exception ex) - { - Log(LogLevel.Error, () => FailedToGenerateLogMessage, ex); - } - - return null; - }; - - return _logger(logLevel, wrappedMessageFunc, exception, formatArgs); - } - } - } -} diff --git a/src/ScriptCs.Contracts/Logger.cs b/src/ScriptCs.Contracts/Logger.cs deleted file mode 100644 index 71e8b661..00000000 --- a/src/ScriptCs.Contracts/Logger.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Contracts -{ - using System; - - public delegate bool Logger( - LogLevel logLevel, Func messageFunc, Exception exception = null, params object[] formatParameters); -} diff --git a/src/ScriptCs.Contracts/ModuleAttribute.cs b/src/ScriptCs.Contracts/ModuleAttribute.cs deleted file mode 100644 index ddc4fc3a..00000000 --- a/src/ScriptCs.Contracts/ModuleAttribute.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.ComponentModel.Composition; - -namespace ScriptCs.Contracts -{ - [MetadataAttribute] - [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] - public class ModuleAttribute : ExportAttribute, IModuleMetadata - { - public ModuleAttribute(string name) : base(typeof(IModule)) - { - Name = name; - Autoload = false; - } - - public string Name { get; private set; } - - public string Extensions { get; set; } - - public bool Autoload { get; set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/Printers.cs b/src/ScriptCs.Contracts/Printers.cs deleted file mode 100644 index 73a8e9ab..00000000 --- a/src/ScriptCs.Contracts/Printers.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public class Printers - { - private readonly IObjectSerializer _serializer; - private readonly Dictionary> _dictionary = new Dictionary>(); - public Printers(IObjectSerializer serializer) - { - _serializer = serializer; - } - - public void AddCustomPrinter(Func printer) - { - _dictionary[typeof(T)] = x => printer((T)x); - } - - private string GetStringFor(Type t, object obj) - { - Func printer; - if (_dictionary.TryGetValue(t, out printer)) - { - return printer(obj); - } - return _serializer.Serialize(obj); - } - - public string GetStringFor(object obj) - { - return GetStringFor(obj.GetType(), obj); - } - - public string GetStringFor(T obj) - { - return GetStringFor(typeof(T), obj); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/ProjectItem.cs b/src/ScriptCs.Contracts/ProjectItem.cs deleted file mode 100644 index 5a11d254..00000000 --- a/src/ScriptCs.Contracts/ProjectItem.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace ScriptCs.Contracts -{ - public class ProjectItem - { - public ProjectItem(Guid project, Guid parent) - { - Project = project; - Parent = parent; - } - - public Guid Parent { get; private set; } - public Guid Project { get; private set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/Properties/ScriptCs.Contracts.nuspec b/src/ScriptCs.Contracts/Properties/ScriptCs.Contracts.nuspec deleted file mode 100644 index be678b1b..00000000 --- a/src/ScriptCs.Contracts/Properties/ScriptCs.Contracts.nuspec +++ /dev/null @@ -1,17 +0,0 @@ - - - - ScriptCs.Contracts - $version$ - Glenn Block, Filip Wojcieszyn, Justin Rusbatch, Kristian Hellang, Damian Schenkelman, Adam Ralph - Glenn Block, Justin Rusbatch, Filip Wojcieszyn - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Contracts contains the components necessary to create script packs for scriptcs. - roslyn csx script packs - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj b/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj deleted file mode 100644 index 8d7c12e5..00000000 --- a/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj +++ /dev/null @@ -1,26 +0,0 @@ - - - 1.0.0 - netstandard2.0 - ScriptCs.Contracts - Glenn Block, Filip Wojcieszyn, Justin Rusbatch - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Contracts - ScriptCs.Contracts contains the components necessary to create script packs for scriptcs. - roslyn csx script packs scriptcs - LIBLOG_PROVIDERS_ONLY - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Contracts/ScriptPack.cs b/src/ScriptCs.Contracts/ScriptPack.cs deleted file mode 100644 index 1abeb829..00000000 --- a/src/ScriptCs.Contracts/ScriptPack.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace ScriptCs.Contracts -{ - public abstract class ScriptPack : IScriptPack where TContext : IScriptPackContext - { - public TContext Context { get; set; } - - public virtual void Initialize(IScriptPackSession session) - { - } - - public virtual IScriptPackContext GetContext() - { - return Context; - } - - public virtual void Terminate() - { - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Contracts/ScriptPackSession.cs b/src/ScriptCs.Contracts/ScriptPackSession.cs deleted file mode 100644 index 80851347..00000000 --- a/src/ScriptCs.Contracts/ScriptPackSession.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - - -namespace ScriptCs.Contracts -{ - public class ScriptPackSession : IScriptPackSession - { - private readonly IEnumerable _scriptPacks; - - private readonly string[] _scriptArgs; - - private readonly IEnumerable _contexts; - - private readonly IDictionary _state; - - private readonly IList _references; - - private readonly IList _namespaces; - - public ScriptPackSession(IEnumerable scriptPacks, string[] scriptArgs) - { - _scriptPacks = scriptPacks; - _scriptArgs = scriptArgs; - _contexts = _scriptPacks.Select(s => s.GetContext()).Where(c => c != null); - _references = new List(); - _namespaces = new List(); - _state = new Dictionary(); - AddScriptContextNamespace(); - } - - private void AddScriptContextNamespace() - { - foreach (var context in _contexts) - { - _namespaces.Add(context.GetType().Namespace); - } - } - - public virtual IEnumerable Contexts => _contexts; - - public IEnumerable References => _references; - - public IEnumerable Namespaces => _namespaces; - - public void InitializePacks() - { - foreach (var s in _scriptPacks) - { - s.Initialize(this); - } - } - - public void TerminatePacks() - { - foreach (var s in _scriptPacks) - { - s.Terminate(); - } - } - - public IDictionary State => _state; - - public string[] ScriptArgs => _scriptArgs; - - void IScriptPackSession.AddReference(string assemblyDisplayNameOrPath) - { - _references.Add(assemblyDisplayNameOrPath); - } - - void IScriptPackSession.ImportNamespace(string @namespace) - { - _namespaces.Add(@namespace); - } - - } -} diff --git a/src/ScriptCs.Contracts/ScriptResult.cs b/src/ScriptCs.Contracts/ScriptResult.cs deleted file mode 100644 index f02001af..00000000 --- a/src/ScriptCs.Contracts/ScriptResult.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Linq; -using System.Collections.Generic; -using System.Runtime.ExceptionServices; - -namespace ScriptCs.Contracts -{ - public class ScriptResult - { - private readonly HashSet _invalidNamespaces = new HashSet(); - - public static readonly ScriptResult Empty = new ScriptResult(); - - public static readonly ScriptResult Incomplete = new ScriptResult { IsCompleteSubmission = false }; - - public ScriptResult() - { - // Explicit default ctor to use as mock return value. - IsCompleteSubmission = true; - } - - public ScriptResult( - object returnValue = null, - Exception executionException = null, - Exception compilationException = null, - IEnumerable invalidNamespaces = null) - { - if (returnValue != null) - { - ReturnValue = returnValue; - } - - if (executionException != null) - { - ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(executionException); - } - - if (compilationException != null) - { - CompileExceptionInfo = ExceptionDispatchInfo.Capture(compilationException); - } - - if (invalidNamespaces != null) - { - foreach (var ns in invalidNamespaces.Distinct()) - { - _invalidNamespaces.Add(ns); - } - } - - IsCompleteSubmission = true; - } - - public object ReturnValue { get; private set; } - - public ExceptionDispatchInfo ExecuteExceptionInfo { get; private set; } - - public ExceptionDispatchInfo CompileExceptionInfo { get; private set; } - - public IEnumerable InvalidNamespaces => _invalidNamespaces.ToArray(); - - public bool IsCompleteSubmission { get; private set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/AppDomainAssemblyResolver.cs b/src/ScriptCs.Core/AppDomainAssemblyResolver.cs deleted file mode 100644 index 925420fc..00000000 --- a/src/ScriptCs.Core/AppDomainAssemblyResolver.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using ScriptCs.Contracts; -using System.IO; - -namespace ScriptCs -{ - public class AppDomainAssemblyResolver : IAppDomainAssemblyResolver - { - private readonly ILog _logger; - private readonly IFileSystem _fileSystem; - private readonly IAssemblyResolver _resolver; - private readonly IAssemblyUtility _assemblyUtility; - private readonly IDictionary _assemblyInfoMap; - - public AppDomainAssemblyResolver( - ILogProvider logProvider, - IFileSystem fileSystem, - IAssemblyResolver resolver, - IAssemblyUtility assemblyUtility, - IDictionary assemblyInfoMap = null, - Func resolveHandler = null) - { - Guard.AgainstNullArgument("logProvider", logProvider); - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("resolver", resolver); - Guard.AgainstNullArgument("assemblyUtility", assemblyUtility); - - _assemblyInfoMap = assemblyInfoMap ?? new Dictionary(); - _assemblyUtility = assemblyUtility; - _logger = logProvider.ForCurrentType(); - _fileSystem = fileSystem; - _resolver = resolver; - - if (resolveHandler == null) - { - resolveHandler = AssemblyResolve; - } - - AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(resolveHandler); - } - - internal Assembly AssemblyResolve(object sender, ResolveEventArgs args) - { - AssemblyInfo assemblyInfo; - var name = new AssemblyName(args.Name); - - if (_assemblyInfoMap.TryGetValue(name.Name, out assemblyInfo)) - { - lock (assemblyInfo) - { - if (assemblyInfo.Assembly == null) - { - assemblyInfo.Assembly = _assemblyUtility.LoadFile(assemblyInfo.Path); - } - } - - _logger.DebugFormat("Resolving from: {0} to: {1}", args.Name, assemblyInfo.Assembly.GetName()); - return assemblyInfo.Assembly; - } - - return null; - } - - public void Initialize() - { - var hostAssemblyPaths = _fileSystem.EnumerateBinaries(_fileSystem.HostBin, SearchOption.TopDirectoryOnly); - AddAssemblyPaths(hostAssemblyPaths); - - var globalPaths = _resolver.GetAssemblyPaths(_fileSystem.GlobalFolder, true); - AddAssemblyPaths(globalPaths); - - var scriptAssemblyPaths = _resolver.GetAssemblyPaths(_fileSystem.CurrentDirectory, true); - AddAssemblyPaths(scriptAssemblyPaths); - } - - public virtual void AddAssemblyPaths(IEnumerable assemblyPaths) - { - Guard.AgainstNullArgument("assemblyPaths", assemblyPaths); - - foreach (var assemblyPath in assemblyPaths) - { - if (_assemblyUtility.IsManagedAssembly(assemblyPath)) - { - var info = new AssemblyInfo { Path = assemblyPath }; - var name = _assemblyUtility.GetAssemblyName(assemblyPath); - info.Version = name.Version; - - AssemblyInfo foundInfo; - var found = _assemblyInfoMap.TryGetValue(name.Name, out foundInfo); - - if (!found || foundInfo.Version.CompareTo(info.Version) < 0) - { - // if the assembly being passed is a higher version - // and an assembly with it's name has already been resolved - if (found && foundInfo.Assembly != null) - { - _logger.WarnFormat( - "Conflict: Assembly {0} with version {1} cannot be added as it has already been resolved", - assemblyPath, - info.Version); - - continue; - } - - _logger.DebugFormat("Mapping Assembly {0} to version:{1}", name.Name, name.Version); - _assemblyInfoMap[name.Name] = info; - } - } - else - { - _logger.DebugFormat("Skipping Mapping Native Assembly {0}", assemblyPath); - } - } - } - } -} diff --git a/src/ScriptCs.Core/AssemblyInfo.cs b/src/ScriptCs.Core/AssemblyInfo.cs deleted file mode 100644 index fb45aac9..00000000 --- a/src/ScriptCs.Core/AssemblyInfo.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Reflection; - -namespace ScriptCs -{ - public class AssemblyInfo - { - public string Path { get; set; } - public Assembly Assembly { get; set; } - public Version Version { get; set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/AssemblyResolver.cs b/src/ScriptCs.Core/AssemblyResolver.cs deleted file mode 100644 index d3a0f058..00000000 --- a/src/ScriptCs.Core/AssemblyResolver.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class AssemblyResolver : IAssemblyResolver - { - private readonly Dictionary> _assemblyPathCache = new Dictionary>(); - private readonly IFileSystem _fileSystem; - private readonly IPackageAssemblyResolver _packageAssemblyResolver; - private readonly IAssemblyUtility _assemblyUtility; - private readonly ILog _logger; - - public AssemblyResolver( - IFileSystem fileSystem, - IPackageAssemblyResolver packageAssemblyResolver, - IAssemblyUtility assemblyUtility, - ILogProvider logProvider) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgumentProperty("fileSystem", "PackagesFolder", fileSystem.PackagesFolder); - Guard.AgainstNullArgumentProperty("fileSystem", "BinFolder", fileSystem.BinFolder); - - Guard.AgainstNullArgument("packageAssemblyResolver", packageAssemblyResolver); - Guard.AgainstNullArgument("assemblyUtility", assemblyUtility); - Guard.AgainstNullArgument("logProvider", logProvider); - - _fileSystem = fileSystem; - _packageAssemblyResolver = packageAssemblyResolver; - _assemblyUtility = assemblyUtility; - _logger = logProvider.ForCurrentType(); - } - - public IEnumerable GetAssemblyPaths(string path, bool binariesOnly = false) - { - Guard.AgainstNullArgument("path", path); - - List assemblies; - if (!_assemblyPathCache.TryGetValue(path, out assemblies)) - { - assemblies = GetPackageAssemblyNames(path).Union(GetBinAssemblyPaths(path)).ToList(); - _assemblyPathCache.Add(path, assemblies); - } - - return binariesOnly - ? assemblies.Where(m => - m.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) || - m.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase)) - : assemblies.ToArray(); - } - - private IEnumerable GetBinAssemblyPaths(string path) - { - var binFolder = Path.Combine(path, _fileSystem.BinFolder); - if (!_fileSystem.DirectoryExists(binFolder)) - { - yield break; - } - - foreach (var assembly in _fileSystem.EnumerateBinaries(binFolder, SearchOption.TopDirectoryOnly) - .Where(f => _assemblyUtility.IsManagedAssembly(f))) - { - _logger.DebugFormat("Found assembly in bin folder: {0}", Path.GetFileName(assembly)); - yield return assembly; - } - } - - private IEnumerable GetPackageAssemblyNames(string path) - { - foreach (var assembly in _packageAssemblyResolver.GetAssemblyNames(path)) - { - _logger.DebugFormat("Found package assembly: {0}", Path.GetFileName(assembly)); - yield return assembly; - } - } - } -} diff --git a/src/ScriptCs.Core/AssemblyUtility.cs b/src/ScriptCs.Core/AssemblyUtility.cs deleted file mode 100644 index 80e37c0f..00000000 --- a/src/ScriptCs.Core/AssemblyUtility.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Reflection; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class AssemblyUtility : IAssemblyUtility - { - public bool IsManagedAssembly(string path) - { - try - { - AssemblyName.GetAssemblyName(path); - return true; - } - catch (BadImageFormatException) - { - return false; - } - } - - public Assembly LoadFile(string path) => Assembly.LoadFile(path); - - public Assembly Load(AssemblyName assemblyRef) => Assembly.Load(assemblyRef); - - public AssemblyName GetAssemblyName(string path) => AssemblyName.GetAssemblyName(path); - } -} diff --git a/src/ScriptCs.Core/Constants.cs b/src/ScriptCs.Core/Constants.cs deleted file mode 100644 index 99658500..00000000 --- a/src/ScriptCs.Core/Constants.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; - -namespace ScriptCs -{ - public static class Constants - { - [Obsolete("Use IFileSystem instead.")] - public const string PackagesFile = "packages.config"; - - [Obsolete("Use IFileSystem instead.")] - public const string NugetFile = "nuget.config"; - - [Obsolete("Use IFileSystem instead.")] - public const string PackagesFolder = "packages"; - - [Obsolete("Use IFileSystem instead.")] - public const string BinFolder = "bin"; - - [Obsolete("Use IFileSystem instead.")] - public const string DllCacheFolder = ".cache"; - - public const string ConfigFilename = "scriptcs.opts"; - - public const string DefaultRepositoryUrl = "https://nuget.org/api/v2/"; - } -} diff --git a/src/ScriptCs.Core/DebugScriptExecutor.cs b/src/ScriptCs.Core/DebugScriptExecutor.cs deleted file mode 100644 index b9764f8a..00000000 --- a/src/ScriptCs.Core/DebugScriptExecutor.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class DebugScriptExecutor : ScriptExecutor - { - public DebugScriptExecutor(IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, ILogProvider logProvider, IScriptLibraryComposer composer, IScriptInfo scriptInfo) - : base(fileSystem, filePreProcessor, scriptEngine, logProvider, composer, scriptInfo) - { - } - } -} diff --git a/src/ScriptCs.Core/Exceptions/ScriptCompilationException.cs b/src/ScriptCs.Core/Exceptions/ScriptCompilationException.cs deleted file mode 100644 index 4139054e..00000000 --- a/src/ScriptCs.Core/Exceptions/ScriptCompilationException.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace ScriptCs.Exceptions -{ - [Serializable] - public class ScriptCompilationException : Exception - { - public ScriptCompilationException(string message) - : base(message) - { - } - - public ScriptCompilationException(string message, Exception innerException) - : base(message, innerException) - { - } - - protected ScriptCompilationException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/Exceptions/ScriptExecutionException.cs b/src/ScriptCs.Core/Exceptions/ScriptExecutionException.cs deleted file mode 100644 index 866b979f..00000000 --- a/src/ScriptCs.Core/Exceptions/ScriptExecutionException.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace ScriptCs.Exceptions -{ - [Serializable] - public class ScriptExecutionException : Exception - { - public ScriptExecutionException(string message) - : base(message) - { - } - - public ScriptExecutionException(string message, Exception innerException) - : base(message, innerException) - { - } - - protected ScriptExecutionException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} diff --git a/src/ScriptCs.Core/Exceptions/ScriptPackException.cs b/src/ScriptCs.Core/Exceptions/ScriptPackException.cs deleted file mode 100644 index 7c98d5e9..00000000 --- a/src/ScriptCs.Core/Exceptions/ScriptPackException.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace ScriptCs.Exceptions -{ - [Serializable] - public class ScriptPackException : Exception - { - public ScriptPackException(string message) - : base(message) - { - } - - protected ScriptPackException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} diff --git a/src/ScriptCs.Core/Extensions/EnumerableExtensions.cs b/src/ScriptCs.Core/Extensions/EnumerableExtensions.cs deleted file mode 100644 index b7aa9ce7..00000000 --- a/src/ScriptCs.Core/Extensions/EnumerableExtensions.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace ScriptCs.Extensions -{ - internal static class EnumerableExtensions - { - internal static bool IsNullOrEmpty(this IEnumerable enumerable) - { - if (enumerable == null) - { - return true; - } - - var collection = enumerable as ICollection; - if (collection != null) - { - return collection.Count == 0; - } - - return !enumerable.Any(); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/Extensions/MethodInfoExtensions.cs b/src/ScriptCs.Core/Extensions/MethodInfoExtensions.cs deleted file mode 100644 index 5d083c63..00000000 --- a/src/ScriptCs.Core/Extensions/MethodInfoExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; - -namespace ScriptCs.Extensions -{ - internal static class MethodInfoExtensions - { - internal static IEnumerable GetParametersWithoutExtensions(this MethodInfo method) - { - IEnumerable methodParams = method.GetParameters(); - if (method.IsDefined(typeof(ExtensionAttribute), false)) - { - methodParams = methodParams.Skip(1); - } - - return methodParams; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/Extensions/TypeExtensions.cs b/src/ScriptCs.Core/Extensions/TypeExtensions.cs deleted file mode 100644 index 4d51b6a7..00000000 --- a/src/ScriptCs.Core/Extensions/TypeExtensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; - -namespace ScriptCs.Extensions -{ - internal static class TypeExtensions - { - internal static IEnumerable GetExtensionMethods(this Type type) - { - return type.Assembly.GetExportedTypes().Where(x => !x.IsGenericType && !x.IsNested && x.IsSealed) - .SelectMany(x => x.GetMethods(BindingFlags.Static | BindingFlags.Public)) - .Where(x => x.IsDefined(typeof(ExtensionAttribute), false)) - .Where(x => x.GetParameters()[0].ParameterType == type); - } - - internal static IEnumerable GetAllMethods(this Type type) - { - return type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly) - .Where(m => !m.IsSpecialName).Union(type.GetExtensionMethods()).OrderBy(x => x.Name); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/FilePreProcessor.cs b/src/ScriptCs.Core/FilePreProcessor.cs deleted file mode 100644 index 044d4ec0..00000000 --- a/src/ScriptCs.Core/FilePreProcessor.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class FilePreProcessor : IFilePreProcessor - { - private readonly ILog _logger; - - private readonly IEnumerable _lineProcessors; - private readonly IEnumerable _directiveLineProcessors; - - private readonly IFileSystem _fileSystem; - - public FilePreProcessor(IFileSystem fileSystem, ILogProvider logProvider, IEnumerable lineProcessors) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("logProvider", logProvider); - - _fileSystem = fileSystem; - _logger = logProvider.ForCurrentType(); - _lineProcessors = lineProcessors; - _directiveLineProcessors = _lineProcessors.OfType(); - } - - public virtual FilePreProcessorResult ProcessFile(string path) - { - return Process(context => ParseFile(path, context)); - } - - public virtual FilePreProcessorResult ProcessScript(string script) - { - var scriptLines = _fileSystem.SplitLines(script).ToList(); - return Process(context => ParseScript(scriptLines, context)); - } - - protected virtual FilePreProcessorResult Process(Action parseAction) - { - Guard.AgainstNullArgument("parseAction", parseAction); - - var context = new FileParserContext(); - - _logger.DebugFormat("Starting pre-processing"); - - parseAction(context); - - var code = GenerateCode(context); - - _logger.DebugFormat("Pre-processing finished successfully"); - - return new FilePreProcessorResult - { - Namespaces = context.Namespaces, - LoadedScripts = context.LoadedScripts, - References = context.References, - Code = code, - ScriptPath = context.ScriptPath - }; - } - - protected virtual string GenerateCode(FileParserContext context) - { - Guard.AgainstNullArgument("context", context); - return string.Join(_fileSystem.NewLine, context.BodyLines); - } - - public virtual void ParseFile(string path, FileParserContext context) - { - Guard.AgainstNullArgument("path", path); - Guard.AgainstNullArgument("context", context); - - var fullPath = _fileSystem.GetFullPath(path); - var filename = Path.GetFileName(path); - - if (context.LoadedScripts.Contains(fullPath)) - { - _logger.DebugFormat("Skipping {0} because it's already been loaded.", filename); - return; - } - - _logger.DebugFormat("Processing {0}...", filename); - - if (context.ScriptPath == null) - { - context.ScriptPath = fullPath; - } - else - { - // Add script to loaded collection before parsing to avoid loop. - context.LoadedScripts.Add(fullPath); - } - - var scriptLines = _fileSystem.ReadFileLines(fullPath).ToList(); - - InsertLineDirective(fullPath, scriptLines); - InDirectory(fullPath, () => ParseScript(scriptLines, context)); - } - - public virtual void ParseScript(List scriptLines, FileParserContext context) - { - Guard.AgainstNullArgument("scriptLines", scriptLines); - Guard.AgainstNullArgument("context", context); - - var codeIndex = scriptLines.FindIndex(IsNonDirectiveLine); - - for (var index = 0; index < scriptLines.Count; index++) - { - var line = scriptLines[index]; - var isBeforeCode = index < codeIndex || codeIndex < 0; - - var wasProcessed = _lineProcessors.Any(x => x.ProcessLine(this, context, line, isBeforeCode)); - if (!wasProcessed) - { - context.BodyLines.Add(line); - } - } - } - - protected virtual void InsertLineDirective(string path, List fileLines) - { - Guard.AgainstNullArgument("fileLines", fileLines); - - var bodyIndex = fileLines.FindIndex(line => IsNonDirectiveLine(line) && !IsUsingLine(line)); - if (bodyIndex == -1) - { - return; - } - - var directiveLine = string.Format("#line {0} \"{1}\"", bodyIndex + 1, path); - fileLines.Insert(bodyIndex, directiveLine); - } - - private void InDirectory(string path, Action action) - { - var oldCurrentDirectory = _fileSystem.CurrentDirectory; - _fileSystem.CurrentDirectory = _fileSystem.GetWorkingDirectory(path); - - action(); - - _fileSystem.CurrentDirectory = oldCurrentDirectory; - } - - private bool IsNonDirectiveLine(string line) - { - line = line.Trim(); - if (line.StartsWith("//") || line.Equals(string.Empty)) - return false; - - return !_directiveLineProcessors.Any(lp => lp.Matches(line)); - } - - private static bool IsUsingLine(string line) - { - return line.TrimStart(' ').StartsWith("using ") && !line.Contains("{") && line.Contains(";"); - } - } -} diff --git a/src/ScriptCs.Core/FileSystem.cs b/src/ScriptCs.Core/FileSystem.cs deleted file mode 100644 index 6bd0ef84..00000000 --- a/src/ScriptCs.Core/FileSystem.cs +++ /dev/null @@ -1,192 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class FileSystem : IFileSystem - { - public virtual IEnumerable EnumerateFiles( - string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories) - { - return Directory.EnumerateFiles(dir, searchPattern, searchOption); - } - - public virtual IEnumerable EnumerateDirectories( - string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories) - { - return Directory.EnumerateDirectories(dir, searchPattern, searchOption); - } - - public virtual IEnumerable EnumerateFilesAndDirectories( - string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories) - { - return Directory.EnumerateFileSystemEntries(dir, searchPattern, searchOption); - } - - public virtual void Copy(string source, string dest, bool overwrite) - { - File.Copy(source, dest, overwrite); - } - - public virtual void CopyDirectory(string source, string dest, bool overwrite) - { - // NOTE: adding guards since the exceptions thrown by System.IO would be confusing - Guard.AgainstNullArgument("source", source); - Guard.AgainstNullArgument("dest", dest); - - if (!Directory.Exists(dest)) - { - Directory.CreateDirectory(dest); - } - - foreach (var file in Directory.GetFiles(source)) - { - File.Copy(file, Path.Combine(dest, Path.GetFileName(file)), overwrite); - } - - foreach (var directory in Directory.GetDirectories(source)) - { - CopyDirectory(directory, Path.Combine(dest, Path.GetFileName(directory)), overwrite); - } - } - - public virtual bool DirectoryExists(string path) - { - return Directory.Exists(path); - } - - public virtual void CreateDirectory(string path, bool hidden) - { - var directory = Directory.CreateDirectory(path); - - if (hidden) - { - directory.Attributes = FileAttributes.Directory | FileAttributes.Hidden; - } - } - - public virtual void DeleteDirectory(string path) - { - Directory.Delete(path, true); - } - - public virtual string ReadFile(string path) - { - return File.ReadAllText(path); - } - - public virtual string[] ReadFileLines(string path) - { - return File.ReadAllLines(path); - } - - public virtual bool IsPathRooted(string path) - { - return Path.IsPathRooted(path); - } - - public virtual string CurrentDirectory - { - get => Environment.CurrentDirectory; - set => Environment.CurrentDirectory = value; - } - - public virtual string NewLine => Environment.NewLine; - - public virtual DateTime GetLastWriteTime(string file) - { - return File.GetLastWriteTime(file); - } - - public virtual void Move(string source, string dest) - { - File.Move(source, dest); - } - - public virtual void MoveDirectory(string source, string dest) - { - Directory.Move(source, dest); - } - - public virtual bool FileExists(string path) - { - return File.Exists(path); - } - - public virtual void FileDelete(string path) - { - File.Delete(path); - } - - public virtual IEnumerable SplitLines(string value) - { - Guard.AgainstNullArgument("value", value); - - return value.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); - } - - public virtual void WriteToFile(string path, string text) - { - File.WriteAllText(path, text); - } - - public virtual Stream CreateFileStream(string filePath, FileMode mode) - { - return new FileStream(filePath, mode); - } - - public virtual void WriteAllBytes(string filePath, byte[] bytes) - { - File.WriteAllBytes(filePath, bytes); - } - - public virtual string GlobalFolder => Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "scriptcs"); - - public virtual string GetWorkingDirectory(string path) - { - if (string.IsNullOrWhiteSpace(path)) - { - return CurrentDirectory; - } - - var realPath = GetFullPath(path); - - if (FileExists(realPath) || DirectoryExists(realPath)) - { - if ((File.GetAttributes(realPath) & FileAttributes.Directory) == FileAttributes.Directory) - { - return realPath; - } - - return Path.GetDirectoryName(realPath); - } - - return Path.GetDirectoryName(realPath); - } - - public virtual string GetFullPath(string path) - { - return Path.GetFullPath(path); - } - - - public virtual string TempPath => Path.GetTempPath(); - - public virtual string HostBin => AppDomain.CurrentDomain.BaseDirectory; - - public virtual string BinFolder => "scriptcs_bin"; - - public virtual string DllCacheFolder => ".scriptcs_cache"; - - public virtual string PackagesFile => "scriptcs_packages.config"; - - public virtual string PackagesFolder => "scriptcs_packages"; - - public virtual string NugetFile => "scriptcs_nuget.config"; - - public virtual string GlobalOptsFile => Path.Combine(GlobalFolder, Constants.ConfigFilename); - } -} diff --git a/src/ScriptCs.Core/FrameworkUtils.cs b/src/ScriptCs.Core/FrameworkUtils.cs deleted file mode 100644 index 6a1935fc..00000000 --- a/src/ScriptCs.Core/FrameworkUtils.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -namespace ScriptCs -{ - public static class FrameworkUtils - { - private static string _frameworkName; - public static string FrameworkName - { - get - { - if (_frameworkName == null) - { - // in order to handle the weird behavior of old nuget packages - // with NET Standard 2.0, we'll use the entry assembly if possible - var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly(); - - //Thanks to Dave Glick for this code contribution - var frameworkName = assembly.GetCustomAttributes(true) - .OfType() - .Select(x => x.FrameworkName) - .FirstOrDefault(); - _frameworkName = frameworkName; - } - return _frameworkName; - } - } - } -} diff --git a/src/ScriptCs.Core/Guard.cs b/src/ScriptCs.Core/Guard.cs deleted file mode 100644 index a5cbf173..00000000 --- a/src/ScriptCs.Core/Guard.cs +++ /dev/null @@ -1,113 +0,0 @@ -namespace ScriptCs -{ - using System; - using System.Diagnostics; - using System.Diagnostics.CodeAnalysis; - using System.Globalization; - - /// - /// Provides guard clauses. - /// - internal static class Guard - { - /// - /// Guards against a null argument. - /// - /// The type of the argument. - /// Name of the parameter. - /// The argument. - /// is null. - /// is restricted to reference types to avoid boxing of value type objects. - [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")] - [DebuggerStepThrough] - public static void AgainstNullArgument(string parameterName, [ValidatedNotNull]TArgument argument) where TArgument : class - { - if (argument == null) - { - throw new ArgumentNullException(parameterName, string.Format(CultureInfo.InvariantCulture, "{0} is null.", parameterName)); - } - } - - /// - /// Guards against a null argument if can be null. - /// - /// The type of the argument. - /// Name of the parameter. - /// The argument. - /// is null. - /// - /// Performs a type check to avoid boxing of value type objects. - /// - [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")] - [DebuggerStepThrough] - public static void AgainstNullArgumentIfNullable(string parameterName, [ValidatedNotNull]TArgument argument) - { - if (typeof(TArgument).IsNullableType() && argument == null) - { - throw new ArgumentNullException(parameterName, string.Format(CultureInfo.InvariantCulture, "{0} is null.", parameterName)); - } - } - - /// - /// Guards against a null argument property value. - /// - /// The type of the property. - /// Name of the parameter. - /// Name of the property. - /// The argument property. - /// is null. - /// is restricted to reference types to avoid boxing of value type objects. - [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")] - [DebuggerStepThrough] - public static void AgainstNullArgumentProperty(string parameterName, string propertyName, [ValidatedNotNull]TProperty argumentProperty) - where TProperty : class - { - if (argumentProperty == null) - { - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "{0}.{1} is null.", parameterName, propertyName), parameterName); - } - } - - /// - /// Guards against a null argument property value if can be null. - /// - /// The type of the property. - /// Name of the parameter. - /// Name of the property. - /// The argument property. - /// is null. - /// - /// Performs a type check to avoid boxing of value type objects. - /// - [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")] - [DebuggerStepThrough] - public static void AgainstNullArgumentPropertyIfNullable( - string parameterName, string propertyName, [ValidatedNotNull]TProperty argumentProperty) - { - if (typeof(TProperty).IsNullableType() && argumentProperty == null) - { - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "{0}.{1} is null.", parameterName, propertyName), parameterName); - } - } - - /// - /// Determines whether the specified type is a nullable type. - /// - /// The type. - /// - /// true if the specified type is a nullable type; otherwise, false. - /// - [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")] - private static bool IsNullableType(this Type type) - { - return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)); - } - - /// - /// When applied to a parameter, this attribute provides an indication to code analysis that the argument has been null checked. - /// - private sealed class ValidatedNotNullAttribute : Attribute - { - } - } -} diff --git a/src/ScriptCs.Core/ILoggerConfigurator.cs b/src/ScriptCs.Core/ILoggerConfigurator.cs deleted file mode 100644 index a10efab0..00000000 --- a/src/ScriptCs.Core/ILoggerConfigurator.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public interface ILoggerConfigurator - { - void Configure(IConsole console); - } -} diff --git a/src/ScriptCs.Core/LoadLineProcessor.cs b/src/ScriptCs.Core/LoadLineProcessor.cs deleted file mode 100644 index 8313b943..00000000 --- a/src/ScriptCs.Core/LoadLineProcessor.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; - -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public interface ILoadLineProcessor : ILineProcessor - { - } - - public class LoadLineProcessor : DirectiveLineProcessor, ILoadLineProcessor - { - private readonly IFileSystem _fileSystem; - - public LoadLineProcessor(IFileSystem fileSystem) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - - _fileSystem = fileSystem; - } - - protected override string DirectiveName => "load"; - - protected override BehaviorAfterCode BehaviorAfterCode => BehaviorAfterCode.Throw; - - protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line) - { - Guard.AgainstNullArgument("parser", parser); - - var argument = GetDirectiveArgument(line); - var filePath = Environment.ExpandEnvironmentVariables(argument); - - var fullPath = _fileSystem.GetFullPath(filePath); - if (!string.IsNullOrWhiteSpace(fullPath)) - { - parser.ParseFile(fullPath, context); - } - - return true; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/NullScriptLibraryComposer.cs b/src/ScriptCs.Core/NullScriptLibraryComposer.cs deleted file mode 100644 index a4b637d2..00000000 --- a/src/ScriptCs.Core/NullScriptLibraryComposer.cs +++ /dev/null @@ -1,14 +0,0 @@ -using ScriptCs.Contracts; -using System.Text; - -namespace ScriptCs -{ - public class NullScriptLibraryComposer : IScriptLibraryComposer - { - public void Compose(string workingDirectory, StringBuilder builder = null) - { - } - - public string ScriptLibrariesFile => string.Empty; - } -} diff --git a/src/ScriptCs.Core/PackageAssemblyResolver.cs b/src/ScriptCs.Core/PackageAssemblyResolver.cs deleted file mode 100644 index 860344ce..00000000 --- a/src/ScriptCs.Core/PackageAssemblyResolver.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class PackageAssemblyResolver : IPackageAssemblyResolver - { - private readonly IFileSystem _fileSystem; - private readonly IPackageContainer _packageContainer; - private readonly ILog _logger; - private readonly IAssemblyUtility _assemblyUtility; - - private List _topLevelPackages; - - public PackageAssemblyResolver( - IFileSystem fileSystem, IPackageContainer packageContainer, ILogProvider logProvider, IAssemblyUtility assemblyUtility) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgumentProperty("fileSystem", "PackagesFolder", fileSystem.PackagesFolder); - Guard.AgainstNullArgumentProperty("fileSystem", "PackagesFile", fileSystem.PackagesFile); - - Guard.AgainstNullArgument("packageContainer", packageContainer); - Guard.AgainstNullArgument("logProvider", logProvider); - Guard.AgainstNullArgument("assemblyUtility", assemblyUtility); - - _fileSystem = fileSystem; - _packageContainer = packageContainer; - _logger = logProvider.ForCurrentType(); - _assemblyUtility = assemblyUtility; - } - - public void SavePackages() - { - var packagesFolder = Path.Combine(_fileSystem.CurrentDirectory, _fileSystem.PackagesFolder); - if (!_fileSystem.DirectoryExists(packagesFolder)) - { - _logger.Warn("Packages directory does not exist!"); - return; - } - - _packageContainer.CreatePackageFile(); - } - - public IEnumerable GetPackages(string workingDirectory) - { - var packagesFile = Path.Combine(workingDirectory, _fileSystem.PackagesFile); - _topLevelPackages = _packageContainer.FindReferences(packagesFile).ToList(); - return _topLevelPackages.ToArray(); - } - - public IEnumerable GetAssemblyNames(string workingDirectory) - { - var packages = GetPackages(workingDirectory).ToList(); - if (!packages.Any()) - { - return Enumerable.Empty(); - } - - var packagesFile = Path.Combine(workingDirectory, _fileSystem.PackagesFile); - var packagesFolder = Path.Combine(workingDirectory, _fileSystem.PackagesFolder); - - var names = new List(); - GetAssemblyNames(packagesFolder, packages, names, _fileSystem.FileExists(packagesFile)); - return names; - } - - private void GetAssemblyNames( - string packageDir, - IEnumerable packageReferences, - ICollection names, - bool strictLoad) - { - foreach (var packageReference in packageReferences) - { - var packageObject = _packageContainer.FindPackage(packageDir, packageReference); - if (packageObject == null) - { - _logger.WarnFormat( - "Cannot find: {0} {1}", - packageReference.PackageId, - packageReference.Version); - - continue; - } - - var compatibleDlls = packageObject.GetCompatibleDlls(packageReference.FrameworkName); - if (compatibleDlls == null) - { - _logger.WarnFormat( - "Cannot find compatible binaries for {0} in: {1} {2}", - packageReference.FrameworkName, - packageReference.PackageId, - packageReference.Version); - - continue; - } - - foreach (var name in compatibleDlls - .Select(packageFile => Path.Combine(packageDir, packageObject.FullName, packageFile)) - .Where(path => _assemblyUtility.IsManagedAssembly(path)) - .Concat(packageObject.FrameworkAssemblies) - .Where(name => !names.Contains(name))) - { - names.Add(name); - _logger.Debug("Found: " + name); - } - - if (packageObject.Dependencies == null || !packageObject.Dependencies.Any() || !strictLoad) - { - continue; - } - - var dependencyReferences = packageObject.Dependencies - .Where(dependency => - _topLevelPackages.All(topLevelPackage => topLevelPackage.PackageId != dependency.Id)) - .Select(dependency => - new PackageReference(dependency.Id, dependency.FrameworkName, dependency.Version)); - - GetAssemblyNames(packageDir, dependencyReferences, names, true); - } - } - } -} diff --git a/src/ScriptCs.Core/PackageReference.cs b/src/ScriptCs.Core/PackageReference.cs deleted file mode 100644 index dbd70ef3..00000000 --- a/src/ScriptCs.Core/PackageReference.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Runtime.Versioning; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class PackageReference : IPackageReference - { - public PackageReference(string packageId, FrameworkName frameworkName, Version version) - : this(packageId, frameworkName, version, null) - { - } - - public PackageReference(string packageId, FrameworkName frameworkName, Version version, string specialVersion) - { - FrameworkName = frameworkName; - PackageId = packageId; - Version = version; - SpecialVersion = specialVersion; - } - - public PackageReference(string packageId, FrameworkName frameworkName, string stringVersion) - { - FrameworkName = frameworkName; - PackageId = packageId; - SetVersionFromString(stringVersion); - } - - public string PackageId { get; private set; } - - public FrameworkName FrameworkName { get; private set; } - - public Version Version { get; set; } - - public string SpecialVersion { get; set; } - - private void SetVersionFromString(string stringVersion) - { - if (string.IsNullOrWhiteSpace(stringVersion)) - { - Version = new Version(); - } - else - { - if (stringVersion.Contains("-")) - { - var splitVersion = stringVersion.Split(new[] { '-' }, 2); - if (splitVersion.Length == 2) - { - Version = new Version(splitVersion[0]); - SpecialVersion = splitVersion[1]; - return; - } - } - - Version = new Version(stringVersion); - } - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/Properties/AssemblyInfo.cs b/src/ScriptCs.Core/Properties/AssemblyInfo.cs deleted file mode 100644 index 8a1cb333..00000000 --- a/src/ScriptCs.Core/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -[assembly: InternalsVisibleTo("ScriptCs.Core.Tests")] -[assembly: InternalsVisibleTo("ScriptCs.Engine.Roslyn.Tests")] \ No newline at end of file diff --git a/src/ScriptCs.Core/Properties/ScriptCs.Core.nuspec b/src/ScriptCs.Core/Properties/ScriptCs.Core.nuspec deleted file mode 100644 index 486e9ead..00000000 --- a/src/ScriptCs.Core/Properties/ScriptCs.Core.nuspec +++ /dev/null @@ -1,20 +0,0 @@ - - - - ScriptCs.Core - $version$ - Glenn Block, Filip Wojcieszyn, Justin Rusbatch, Kristian Hellang, Damian Schenkelman, Adam Ralph - Glenn Block, Justin Rusbatch, Filip Wojcieszyn - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Core is the core framework assembly for scriptcs. - roslyn csx - - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Core/ReferenceLineProcessor.cs b/src/ScriptCs.Core/ReferenceLineProcessor.cs deleted file mode 100644 index 52ccc633..00000000 --- a/src/ScriptCs.Core/ReferenceLineProcessor.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.IO; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public interface IReferenceLineProcessor : ILineProcessor - { - } - - public class ReferenceLineProcessor : DirectiveLineProcessor, IReferenceLineProcessor - { - private readonly IFileSystem _fileSystem; - - public ReferenceLineProcessor(IFileSystem fileSystem) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - - _fileSystem = fileSystem; - } - - protected override string DirectiveName => "r"; - - protected override BehaviorAfterCode BehaviorAfterCode => BehaviorAfterCode.Throw; - - protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line) - { - Guard.AgainstNullArgument("context", context); - - var argument = GetDirectiveArgument(line); - var assemblyPath = Environment.ExpandEnvironmentVariables(argument); - - var referencePath = _fileSystem.GetFullPath(assemblyPath); - var referencePathOrName = _fileSystem.FileExists(referencePath) ? referencePath : argument; - - if (!string.IsNullOrWhiteSpace(referencePathOrName) && !context.References.Contains(referencePathOrName)) - { - context.References.Add(referencePathOrName); - } - - return true; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/Repl.cs b/src/ScriptCs.Core/Repl.cs deleted file mode 100644 index 9b9ddb35..00000000 --- a/src/ScriptCs.Core/Repl.cs +++ /dev/null @@ -1,213 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class Repl : ScriptExecutor, IRepl - { - private readonly string[] _scriptArgs; - - private readonly IObjectSerializer _serializer; - private readonly Printers _printers; - private readonly ILog _log; - - public Repl( - string[] scriptArgs, - IFileSystem fileSystem, - IScriptEngine scriptEngine, - IObjectSerializer serializer, - ILogProvider logProvider, - IScriptLibraryComposer composer, - IConsole console, - IFilePreProcessor filePreProcessor, - IEnumerable replCommands, - Printers printers, - IScriptInfo scriptInfo) - : base(fileSystem, filePreProcessor, scriptEngine, logProvider, composer, scriptInfo) - { - Guard.AgainstNullArgument("serializer", serializer); - Guard.AgainstNullArgument("logProvider", logProvider); - Guard.AgainstNullArgument("console", console); - - _scriptArgs = scriptArgs; - _serializer = serializer; - _printers = printers; - _log = logProvider.ForCurrentType(); - Console = console; - Commands = replCommands != null ? replCommands.Where(x => x.CommandName != null).ToDictionary(x => x.CommandName, x => x) : new Dictionary(); - } - - public string Buffer { get; set; } - - public IConsole Console { get; private set; } - - public Dictionary Commands { get; private set; } - - public override void Terminate() - { - base.Terminate(); - _log.Debug("Exiting console"); - Console.Exit(); - } - - public override ScriptResult Execute(string script, params string[] scriptArgs) - { - Guard.AgainstNullArgument("script", script); - try - { - if (script.StartsWith(":")) - { - var tokens = script.Split(' '); - if (tokens[0].Length > 1) - { - var command = Commands.FirstOrDefault(x => x.Key == tokens[0].Substring(1)); - - if (command.Value != null) - { - var argsToPass = new List(); - foreach (var argument in tokens.Skip(1)) - { - var argumentResult = ScriptEngine.Execute( - argument, _scriptArgs, References, Namespaces, ScriptPackSession); - - if (argumentResult.CompileExceptionInfo != null) - { - throw new Exception( - GetInvalidCommandArgumentMessage(argument), - argumentResult.CompileExceptionInfo.SourceException); - } - - if (argumentResult.ExecuteExceptionInfo != null) - { - throw new Exception( - GetInvalidCommandArgumentMessage(argument), - argumentResult.ExecuteExceptionInfo.SourceException); - } - - if (!argumentResult.IsCompleteSubmission) - { - throw new Exception(GetInvalidCommandArgumentMessage(argument)); - } - - argsToPass.Add(argumentResult.ReturnValue); - } - - var commandResult = command.Value.Execute(this, argsToPass.ToArray()); - return ProcessCommandResult(commandResult); - } - } - } - - var preProcessResult = FilePreProcessor.ProcessScript(script); - - ImportNamespaces(preProcessResult.Namespaces.ToArray()); - - foreach (var reference in preProcessResult.References) - { - var referencePath = FileSystem.GetFullPath(Path.Combine(FileSystem.BinFolder, reference)); - AddReferences(FileSystem.FileExists(referencePath) ? referencePath : reference); - } - - Console.ForegroundColor = ConsoleColor.Cyan; - - InjectScriptLibraries(FileSystem.CurrentDirectory, preProcessResult, ScriptPackSession.State); - - Buffer = (Buffer == null) - ? preProcessResult.Code - : Buffer + Environment.NewLine + preProcessResult.Code; - - var namespaces = Namespaces.Union(preProcessResult.Namespaces); - var references = References.Union(preProcessResult.References); - - var result = ScriptEngine.Execute(Buffer, _scriptArgs, references, namespaces, ScriptPackSession); - if (result == null) return ScriptResult.Empty; - - if (result.CompileExceptionInfo != null) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(result.CompileExceptionInfo.SourceException.Message); - } - - if (result.ExecuteExceptionInfo != null) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(result.ExecuteExceptionInfo.SourceException.Message); - } - - if (result.InvalidNamespaces.Any()) - { - RemoveNamespaces(result.InvalidNamespaces.ToArray()); - } - - if (!result.IsCompleteSubmission) - { - return result; - } - - if (result.ReturnValue != null) - { - Console.ForegroundColor = ConsoleColor.Yellow; - - Console.WriteLine(_printers.GetStringFor(result.ReturnValue)); - } - - Buffer = null; - return result; - } - catch (FileNotFoundException fileEx) - { - RemoveReferences(fileEx.FileName); - - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Environment.NewLine + fileEx + Environment.NewLine); - - return new ScriptResult(compilationException: fileEx); - } - catch (Exception ex) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Environment.NewLine + ex + Environment.NewLine); - - return new ScriptResult(executionException: ex); - } - finally - { - Console.ResetColor(); - } - } - - private static string GetInvalidCommandArgumentMessage(string argument) - { - return string.Format(CultureInfo.InvariantCulture, "Argument is not a valid expression: {0}", argument); - } - - private ScriptResult ProcessCommandResult(object commandResult) - { - Buffer = null; - - if (commandResult != null) - { - if (commandResult is ScriptResult) - { - var scriptCommandResult = commandResult as ScriptResult; - if (scriptCommandResult.ReturnValue != null) - { - Console.WriteLine(_serializer.Serialize(scriptCommandResult.ReturnValue)); - } - return scriptCommandResult; - } - - //if command has a result, print it - Console.WriteLine(_serializer.Serialize(commandResult)); - - return new ScriptResult(returnValue: commandResult); - } - - return ScriptResult.Empty; - } - } -} diff --git a/src/ScriptCs.Core/ReplCommands/AliasCommand.cs b/src/ScriptCs.Core/ReplCommands/AliasCommand.cs deleted file mode 100644 index fd46b923..00000000 --- a/src/ScriptCs.Core/ReplCommands/AliasCommand.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - using System; - using System.Globalization; - - public class AliasCommand : IReplCommand - { - private readonly IConsole _console; - - public AliasCommand(IConsole console) - { - Guard.AgainstNullArgument("console", console); - - _console = console; - } - - public string Description => "Allows you to alias a command with a custom name"; - - public string CommandName => "alias"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - if (args == null || args.Length != 2) - { - _console.WriteLine("You must specifiy the command name and alias, e.g. :alias \"clear\" \"cls\""); - return null; - } - - var commandName = args[0].ToString(); - var alias = args[1].ToString(); - - if (repl.Commands.Any(x => string.Equals(x.Key, alias, StringComparison.InvariantCultureIgnoreCase))) - { - var message = string.Format( - CultureInfo.InvariantCulture, - "\"{0}\" cannot be used as an alias since it is the name of an existing command.", - alias); - - _console.WriteLine(message); - return null; - } - - - if (!repl.Commands.TryGetValue(commandName, out var command)) - { - var message = string.Format( - CultureInfo.InvariantCulture, "There is no command named or aliased \"{0}\".", alias); - - _console.WriteLine(message); - return null; - } - - repl.Commands[alias] = command; - _console.WriteLine(string.Format("Aliased \"{0}\" as \"{1}\".", commandName, alias)); - - return null; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/CdCommand.cs b/src/ScriptCs.Core/ReplCommands/CdCommand.cs deleted file mode 100644 index 86010326..00000000 --- a/src/ScriptCs.Core/ReplCommands/CdCommand.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.IO; -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class CdCommand : IReplCommand - { - public string Description => "Changes the working directory to the path provided."; - - public string CommandName => "cd"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - if (args == null || args.Length == 0) - { - return null; - } - - var path = args[0].ToString(); - - repl.FileSystem.CurrentDirectory = Path.GetFullPath(Path.Combine(repl.FileSystem.CurrentDirectory, path)); - - return null; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/ClearCommand.cs b/src/ScriptCs.Core/ReplCommands/ClearCommand.cs deleted file mode 100644 index a7ede226..00000000 --- a/src/ScriptCs.Core/ReplCommands/ClearCommand.cs +++ /dev/null @@ -1,26 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class ClearCommand : IReplCommand - { - private readonly IConsole _console; - - public string Description => "Clears the console window."; - - public ClearCommand(IConsole console) - { - Guard.AgainstNullArgument("console", console); - - _console = console; - } - - public string CommandName => "clear"; - - public object Execute(IRepl repl, object[] args) - { - _console.Clear(); - return null; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/CwdCommand.cs b/src/ScriptCs.Core/ReplCommands/CwdCommand.cs deleted file mode 100644 index e193347d..00000000 --- a/src/ScriptCs.Core/ReplCommands/CwdCommand.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class CwdCommand : IReplCommand - { - private readonly IConsole _console; - - public CwdCommand(IConsole console) - { - Guard.AgainstNullArgument("console", console); - - _console = console; - } - - public string Description => "Displays the current working directory."; - - public string CommandName => "cwd"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - var dir = repl.FileSystem.CurrentDirectory; - - var originalColor = _console.ForegroundColor; - _console.ForegroundColor = ConsoleColor.Yellow; - try - { - _console.WriteLine(dir); - } - finally - { - _console.ForegroundColor = originalColor; - } - - return null; - } - } -} diff --git a/src/ScriptCs.Core/ReplCommands/ExitCommand.cs b/src/ScriptCs.Core/ReplCommands/ExitCommand.cs deleted file mode 100644 index 81c3e6e8..00000000 --- a/src/ScriptCs.Core/ReplCommands/ExitCommand.cs +++ /dev/null @@ -1,41 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class ExitCommand : IReplCommand - { - private readonly IConsole _console; - - public string Description => "Exits the REPL"; - - public string CommandName => "exit"; - - public ExitCommand(IConsole console) - { - Guard.AgainstNullArgument("console", console); - - _console = console; - } - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - var response = string.Empty; - var responseIsValid = false; - - while (!responseIsValid) - { - response = (_console.ReadLine("Are you sure you wish to exit? (y/n):") ?? string.Empty).ToLowerInvariant(); - responseIsValid = response == "y" || response == "n"; - } - - if (response == "y") - { - repl.Terminate(); - } - - return null; - } - } -} diff --git a/src/ScriptCs.Core/ReplCommands/HelpCommand.cs b/src/ScriptCs.Core/ReplCommands/HelpCommand.cs deleted file mode 100644 index 508464f8..00000000 --- a/src/ScriptCs.Core/ReplCommands/HelpCommand.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System.Linq; -using System.Text; -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class HelpCommand : IReplCommand - { - private readonly IConsole _console; - - public HelpCommand(IConsole console) - { - Guard.AgainstNullArgument("console", console); - - _console = console; - } - - public string Description => "Shows this help."; - - public string CommandName => "help"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - _console.WriteLine("\nThe following commands are available in the REPL:"); - foreach (var command in repl.Commands.OrderBy(x => x.Key)) - { - string key = string.Format(" :{0,-15} - ", command.Key); - - // make sure we have a good width for formatting purposes - int descWidth = _console.Width - key.Length - 1; - if (descWidth > 25) - { - _console.WriteLine(string.Format("{0}{1,10}", key, WrapTextToColumn(command.Value.Description, descWidth, indentWidth: key.Length))); - } - else - { - // safe-guard: just in the case we have a really long Repl Command "key" - // and a really narrow console width don't wrap the description - // note: the extra newline if to at least make somewhat readable - _console.WriteLine(string.Format("{0}{1,10}\n", key, command.Value.Description)); - } - } - _console.WriteLine(string.Empty); - - return null; - } - - /// - /// Word wrap text to specified column width. - /// - /// Unformatted text. - /// Size of the column width. - /// Indentation width when the text is wrap. The first line is not indented. - /// First line indent width. - /// Formatted text. - /// In the future, I believe this method will be moved into some sort of formatting helper class. - private string WrapTextToColumn(string text, int columnWidth, int indentWidth = 0, int initialWidth = 0) - { - // check the initial width - if ((initialWidth < 0) || (initialWidth > (indentWidth + columnWidth))) - { - throw new System.ArgumentOutOfRangeException("initialWidth"); - } - - // TODO: Add additional parameter error checking - - StringBuilder paragraph = new StringBuilder(text.Trim()); - - // add the initial space to text - paragraph.Insert(0, " ", initialWidth); - - if (paragraph.Length > (columnWidth)) - { - int pos = columnWidth; - int backSearchLimit = initialWidth; - do - { - // find a whitespace we can wrap the description line - int savedPos = pos; - while (!char.IsWhiteSpace(paragraph[pos])) - { - pos--; - - // guard against not finding a natural whitespace - // don't go below the spaces we create (indent) - if (pos < backSearchLimit) - { - pos = savedPos; - break; - } - } - - if (char.IsWhiteSpace(paragraph[pos])) - { - paragraph.Remove(pos, 1); // remove the whitespace we found - } - // inject a newline - paragraph.Insert(pos, System.Environment.NewLine); - pos += System.Environment.NewLine.Length; - paragraph.Insert(pos, " ", indentWidth); - pos += indentWidth; - - // prevent searching for whitespace to go below the spaces we put in - backSearchLimit = pos; - - pos += columnWidth; - } while (pos < paragraph.Length); - - } - - return paragraph.ToString(); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/InstallCommand.cs b/src/ScriptCs.Core/ReplCommands/InstallCommand.cs deleted file mode 100644 index e668c82c..00000000 --- a/src/ScriptCs.Core/ReplCommands/InstallCommand.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using System.Linq; -using System.Runtime.Versioning; -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class InstallCommand : IReplCommand - { - private readonly IPackageInstaller _packageInstaller; - private readonly IPackageAssemblyResolver _packageAssemblyResolver; - private readonly ILog _logger; - private readonly IInstallationProvider _installationProvider; - - public InstallCommand( - IPackageInstaller packageInstaller, - IPackageAssemblyResolver packageAssemblyResolver, - ILogProvider logProvider, - IInstallationProvider installationProvider) - { - Guard.AgainstNullArgument("packageInstaller", packageInstaller); - Guard.AgainstNullArgument("packageAssemblyResolver", packageAssemblyResolver); - Guard.AgainstNullArgument("logProvider", logProvider); - Guard.AgainstNullArgument("installationProvider", installationProvider); - - _packageInstaller = packageInstaller; - _packageAssemblyResolver = packageAssemblyResolver; - _logger = logProvider.ForCurrentType(); - _installationProvider = installationProvider; - } - - public string Description => "Installs a Nuget package. I.e. :install "; - - public string CommandName => "install"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - if (args == null || args.Length == 0) - { - return null; - } - - string version = null; - if (args.Length >= 2) - { - version = args[1].ToString(); - } - - var allowPre = args.Length >= 3 && args[2].ToString().ToUpperInvariant() == "PRE"; - - _logger.InfoFormat("Installing {0}", args[0]); - - _installationProvider.Initialize(); - - var packageRef = new PackageReference( - args[0].ToString(), new FrameworkName(".NETFramework,Version=v4.6.1"), version); - - _packageInstaller.InstallPackages(new[] { packageRef }, allowPre); - _packageAssemblyResolver.SavePackages(); - - var dlls = _packageAssemblyResolver.GetAssemblyNames(repl.FileSystem.CurrentDirectory) - .Except(repl.References.Paths).ToArray(); - - repl.AddReferences(dlls); - - foreach (var dll in dlls) - { - _logger.InfoFormat("Added reference to {0}", dll); - } - - return null; - } - } -} diff --git a/src/ScriptCs.Core/ReplCommands/ReferencesCommand.cs b/src/ScriptCs.Core/ReplCommands/ReferencesCommand.cs deleted file mode 100644 index 668b40d7..00000000 --- a/src/ScriptCs.Core/ReplCommands/ReferencesCommand.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class ReferencesCommand : IReplCommand - { - public string CommandName => "references"; - - public string Description => "Displays a list of assemblies referenced from the REPL context."; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - return repl.References != null - ? repl.References.Assemblies.Select(x => x.FullName).Union(repl.References.Paths) - : null; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/ResetCommand.cs b/src/ScriptCs.Core/ReplCommands/ResetCommand.cs deleted file mode 100644 index 8e68ec56..00000000 --- a/src/ScriptCs.Core/ReplCommands/ResetCommand.cs +++ /dev/null @@ -1,19 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class ResetCommand : IReplCommand - { - public string Description => "Resets the REPL state. All local variables and member definitions are cleared."; - - public string CommandName => "reset"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - repl.Reset(); - return null; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/ScriptPacksCommand.cs b/src/ScriptCs.Core/ReplCommands/ScriptPacksCommand.cs deleted file mode 100644 index b5b9a08d..00000000 --- a/src/ScriptCs.Core/ReplCommands/ScriptPacksCommand.cs +++ /dev/null @@ -1,183 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using ScriptCs.Contracts; -using ScriptCs.Extensions; - -namespace ScriptCs.ReplCommands -{ - public class ScriptPacksCommand : IReplCommand - { - private readonly IConsole _console; - - public ScriptPacksCommand(IConsole console) - { - _console = console; - } - - public string Description => "Displays information about script packs available in the REPL session"; - - public string CommandName => "scriptpacks"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - var packContexts = repl.ScriptPackSession.Contexts; - - if (packContexts.IsNullOrEmpty()) - { - PrintInColor("There are no script packs available in this REPL session"); - return null; - } - - var importedNamespaces = repl.ScriptPackSession.Namespaces.IsNullOrEmpty() ? repl.Namespaces.ToArray() : repl.Namespaces.Union(repl.ScriptPackSession.Namespaces).ToArray(); - - foreach (var packContext in packContexts) - { - var contextType = packContext.GetType(); - PrintInColor(contextType.ToString()); - - var methods = contextType.GetAllMethods(); - var properties = contextType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); - - PrintMethods(methods, importedNamespaces); - PrintProperties(properties, importedNamespaces); - - _console.WriteLine(); - } - - return null; - } - - private void PrintInColor(string text) - { - var originalColor = _console.ForegroundColor; - _console.ForegroundColor = ConsoleColor.Yellow; - _console.WriteLine(text); - _console.ForegroundColor = originalColor; - } - - private void PrintMethods(IEnumerable methods, string[] importedNamespaces) - { - if (!methods.IsNullOrEmpty()) - { - _console.WriteLine("** Methods **"); - foreach (var method in methods) - { - var methodParams = method.GetParametersWithoutExtensions() - .Select(p => string.Format("{0} {1}", GetPrintableType(p.ParameterType, importedNamespaces), p.Name)); - var methodSignature = string.Format(" - {0} {1}({2})", GetPrintableType(method.ReturnType, importedNamespaces), method.Name, - string.Join(", ", methodParams)); - - _console.WriteLine(methodSignature); - } - _console.WriteLine(); - } - } - - private void PrintProperties(IEnumerable properties, string[] importedNamespaces) - { - if (!properties.IsNullOrEmpty()) - { - _console.WriteLine("** Properties **"); - foreach (var property in properties) - { - var propertyBuilder = new StringBuilder(string.Format(" - {0} {1}", GetPrintableType(property.PropertyType, importedNamespaces), property.Name)); - propertyBuilder.Append(" {"); - - if (property.GetGetMethod() != null) - { - propertyBuilder.Append(" get;"); - } - - if (property.GetSetMethod() != null) - { - propertyBuilder.Append(" set;"); - } - - propertyBuilder.Append(" }"); - - _console.WriteLine(propertyBuilder.ToString()); - } - } - } - - private string GetPrintableType(Type type, string[] importedNamespaces) - { - if (type.Name == "Void") - { - return "void"; - } - - if (type.Name == "Object") - { - return "object"; - } - - if (type.IsGenericType) - { - return BuildGeneric(type, importedNamespaces); - } - - var nullableType = Nullable.GetUnderlyingType(type); - if (nullableType != null) - { - return string.Format("{0}?", GetPrintableType(nullableType, importedNamespaces)); - } - - switch (Type.GetTypeCode(type)) - { - case TypeCode.Boolean: - return "bool"; - case TypeCode.Byte: - return "byte"; - case TypeCode.Char: - return "char"; - case TypeCode.Decimal: - return "decimal"; - case TypeCode.Double: - return "double"; - case TypeCode.Int16: - return "short"; - case TypeCode.Int32: - return "int"; - case TypeCode.Int64: - return "long"; - case TypeCode.SByte: - return "sbyte"; - case TypeCode.Single: - return "Single"; - case TypeCode.String: - return "string"; - case TypeCode.UInt16: - return "UInt16"; - case TypeCode.UInt32: - return "UInt32"; - case TypeCode.UInt64: - return "UInt64"; - default: - return string.IsNullOrEmpty(type.FullName) || importedNamespaces.Contains(type.Namespace) ? type.Name : type.FullName; - } - } - - private string BuildGeneric(Type type, string[] importedNamespaces) - { - var baseName = type.Name.Substring(0, type.Name.IndexOf("`", StringComparison.Ordinal)); - var genericDefinition = new StringBuilder(string.Format("{0}<", baseName)); - var firstArgument = true; - foreach (var t in type.GetGenericArguments()) - { - if (!firstArgument) - { - genericDefinition.Append(", "); - } - genericDefinition.Append(GetPrintableType(t, importedNamespaces)); - firstArgument = false; - } - genericDefinition.Append(">"); - return genericDefinition.ToString(); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/UsingsCommand.cs b/src/ScriptCs.Core/ReplCommands/UsingsCommand.cs deleted file mode 100644 index 1548b551..00000000 --- a/src/ScriptCs.Core/ReplCommands/UsingsCommand.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class UsingsCommand : IReplCommand - { - public string Description => "Displays a list of namespaces imported into REPL context."; - - public string CommandName => "usings"; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - var namespaces = repl.Namespaces; - - if (repl.ScriptPackSession == null || repl.ScriptPackSession.Namespaces == null || !repl.ScriptPackSession.Namespaces.Any()) - return namespaces; - - return namespaces.Union(repl.ScriptPackSession.Namespaces).OrderBy(x => x); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ReplCommands/VarsCommand.cs b/src/ScriptCs.Core/ReplCommands/VarsCommand.cs deleted file mode 100644 index 87594aab..00000000 --- a/src/ScriptCs.Core/ReplCommands/VarsCommand.cs +++ /dev/null @@ -1,19 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.ReplCommands -{ - public class VarsCommand : IReplCommand - { - public string CommandName => "vars"; - - public string Description => "Displays a list of variables defined within the REPL, along with their types and values."; - - public object Execute(IRepl repl, object[] args) - { - Guard.AgainstNullArgument("repl", repl); - - var replEngine = repl.ScriptEngine as IReplEngine; - return replEngine != null ? replEngine.GetLocalVariables(repl.ScriptPackSession) : null; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ScriptCs.Core.csproj b/src/ScriptCs.Core/ScriptCs.Core.csproj deleted file mode 100644 index a83e1dc5..00000000 --- a/src/ScriptCs.Core/ScriptCs.Core.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - 1.0.0 - netstandard2.0 - ScriptCs.Core - Glenn Block, Filip Wojcieszyn, Justin Rusbatch - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Core - ScriptCs.Core is the core framework assembly for scriptcs. - roslyn csx scriptcs - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Core/ScriptEnvironment.cs b/src/ScriptCs.Core/ScriptEnvironment.cs deleted file mode 100644 index 2e307fc2..00000000 --- a/src/ScriptCs.Core/ScriptEnvironment.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptEnvironment : IScriptEnvironment - { - private readonly IConsole _console; - private readonly Printers _printers; - private readonly IScriptInfo _scriptInfo; - - public ScriptEnvironment(string[] scriptArgs, IConsole console, Printers printers, IScriptInfo scriptInfo = null ) - { - _console = console; - _printers = printers; - _scriptInfo = scriptInfo; - ScriptArgs = scriptArgs; - } - - public IReadOnlyList ScriptArgs { get; private set; } - - public void AddCustomPrinter(Func printer) - { - _console.WriteLine("Adding custom printer for " + typeof(T).Name); - _printers.AddCustomPrinter(printer); - } - - public void Print(object o) - { - _console.WriteLine(_printers.GetStringFor(o)); - } - - public void Print(T o) - { - _console.WriteLine(_printers.GetStringFor(o)); - } - - public string ScriptPath => _scriptInfo.ScriptPath; - - public string[] LoadedScripts => _scriptInfo.LoadedScripts.ToArray(); - - public Assembly ScriptAssembly { get; private set; } - - private bool _initialized; - public void Initialize() - { - if (!_initialized) - { - ScriptAssembly = Assembly.GetCallingAssembly(); - _initialized = true; - } - } - } -} diff --git a/src/ScriptCs.Core/ScriptExecutor.cs b/src/ScriptCs.Core/ScriptExecutor.cs deleted file mode 100644 index 499f0e8b..00000000 --- a/src/ScriptCs.Core/ScriptExecutor.cs +++ /dev/null @@ -1,264 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.IO; -using System.Linq; -using System.Reflection; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptExecutor : IScriptExecutor - { - private readonly ILog _log; - - public static readonly string[] DefaultReferences = - { - "System", - "System.Core", - "System.Data", - "System.Data.DataSetExtensions", - "System.Xml", - "System.Xml.Linq", - "System.Net.Http", - "Microsoft.CSharp", - typeof(ScriptExecutor).Assembly.Location, - typeof(IScriptEnvironment).Assembly.Location - }; - - public static readonly string[] DefaultNamespaces = - { - "System", - "System.Collections.Generic", - "System.Linq", - "System.Text", - "System.Threading.Tasks", - "System.IO", - "System.Net.Http", - "System.Dynamic" - }; - - private const string ScriptLibrariesInjected = "ScriptLibrariesInjected"; - - public IFileSystem FileSystem { get; private set; } - - public IFilePreProcessor FilePreProcessor { get; private set; } - - public IScriptEngine ScriptEngine { get; private set; } - - public AssemblyReferences References { get; private set; } - - public IReadOnlyCollection Namespaces { get; private set; } - - public ScriptPackSession ScriptPackSession { get; protected set; } - - public IScriptLibraryComposer ScriptLibraryComposer { get; protected set; } - - public IScriptInfo ScriptInfo { get; protected set; } - - public ScriptExecutor( - IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, ILogProvider logProvider, IScriptInfo scriptInfo) - : this(fileSystem, filePreProcessor, scriptEngine, logProvider, new NullScriptLibraryComposer(), scriptInfo) - { - } - - public ScriptExecutor( - IFileSystem fileSystem, - IFilePreProcessor filePreProcessor, - IScriptEngine scriptEngine, - ILogProvider logProvider, - IScriptLibraryComposer composer, - IScriptInfo scriptInfo) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgumentProperty("fileSystem", "BinFolder", fileSystem.BinFolder); - Guard.AgainstNullArgumentProperty("fileSystem", "DllCacheFolder", fileSystem.DllCacheFolder); - Guard.AgainstNullArgument("filePreProcessor", filePreProcessor); - Guard.AgainstNullArgument("scriptEngine", scriptEngine); - Guard.AgainstNullArgument("logProvider", logProvider); - Guard.AgainstNullArgument("composer", composer); - Guard.AgainstNullArgument("scriptInfo", scriptInfo); - - References = new AssemblyReferences(new[] { GetAssemblyFromName("System.Runtime") }, DefaultReferences); - Namespaces = new ReadOnlyCollection(DefaultNamespaces); - FileSystem = fileSystem; - FilePreProcessor = filePreProcessor; - ScriptEngine = scriptEngine; - _log = logProvider.ForCurrentType(); - ScriptLibraryComposer = composer; - ScriptInfo = scriptInfo; - } - - public virtual void ImportNamespaces(params string[] namespaces) - { - Guard.AgainstNullArgument("namespaces", namespaces); - Namespaces = new ReadOnlyCollection(Namespaces.Union(namespaces).ToArray()); - } - - public virtual void RemoveNamespaces(params string[] namespaces) - { - Guard.AgainstNullArgument("namespaces", namespaces); - Namespaces = new ReadOnlyCollection(Namespaces.Except(namespaces).ToArray()); - } - - public virtual void AddReferences(params Assembly[] assemblies) - { - Guard.AgainstNullArgument("assemblies", assemblies); - - References = References.Union(assemblies); - } - - public virtual void RemoveReferences(params Assembly[] assemblies) - { - Guard.AgainstNullArgument("assemblies", assemblies); - - References = References.Except(assemblies); - } - - public virtual void AddReferences(params string[] paths) - { - Guard.AgainstNullArgument("paths", paths); - - References = References.Union(paths); - } - - public virtual void RemoveReferences(params string[] paths) - { - Guard.AgainstNullArgument("paths", paths); - - References = References.Except(paths); - } - - public virtual void Initialize( - IEnumerable paths, IEnumerable scriptPacks, params string[] scriptArgs) - { - AddReferences(paths.ToArray()); - var bin = Path.Combine(FileSystem.CurrentDirectory, FileSystem.BinFolder); - var cache = Path.Combine(FileSystem.CurrentDirectory, FileSystem.DllCacheFolder); - - ScriptEngine.BaseDirectory = bin; - ScriptEngine.CacheDirectory = cache; - - _log.Debug("Initializing script packs"); - var scriptPackSession = new ScriptPackSession(scriptPacks, scriptArgs); - ScriptPackSession = scriptPackSession; - scriptPackSession.InitializePacks(); - } - - public virtual void Reset() - { - References = new AssemblyReferences(DefaultReferences); - Namespaces = new ReadOnlyCollection(DefaultNamespaces); - ScriptPackSession.State.Clear(); - } - - public virtual void Terminate() - { - _log.Debug("Terminating packs"); - ScriptPackSession.TerminatePacks(); - } - - public virtual ScriptResult Execute(string script, params string[] scriptArgs) - { - var path = Path.IsPathRooted(script) ? script : Path.Combine(FileSystem.CurrentDirectory, script); - var result = FilePreProcessor.ProcessFile(path); - - ScriptEngine.FileName = Path.GetFileName(path); - ScriptInfo.ScriptPath = Path.GetFullPath(path); - - return EngineExecute(Path.GetDirectoryName(path), scriptArgs, result); - } - - public virtual ScriptResult ExecuteScript(string script, params string[] scriptArgs) - { - var result = FilePreProcessor.ProcessScript(script); - return EngineExecute(FileSystem.CurrentDirectory, scriptArgs, result); - } - - protected internal virtual ScriptResult EngineExecute( - string workingDirectory, - string[] scriptArgs, - FilePreProcessorResult result - ) - { - InjectScriptLibraries(workingDirectory, result, ScriptPackSession.State); - var namespaces = Namespaces.Union(result.Namespaces); - var references = References.Union(result.References); - ScriptInfo.ScriptPath = result.ScriptPath; - foreach (var loadedScript in result.LoadedScripts) - { - ScriptInfo.LoadedScripts.Add(loadedScript); - } - _log.Debug("Starting execution in engine"); - return ScriptEngine.Execute(result.Code, scriptArgs, references, namespaces, ScriptPackSession); - } - - protected internal virtual void InjectScriptLibraries( - string workingDirectory, - FilePreProcessorResult result, - IDictionary state - ) - { - Guard.AgainstNullArgument("result", result); - Guard.AgainstNullArgument("state", state); - - if (state.ContainsKey(ScriptLibrariesInjected)) - { - return; - } - - var scriptLibrariesPreProcessorResult = LoadScriptLibraries(workingDirectory); - - // script libraries should be injected just before the #line directive - // if there is no #line directive, they can be injected at the beginning of code - if (result.Code == null) result.Code = string.Empty; - var safeInsertIndex = result.Code.IndexOf("#line"); - if (safeInsertIndex < 0) safeInsertIndex = 0; - - if (scriptLibrariesPreProcessorResult != null) - { - result.Code = result.Code.Insert(safeInsertIndex, scriptLibrariesPreProcessorResult.Code + Environment.NewLine - + "Env.Initialize();" + Environment.NewLine); - result.References.AddRange(scriptLibrariesPreProcessorResult.References); - result.Namespaces.AddRange(scriptLibrariesPreProcessorResult.Namespaces); - } - else - { - result.Code = result.Code.Insert(safeInsertIndex, "Env.Initialize();" + Environment.NewLine); - } - - state.Add(ScriptLibrariesInjected, null); - } - - protected internal virtual FilePreProcessorResult LoadScriptLibraries(string workingDirectory) - { - if (string.IsNullOrWhiteSpace(ScriptLibraryComposer.ScriptLibrariesFile)) - { - return null; - } - - var scriptLibrariesPath = Path.Combine(workingDirectory, FileSystem.PackagesFolder, - ScriptLibraryComposer.ScriptLibrariesFile); - - if (FileSystem.FileExists(scriptLibrariesPath)) - { - _log.DebugFormat("Found Script Library at {0}", scriptLibrariesPath); - return FilePreProcessor.ProcessFile(scriptLibrariesPath); - } - - return null; - } - - private static Assembly GetAssemblyFromName(string assemblyName) - { - try - { - return Assembly.Load(new AssemblyName(assemblyName)); - } - catch - { - return null; - } - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ScriptExecutorExtensions.cs b/src/ScriptCs.Core/ScriptExecutorExtensions.cs deleted file mode 100644 index 5b22d80b..00000000 --- a/src/ScriptCs.Core/ScriptExecutorExtensions.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public static class ScriptExecutorExtensions - { - public static void ImportNamespaces(this IScriptExecutor executor, params Type[] typesFromReferencedAssembly) - { - Guard.AgainstNullArgument("executor", executor); - Guard.AgainstNullArgument("typesFromReferencedAssembly", typesFromReferencedAssembly); - - var namespaces = typesFromReferencedAssembly.Select(t => t.Namespace); - executor.ImportNamespaces(namespaces.ToArray()); - } - - public static void ImportNamespace(this IScriptExecutor executor) - { - Guard.AgainstNullArgument("executor", executor); - - executor.ImportNamespaces(typeof(T)); - } - - public static void AddReferences(this IScriptExecutor executor, params Type[] typesFromReferencedAssembly) - { - Guard.AgainstNullArgument("executor", executor); - Guard.AgainstNullArgument("typeFromReferencedAssembly", typesFromReferencedAssembly); - - var paths = typesFromReferencedAssembly.Select(t => t.Assembly.Location); - - executor.AddReferences(paths.ToArray()); - } - - public static void AddReference(this IScriptExecutor executor) - { - Guard.AgainstNullArgument("executor", executor); - - executor.AddReferences(typeof(T)); - } - - public static void AddReferenceAndImportNamespaces(this IScriptExecutor executor, Type[] types) - { - Guard.AgainstNullArgument("executor", executor); - - executor.AddReferences(types); - executor.ImportNamespaces(types); - } - } -} diff --git a/src/ScriptCs.Core/ScriptHost.cs b/src/ScriptCs.Core/ScriptHost.cs deleted file mode 100644 index 12021bde..00000000 --- a/src/ScriptCs.Core/ScriptHost.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptHost : IScriptHost - { - private readonly IScriptPackManager _scriptPackManager; - - public ScriptHost(IScriptPackManager scriptPackManager, ScriptEnvironment environment) - { - Guard.AgainstNullArgument("scriptPackManager", scriptPackManager); - - _scriptPackManager = scriptPackManager; - Env = environment; - } - - public IScriptEnvironment Env { get; private set; } - - public T Require() where T : IScriptPackContext - { - return _scriptPackManager.Get(); - } - } -} diff --git a/src/ScriptCs.Core/ScriptHostFactory.cs b/src/ScriptCs.Core/ScriptHostFactory.cs deleted file mode 100644 index 3fe65ba2..00000000 --- a/src/ScriptCs.Core/ScriptHostFactory.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptHostFactory : IScriptHostFactory - { - private readonly IConsole _console; - private readonly Printers _printers; - private IScriptInfo _scriptInfo; - - public ScriptHostFactory(IConsole console, Printers printers, IScriptInfo scriptInfo) - { - _console = console; - _printers = printers; - _scriptInfo = scriptInfo; - } - - public IScriptHost CreateScriptHost(IScriptPackManager scriptPackManager, string[] scriptArgs) - { - return new ScriptHost(scriptPackManager, new ScriptEnvironment(scriptArgs, _console, _printers, _scriptInfo)); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ScriptInfo.cs b/src/ScriptCs.Core/ScriptInfo.cs deleted file mode 100644 index 0aaa7ef0..00000000 --- a/src/ScriptCs.Core/ScriptInfo.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptInfo : IScriptInfo - { - public ScriptInfo() - { - LoadedScripts = new List(); - } - - public string ScriptPath { get; set; } - public IList LoadedScripts { get; private set; } - } -} diff --git a/src/ScriptCs.Core/ScriptLibraryComposer.cs b/src/ScriptCs.Core/ScriptLibraryComposer.cs deleted file mode 100644 index c04d0c6b..00000000 --- a/src/ScriptCs.Core/ScriptLibraryComposer.cs +++ /dev/null @@ -1,144 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptLibraryComposer : IScriptLibraryComposer - { - private readonly IFileSystem _fileSystem; - private readonly IFilePreProcessor _preProcessor; - private readonly IPackageContainer _packageContainer; - private readonly IPackageAssemblyResolver _packageAssemblyResolver; - private readonly ILog _logger; - - public ScriptLibraryComposer(IFileSystem fileSystem, IFilePreProcessor preProcessor, IPackageContainer packageContainer, IPackageAssemblyResolver packageAssemblyResolver, ILogProvider logProvider) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("preProcessor", preProcessor); - Guard.AgainstNullArgument("packageContainer", packageContainer); - Guard.AgainstNullArgument("packageAssemblyResolver", packageAssemblyResolver); - Guard.AgainstNullArgument("logProvider", logProvider); - - _fileSystem = fileSystem; - _preProcessor = preProcessor; - _packageContainer = packageContainer; - _packageAssemblyResolver = packageAssemblyResolver; - _logger = logProvider.ForCurrentType(); - } - - internal string GetMainScript(IPackageObject package) - { - var content = package.GetContentFiles() - .Where(f => f.EndsWith("main.csx", StringComparison.InvariantCultureIgnoreCase)).ToArray(); - - string script = null; - var count = content.Length; - - if (count == 1) - { - script = content[0]; - } - else if (content.Length > 1) - { - _logger.WarnFormat("Script Libraries in '{0}' ignored due to multiple Main files being present", package.FullName); - return null; - } - - if (script != null) - { - _logger.DebugFormat("Found main script: {0}", script); - } - - return script; - } - - public virtual string ScriptLibrariesFile => "ScriptLibraries.csx"; - - public void Compose(string workingDirectory, StringBuilder builder = null) - { - if (string.IsNullOrWhiteSpace(ScriptLibrariesFile)) - { - return; - } - - var packagesPath = Path.Combine(workingDirectory, _fileSystem.PackagesFolder); - var packageScriptsPath = Path.Combine(packagesPath, ScriptLibrariesFile); - if (!_fileSystem.DirectoryExists(packagesPath) || _fileSystem.FileExists(packageScriptsPath)) - { - return; - } - - if (builder == null) - { - builder = new StringBuilder(); - } - - var namespaces = new List(); - var references = new List(); - var packageReferences = _packageAssemblyResolver.GetPackages(workingDirectory); - foreach (var reference in packageReferences) - { - ProcessPackage(packagesPath, reference, builder, references, namespaces); - } - - foreach (var ns in namespaces) - { - builder.Insert(0, String.Format("using {0};{1}", ns, Environment.NewLine)); - } - - foreach (var reference in references) - { - builder.Insert(0, String.Format("#r {0}{1}", reference, Environment.NewLine)); - } - - _fileSystem.WriteToFile(packageScriptsPath, builder.ToString()); - } - - protected internal virtual void ProcessPackage( - string packagesPath, - IPackageReference reference, - StringBuilder builder, - List references, - List namespaces) - { - Guard.AgainstNullArgument("reference", reference); - Guard.AgainstNullArgument("builder", builder); - Guard.AgainstNullArgument("references", references); - Guard.AgainstNullArgument("namespaces", namespaces); - - _logger.DebugFormat("Finding package:{0}", reference.PackageId); - var package = _packageContainer.FindPackage(packagesPath, reference); - - if (package == null) - { - _logger.WarnFormat("Package missing: {0}", reference.PackageId); - return; - } - - _logger.Debug("Package found"); - var script = GetMainScript(package); - if (script != null) - { - script = Path.Combine(packagesPath, string.Format("{0}.{1}", package.Id, package.TextVersion), script); - _logger.Debug("Pre-processing script"); - var result = _preProcessor.ProcessFile(script); - var fileWithoutExtension = Path.GetFileNameWithoutExtension(script); - var classname = fileWithoutExtension.Substring(0, fileWithoutExtension.Length - 4); - _logger.DebugFormat("Created Script Libary class: {0}", classname); - var classBuilder = new StringBuilder(); - classBuilder.AppendFormat("public class {0} : ScriptCs.ScriptLibraryWrapper {{{1}", classname, Environment.NewLine); - classBuilder.AppendLine(result.Code); - classBuilder.AppendLine("}"); - var classDefinition = classBuilder.ToString(); - _logger.TraceFormat("Class definition:{0}{0}{1}", Environment.NewLine, classDefinition); - builder.Append(classDefinition); - references.AddRange(result.References); - namespaces.AddRange(result.Namespaces); - } - } - } -} diff --git a/src/ScriptCs.Core/ScriptLibraryWrapper.cs b/src/ScriptCs.Core/ScriptLibraryWrapper.cs deleted file mode 100644 index 88d71c23..00000000 --- a/src/ScriptCs.Core/ScriptLibraryWrapper.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public abstract class ScriptLibraryWrapper - { - private static IScriptHost _scriptHost; - - internal static IScriptHost ScriptHost => _scriptHost; - - public static void SetHost(IScriptHost scriptHost) - { - _scriptHost = scriptHost; - } - - public static T Require() where T:IScriptPackContext - { - return _scriptHost.Require(); - } - - public static IScriptEnvironment Env => _scriptHost.Env; - } -} diff --git a/src/ScriptCs.Core/ScriptPackManager.cs b/src/ScriptCs.Core/ScriptPackManager.cs deleted file mode 100644 index cd21946a..00000000 --- a/src/ScriptCs.Core/ScriptPackManager.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using ScriptCs.Contracts; -using ScriptCs.Exceptions; - -namespace ScriptCs -{ - public class ScriptPackManager : IScriptPackManager - { - private readonly IDictionary _contexts = new Dictionary(); - - public ScriptPackManager(IEnumerable contexts) - { - Guard.AgainstNullArgument("contexts", contexts); - - foreach (var context in contexts) - { - _contexts[context.GetType()] = context; - } - } - - public TContext Get() where TContext : IScriptPackContext - { - var key = typeof(TContext); - if (!_contexts.ContainsKey(key)) - { - throw new ScriptPackException(string.Format("Tried to resolve a script pack '{0}', but such script pack is not available in the current execution context.", key)); - } - - return (TContext)_contexts[key]; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ScriptPackResolver.cs b/src/ScriptCs.Core/ScriptPackResolver.cs deleted file mode 100644 index 00d6da11..00000000 --- a/src/ScriptCs.Core/ScriptPackResolver.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptPackResolver : IScriptPackResolver - { - private IEnumerable _scriptPacks; - - public ScriptPackResolver(IEnumerable scriptPacks) - { - _scriptPacks = scriptPacks; - } - - public IEnumerable GetPacks() => _scriptPacks; - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ScriptServices.cs b/src/ScriptCs.Core/ScriptServices.cs deleted file mode 100644 index bdd221e9..00000000 --- a/src/ScriptCs.Core/ScriptServices.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Collections.Generic; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class ScriptServices - { - public ScriptServices( - IFileSystem fileSystem, - IPackageAssemblyResolver packageAssemblyResolver, - IScriptExecutor executor, - IRepl repl, - IScriptEngine engine, - IFilePreProcessor filePreProcessor, - IScriptPackResolver scriptPackResolver, - IPackageInstaller packageInstaller, - IObjectSerializer objectSerializer, - ILogProvider logProvider, - IAssemblyResolver assemblyResolver, - IEnumerable replCommands, - IConsole console = null, - IInstallationProvider installationProvider = null, - IScriptLibraryComposer scriptLibraryComposer = null - ) - { - FileSystem = fileSystem; - PackageAssemblyResolver = packageAssemblyResolver; - Executor = executor; - Repl = repl; - Engine = engine; - FilePreProcessor = filePreProcessor; - ScriptPackResolver = scriptPackResolver; - PackageInstaller = packageInstaller; - ObjectSerializer = objectSerializer; - LogProvider = logProvider; - Console = console; - AssemblyResolver = assemblyResolver; - InstallationProvider = installationProvider; - ReplCommands = replCommands; - ScriptLibraryComposer = scriptLibraryComposer; - } - - public IFileSystem FileSystem { get; private set; } - public IPackageAssemblyResolver PackageAssemblyResolver { get; private set; } - public IScriptExecutor Executor { get; private set; } - public IRepl Repl { get; private set; } - public IScriptPackResolver ScriptPackResolver { get; private set; } - public IPackageInstaller PackageInstaller { get; private set; } - public IObjectSerializer ObjectSerializer { get; private set; } - public ILogProvider LogProvider { get; private set; } - public IScriptEngine Engine { get; private set; } - public IFilePreProcessor FilePreProcessor { get; private set; } - public IConsole Console { get; private set; } - public IAssemblyResolver AssemblyResolver { get; private set; } - public IInstallationProvider InstallationProvider { get; private set; } - public IEnumerable ReplCommands { get; private set; } - public IScriptLibraryComposer ScriptLibraryComposer { get; private set; } - } -} diff --git a/src/ScriptCs.Core/SessionState.cs b/src/ScriptCs.Core/SessionState.cs deleted file mode 100644 index 67f9fff7..00000000 --- a/src/ScriptCs.Core/SessionState.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Collections.Generic; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public class SessionState - { - public T Session { get; set; } - - public AssemblyReferences References { get; set; } - - public HashSet Namespaces { get; set; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/ShebangLineProcessor.cs b/src/ScriptCs.Core/ShebangLineProcessor.cs deleted file mode 100644 index a422f26b..00000000 --- a/src/ScriptCs.Core/ShebangLineProcessor.cs +++ /dev/null @@ -1,15 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public interface IShebangLineProcessor : ILineProcessor - { - } - - public class ShebangLineProcessor : DirectiveLineProcessor, IShebangLineProcessor - { - protected override string DirectiveName => "!/usr/bin/env"; - - protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line) => true; - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/StringExtensions.cs b/src/ScriptCs.Core/StringExtensions.cs deleted file mode 100644 index 07341764..00000000 --- a/src/ScriptCs.Core/StringExtensions.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace ScriptCs -{ - public static class StringExtensions - { - public static string DefineTrace(this string code) - { - return string.Format("#define TRACE{0}{1}", Environment.NewLine, code); - } - - /// - /// Split string on whitespace, but keeps string with quotes together - /// For example: :cd "\\Foo Bar" - /// :cd - /// "\\Foo Bar". - /// - /// String with or without quotes. - /// Array of strings. - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", - Justification = "Guard Against Null Argument method is not need because of the if statement.", - MessageId = "0")] - public static string[] SplitQuoted(this string argument) - { - if (string.IsNullOrWhiteSpace(argument)) - { - return argument.Split(' '); - } - - // count the number of quotes and throw something is not even - // the fastest way is to just loop thru the string - // http://cc.davelozinski.com/c-sharp/fastest-way-to-check-if-a-string-occurs-within-a-string - Func quoteCounterFunc = delegate (string line) - { - int count = 0; - for (int x = 0; x < line.Length; x++) - { - if (line[x] == '"') - { - count++; - } - } - return count; - }; - int quotes = quoteCounterFunc(argument); - if ((quotes % 2) != 0) - { - throw new ArgumentException("String is missing a closing quote"); - } - - List list = new List(argument.Split(' ')); - - // quoted string needs to be combine back together - if (quotes > 0 && list.Count > 0) - { - Predicate findQuoteFunc = delegate (string s) { return s.Contains("\""); }; - // create function to find string item with odd number of quotes - Func findOddQuotedItemFunc = delegate (int startingIndex) { - if (startingIndex < list.Count) - { - do - { - int quickFind = list.FindIndex(startingIndex, findQuoteFunc); - int quickCount = quoteCounterFunc(list[quickFind]); - if ((quickCount % 2) != 0) - { - return quickFind; - } - // we didn't find the quoted line we are looking for - startingIndex = quickFind + 1; - } while (startingIndex < list.Count); - } - return -1; - }; - - int index = 0; - do - { - int start = findOddQuotedItemFunc(index); - if (start > 0) - { - // we have to locate the next string with odd number of quotes - int end = findOddQuotedItemFunc(start + 1); - - string combined = string.Empty; - for (int x = start; x <= end; x++) - { - // because we split on whitespace, we have to put it back when combining - combined += list[x] + ' '; - } - list[start] = combined.TrimEnd(); // remove the extra whitespace that was added - - // removed the other parts of the combined string from the list - do - { - list.RemoveAt(end--); // from the bottom up - } while (start < end); - - // advance to next item in the adjusted list - index = start + 1; - } - else - { - break; - } - - } while (index < list.Count); - } - - return list.ToArray(); - } - - public static string UndefineTrace(this string code) - { - return string.Format("#undef TRACE{0}{1}", Environment.NewLine, code); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Core/UsingLineProcessor.cs b/src/ScriptCs.Core/UsingLineProcessor.cs deleted file mode 100644 index 5acbad12..00000000 --- a/src/ScriptCs.Core/UsingLineProcessor.cs +++ /dev/null @@ -1,47 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public interface IUsingLineProcessor : ILineProcessor - { - } - - public class UsingLineProcessor : IUsingLineProcessor - { - private const string UsingString = "using "; - - public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode) - { - Guard.AgainstNullArgument("context", context); - - if (!IsUsingLine(line)) - { - return false; - } - - // for using static, we will not extract the import into the context, but rather let it be treated as code - if (line.Contains(" static ")) - { - return false; - } - - var @namespace = GetNamespace(line); - if (!context.Namespaces.Contains(@namespace)) - { - context.Namespaces.Add(@namespace); - } - - return true; - } - - private static bool IsUsingLine(string line) => line.Trim(' ').StartsWith(UsingString) && !line.Contains("{") && line.Contains(";") && !line.Contains("="); - - private static string GetNamespace(string line) - { - return line.Trim(' ') - .Replace(UsingString, string.Empty) - .Replace("\"", string.Empty) - .Replace(";", string.Empty); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/CSharpPersistentEngine.cs b/src/ScriptCs.Engine.Roslyn/CSharpPersistentEngine.cs deleted file mode 100644 index 380e63a7..00000000 --- a/src/ScriptCs.Engine.Roslyn/CSharpPersistentEngine.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using ScriptCs.Contracts; - -namespace ScriptCs.Engine.Roslyn -{ - public class CSharpPersistentEngine : CSharpScriptCompilerEngine - { - private readonly IFileSystem _fileSystem; - private readonly ILog _log; - - public CSharpPersistentEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider, IFileSystem fileSystem) - : base(scriptHostFactory, logProvider) - { - Guard.AgainstNullArgument("logProvider", logProvider); - _log = logProvider.ForCurrentType(); - _fileSystem = fileSystem; - } - - protected override bool ShouldCompile() - { - var dllPath = GetDllTargetPath(); - - return !_fileSystem.FileExists(dllPath); - } - - protected override Assembly LoadAssembly(byte[] exeBytes, byte[] pdbBytes) - { - _log.DebugFormat("Writing assembly to {0}.", FileName); - - if (!_fileSystem.DirectoryExists(CacheDirectory)) - { - _fileSystem.CreateDirectory(CacheDirectory, true); - } - - var dllPath = GetDllTargetPath(); - _fileSystem.WriteAllBytes(dllPath, exeBytes); - - _log.DebugFormat("Loading assembly {0}.", dllPath); - return LoadAssemblyFromCache(); - } - - protected override Assembly LoadAssemblyFromCache() - { - var dllPath = GetDllTargetPath(); - return Assembly.LoadFrom(dllPath); - } - - private string GetDllTargetPath() - { - var dllName = FileName.Replace(Path.GetExtension(FileName), ".dll"); - var dllPath = Path.Combine(CacheDirectory, dllName); - return dllPath; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/CSharpReplEngine.cs b/src/ScriptCs.Engine.Roslyn/CSharpReplEngine.cs deleted file mode 100644 index 01e841ac..00000000 --- a/src/ScriptCs.Engine.Roslyn/CSharpReplEngine.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.CodeAnalysis.Scripting; -using ScriptCs.Contracts; - -namespace ScriptCs.Engine.Roslyn -{ - public class CSharpReplEngine : CSharpScriptEngine, IReplEngine - { - public CSharpReplEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider) - : base(scriptHostFactory, logProvider) - { - } - - public ICollection GetLocalVariables(ScriptPackSession scriptPackSession) - { - return this.GetLocalVariables(SessionKey, scriptPackSession); - } - - protected override ScriptResult Execute(string code, object globals, SessionState sessionState) - { - if (string.IsNullOrWhiteSpace(FileName) && !IsCompleteSubmission(code)) - return ScriptResult.Incomplete; - - if (sessionState.Session != null) - { - try - { - Log.Debug("Starting subsequent REPL execution"); - var result = sessionState.Session.ContinueWithAsync(code, ScriptOptions).GetAwaiter().GetResult(); - Log.Debug("Finished subsequent REPL execution"); - sessionState.Session = result; - return new ScriptResult(returnValue: result.ReturnValue); - } - catch (AggregateException ex) - { - return new ScriptResult(executionException: ex.InnerException); - } - catch (CompilationErrorException ex) - { - return new ScriptResult(compilationException: ex); - } - catch (Exception ex) - { - return new ScriptResult(executionException: ex); - } - } - - return base.Execute(code, globals, sessionState); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/CSharpScriptCompilerEngine.cs b/src/ScriptCs.Engine.Roslyn/CSharpScriptCompilerEngine.cs deleted file mode 100644 index 3360ddf5..00000000 --- a/src/ScriptCs.Engine.Roslyn/CSharpScriptCompilerEngine.cs +++ /dev/null @@ -1,119 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CSharp.Scripting; -using Microsoft.CodeAnalysis.Scripting; -using ScriptCs.Contracts; -using ScriptCs.Exceptions; -using Microsoft.CodeAnalysis.Emit; - -namespace ScriptCs.Engine.Roslyn -{ - public abstract class CSharpScriptCompilerEngine : CommonScriptEngine - { - private const string CompiledScriptClass = "Submission#0"; - private const string CompiledScriptMethod = ""; - private readonly ILog _log; - - protected CSharpScriptCompilerEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider) - : base(scriptHostFactory, logProvider) - { - Guard.AgainstNullArgument("logProvider", logProvider); - _log = logProvider.ForCurrentType(); - } - - protected abstract bool ShouldCompile(); - - protected abstract Assembly LoadAssembly(byte[] exeBytes, byte[] pdbBytes); - - protected abstract Assembly LoadAssemblyFromCache(); - - protected override ScriptResult Execute(string code, object globals, SessionState sessionState) - { - return ShouldCompile() - ? CompileAndExecute(code, globals) - : InvokeEntryPointMethod(globals, LoadAssemblyFromCache()); - } - - protected override ScriptState GetScriptState(string code, object globals) - { - return null; - } - - protected ScriptResult CompileAndExecute(string code, object globals) - { - _log.Debug("Compiling submission"); - try - { - var script = CSharpScript.Create(code, ScriptOptions, globals.GetType()); - var compilation = script.GetCompilation(); - - using (var exeStream = new MemoryStream()) - using (var pdbStream = new MemoryStream()) - { - var result = compilation.Emit(exeStream, pdbStream: pdbStream, options: new EmitOptions(). - WithDebugInformationFormat(GetPlatformSpecificDebugInformationFormat())); - - if (result.Success) - { - _log.Debug("Compilation was successful."); - - var assembly = LoadAssembly(exeStream.ToArray(), pdbStream.ToArray()); - return InvokeEntryPointMethod(globals, assembly); - } - - var errors = string.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString())); - - _log.ErrorFormat("Error occurred when compiling: {0})", errors); - - return new ScriptResult(compilationException: new ScriptCompilationException(errors)); - } - } - catch (Exception compileException) - { - //we catch Exception rather than CompilationErrorException because there might be issues with assembly loading too - return new ScriptResult(compilationException: new ScriptCompilationException(compileException.Message, compileException)); - } - } - - private ScriptResult InvokeEntryPointMethod(object globals, Assembly assembly) - { - _log.Debug("Retrieving compiled script class (reflection)."); - - // the following line can throw NullReferenceException, if that happens it's useful to notify that an error ocurred - var type = assembly.GetType(CompiledScriptClass); - _log.Debug("Retrieving compiled script method (reflection)."); - var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public); - - try - { - _log.Debug("Invoking method."); - var submissionStates = new object[2]; - submissionStates[0] = globals; - var result = method.Invoke(null, new[] {submissionStates}) as Task; - return new ScriptResult(returnValue: result.GetAwaiter().GetResult()); - } - catch (Exception executeException) - { - _log.Error("An error occurred when executing the scripts."); - - var ex = executeException.InnerException ?? executeException; - return new ScriptResult(executionException: ex); - } - } - - private static DebugInformationFormat GetPlatformSpecificDebugInformationFormat() - { - // Mono, use PortablePdb - if (Type.GetType("Mono.Runtime") != null) - { - return DebugInformationFormat.PortablePdb; - } - - // otherwise standard PDB - return DebugInformationFormat.Pdb; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/CSharpScriptEngine.cs b/src/ScriptCs.Engine.Roslyn/CSharpScriptEngine.cs deleted file mode 100644 index ad99e9b9..00000000 --- a/src/ScriptCs.Engine.Roslyn/CSharpScriptEngine.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Scripting; -using Microsoft.CodeAnalysis.Scripting; -using ScriptCs.Contracts; - -namespace ScriptCs.Engine.Roslyn -{ - public class CSharpScriptEngine : CommonScriptEngine - { - public CSharpScriptEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider) : base(scriptHostFactory, logProvider) - { - } - - - protected override ScriptState GetScriptState(string code, object globals) - { - return CSharpScript.RunAsync(code, ScriptOptions, globals).GetAwaiter().GetResult(); - } - - protected bool IsCompleteSubmission(string code) - { - //invalid REPL command - if (code.StartsWith(":")) - { - return true; - } - - var options = new CSharpParseOptions(LanguageVersion.Latest, DocumentationMode.Parse, SourceCodeKind.Script); - - var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, options: options); - return SyntaxFactory.IsCompleteSubmission(syntaxTree); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/CSharpScriptInMemoryEngine.cs b/src/ScriptCs.Engine.Roslyn/CSharpScriptInMemoryEngine.cs deleted file mode 100644 index c93cead6..00000000 --- a/src/ScriptCs.Engine.Roslyn/CSharpScriptInMemoryEngine.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Reflection; -using ScriptCs.Contracts; - -namespace ScriptCs.Engine.Roslyn -{ - public class CSharpScriptInMemoryEngine : CSharpScriptCompilerEngine - { - private readonly ILog _log; - - public CSharpScriptInMemoryEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider) - : base(scriptHostFactory, logProvider) - { - Guard.AgainstNullArgument("logProvider", logProvider); - _log = logProvider.ForCurrentType(); - } - - protected override bool ShouldCompile() - { - return true; - } - - protected override Assembly LoadAssemblyFromCache() - { - throw new NotImplementedException("Reaching this point indicates a bug. The CSharpScriptInMemoryEngine should never load the assembly from the cache."); - } - - protected override Assembly LoadAssembly(byte[] exeBytes, byte[] pdbBytes) - { - _log.Debug("Loading assembly from memory."); - - // this is required for debugging. otherwise, the .dll is not related to the .pdb - // there might be ways of doing this without "loading", haven't found one yet - return AppDomain.CurrentDomain.Load(exeBytes, pdbBytes); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/CommonScriptEngine.cs b/src/ScriptCs.Engine.Roslyn/CommonScriptEngine.cs deleted file mode 100644 index 5c482533..00000000 --- a/src/ScriptCs.Engine.Roslyn/CommonScriptEngine.cs +++ /dev/null @@ -1,206 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Reflection; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Scripting; -using Microsoft.CodeAnalysis.Scripting; -using ScriptCs.Contracts; - -namespace ScriptCs.Engine.Roslyn -{ - // note this class is a base for future VB engine - public abstract class CommonScriptEngine : IScriptEngine - { - protected ScriptOptions ScriptOptions { get; set; } - - protected ScriptMetadataResolver ScriptMetadataResolver { get; private set; } - - private readonly IScriptHostFactory _scriptHostFactory; - protected ILog Log; - - public const string SessionKey = "Session"; - - protected CommonScriptEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider) - { - Guard.AgainstNullArgument("logProvider", logProvider); - ScriptMetadataResolver = ScriptMetadataResolver.Default; - ScriptOptions = ScriptOptions.Default. - WithReferences(typeof(object).Assembly, typeof(TupleElementNamesAttribute).Assembly). // System.ValueTuple - WithMetadataResolver(ScriptMetadataResolver); - _scriptHostFactory = scriptHostFactory; - Log = logProvider.ForCurrentType(); - SetCSharpVersionToLatest(); - } - - public string BaseDirectory - { - get => ScriptMetadataResolver.BaseDirectory; - set => ScriptMetadataResolver = ScriptMetadataResolver.WithBaseDirectory(value); - } - - public string CacheDirectory { get; set; } - - public string FileName { get; set; } - - public string ScriptPath { get; set; } - - public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable namespaces, ScriptPackSession scriptPackSession) - { - if (scriptPackSession == null) - { - throw new ArgumentNullException(nameof(scriptPackSession)); - } - - if (references == null) - { - throw new ArgumentNullException(nameof(references)); - } - - Log.Debug("Starting to create execution components"); - Log.Debug("Creating script host"); - - var executionReferences = new AssemblyReferences(references.Assemblies, references.Paths); - executionReferences.Union(scriptPackSession.References); - - ScriptResult scriptResult; - SessionState sessionState; - - var isFirstExecution = !scriptPackSession.State.ContainsKey(SessionKey); - - if (isFirstExecution) - { - var host = _scriptHostFactory.CreateScriptHost( - new ScriptPackManager(scriptPackSession.Contexts), scriptArgs); - - ScriptLibraryWrapper.SetHost(host); - Log.Debug("Creating session"); - - var hostType = host.GetType(); - - ScriptOptions = ScriptOptions.AddReferences(hostType.Assembly); - - var allNamespaces = namespaces.Union(scriptPackSession.Namespaces).Distinct(); - - foreach (var reference in executionReferences.Paths) - { - Log.DebugFormat("Adding reference to {0}", reference); - ScriptOptions = ScriptOptions.AddReferences(reference); - } - - foreach (var assembly in executionReferences.Assemblies) - { - Log.DebugFormat("Adding reference to {0}", assembly.FullName); - ScriptOptions = ScriptOptions.AddReferences(assembly); - } - - foreach (var @namespace in allNamespaces) - { - Log.DebugFormat("Importing namespace {0}", @namespace); - ScriptOptions = ScriptOptions.AddImports(@namespace); - } - - sessionState = new SessionState { References = executionReferences, Namespaces = new HashSet(allNamespaces) }; - scriptPackSession.State[SessionKey] = sessionState; - - scriptResult = Execute(code, host, sessionState); - } - else - { - Log.Debug("Reusing existing session"); - sessionState = (SessionState)scriptPackSession.State[SessionKey]; - - if (sessionState.References == null) - { - sessionState.References = new AssemblyReferences(); - } - - if (sessionState.Namespaces == null) - { - sessionState.Namespaces = new HashSet(); - } - - var newReferences = executionReferences.Except(sessionState.References); - - foreach (var reference in newReferences.Paths) - { - Log.DebugFormat("Adding reference to {0}", reference); - ScriptOptions = ScriptOptions.AddReferences(reference); - sessionState.References = sessionState.References.Union(new[] { reference }); - } - - foreach (var assembly in newReferences.Assemblies) - { - Log.DebugFormat("Adding reference to {0}", assembly.FullName); - ScriptOptions = ScriptOptions.AddReferences(assembly); - sessionState.References = sessionState.References.Union(new[] { assembly }); - } - - var newNamespaces = namespaces.Except(sessionState.Namespaces); - - foreach (var @namespace in newNamespaces) - { - Log.DebugFormat("Importing namespace {0}", @namespace); - ScriptOptions = ScriptOptions.AddImports(@namespace); - sessionState.Namespaces.Add(@namespace); - } - - if (string.IsNullOrWhiteSpace(code)) - { - return ScriptResult.Empty; - } - - scriptResult = Execute(code, sessionState.Session, sessionState); - } - - return scriptResult; - - //todo handle namespace failures - //https://github.com/dotnet/roslyn/issues/1012 - } - - protected virtual ScriptResult Execute(string code, object globals, SessionState sessionState) - { - try - { - Log.Debug("Starting execution"); - var result = GetScriptState(code, globals); - Log.Debug("Finished execution"); - sessionState.Session = result; - return new ScriptResult(returnValue: result.ReturnValue); - } - catch (AggregateException ex) - { - return new ScriptResult(executionException: ex.InnerException); - } - catch (CompilationErrorException ex) - { - return new ScriptResult(compilationException: ex); - } - catch (Exception ex) - { - return new ScriptResult(executionException: ex); - } - } - - protected abstract ScriptState GetScriptState(string code, object globals); - - private void SetCSharpVersionToLatest() - { - try - { - // reset default scripting mode to latest language version to enable C# 7.1 features - // this is not needed once https://github.com/dotnet/roslyn/pull/21331 ships - var csharpScriptCompilerType = typeof(CSharpScript).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScriptCompiler"); - var parseOptionsField = csharpScriptCompilerType?.GetField("s_defaultOptions", BindingFlags.Static | BindingFlags.NonPublic); - parseOptionsField?.SetValue(null, new CSharpParseOptions(LanguageVersion.Latest, kind: SourceCodeKind.Script)); - } - catch (Exception) - { - Log.Warn("Unable to set C# language version to latest, will use the default version."); - } - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/Properties/ScriptCs.Engine.Roslyn.nuspec b/src/ScriptCs.Engine.Roslyn/Properties/ScriptCs.Engine.Roslyn.nuspec deleted file mode 100644 index e4e3ff78..00000000 --- a/src/ScriptCs.Engine.Roslyn/Properties/ScriptCs.Engine.Roslyn.nuspec +++ /dev/null @@ -1,20 +0,0 @@ - - - - ScriptCs.Engine.Roslyn - $version$ - Glenn Block, Filip Wojcieszyn, Justin Rusbatch, Kristian Hellang, Damian Schenkelman, Adam Ralph - Glenn Block, Justin Rusbatch, Filip Wojcieszyn - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Engine.Roslyn provides a Roslyn-based script engine for scriptcs. - roslyn csx script scriptcs - - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/ReplEngineExtensions.cs b/src/ScriptCs.Engine.Roslyn/ReplEngineExtensions.cs deleted file mode 100644 index 4b4307c4..00000000 --- a/src/ScriptCs.Engine.Roslyn/ReplEngineExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Microsoft.CodeAnalysis.Scripting; -using ScriptCs.Contracts; - -namespace ScriptCs.Engine.Roslyn -{ - public static class ReplEngineExtensions - { - public static ICollection GetLocalVariables(this IReplEngine replEngine, string sessionKey, - ScriptPackSession scriptPackSession) - { - if (scriptPackSession != null && scriptPackSession.State.ContainsKey(sessionKey)) - { - var sessionState = (SessionState)scriptPackSession.State[sessionKey]; - return sessionState.Session.Variables.Select(x => $"{x.Type} {x.Name}").Distinct().ToArray(); - } - - return new string[0]; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/RoslynModule.cs b/src/ScriptCs.Engine.Roslyn/RoslynModule.cs deleted file mode 100644 index 4fdb0196..00000000 --- a/src/ScriptCs.Engine.Roslyn/RoslynModule.cs +++ /dev/null @@ -1,21 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.Engine.Roslyn -{ - [Module("roslyn")] - public class RoslynModule : IModule - { - public void Initialize(IModuleConfiguration config) - { - Guard.AgainstNullArgument("config", config); - - if (!config.Overrides.ContainsKey(typeof(IScriptEngine))) - { - var engineType = config.Cache ? typeof(CSharpPersistentEngine) : typeof(CSharpScriptEngine); - engineType = config.Debug ? typeof(CSharpScriptInMemoryEngine) : engineType; - engineType = config.IsRepl ? typeof(CSharpReplEngine) : engineType; - config.Overrides[typeof(IScriptEngine)] = engineType; - } - } - } -} diff --git a/src/ScriptCs.Engine.Roslyn/ScriptCs.Engine.Roslyn.csproj b/src/ScriptCs.Engine.Roslyn/ScriptCs.Engine.Roslyn.csproj deleted file mode 100644 index e88e280a..00000000 --- a/src/ScriptCs.Engine.Roslyn/ScriptCs.Engine.Roslyn.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - 1.0.0 - netstandard2.0 - ScriptCs.Engine.Roslyn - Glenn Block, Filip Wojcieszyn, Justin Rusbatch - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Engine.Roslyn - ScriptCs.Engine.Roslyn provides a Roslyn-based script engine for scriptcs. - roslyn csx script scriptcs - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Engine.Roslyn/app.config b/src/ScriptCs.Engine.Roslyn/app.config deleted file mode 100644 index d97e7d25..00000000 --- a/src/ScriptCs.Engine.Roslyn/app.config +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ColoredConsoleLogProvider.cs b/src/ScriptCs.Hosting/ColoredConsoleLogProvider.cs deleted file mode 100644 index 94dd7d23..00000000 --- a/src/ScriptCs.Hosting/ColoredConsoleLogProvider.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class ColoredConsoleLogProvider : ILogProvider - { - private static readonly Disposable disposable = new Disposable(); - private static readonly Dictionary colors = - new Dictionary - { - { LogLevel.Fatal, ConsoleColor.Red }, - { LogLevel.Error, ConsoleColor.Red }, - { LogLevel.Warn, ConsoleColor.DarkYellow }, - { LogLevel.Info, ConsoleColor.Gray }, - { LogLevel.Debug, ConsoleColor.DarkGray }, - { LogLevel.Trace, ConsoleColor.DarkMagenta }, - }; - - private readonly LogLevel _logLevel; - private readonly IConsole _console; - - public ColoredConsoleLogProvider(LogLevel logLevel, IConsole console) - { - Guard.AgainstNullArgument("console", console); - - _logLevel = logLevel; - _console = console; - } - - public Logger GetLogger(string name) - { - return (logLevel, messageFunc, exception, formatParameters) => - Log(name, logLevel, messageFunc, exception, formatParameters); - } - - public IDisposable OpenNestedContext(string message) => disposable; - - public IDisposable OpenMappedContext(string key, string value) => disposable; - - public bool Log( - string name, LogLevel logLevel, Func messageFunc, Exception exception, params object[] formatParameters) - { - Guard.AgainstNullArgument("formatParameters", formatParameters); - - var isEnabled = IsEnabled(logLevel); - if (!isEnabled || messageFunc == null) - { - return isEnabled; - } - - var prefix = logLevel == LogLevel.Info - ? null - : string.Concat(logLevel.ToString().ToUpperInvariant(), ": "); - - if (_logLevel == LogLevel.Debug || _logLevel == LogLevel.Trace) - { - prefix = string.Concat(prefix, "[", name, "] "); - } - - var message = string.Format(CultureInfo.InvariantCulture, messageFunc(), formatParameters); - - var suffix = string.Empty; - if (exception != null) - { - if (_logLevel == LogLevel.Debug || _logLevel == LogLevel.Trace) - { - var exceptions = new List(); - while (exception != null) - { - var exceptionString = string.Format( - CultureInfo.InvariantCulture, - "[{0}] {1}{2}{3}", - exception.GetType().FullName, - exception.Message, - Environment.NewLine, - exception.StackTrace); - - exceptions.Add(exceptionString); - exception = exception.InnerException; - } - - var divider = string.Format( - CultureInfo.InvariantCulture, "{0}{1}{0}", Environment.NewLine, "=== INNER EXCEPTION ==="); - - suffix = " " + string.Join(divider, exceptions); - } - else - { - suffix = string.Format( - CultureInfo.InvariantCulture, " [{0}] {1}", exception.GetType().Name, exception.Message); - } - } - - ConsoleColor color; - if (!colors.TryGetValue(logLevel, out color)) - { - color = ConsoleColor.White; - } - - var originalColor = _console.ForegroundColor; - _console.ForegroundColor = color; - try - { - _console.WriteLine(string.Concat(prefix, message, suffix)); - } - finally - { - _console.ForegroundColor = originalColor; - } - - return true; - } - - private bool IsEnabled(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Fatal: - return true; - case LogLevel.Error: - return - _logLevel == LogLevel.Error || - _logLevel == LogLevel.Warn || - _logLevel == LogLevel.Info || - _logLevel == LogLevel.Debug || - _logLevel == LogLevel.Trace; - case LogLevel.Warn: - return - _logLevel == LogLevel.Warn || - _logLevel == LogLevel.Info || - _logLevel == LogLevel.Debug || - _logLevel == LogLevel.Trace; - case LogLevel.Info: - return - _logLevel == LogLevel.Info || - _logLevel == LogLevel.Debug || - _logLevel == LogLevel.Trace; - case LogLevel.Debug: - return - _logLevel == LogLevel.Debug || - _logLevel == LogLevel.Trace; - case LogLevel.Trace: - return - _logLevel == LogLevel.Trace; - } - - return true; - } - - private sealed class Disposable : IDisposable - { - public void Dispose() - { - } - } - } -} diff --git a/src/ScriptCs.Hosting/DirectoryInfo.cs b/src/ScriptCs.Hosting/DirectoryInfo.cs deleted file mode 100644 index dff1ed75..00000000 --- a/src/ScriptCs.Hosting/DirectoryInfo.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ScriptCs.Hosting -{ - public class DirectoryInfo - { - public DirectoryInfo() - { - Guid = Guid.NewGuid(); - Files = new List(); - Directories = new Dictionary(); - } - - public Guid Guid { get; private set; } - public string Name { get; set; } - public string Path { get; set; } - public string FullPath { get; set; } - public List Files { get; private set; } - public Dictionary Directories { get; private set; } - } -} diff --git a/src/ScriptCs.Hosting/FileConsole.cs b/src/ScriptCs.Hosting/FileConsole.cs deleted file mode 100644 index 984b51ed..00000000 --- a/src/ScriptCs.Hosting/FileConsole.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.IO; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class FileConsole : IConsole - { - private readonly string _path; - private readonly IConsole _innerConsole; - - public FileConsole(string path, IConsole innerConsole) - { - Guard.AgainstNullArgument("innerConsole", innerConsole); - - _path = path; - _innerConsole = innerConsole; - } - - public void Write(string value) - { - _innerConsole.Write(value); - this.Append(value); - } - - public void WriteLine() - { - _innerConsole.WriteLine(); - this.AppendLine(string.Empty); - } - - public void WriteLine(string value) - { - _innerConsole.WriteLine(value); - this.AppendLine(value); - } - - public string ReadLine(string prompt) - { - var line = _innerConsole.ReadLine(""); - this.AppendLine(line); - return line; - } - - public void Clear() - { - _innerConsole.Clear(); - } - - public void Exit() - { - _innerConsole.Exit(); - } - - public void ResetColor() - { - _innerConsole.ResetColor(); - } - - public ConsoleColor ForegroundColor - { - get => _innerConsole.ForegroundColor; - set => _innerConsole.ForegroundColor = value; - } - - public int Width => int.MaxValue; - - private void Append(string text) - { - using (var writer = new StreamWriter(_path, true)) - { - writer.Write(text); - writer.Flush(); - } - } - - private void AppendLine(string text) - { - this.Append(text + Environment.NewLine); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/IInitializationServices.cs b/src/ScriptCs.Hosting/IInitializationServices.cs deleted file mode 100644 index 66ae6e88..00000000 --- a/src/ScriptCs.Hosting/IInitializationServices.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public interface IInitializationServices - { - IAssemblyResolver GetAssemblyResolver(); - - IModuleLoader GetModuleLoader(); - - IFileSystem GetFileSystem(); - - IInstallationProvider GetInstallationProvider(); - - IPackageAssemblyResolver GetPackageAssemblyResolver(); - - IPackageInstaller GetPackageInstaller(); - - ILogProvider LogProvider { get; } - - IAppDomainAssemblyResolver GetAppDomainAssemblyResolver(); - - IAssemblyUtility GetAssemblyUtility(); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/IModuleLoader.cs b/src/ScriptCs.Hosting/IModuleLoader.cs deleted file mode 100644 index ec610e53..00000000 --- a/src/ScriptCs.Hosting/IModuleLoader.cs +++ /dev/null @@ -1,9 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public interface IModuleLoader - { - void Load(IModuleConfiguration config, string[] modulePackagesPaths, string hostBin, string extension, params string[] moduleNames); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/IRuntimeServices.cs b/src/ScriptCs.Hosting/IRuntimeServices.cs deleted file mode 100644 index 9dd18b36..00000000 --- a/src/ScriptCs.Hosting/IRuntimeServices.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ScriptCs.Hosting -{ - public interface IRuntimeServices - { - ScriptServices GetScriptServices(); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/IScriptServicesBuilder.cs b/src/ScriptCs.Hosting/IScriptServicesBuilder.cs deleted file mode 100644 index aa179f89..00000000 --- a/src/ScriptCs.Hosting/IScriptServicesBuilder.cs +++ /dev/null @@ -1,27 +0,0 @@ -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public interface IScriptServicesBuilder : IServiceOverrides - { - ScriptServices Build(); - - IScriptServicesBuilder Cache(bool cache = true); - - IScriptServicesBuilder ScriptName(string name); - - IScriptServicesBuilder Repl(bool repl = true); - - IScriptServicesBuilder Debug(bool debug = true); - - IScriptServicesBuilder LogLevel(LogLevel level); - - IScriptServicesBuilder LoadModules(string extension, params string[] moduleNames); - - IScriptServicesBuilder LoadScriptPacks(bool load = true); - - IInitializationServices InitializationServices { get; } - - IConsole ConsoleInstance { get; } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ITypeResolver.cs b/src/ScriptCs.Hosting/ITypeResolver.cs deleted file mode 100644 index f262527f..00000000 --- a/src/ScriptCs.Hosting/ITypeResolver.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using System.Threading.Tasks; - -namespace ScriptCs.Hosting -{ - public interface ITypeResolver - { - Type ResolveType(string typeName); - } -} diff --git a/src/ScriptCs.Hosting/IVisualStudioSolution.cs b/src/ScriptCs.Hosting/IVisualStudioSolution.cs deleted file mode 100644 index 91eb6111..00000000 --- a/src/ScriptCs.Hosting/IVisualStudioSolution.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ScriptCs.Contracts -{ - public interface IVisualStudioSolution - { - void AddHeader(); - void AddScriptcsProject(string scriptcsPath, string workingPath, string args, bool attach, Guid projectGuid); - void AddProject(string path, string name, Guid guid, string[] files); - void AddGlobalHeader(Guid projectGuid); - void AddGlobalNestedProjects(IList nestedItems); - void AddGlobal(Guid projectGuid, IList nestedItems); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/IVisualStudioSolutionWriter.cs b/src/ScriptCs.Hosting/IVisualStudioSolutionWriter.cs deleted file mode 100644 index c893902e..00000000 --- a/src/ScriptCs.Hosting/IVisualStudioSolutionWriter.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using ScriptCs.Contracts; - -namespace ScriptCs.Contracts -{ - public interface IVisualStudioSolutionWriter - { - string WriteSolution(IFileSystem fs, string script, IVisualStudioSolution solution, IList nestedItems = null); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/InitializationServices.cs b/src/ScriptCs.Hosting/InitializationServices.cs deleted file mode 100644 index 8e0fa92c..00000000 --- a/src/ScriptCs.Hosting/InitializationServices.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System; -using System.Collections.Generic; -using Autofac; -using ScriptCs.Contracts; -using ScriptCs.Hosting.Package; - -namespace ScriptCs.Hosting -{ - public class InitializationServices : ScriptServicesRegistration, IInitializationServices - { - private readonly ILog _log; - - public InitializationServices(ILogProvider logProvider, IDictionary overrides = null) - : base(logProvider, overrides) - { - Guard.AgainstNullArgument("logProvider", logProvider); - - _log = logProvider.ForCurrentType(); - - } - - protected override IContainer CreateContainer() - { - var builder = new ContainerBuilder(); - _log.Debug("Registering initialization services"); - builder.RegisterInstance(this.LogProvider); - builder.RegisterType().As(); - - RegisterLineProcessors(builder); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - return builder.Build(); - } - - private IAssemblyResolver _assemblyResolver; - - public IAssemblyResolver GetAssemblyResolver() - { - return GetService(ref _assemblyResolver); - } - - private IModuleLoader _moduleLoader; - - public IModuleLoader GetModuleLoader() - { - return GetService(ref _moduleLoader); - } - - private IFileSystem _fileSystem; - - public IFileSystem GetFileSystem() - { - return GetService(ref _fileSystem); - } - - private IInstallationProvider _installationProvider; - - public IInstallationProvider GetInstallationProvider() - { - return GetService(ref _installationProvider); - } - - private IPackageAssemblyResolver _packageAssemblyResolver; - - public IPackageAssemblyResolver GetPackageAssemblyResolver() - { - return GetService(ref _packageAssemblyResolver); - } - - private IPackageInstaller _packageInstaller; - - public IPackageInstaller GetPackageInstaller() - { - return GetService(ref _packageInstaller); - } - - private IAppDomainAssemblyResolver _appDomainAssemblyResolver; - - public IAppDomainAssemblyResolver GetAppDomainAssemblyResolver() - { - return GetService(ref _appDomainAssemblyResolver); - } - - private IAssemblyUtility _assemblyUtility; - - public IAssemblyUtility GetAssemblyUtility() - { - return GetService(ref _assemblyUtility); - } - - private T GetService(ref T service ) - { - if (Equals(service,null)) - { - _log.Debug(string.Format("Resolving {0}", typeof(T).Name)); - service = Container.Resolve(); - } - - return service; - } - } -} diff --git a/src/ScriptCs.Hosting/LineEditor.cs b/src/ScriptCs.Hosting/LineEditor.cs deleted file mode 100644 index 7c2ae132..00000000 --- a/src/ScriptCs.Hosting/LineEditor.cs +++ /dev/null @@ -1,1173 +0,0 @@ -// -// getline.cs: A command line editor -// -// Authors: -// Miguel de Icaza (miguel@novell.com) -// -// Copyright 2008 Novell, Inc. -// -// Dual-licensed under the terms of the MIT X11 license or the -// Apache License 2.0 -// -// USE -define:DEMO to build this as a standalone file and test it -// -// TODO: -// Enter an error (a = 1); Notice how the prompt is in the wrong line -// This is caused by Stderr not being tracked by System.Console. -// Completion support -// Why is Thread.Interrupt not working? Currently I resort to Abort which is too much. -// -// Limitations in System.Console: -// Console needs SIGWINCH support of some sort -// Console needs a way of updating its position after things have been written -// behind its back (P/Invoke puts for example). -// System.Console needs to get the DELETE character, and report accordingly. -// - -using System; -using System.Text; -using System.IO; -using System.Threading; -using System.Reflection; - -namespace Mono.Terminal -{ - public class LineEditor - { - public class Completion - { - public string[] Result; - public string Prefix; - - public Completion(string prefix, string[] result) - { - Prefix = prefix; - Result = result; - } - } - - public delegate Completion AutoCompleteHandler(string text, int pos); - - //static StreamWriter log; - - // The text being edited. - StringBuilder text; - - // The text as it is rendered (replaces (char)1 with ^A on display for example). - StringBuilder rendered_text; - - // The prompt specified, and the prompt shown to the user. - string prompt; - string shown_prompt; - - // The current cursor position, indexes into "text", for an index - // into rendered_text, use TextToRenderPos - int cursor; - - // The row where we started displaying data. - int home_row; - - // The maximum length that has been displayed on the screen - int max_rendered; - - // If we are done editing, this breaks the interactive loop - bool done = false; - - // The thread where the Editing started taking place - Thread edit_thread; - - // Our object that tracks history - History history; - - // The contents of the kill buffer (cut/paste in Emacs parlance) - string kill_buffer = ""; - - // The string being searched for - string search; - string last_search; - - // whether we are searching (-1= reverse; 0 = no; 1 = forward) - int searching; - - // The position where we found the match. - int match_at; - - // Used to implement the Kill semantics (multiple Alt-Ds accumulate) - KeyHandler last_handler; - - delegate void KeyHandler(); - - struct Handler - { - public ConsoleKeyInfo CKI; - public KeyHandler KeyHandler; - - public Handler(ConsoleKey key, KeyHandler h) - { - CKI = new ConsoleKeyInfo((char)0, key, false, false, false); - KeyHandler = h; - } - - public Handler(char c, KeyHandler h) - { - KeyHandler = h; - // Use the "Zoom" as a flag that we only have a character. - CKI = new ConsoleKeyInfo(c, ConsoleKey.Zoom, false, false, false); - } - - public Handler(ConsoleKeyInfo cki, KeyHandler h) - { - CKI = cki; - KeyHandler = h; - } - - public static Handler Control(char c, KeyHandler h) - { - return new Handler((char)(c - 'A' + 1), h); - } - - public static Handler Alt(char c, ConsoleKey k, KeyHandler h) - { - ConsoleKeyInfo cki = new ConsoleKeyInfo((char)c, k, false, true, false); - return new Handler(cki, h); - } - } - - /// - /// Invoked when the user requests auto-completion using the tab character - /// - /// - /// The result is null for no values found, an array with a single - /// string, in that case the string should be the text to be inserted - /// for example if the word at pos is "T", the result for a completion - /// of "ToString" should be "oString", not "ToString". - /// - /// When there are multiple results, the result should be the full - /// text - /// - public AutoCompleteHandler AutoCompleteEvent; - - static Handler[] handlers; - - public LineEditor(string name) : this(name, 10) { } - - public LineEditor(string name, int histsize) - { - handlers = new Handler[] { - new Handler (ConsoleKey.Home, CmdHome), - new Handler (ConsoleKey.End, CmdEnd), - new Handler (ConsoleKey.LeftArrow, CmdLeft), - new Handler (ConsoleKey.RightArrow, CmdRight), - new Handler (ConsoleKey.UpArrow, CmdHistoryPrev), - new Handler (ConsoleKey.DownArrow, CmdHistoryNext), - new Handler (ConsoleKey.Enter, CmdDone), - new Handler (ConsoleKey.Backspace, CmdBackspace), - new Handler (ConsoleKey.Delete, CmdDeleteChar), - new Handler (ConsoleKey.Tab, CmdTabOrComplete), - - // Emacs keys - Handler.Control ('A', CmdHome), - Handler.Control ('E', CmdEnd), - Handler.Control ('B', CmdLeft), - Handler.Control ('F', CmdRight), - Handler.Control ('P', CmdHistoryPrev), - Handler.Control ('N', CmdHistoryNext), - Handler.Control ('K', CmdKillToEOF), - Handler.Control ('Y', CmdYank), - Handler.Control ('D', CmdDeleteChar), - Handler.Control ('L', CmdRefresh), - Handler.Control ('R', CmdReverseSearch), - Handler.Control ('G', delegate {} ), - Handler.Alt ('B', ConsoleKey.B, CmdBackwardWord), - Handler.Alt ('F', ConsoleKey.F, CmdForwardWord), - - Handler.Alt ('D', ConsoleKey.D, CmdDeleteWord), - Handler.Alt ((char) 8, ConsoleKey.Backspace, CmdDeleteBackword), - - // DEBUG - //Handler.Control ('T', CmdDebug), - - // quote - Handler.Control ('Q', delegate { HandleChar (Console.ReadKey (true).KeyChar); }) - }; - - rendered_text = new StringBuilder(); - text = new StringBuilder(); - - history = new History(name, histsize); - - //if (File.Exists ("log"))File.Delete ("log"); - //log = File.CreateText ("log"); - } - - void CmdDebug() - { - history.Dump(); - Console.WriteLine(); - Render(); - } - - void Render() - { - Console.Write(shown_prompt); - Console.Write(rendered_text); - - int max = System.Math.Max(rendered_text.Length + shown_prompt.Length, max_rendered); - - for (int i = rendered_text.Length + shown_prompt.Length; i < max_rendered; i++) - Console.Write(' '); - max_rendered = shown_prompt.Length + rendered_text.Length; - - // Write one more to ensure that we always wrap around properly if we are at the - // end of a line. - Console.Write(' '); - - UpdateHomeRow(max); - } - - void UpdateHomeRow(int screenpos) - { - int lines = 1 + (screenpos / Console.WindowWidth); - - home_row = Console.CursorTop - (lines - 1); - if (home_row < 0) - home_row = 0; - } - - - void RenderFrom(int pos) - { - int rpos = TextToRenderPos(pos); - int i; - - for (i = rpos; i < rendered_text.Length; i++) - Console.Write(rendered_text[i]); - - if ((shown_prompt.Length + rendered_text.Length) > max_rendered) - max_rendered = shown_prompt.Length + rendered_text.Length; - else - { - int max_extra = max_rendered - shown_prompt.Length; - for (; i < max_extra; i++) - Console.Write(' '); - } - } - - void ComputeRendered() - { - rendered_text.Length = 0; - - for (int i = 0; i < text.Length; i++) - { - int c = (int)text[i]; - if (c < 26) - { - if (c == '\t') - rendered_text.Append(" "); - else - { - rendered_text.Append('^'); - rendered_text.Append((char)(c + (int)'A' - 1)); - } - } - else - rendered_text.Append((char)c); - } - } - - int TextToRenderPos(int pos) - { - int p = 0; - - for (int i = 0; i < pos; i++) - { - int c; - - c = (int)text[i]; - - if (c < 26) - { - if (c == 9) - p += 4; - else - p += 2; - } - else - p++; - } - - return p; - } - - int TextToScreenPos(int pos) - { - return shown_prompt.Length + TextToRenderPos(pos); - } - - string Prompt - { - get => prompt; - set => prompt = value; - } - - int LineCount => (shown_prompt.Length + rendered_text.Length) / Console.WindowWidth; - - void ForceCursor(int newpos) - { - cursor = newpos; - - int actual_pos = shown_prompt.Length + TextToRenderPos(cursor); - int row = home_row + (actual_pos / Console.WindowWidth); - int col = actual_pos % Console.WindowWidth; - - if (row >= Console.BufferHeight) - row = Console.BufferHeight - 1; - Console.SetCursorPosition(col, row); - - //log.WriteLine ("Going to cursor={0} row={1} col={2} actual={3} prompt={4} ttr={5} old={6}", newpos, row, col, actual_pos, prompt.Length, TextToRenderPos (cursor), cursor); - //log.Flush (); - } - - void UpdateCursor(int newpos) - { - if (cursor == newpos) - return; - - ForceCursor(newpos); - } - - void InsertChar(char c) - { - int prev_lines = LineCount; - text = text.Insert(cursor, c); - ComputeRendered(); - if (prev_lines != LineCount) - { - - Console.SetCursorPosition(0, home_row); - Render(); - ForceCursor(++cursor); - } - else - { - RenderFrom(cursor); - ForceCursor(++cursor); - UpdateHomeRow(TextToScreenPos(cursor)); - } - } - - // - // Commands - // - void CmdDone() - { - done = true; - } - - void CmdTabOrComplete() - { - bool complete = false; - - if (AutoCompleteEvent != null) - { - if (TabAtStartCompletes) - complete = true; - else - { - for (int i = 0; i < cursor; i++) - { - if (!Char.IsWhiteSpace(text[i])) - { - complete = true; - break; - } - } - } - - if (complete) - { - Completion completion = AutoCompleteEvent(text.ToString(), cursor); - string[] completions = completion.Result; - if (completions == null) - return; - - int ncompletions = completions.Length; - if (ncompletions == 0) - return; - - if (completions.Length == 1) - { - InsertTextAtCursor(completions[0]); - } - else - { - int last = -1; - - for (int p = 0; p < completions[0].Length; p++) - { - char c = completions[0][p]; - - - for (int i = 1; i < ncompletions; i++) - { - if (completions[i].Length < p) - goto mismatch; - - if (completions[i][p] != c) - { - goto mismatch; - } - } - last = p; - } - mismatch: - if (last != -1) - { - InsertTextAtCursor(completions[0].Substring(0, last + 1)); - } - Console.WriteLine(); - foreach (string s in completions) - { - Console.Write(completion.Prefix); - Console.Write(s); - Console.Write(' '); - } - Console.WriteLine(); - Render(); - ForceCursor(cursor); - } - } - else - HandleChar('\t'); - } - else - HandleChar('t'); - } - - void CmdHome() - { - UpdateCursor(0); - } - - void CmdEnd() - { - UpdateCursor(text.Length); - } - - void CmdLeft() - { - if (cursor == 0) - return; - - UpdateCursor(cursor - 1); - } - - void CmdBackwardWord() - { - int p = WordBackward(cursor); - if (p == -1) - return; - UpdateCursor(p); - } - - void CmdForwardWord() - { - int p = WordForward(cursor); - if (p == -1) - return; - UpdateCursor(p); - } - - void CmdRight() - { - if (cursor == text.Length) - return; - - UpdateCursor(cursor + 1); - } - - void RenderAfter(int p) - { - ForceCursor(p); - RenderFrom(p); - ForceCursor(cursor); - } - - void CmdBackspace() - { - if (cursor == 0) - return; - - text.Remove(--cursor, 1); - ComputeRendered(); - RenderAfter(cursor); - } - - void CmdDeleteChar() - { - // If there is no input, this behaves like EOF - if (text.Length == 0) - { - done = true; - text = null; - Console.WriteLine(); - return; - } - - if (cursor == text.Length) - return; - text.Remove(cursor, 1); - ComputeRendered(); - RenderAfter(cursor); - } - - int WordForward(int p) - { - if (p >= text.Length) - return -1; - - int i = p; - if (Char.IsPunctuation(text[p]) || Char.IsSymbol(text[p]) || Char.IsWhiteSpace(text[p])) - { - for (; i < text.Length; i++) - { - if (Char.IsLetterOrDigit(text[i])) - break; - } - for (; i < text.Length; i++) - { - if (!Char.IsLetterOrDigit(text[i])) - break; - } - } - else - { - for (; i < text.Length; i++) - { - if (!Char.IsLetterOrDigit(text[i])) - break; - } - } - if (i != p) - return i; - return -1; - } - - int WordBackward(int p) - { - if (p == 0) - return -1; - - int i = p - 1; - if (i == 0) - return 0; - - if (Char.IsPunctuation(text[i]) || Char.IsSymbol(text[i]) || Char.IsWhiteSpace(text[i])) - { - for (; i >= 0; i--) - { - if (Char.IsLetterOrDigit(text[i])) - break; - } - for (; i >= 0; i--) - { - if (!Char.IsLetterOrDigit(text[i])) - break; - } - } - else - { - for (; i >= 0; i--) - { - if (!Char.IsLetterOrDigit(text[i])) - break; - } - } - i++; - - if (i != p) - return i; - - return -1; - } - - void CmdDeleteWord() - { - int pos = WordForward(cursor); - - if (pos == -1) - return; - - string k = text.ToString(cursor, pos - cursor); - - if (last_handler == CmdDeleteWord) - kill_buffer = kill_buffer + k; - else - kill_buffer = k; - - text.Remove(cursor, pos - cursor); - ComputeRendered(); - RenderAfter(cursor); - } - - void CmdDeleteBackword() - { - int pos = WordBackward(cursor); - if (pos == -1) - return; - - string k = text.ToString(pos, cursor - pos); - - if (last_handler == CmdDeleteBackword) - kill_buffer = k + kill_buffer; - else - kill_buffer = k; - - text.Remove(pos, cursor - pos); - ComputeRendered(); - RenderAfter(pos); - } - - // - // Adds the current line to the history if needed - // - void HistoryUpdateLine() - { - history.Update(text.ToString()); - } - - void CmdHistoryPrev() - { - if (!history.PreviousAvailable()) - return; - - HistoryUpdateLine(); - - SetText(history.Previous()); - } - - void CmdHistoryNext() - { - if (!history.NextAvailable()) - return; - - history.Update(text.ToString()); - SetText(history.Next()); - - } - - void CmdKillToEOF() - { - kill_buffer = text.ToString(cursor, text.Length - cursor); - text.Length = cursor; - ComputeRendered(); - RenderAfter(cursor); - } - - void CmdYank() - { - InsertTextAtCursor(kill_buffer); - } - - void InsertTextAtCursor(string str) - { - int prev_lines = LineCount; - text.Insert(cursor, str); - ComputeRendered(); - if (prev_lines != LineCount) - { - Console.SetCursorPosition(0, home_row); - Render(); - cursor += str.Length; - ForceCursor(cursor); - } - else - { - RenderFrom(cursor); - cursor += str.Length; - ForceCursor(cursor); - UpdateHomeRow(TextToScreenPos(cursor)); - } - } - - void SetSearchPrompt(string s) - { - SetPrompt("(reverse-i-search)`" + s + "': "); - } - - void ReverseSearch() - { - int p; - - if (cursor == text.Length) - { - // The cursor is at the end of the string - - p = text.ToString().LastIndexOf(search); - if (p != -1) - { - match_at = p; - cursor = p; - ForceCursor(cursor); - return; - } - } - else - { - // The cursor is somewhere in the middle of the string - int start = (cursor == match_at) ? cursor - 1 : cursor; - if (start != -1) - { - p = text.ToString().LastIndexOf(search, start); - if (p != -1) - { - match_at = p; - cursor = p; - ForceCursor(cursor); - return; - } - } - } - - // Need to search backwards in history - HistoryUpdateLine(); - string s = history.SearchBackward(search); - if (s != null) - { - match_at = -1; - SetText(s); - ReverseSearch(); - } - } - - void CmdReverseSearch() - { - if (searching == 0) - { - match_at = -1; - last_search = search; - searching = -1; - search = ""; - SetSearchPrompt(""); - } - else - { - if (search == "") - { - if (last_search != "" && last_search != null) - { - search = last_search; - SetSearchPrompt(search); - - ReverseSearch(); - } - return; - } - ReverseSearch(); - } - } - - void SearchAppend(char c) - { - search = search + c; - SetSearchPrompt(search); - - // - // If the new typed data still matches the current text, stay here - // - if (cursor < text.Length) - { - string r = text.ToString(cursor, text.Length - cursor); - if (r.StartsWith(search)) - return; - } - - ReverseSearch(); - } - - void CmdRefresh() - { - Console.Clear(); - max_rendered = 0; - Render(); - ForceCursor(cursor); - } - - void InterruptEdit(object sender, ConsoleCancelEventArgs a) - { - // Do not abort our program: - a.Cancel = false; - - // Interrupt the editor - edit_thread.Abort(); - } - - void HandleChar(char c) - { - if (searching != 0) - SearchAppend(c); - else - InsertChar(c); - } - - void EditLoop() - { - ConsoleKeyInfo cki; - - while (!done) - { - ConsoleModifiers mod; - - cki = Console.ReadKey(true); - if (cki.Key == ConsoleKey.Escape) - { - cki = Console.ReadKey(true); - - mod = ConsoleModifiers.Alt; - } - else - mod = cki.Modifiers; - - bool handled = false; - - foreach (Handler handler in handlers) - { - ConsoleKeyInfo t = handler.CKI; - - if (t.Key == cki.Key && t.Modifiers == mod) - { - handled = true; - handler.KeyHandler(); - last_handler = handler.KeyHandler; - break; - } - else if (t.KeyChar == cki.KeyChar && t.Key == ConsoleKey.Zoom) - { - handled = true; - handler.KeyHandler(); - last_handler = handler.KeyHandler; - break; - } - } - if (handled) - { - if (searching != 0) - { - if (last_handler != CmdReverseSearch) - { - searching = 0; - SetPrompt(prompt); - } - } - continue; - } - - if (cki.KeyChar != (char)0) - HandleChar(cki.KeyChar); - } - } - - void InitText(string initial) - { - text = new StringBuilder(initial); - ComputeRendered(); - cursor = text.Length; - Render(); - ForceCursor(cursor); - } - - void SetText(string newtext) - { - Console.SetCursorPosition(0, home_row); - InitText(newtext); - } - - void SetPrompt(string newprompt) - { - shown_prompt = newprompt; - Console.SetCursorPosition(0, home_row); - Render(); - ForceCursor(cursor); - } - - public string Edit(string prompt, string initial) - { - edit_thread = Thread.CurrentThread; - searching = 0; - Console.CancelKeyPress += InterruptEdit; - - done = false; - history.CursorToEnd(); - max_rendered = 0; - - Prompt = prompt; - shown_prompt = prompt; - InitText(initial); - history.Append(initial); - - do - { - try - { - EditLoop(); - } - catch (ThreadAbortException) - { - searching = 0; - Thread.ResetAbort(); - Console.WriteLine(); - SetPrompt(prompt); - SetText(""); - } - } while (!done); - Console.WriteLine(); - - Console.CancelKeyPress -= InterruptEdit; - - if (text == null) - { - history.Close(); - return null; - } - - string result = text.ToString(); - if (result != "") - history.Accept(result); - else - history.RemoveLast(); - - return result; - } - - public void SaveHistory() - { - if (history != null) - { - history.Close(); - } - } - - public bool TabAtStartCompletes { get; set; } - - // - // Emulates the bash-like behavior, where edits done to the - // history are recorded - // - class History - { - string[] history; - int head, tail; - int cursor, count; - string histfile; - - public History(string app, int size) - { - if (size < 1) - throw new ArgumentException("size"); - - if (app != null) - { - string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); - //Console.WriteLine (dir); - if (!Directory.Exists(dir)) - { - try - { - Directory.CreateDirectory(dir); - } - catch - { - app = null; - } - } - if (app != null) - histfile = Path.Combine(dir, app) + ".history"; - } - - history = new string[size]; - head = tail = cursor = 0; - - if (File.Exists(histfile)) - { - using (StreamReader sr = File.OpenText(histfile)) - { - string line; - - while ((line = sr.ReadLine()) != null) - { - if (line != "") - Append(line); - } - } - } - } - - public void Close() - { - if (histfile == null) - return; - - try - { - using (StreamWriter sw = File.CreateText(histfile)) - { - int start = (count == history.Length) ? head : tail; - for (int i = start; i < start + count; i++) - { - int p = i % history.Length; - sw.WriteLine(history[p]); - } - } - } - catch - { - // ignore - } - } - - // - // Appends a value to the history - // - public void Append(string s) - { - //Console.WriteLine ("APPENDING {0} head={1} tail={2}", s, head, tail); - history[head] = s; - head = (head + 1) % history.Length; - if (head == tail) - tail = (tail + 1 % history.Length); - if (count != history.Length) - count++; - //Console.WriteLine ("DONE: head={1} tail={2}", s, head, tail); - } - - // - // Updates the current cursor location with the string, - // to support editing of history items. For the current - // line to participate, an Append must be done before. - // - public void Update(string s) - { - history[cursor] = s; - } - - public void RemoveLast() - { - head = head - 1; - if (head < 0) - head = history.Length - 1; - } - - public void Accept(string s) - { - int t = head - 1; - if (t < 0) - t = history.Length - 1; - - history[t] = s; - } - - public bool PreviousAvailable() - { - //Console.WriteLine ("h={0} t={1} cursor={2}", head, tail, cursor); - if (count == 0) - return false; - int next = cursor - 1; - if (next < 0) - next = count - 1; - - if (next == head) - return false; - - return true; - } - - public bool NextAvailable() - { - if (count == 0) - return false; - int next = (cursor + 1) % history.Length; - if (next == head) - return false; - return true; - } - - - // - // Returns: a string with the previous line contents, or - // nul if there is no data in the history to move to. - // - public string Previous() - { - if (!PreviousAvailable()) - return null; - - cursor--; - if (cursor < 0) - cursor = history.Length - 1; - - return history[cursor]; - } - - public string Next() - { - if (!NextAvailable()) - return null; - - cursor = (cursor + 1) % history.Length; - return history[cursor]; - } - - public void CursorToEnd() - { - if (head == tail) - return; - - cursor = head; - } - - public void Dump() - { - Console.WriteLine("Head={0} Tail={1} Cursor={2} count={3}", head, tail, cursor, count); - for (int i = 0; i < history.Length; i++) - { - Console.WriteLine(" {0} {1}: {2}", i == cursor ? "==>" : " ", i, history[i]); - } - //log.Flush (); - } - - public string SearchBackward(string term) - { - for (int i = 0; i < count; i++) - { - int slot = cursor - i - 1; - if (slot < 0) - slot = history.Length + slot; - if (slot >= history.Length) - slot = 0; - if (history[slot] != null && history[slot].IndexOf(term) != -1) - { - cursor = slot; - return history[slot]; - } - } - - return null; - } - - } - } - -#if DEMO - class Demo { - static void Main () - { - LineEditor le = new LineEditor ("foo"); - string s; - - while ((s = le.Edit ("shell> ", "")) != null){ - Console.WriteLine ("----> [{0}]", s); - } - } - } -#endif -} diff --git a/src/ScriptCs.Hosting/ModuleConfiguration.cs b/src/ScriptCs.Hosting/ModuleConfiguration.cs deleted file mode 100644 index b2cd4302..00000000 --- a/src/ScriptCs.Hosting/ModuleConfiguration.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class ModuleConfiguration : ServiceOverrides, IModuleConfiguration - { - public ModuleConfiguration( - bool cache, - string scriptName, - bool isRepl, - LogLevel logLevel, - bool debug, - IDictionary overrides) - : base(overrides) - { - Cache = cache; - ScriptName = scriptName; - IsRepl = isRepl; - LogLevel = logLevel; - Debug = debug; - } - - public bool Cache { get; private set; } - - public string ScriptName { get; private set; } - - public bool IsRepl { get; private set; } - - public LogLevel LogLevel { get; private set; } - - public bool Debug { get; private set; } - } -} diff --git a/src/ScriptCs.Hosting/ModuleLoader.cs b/src/ScriptCs.Hosting/ModuleLoader.cs deleted file mode 100644 index 0e402b37..00000000 --- a/src/ScriptCs.Hosting/ModuleLoader.cs +++ /dev/null @@ -1,209 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.ComponentModel.Composition.Hosting; -using System.IO; -using System.Linq; -using System.Reflection; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class ModuleLoader : IModuleLoader - { - internal static readonly Dictionary DefaultCSharpModules = new Dictionary - { - {"roslyn", "ScriptCs.Engine.Roslyn.dll"} - }; - - internal static readonly string DefaultCSharpExtension = ".csx"; - - private readonly IAssemblyResolver _resolver; - private readonly ILog _logger; - private readonly Action _addToCatalog; - private readonly Func>> _getLazyModules; - private readonly IFileSystem _fileSystem; - private readonly IAssemblyUtility _assemblyUtility; - - [ImportingConstructor] - public ModuleLoader(IAssemblyResolver resolver, ILogProvider logProvider, IFileSystem fileSystem, IAssemblyUtility assemblyUtility) : - this(resolver, logProvider, null, null, fileSystem, assemblyUtility) - { - } - - public ModuleLoader(IAssemblyResolver resolver, ILogProvider logProvider, Action addToCatalog, Func>> getLazyModules, IFileSystem fileSystem, IAssemblyUtility assemblyUtility) - { - Guard.AgainstNullArgument("logProvider", logProvider); - - _resolver = resolver; - _logger = logProvider.ForCurrentType(); - - if (addToCatalog == null) - { - addToCatalog = (assembly, catalog) => - { - try - { - var assemblyCatalog = new AssemblyCatalog(assembly); - catalog.Catalogs.Add(assemblyCatalog); - } - catch (Exception exception) - { - _logger.DebugFormat("Module Loader exception: {0}", exception.Message); - } - }; - } - - _addToCatalog = addToCatalog; - - if (getLazyModules == null) - { - getLazyModules = container => container.GetExports(); - } - - _getLazyModules = getLazyModules; - _fileSystem = fileSystem; - _assemblyUtility = assemblyUtility; - } - - public void Load(IModuleConfiguration config, string[] modulePackagesPaths, string hostBin, string extension, - params string[] moduleNames) - { - Guard.AgainstNullArgument("moduleNames", moduleNames); - - if (modulePackagesPaths == null) return; - - // only CSharp module needed - use fast path - if (moduleNames.Length == 1 && DefaultCSharpModules.ContainsKey(moduleNames[0]) && (string.IsNullOrWhiteSpace(extension) || extension.Equals(DefaultCSharpExtension, StringComparison.InvariantCultureIgnoreCase))) - { - _logger.Debug("Only CSharp module is needed - will skip module lookup"); - var csharpModuleAssembly = DefaultCSharpModules[moduleNames[0]]; - var module = _assemblyUtility.LoadFile(Path.Combine(hostBin, csharpModuleAssembly)); - - if (module != null) - { - var moduleType = module.GetExportedTypes().FirstOrDefault(f => typeof (IModule).IsAssignableFrom(f)); - - if (moduleType != null) - { - var moduleInstance = Activator.CreateInstance(moduleType) as IModule; - - if (moduleInstance != null) - { - _logger.Debug(String.Format("Initializing module: {0}", module.GetType().FullName)); - moduleInstance.Initialize(config); - return; - } - } - } - } - - _logger.Debug("Loading modules from: " + String.Join(", ", modulePackagesPaths.Select(i => i))); - var paths = new List(); - - AddPaths(modulePackagesPaths, hostBin, paths); - - var catalog = CreateAggregateCatalog(paths); - var container = new CompositionContainer(catalog); - - var lazyModules = GetLazyModules(container); - InitializeModules(config, extension, moduleNames, lazyModules); - } - - private void InitializeModules( - IModuleConfiguration config, - string extension, - IEnumerable moduleNames, - IEnumerable> lazyModules) - { - var modules = lazyModules - .Where(m => moduleNames.Contains(m.Metadata.Name) || - (extension != null && m.Metadata.Extensions != null && - (m.Metadata.Extensions.Split(',').Contains(extension))) || - m.Metadata.Autoload) - .Select(m => m.Value); - - _logger.Debug("Initializing modules"); - - foreach (var module in modules) - { - _logger.Debug(String.Format("Initializing module: {0}", module.GetType().FullName)); - module.Initialize(config); - } - - _logger.Debug("Modules initialized"); - } - - private AggregateCatalog CreateAggregateCatalog(IEnumerable paths) - { - var catalog = new AggregateCatalog(); - foreach (var path in paths) - { - _logger.DebugFormat("Found assembly: {0}", path); - - try - { - if (_assemblyUtility.IsManagedAssembly(path)) - { - _logger.DebugFormat("Adding Assembly: {0} to catalog", path); - var name = _assemblyUtility.GetAssemblyName(path); - var assembly = _assemblyUtility.Load(name); - _addToCatalog(assembly, catalog); - } - else - { - _logger.DebugFormat("Skipping Adding Native Assembly {0} to catalog", path); - } - } - catch (Exception exception) - { - _logger.DebugFormat("Module Loader exception: {0}", exception.Message); - } - } - return catalog; - } - - private void AddPaths(IEnumerable modulePackagesPaths, string hostBin, List paths) - { - foreach (var modulePaths in modulePackagesPaths - .Select(modulePackagesPath => _resolver.GetAssemblyPaths(modulePackagesPath, true))) - { - paths.AddRange(modulePaths); - } - - if (hostBin != null) - { - var assemblyPaths = _fileSystem.EnumerateBinaries(hostBin, SearchOption.TopDirectoryOnly); - paths.AddRange(assemblyPaths); - } - } - - private IEnumerable> GetLazyModules(CompositionContainer container) - { - IEnumerable> lazyModules; - - try - { - lazyModules = _getLazyModules(container); - } - catch (ReflectionTypeLoadException exception) - { - if (exception.LoaderExceptions != null && exception.LoaderExceptions.Any()) - { - foreach (var loaderException in exception.LoaderExceptions) - { - _logger.DebugFormat("Module Loader exception: {0}", loaderException.Message); - } - } - else - { - _logger.DebugFormat("Module Loader exception: {0}", exception.Message); - } - - lazyModules = Enumerable.Empty>(); - } - - return lazyModules; - } - } -} diff --git a/src/ScriptCs.Hosting/ObjectSerializer.cs b/src/ScriptCs.Hosting/ObjectSerializer.cs deleted file mode 100644 index bc342795..00000000 --- a/src/ScriptCs.Hosting/ObjectSerializer.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System.Linq; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class ObjectSerializer : IObjectSerializer - { - private static readonly JsonSerializerSettings settings = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - PreserveReferencesHandling = PreserveReferencesHandling.Objects, - ReferenceLoopHandling = ReferenceLoopHandling.Serialize, - }; - - public string Serialize(object value) - { - try - { - var writer = new JTokenWriter(); - var serializer = JsonSerializer.Create(settings); - serializer.Serialize(writer, value); - - var container = writer.Token as JContainer; - if (container != null) - { - var idProperties = container.Descendants().OfType().Where(d => d.Name == "$id").ToList(); - if (idProperties.Any()) - { - var refProperties = container.Descendants().OfType().Where(d => d.Name == "$ref").ToList(); - if (refProperties.Any()) - { - foreach (var idProperty in idProperties - .Where(idProperty => refProperties - .All(refProperty => refProperty.Value.ToString() != idProperty.Value.ToString()))) - { - idProperty.Remove(); - } - } - else - { - foreach (var idProperty in idProperties) - { - idProperty.Remove(); - } - } - } - } - - return writer.Token.ToString(); - } - catch (JsonSerializationException) - { - return string.Format("Couldn't serialize a returned instance of {0}", value.GetType()); - } - } - } -} diff --git a/src/ScriptCs.Hosting/Package/NugetInstallationProvider.cs b/src/ScriptCs.Hosting/Package/NugetInstallationProvider.cs deleted file mode 100644 index e4eddb98..00000000 --- a/src/ScriptCs.Hosting/Package/NugetInstallationProvider.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using NuGet; -using ScriptCs.Contracts; -using IFileSystem = ScriptCs.Contracts.IFileSystem; - -namespace ScriptCs.Hosting.Package -{ - public class NugetInstallationProvider : IInstallationProvider - { - private readonly IFileSystem _fileSystem; - private readonly ILog _logger; - private PackageManager _manager; - private IEnumerable _repositoryUrls; - - private static readonly Version EmptyVersion = new Version(); - - public NugetInstallationProvider(IFileSystem fileSystem, ILogProvider logProvider) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("logProvider", logProvider); - - _fileSystem = fileSystem; - _logger = logProvider.ForCurrentType(); - } - - public void Initialize() - { - var path = Path.Combine(_fileSystem.CurrentDirectory, _fileSystem.PackagesFolder); - _repositoryUrls = GetRepositorySources(path); - var remoteRepository = new AggregateRepository(PackageRepositoryFactory.Default, _repositoryUrls, true); - _manager = new ScriptCsPackageManager(remoteRepository, path); - } - - public IEnumerable GetRepositorySources(string path) - { - var configFileSystem = new PhysicalFileSystem(path); - - ISettings settings; - var localNuGetConfigFile = Path.Combine(_fileSystem.CurrentDirectory, _fileSystem.NugetFile); - if (_fileSystem.FileExists(localNuGetConfigFile)) - { - settings = Settings.LoadDefaultSettings(configFileSystem, localNuGetConfigFile, null); - } - else - { - settings = Settings.LoadDefaultSettings(configFileSystem, null, new NugetMachineWideSettings()); - } - - if (settings == null) - { - return new[] { Constants.DefaultRepositoryUrl }; - } - - var sourceProvider = new PackageSourceProvider(settings); - - HttpClient.DefaultCredentialProvider = new SettingsCredentialProvider(NullCredentialProvider.Instance, sourceProvider); - - var sources = sourceProvider.LoadPackageSources().Where(i => i.IsEnabled == true); - - if (sources == null || !sources.Any()) - { - return new[] { Constants.DefaultRepositoryUrl }; - } - - return sources.Select(i => i.Source); - } - - public void InstallPackage(IPackageReference packageId, bool allowPreRelease = false) - { - Guard.AgainstNullArgument("packageId", packageId); - - var version = GetVersion(packageId); - var packageName = packageId.PackageId + " " + (version == null ? string.Empty : packageId.Version.ToString()); - - _manager.InstallPackage(packageId.PackageId, version, allowPrereleaseVersions: allowPreRelease, ignoreDependencies: false); - _logger.Info("Installed: " + packageName); - } - - private static SemanticVersion GetVersion(IPackageReference packageReference) - { - return packageReference.Version == EmptyVersion ? null : new SemanticVersion(packageReference.Version, packageReference.SpecialVersion); - } - - public bool IsInstalled(IPackageReference packageReference, bool allowPreRelease = false) - { - Guard.AgainstNullArgument("packageReference", packageReference); - - var version = GetVersion(packageReference); - return _manager.LocalRepository.FindPackage(packageReference.PackageId, version, allowPreRelease, allowUnlisted: false) != null; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/Package/NugetMachineWideSettings.cs b/src/ScriptCs.Hosting/Package/NugetMachineWideSettings.cs deleted file mode 100644 index ffd6d099..00000000 --- a/src/ScriptCs.Hosting/Package/NugetMachineWideSettings.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using NuGet; - -namespace ScriptCs.Hosting.Package -{ - internal class NugetMachineWideSettings : IMachineWideSettings - { - private readonly Lazy> _settings; - - public NugetMachineWideSettings() - { - var baseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); - _settings = new Lazy>(() => NuGet.Settings.LoadMachineWideSettings(new PhysicalFileSystem(baseDirectory))); - } - - public IEnumerable Settings => _settings.Value; - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/Package/PackageContainer.cs b/src/ScriptCs.Hosting/Package/PackageContainer.cs deleted file mode 100644 index 1dc7c8c8..00000000 --- a/src/ScriptCs.Hosting/Package/PackageContainer.cs +++ /dev/null @@ -1,156 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.Versioning; -using NuGet; -using ScriptCs.Contracts; -using IFileSystem = ScriptCs.Contracts.IFileSystem; - -namespace ScriptCs.Hosting.Package -{ - public class PackageContainer : IPackageContainer - { - private const string DotNetFramework = ".NETFramework"; - - private const string DotNetPortable = ".NETPortable"; - - private readonly IFileSystem _fileSystem; - - private readonly ILog _logger; - - public PackageContainer(IFileSystem fileSystem, ILogProvider logProvider) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("logProvider", logProvider); - - _fileSystem = fileSystem; - _logger = logProvider.ForCurrentType(); - } - - public void CreatePackageFile() - { - var packagesFile = Path.Combine(_fileSystem.CurrentDirectory, _fileSystem.PackagesFile); - var packageReferenceFile = new PackageReferenceFile(packagesFile); - - var packagesFolder = Path.Combine(_fileSystem.CurrentDirectory, _fileSystem.PackagesFolder); - var repository = new LocalPackageRepository(packagesFolder); - - var newestPackages = repository.GetPackages().GroupBy(p => p.Id) - .Select(g => g.OrderByDescending(p => p.Version).FirstOrDefault()); - - if (!newestPackages.Any()) - { - _logger.Info("No packages found!"); - return; - } - - _logger.InfoFormat("{0} {1}...", (File.Exists(packagesFile) ? "Updating" : "Creating"), _fileSystem.PackagesFile); - - foreach (var package in newestPackages) - { - var newestFramework = GetNewestSupportedFramework(package); - - if (!packageReferenceFile.EntryExists(package.Id, package.Version)) - { - packageReferenceFile.AddEntry(package.Id, package.Version, package.DevelopmentDependency, newestFramework); - - if (newestFramework == null) - { - _logger.InfoFormat("Added {0} (v{1}) to {2}", package.Id, package.Version, _fileSystem.PackagesFile); - } - else - { - _logger.InfoFormat("Added {0} (v{1}, .NET {2}) to {3}", package.Id, package.Version, newestFramework.Version, _fileSystem.PackagesFile); - } - - continue; - } - - _logger.InfoFormat("Skipped {0} because it already exists.", package.Id); - } - - _logger.InfoFormat("Successfully {0} {1}.", (File.Exists(packagesFile) ? "updated" : "created"), _fileSystem.PackagesFile); - } - - public IPackageObject FindPackage(string path, IPackageReference packageRef) - { - Guard.AgainstNullArgument("packageRef", packageRef); - - var repository = new LocalPackageRepository(path); - - var package = packageRef.Version != null && !(packageRef.Version.Major == 0 && packageRef.Version.Minor == 0) - ? repository.FindPackage(packageRef.PackageId, new SemanticVersion(packageRef.Version, packageRef.SpecialVersion), true, true) - : repository.FindPackage(packageRef.PackageId); - - return package == null ? null : new PackageObject(package, packageRef.FrameworkName); - } - - public IEnumerable FindReferences(string path) - { - var packageReferenceFile = new PackageReferenceFile(path); - - var references = packageReferenceFile.GetPackageReferences().ToList(); - if (references.Any()) - { - foreach (var packageReference in references) - { - yield return new PackageReference( - packageReference.Id, - packageReference.TargetFramework, - packageReference.Version.Version, - packageReference.Version.SpecialVersion); - } - - yield break; - } - - // No packages.config, check packages folder - var packagesFolder = Path.Combine(_fileSystem.GetWorkingDirectory(path), _fileSystem.PackagesFolder); - if (!_fileSystem.DirectoryExists(packagesFolder)) - { - yield break; - } - - var repository = new LocalPackageRepository(packagesFolder); - - var arbitraryPackages = repository.GetPackages(); - if (!arbitraryPackages.Any()) - { - yield break; - } - - foreach (var arbitraryPackage in arbitraryPackages) - { - var newestFramework = GetNewestSupportedFramework(arbitraryPackage) - ?? VersionUtility.EmptyFramework; - - yield return new PackageReference( - arbitraryPackage.Id, - newestFramework, - arbitraryPackage.Version.Version, - arbitraryPackage.Version.SpecialVersion); - } - } - - private static FrameworkName GetNewestSupportedFramework(IPackage packageMetadata) - { - return packageMetadata.GetSupportedFrameworks() - .Where(IsValidFramework) - .OrderByDescending(x => x.Version) - .FirstOrDefault(); - } - - private static bool IsValidFramework(FrameworkName frameworkName) - { - return frameworkName.Identifier == DotNetFramework - || (frameworkName.Identifier == DotNetPortable - && frameworkName.Profile.Split('+').Any(IsValidProfile)); - } - - private static bool IsValidProfile(string profile) - { - return profile == "net40" || profile == "net45" || profile == "net461" || profile == "netstandard20"; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/Package/PackageInstaller.cs b/src/ScriptCs.Hosting/Package/PackageInstaller.cs deleted file mode 100644 index 45c20d31..00000000 --- a/src/ScriptCs.Hosting/Package/PackageInstaller.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting.Package -{ - public class PackageInstaller : IPackageInstaller - { - private readonly IInstallationProvider _installer; - private readonly ILog _logger; - - public PackageInstaller(IInstallationProvider installer, ILogProvider logProvider) - { - Guard.AgainstNullArgument("installer", installer); - Guard.AgainstNullArgument("logProvider", logProvider); - - _installer = installer; - _logger = logProvider.ForCurrentType(); - } - - public void InstallPackages(IEnumerable packageIds, bool allowPreRelease = false) - { - Guard.AgainstNullArgument("packageIds", packageIds); - - packageIds = packageIds.Where(packageId => !_installer.IsInstalled(packageId, allowPreRelease)).ToList(); - - if (!packageIds.Any()) - { - _logger.Info("Nothing to install."); - return; - } - - var exceptions = new List(); - foreach (var packageId in packageIds) - { - try - { - _installer.InstallPackage(packageId, allowPreRelease); - } - catch (Exception ex) - { - _logger.ErrorException("Error installing package.", ex); - exceptions.Add(ex); - } - } - - if (exceptions.Any()) - { - throw new AggregateException(exceptions); - } - } - } -} diff --git a/src/ScriptCs.Hosting/Package/PackageObject.cs b/src/ScriptCs.Hosting/Package/PackageObject.cs deleted file mode 100644 index e997d3f2..00000000 --- a/src/ScriptCs.Hosting/Package/PackageObject.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.Versioning; -using System.Text.RegularExpressions; -using NuGet; - -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting.Package -{ - internal class PackageObject : IPackageObject - { - private const string Dll = ".dll"; - private const string Exe = ".exe"; - private readonly IPackage _package; - - public PackageObject(IPackage package, FrameworkName frameworkName) - { - _package = package; - FrameworkName = frameworkName; - Id = package.Id; - Version = package.Version.Version; - TextVersion = package.Version.ToString(); - FrameworkAssemblies = package.FrameworkAssemblies - .Where(x => x.SupportedFrameworks.Any(y => y == frameworkName)) - .Select(x => x.AssemblyName); - - var dependencies = _package.GetCompatiblePackageDependencies(frameworkName); - if (dependencies != null) - { - Dependencies = dependencies.Select(i => new PackageObject(i.Id) { FrameworkName = frameworkName }); - } - } - - public PackageObject(string packageId) - { - Id = packageId; - } - - public IEnumerable FrameworkAssemblies { get; private set; } - - public string Id { get; private set; } - - public string TextVersion { get; private set; } - - public Version Version { get; private set; } - - public FrameworkName FrameworkName { get; private set; } - - public IEnumerable Dependencies { get; set; } - - public string FullName => Id + "." + TextVersion; - - public IEnumerable GetCompatibleDlls(FrameworkName frameworkName) - { - var dlls = _package.GetLibFiles().Where(i => i.EffectivePath.EndsWith(Dll) || i.EffectivePath.EndsWith(Exe)); - IEnumerable compatibleFiles; - VersionUtility.TryGetCompatibleItems(frameworkName, dlls, out compatibleFiles); - - // HACK: Delete unnecessary temporary NuGet files - List tempDirectories = new List(); - foreach (PhysicalPackageFile file in dlls) - { - var match = Regex.Match(((PhysicalPackageFile)dlls.First()).SourcePath, "(" + Path.Combine(Path.GetTempPath(), "nuget").Replace("\\", "\\\\") + "\\\\[^\\\\]+?)\\\\"); - if (match.Success && match.Groups.Count > 1) - { - tempDirectories.Add(match.Groups[1].Value); - } - - } - - foreach (string directory in tempDirectories.Distinct()) - { - if (Directory.Exists(directory)) - { - Directory.Delete(directory, true); - } - } - - // /HACK - - return compatibleFiles != null ? compatibleFiles.Select(i => i.Path) : null; - } - - public IEnumerable GetContentFiles() - { - foreach (var file in _package.GetContentFiles()) - { - yield return file.Path; - } - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/Package/ScriptCsPackageManager.cs b/src/ScriptCs.Hosting/Package/ScriptCsPackageManager.cs deleted file mode 100644 index d060c5d6..00000000 --- a/src/ScriptCs.Hosting/Package/ScriptCsPackageManager.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.Linq; -using System.Reflection; -using System.Runtime.Versioning; -using NuGet; - -namespace ScriptCs.Hosting.Package -{ - //needed to allow forcing framework version for installation as the InstallPackage method that accepts version is protected and all other overloads force version to null! - public class ScriptCsPackageManager : PackageManager - { - public ScriptCsPackageManager(IPackageRepository sourceRepository, string path) : base(sourceRepository, path) - { - } - - public override void InstallPackage(IPackage package, bool ignoreDependencies, bool allowPrereleaseVersions) - { - base.InstallPackage(package, new FrameworkName(FrameworkUtils.FrameworkName), ignoreDependencies, allowPrereleaseVersions); - } - - public override void InstallPackage(string packageId, SemanticVersion version, bool ignoreDependencies, bool allowPrereleaseVersions) - { - var package = PackageRepositoryHelper.ResolvePackage(SourceRepository, LocalRepository, packageId, version, allowPrereleaseVersions); - base.InstallPackage(package, new FrameworkName(FrameworkUtils.FrameworkName), ignoreDependencies, allowPrereleaseVersions); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/Properties/AssemblyInfo.cs b/src/ScriptCs.Hosting/Properties/AssemblyInfo.cs deleted file mode 100644 index ab73017e..00000000 --- a/src/ScriptCs.Hosting/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,4 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("ScriptCs.Hosting.Tests")] \ No newline at end of file diff --git a/src/ScriptCs.Hosting/Properties/ScriptCs.Hosting.nuspec b/src/ScriptCs.Hosting/Properties/ScriptCs.Hosting.nuspec deleted file mode 100644 index 3c9437f1..00000000 --- a/src/ScriptCs.Hosting/Properties/ScriptCs.Hosting.nuspec +++ /dev/null @@ -1,17 +0,0 @@ - - - - ScriptCs.Hosting - $version$ - Glenn Block, Filip Wojcieszyn, Justin Rusbatch, Kristian Hellang, Damian Schenkelman, Adam Ralph - Glenn Block, Justin Rusbatch, Filip Wojcieszyn - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Hosting provides common services necessary for hosting scriptcs in your application. - roslyn csx script scriptcs - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ReplCommands/OpenVSCommand.cs b/src/ScriptCs.Hosting/ReplCommands/OpenVSCommand.cs deleted file mode 100644 index 17fe4193..00000000 --- a/src/ScriptCs.Hosting/ReplCommands/OpenVSCommand.cs +++ /dev/null @@ -1,43 +0,0 @@ -using ScriptCs.Contracts; -using System; - -namespace ScriptCs.Hosting.ReplCommands -{ - public class OpenVsCommand : IReplCommand - { - private readonly IConsole _console; - private IVisualStudioSolutionWriter _writer; - - public OpenVsCommand(IConsole console, IVisualStudioSolutionWriter writer) - { - _console = console; - _writer = writer; - } - - public object Execute(IRepl repl, object[] args) - { - if (PlatformID != PlatformID.Win32NT) - { - _console.WriteLine("Requires Windows 8 or later to run"); - return null; - } - var fs = repl.FileSystem; - string arg = args.Length > 0 ? (string)args[0] : null; - var launcher = _writer.WriteSolution(fs, arg, new VisualStudioSolution()); - _console.WriteLine("Opening Visual Studio"); - LaunchSolution(launcher); - return null; - } - - protected internal virtual void LaunchSolution(string launcher) - { - System.Diagnostics.Process.Start(launcher); - } - - protected internal virtual PlatformID PlatformID => Environment.OSVersion.Platform; - - public string Description => "Opens a script to edit/debug in Visual Studio"; - - public string CommandName => "openvs"; - } -} diff --git a/src/ScriptCs.Hosting/RuntimeServices.cs b/src/ScriptCs.Hosting/RuntimeServices.cs deleted file mode 100644 index 9a07390c..00000000 --- a/src/ScriptCs.Hosting/RuntimeServices.cs +++ /dev/null @@ -1,185 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition.Hosting; -using System.Linq; -using System.Reflection; -using Autofac; -using Autofac.Integration.Mef; -using ScriptCs.Contracts; -using ScriptCs.Hosting.Package; - -namespace ScriptCs.Hosting -{ - public class RuntimeServices : ScriptServicesRegistration, IRuntimeServices - { - private readonly ILog _log; - private readonly IConsole _console; - private readonly Type _scriptEngineType; - private readonly Type _scriptExecutorType; - private readonly Type _replType; - private readonly bool _initDirectoryCatalog; - private readonly IInitializationServices _initializationServices; - private readonly string _scriptName; - - public RuntimeServices( - ILogProvider logProvider, - IDictionary overrides, - IConsole console, - Type scriptEngineType, - Type scriptExecutorType, - Type replType, - bool initDirectoryCatalog, - IInitializationServices initializationServices, - string scriptName) - : base(logProvider, overrides) - { - Guard.AgainstNullArgument("logProvider", logProvider); - - _log = logProvider.ForCurrentType(); - _console = console; - _scriptEngineType = scriptEngineType; - _scriptExecutorType = scriptExecutorType; - _replType = replType; - _initDirectoryCatalog = initDirectoryCatalog; - _initializationServices = initializationServices; - _scriptName = scriptName; - } - - internal bool InitDirectoryCatalog => _initDirectoryCatalog; - - protected override IContainer CreateContainer() - { - var builder = new ContainerBuilder(); - _log.Debug("Registering runtime services"); - - builder.RegisterInstance(this.LogProvider).Exported(x => x.As()); - builder.RegisterType(_scriptEngineType).As().SingleInstance(); - builder.RegisterType(_scriptExecutorType).As().SingleInstance(); - builder.RegisterType(_replType ?? typeof(Repl)).As().SingleInstance(); - builder.RegisterType().SingleInstance(); - builder.RegisterType().SingleInstance(); - - RegisterLineProcessors(builder); - RegisterReplCommands(builder); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterInstance(_console)); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault( - builder, b => b.RegisterType().As().SingleInstance()); - - RegisterOverrideOrDefault(builder, b => b.RegisterType().As().SingleInstance()); - - if (_initDirectoryCatalog) - { - var fileSystem = _initializationServices.GetFileSystem(); - - var assemblies = _initializationServices.GetAssemblyResolver() - .GetAssemblyPaths(fileSystem.GetWorkingDirectory(_scriptName)) - .Where(assembly => ShouldLoadAssembly(fileSystem, _initializationServices.GetAssemblyUtility(), assembly)); - - var aggregateCatalog = new AggregateCatalog(); - var assemblyLoadFailures = false; - - foreach (var assemblyPath in assemblies) - { - try - { - var catalog = new AssemblyCatalog(assemblyPath); - // force the parts to be queried to catch any errors that would otherwise show up later - catalog.Parts.ToList(); - aggregateCatalog.Catalogs.Add(catalog); - } - catch (ReflectionTypeLoadException typeLoadEx) - { - assemblyLoadFailures = true; - if (typeLoadEx.LoaderExceptions != null && typeLoadEx.LoaderExceptions.Any()) - { - foreach (var ex in typeLoadEx.LoaderExceptions.GroupBy(x => x.Message)) - { - _log.DebugFormat( - "Failure loading assembly: {0}. Exception: {1}", assemblyPath, ex.First().Message); - } - } - } - catch (Exception ex) - { - assemblyLoadFailures = true; - _log.DebugFormat("Failure loading assembly: {0}. Exception: {1}", assemblyPath, ex.Message); - } - } - if (assemblyLoadFailures) - { - _log.Warn(string.IsNullOrEmpty(_scriptName) - ? "Some assemblies failed to load. Launch with '-repl -loglevel debug' to see the details" - : "Some assemblies failed to load. Launch with '-loglevel debug' to see the details"); - } - builder.RegisterComposablePartCatalog(aggregateCatalog); - } - - return builder.Build(); - } - - // HACK: Filter out assemblies in the GAC by checking if full path is specified. - private static bool ShouldLoadAssembly(IFileSystem fileSystem, IAssemblyUtility assemblyUtility, string assembly) - { - return fileSystem.IsPathRooted(assembly) && assemblyUtility.IsManagedAssembly(assembly); - } - - private static void RegisterReplCommands(ContainerBuilder builder) - { - var replCommands = AppDomain.CurrentDomain.GetAssemblies() - .Where(x => !x.IsDynamic) - .SelectMany(x => x.GetExportedTypes()) - .Where(x => typeof(IReplCommand).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract) - .ToArray(); - - builder.RegisterTypes(replCommands).As(); - } - - public ScriptServices GetScriptServices() - { - _log.Debug("Resolving ScriptServices"); - return Container.Resolve(); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ScriptConsole.cs b/src/ScriptCs.Hosting/ScriptConsole.cs deleted file mode 100644 index 251e720e..00000000 --- a/src/ScriptCs.Hosting/ScriptConsole.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using ScriptCs.Contracts; -using Mono.Terminal; - -namespace ScriptCs.Hosting -{ - public class ScriptConsole : IConsole - { - private LineEditor _editor; - - public ScriptConsole() - { - _editor = new LineEditor ("scriptcs"); - } - - public void Write(string value) - { - Console.Write(value); - } - - public void WriteLine() - { - Console.WriteLine(); - } - - public void WriteLine(string value) - { - Console.WriteLine(value); - } - - public string ReadLine(string prompt) - { - return _editor.Edit (prompt, ""); - } - - public void Clear() - { - Console.Clear(); - } - - public void Exit() - { - ResetColor(); - Environment.Exit(0); - } - - public void ResetColor() - { - Console.ResetColor(); - } - - public ConsoleColor ForegroundColor - { - get => Console.ForegroundColor; - set => Console.ForegroundColor = value; - } - - public int Width => Console.BufferWidth; - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj b/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj deleted file mode 100644 index 3a164047..00000000 --- a/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj +++ /dev/null @@ -1,31 +0,0 @@ - - - 1.0.0 - net461 - ScriptCs.Hosting - Glenn Block, Filip Wojcieszyn, Justin Rusbatch - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - ScriptCs.Hosting - ScriptCs.Hosting provides common services necessary for hosting scriptcs in your application. - roslyn csx script scriptcs - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ScriptServicesBuilder.cs b/src/ScriptCs.Hosting/ScriptServicesBuilder.cs deleted file mode 100644 index cd95d4f2..00000000 --- a/src/ScriptCs.Hosting/ScriptServicesBuilder.cs +++ /dev/null @@ -1,143 +0,0 @@ -using System; -using System.Linq; -using System.Collections.Generic; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class ScriptServicesBuilder : ServiceOverrides, IScriptServicesBuilder - { - private readonly ILogProvider _logProvider; - - private IRuntimeServices _runtimeServices; - private bool _repl; - private bool _cache; - private bool _debug; - private string _scriptName; - private LogLevel _logLevel; - private Type _scriptExecutorType; - private Type _replType; - private Type _scriptEngineType; - private bool? _loadScriptPacks; - - public ScriptServicesBuilder( - IConsole console, - ILogProvider logProvider, - IRuntimeServices runtimeServices = null, - IInitializationServices initializationServices = null) - { - InitializationServices = initializationServices ?? new InitializationServices(logProvider); - _runtimeServices = runtimeServices; - ConsoleInstance = console; - _logProvider = logProvider; - } - - public ScriptServices Build() - { - var defaultExecutorType = typeof(ScriptExecutor); - _scriptExecutorType = Overrides.ContainsKey(typeof(IScriptExecutor)) - ? (Type)Overrides[typeof(IScriptExecutor)] - : defaultExecutorType; - - var defaultReplType = typeof(Repl); - _replType = Overrides.ContainsKey(typeof(IRepl)) ? (Type)Overrides[typeof(IRepl)] : defaultReplType; - - _scriptEngineType = (Type)Overrides[typeof(IScriptEngine)]; - - bool initDirectoryCatalog; - - if (_loadScriptPacks.HasValue) - { - initDirectoryCatalog = _loadScriptPacks.Value; - } - else - { - initDirectoryCatalog = _scriptName != null || _repl; - } - - if (_runtimeServices == null) - { - _runtimeServices = new RuntimeServices( - _logProvider, - Overrides, - ConsoleInstance, - _scriptEngineType, - _scriptExecutorType, - _replType, - initDirectoryCatalog, - InitializationServices, - _scriptName); - } - - return _runtimeServices.GetScriptServices(); - } - - public IScriptServicesBuilder LoadModules(string extension, params string[] moduleNames) - { - moduleNames = moduleNames.Union(new[] { "roslyn" }).ToArray(); - - var config = new ModuleConfiguration(_cache, _scriptName, _repl, _logLevel, _debug, Overrides); - var loader = InitializationServices.GetModuleLoader(); - - var fs = InitializationServices.GetFileSystem(); - - var folders = new[] { fs.GlobalFolder }; - loader.Load(config, folders, InitializationServices.GetFileSystem().HostBin, extension, moduleNames); - return this; - } - - public IScriptServicesBuilder LoadScriptPacks(bool load = true) - { - _loadScriptPacks = load; - return this; - } - - public IScriptServicesBuilder Cache(bool cache = true) - { - _cache = cache; - return this; - } - - public IScriptServicesBuilder ScriptName(string name) - { - _scriptName = name; - return this; - } - - public IScriptServicesBuilder Repl(bool repl = true) - { - _repl = repl; - return this; - } - - public IScriptServicesBuilder LogLevel(LogLevel level) - { - _logLevel = level; - return this; - } - - public IScriptServicesBuilder Debug(bool debug = true) - { - _debug = debug; - return this; - } - - public IScriptServicesBuilder SetOverride(TImpl value) where TImpl : TContract - { - Overrides[typeof(TContract)] = value; - return this; - } - - public IScriptServicesBuilder SetOverride() where TImpl : TContract - { - Overrides[typeof(TContract)] = typeof(TImpl); - return this; - } - - public IInitializationServices InitializationServices { get; private set; } - - public IConsole ConsoleInstance { get; private set; } - - internal IRuntimeServices RuntimeServices => _runtimeServices; - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/ScriptServicesRegistration.cs b/src/ScriptCs.Hosting/ScriptServicesRegistration.cs deleted file mode 100644 index 33d03631..00000000 --- a/src/ScriptCs.Hosting/ScriptServicesRegistration.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using ScriptCs.Contracts; -using Autofac; - -namespace ScriptCs.Hosting -{ - public abstract class ScriptServicesRegistration - { - private readonly ILogProvider _logProvider; - private readonly ILog _log; - private readonly IDictionary _overrides; - - public ILogProvider LogProvider => _logProvider; - - protected ScriptServicesRegistration(ILogProvider logProvider, IDictionary overrides) - { - Guard.AgainstNullArgument("logProvider", logProvider); - - _overrides = overrides ?? new Dictionary(); - _logProvider = logProvider; - _log = _logProvider.ForCurrentType(); - } - - protected void RegisterOverrideOrDefault(ContainerBuilder builder, Action registrationAction) - { - Guard.AgainstNullArgument("registrationAction", registrationAction); - - if (_overrides.ContainsKey(typeof(T))) - { - var reg = _overrides[typeof(T)]; - _log.Debug(string.Format("Registering override: {0}", reg)); - - if (reg.GetType().IsSubclassOf(typeof(Type))) - { - builder.RegisterType((Type)reg).As().SingleInstance(); - } - else - { - builder.RegisterInstance(reg).As(); - } - } - else - { - _log.Debug(string.Format("Registering default: {0}", typeof(T))); - registrationAction(builder); - } - } - - protected void RegisterLineProcessors(ContainerBuilder builder) - { - object processors; - this.Overrides.TryGetValue(typeof(ILineProcessor), out processors); - var processorList = (processors as IEnumerable ?? Enumerable.Empty()).ToArray(); - - var loadProcessorType = processorList - .FirstOrDefault(x => typeof(ILoadLineProcessor).IsAssignableFrom(x)) - ?? typeof(LoadLineProcessor); - - var usingProcessorType = processorList - .FirstOrDefault(x => typeof(IUsingLineProcessor).IsAssignableFrom(x)) - ?? typeof(UsingLineProcessor); - - var referenceProcessorType = processorList - .FirstOrDefault(x => typeof(IReferenceLineProcessor).IsAssignableFrom(x)) - ?? typeof(ReferenceLineProcessor); - - var shebangProcessorType = processorList - .FirstOrDefault(x => typeof(IShebangLineProcessor).IsAssignableFrom(x)) - ?? typeof(ShebangLineProcessor); - - var processorArray = new[] { loadProcessorType, usingProcessorType, referenceProcessorType, shebangProcessorType } - .Union(processorList).ToArray(); - - builder.RegisterTypes(processorArray).As(); - } - - private IContainer _container; - - public IContainer Container => _container ?? (_container = CreateContainer()); - - protected IDictionary Overrides => _overrides; - - protected abstract IContainer CreateContainer(); - } -} diff --git a/src/ScriptCs.Hosting/ServiceOverrides.cs b/src/ScriptCs.Hosting/ServiceOverrides.cs deleted file mode 100644 index c303189c..00000000 --- a/src/ScriptCs.Hosting/ServiceOverrides.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public abstract class ServiceOverrides : IServiceOverrides - where TConfig : class, IServiceOverrides - { - public IDictionary Overrides { get; private set; } - - private readonly TConfig _this; - - protected ServiceOverrides() - { - _this = this as TConfig; - Overrides = new Dictionary(); - } - - protected ServiceOverrides(IDictionary overrides) - : this() - { - Overrides = overrides; - } - - public TConfig ScriptHostFactory() where T : IScriptHostFactory - { - Overrides[typeof(IScriptHostFactory)] = typeof(T); - return _this; - } - - public TConfig ScriptExecutor() where T : IScriptExecutor - { - Overrides[typeof(IScriptExecutor)] = typeof(T); - return _this; - } - - public TConfig Repl() where T : IRepl - { - Overrides[typeof(IRepl)] = typeof(T); - return _this; - } - - public TConfig ScriptEngine() where T : IScriptEngine - { - Overrides[typeof(IScriptEngine)] = typeof(T); - return _this; - } - - public TConfig ScriptPackManager() where T : IScriptPackManager - { - Overrides[typeof(IScriptPackManager)] = typeof(T); - return _this; - } - - public TConfig ScriptPackResolver() where T : IScriptPackResolver - { - Overrides[typeof(IScriptPackResolver)] = typeof(T); - return _this; - } - - public TConfig InstallationProvider() where T : IInstallationProvider - { - Overrides[typeof(IInstallationProvider)] = typeof(T); - return _this; - } - - public TConfig FileSystem() where T : IFileSystem - { - Overrides[typeof(IFileSystem)] = typeof(T); - return _this; - } - - public TConfig AssemblyUtility() where T : IAssemblyUtility - { - Overrides[typeof(IAssemblyUtility)] = typeof(T); - return _this; - } - - public TConfig ObjectSerializer() where T : IObjectSerializer - { - Overrides[typeof(IObjectSerializer)] = typeof(T); - return _this; - } - - public TConfig PackageContainer() where T : IPackageContainer - { - Overrides[typeof(IPackageContainer)] = typeof(T); - return _this; - } - - public TConfig PackageInstaller() where T : IPackageInstaller - { - Overrides[typeof(IPackageInstaller)] = typeof(T); - return _this; - } - - public TConfig FilePreProcessor() where T : IFilePreProcessor - { - Overrides[typeof(IFilePreProcessor)] = typeof(T); - return _this; - } - - public TConfig PackageAssemblyResolver() where T : IPackageAssemblyResolver - { - Overrides[typeof(IPackageAssemblyResolver)] = typeof(T); - return _this; - } - - public TConfig AssemblyResolver() where T : IAssemblyResolver - { - Overrides[typeof(IAssemblyResolver)] = typeof(T); - return _this; - } - - public TConfig Console() where T : IConsole - { - Overrides[typeof(IConsole)] = typeof(T); - return _this; - } - - public TConfig LineProcessor() where T : ILineProcessor - { - var key = typeof(ILineProcessor); - object value; - if (!Overrides.TryGetValue(key, out value) || value == null) - { - value = Enumerable.Empty(); - } - - IEnumerable processors; - try - { - processors = (IEnumerable)value; - } - catch (InvalidCastException ex) - { - throw new InvalidOperationException("The line processors override has an invalid type.", ex); - } - - Overrides[typeof(ILineProcessor)] = new[] { typeof(T) }.Concat(processors).ToList(); - - return _this; - } - } -} diff --git a/src/ScriptCs.Hosting/TypeResolver.cs b/src/ScriptCs.Hosting/TypeResolver.cs deleted file mode 100644 index 36f7c35e..00000000 --- a/src/ScriptCs.Hosting/TypeResolver.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace ScriptCs.Hosting -{ - public class TypeResolver : ITypeResolver - { - public Type ResolveType(string type) => Type.GetType(type); - } -} \ No newline at end of file diff --git a/src/ScriptCs.Hosting/VisualStudioSolution.cs b/src/ScriptCs.Hosting/VisualStudioSolution.cs deleted file mode 100644 index 855f47c6..00000000 --- a/src/ScriptCs.Hosting/VisualStudioSolution.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class VisualStudioSolution : IVisualStudioSolution - { - internal StringBuilder Header { get; set; } - internal StringBuilder Projects { get; set; } - internal StringBuilder Global { get; set; } - - public VisualStudioSolution() - { - Header = new StringBuilder(); - Projects = new StringBuilder(); - Global = new StringBuilder(); - AddHeader(); - } - - public void AddHeader() - { - Header.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00"); - Header.AppendLine("# Visual Studio 2013"); - Header.AppendLine("VisualStudioVersion = 12.0.30501.0"); - Header.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1"); - } - - public void AddScriptcsProject(string scriptcsPath, string workingPath, string args, bool attach, Guid projectGuid) - { - Projects.AppendFormat(@"Project(""{{911E67C6-3D85-4FCE-B560-20A9C3E3FF48}}"") = ""scriptcs"", ""{0}"", ""{{{1}}}""{2}", scriptcsPath, projectGuid, Environment.NewLine); - Projects.AppendLine("\tProjectSection(DebuggerProjectSystem) = preProject"); - Projects.AppendLine("\t\tPortSupplier = 00000000-0000-0000-0000-000000000000"); - Projects.AppendFormat("\t\tExecutable = {0}{1}", scriptcsPath, Environment.NewLine); - Projects.AppendLine("\t\tRemoteMachine = localhost"); - Projects.AppendFormat("\t\tStartingDirectory = {0}{1}", workingPath, Environment.NewLine); - Projects.AppendFormat("\t\tArguments = {0}{1}", args, Environment.NewLine); - Projects.AppendLine("\t\tEnvironment = Default"); - Projects.AppendLine("\t\tLaunchingEngine = 00000000-0000-0000-0000-000000000000"); - Projects.AppendLine("\t\tUseLegacyDebugEngines = No"); - Projects.AppendLine("\t\tLaunchSQLEngine = No"); - Projects.AppendFormat("\t\tAttachLaunchAction = {0}{1}", attach == true ? "Yes" : "No", Environment.NewLine); - Projects.AppendLine("\tEndProjectSection"); - Projects.AppendLine("EndProject"); - } - - public void AddProject(string path, string name, Guid guid, string[] files) - { - Projects.AppendFormat(@"Project(""{{2150E333-8FDC-42A3-9474-1A3956D46DE8}}"") = ""{0}"", ""{0}"", ""{{{1}}}""{2}", name, guid, Environment.NewLine); - Projects.AppendLine("\tProjectSection(SolutionItems) = preProject"); - foreach (var file in files) - { - if (path == null) - { - Projects.AppendFormat("\t\t{0} = {0}{1}", file, Environment.NewLine); - } - else - { - Projects.AppendFormat("\t\t{0}\\{1} = {0}\\{1}{2}", path, file, Environment.NewLine); - } - } - Projects.AppendLine("\tEndProjectSection"); - Projects.AppendLine("EndProject"); - } - - public void AddGlobalHeader(Guid projectGuid) - { - Global.AppendLine("Global"); - Global.AppendLine("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution"); - Global.AppendLine("\t\tDebug|Any CPU = Debug|Any CPU"); - Global.AppendLine("\tEndGlobalSection"); - Global.AppendLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution"); - Global.AppendFormat("\t\t{{{0}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU{1}", projectGuid, Environment.NewLine); - Global.AppendLine("\tEndGlobalSection"); - Global.AppendLine("\tGlobalSection(SolutionProperties) = preSolution"); - Global.AppendLine("\t\tHideSolutionNode = FALSE"); - Global.AppendLine("\tEndGlobalSection"); - } - - public void AddGlobalNestedProjects(IList nestedItems) - { - if (nestedItems.Any()) - { - Global.AppendLine("\tGlobalSection(NestedProjects) = preSolution"); - foreach (var item in nestedItems) - { - Global.AppendFormat("\t\t{{{0}}} = {{{1}}}{2}", item.Project, item.Parent, Environment.NewLine); - } - Global.AppendLine("\tEndGlobalSection"); - } - } - - public void AddGlobal(Guid projectGuid, IList nestedItems) - { - AddGlobalHeader(projectGuid); - AddGlobalNestedProjects(nestedItems); - Global.AppendLine("EndGlobal"); - } - - public override string ToString() - { - var solutionBuilder = new StringBuilder(); - solutionBuilder.Append(Header); - solutionBuilder.Append(Projects); - solutionBuilder.Append(Global); - return solutionBuilder.ToString(); - } - } -} diff --git a/src/ScriptCs.Hosting/VisualStudioSolutionWriter.cs b/src/ScriptCs.Hosting/VisualStudioSolutionWriter.cs deleted file mode 100644 index 244f46e4..00000000 --- a/src/ScriptCs.Hosting/VisualStudioSolutionWriter.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting -{ - public class VisualStudioSolutionWriter : IVisualStudioSolutionWriter - { - internal DirectoryInfo Root { get; set; } - private Guid _nullGuid = new Guid(); - - public string WriteSolution(IFileSystem fs, string script, IVisualStudioSolution solution, IList nestedItems = null) - { - if (nestedItems == null) - { - nestedItems = new List(); - } - - var launcher = Path.Combine(fs.TempPath, "launcher-" + Guid.NewGuid().ToString() + ".sln"); - - if (fs.FileExists(launcher)) - { - fs.FileDelete(launcher); - } - - var currentDir = fs.CurrentDirectory; - var scriptcsPath = Path.Combine(fs.HostBin, "scriptcs.exe"); - var scriptcsArgs = string.Format("{0} -debug -loglevel info", script); - Root = new DirectoryInfo { Name = "Solution Items", FullPath = currentDir}; - var projectGuid = Guid.NewGuid(); - - solution.AddScriptcsProject(scriptcsPath, currentDir, scriptcsArgs, false, projectGuid); - GetDirectoryInfo(fs, currentDir, Root); - AddDirectoryProject(solution, fs, Root, _nullGuid, nestedItems); - solution.AddGlobal(projectGuid, nestedItems); - fs.WriteToFile(launcher, solution.ToString()); - return launcher; - } - - private void AddDirectoryProject(IVisualStudioSolution solution, IFileSystem fs, DirectoryInfo currentDirectory, Guid parent, IList nestedItems) - { - solution.AddProject(currentDirectory.FullPath, currentDirectory.Name, currentDirectory.Guid, currentDirectory.Files.ToArray()); - foreach (DirectoryInfo dir in currentDirectory.Directories.Values) - { - AddDirectoryProject(solution, fs, dir, currentDirectory.Guid, nestedItems); - } - if (parent != _nullGuid) - { - nestedItems.Add(new ProjectItem(currentDirectory.Guid, parent)); - } - } - - private void GetDirectoryInfo(IFileSystem fs, string currentDir, DirectoryInfo root) - { - IEnumerable files; - var packagesFolder = Path.Combine(currentDir, fs.PackagesFolder); - files = fs.EnumerateFilesAndDirectories(currentDir, "*.csx").Where( - f => f.StartsWith(packagesFolder).Equals(false)); - - var skip = currentDir.Length; - - foreach (var file in files) - { - var pruned = file.Substring(skip + 1); - var segments = pruned.Split(Path.DirectorySeparatorChar); - if (segments.Length == 1) - { - root.Files.Add(segments[0]); - } - else - { - var currentDirectory = root; - var path = ""; - for (int i = 0; i < segments.Length - 1; i++) - { - var segment = segments[i]; - path = path + segment + @"\"; - if (!currentDirectory.Directories.ContainsKey(segment)) - { - var newDirectory = new DirectoryInfo { Name = segment, Path=path, FullPath = Path.GetDirectoryName(file)}; - currentDirectory.Directories[segment] = newDirectory; - currentDirectory = newDirectory; - } - else - { - currentDirectory = currentDirectory.Directories[segment]; - } - } - currentDirectory.Files.Add(segments[segments.Length - 1]); - } - } - } - } -} diff --git a/src/ScriptCs/Application.cs b/src/ScriptCs/Application.cs deleted file mode 100644 index 7b80c876..00000000 --- a/src/ScriptCs/Application.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace ScriptCs -{ - using ScriptCs.Command; - - internal static class Application - { - public static int Run(Config config, string[] scriptArgs) - { - var scriptServicesBuilder = ScriptServicesBuilderFactory.Create(config, scriptArgs); - var factory = new CommandFactory(scriptServicesBuilder); - var command = factory.CreateCommand(config, scriptArgs); - return (int)command.Execute(); - } - } -} diff --git a/src/ScriptCs/Command/CleanCommand.cs b/src/ScriptCs/Command/CleanCommand.cs deleted file mode 100644 index f543c595..00000000 --- a/src/ScriptCs/Command/CleanCommand.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.IO; -using ScriptCs.Contracts; - -namespace ScriptCs.Command -{ - internal class CleanCommand : ICleanCommand - { - private readonly string _scriptName; - private readonly IFileSystem _fileSystem; - private readonly ILog _logger; - - public CleanCommand(string scriptName, IFileSystem fileSystem, ILogProvider logProvider) - { - Guard.AgainstNullArgumentProperty("fileSystem", "PackagesFolder", fileSystem.PackagesFolder); - Guard.AgainstNullArgumentProperty("fileSystem", "DllCacheFolder", fileSystem.DllCacheFolder); - - Guard.AgainstNullArgument("logProvider", logProvider); - - _scriptName = scriptName; - _fileSystem = fileSystem; - _logger = logProvider.ForCurrentType(); - } - - public CommandResult Execute() - { - _logger.Info("Cleaning initiated..."); - - var workingDirectory = _fileSystem.GetWorkingDirectory(_scriptName); - _logger.TraceFormat("Working directory: {0}", workingDirectory); - - var packageFolder = Path.Combine(workingDirectory, _fileSystem.PackagesFolder); - _logger.TraceFormat("Packages folder: {0}", packageFolder); - - var cacheFolder = Path.Combine(workingDirectory, _fileSystem.DllCacheFolder); - _logger.TraceFormat("Cache folder: {0}", cacheFolder); - - try - { - if (_fileSystem.DirectoryExists(packageFolder)) - { - _logger.DebugFormat("Deleting package directory: {0}", packageFolder); - _fileSystem.DeleteDirectory(packageFolder); - } - - if (_fileSystem.DirectoryExists(cacheFolder)) - { - _logger.DebugFormat("Deleting cache directory: {0}", cacheFolder); - _fileSystem.DeleteDirectory(cacheFolder); - } - - _logger.Info("Cleaning completed."); - return CommandResult.Success; - } - catch (Exception ex) - { - _logger.ErrorFormat("Cleaning failed: {0}.", ex, ex.Message); - return CommandResult.Error; - } - } - } -} diff --git a/src/ScriptCs/Command/CommandFactory.cs b/src/ScriptCs/Command/CommandFactory.cs deleted file mode 100644 index ae1ef9e1..00000000 --- a/src/ScriptCs/Command/CommandFactory.cs +++ /dev/null @@ -1,200 +0,0 @@ -using System; -using System.IO; -using ScriptCs.Contracts; -using ScriptCs.Hosting; - -namespace ScriptCs.Command -{ - public class CommandFactory - { - private readonly IScriptServicesBuilder _scriptServicesBuilder; - private readonly IInitializationServices _initializationServices; - private readonly IFileSystem _fileSystem; - - public CommandFactory(IScriptServicesBuilder scriptServicesBuilder) - { - Guard.AgainstNullArgument("scriptServicesBuilder", scriptServicesBuilder); - - _scriptServicesBuilder = scriptServicesBuilder; - _initializationServices = _scriptServicesBuilder.InitializationServices; - _fileSystem = _initializationServices.GetFileSystem(); - - if (_fileSystem.PackagesFile == null) - { - throw new ArgumentException( - "The file system provided by the initialization services provided by the script services builder has a null packages file.", "scriptServicesBuilder"); - } - - if (_fileSystem.PackagesFolder == null) - { - throw new ArgumentException( - "The file system provided by the initialization services provided by the script services builder has a null package folder.", "scriptServicesBuilder"); - } - } - - public ICommand CreateCommand(Config config, string[] scriptArgs) - { - Guard.AgainstNullArgument("config", config); - - var scriptServices = _scriptServicesBuilder.Build(); - - if (config.Global) - { - var currentDir = _fileSystem.GlobalFolder; - if (!_fileSystem.DirectoryExists(currentDir)) - { - _fileSystem.CreateDirectory(currentDir); - } - - _fileSystem.CurrentDirectory = currentDir; - } - - _initializationServices.GetInstallationProvider().Initialize(); - - if (config.Repl) - { - var explicitReplCommand = new ExecuteReplCommand( - config.ScriptName, - scriptArgs, - scriptServices.FileSystem, - scriptServices.ScriptPackResolver, - scriptServices.Repl, - scriptServices.LogProvider, - scriptServices.Console, - scriptServices.AssemblyResolver, - scriptServices.ScriptLibraryComposer); - - return explicitReplCommand; - } - - if (config.Eval != null) - { - var executeLooseScriptCommand = new ExecuteLooseScriptCommand( - config.Eval, - scriptArgs, - scriptServices.FileSystem, - scriptServices.Executor, - scriptServices.ScriptPackResolver, - scriptServices.LogProvider, - scriptServices.AssemblyResolver, - scriptServices.ScriptLibraryComposer); - - return executeLooseScriptCommand; - } - - if (config.ScriptName != null) - { - var currentDirectory = _fileSystem.CurrentDirectory; - var packageFile = Path.Combine(currentDirectory, _fileSystem.PackagesFile); - var packagesFolder = Path.Combine(currentDirectory, _fileSystem.PackagesFolder); - - if (_fileSystem.FileExists(packageFile) && !_fileSystem.DirectoryExists(packagesFolder)) - { - var installCommand = new InstallCommand( - null, - null, - true, - _fileSystem, - _initializationServices.GetPackageAssemblyResolver(), - _initializationServices.GetPackageInstaller(), - scriptServices.ScriptLibraryComposer, - _initializationServices.LogProvider); - - var executeCommand = new DeferredCreationCommand(() => - CreateScriptCommand( - config, - scriptArgs, - ScriptServicesBuilderFactory.Create(config, scriptArgs).Build())); - - return new CompositeCommand(installCommand, executeCommand); - } - - return CreateScriptCommand(config, scriptArgs, scriptServices); - } - - if (config.Clean) - { - var saveCommand = new SaveCommand( - _initializationServices.GetPackageAssemblyResolver(), - _fileSystem, - _initializationServices.LogProvider); - - if (config.Global) - { - var currentDirectory = _fileSystem.GlobalFolder; - _fileSystem.CurrentDirectory = currentDirectory; - if (!_fileSystem.DirectoryExists(currentDirectory)) - { - _fileSystem.CreateDirectory(currentDirectory); - } - } - - var cleanCommand = new CleanCommand(config.ScriptName, _fileSystem, _initializationServices.LogProvider); - - return new CompositeCommand(saveCommand, cleanCommand); - } - - if (config.Save) - { - return new SaveCommand( - _initializationServices.GetPackageAssemblyResolver(), - _fileSystem, - _initializationServices.LogProvider); - } - - if (config.PackageName != null) - { - var packageAssemblyResolver = _initializationServices.GetPackageAssemblyResolver(); - - var installCommand = new InstallCommand( - config.PackageName, - config.PackageVersion, - config.AllowPreRelease, - _fileSystem, - packageAssemblyResolver, - _initializationServices.GetPackageInstaller(), - scriptServices.ScriptLibraryComposer, - _initializationServices.LogProvider); - - var saveCommand = new SaveCommand(packageAssemblyResolver, _fileSystem, _initializationServices.LogProvider); - - return new CompositeCommand(installCommand, saveCommand); - } - - // NOTE (adamralph): no script name or command so assume REPL - var replCommand = new ExecuteReplCommand( - config.ScriptName, - scriptArgs, - scriptServices.FileSystem, - scriptServices.ScriptPackResolver, - scriptServices.Repl, - scriptServices.LogProvider, - scriptServices.Console, - scriptServices.AssemblyResolver, - scriptServices.ScriptLibraryComposer); - - return replCommand; - } - - private static IScriptCommand CreateScriptCommand( - Config config, string[] scriptArgs, ScriptServices scriptServices) - { - return config.Watch - ? (IScriptCommand)new WatchScriptCommand( - config, - scriptArgs, - scriptServices.Console, - scriptServices.FileSystem, - scriptServices.LogProvider) - : new ExecuteScriptCommand( - config.ScriptName, - scriptArgs, - scriptServices.FileSystem, - scriptServices.Executor, - scriptServices.ScriptPackResolver, - scriptServices.LogProvider, - scriptServices.AssemblyResolver, - scriptServices.ScriptLibraryComposer); - } - } -} diff --git a/src/ScriptCs/Command/CommandResult.cs b/src/ScriptCs/Command/CommandResult.cs deleted file mode 100644 index e4ba13b6..00000000 --- a/src/ScriptCs/Command/CommandResult.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace ScriptCs.Command -{ - public enum CommandResult - { - Success = 0, - Error = 1, - } -} \ No newline at end of file diff --git a/src/ScriptCs/Command/CompositeCommand.cs b/src/ScriptCs/Command/CompositeCommand.cs deleted file mode 100644 index d43ee339..00000000 --- a/src/ScriptCs/Command/CompositeCommand.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace ScriptCs.Command -{ - public class CompositeCommand : ICompositeCommand - { - public CompositeCommand(params ICommand[] commands) - { - Commands = commands.ToList(); - } - - public List Commands { get; private set; } - - public CommandResult Execute() - { - var result = CommandResult.Success; - foreach (var command in Commands) - { - result = command.Execute(); - - if (result != CommandResult.Success) - { - return result; - } - } - - return result; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs/Command/CrossAppDomainExecuteScriptCommand.cs b/src/ScriptCs/Command/CrossAppDomainExecuteScriptCommand.cs deleted file mode 100644 index b4ac5dca..00000000 --- a/src/ScriptCs/Command/CrossAppDomainExecuteScriptCommand.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; - -namespace ScriptCs.Command -{ - [Serializable] - public class CrossAppDomainExecuteScriptCommand : ICrossAppDomainScriptCommand - { - public Config Config { get; set; } - public string[] ScriptArgs { get; set; } - public CommandResult Result { get; private set; } - - public void Execute() - { - if (this.Config == null) - { - throw new InvalidOperationException("The config is missing."); - } - - var services = ScriptServicesBuilderFactory.Create(this.Config, this.ScriptArgs).Build(); - var command = new ExecuteScriptCommand( - this.Config.ScriptName, - this.ScriptArgs, - services.FileSystem, - services.Executor, - services.ScriptPackResolver, - services.LogProvider, - services.AssemblyResolver, - services.ScriptLibraryComposer); - - this.Result = command.Execute(); - } - } -} diff --git a/src/ScriptCs/Command/DeferredCreationCommand.cs b/src/ScriptCs/Command/DeferredCreationCommand.cs deleted file mode 100644 index af3953e2..00000000 --- a/src/ScriptCs/Command/DeferredCreationCommand.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -namespace ScriptCs.Command -{ - internal class DeferredCreationCommand : IDeferredCreationCommand where TCommand : ICommand - { - private readonly Func _factory; - - public DeferredCreationCommand(Func factory) - { - Guard.AgainstNullArgument("factory", factory); - - _factory = factory; - } - - public CommandResult Execute() => _factory().Execute(); - } -} \ No newline at end of file diff --git a/src/ScriptCs/Command/ExecuteLooseScriptCommand.cs b/src/ScriptCs/Command/ExecuteLooseScriptCommand.cs deleted file mode 100644 index b6952370..00000000 --- a/src/ScriptCs/Command/ExecuteLooseScriptCommand.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.Command -{ - internal class ExecuteLooseScriptCommand : ExecuteScriptCommandBase, IExecuteLooseScriptCommand - { - public ExecuteLooseScriptCommand( - string script, string[] scriptArgs, - IFileSystem fileSystem, IScriptExecutor scriptExecutor, - IScriptPackResolver scriptPackResolver, - ILogProvider logProvider, - IAssemblyResolver assemblyResolver, - IScriptLibraryComposer composer) : - base(script, scriptArgs, fileSystem, scriptExecutor, scriptPackResolver, logProvider, assemblyResolver, composer) - { - } - - public override CommandResult Execute() - { - try - { - var assemblyPaths = Enumerable.Empty(); - var workingDirectory = FileSystem.CurrentDirectory; - assemblyPaths = AssemblyResolver.GetAssemblyPaths(workingDirectory); - Composer.Compose(workingDirectory); - - ScriptExecutor.Initialize(assemblyPaths, _scriptPackResolver.GetPacks()); - - // HACK: This is a (dirty) fix for #1086. This might be a temporary solution until some further refactoring can be done. - ScriptExecutor.ScriptEngine.CacheDirectory = Path.Combine(workingDirectory ?? FileSystem.CurrentDirectory, FileSystem.DllCacheFolder); - var scriptResult = ScriptExecutor.ExecuteScript(Script, ScriptArgs); - var commandResult = Inspect(scriptResult); - ScriptExecutor.Terminate(); - return commandResult; - } - catch (Exception ex) - { - Logger.ErrorException("Error executing script '{0}'", ex, Script); - return CommandResult.Error; - } - } - } -} diff --git a/src/ScriptCs/Command/ExecuteReplCommand.cs b/src/ScriptCs/Command/ExecuteReplCommand.cs deleted file mode 100644 index fb2a2287..00000000 --- a/src/ScriptCs/Command/ExecuteReplCommand.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using ScriptCs.Contracts; - -namespace ScriptCs.Command -{ - internal class ExecuteReplCommand : IExecuteReplCommand - { - private readonly string _scriptName; - private readonly string[] _scriptArgs; - private readonly IFileSystem _fileSystem; - private readonly IScriptPackResolver _scriptPackResolver; - private readonly IRepl _repl; - private readonly ILog _logger; - private readonly IConsole _console; - private readonly IAssemblyResolver _assemblyResolver; - private readonly IScriptLibraryComposer _composer; - - public ExecuteReplCommand( - string scriptName, - string[] scriptArgs, - IFileSystem fileSystem, - IScriptPackResolver scriptPackResolver, - IRepl repl, - ILogProvider logProvider, - IConsole console, - IAssemblyResolver assemblyResolver, - IScriptLibraryComposer composer) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("scriptPackResolver", scriptPackResolver); - Guard.AgainstNullArgument("repl", repl); - Guard.AgainstNullArgument("logProvider", logProvider); - Guard.AgainstNullArgument("console", console); - Guard.AgainstNullArgument("assemblyResolver", assemblyResolver); - Guard.AgainstNullArgument("composer", composer); - - _scriptName = scriptName; - _scriptArgs = scriptArgs; - _fileSystem = fileSystem; - _scriptPackResolver = scriptPackResolver; - _repl = repl; - _logger = logProvider.ForCurrentType(); - _console = console; - _assemblyResolver = assemblyResolver; - _composer = composer; - } - - public string[] ScriptArgs => _scriptArgs; - - public CommandResult Execute() - { - _console.WriteLine("scriptcs (ctrl-c to exit or :help for help)" + Environment.NewLine); - - var workingDirectory = _fileSystem.CurrentDirectory; - var assemblies = _assemblyResolver.GetAssemblyPaths(workingDirectory); - var scriptPacks = _scriptPackResolver.GetPacks(); - - _composer.Compose(workingDirectory); - - _repl.Initialize(assemblies, scriptPacks, ScriptArgs); - - if (!string.IsNullOrWhiteSpace(_scriptName)) - { - _logger.InfoFormat("Executing script '{0}'", _scriptName); - try - { - _repl.Execute(string.Format("#load {0}", _scriptName)); - } - catch (Exception ex) - { - _logger.ErrorException("Error executing script '{0}'", ex, _scriptName); - return CommandResult.Error; - } - } - - try - { - while (ExecuteLine(_repl)) - { - } - - _console.WriteLine(); - } - catch (Exception ex) - { - _logger.ErrorException("Error executing REPL", ex); - return CommandResult.Error; - } - - _repl.Terminate(); - return CommandResult.Success; - } - - private bool ExecuteLine(IRepl repl) - { - var prompt = string.IsNullOrWhiteSpace (repl.Buffer) ? "> " : "* "; - - try - { - var line = _console.ReadLine(prompt); - - if (line == null) - return false; - - if (!string.IsNullOrWhiteSpace(line)) - { - repl.Execute(line); - } - - return true; - } - catch - { - return false; - } - } - } -} diff --git a/src/ScriptCs/Command/ExecuteScriptCommand.cs b/src/ScriptCs/Command/ExecuteScriptCommand.cs deleted file mode 100644 index f55100f9..00000000 --- a/src/ScriptCs/Command/ExecuteScriptCommand.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using ScriptCs.Contracts; - -namespace ScriptCs.Command -{ - internal class ExecuteScriptCommand : ExecuteScriptCommandBase, IScriptCommand - { - public ExecuteScriptCommand( - string script, string[] scriptArgs, - IFileSystem fileSystem, IScriptExecutor scriptExecutor, - IScriptPackResolver scriptPackResolver, - ILogProvider logProvider, - IAssemblyResolver assemblyResolver, - IScriptLibraryComposer composer) : - base(script, scriptArgs, fileSystem, scriptExecutor, scriptPackResolver, logProvider, assemblyResolver, composer) - { - } - - public override CommandResult Execute() - { - try - { - var assemblyPaths = Enumerable.Empty(); - var workingDirectory = FileSystem.GetWorkingDirectory(Script); - if (workingDirectory != null) - { - assemblyPaths = AssemblyResolver.GetAssemblyPaths(workingDirectory); - } - - Composer.Compose(workingDirectory); - - ScriptExecutor.Initialize(assemblyPaths, _scriptPackResolver.GetPacks(), ScriptArgs); - - // HACK: This is a (dirty) fix for #1086. This might be a temporary solution until some further refactoring can be done. - ScriptExecutor.ScriptEngine.CacheDirectory = Path.Combine(workingDirectory ?? FileSystem.CurrentDirectory, FileSystem.DllCacheFolder); - var scriptResult = ScriptExecutor.Execute(Script, ScriptArgs); - - var commandResult = Inspect(scriptResult); - ScriptExecutor.Terminate(); - return commandResult; - } - catch (Exception ex) - { - Logger.ErrorException("Error executing script '{0}'", ex, Script); - return CommandResult.Error; - } - } - } -} diff --git a/src/ScriptCs/Command/FileWatcher.cs b/src/ScriptCs/Command/FileWatcher.cs deleted file mode 100644 index e590439b..00000000 --- a/src/ScriptCs/Command/FileWatcher.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using System.Threading; -using ScriptCs.Contracts; - -namespace ScriptCs.Command -{ - public sealed class FileWatcher : IDisposable - { - private readonly object _timerLock = new object(); - private readonly string _file; - private readonly int _intervalMilliseconds; - private readonly IFileSystem _fileSystem; - - private DateTime _lastWriteTime; - private Timer _timer; - - public FileWatcher(string file, int intervalMilliseconds, IFileSystem fileSystem) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - - _file = file; - _intervalMilliseconds = intervalMilliseconds; - _fileSystem = fileSystem; - } - - public event EventHandler Changed; - - public void Start() - { - lock (_timerLock) - { - if (_timer != null) - { - return; - } - - _lastWriteTime = _fileSystem.GetLastWriteTime(_file); - _timer = new Timer(_ => CheckLastWriteTime(), null, Timeout.Infinite, Timeout.Infinite); - _timer.Change(_intervalMilliseconds, Timeout.Infinite); - } - } - - public void Stop() - { - lock (_timerLock) - { - if (_timer == null) - { - return; - } - - _timer.Dispose(); - _timer = null; - } - } - - public void Dispose() - { - Stop(); - } - - private void CheckLastWriteTime() - { - lock (_timerLock) - { - if (_timer == null) - { - return; - } - - var previousLastWriteTime = _lastWriteTime; - _lastWriteTime = _fileSystem.GetLastWriteTime(_file); - if (_lastWriteTime != previousLastWriteTime) - { - var changed = this.Changed; - if (changed != null) - { - changed.Invoke(this, EventArgs.Empty); - } - } - - _timer.Change(_intervalMilliseconds, Timeout.Infinite); - } - } - } -} diff --git a/src/ScriptCs/Command/ICommand.cs b/src/ScriptCs/Command/ICommand.cs deleted file mode 100644 index 705eb21f..00000000 --- a/src/ScriptCs/Command/ICommand.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System.Collections.Generic; - -namespace ScriptCs.Command -{ - public interface IScriptCommand : ICommand - { - string[] ScriptArgs { get; } - } - - public interface IExecuteReplCommand : IScriptCommand - { - } - - public interface IExecuteLooseScriptCommand : ICommand - { - } - - public interface ISaveCommand : ICommand - { - } - - public interface ICleanCommand : ICommand - { - } - - public interface IInstallCommand : ICommand - { - } - - public interface ICompositeCommand : ICommand - { - List Commands { get; } - } - - public interface IDeferredCreationCommand : ICommand where TCommand : ICommand - { - } - - public interface ICommand - { - CommandResult Execute(); - } - - public interface ICrossAppDomainCommand - { - CommandResult Result { get; } - - void Execute(); - } - - public interface ICrossAppDomainScriptCommand : ICrossAppDomainCommand - { - string[] ScriptArgs { get; } - } -} \ No newline at end of file diff --git a/src/ScriptCs/Command/InstallCommand.cs b/src/ScriptCs/Command/InstallCommand.cs deleted file mode 100644 index d1e960dc..00000000 --- a/src/ScriptCs/Command/InstallCommand.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Runtime.Versioning; -using ScriptCs.Contracts; -using ScriptCs.Hosting.Package; - -namespace ScriptCs.Command -{ - internal class InstallCommand : IInstallCommand - { - private readonly string _name; - private readonly string _version; - private readonly bool _allowPre; - private readonly IFileSystem _fileSystem; - private readonly IPackageAssemblyResolver _packageAssemblyResolver; - private readonly IPackageInstaller _packageInstaller; - private readonly IScriptLibraryComposer _composer; - private readonly ILog _logger; - - public InstallCommand( - string name, - string version, - bool allowPre, - IFileSystem fileSystem, - IPackageAssemblyResolver packageAssemblyResolver, - IPackageInstaller packageInstaller, - IScriptLibraryComposer composer, - ILogProvider logger) - { - _name = name; - _version = version ?? string.Empty; - _allowPre = allowPre; - _fileSystem = fileSystem; - _packageAssemblyResolver = packageAssemblyResolver; - _packageInstaller = packageInstaller; - _composer = composer; - _logger = logger.ForCurrentType(); - } - - public CommandResult Execute() - { - _logger.Info("Installing packages..."); - - var packagesFolder = Path.Combine(_fileSystem.CurrentDirectory, _fileSystem.PackagesFolder); - if (!string.IsNullOrWhiteSpace(_composer.ScriptLibrariesFile)) - { - var scriptLibrariesFile = Path.Combine(packagesFolder, _composer.ScriptLibrariesFile); - - if (_fileSystem.FileExists(scriptLibrariesFile)) - { - _logger.DebugFormat("Deleting: {0}", scriptLibrariesFile); - _fileSystem.FileDelete(scriptLibrariesFile); - } - } - - var packages = GetPackages(_fileSystem.CurrentDirectory); - try - { - _packageInstaller.InstallPackages(packages, _allowPre); - _logger.Info("Package installation succeeded."); - return CommandResult.Success; - } - catch (Exception ex) - { - if (ex is AggregateException agrEx) - { - ex = agrEx.Flatten().InnerException; - } - _logger.ErrorException("Package installation failed.", ex); - return CommandResult.Error; - } - } - - private IEnumerable GetPackages(string workingDirectory) - { - if (string.IsNullOrWhiteSpace(_name)) - { - var packages = _packageAssemblyResolver.GetPackages(workingDirectory); - foreach (var packageReference in packages) - { - yield return packageReference; - } - - yield break; - } - - yield return new PackageReference(_name, new FrameworkName(FrameworkUtils.FrameworkName), _version); - } - } -} \ No newline at end of file diff --git a/src/ScriptCs/Command/SaveCommand.cs b/src/ScriptCs/Command/SaveCommand.cs deleted file mode 100644 index 70819d6a..00000000 --- a/src/ScriptCs/Command/SaveCommand.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using ScriptCs.Contracts; - -namespace ScriptCs.Command -{ - internal class SaveCommand : ISaveCommand - { - private readonly IPackageAssemblyResolver _packageAssemblyResolver; - - private readonly IFileSystem _fileSystem; - private readonly ILog _logger; - - public SaveCommand(IPackageAssemblyResolver packageAssemblyResolver, IFileSystem fileSystem, ILogProvider logProvider) - { - Guard.AgainstNullArgument("packageAssemblyResolver", packageAssemblyResolver); - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("logProvider", logProvider); - - _packageAssemblyResolver = packageAssemblyResolver; - _fileSystem = fileSystem; - _logger = logProvider.ForCurrentType(); - } - - public CommandResult Execute() - { - _logger.InfoFormat("Saving packages in {0}...", _fileSystem.PackagesFile); - - try - { - _packageAssemblyResolver.SavePackages(); - } - catch (Exception ex) - { - _logger.ErrorFormat("Package saving failed: {0}.", ex, ex.Message); - return CommandResult.Error; - } - - return CommandResult.Success; - } - } -} \ No newline at end of file diff --git a/src/ScriptCs/Command/WatchScriptCommand.cs b/src/ScriptCs/Command/WatchScriptCommand.cs deleted file mode 100644 index 3c1a0f44..00000000 --- a/src/ScriptCs/Command/WatchScriptCommand.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.Threading; -using ScriptCs.Contracts; - -namespace ScriptCs.Command -{ - internal class WatchScriptCommand : IScriptCommand - { - private readonly AppDomainSetup _setup = new AppDomainSetup - { - ApplicationBase = AppDomain.CurrentDomain.BaseDirectory - }; - - private readonly Config _config; - private readonly string[] _scriptArgs; - private readonly IConsole _console; - private readonly IFileSystem _fileSystem; - private readonly ILog _logger; - private readonly CrossAppDomainExecuteScriptCommand _executeScriptCommand; - - public WatchScriptCommand( - Config config, - string[] scriptArgs, - IConsole console, - IFileSystem fileSystem, - ILogProvider logProvider) - { - Guard.AgainstNullArgument("config", config); - Guard.AgainstNullArgument("scriptArgs", scriptArgs); - Guard.AgainstNullArgument("console", console); - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("logProvider", logProvider); - - _config = config; - _scriptArgs = scriptArgs; - _console = console; - _fileSystem = fileSystem; - _logger = logProvider.ForCurrentType(); - - _executeScriptCommand = new CrossAppDomainExecuteScriptCommand - { - Config = _config, - ScriptArgs = _scriptArgs, - }; - } - - public CommandResult Execute() - { - _console.WriteLine("scriptcs (ctrl-c to exit)"); - _logger.InfoFormat("Running script '{0}' and watching for changes...", _config.ScriptName); - - while (true) - { - using (var fileChanged = new ManualResetEventSlim()) - using (var watcher = new FileWatcher(_config.ScriptName, 500, _fileSystem)) - { - _logger.DebugFormat("Creating app domain '{0}'...", _config.ScriptName); - var appDomain = AppDomain.CreateDomain(_config.ScriptName, null, _setup); - try - { - watcher.Changed += (sender, e) => - { - _logger.DebugFormat("Script '{0}' changed.", _config.ScriptName); - EnsureUnloaded(appDomain); - fileChanged.Set(); - }; - - watcher.Start(); - _logger.DebugFormat("Executing script '{0}' and watching for changes...", _config.ScriptName); - fileChanged.Reset(); - try - { - appDomain.DoCallBack(_executeScriptCommand.Execute); - } - catch (AppDomainUnloadedException ex) - { - _logger.DebugFormat("App domain '{0}' has been unloaded.", ex, _config.ScriptName); - } - } - finally - { - EnsureUnloaded(appDomain); - } - - fileChanged.Wait(); - _logger.InfoFormat("Script changed. Reloading...", _config.ScriptName); - } - } - } - - private void EnsureUnloaded(AppDomain domain) - { - try - { - _logger.DebugFormat("Unloading app domain '{0}'", _config.ScriptName); - AppDomain.Unload(domain); - } - catch (AppDomainUnloadedException ex) - { - _logger.DebugFormat("App domain '{0}' has already been unloaded.", ex, _config.ScriptName); - } - } - - public string[] ScriptArgs => this._scriptArgs; - } -} diff --git a/src/ScriptCs/Config.cs b/src/ScriptCs/Config.cs deleted file mode 100644 index f3cc226e..00000000 --- a/src/ScriptCs/Config.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using ScriptCs.Contracts; -using System.IO; - -namespace ScriptCs -{ - // NOTE (Adam): passed across app domains as a property of CrossAppDomainExecuteScriptCommand - [Serializable] - public class Config - { - private string[] _modules; - - public Config() - { - LogLevel = LogLevel.Info; - } - - // global - public LogLevel LogLevel { get; set; } - - public string[] Modules - { - get => _modules ?? new string[0]; - set => _modules = value; - } - - public string OutputFile { get; set; } - - // clean command - public bool Clean { get; set; } - - // install command - public bool AllowPreRelease { get; set; } - - public bool Global { get; set; } - - public string PackageName { get; set; } - - public string PackageVersion { get; set; } - - // save command - public bool Save { get; set; } - - // run command - public string ScriptName { get; set; } - - public bool Cache { get; set; } - - public bool Debug { get; set; } - - public bool Repl { get; set; } - - public bool Watch { get; set; } - - public string Eval { get; set; } - - public static Config Create(string maskFile, ConfigMask configMask) - { - Guard.AgainstNullArgument("configMask", configMask); - - return new Config() - .Apply(ConfigMask.ReadGlobalOrDefault()) - .Apply(maskFile == null ? ConfigMask.ReadLocalOrDefault() : ConfigMask.Read(maskFile)) - .Apply(configMask); - } - - public Config Apply(ConfigMask mask) - { - if (mask == null) - { - return this; - } - - var logLevel = mask.Debug.GetValueOrDefault() && !mask.LogLevel.HasValue && LogLevel != LogLevel.Trace - ? LogLevel.Debug - : mask.LogLevel; - - var modules = mask.Modules == null - ? null - : mask.Modules.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries); - - var scriptName = mask.ScriptName != null && !Path.GetFileName(mask.ScriptName).Contains(".") - ? Path.ChangeExtension(mask.ScriptName, "csx") - : mask.ScriptName; - - return new Config - { - LogLevel = logLevel ?? LogLevel, - _modules = modules ?? _modules, - OutputFile = mask.Output ?? OutputFile, - Clean = mask.Clean ?? Clean, - AllowPreRelease = mask.AllowPreRelease ?? AllowPreRelease, - Global = mask.Global ?? Global, - PackageName = mask.Install ?? PackageName, - PackageVersion = mask.PackageVersion ?? PackageVersion, - Save = mask.Save ?? Save, - Cache = mask.Cache ?? Cache, - Debug = mask.Debug ?? Debug, - Repl = mask.Repl ?? Repl, - ScriptName = scriptName ?? ScriptName, - Eval = mask.Eval ?? Eval, - Watch = mask.Watch ?? Watch, - }; - } - } -} diff --git a/src/ScriptCs/ConfigMask.cs b/src/ScriptCs/ConfigMask.cs deleted file mode 100644 index 53120ea4..00000000 --- a/src/ScriptCs/ConfigMask.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using Newtonsoft.Json; - -namespace ScriptCs -{ - using ScriptCs.Contracts; - - public class ConfigMask - { - public bool? AllowPreRelease { get; set; } - - public bool? Cache { get; set; } - - public bool? Clean { get; set; } - - public bool? Debug { get; set; } - - public bool? Global { get; set; } - - public string Install { get; set; } - - public LogLevel? LogLevel { get; set; } - - public string Modules { get; set; } - - public string Output { get; set; } - - public string PackageVersion { get; set; } - - public bool? Repl { get; set; } - - public bool? Save { get; set; } - - public string ScriptName { get; set; } - - public string Eval { get; set; } - - public bool? Watch { get; set; } - - public static ConfigMask ReadGlobalOrDefault() => Read(new FileSystem().GlobalOptsFile, true); - - public static ConfigMask ReadLocalOrDefault() => Read(Constants.ConfigFilename, true); - - public static ConfigMask Read(string path) => Read(path, false); - - private static ConfigMask Read(string path, bool defaultIfNotExists) - { - if (defaultIfNotExists && !File.Exists(path)) - { - return null; - } - - var json = File.ReadAllText(path); - try - { - return JsonConvert.DeserializeObject(json); - } - catch (Exception ex) - { - var message = string.Format( - CultureInfo.InvariantCulture, "Error reading JSON config from '{0}'.", path); - - throw new InvalidOperationException(message, ex); - } - } - } -} diff --git a/src/ScriptCs/ExecuteScriptCommandBase.cs b/src/ScriptCs/ExecuteScriptCommandBase.cs deleted file mode 100644 index 2c98b677..00000000 --- a/src/ScriptCs/ExecuteScriptCommandBase.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ScriptCs.Command; -using ScriptCs.Contracts; - -namespace ScriptCs -{ - public abstract class ExecuteScriptCommandBase - { - protected string Script { get; private set; } - protected IFileSystem FileSystem { get; private set; } - protected IScriptExecutor ScriptExecutor { get; private set; } - protected IScriptPackResolver _scriptPackResolver { get; private set; } - protected ILog Logger { get; private set; } - protected IAssemblyResolver AssemblyResolver { get; private set; } - protected IScriptLibraryComposer Composer { get; private set; } - - public ExecuteScriptCommandBase( - string script, - string[] scriptArgs, - IFileSystem fileSystem, - IScriptExecutor scriptExecutor, - IScriptPackResolver scriptPackResolver, - ILogProvider logProvider, - IAssemblyResolver assemblyResolver, - IScriptLibraryComposer composer - ) - { - Guard.AgainstNullArgument("fileSystem", fileSystem); - Guard.AgainstNullArgument("scriptExecutor", scriptExecutor); - Guard.AgainstNullArgument("scriptPackResolver", scriptPackResolver); - Guard.AgainstNullArgument("logProvider", logProvider); - Guard.AgainstNullArgument("assemblyResolver", assemblyResolver); - Guard.AgainstNullArgument("composer", composer); - - Script = script; - ScriptArgs = scriptArgs; - FileSystem = fileSystem; - ScriptExecutor = scriptExecutor; - _scriptPackResolver = scriptPackResolver; - Logger = logProvider.ForCurrentType(); - AssemblyResolver = assemblyResolver; - Composer = composer; - } - - public string[] ScriptArgs { get; private set; } - - public abstract CommandResult Execute(); - - protected CommandResult Inspect(ScriptResult result) - { - if (result == null) - { - return CommandResult.Error; - } - - if (result.CompileExceptionInfo != null) - { - var ex = result.CompileExceptionInfo.SourceException; - Logger.ErrorException("Script compilation failed.", ex); - return CommandResult.Error; - } - - if (result.ExecuteExceptionInfo != null) - { - var ex = result.ExecuteExceptionInfo.SourceException; - Logger.ErrorException("Script execution failed.", ex); - return CommandResult.Error; - } - - if (!result.IsCompleteSubmission) - { - Logger.Error("The script is incomplete."); - return CommandResult.Error; - } - - return CommandResult.Success; - } - } -} diff --git a/src/ScriptCs/ProfileOptimizationShim.cs b/src/ScriptCs/ProfileOptimizationShim.cs deleted file mode 100644 index ff83bffc..00000000 --- a/src/ScriptCs/ProfileOptimizationShim.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Reflection; - -namespace ScriptCs -{ - internal static class ProfileOptimizationShim - { - private static readonly Type profileOptimization = Type.GetType("System.Runtime.ProfileOptimization"); - - public static void SetProfileRoot(string directoryPath) - { - if (profileOptimization != null) - { - profileOptimization - .GetMethod("SetProfileRoot", BindingFlags.Public | BindingFlags.Static) - .Invoke(null, new object[] { directoryPath }); - } - } - - public static void StartProfile(string profile) - { - if (profileOptimization != null) - { - profileOptimization - .GetMethod("StartProfile", BindingFlags.Public | BindingFlags.Static) - .Invoke(null, new object[] { profile }); - } - } - } -} diff --git a/src/ScriptCs/Program.cs b/src/ScriptCs/Program.cs deleted file mode 100644 index 789353ee..00000000 --- a/src/ScriptCs/Program.cs +++ /dev/null @@ -1,114 +0,0 @@ -using McMaster.Extensions.CommandLineUtils; -using ScriptCs.Contracts; -using System; -using System.Diagnostics; -using System.IO; -using System.Linq; - -namespace ScriptCs -{ - internal static class Program - { - [LoaderOptimizationAttribute(LoaderOptimization.MultiDomain)] - private static int Main(string[] args) - { - //args = args.Skip(2).ToArray(); - ProfileOptimizationShim.SetProfileRoot(Path.GetDirectoryName(typeof(Program).Assembly.Location)); - ProfileOptimizationShim.StartProfile(typeof(Program).Assembly.GetName().Name + ".profile"); - - var nonScriptArgs = args.TakeWhile(arg => arg != "--").ToArray(); - var scriptArgs = args.Skip(nonScriptArgs.Length + 1).ToArray(); - - var app = new CommandLineApplication(throwOnUnexpectedArg: true) - { - ExtendedHelpText = "Usage: scriptcs options", - OptionsComparison = StringComparison.OrdinalIgnoreCase - }; - - var script = app.Argument("script", "Script file name, must be specified first"); - - var scriptNameFallback = app.Option("--scriptname | -script", "Alternative way to pass a script filename", CommandOptionType.NoValue); - var repl = app.Option("--repl | -r", "Launch REPL mode when running script. To just launch REPL, simply omit the 'script' argument", CommandOptionType.NoValue); - var eval = app.Option("--eval | -e", "Code to immediately evaluate", CommandOptionType.SingleValue); - var configFile = app.Option("--config | -co", "Defines config file name", CommandOptionType.SingleValue); - var debug = app.Option("--debug | -d", "Emits PDB symbols allowing for attaching a Visual Studio debugger", CommandOptionType.NoValue); - var version = app.Option("--version | -v", "Outputs version information", CommandOptionType.NoValue); - var cache = app.Option("--cache | -c", "Flag which determines whether to run in memory or from a .dll", CommandOptionType.NoValue); - var logLevel = app.Option("--loglevel | -log", "Flag which defines the log level used", CommandOptionType.SingleValue); - var watch = app.Option("--watch | -w", "Watch the script file and reload it when changed", CommandOptionType.NoValue); - var modules = app.Option("--modules | -m", "Specify modules to load (comma separated)", CommandOptionType.SingleValue); - var output = app.Option("--output | -o", "Write all console output to the specified file", CommandOptionType.SingleValue); - - app.HelpOption("-Help | -?"); - - app.Command("install", c => - { - var package = c.Argument("package", "Specific package to install, otherwise installs and restores packages which are specified in packages.config"); - var allowPrerelease = c.Option("--allowprerelease | -pre", "Allows installation of packages' prelease versions", CommandOptionType.NoValue); - var packageVersion = c.Option("--packageversion | -p", "Defines the version of the package to install", CommandOptionType.SingleValue); - var save = c.Option("--save | -s", "Creates a packages.config file based on the packages directory", CommandOptionType.NoValue); - var clean = c.Option("--clean | -cl", "Cleans installed packages from working directory", CommandOptionType.NoValue); - var global = c.Option("--global | -g", "Installs and restores global packages which are specified in packages.config", CommandOptionType.NoValue); - - c.OnExecute(() => - { - var configMask = new ConfigMask - { - Repl = false, - Global = global.HasValue() ? true : (bool?)null, - Install = package.Value ?? string.Empty, // needed to trigger install of all packages! - PackageVersion = packageVersion.Value(), - AllowPreRelease = allowPrerelease.HasValue() ? true : (bool?)null, - Save = save.HasValue() ? true : (bool?)null, - Clean = clean.HasValue() ? true : (bool?)null, - Output = output.Value(), - Debug = debug.HasValue() ? true : (bool?)null, - LogLevel = logLevel.ParsedValue - }; - - return Application.Run(Config.Create(configFile.Value(), configMask), scriptArgs); - }); - }); - - app.OnExecute(() => - { - if (configFile.HasValue() && !File.Exists(configFile.Value())) - { - Console.WriteLine("The specified config file does not exist."); - return 1; - } - - if (version.HasValue()) - { - VersionWriter.Write(); - return 0; - } - - var configMask = new ConfigMask - { - Repl = repl.HasValue() ? true : (bool?)null, - ScriptName = scriptNameFallback.HasValue() ? scriptNameFallback.Value() : script.Value, - Debug = debug.HasValue() ? true : (bool?)null, - Eval = eval.Value(), - Cache = cache.HasValue() ? true : (bool?)null, - Watch = watch.HasValue() ? true : (bool?)null, - LogLevel = logLevel.ParsedValue, - Modules = modules.Value(), - Output = output.Value() - }; - - return Application.Run(Config.Create(configFile.Value(), configMask), scriptArgs); - }); - - try - { - return app.Execute(nonScriptArgs); - } - catch (CommandParsingException) - { - app.ShowHelp(); - return 1; - } - } - } -} diff --git a/src/ScriptCs/Properties/AssemblyInfo.cs b/src/ScriptCs/Properties/AssemblyInfo.cs deleted file mode 100644 index 707f917b..00000000 --- a/src/ScriptCs/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.Reflection; -using System.Resources; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -[assembly: Guid("f624ee58-910a-4541-a46f-afcd3edca9df")] -[assembly: NeutralResourcesLanguage("en-US")] -[assembly: InternalsVisibleTo("ScriptCs.Tests")] diff --git a/src/ScriptCs/Properties/Settings.Designer.cs b/src/ScriptCs/Properties/Settings.Designer.cs deleted file mode 100644 index 3d2dfa89..00000000 --- a/src/ScriptCs/Properties/Settings.Designer.cs +++ /dev/null @@ -1,26 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace ScriptCs.Properties { - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.5.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default { - get { - return defaultInstance; - } - } - } -} diff --git a/src/ScriptCs/Properties/Settings.settings b/src/ScriptCs/Properties/Settings.settings deleted file mode 100644 index 049245f4..00000000 --- a/src/ScriptCs/Properties/Settings.settings +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/src/ScriptCs/Properties/chocolateyInstall.ps1 b/src/ScriptCs/Properties/chocolateyInstall.ps1 deleted file mode 100644 index 85c2846b..00000000 --- a/src/ScriptCs/Properties/chocolateyInstall.ps1 +++ /dev/null @@ -1,43 +0,0 @@ -try { - $tools = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)" - - if (Test-Path "$tools\..\lib") { - Remove-Item "$tools\..\lib" -Recurse -Force - } - - # Handle upgrade from previous packages that installed to the %AppData%/scriptcs folders. - $oldPaths = @( - "$env:APPDATA\scriptcs", - "$env:LOCALAPPDATA\scriptcs" - ) - - $oldPaths | foreach { - # Remove the old user-specific scriptcs folders. - if (Test-Path $_) { - Remove-Item $_ -Recurse -Force - } - - # Remove the user-specific path that got added in previous installs. - # There's no Uninstall-ChocolateyPath yet so we need to do it manually. - # https://github.com/chocolatey/chocolatey/issues/97 - $envPath = $env:PATH - if ($envPath.ToLower().Contains($_.ToLower())) { - $userPath = [Environment]::GetEnvironmentVariable("Path","User") - if($userPath) { - $actualPath = [System.Collections.ArrayList]($userPath).Split(";") - $actualPath.Remove($_) - $newPath = $actualPath -Join ";" - [Environment]::SetEnvironmentVariable("Path", $newPath, "User") - } - } - - Write-Host "'$_' has been removed." -ForegroundColor DarkYellow - } - Update-SessionEnvironment - # End upgrade handling. - - Write-ChocolateySuccess 'scriptcs' -} catch { - Write-ChocolateyFailure 'scriptcs' "$($_.Exception.Message)" - throw -} \ No newline at end of file diff --git a/src/ScriptCs/Properties/scriptcs.nuspec b/src/ScriptCs/Properties/scriptcs.nuspec deleted file mode 100644 index 610aa737..00000000 --- a/src/ScriptCs/Properties/scriptcs.nuspec +++ /dev/null @@ -1,24 +0,0 @@ - - - - scriptcs - $version$ - Glenn Block, Filip Wojcieszyn, Justin Rusbatch, Kristian Hellang, Damian Schenkelman, Adam Ralph - Glenn Block, Justin Rusbatch, Filip Wojcieszyn - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - Write .Net apps with a text editor, NuGet, and the power of Roslyn! - roslyn csx script scriptcs - - - - - - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs/ScriptCs.csproj b/src/ScriptCs/ScriptCs.csproj deleted file mode 100644 index 20e9f2dc..00000000 --- a/src/ScriptCs/ScriptCs.csproj +++ /dev/null @@ -1,29 +0,0 @@ - - - 1.0.0 - Exe - net461 - scriptcs - Glenn Block, Filip Wojcieszyn, Justin Rusbatch - https://github.com/scriptcs/scriptcs/blob/master/LICENSE.md - http://scriptcs.net - http://www.gravatar.com/avatar/5c754f646971d8bc800b9d4057931938.png?s=120 - scriptcs - Write .Net apps with a text editor, NuGet, and the power of Roslyn! - roslyn csx script scriptcs - scriptcs command line tool. - scriptcs - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/ScriptCs/ScriptServicesBuilderFactory.cs b/src/ScriptCs/ScriptServicesBuilderFactory.cs deleted file mode 100644 index c5450516..00000000 --- a/src/ScriptCs/ScriptServicesBuilderFactory.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.IO; -using ScriptCs.Contracts; -using ScriptCs.Hosting; - -namespace ScriptCs -{ - public static class ScriptServicesBuilderFactory - { - public static IScriptServicesBuilder Create(Config config, string[] scriptArgs) - { - Guard.AgainstNullArgument("commandArgs", config); - Guard.AgainstNullArgument("scriptArgs", scriptArgs); - - IConsole console = new ScriptConsole(); - if (!string.IsNullOrWhiteSpace(config.OutputFile)) - { - console = new FileConsole(config.OutputFile, console); - } - - var logProvider = new ColoredConsoleLogProvider(config.LogLevel, console); - var initializationServices = new InitializationServices(logProvider); - initializationServices.GetAppDomainAssemblyResolver().Initialize(); - - if (config.ScriptName != null && Path.GetFileName(config.ScriptName) != config.ScriptName) - { - var path = Path.GetFullPath(config.ScriptName); - initializationServices.GetFileSystem().CurrentDirectory = Path.GetDirectoryName(path); - config.ScriptName = path; - } - - // NOTE (adamralph): this is a hideous assumption about what happens inside the CommandFactory. - // It is a result of the ScriptServicesBuilderFactory also having to know what is going to happen inside the - // Command Factory so that it builds the builder(:-p) correctly in advance. - // This demonstrates the technical debt that exists with the ScriptServicesBuilderFactory and CommandFactory - // in their current form. We have a separate refactoring task raised to address this. - var repl = config.Repl || - (!config.Clean && config.PackageName == null && !config.Save && config.ScriptName == null); - - var scriptServicesBuilder = new ScriptServicesBuilder(console, logProvider, null, initializationServices) - .Cache(config.Cache) - .Debug(config.Debug) - .LogLevel(config.LogLevel) - .ScriptName(config.ScriptName) - .Repl(repl); - - return scriptServicesBuilder.LoadModules(Path.GetExtension(config.ScriptName) ?? ".csx", config.Modules); - } - } -} diff --git a/src/ScriptCs/VersionWriter.cs b/src/ScriptCs/VersionWriter.cs deleted file mode 100644 index e1bf4b36..00000000 --- a/src/ScriptCs/VersionWriter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Diagnostics; -using System.Linq; -using System.Text.RegularExpressions; - -namespace ScriptCs -{ - public static class VersionWriter - { - private static readonly string version = - FileVersionInfo.GetVersionInfo(typeof(VersionWriter).Assembly.Location).ProductVersion; - - private static readonly Regex colorRegex = new Regex( - @"\+(?\w*)(?(.*(?=\+))|.*)", RegexOptions.Compiled | RegexOptions.Singleline); - - public static void Write() - { - var lines = new[] - { - @"+cyan _ _", - @"+cyan ___ ___ _ __(_)_ __ | |__+darkMagenta ___ ___", - @"+cyan/ __|/ __| '__| | '_ \| __/+darkMagenta/ __/ __|", - @"+cyan\__ \ (__| | | | |_) | |_+darkMagenta| (__\__ \", - @"+cyan|___/\___|_| |_| .__/ \__\+darkMagenta\___|___/", - string.Format(@"+cyan |_|+white Version: {0}", version) - }; - - foreach (var lineMatches in lines.Select(line => colorRegex.Matches(line))) - { - foreach (Match match in lineMatches) - { - ConsoleColor color; - if (Enum.TryParse(match.Groups["color"].Value, true, out color)) - { - Console.ForegroundColor = color; - } - - try - { - Console.Write(match.Groups["ascii"].Value); - } - finally - { - Console.ResetColor(); - } - } - - Console.WriteLine(); - } - - Console.WriteLine(); - } - } -} diff --git a/src/ScriptCs/app.config b/src/ScriptCs/app.config deleted file mode 100644 index a61f4a50..00000000 --- a/src/ScriptCs/app.config +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/src/ScriptCs/scriptcs b/src/ScriptCs/scriptcs deleted file mode 100755 index 5f56d178..00000000 --- a/src/ScriptCs/scriptcs +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash -## Make sure to chmod +x this file! - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -mono "$DIR/scriptcs.exe" $@ \ No newline at end of file diff --git a/stylesheets/pygment_trac.css b/stylesheets/pygment_trac.css new file mode 100644 index 00000000..c6a6452d --- /dev/null +++ b/stylesheets/pygment_trac.css @@ -0,0 +1,69 @@ +.highlight { background: #ffffff; } +.highlight .c { color: #999988; font-style: italic } /* Comment */ +.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ +.highlight .k { font-weight: bold } /* Keyword */ +.highlight .o { font-weight: bold } /* Operator */ +.highlight .cm { color: #999988; font-style: italic } /* Comment.Multiline */ +.highlight .cp { color: #999999; font-weight: bold } /* Comment.Preproc */ +.highlight .c1 { color: #999988; font-style: italic } /* Comment.Single */ +.highlight .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */ +.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ +.highlight .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */ +.highlight .ge { font-style: italic } /* Generic.Emph */ +.highlight .gr { color: #aa0000 } /* Generic.Error */ +.highlight .gh { color: #999999 } /* Generic.Heading */ +.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ +.highlight .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */ +.highlight .go { color: #888888 } /* Generic.Output */ +.highlight .gp { color: #555555 } /* Generic.Prompt */ +.highlight .gs { font-weight: bold } /* Generic.Strong */ +.highlight .gu { color: #800080; font-weight: bold; } /* Generic.Subheading */ +.highlight .gt { color: #aa0000 } /* Generic.Traceback */ +.highlight .kc { font-weight: bold } /* Keyword.Constant */ +.highlight .kd { font-weight: bold } /* Keyword.Declaration */ +.highlight .kn { font-weight: bold } /* Keyword.Namespace */ +.highlight .kp { font-weight: bold } /* Keyword.Pseudo */ +.highlight .kr { font-weight: bold } /* Keyword.Reserved */ +.highlight .kt { color: #445588; font-weight: bold } /* Keyword.Type */ +.highlight .m { color: #009999 } /* Literal.Number */ +.highlight .s { color: #d14 } /* Literal.String */ +.highlight .na { color: #008080 } /* Name.Attribute */ +.highlight .nb { color: #0086B3 } /* Name.Builtin */ +.highlight .nc { color: #445588; font-weight: bold } /* Name.Class */ +.highlight .no { color: #008080 } /* Name.Constant */ +.highlight .ni { color: #800080 } /* Name.Entity */ +.highlight .ne { color: #990000; font-weight: bold } /* Name.Exception */ +.highlight .nf { color: #990000; font-weight: bold } /* Name.Function */ +.highlight .nn { color: #555555 } /* Name.Namespace */ +.highlight .nt { color: #000080 } /* Name.Tag */ +.highlight .nv { color: #008080 } /* Name.Variable */ +.highlight .ow { font-weight: bold } /* Operator.Word */ +.highlight .w { color: #bbbbbb } /* Text.Whitespace */ +.highlight .mf { color: #009999 } /* Literal.Number.Float */ +.highlight .mh { color: #009999 } /* Literal.Number.Hex */ +.highlight .mi { color: #009999 } /* Literal.Number.Integer */ +.highlight .mo { color: #009999 } /* Literal.Number.Oct */ +.highlight .sb { color: #d14 } /* Literal.String.Backtick */ +.highlight .sc { color: #d14 } /* Literal.String.Char */ +.highlight .sd { color: #d14 } /* Literal.String.Doc */ +.highlight .s2 { color: #d14 } /* Literal.String.Double */ +.highlight .se { color: #d14 } /* Literal.String.Escape */ +.highlight .sh { color: #d14 } /* Literal.String.Heredoc */ +.highlight .si { color: #d14 } /* Literal.String.Interpol */ +.highlight .sx { color: #d14 } /* Literal.String.Other */ +.highlight .sr { color: #009926 } /* Literal.String.Regex */ +.highlight .s1 { color: #d14 } /* Literal.String.Single */ +.highlight .ss { color: #990073 } /* Literal.String.Symbol */ +.highlight .bp { color: #999999 } /* Name.Builtin.Pseudo */ +.highlight .vc { color: #008080 } /* Name.Variable.Class */ +.highlight .vg { color: #008080 } /* Name.Variable.Global */ +.highlight .vi { color: #008080 } /* Name.Variable.Instance */ +.highlight .il { color: #009999 } /* Literal.Number.Integer.Long */ + +.type-csharp .highlight .k { color: #0000FF } +.type-csharp .highlight .kt { color: #0000FF } +.type-csharp .highlight .nf { color: #000000; font-weight: normal } +.type-csharp .highlight .nc { color: #2B91AF } +.type-csharp .highlight .nn { color: #000000 } +.type-csharp .highlight .s { color: #A31515 } +.type-csharp .highlight .sc { color: #A31515 } diff --git a/stylesheets/styles.css b/stylesheets/styles.css new file mode 100644 index 00000000..00625eee --- /dev/null +++ b/stylesheets/styles.css @@ -0,0 +1,264 @@ +@import url(https://fonts.googleapis.com/css?family=Lato:300italic,700italic,300,700); + +body { + padding:50px; + font:14px/1.5 Lato, "Helvetica Neue", Helvetica, Arial, sans-serif; + color:#777; + font-weight:300; +} + +h1, h2, h3, h4, h5, h6 { + color:#222; + margin:0 0 20px; +} + +p, ul, ol, table, pre, dl { + margin:0 0 20px; +} + +h1, h2, h3 { + line-height:1.1; +} + +h1 { + font-size:28px; +} + +h2 { + color:#393939; +} + +h3, h4, h5, h6 { + color:#494949; +} + +a { + color:#39c; + font-weight:400; + text-decoration:none; +} + +a small { + font-size:11px; + color:#777; + margin-top:-0.6em; + display:block; +} + +.wrapper { + width:860px; + margin:0 auto; +} + +blockquote { + border-left:1px solid #e5e5e5; + margin:0; + padding:0 0 0 20px; + font-style:italic; +} + +code, pre { + font-family:Monaco, Bitstream Vera Sans Mono, Lucida Console, Terminal; + color:#333; + font-size:12px; +} + +pre { + padding:8px 15px; + background: #f8f8f8; + border-radius:5px; + border:1px solid #e5e5e5; + overflow-x: auto; +} + +table { + width:100%; + border-collapse:collapse; +} + +th, td { + text-align:left; + padding:5px 10px; + border-bottom:1px solid #e5e5e5; +} + +dt { + color:#444; + font-weight:700; +} + +th { + color:#444; +} + +img { + max-width:100%; +} + +header { + width:270px; + float:left; + position:fixed; +} + +header ul { + list-style:none; + height:40px; + + padding:0; + + background: #eee; + background: -moz-linear-gradient(top, #f8f8f8 0%, #dddddd 100%); + background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f8f8f8), color-stop(100%,#dddddd)); + background: -webkit-linear-gradient(top, #f8f8f8 0%,#dddddd 100%); + background: -o-linear-gradient(top, #f8f8f8 0%,#dddddd 100%); + background: -ms-linear-gradient(top, #f8f8f8 0%,#dddddd 100%); + background: linear-gradient(top, #f8f8f8 0%,#dddddd 100%); + + border-radius:5px; + border:1px solid #d2d2d2; + box-shadow:inset #fff 0 1px 0, inset rgba(0,0,0,0.03) 0 -1px 0; + width:270px; +} + +header li { + width:89px; + float:left; + border-right:1px solid #d2d2d2; + height:40px; +} + +header ul a { + line-height:1; + font-size:11px; + color:#999; + display:block; + text-align:center; + padding-top:6px; + height:40px; +} + +strong { + color:#222; + font-weight:700; +} + +header ul li + li { + width:88px; + border-left:1px solid #fff; +} + +header ul li + li + li { + border-right:none; + width:89px; +} + +header ul a strong { + font-size:14px; + display:block; + color:#222; +} + +section { + width:500px; + float:right; + padding-bottom:50px; +} + +small { + font-size:11px; +} + +hr { + border:0; + background:#e5e5e5; + height:1px; + margin:0 0 20px; +} + +footer { + width:270px; + float:left; + position:fixed; + bottom:50px; +} + +#logo { + width: 230px; + height: 77px; +} + +.tagline { + font-weight: 700; +} + +@media print, screen and (max-width: 960px) { + + div.wrapper { + width:auto; + margin:0; + } + + header, section, footer { + float:none; + position:static; + width:auto; + } + + header { + padding-right:320px; + } + + section { + border:1px solid #e5e5e5; + border-width:1px 0; + padding:20px 0; + margin:0 0 20px; + } + + header a small { + display:inline; + } + + header ul { + position:absolute; + right:50px; + top:52px; + } +} + +@media print, screen and (max-width: 720px) { + body { + word-wrap:break-word; + } + + header { + padding:0; + } + + header ul, header p.view { + position:static; + } + + pre, code { + word-wrap:normal; + } +} + +@media print, screen and (max-width: 480px) { + body { + padding:15px; + } + + header ul { + display:none; + } +} + +@media print { + body { + padding:0.4in; + font-size:12pt; + color:#444; + } +} diff --git a/test/ScriptCs.Core.Tests/AppDomainAssemblyResolverTests.cs b/test/ScriptCs.Core.Tests/AppDomainAssemblyResolverTests.cs deleted file mode 100644 index a20b191d..00000000 --- a/test/ScriptCs.Core.Tests/AppDomainAssemblyResolverTests.cs +++ /dev/null @@ -1,285 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using Moq; -using ScriptCs.Contracts; -using Should; -using Xunit; -using Xunit.Extensions; -using AutoFixture.Xunit2; -using System.Threading; - -namespace ScriptCs.Tests -{ - public class AppDomainAssemblyResolverTests - { - static AppDomainAssemblyResolverTests() - { - _assemblyName = typeof(Mock).Assembly.GetName(); - _info = new AssemblyInfo { Path = _assemblyName.CodeBase.Substring(8).Replace('/', Path.DirectorySeparatorChar) }; - } - - private static AssemblyName _assemblyName; - private static AssemblyInfo _info; - - public class TheConstructor - { - [Theory(Skip = "AppDomain events are flaky here - needs to be reviewed"), ScriptCsAutoData] - public void ShouldSubscribeToTheResolveEvent( - TestLogProvider logProvider, - IFileSystem fileSystem, - IAssemblyResolver assemblyResolver, - [Frozen] Mock assemblyUtilityMock) - { - var autoResetEvent = new AutoResetEvent(false); - - assemblyUtilityMock.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true); - var assemblyUtility = assemblyUtilityMock.Object; - - new AppDomainAssemblyResolver(logProvider, fileSystem, assemblyResolver, - assemblyUtility, - resolveHandler: (o, r) => - { - autoResetEvent.Set(); - return Assembly.GetExecutingAssembly(); - } - ); - Assembly.Load("test"); - var called = autoResetEvent.WaitOne(1000); - called.ShouldBeTrue(); - } - } - - public class TheInitializeMethod - { - [Theory, ScriptCsAutoData] - public void ShouldAddHostAssemblyPaths( - [Frozen] Mock fileSystemMock, - Mock resolverMock) - { - var hostBin = "c:\test"; - var dll = "c:\test\test.dll"; - - fileSystemMock.Setup(fs => fs.EnumerateFiles(hostBin, "*.dll", SearchOption.TopDirectoryOnly)).Returns(new[] { dll }); - fileSystemMock.SetupGet(fs => fs.HostBin).Returns(hostBin); - resolverMock.Setup(r => r.AddAssemblyPaths(It.IsAny>())); - resolverMock.Object.Initialize(); - resolverMock.Verify(r => r.AddAssemblyPaths(It.Is>(paths => paths.Contains(dll)))); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddModuleAssemblyPaths( - [Frozen] Mock fileSystemMock, - [Frozen] Mock assemblyResolverMock, - Mock resolverMock) - { - var modulesFolder = "c:\test"; - var dll = "c:\test\test.dll"; - assemblyResolverMock.Setup(a => a.GetAssemblyPaths(modulesFolder, true)).Returns(new[] { dll }); - fileSystemMock.SetupGet(fs => fs.GlobalFolder).Returns(modulesFolder); - resolverMock.Setup(r => r.AddAssemblyPaths(It.IsAny>())); - resolverMock.Object.Initialize(); - resolverMock.Verify(r => r.AddAssemblyPaths(It.Is>(paths => paths.Contains(dll)))); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddScriptPackAssemblyPaths( - [Frozen] Mock fileSystemMock, - [Frozen] Mock assemblyResolverMock, - Mock resolverMock) - { - var scriptAssemblyPath = "c:\test"; - var dll = "c:\test\test.dll"; - assemblyResolverMock.Setup(a => a.GetAssemblyPaths(scriptAssemblyPath, true)).Returns(new[] { dll }); - fileSystemMock.SetupGet(fs => fs.CurrentDirectory).Returns(scriptAssemblyPath); - resolverMock.Setup(r => r.AddAssemblyPaths(It.IsAny>())); - resolverMock.Object.Initialize(); - resolverMock.Verify(r => r.AddAssemblyPaths(It.Is>(paths => paths.Contains(dll)))); - } - } - - public class TheAddAssemblyPathsMethod - { - //I can't use Autofixture's [Frozen] attribute here as there appears to be a bug. If you try to freeze a Mock> - //instead of a configurable Mock, an Dictionary<> is injected. I need to override TryGetValue for this test. - [Fact] - public void ShouldRetrieveTheMappedAssemblyInfo() - { - var assemblyUtilityMock = new Mock(); - var assemblyInfoMapMock = new Mock>(); - assemblyUtilityMock.Setup(u => u.IsManagedAssembly(It.IsAny())).Returns(true); - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - AssemblyInfo foundInfo = null; - assemblyInfoMapMock.Setup(m => m.TryGetValue(It.IsAny(), out foundInfo)).Returns(false); - - var resolver = new AppDomainAssemblyResolver( - new TestLogProvider(), - Mock.Of(), - Mock.Of(), - assemblyUtilityMock.Object, - assemblyInfoMapMock.Object - ); - - resolver.AddAssemblyPaths(new[] { _info.Path }); - assemblyInfoMapMock.Verify(m => m.TryGetValue(_assemblyName.Name, out foundInfo)); - } - - //Here I can use [Frozen] as I don't need to override members - [Theory, ScriptCsAutoData] - public void ShouldRegisterTheAssemblyIfTheAssemblyDoesNotExist( - [Frozen] Mock assemblyUtilityMock, - [Frozen] IDictionary assemblyInfoMap, - AppDomainAssemblyResolver resolver) - { - assemblyUtilityMock.Setup(u => u.IsManagedAssembly(It.IsAny())).Returns(true); - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - resolver.AddAssemblyPaths(new[] { _info.Path }); - assemblyInfoMap.ContainsKey(_assemblyName.Name).ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldOverrideIfTheAssemblyVersionIsGreaterThanTheMappedAssemblyAndItWasNotLoaded( - [Frozen] Mock assemblyUtilityMock, - [Frozen] IDictionary assemblyInfoMap, - AppDomainAssemblyResolver resolver) - { - assemblyUtilityMock.Setup(u => u.IsManagedAssembly(It.IsAny())).Returns(true); - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - _info.Version = new Version(0, 0); - resolver.AddAssemblyPaths(new[] { _info.Path }); - _info = assemblyInfoMap[_assemblyName.Name]; - _info.Version.ShouldEqual(_assemblyName.Version); - } - - [Theory, ScriptCsAutoData] - public void ShouldLogWhenTheAssemblyIsMapped( - [Frozen] Mock assemblyUtilityMock, - [Frozen] TestLogProvider logProvider, - AppDomainAssemblyResolver resolver) - { - assemblyUtilityMock.Setup(u => u.IsManagedAssembly(It.IsAny())).Returns(true); - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - resolver.AddAssemblyPaths(new[] { _info.Path }); - logProvider.Output.ShouldContain( - "DEBUG: Mapping Assembly " + _assemblyName.Name + " to version:" + _assemblyName.Version); - } - - [Theory, ScriptCsAutoData] - public void ShouldWarnIfTheAssemblyVersionIsGreaterThanTheMappedAssemblyAndItWasLoaded( - [Frozen] Mock assemblyUtilityMock, - [Frozen] IDictionary assemblyInfoMap, - [Frozen] TestLogProvider logProvider, - AppDomainAssemblyResolver resolver) - { - _info.Version = new Version(0, 0); - assemblyUtilityMock.Setup(u => u.IsManagedAssembly(It.IsAny())).Returns(true); - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - _info.Assembly = typeof(Mock).Assembly; - assemblyInfoMap[_assemblyName.Name] = _info; - resolver.AddAssemblyPaths(new[] { _info.Path }); - logProvider.Output.ShouldContain( - "WARN: Conflict: Assembly " + _info.Path + " with version " + _assemblyName.Version + - " cannot be added as it has already been resolved"); - } - - [Theory, ScriptCsAutoData] - public void ShouldNotOverrideIfTheAssemblyVersionIsGreaterThanTheMappedAssemblyAndItWasLoaded( - [Frozen] Mock assemblyUtilityMock, - [Frozen] IDictionary assemblyInfoMap, - AppDomainAssemblyResolver resolver) - { - _info.Version = new Version(0, 0); - _info.Assembly = typeof(Mock).Assembly; - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - assemblyInfoMap[_assemblyName.Name] = _info; - resolver.AddAssemblyPaths(new[] { _info.Path }); - var newInfo = assemblyInfoMap[_assemblyName.Name]; - newInfo.Version.ShouldEqual(_info.Version); - } - - [Theory, ScriptCsAutoData] - public void ShouldNotOverrideIfTheAssemblyVersionIsLessThenOrEqualToTheMappedAssembly( - [Frozen] Mock assemblyUtilityMock, - [Frozen] IDictionary assemblyInfoMap, - AppDomainAssemblyResolver resolver) - { - _info.Version = new Version(99, 0, 0); - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - assemblyInfoMap[_assemblyName.Name] = _info; - resolver.AddAssemblyPaths(new[] { _info.Path }); - var newInfo = assemblyInfoMap[_assemblyName.Name]; - newInfo.Version.ShouldEqual(_info.Version); - } - } - - public class TheAssemblyResolveMethod - { - [Fact] - public void ShouldRetrieveTheMappedAssemblyInfo() - { - var assemblyUtilityMock = new Mock(); - var assemblyInfoMapMock = new Mock>(); - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - AssemblyInfo foundInfo = null; - assemblyInfoMapMock.Setup(m => m.TryGetValue(It.IsAny(), out foundInfo)).Returns(false); - - var resolver = new AppDomainAssemblyResolver( - new TestLogProvider(), - Mock.Of(), - Mock.Of(), - assemblyUtilityMock.Object, - assemblyInfoMapMock.Object - ); - - resolver.AssemblyResolve(this, new ResolveEventArgs(_assemblyName.Name)); - assemblyInfoMapMock.Verify(m => m.TryGetValue(_assemblyName.Name, out foundInfo)); - } - - [Theory, ScriptCsAutoData] - public void ShouldLoadTheAssemblyIfTheMappedAssemblyInfoExistsAndItHasNotBeenLoaded( - [Frozen] Mock assemblyUtilityMock, - [Frozen] IDictionary assemblyInfoMap, - AppDomainAssemblyResolver resolver) - { - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - assemblyUtilityMock.Setup(u => u.LoadFile(_info.Path)).Returns(typeof(Mock).Assembly); - assemblyInfoMap[_assemblyName.Name] = _info; - var args = new ResolveEventArgs(_assemblyName.Name); - var assembly = resolver.AssemblyResolve(this, args); - assembly.ShouldEqual(_info.Assembly); - } - - [Theory, ScriptCsAutoData] - public void ShouldLogTheAssemblyThatIsBeingResolved( - [Frozen] Mock assemblyUtilityMock, - [Frozen] TestLogProvider logProvider, - [Frozen] IDictionary assemblyInfoMap, - AppDomainAssemblyResolver resolver) - { - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - assemblyUtilityMock.Setup(u => u.LoadFile(_info.Path)).Returns(typeof(Mock).Assembly); - assemblyInfoMap[_assemblyName.Name] = _info; - - resolver.AssemblyResolve(this, new ResolveEventArgs(_assemblyName.Name)); - logProvider.Output.ShouldContain( - "DEBUG: Resolving from: " + _assemblyName.Name + " to: " + _assemblyName.ToString()); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnTheAssemblyIfItWasLoaded( - [Frozen] Mock assemblyUtilityMock, - [Frozen] IDictionary assemblyInfoMap, - AppDomainAssemblyResolver resolver) - { - assemblyUtilityMock.Setup(u => u.GetAssemblyName(_info.Path)).Returns(_assemblyName); - assemblyUtilityMock.Setup(u => u.LoadFile(_info.Path)).Returns(typeof(Mock).Assembly); - assemblyInfoMap[_assemblyName.Name] = _info; - var args = new ResolveEventArgs(_assemblyName.Name); - var assembly = resolver.AssemblyResolve(this, args); - assembly.ShouldEqual(_info.Assembly); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/AssemblyResolverTests.cs b/test/ScriptCs.Core.Tests/AssemblyResolverTests.cs deleted file mode 100644 index 29addbe5..00000000 --- a/test/ScriptCs.Core.Tests/AssemblyResolverTests.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System.IO; -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class AssemblyResolverTests - { - public class GetAssemblyPathsMethod - { - [Theory, ScriptCsAutoData] - public void ShouldReturnAssembliesFromPackagesFolder( - [Frozen] Mock fileSystemMock, - [Frozen] Mock packageAssemblyResolverMock, - [Frozen] Mock assemblyUtilityMock, - AssemblyResolver resolver - ) - { - const string WorkingDirectory = @"C:\"; - - var packagesFolder = Path.Combine(WorkingDirectory, "packages"); - var assemblyFile = Path.Combine(packagesFolder, "MyAssembly.dll"); - - assemblyUtilityMock.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true); - fileSystemMock.Setup(x => x.DirectoryExists(packagesFolder)).Returns(true); - fileSystemMock.SetupGet(x => x.PackagesFolder).Returns("packages"); - fileSystemMock.SetupGet(x => x.BinFolder).Returns("bin"); - - packageAssemblyResolverMock.Setup(x => x.GetAssemblyNames(WorkingDirectory)).Returns(new[] { assemblyFile }); - - var assemblies = resolver.GetAssemblyPaths(WorkingDirectory).ToList(); - - assemblies.Count.ShouldEqual(1); - assemblies[0].ShouldEqual(assemblyFile); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnAssembliesFromBinFolder( - [Frozen] Mock fileSystemMock, - [Frozen] Mock assemblyUtilityMock, - AssemblyResolver resolver - ) - { - const string WorkingDirectory = @"C:\"; - - var binFolder = Path.Combine(WorkingDirectory, "bin"); - var assemblyFile = Path.Combine(binFolder, "MyAssembly.dll"); - - fileSystemMock.Setup(x => x.DirectoryExists(binFolder)).Returns(true); - fileSystemMock.SetupGet(x => x.PackagesFolder).Returns("packages"); - fileSystemMock.SetupGet(x => x.BinFolder).Returns("bin"); - fileSystemMock.Setup(x => x.EnumerateFiles(binFolder, It.IsAny(), SearchOption.TopDirectoryOnly)).Returns(new[] { assemblyFile }); - - assemblyUtilityMock.Setup(x => x.IsManagedAssembly(assemblyFile)).Returns(true); - - var assemblies = resolver.GetAssemblyPaths(WorkingDirectory).ToList(); - - assemblies.Count.ShouldEqual(1); - assemblies[0].ShouldEqual(assemblyFile); - } - - [Theory, ScriptCsAutoData] - public void ShouldNotReturnNonManagedAssemblies( - [Frozen] Mock fileSystemMock, - [Frozen] Mock assemblyUtilityMock, - AssemblyResolver resolver - ) - { - const string WorkingDirectory = @"C:\"; - - var binFolder = Path.Combine(WorkingDirectory, "bin"); - var managed = Path.Combine(binFolder, "MyAssembly.dll"); - var nonManaged = Path.Combine(binFolder, "MyNonManagedAssembly.dll"); - - fileSystemMock.Setup(x => x.DirectoryExists(binFolder)).Returns(true); - fileSystemMock.SetupGet(x => x.PackagesFolder).Returns("packages"); - fileSystemMock.SetupGet(x => x.BinFolder).Returns("bin"); - fileSystemMock.Setup(x => x.EnumerateFiles(binFolder, It.IsAny(), SearchOption.TopDirectoryOnly - )) - .Returns(new[] { managed, nonManaged }); - - assemblyUtilityMock.Setup(x => x.IsManagedAssembly(managed)).Returns(true); - assemblyUtilityMock.Setup(x => x.IsManagedAssembly(nonManaged)).Returns(false); - - var assemblies = resolver.GetAssemblyPaths(WorkingDirectory).ToList(); - - assemblies.Count.ShouldEqual(1); - assemblies[0].ShouldEqual(managed); - } - - [Theory, ScriptCsAutoData] - public void ShouldOnlyReturnBinariesWhenFlagIsSet( - [Frozen] Mock packageAssemblyResolverMock, - [Frozen] Mock fileSystemMock, - [Frozen] Mock assemblyUtilityMock, - AssemblyResolver resolver) - { - const string WorkingDirectory = @"C:\"; - - var binFolder = Path.Combine(WorkingDirectory, "bin"); - - assemblyUtilityMock.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true); - fileSystemMock.Setup(x => x.DirectoryExists(binFolder)).Returns(true); - fileSystemMock.Setup(x => x.DirectoryExists(@"C:\packages")).Returns(true); - fileSystemMock.SetupGet(x => x.PackagesFolder).Returns("packages"); - fileSystemMock.SetupGet(x => x.BinFolder).Returns("bin"); - fileSystemMock.Setup(x => x.EnumerateFiles(binFolder, It.IsAny(), SearchOption.AllDirectories)) - .Returns(Enumerable.Empty()); - - packageAssemblyResolverMock.Setup(p=>p.GetAssemblyNames(WorkingDirectory)).Returns(new string[] {"test.dll", "test.exe", "test.foo"}); - - var assemblies = resolver.GetAssemblyPaths(WorkingDirectory, true).ToList(); - assemblies.ShouldNotContain("test.foo"); - assemblies.ShouldContain("test.dll"); - assemblies.ShouldContain("test.exe"); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/AssemblyUtilityTests.cs b/test/ScriptCs.Core.Tests/AssemblyUtilityTests.cs deleted file mode 100644 index 4e1a8834..00000000 --- a/test/ScriptCs.Core.Tests/AssemblyUtilityTests.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class AssemblyUtilityTests - { - public class TheIsManagedAssemblyMethod - { - private readonly IAssemblyUtility _assemblyUtility = new AssemblyUtility(); - - [Fact] - public void ShouldReturnFalseWhenThePathDoesNotPointToAManagedAssembly() - { - _assemblyUtility.IsManagedAssembly("ScriptCs.Core.Tests.dll.config").ShouldBeFalse(); - } - - [Fact] - public void ShouldReturnTrueWhenThePathPointsToAManagedAssembly() - { - _assemblyUtility.IsManagedAssembly(typeof(String).Assembly.Location).ShouldBeTrue(); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/DirectiveLineProcessorTests.cs b/test/ScriptCs.Core.Tests/DirectiveLineProcessorTests.cs deleted file mode 100644 index c95f84d0..00000000 --- a/test/ScriptCs.Core.Tests/DirectiveLineProcessorTests.cs +++ /dev/null @@ -1,102 +0,0 @@ -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class DirectiveLineProcessorTests - { - public class TheProcessLineMethod - { - [Fact] - public void ShouldReturnTrueButNotContinueProcessingDirectiveIfAfterCodeByDefault() - { - var directiveLineProcessor = new TestableDirectiveLineProcessor(); - var returnValue = directiveLineProcessor.ProcessLine(null, null, "#Test x", false); - returnValue.ShouldBeTrue(); - directiveLineProcessor.InheritedProcessLineCalled.ShouldBeFalse(); - } - - [Fact] - public void ShouldReturnTrueAndContinueProcessingDirectiveIfAfterCodeAndBehaviourAfterCodeIsAllow() - { - var directiveLineProcessor = new TestableDirectiveLineProcessor(BehaviorAfterCode.Allow); - var returnValue = directiveLineProcessor.ProcessLine(null, null, "#Test x", false); - returnValue.ShouldBeTrue(); - directiveLineProcessor.InheritedProcessLineCalled.ShouldBeTrue(); - } - } - - public class TheMatchesMethod - { - [Fact] - public void ShouldReturnTrueWhenLineMatchesDirectiveStringWithAnArgument() - { - var directiveLineProcessor = new TestableDirectiveLineProcessor(); - directiveLineProcessor.Matches("#Test argument").ShouldBeTrue(); - } - - [Fact] - public void ShouldReturnTrueWhenLineMatchesDirectiveStringWithoutAnArgument() - { - var directiveLineProcessor = new TestableDirectiveLineProcessor(); - directiveLineProcessor.Matches("#Test").ShouldBeTrue(); - } - - [Fact] - public void ShouldReturnFalseWhenLineDoesNotMatchDirectiveStringWithAnArgument() - { - var directiveLineProcessor = new TestableDirectiveLineProcessor(); - directiveLineProcessor.Matches("#NotATest argument").ShouldBeFalse(); - } - - [Fact] - public void ShouldReturnFalseWhenLineDoesNotMatchDirectiveStringWithoutAnArgument() - { - var directiveLineProcessor = new TestableDirectiveLineProcessor(); - directiveLineProcessor.Matches("#NotATest").ShouldBeFalse(); - } - } - - public class TheGetDirectiveArgumentMethod - { - [Fact] - public void ShouldParseTheArgumentFromTheDirectiveLine() - { - var directiveLineProcessor = new TestableDirectiveLineProcessor(BehaviorAfterCode.Allow); - directiveLineProcessor.ProcessLine(null, null, "#Test argument", false); - directiveLineProcessor.ArgumentParsedCorrectly.ShouldBeTrue(); - } - } - - public class TestableDirectiveLineProcessor : DirectiveLineProcessor - { - private BehaviorAfterCode? _behaviourAfterCode; - public TestableDirectiveLineProcessor() - { } - public TestableDirectiveLineProcessor(BehaviorAfterCode behaviourAfterCode) - { - _behaviourAfterCode = behaviourAfterCode; - } - protected override BehaviorAfterCode BehaviorAfterCode - { - get - { - return _behaviourAfterCode ?? base.BehaviorAfterCode; - } - } - protected override string DirectiveName - { - get { return "Test"; } - } - public bool ArgumentParsedCorrectly { get; private set; } - public bool InheritedProcessLineCalled { get; private set; } - protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line) - { - ArgumentParsedCorrectly = GetDirectiveArgument(line) == "argument"; - InheritedProcessLineCalled = true; - return true; - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/FileProcessorTests.cs b/test/ScriptCs.Core.Tests/FileProcessorTests.cs deleted file mode 100644 index 179a849d..00000000 --- a/test/ScriptCs.Core.Tests/FileProcessorTests.cs +++ /dev/null @@ -1,728 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.Contracts; -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.Contracts.Exceptions; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class FileProcessorTests - { - public class TheParseFileMethod - { - private readonly Mock _fileSystem; - - public TheParseFileMethod() - { - _fileSystem = new Mock(); - _fileSystem.SetupGet(x => x.NewLine).Returns(Environment.NewLine); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == @"c:\test\main.csx"))) - .Returns(new string[] {"main"}); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == @"c:\test\child.csx"))) - .Returns(new string[] {"child"}); - - _fileSystem.Setup(fs => fs.GetFullPath(It.IsAny())).Returns((path) => path); - } - - [Fact] - public void SetsTheScriptPath() - { - var path = @"c:\test\main.csx"; - var processor = GetFilePreProcessor(); - var context = new FileParserContext(); - processor.ParseFile(path, context); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(f => f == path)), Times.Exactly(1)); - context.ScriptPath.ShouldEqual(path); - } - - [Fact] - public void AddsLoadedScripts() - { - var path = @"c:\test\child.csx"; - var processor = GetFilePreProcessor(); - var context = new FileParserContext(); - context.ScriptPath = @"c:\test\main.csx"; - processor.ParseFile(path, context); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(f => f == path)), Times.Exactly(1)); - context.LoadedScripts.ShouldContain(path); - } - - private IFilePreProcessor GetFilePreProcessor() - { - return new FilePreProcessor(_fileSystem.Object, new TestLogProvider(), Enumerable.Empty()); - } - } - - public class ProcessFileMethod - { - private List _file1 = new List - { - @"#!/usr/bin/env scriptcs", - @"#load ""script2.csx""", - @"#load ""script4.csx"";", - "using System;", - @"Console.WriteLine(""Hello Script 1"");", - @"Console.WriteLine(""Loading Script 2"");", - @"Console.WriteLine(""Loaded Script 2"");", - @"Console.WriteLine(""Loading Script 4"");", - @"Console.WriteLine(""Loaded Script 4"");", - @"Console.WriteLine(""Goodbye Script 1"");" - }; - - private List _file2 = new List - { - "using System;", - @"Console.WriteLine(""Hello Script 2"");", - @"Console.WriteLine(""Goodbye Script 2"");" - }; - - private readonly List _file3 = new List - { - "using System;", - "using System.Collections.Generic;", - @"Console.WriteLine(""Hello Script 3"");", - @"Console.WriteLine(""Goodbye Script 3"");" - }; - - private readonly List _file4 = new List - { - "using System;", - "using System.Core;", - @"Console.WriteLine(""Hello Script 4"");", - @"Console.WriteLine(""Goodbye Script 4"");" - }; - - private readonly Mock _fileSystem; - - public ProcessFileMethod() - { - _fileSystem = new Mock(); - _fileSystem.SetupGet(x => x.NewLine).Returns(Environment.NewLine); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script1.csx"))) - .Returns(_file1.ToArray()); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script2.csx"))) - .Returns(_file2.ToArray()); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script3.csx"))) - .Returns(_file3.ToArray()); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script4.csx"))) - .Returns(_file4.ToArray()); - - _fileSystem.Setup(fs => fs.GetFullPath(It.IsAny())).Returns((path) => path); - } - - [Fact] - public void MultipleUsingStatementsShouldProduceDistinctOutput() - { - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - result.Code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); - - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i.StartsWith("script"))), Times.Exactly(3)); - result.Namespaces.Count.ShouldEqual(2); - } - - [Fact] - public void UsingStateMentsShouldAllBeAtTheTop() - { - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - var splitOutput = result.Code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); - var lastUsing = splitOutput.ToList().FindLastIndex(x => x.TrimStart(' ').StartsWith("using ")); - var firsNotUsing = splitOutput.ToList().FindIndex(x => !x.TrimStart(' ').StartsWith("using ")); - - Assert.True(lastUsing < firsNotUsing); - } - - [Fact] - public void ShouldNotLoadInlineLoads() - { - var processor = GetFilePreProcessor(); - processor.ProcessFile("script1.csx"); - - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script1.csx")), Times.Once()); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script2.csx")), Times.Once()); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script3.csx")), Times.Never()); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script4.csx")), Times.Once()); - } - - [Fact] - public void ShouldReturnResultWithAllLoadedFiles() - { - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - result.LoadedScripts.Count.ShouldEqual(2); - result.LoadedScripts.ShouldContain("script2.csx"); - result.LoadedScripts.ShouldContain("script4.csx"); - } - - [Fact] - public void ShouldReturnResultWithAllUsings() - { - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - result.Namespaces.Count.ShouldEqual(2); - result.Namespaces.ShouldContain("System"); - result.Namespaces.ShouldContain("System.Core"); - } - - [Fact] - public void ShouldReturnResultWithAllReferences() - { - var file1 = new List - { - @"#r ""My.dll""", - @"#load ""scriptX.csx""", - "using System;", - @"Console.WriteLine(""Hi!"");" - }; - - var file2 = new List - { - @"#r ""My2.dll""", - "using System;", - @"Console.WriteLine(""Hi!"");" - }; - - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script1.csx"))).Returns(file1.ToArray()); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "scriptX.csx"))).Returns(file2.ToArray()); - - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - result.References.Count.ShouldEqual(2); - result.References.ShouldContain("My.dll"); - result.References.ShouldContain("My2.dll"); - } - - [Fact] - public void ShouldNotIncludeReferencesInCode() - { - var file1 = new List - { - @"#r ""My.dll""", - @"#load ""scriptX.csx""", - "using System;", - @"Console.WriteLine(""Hi!"");" - }; - - var file2 = new List - { - @"#r ""My2.dll""", - "using System;", - @"Console.WriteLine(""Hi!"");" - }; - - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script1.csx"))).Returns(file1.ToArray()); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "scriptX.csx"))).Returns(file2.ToArray()); - - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - result.Code.ShouldNotContain("#r"); - } - - [Fact] - public void ShouldNotIncludeShebangsInCode() - { - var file1 = new List - { - @"#!/usr/bin/env scriptcs", - "using System;", - @"Console.WriteLine(""Hi!"");" - }; - - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script1.csx"))).Returns(file1.ToArray()); - - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - result.Code.ShouldNotContain("#!/usr/bin/env"); - } - - [Fact] - public void ShouldNotLoadSameFileTwice() - { - var file = new List - { - @"#load ""script4.csx""", - "using System;", - @"Console.WriteLine(""Hello Script 2"");", - }; - - var fs = new Mock(); - fs.Setup(i => i.NewLine).Returns(Environment.NewLine); - fs.Setup(x => x.ReadFileLines(It.Is(f => f == "script2.csx"))) - .Returns(file.ToArray()); - fs.Setup(x => x.ReadFileLines(It.Is(f => f == "script4.csx"))) - .Returns(_file4.ToArray()); - - var processor = GetFilePreProcessor(); - processor.ProcessFile("script1.csx"); - - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script1.csx")), Times.Once()); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script2.csx")), Times.Once()); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script3.csx")), Times.Never()); - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script4.csx")), Times.Once()); - } - - [Fact] - public void LoadBeforeUsingShouldBeAllowed() - { - var file = new List - { - @"#load ""script4.csx""", - string.Empty, - "using System;", - @"Console.WriteLine(""abc"");" - }; - - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "file.csx"))).Returns(file.ToArray()); - - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("file.csx"); - - var splitOutput = result.Code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); - var lastUsing = splitOutput.ToList().FindLastIndex(x => x.TrimStart(' ').StartsWith("using ")); - var firsNotUsing = splitOutput.ToList().FindIndex(x => !x.TrimStart(' ').StartsWith("using ")); - - splitOutput.Count(x => x.TrimStart(' ').StartsWith("using ")).ShouldEqual(0); - result.Namespaces.ShouldContain("System"); - Assert.True(lastUsing < firsNotUsing); - } - - [Fact] - public void ShouldNotThrowStackOverflowExceptionOnLoadLoop() - { - var a = new List { "#load B.csx" }; - var b = new List { "#load A.csx" }; - - _fileSystem.Setup(x => x.ReadFileLines("A.csx")).Returns(a.ToArray()); - _fileSystem.Setup(x => x.ReadFileLines("B.csx")).Returns(b.ToArray()); - - var ex = Record.Exception(() => GetFilePreProcessor().ProcessFile("A.csx")); - Assert.Null(ex); - } - - [Fact] - public void ShouldNotBeAllowedToLoadAfterUsing() - { - var file = new List - { - "using System;", - @"Console.WriteLine(""abc"");", - @"#load ""script4.csx""" - }; - - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "file.csx"))).Returns(file.ToArray()); - - var processor = GetFilePreProcessor(); - Assert.Throws(() => processor.ProcessFile("file.csx")); - - _fileSystem.Verify(x => x.ReadFileLines(It.Is(i => i == "script4.csx")), Times.Never()); - } - - [Fact] - public void UsingInCodeDoesNotCountAsUsingImport() - { - var file = new List - { - @"#load ""script4.csx""", - string.Empty, - "using System;", - "using System.IO;", - "Console.WriteLine();", - @"using (var stream = new MemoryStream) {", - @"//do stuff", - @"}" - }; - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "file.csx"))).Returns(file.ToArray()); - - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("file.csx"); - - var splitOutput = result.Code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); - var firstNonImportUsing = - splitOutput.ToList().FindIndex(x => x.TrimStart(' ').StartsWith("using ") && !x.Contains(";")); - var firstCodeLine = splitOutput.ToList().FindIndex(x => x.Contains("Console")); - - Assert.True(firstNonImportUsing > firstCodeLine); - } - - [Fact] - public void ShouldHaveReferencesFromAllFiles() - { - var file1 = new List - { - @"#r ""My.dll""", - @"#load ""scriptX.csx""", - "using System;", - @"Console.WriteLine(""Hi!"");" - }; - - var file2 = new List - { - @"#r ""My2.dll""", - "using System;", - @"Console.WriteLine(""Hi!"");" - }; - - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "script1.csx"))) - .Returns(file1.ToArray()); - _fileSystem.Setup(x => x.ReadFileLines(It.Is(f => f == "scriptX.csx"))) - .Returns(file2.ToArray()); - - var processor = GetFilePreProcessor(); - var result = processor.ProcessFile("script1.csx"); - - result.References.Count.ShouldEqual(2); - } - - [Fact] - public void ShouldAddLineDirectiveRightAfterLastLoadIsIncludedInEachFile() - { - // f1 has usings and then loads - var f1 = new List - { - @"#load ""C:\f2.csx"";", - @"#load ""C:\f3.csx"";", - "using System;", - "using System.Diagnostics;", - @"Console.WriteLine(""First line of f1"");", - }; - - // f2 has no usings and multiple loads - var f2 = new List - { - @"#load ""C:\f4.csx"";", - @"#load ""C:\f5.csx"";", - @"Console.WriteLine(""First line of f2"");", - }; - - // f3 has usings and no loads - var f3 = new List - { - @"using System;", - @"using System.Diagnostics;", - @"Console.WriteLine(""First line of f3"");", - }; - - // f4 has no usings and no loads - var f4 = new List - { - @"Console.WriteLine(""First line of f4"");", - }; - - // f5 is no special case, just used to be loaded - var f5 = new List - { - @"using System;", - @"Console.WriteLine(""First line of f5"");", - }; - - _fileSystem.SetupGet(fs => fs.NewLine).Returns(Environment.NewLine); - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\f1.csx")) - .Returns(f1.ToArray()); - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\f2.csx")) - .Returns(f2.ToArray()).Verifiable(); - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\f3.csx")) - .Returns(f3.ToArray()); - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\f4.csx")) - .Returns(f4.ToArray()); - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\f5.csx")) - .Returns(f5.ToArray()); - _fileSystem.Setup(fs => fs.IsPathRooted(It.IsAny())).Returns(true); - - var preProcessor = GetFilePreProcessor(); - - var result = preProcessor.ProcessFile(@"C:\f1.csx"); - - var fileLines = result.Code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); - - // using should not be here, so only f4 -> f5 -> f2 -> f3 -> f1 - var line = 0; - - fileLines[line++].ShouldEqual(@"#line 1 ""C:\f4.csx"""); - fileLines[line++].ShouldEqual(f4[0]); - - fileLines[line++].ShouldEqual(@"#line 2 ""C:\f5.csx"""); - fileLines[line++].ShouldEqual(f5[1]); - - fileLines[line++].ShouldEqual(@"#line 3 ""C:\f2.csx"""); - fileLines[line++].ShouldEqual(f2[2]); - - fileLines[line++].ShouldEqual(@"#line 3 ""C:\f3.csx"""); - fileLines[line++].ShouldEqual(f3[2]); - - fileLines[line++].ShouldEqual(@"#line 5 ""C:\f1.csx"""); - fileLines[line].ShouldEqual(f1[4]); - - result.Namespaces.Count.ShouldEqual(2); - } - - [Fact] - public void ShouldLoadNestedScriptcsRelativeToScriptLocation() - { - // f1 has usings and then loads - var f1 = new List - { - @"#load ""SubFolder\f2.csx"";", - @"#load ""SubFolder\f3.csx"";", - @"using System;", - @"Console.WriteLine(""First line of f1"");" - }; - - // f2 has no usings and multiple loads - var f2 = new List - { - @"#load ""f3.csx"";", - @"using System;", - @"Console.WriteLine(""First line of f2"");" - }; - - // f3 has usings and no loads - var f3 = new List - { - @"using System;", - @"Console.WriteLine(""First line of f3"");" - }; - - var currentDirectory = "c:\\"; - _fileSystem.SetupGet(y => y.CurrentDirectory).Returns(() => currentDirectory); - _fileSystem.SetupSet(fs => fs.CurrentDirectory = It.IsAny()) - .Callback((newCurrentDirectory) => - { - currentDirectory = newCurrentDirectory; - }); - - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\f1.csx")) - .Returns(f1.ToArray()); - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\SubFolder\f2.csx")) - .Returns(f2.ToArray()); - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\SubFolder\f3.csx")) - .Returns(f3.ToArray()); - - _fileSystem.Setup(fs => fs.GetFullPath(@"C:\f1.csx")).Returns(@"C:\f1.csx"); - _fileSystem.Setup(fs => fs.GetFullPath(@"SubFolder\f2.csx")).Returns(@"C:\SubFolder\f2.csx"); - _fileSystem.Setup(fs => fs.GetFullPath(@"f3.csx")).Returns(@"C:\SubFolder\f3.csx"); - _fileSystem.Setup(fs => fs.GetFullPath(@"SubFolder\f3.csx")).Returns(@"C:\SubFolder\f3.csx"); - - _fileSystem.Setup(fs => fs.GetWorkingDirectory(@"C:\f1.csx")).Returns(@"C:\"); - _fileSystem.Setup(fs => fs.GetWorkingDirectory(@"C:\SubFolder\f2.csx")).Returns(@"C:\SubFolder\"); - _fileSystem.Setup(fs => fs.GetWorkingDirectory(@"C:\SubFolder\f3.csx")).Returns(@"C:\SubFolder\"); - - var preProcessor = GetFilePreProcessor(); - - preProcessor.ProcessFile(@"C:\f1.csx"); - - _fileSystem.Verify(fs => fs.ReadFileLines(@"C:\f1.csx"), Times.Once()); - _fileSystem.Verify(fs => fs.ReadFileLines(@"C:\SubFolder\f2.csx"), Times.Once()); - _fileSystem.Verify(fs => fs.ReadFileLines(@"C:\SubFolder\f3.csx"), Times.Once()); - } - - [Fact] - public void ShouldResetTheCurrentDirectoryWhenLoadingScript() - { - // f1 has usings and then loads - var f1 = new List - { - @"using System;", - @"Console.WriteLine(""First line of f1"");" - }; - - var startingDirectory = "c:\\"; - var currentDirectory = startingDirectory; - var lastCurrentDirectory = string.Empty; - _fileSystem.SetupGet(y => y.CurrentDirectory).Returns(() => currentDirectory); - _fileSystem.SetupSet(fs => fs.CurrentDirectory = It.IsAny()) - .Callback((newCurrentDirectory) => - { - lastCurrentDirectory = newCurrentDirectory; - currentDirectory = newCurrentDirectory; - }); - - _fileSystem.Setup(fs => fs.ReadFileLines(@"C:\SubFolder\f1.csx")) - .Returns(f1.ToArray()); - _fileSystem.Setup(fs => fs.GetFullPath(@"C:\SubFolder\f1.csx")).Returns(@"C:\SubFolder\f1.csx"); - _fileSystem.Setup(fs => fs.GetWorkingDirectory(@"C:\SubFolder\f1.csx")).Returns(@"C:\SubFolder\"); - - var preProcessor = GetFilePreProcessor(); - - preProcessor.ProcessFile(@"C:\f1.csx"); - - lastCurrentDirectory.ShouldBeSameAs(startingDirectory); - } - - private IFilePreProcessor GetFilePreProcessor() - { - var lineProcessors = new ILineProcessor[] - { - new UsingLineProcessor(), - new ReferenceLineProcessor(_fileSystem.Object), - new LoadLineProcessor(_fileSystem.Object), - new ShebangLineProcessor() - }; - - return new FilePreProcessor(_fileSystem.Object, new TestLogProvider(), lineProcessors); - } - } - - public class ProcessScriptMethod - { - private readonly Mock _fileSystem; - - public ProcessScriptMethod() - { - _fileSystem = new Mock(); - _fileSystem.SetupGet(x => x.NewLine).Returns(Environment.NewLine); - _fileSystem.Setup(fs => fs.GetFullPath(It.IsAny())).Returns((path) => path); - } - - [Fact] - public void ShouldSplitScriptIntoLines() - { - var preProcessor = GetFilePreProcessor(); - var script = @"Console.WriteLine(""Testing..."");"; - - preProcessor.ProcessScript(script); - - _fileSystem.Verify(x => x.SplitLines(script), Times.Once()); - } - - [Fact] - public void ShouldNotReadFromFile() - { - var preProcessor = GetFilePreProcessor(); - var script = @"Console.WriteLine(""Testing..."");"; - - preProcessor.ProcessScript(script); - - _fileSystem.Verify(x => x.ReadFileLines(It.IsAny()), Times.Never()); - } - - [Fact] - public void ShouldNotIncludeLineDirectiveForRootScript() - { - var script1 = new List { @"Console.WriteLine(""Hello from script1.csx""" }; - _fileSystem.Setup(x => x.ReadFileLines("script1.csx")).Returns(script1.ToArray()); - _fileSystem.Setup(x => x.SplitLines(It.IsAny())) - .Returns(x => x.Split(new[] { Environment.NewLine }, StringSplitOptions.None)); - - var preProcessor = GetFilePreProcessor(); - var script = @"#load script1.csx"; - - var result = preProcessor.ProcessScript(script); - var fileLines = result.Code.Split(new[] { Environment.NewLine }, StringSplitOptions.None); - - fileLines.Count(x => x.StartsWith("#line ")).ShouldEqual(1); - } - - private IFilePreProcessor GetFilePreProcessor() - { - var lineProcessors = new ILineProcessor[] - { - new UsingLineProcessor(), - new ReferenceLineProcessor(_fileSystem.Object), - new LoadLineProcessor(_fileSystem.Object), - new ShebangLineProcessor() - }; - - return new FilePreProcessor(_fileSystem.Object, new TestLogProvider(), lineProcessors); - } - } - - public class TheParseScriptMethod - { - private readonly Mock _fileSystem; - - public TheParseScriptMethod() - { - _fileSystem = new Mock(); - _fileSystem.SetupGet(x => x.NewLine).Returns(Environment.NewLine); - _fileSystem.Setup(fs => fs.GetFullPath(It.IsAny())).Returns((path) => path); - } - - [Fact] - public void ShouldAllowSingleLineCommentsBeforeDirectives() - { - var testableDirectiveProcessor = new DirectiveLineProcessorTests.TestableDirectiveLineProcessor(BehaviorAfterCode.Throw); - var filePreprocessor = GetFilePreProcessor(testableDirectiveProcessor, new LoadLineProcessor(_fileSystem.Object)); - var lines = new List - { - "//Test Comment", - "#Test something", - "Console.WriteLine(\"Success\");" - }; - - var ex = Record.Exception(() => filePreprocessor.ParseScript(lines, new FileParserContext())); - Assert.Null(ex); - } - - [Fact] - public void ShouldProcessCustomDirectiveWhenItComesBeforeCode() - { - var testableDirectiveProcessor = new DirectiveLineProcessorTests.TestableDirectiveLineProcessor(); - var filePreprocessor = GetFilePreProcessor(testableDirectiveProcessor, new LoadLineProcessor(_fileSystem.Object)); - var lines = new List - { - "#Test something", - "Console.WriteLine(\"Success\");" - }; - - filePreprocessor.ParseScript(lines, new FileParserContext()); - testableDirectiveProcessor.InheritedProcessLineCalled.ShouldBeTrue(); - } - - [Fact] - public void ShouldProcessALoadDirectiveWhenItComesAfterACustomDirective() - { - var testableDirectiveProcessor = new DirectiveLineProcessorTests.TestableDirectiveLineProcessor(); - var loadLineProcessor = new TestableLoadLineProcessor(_fileSystem.Object); - var filePreprocessor = GetFilePreProcessor(testableDirectiveProcessor, loadLineProcessor); - var lines = new List - { - "#Test something", - "#load myscript.csx", - "Console.WriteLine(\"Success\");" - }; - - filePreprocessor.ParseScript(lines, new FileParserContext()); - loadLineProcessor.InheritedProcessLineCalled.ShouldBeTrue(); - } - - private IFilePreProcessor GetFilePreProcessor(ILineProcessor customDirectiveProcessor, ILineProcessor loadLineProcessor) - { - var lineProcessors = new ILineProcessor[] - { - new UsingLineProcessor(), - new ReferenceLineProcessor(_fileSystem.Object), - loadLineProcessor, - new ShebangLineProcessor(), - customDirectiveProcessor - }; - - return new FilePreProcessor(_fileSystem.Object, new TestLogProvider(), lineProcessors); - } - - public class TestableLoadLineProcessor : LoadLineProcessor - { - public TestableLoadLineProcessor(IFileSystem fileSystem) - : base(fileSystem) - { } - public bool InheritedProcessLineCalled { get; private set; } - protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line) - { - InheritedProcessLineCalled = true; - return true; - } - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/FileSystemTests.cs b/test/ScriptCs.Core.Tests/FileSystemTests.cs deleted file mode 100644 index f53667f3..00000000 --- a/test/ScriptCs.Core.Tests/FileSystemTests.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -using Should; - -using Xunit; - -namespace ScriptCs.Tests -{ - public class FileSystemTests - { - public class GetWorkingDirectoryMethod - { - private readonly FileSystem _fileSystem = new FileSystem(); - - [Fact] - public void ShouldProperlyConstructWorkingDirectoryIfScriptIsRunFromRelativePath() - { - const string PathToMyScriptFolder = @"..\my_script"; - - try - { - Directory.CreateDirectory(PathToMyScriptFolder); - - _fileSystem.GetWorkingDirectory(PathToMyScriptFolder) - .ShouldEqual(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathToMyScriptFolder))); - } - finally - { - if (Directory.Exists(PathToMyScriptFolder)) - { - Directory.Delete(PathToMyScriptFolder); - } - } - } - - [Fact] - public void ShouldReturnWorkingDirectoryIfPathIsInvalid() - { - var invalidPaths = new List { string.Empty, " ", null }; - - foreach (var invalidPath in invalidPaths) - { - _fileSystem.GetWorkingDirectory(invalidPath).ShouldEqual(_fileSystem.CurrentDirectory); - } - } - - [Fact] - public void ReturnsCorrectWorkingDirectory() - { - var workingPath = string.Format(@".{0}working_dir", Path.DirectorySeparatorChar); - var existingPath = string.Format(@".{0}existing_dir{0}", Path.DirectorySeparatorChar); - var existingFile = string.Format(@".{0}existing_file.txt", Path.DirectorySeparatorChar); - - string workingDir = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, workingPath)); - string existingDirectoryPath = Path.GetFullPath(Path.Combine(workingDir, existingPath)); - string existingFilePath = Path.GetFullPath(Path.Combine(workingDir, existingFile)); - - try - { - Directory.CreateDirectory(workingDir); - Directory.CreateDirectory(existingDirectoryPath); - File.Create(existingFilePath).Dispose(); - - _fileSystem.GetWorkingDirectory(existingDirectoryPath).ShouldEqual(existingDirectoryPath); - _fileSystem.GetWorkingDirectory(existingFilePath).ShouldEqual( - Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, workingDir))); - } - finally - { - if (Directory.Exists(existingDirectoryPath)) - { - Directory.Delete(existingDirectoryPath); - } - - if (File.Exists(existingFilePath)) - { - File.Delete(existingFilePath); - } - - if (Directory.Exists(workingDir)) - { - Directory.Delete(workingDir); - } - } - } - - [Fact] - public void ReturnsCorrectWorkingDirectoryIfPathDoesNotExist() - { - var tempPath = Path.GetTempPath(); - - var nonExistantFilePath = Path.Combine(tempPath, "i_dont_exist.txt"); - - _fileSystem.GetWorkingDirectory(nonExistantFilePath) - .ShouldEqual(tempPath.TrimEnd(Path.DirectorySeparatorChar)); - } - } - - public class SplitLinesMethod - { - private readonly FileSystem _fileSystem = new FileSystem(); - - [Fact] - public void ReturnsCorrectLines() - { - var fileContentsLin = "using System;\nusing System.IO;"; - var fileContentsWin = "using System;\r\nusing System.IO;"; - - _fileSystem.SplitLines(fileContentsLin).Count().ShouldEqual(2); - _fileSystem.SplitLines(fileContentsWin).Count().ShouldEqual(2); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/LoadLineProcessorTests.cs b/test/ScriptCs.Core.Tests/LoadLineProcessorTests.cs deleted file mode 100644 index 6a37a696..00000000 --- a/test/ScriptCs.Core.Tests/LoadLineProcessorTests.cs +++ /dev/null @@ -1,115 +0,0 @@ -using System; -using Moq; -using ScriptCs.Contracts; -using Should; -using Should.Core.Assertions; -using ScriptCs.Contracts.Exceptions; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class LoadLineProcessorTests - { - public class TheProcessLineMethod : IDisposable - { - private const string EnvVarKey = "scriptcs"; - - private const string EnvVarValue = "Awesomeness!"; - - public TheProcessLineMethod() - { - Environment.SetEnvironmentVariable(EnvVarKey, EnvVarValue); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnTrueOnLoadLine(IFileParser parser, LoadLineProcessor processor) - { - // Arrange - const string Line = @"#load ""script.csx"""; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), Line, true); - - // Assert - result.ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnFalseOtherwise(IFileParser parser, LoadLineProcessor processor) - { - // Arrange - const string Line = @"var x = new Test();"; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), Line, true); - - // Assert - result.ShouldBeFalse(); - } - - [Theory, ScriptCsAutoData] - public void ShouldThrowExceptionIfAfterCode( - [Frozen] Mock parser, - [Frozen] Mock fileSystem, - LoadLineProcessor processor) - { - // Arrange - var context = new FileParserContext(); - - const string RelativePath = "..\\script.csx"; - const string Line = @"#load " + RelativePath; - const string FullPath = "C:\\script.csx"; - - fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath); - - // Act / Assert - Xunit.Assert.Throws(() => processor.ProcessLine(parser.Object, context, Line, false)); - } - - [Theory, ScriptCsAutoData] - public void ShouldParseLoadedFile( - [Frozen] Mock parser, - [Frozen] Mock fileSystem, - LoadLineProcessor processor) - { - // Arrange - var context = new FileParserContext(); - - const string RelativePath = "..\\script.csx"; - const string Line = @"#load " + RelativePath; - const string FullPath = "C:\\script.csx"; - - fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath); - - // Act - processor.ProcessLine(parser.Object, context, Line, true); - - // Assert - parser.Verify(x => x.ParseFile(FullPath, It.IsAny())); - } - - [Theory, ScriptCsAutoData] - public void ShouldExpandEnvironmentVariables( - [Frozen] Mock fileSystem, - LoadLineProcessor processor, - IFileParser parser) - { - // Arrange - var context = new FileParserContext(); - var line = string.Format("#load %{0}%", EnvVarKey); - - // Act - processor.ProcessLine(parser, context, line, true); - - // Assert - fileSystem.Verify(x => x.GetFullPath(EnvVarValue)); - } - - public void Dispose() - { - Environment.SetEnvironmentVariable(EnvVarKey, null); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/PackageAssemblyResolverTests.cs b/test/ScriptCs.Core.Tests/PackageAssemblyResolverTests.cs deleted file mode 100644 index 89ad9385..00000000 --- a/test/ScriptCs.Core.Tests/PackageAssemblyResolverTests.cs +++ /dev/null @@ -1,387 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.Versioning; -using Moq; -using NuGet; -using ScriptCs.Contracts; -using Should; -using Xunit; -using IFileSystem = ScriptCs.Contracts.IFileSystem; - -namespace ScriptCs.Tests -{ - public class PackageAssemblyResolverTests - { - public class GetAssemblyNamesMethod - { - private readonly TestLogProvider _logProvider=new TestLogProvider(); - private readonly Mock _filesystem; - private readonly Mock _package; - private readonly Mock _packageContainer; - private readonly Mock _assemblyUtility; - private readonly List _packageIds; - private readonly string _workingDirectory; - - public GetAssemblyNamesMethod() - { - _workingDirectory = Path.GetTempPath(); - - _filesystem = new Mock(); - - _filesystem.SetupGet(i => i.CurrentDirectory).Returns(_workingDirectory); - _filesystem.SetupGet(i => i.PackagesFile).Returns("packages.config"); - _filesystem.SetupGet(i => i.PackagesFolder).Returns("packages"); - - _filesystem.Setup(i => i.DirectoryExists(It.IsAny())).Returns(true); - _filesystem.Setup(i => i.FileExists(It.IsAny())).Returns(true); - - _package = new Mock(); - _package.Setup(i => i.GetCompatibleDlls(It.IsAny())) - .Returns(new List { "test.dll", "test2.dll" }); - _package.SetupGet(i => i.Id).Returns("id"); - _package.SetupGet(i => i.Version).Returns(new Version("3.0")); - _package.SetupGet(i => i.TextVersion).Returns("3.0"); - _package.SetupGet(i => i.FullName).Returns(_package.Object.Id + "." + _package.Object.Version); - _package.SetupGet(i => i.FrameworkAssemblies).Returns(new[] { "System.Net.Http", "System.ComponentModel" }); - - _packageIds = new List - { - new PackageReference("testId", VersionUtility.ParseFrameworkName("net40"), new Version("3.0")) - }; - - _packageContainer = new Mock(); - _packageContainer.Setup(i => i.FindReferences(It.IsAny())).Returns(_packageIds); - _packageContainer.Setup(i => i.FindPackage(It.IsAny(), It.IsAny())).Returns(_package.Object); - - _assemblyUtility = new Mock(); - _assemblyUtility.Setup(u => u.IsManagedAssembly(It.IsAny())).Returns(true); - } - - [Fact] - public void WhenManyPackagesAreMatchedAllMatchingDllsWithUniquePathsShouldBeReturned() - { - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - _packageIds.Add(new PackageReference("testId2", VersionUtility.ParseFrameworkName("net40"), new Version("3.0"))); - - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(4); - } - - [Fact] - public void WhenManyPackagesAreMatchedAllMatchingDllsShouldBeReturned() - { - _packageIds.Add(new PackageReference("testId2", VersionUtility.ParseFrameworkName("net40"), new Version("3.0"))); - var p = new Mock(); - p.Setup(i => i.GetCompatibleDlls(It.IsAny())) - .Returns(new List { "test3.dll", "test4.dll" }); - p.SetupGet(i => i.Id).Returns("testId2"); - p.SetupGet(i => i.Version).Returns(new Version("3.0")); - p.SetupGet(i => i.TextVersion).Returns("3.0"); - p.SetupGet(i => i.FullName).Returns(_package.Object.Id + "." + _package.Object.Version); - - _packageContainer.Setup(i => i.FindPackage(It.IsAny(), It.Is(x => x.PackageId == "testId2"))).Returns(p.Object); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(6); - } - - [Fact] - public void WhenPackageIsMatchedItsNonMatchingDllsShouldBeExcluded() - { - _package.Setup( - i => - i.GetCompatibleDlls( - It.Is(x => x.FullName == VersionUtility.ParseFrameworkName("net40").FullName))) - .Returns(new List { "test.dll" }); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(3); - } - - [Fact] - public void WhenManyPackagesAreMatchedAllNonMatchingDllsShouldBeExcluded() - { - _package.Setup( - i => - i.GetCompatibleDlls( - It.Is(x => x.FullName == VersionUtility.ParseFrameworkName("net40").FullName))) - .Returns(new List { "test.dll" }); - _packageIds.Add(new PackageReference("testId2", VersionUtility.ParseFrameworkName("net40"), new Version("3.0"))); - - var p = new Mock(); - p.Setup(i => i.GetCompatibleDlls(It.IsAny())) - .Returns(new List { "test3.dll" }); - p.SetupGet(i => i.Id).Returns("testId2"); - p.SetupGet(i => i.Version).Returns(new Version("3.0")); - p.SetupGet(i => i.TextVersion).Returns("3.0"); - p.SetupGet(i => i.FullName).Returns(_package.Object.Id + "." + _package.Object.Version); - - _packageContainer.Setup(i => i.FindPackage(It.IsAny(), It.Is(x => x.PackageId == "testId2"))).Returns(p.Object); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(4); - } - - [Fact] - public void WhenDllsAreMatchedDllFilePathsAreCorrectlyConcatenated() - { - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - var found = resolver.GetAssemblyNames(_workingDirectory); - - found.First().ShouldEqual( - Path.Combine(_workingDirectory, "packages", "id.3.0", "test.dll")); - found.ElementAt(1).ShouldEqual( - Path.Combine(_workingDirectory, "packages", "id.3.0", "test2.dll")); - } - - [Fact] - public void WhenNoPackagesAreFoundShouldLogWarning() - { - // arrange - _packageContainer.Setup(i => i.FindPackage(It.IsAny(), It.IsAny())) - .Returns>(null); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - // act - resolver.GetAssemblyNames(_workingDirectory); - - // assert - _logProvider.Output.ShouldContain("WARN: Cannot find: testId 3.0"); - } - - [Fact] - public void WhenPackagesAreFoundButNoMatchingDllsExistShouldLogWarning() - { - // arrange - _package.Setup(i => i.GetCompatibleDlls(It.IsAny())).Returns>(null); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - // act - resolver.GetAssemblyNames(_workingDirectory); - - // assert - _logProvider.Output.ShouldContain( - "WARN: Cannot find compatible binaries for .NETFramework,Version=v4.0 in: testId 3.0"); - } - - [Fact] - public void WhenPackageDirectoryDoesNotExistShouldReturnEmptyPackagesList() - { - var resolver = new PackageAssemblyResolver(_filesystem.Object, new Mock().Object, _logProvider, _assemblyUtility.Object); - _filesystem.Setup(i => i.DirectoryExists(It.IsAny())).Returns(false); - - var found = resolver.GetAssemblyNames(_workingDirectory); - found.ShouldBeEmpty(); - } - - [Fact] - public void WhenPackagesConfigDoesNotExistShouldReturnEmptyPackagesList() - { - var resolver = new PackageAssemblyResolver(_filesystem.Object, new Mock().Object, _logProvider, _assemblyUtility.Object); - _filesystem.Setup(i => i.FileExists(It.IsAny())).Returns(false); - - var found = resolver.GetAssemblyNames(_workingDirectory); - found.ShouldBeEmpty(); - } - - [Fact] - public void ShouldLoadAllDependenciesIfPackageHasAny() - { - var p = new Mock(); - p.Setup(i => i.GetCompatibleDlls(It.IsAny())) - .Returns(new List { "test3.dll", "test4.dll" }); - p.SetupGet(i => i.Id).Returns("p2"); - p.SetupGet(i => i.Version).Returns(new Version("4.0")); - p.SetupGet(i => i.TextVersion).Returns("4.0"); - p.SetupGet(i => i.FullName).Returns(_package.Object.Id + "." + _package.Object.Version); - - _package.Setup(i => i.Dependencies).Returns(new List { p.Object }); - _packageContainer.Setup( - i => i.FindPackage(It.IsAny(), It.Is(x => x.PackageId == "p2"))) - .Returns(p.Object); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - _packageContainer.Verify(i => i.FindPackage(It.IsAny(), It.IsAny()), Times.Exactly(2)); - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(6); - } - - [Fact] - public void ShouldNotLoadDuplicateDependencies() - { - var p = new Mock(); - p.Setup(i => i.GetCompatibleDlls(It.IsAny())) - .Returns(new List { "test3.dll", "test4.dll" }); - p.SetupGet(i => i.Id).Returns("p2"); - p.SetupGet(i => i.Version).Returns(new Version("4.0")); - p.SetupGet(i => i.TextVersion).Returns("4.0"); - p.SetupGet(i => i.FullName).Returns(_package.Object.Id + "." + _package.Object.Version); - - _packageIds.Add(new PackageReference("p2", VersionUtility.ParseFrameworkName("net40"), new Version("3.0"))); - - _package.Setup(i => i.Dependencies).Returns(new List { p.Object }); - _packageContainer.Setup( - i => i.FindPackage(It.IsAny(), It.Is(x => x.PackageId == "p2"))) - .Returns(p.Object); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - _packageContainer.Verify(i => i.FindPackage(It.IsAny(), It.IsAny()), Times.Exactly(2)); - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(6); - } - - [Fact] - public void ShouldNotLoadIncompatibleDependenciesDlls() - { - var p = new Mock(); - p.Setup(i => i.GetCompatibleDlls(It.IsAny())) - .Returns(new List()); - p.SetupGet(i => i.Id).Returns("p2"); - p.SetupGet(i => i.Version).Returns(new Version("4.0")); - p.SetupGet(i => i.TextVersion).Returns("4.0"); - p.SetupGet(i => i.FullName).Returns(_package.Object.Id + "." + _package.Object.Version); - - _package.Setup(i => i.Dependencies).Returns(new List { p.Object }); - _packageContainer.Setup( - i => i.FindPackage(It.IsAny(), It.Is(x => x.PackageId == "p2"))) - .Returns(p.Object); - - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - _packageContainer.Verify(i => i.FindPackage(It.IsAny(), It.IsAny()), Times.Exactly(2)); - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(4); - } - - [Fact] - public void ShouldIgnoreUnmanagedAssemblies() - { - var resolver = new PackageAssemblyResolver(_filesystem.Object, _packageContainer.Object, _logProvider, _assemblyUtility.Object); - _packageIds.Add(new PackageReference("testId2", VersionUtility.ParseFrameworkName("net40"), new Version("3.0"))); - _assemblyUtility.Setup(u => u.IsManagedAssembly(It.Is(s => Path.GetFileName(s) == "test.dll"))).Returns(false); - - var found = resolver.GetAssemblyNames(_workingDirectory).ToList(); - - found.ShouldNotBeEmpty(); - found.Count.ShouldEqual(3); - } - } - - public class GetPackagesMethod - { - private Mock _fs; - private Mock _pc; - private TestLogProvider _logProvider = new TestLogProvider(); - private Mock _assemblyUtility; - - public GetPackagesMethod() - { - _fs = new Mock(); - _fs.SetupGet(f => f.PackagesFolder).Returns("packages"); - _fs.SetupGet(f => f.PackagesFile).Returns("packages.config"); - - _pc = new Mock(); - _assemblyUtility = new Mock(); - } - - [Fact] - public void ShouldReturnEmptyIfPackagesConfigDoesNotExist() - { - _fs.Setup(i => i.FileExists(It.IsAny())).Returns(false); - - var resolver = new PackageAssemblyResolver(_fs.Object, _pc.Object, _logProvider, _assemblyUtility.Object); - var result = resolver.GetPackages(@"c:/"); - - result.ShouldBeEmpty(); - } - - [Fact] - public void ShouldGetReferencesToPackages() - { - _fs.Setup(i => i.FileExists(It.IsAny())).Returns(true); - _pc.Setup(i => i.FindReferences(It.IsAny())).Returns(new List { new PackageReference("id", VersionUtility.ParseFrameworkName("net40"), new Version("3.0")) }); - - var resolver = new PackageAssemblyResolver(_fs.Object, _pc.Object, _logProvider, _assemblyUtility.Object); - var result = resolver.GetPackages(@"c:/"); - - _pc.Verify(i => i.FindReferences(It.IsAny()), Times.Once()); - result.Count().ShouldEqual(1); - } - } - - public class SavePackagesMethod - { - private Mock _fs; - private Mock _pc; - private readonly TestLogProvider _logProvider = new TestLogProvider(); - private Mock _assemblyUtility; - - public SavePackagesMethod() - { - _fs = new Mock(); - _fs.SetupGet(f => f.PackagesFile).Returns("packages.config"); - _fs.SetupGet(f => f.PackagesFolder).Returns("packages"); - - _pc = new Mock(); - _assemblyUtility = new Mock(); - } - - private IPackageAssemblyResolver GetResolver() - { - return new PackageAssemblyResolver(_fs.Object, _pc.Object, _logProvider, _assemblyUtility.Object); - } - - [Fact] - public void ShouldNotSaveWhenThereIsNoPackagesFolder() - { - _fs.Setup(i => i.CurrentDirectory).Returns("C:/"); - _fs.Setup(i => i.FileExists(It.IsAny())).Returns(false); - _fs.Setup(i => i.DirectoryExists(It.IsAny())).Returns(false); - var resolver = GetResolver(); - - resolver.SavePackages(); - - _pc.Verify(i => i.CreatePackageFile(), Times.Never()); - _logProvider.Output.ShouldContain("WARN: Packages directory does not exist!"); - } - - [Fact] - public void ShouldSaveWhenThereIsNoPackagesConfigAndThereIsPackagesFolder() - { - _fs.Setup(i => i.CurrentDirectory).Returns("C:/"); - _fs.Setup(i => i.DirectoryExists(It.IsAny())).Returns(true); - var resolver = GetResolver(); - - resolver.SavePackages(); - - _pc.Verify(i => i.CreatePackageFile(), Times.Once()); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/PackageReferenceTests.cs b/test/ScriptCs.Core.Tests/PackageReferenceTests.cs deleted file mode 100644 index 89c044b9..00000000 --- a/test/ScriptCs.Core.Tests/PackageReferenceTests.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Runtime.Versioning; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class PackageReferenceTests - { - public class TheConstructor - { - [Theory] - [InlineData(null, null, null)] - [InlineData("", null, null)] - [InlineData(" ", null, null)] - [InlineData("1.0.1", "1.0.1", null)] - [InlineData("1.0.1-alpha", "1.0.1", "alpha")] - [InlineData("1.0.1-beta-2", "1.0.1", "beta-2")] - public void ParsesVersionAndSpecialVersion( - string version, string expectedVersion, string expectedSpecialVersion) - { - // Arrange, Act - var packageReference = new PackageReference( - "packageId", new FrameworkName(".NETFramework,Version=v4.0"), version); - - // Assert - packageReference.Version.ShouldEqual(string.IsNullOrWhiteSpace(expectedVersion) - ? new Version() - : new Version(expectedVersion)); - - packageReference.SpecialVersion.ShouldEqual(expectedSpecialVersion); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ReferenceLineProcessorTests.cs b/test/ScriptCs.Core.Tests/ReferenceLineProcessorTests.cs deleted file mode 100644 index cdf606a4..00000000 --- a/test/ScriptCs.Core.Tests/ReferenceLineProcessorTests.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using Moq; -using ScriptCs.Contracts; -using Should; -using System.Linq; -using Should.Core.Assertions; -using ScriptCs.Contracts.Exceptions; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class ReferenceLineProcessorTests - { - public class TheProcessLineMethod : IDisposable - { - private const string EnvVarKey = "scriptcs"; - - private const string EnvVarValue = "Awesomeness!"; - - public TheProcessLineMethod() - { - Environment.SetEnvironmentVariable(EnvVarKey, EnvVarValue); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnTrueOnReferenceLine(IFileParser parser, ReferenceLineProcessor processor) - { - // Arrange - const string Line = @"#r ""MyDll.dll"""; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), Line, true); - - // Assert - result.ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnFalseOtherwise(IFileParser parser, ReferenceLineProcessor processor) - { - // Arrange - const string Line = @"var x = new Test();"; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), Line, true); - - // Assert - result.ShouldBeFalse(); - } - - [Theory, ScriptCsAutoData] - public void ShouldThrowExceptionIfAfterCode( - [Frozen] Mock fileSystem, - ReferenceLineProcessor processor, - IFileParser parser) - { - // Arrange - var context = new FileParserContext(); - - const string RelativePath = "..\\script.csx"; - const string Line = @"#r " + RelativePath; - const string FullPath = "C:\\script.csx"; - - fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath); - - // Act / Assert - Xunit.Assert.Throws(() => processor.ProcessLine(parser, context, Line, false)); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddReferenceToContext( - [Frozen] Mock fileSystem, - ReferenceLineProcessor processor, - IFileParser parser) - { - // Arrange - var context = new FileParserContext(); - - const string RelativePath = "..\\Assembly.dll"; - const string Line = @"#r " + RelativePath; - const string FullPath = "C:\\Assembly.dll"; - - fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath); - - // Act - processor.ProcessLine(parser, context, Line, true); - - // Assert - context.References.Count.ShouldEqual(1); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddReferenceByNameFromGACIfLocalFileDoesntExist( - [Frozen] Mock fileSystem, - ReferenceLineProcessor processor, - IFileParser parser) - { - // Arrange - var context = new FileParserContext(); - - var name = "script.csx"; - var line = @"#r " + name; - var fullPath = "C:\\script.csx"; - - fileSystem.Setup(x => x.GetFullPath(name)).Returns(fullPath); - fileSystem.Setup(x => x.FileExists(fullPath)).Returns(false); - - // Act - processor.ProcessLine(parser, context, line, true); - - // Assert - context.References.Count(x => x == name).ShouldEqual(1); - } - - [Theory, ScriptCsAutoData] - public void ShouldExpandEnvironmentVariables( - [Frozen] Mock fileSystem, - ReferenceLineProcessor processor, - IFileParser parser) - { - // Arrange - var context = new FileParserContext(); - var line = string.Format("#r %{0}%", EnvVarKey); - - // Act - processor.ProcessLine(parser, context, line, true); - - // Assert - fileSystem.Verify(x => x.GetFullPath(EnvVarValue)); - } - - public void Dispose() - { - Environment.SetEnvironmentVariable(EnvVarKey, null); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/AliasCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/AliasCommandTests.cs deleted file mode 100644 index 7b6203c8..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/AliasCommandTests.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Should; -using Xunit; -using Xunit.Extensions; - -namespace ScriptCs.Tests.ReplCommands -{ - public class AliasCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ShouldReturnAlias() - { - // act - var cmd = new AliasCommand(new Mock().Object); - - // assert - cmd.CommandName.ShouldEqual("alias"); - } - } - - public class ExecuteMethod - { - [Theory, ScriptCsAutoData] - public void ShouldAliasCommandWithNewName( - Mock fileSystem, - Mock engine, - Mock serializer, - TestLogProvider logProvider, - Mock composer, - Mock console, - Mock filePreProcessor) - { - // arrange - var currentDir = @"C:\"; - var dummyCommand = new Mock(); - dummyCommand.Setup(x => x.CommandName).Returns("foo"); - - fileSystem.Setup(x => x.BinFolder).Returns(Path.Combine(currentDir, "bin")); - fileSystem.Setup(x => x.DllCacheFolder).Returns(Path.Combine(currentDir, "cache")); - - var executor = new Repl( - new string[0], - fileSystem.Object, - engine.Object, - serializer.Object, - logProvider, - composer.Object, - console.Object, - filePreProcessor.Object, - new List { dummyCommand.Object }, - new Printers(serializer.Object), - new ScriptInfo()); - - var cmd = new AliasCommand(console.Object); - - // act - cmd.Execute(executor, new[] { "foo", "bar" }); - - // assert - executor.Commands.Count.ShouldEqual(2); - executor.Commands["bar"].ShouldBeSameAs(executor.Commands["foo"]); - } - - [Fact] - public void ShouldNotThrowAnExceptionWhenAnUnknownCommandIsPassed() - { - // arrange - var command = new AliasCommand(new Mock().Object); - - // act - var exception = Record.Exception(() => command.Execute(new Mock().Object, null)); - - // assert - exception.ShouldBeNull(); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ReplCommands/CdCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/CdCommandTests.cs deleted file mode 100644 index a7c7e1e8..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/CdCommandTests.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.IO; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class CdCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsCd() - { - // act - var cmd = new CdCommand(); - - // assert - Assert.Equal("cd", cmd.CommandName); - } - } - - public class ExecuteMethod - { - [Fact] - public void ChangesRelativePathBasedOnArg() - { - // arrange - var fs = new Mock(); - var repl = new Mock(); - - var tempPath = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar); - - fs.Setup(x => x.CurrentDirectory).Returns(Path.Combine(tempPath, "dir")); - repl.Setup(x => x.FileSystem).Returns(fs.Object); - - var cmd = new CdCommand(); - - // act - cmd.Execute(repl.Object, new[] { ".." }); - - // assert - fs.VerifySet(x => x.CurrentDirectory = tempPath, Times.Once()); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/ClearCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/ClearCommandTests.cs deleted file mode 100644 index e6b1f383..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/ClearCommandTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class ClearCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsClear() - { - // act - var cmd = new ClearCommand(new Mock().Object); - - // assert - Assert.Equal("clear", cmd.CommandName); - } - } - - public class ExecuteMethod - { - [Fact] - public void CallsConsoleClear() - { - // arrange - var console = new Mock(); - var cmd = new ClearCommand(console.Object); - - // act - cmd.Execute(new Mock().Object, null); - - // assert - console.Verify(x => x.Clear(), Times.Once); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/CwdCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/CwdCommandTests.cs deleted file mode 100644 index 3124ced0..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/CwdCommandTests.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.IO; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class CwdCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsCwd() - { - // act - var cmd = new CwdCommand(new Mock().Object); - - // assert - Assert.Equal("cwd", cmd.CommandName); - } - } - - public class ExecuteMethod - { - [Fact] - public void PrintsCurrentWorkingDirectoryToConsole() - { - // arrange - var console = new Mock(); - var fs = new Mock(); - var repl = new Mock(); - - var tempPath = Path.GetTempPath(); - - fs.Setup(x => x.CurrentDirectory).Returns(tempPath); - repl.Setup(x => x.FileSystem).Returns(fs.Object); - - var cmd = new CwdCommand(console.Object); - - // act - cmd.Execute(repl.Object, null); - - // assert - console.Verify(x => x.WriteLine(tempPath)); - } - - [Fact] - public void PreservesConsoleColors() - { - // arrange - var console = new Mock(); - var repl = new Mock(); - - console.SetupProperty(x => x.ForegroundColor); - repl.Setup(x => x.FileSystem).Returns(new Mock().Object); - - var cmd = new CwdCommand(console.Object); - var expectedForegroundColor = console.Object.ForegroundColor; - - // act - cmd.Execute(repl.Object, null); - - // assert - Assert.Equal(expectedForegroundColor, console.Object.ForegroundColor); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/ExitCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/ExitCommandTests.cs deleted file mode 100644 index f4e77b88..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/ExitCommandTests.cs +++ /dev/null @@ -1,78 +0,0 @@ -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class ExitCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsExit() - { - // act - var cmd = new ExitCommand(new Mock().Object); - - // assert - Assert.Equal("exit", cmd.CommandName); - } - } - - public class ExecuteMethod - { - private const string message = "Are you sure you wish to exit? (y/n):"; - - [Fact] - public void PromptsUserBeforeExiting() - { - // arrange - var console = new Mock(); - console.Setup(x => x.ReadLine(message)).Returns("n"); - var executor = new Mock(); - var cmd = new ExitCommand(console.Object); - - // act - cmd.Execute(executor.Object, null); - - // assert - console.Verify(x => x.ReadLine(message)); - } - - [Fact] - public void ExitsWhenUserAnswersYes() - { - // arrange - var console = new Mock(); - console.Setup(x => x.ReadLine(message)).Returns("y"); - - var executor = new Mock(); - var cmd = new ExitCommand(console.Object); - - // act - cmd.Execute(executor.Object, null); - - // assert - executor.Verify(x => x.Terminate()); - } - - [Fact] - public void DoesNotExitWhenUserAnswersNo() - { - // arrange - var console = new Mock(); - console.Setup(x => x.ReadLine(message)).Returns("n"); - - var executor = new Mock(); - var cmd = new ExitCommand(console.Object); - - // act - cmd.Execute(executor.Object, null); - - // assert - executor.Verify(x => x.Terminate(), Times.Never); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ReplCommands/HelpCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/HelpCommandTests.cs deleted file mode 100644 index 2cbbecf2..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/HelpCommandTests.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System.Collections.Generic; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class HelpCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsHelp() - { - // act - var cmd = new HelpCommand(new Mock().Object); - - // assert - Assert.Equal("help", cmd.CommandName); - } - } - - public class ExecuteMethod - { - [Fact] - public void PrintsCommandsToConsole() - { - // arrange - var console = new Mock(); - console.Setup(c => c.Width).Returns(80); - - var repl = new Mock(); - var clearCommand = new ClearCommand(console.Object); - var exitCommand = new ExitCommand(console.Object); - - var commands = new Dictionary - { - {clearCommand.CommandName, clearCommand}, - {exitCommand.CommandName, exitCommand}, - }; - - repl.Setup(x => x.Commands).Returns(commands); - - var cmd = new HelpCommand(console.Object); - - // act - cmd.Execute(repl.Object, null); - - // assert - console.Verify(x => x.WriteLine(It.IsAny()), Times.Exactly(4)); - console.Verify(x => x.WriteLine(It.Is(f => f.StartsWith(" :" + clearCommand.CommandName) && f.Contains(clearCommand.Description))), Times.Once); - console.Verify(x => x.WriteLine(It.Is(f => f.StartsWith(" :" + exitCommand.CommandName) && f.Contains(exitCommand.Description))), Times.Once); - } - - [Fact] - public void CorrectlyPrintsCommandsToConsoleAfterAlias() - { - // arrange - var console = new Mock(); - console.Setup(c => c.Width).Returns(80); - - var repl = new Mock(); - var clearCommand = new ClearCommand(console.Object); - var aliasCommand = new AliasCommand(console.Object); - - var commands = new Dictionary - { - {clearCommand.CommandName, clearCommand}, - {aliasCommand.CommandName, aliasCommand}, - }; - - repl.Setup(x => x.Commands).Returns(commands); - var cmd = new HelpCommand(console.Object); - - aliasCommand.Execute(repl.Object, new[] { "clear", "clr" }); - - // act - cmd.Execute(repl.Object, null); - - // assert - // because we now have formatted wrapping with the description, we have to remove - // all the extra spaces before verifying - console.Verify(x => x.WriteLine(It.Is(f => f.StartsWith(" :" + clearCommand.CommandName) && f.Replace(" ", " ").Contains(clearCommand.Description))), Times.Once); - console.Verify(x => x.WriteLine(It.Is(f => f.StartsWith(" :" + aliasCommand.CommandName) && f.Replace(" ", " ").Contains(aliasCommand.Description))), Times.Once); - console.Verify(x => x.WriteLine(It.Is(f => f.StartsWith(" :clr") && f.Replace(" ", " ").Contains(clearCommand.Description))), Times.Once); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/InstallCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/InstallCommandTests.cs deleted file mode 100644 index 5e1322a4..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/InstallCommandTests.cs +++ /dev/null @@ -1,151 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Versioning; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; -using Xunit.Extensions; - -namespace ScriptCs.Tests.ReplCommands -{ - public class InstallCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsInstall() - { - // act - var cmd = new InstallCommand( - new Mock().Object, - new Mock().Object, - new TestLogProvider(), - new Mock().Object); - - // assert - Assert.Equal("install", cmd.CommandName); - } - } - - public class ExecuteMethod - { - private readonly Mock _packageInstaller; - private readonly Mock _packageAssemblyResolver; - private readonly TestLogProvider _logProvider = new TestLogProvider(); - private readonly Mock _installationProvider; - private readonly Mock _repl; - - public ExecuteMethod() - { - _packageInstaller = new Mock(); - _packageAssemblyResolver = new Mock(); - _installationProvider = new Mock(); - _repl = new Mock(); - - var fs = new Mock(); - fs.Setup(x => x.CurrentDirectory).Returns(@"c:\dir"); - - _repl.SetupGet(x => x.FileSystem).Returns(fs.Object); - _repl.SetupGet(x => x.References).Returns(new AssemblyReferences()); - } - - private InstallCommand GetCommand() - { - return new InstallCommand(_packageInstaller.Object, _packageAssemblyResolver.Object, _logProvider, _installationProvider.Object); - } - - [Fact] - public void IfArgsAreEmptyReturns() - { - // arrange - var cmd = GetCommand(); - - // act - var result = cmd.Execute(_repl.Object, null); - - // assert - Assert.Null(result); - } - - [Fact] - public void InstallsPackageUsingArg0AsName() - { - // arrange - var cmd = GetCommand(); - - // act - cmd.Execute(_repl.Object, new[] { "scriptcs" }); - - // assert - _packageInstaller.Verify( - x => x.InstallPackages( - It.Is>(i => i.ElementAt(0).PackageId == "scriptcs"), false), - Times.Once); - } - - [Fact] - public void InstallsPackageUsingArg1AsVersion() - { - // arrange - var cmd = GetCommand(); - - // act - cmd.Execute(_repl.Object, new[] { "scriptcs", "0.9" }); - - // assert - var packageRef = new PackageReference("scriptcs", new FrameworkName(".NETFramework,Version=v4.0"), "0.9"); - _packageInstaller.Verify( - x => x.InstallPackages( - It.Is>(i => i.ElementAt(0).Version == packageRef.Version), - false), - Times.Once); - } - - [Theory] - [InlineData("pre")] - [InlineData("Pre")] - public void InstallsPackageUsingArg2AsPreReleaseFlag(string preReleaseFlag) - { - // arrange - var cmd = GetCommand(); - - // act - cmd.Execute(_repl.Object, new[] { "scriptcs", "0.9", preReleaseFlag }); - - // assert - _packageInstaller.Verify( - x => x.InstallPackages(It.IsAny>(), true), Times.Once); - } - - [Fact] - public void AfterInstallationSavesPackages() - { - // arrange - var cmd = GetCommand(); - - // act - cmd.Execute(_repl.Object, new[] { "scriptcs" }); - - // assert - _packageAssemblyResolver.Verify(x => x.SavePackages(), Times.Once); - } - - [Fact] - public void AddsAssemlbyReferenceToRepl() - { - // arrange - var dummyAssemblies = new[] { "assembly1.dll", "assembly2.dll" }; - _packageAssemblyResolver.Setup(x => x.GetAssemblyNames(@"c:\dir")).Returns(dummyAssemblies); - - var cmd = GetCommand(); - - // act - cmd.Execute(_repl.Object, new[] { "scriptcs" }); - - // assert - _repl.Verify(x => x.AddReferences(dummyAssemblies), Times.Once); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/ReferencesCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/ReferencesCommandTests.cs deleted file mode 100644 index ec591616..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/ReferencesCommandTests.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Should; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class ReferencesCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsReferences() - { - // act - var cmd = new ReferencesCommand(); - - // assert - cmd.CommandName.ShouldEqual("references"); - } - } - - public class ExecuteMethod - { - private readonly Mock _repl; - - public ExecuteMethod() - { - _repl = new Mock(); - } - - [Fact] - public void ShouldReturnAssembliesFromExecutor() - { - var assemblies = new AssemblyReferences( - new List { typeof(string).Assembly }, new List { "path1", "path2" }); - - _repl.SetupGet(x => x.References).Returns(assemblies); - - var cmd = new ReferencesCommand(); - var result = cmd.Execute(_repl.Object, null); - - var expected = new List { typeof(string).Assembly.FullName, "path1", "path2" }; - - ((IEnumerable)result).ToList().ShouldEqual(expected); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ReplCommands/ResetCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/ResetCommandTests.cs deleted file mode 100644 index 97eb4f6c..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/ResetCommandTests.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class ResetCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsReset() - { - // act - var cmd = new ResetCommand(); - - // assert - Assert.Equal("reset", cmd.CommandName); - } - } - - public class ExecuteMethod - { - [Fact] - public void CallsReplReset() - { - // arrange - var repl = new Mock(); - - var cmd = new ResetCommand(); - - // act - cmd.Execute(repl.Object, null); - - // assert - repl.Verify(x => x.Reset(), Times.Once); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/ScriptPacksCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/ScriptPacksCommandTests.cs deleted file mode 100644 index f46e74ee..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/ScriptPacksCommandTests.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class ScriptPacksCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsScriptPacks() - { - // act - var cmd = new ScriptPacksCommand(new Mock().Object); - - // assert - Assert.Equal("scriptpacks", cmd.CommandName); - } - } - - public class ExecuteMethod - { - private Mock _console; - private Mock _repl; - private Mock _scriptPackSession; - - public ExecuteMethod() - { - _console = new Mock(); - _repl = new Mock(); - _scriptPackSession = new Mock(Enumerable.Empty(), new string[0]); - - _scriptPackSession.Setup(x => x.Contexts).Returns(new List { new DummyScriptPack() }); - _repl.Setup(x => x.ScriptPackSession).Returns(_scriptPackSession.Object); - _repl.Setup(x => x.Namespaces).Returns(new Collection()); - } - - [Fact] - public void ShouldExitIfThereAreNoScriptPacks() - { - _scriptPackSession.Setup(x => x.Contexts).Returns((IEnumerable)null); - - var cmd = new ScriptPacksCommand(_console.Object); - cmd.Execute(_repl.Object, null); - - _console.Verify(x => x.WriteLine("There are no script packs available in this REPL session")); - } - - [Fact] - public void ShouldPrintMethodSignatures() - { - var cmd = new ScriptPacksCommand(_console.Object); - - cmd.Execute(_repl.Object, null); - - _console.Verify(x => x.WriteLine(typeof(DummyScriptPack).FullName.ToString())); - _console.Verify(x => x.WriteLine("** Methods **")); - _console.Verify(x => x.WriteLine(" - string Foo(int bar)")); - _console.Verify(x => x.WriteLine(" - ScriptCs.Tests.ReplCommands.DummyScriptPack Something()")); - } - - [Fact] - public void ShouldPrintPropertyInformation() - { - var cmd = new ScriptPacksCommand(_console.Object); - - cmd.Execute(_repl.Object, null); - - _console.Verify(x => x.WriteLine(typeof(DummyScriptPack).FullName.ToString())); - _console.Verify(x => x.WriteLine("** Properties **")); - _console.Verify(x => x.WriteLine(" - double FooBar { get; set; }")); - _console.Verify(x => x.WriteLine(" - ScriptCs.Tests.ReplCommands.DummyScriptPack Xyz { get; }")); - } - - [Fact] - public void ShouldRespectNamespaces() - { - _repl.Setup(x => x.Namespaces).Returns(new Collection { "ScriptCs.Tests.ReplCommands" }); - var cmd = new ScriptPacksCommand(_console.Object); - - cmd.Execute(_repl.Object, null); - - _console.Verify(x => x.WriteLine(" - DummyScriptPack Something()")); - _console.Verify(x => x.WriteLine(" - DummyScriptPack Xyz { get; }")); - } - } - } - - public class DummyScriptPack : IScriptPackContext - { - public string Foo(int bar) - { - return bar.ToString(); - } - - public DummyScriptPack Something() - { - return null; - } - - public double FooBar { get; set; } - - public DummyScriptPack Xyz - { - get { return null; } - } - } - - internal static class DummyScriptPackExtensions - { - public static void FooExtension(this DummyScriptPack dummyScriptPack) - { - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/UsingsCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/UsingsCommandTests.cs deleted file mode 100644 index b884dd4c..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/UsingsCommandTests.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.Collections.Generic; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Should; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class UsingsCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsUsings() - { - // act - var cmd = new UsingsCommand(); - - // assert - cmd.CommandName.ShouldEqual("usings"); - } - } - - public class ExecuteMethod - { - private Mock _repl; - - public ExecuteMethod() - { - _repl = new Mock(); - } - - [Fact] - public void ShouldReturnNamespacesFromExecutor() - { - var ns = new List {"System"}; - _repl.SetupGet(x => x.Namespaces).Returns(ns); - - var cmd = new UsingsCommand(); - var result = cmd.Execute(_repl.Object, null); - - result.ShouldBeSameAs(ns); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplCommands/VarsCommandTests.cs b/test/ScriptCs.Core.Tests/ReplCommands/VarsCommandTests.cs deleted file mode 100644 index bccf517c..00000000 --- a/test/ScriptCs.Core.Tests/ReplCommands/VarsCommandTests.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System.Collections.Generic; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.ReplCommands; -using Should; -using Xunit; - -namespace ScriptCs.Tests.ReplCommands -{ - public class VarsCommandTests - { - public class CommandNameProperty - { - [Fact] - public void ReturnsUsings() - { - // act - var cmd = new VarsCommand(); - - // assert - cmd.CommandName.ShouldEqual("vars"); - } - } - - public class ExecuteMethod - { - private Mock _repl; - - public ExecuteMethod() - { - _repl = new Mock(); - } - - [Fact] - public void ShouldReturnLocalVarsFromEngine() - { - var locals = new List {"int x = 0"}; - var replEngine = new Mock(); - replEngine.Setup(x => x.GetLocalVariables(It.IsAny())).Returns(locals); - _repl.SetupGet(x => x.ScriptEngine).Returns(replEngine.Object); - - var cmd = new VarsCommand(); - var result = cmd.Execute(_repl.Object, null); - - result.ShouldBeSameAs(locals); - } - - [Fact] - public void ShouldReturnNullForEngineWhichisNotReplEngine() - { - var replEngine = new Mock(); - _repl.SetupGet(x => x.ScriptEngine).Returns(replEngine.Object); - - var cmd = new VarsCommand(); - var result = cmd.Execute(_repl.Object, null); - - result.ShouldBeNull(); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ReplTests.cs b/test/ScriptCs.Core.Tests/ReplTests.cs deleted file mode 100644 index 8c8422b4..00000000 --- a/test/ScriptCs.Core.Tests/ReplTests.cs +++ /dev/null @@ -1,751 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Moq; -using Moq.Protected; -using ScriptCs.Contracts; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class ReplTests - { - public class Mocks - { - public Mocks() - { - FileSystem = new Mock(); - FileSystem.SetupGet(x => x.CurrentDirectory).Returns(""); - FileSystem.SetupGet(x => x.BinFolder).Returns("bin"); - FileSystem.SetupGet(x => x.DllCacheFolder).Returns(".cache"); - FileSystem.SetupGet(x => x.PackagesFolder).Returns("scriptcs_packages"); - ScriptEngine = new Mock(); - LogProvider = new TestLogProvider(); - ScriptLibraryComposer = new Mock(); - ScriptLibraryComposer.SetupGet(p => p.ScriptLibrariesFile).Returns("PackageScripts.csx"); - Console = new Mock(); - ScriptPack = new Mock(); - FilePreProcessor = new Mock(); - ObjectSerializer = new Mock(); - ReplCommands = new[] { new Mock() }; - } - - public Mock ObjectSerializer { get; private set; } - - public Mock FileSystem { get; private set; } - - public Mock ScriptEngine { get; private set; } - - public TestLogProvider LogProvider { get; private set; } - - public Mock Console { get; private set; } - - public Mock ScriptPack { get; private set; } - - public Mock FilePreProcessor { get; private set; } - - public Mock ScriptLibraryComposer { get; private set; } - - public Mock[] ReplCommands { get; set; } - } - - public static Repl GetRepl(Mocks mocks) - { - return new Repl( - new string[0], - mocks.FileSystem.Object, - mocks.ScriptEngine.Object, - mocks.ObjectSerializer.Object, - mocks.LogProvider, - mocks.ScriptLibraryComposer.Object, - mocks.Console.Object, - mocks.FilePreProcessor.Object, - mocks.ReplCommands.Select(x => x.Object), - new Printers(mocks.ObjectSerializer.Object), - new ScriptInfo()); - } - - public class TheConstructor - { - [Fact] - public void ShouldProperlyInitializeMembers() - { - var mocks = new Mocks(); - var repl = GetRepl(mocks); - repl.FileSystem.ShouldEqual(mocks.FileSystem.Object); - repl.ScriptEngine.ShouldEqual(mocks.ScriptEngine.Object); - repl.Console.ShouldEqual(mocks.Console.Object); - } - } - - public class TheInitializeMethod - { - private Mocks _mocks; - private Repl _repl; - private string _tempPath; - - public TheInitializeMethod() - { - _tempPath = Path.GetTempPath(); - - _mocks = new Mocks(); - _repl = GetRepl(_mocks); - _mocks.FileSystem.Setup(x => x.CurrentDirectory).Returns(_tempPath); - var paths = new[] { Path.Combine(_tempPath, "path") }; - _repl.Initialize(paths, new[] { _mocks.ScriptPack.Object }); - } - - [Fact] - public void PopulatesReferences() - { - foreach (var reference in Repl.DefaultReferences) - { - _repl.References.Paths.ShouldContain(reference); - } - - _repl.References.Paths.ShouldContain(Path.Combine(_tempPath, "path")); - } - - [Fact] - public void SetsTheScriptEngineBaseDirectory() - { - _mocks.ScriptEngine.VerifySet(x => x.BaseDirectory = Path.Combine(_tempPath, "bin")); - } - - [Fact] - public void CreatesTheScriptPackSession() - { - _repl.ScriptPackSession.ShouldNotBeNull(); - } - - [Fact] - public void InitializesScriptPacks() - { - _mocks.ScriptPack.Verify(x => x.Initialize(It.IsAny())); - } - } - - public class TheTerminateMethod - { - private Mocks _mocks; - private Repl _repl; - - public TheTerminateMethod() - { - _mocks = new Mocks(); - _repl = GetRepl(_mocks); - _mocks.FileSystem.Setup(x => x.CurrentDirectory).Returns(@"c:\"); - _repl.Initialize(new List(), new[] { _mocks.ScriptPack.Object }); - _repl.Terminate(); - } - - [Fact] - public void TerminatesScriptPacks() - { - _mocks.ScriptPack.Verify(x => x.Terminate()); - } - - [Fact] - public void ExitsTheConsole() - { - _mocks.Console.Verify(x => x.Exit()); - } - } - - public class TheExecuteMethod - { - private Mocks _mocks; - private Repl _repl; - - public TheExecuteMethod() - { - _mocks = new Mocks(); - _mocks.FilePreProcessor.Setup(x => x.ProcessScript("foo")) - .Returns(new FilePreProcessorResult { Code = "foo" }); - _repl = GetRepl(_mocks); - _repl.Console.ForegroundColor = ConsoleColor.White; - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty(), String.Empty); - _repl.Execute("foo"); - } - - [Fact] - public void SetsTheForegroundColorToCyan() - { - _mocks.Console.VerifySet(x => x.ForegroundColor = ConsoleColor.Cyan, Times.Once()); - } - - [Fact] - public void ResetsColorAfterExecutingScript() - { - _mocks.Console.Verify(x => x.ResetColor()); - } - - [Fact] - public void CallsExecuteOnTheScriptEngine() - { - _mocks.ScriptEngine.Verify( - x => x.Execute( - "Env.Initialize();" + Environment.NewLine + "foo", - new string[0], - It.Is(i => i.Assemblies.SequenceEqual(_repl.References.Assemblies)), - It.Is>(i => i.SequenceEqual(_repl.Namespaces)), - It.IsAny())); - } - - [Fact] - public void ShouldPassExtraNameSpacesToEngineIfFilePreProcessorProducesThem() - { - _mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.Is(i => i == "#load foo.csx"))) - .Returns(new FilePreProcessorResult { Namespaces = new List { "Foo", "Bar" } }); - - _repl.Execute("#load foo.csx"); - - _mocks.ScriptEngine.Verify(x => x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.Is>(i => i.SequenceEqual(_repl.Namespaces)), - It.IsAny())); - - _repl.Namespaces.Count().ShouldEqual(ScriptExecutor.DefaultNamespaces.Count() + 2); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddNamespacesFromScriptLibrary( - [Frozen] Mock scriptEngine, - Mock repl - ) - { - repl.Protected(); - repl.Setup(r => r.InjectScriptLibraries(It.IsAny(), It.IsAny(), It.IsAny>())) - .Callback((string p, FilePreProcessorResult r, IDictionary s) => - { - r.Namespaces.Add("Foo.Bar"); - }); - - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - repl.Object.Initialize(Enumerable.Empty(), Enumerable.Empty()); - repl.Object.Execute("", new string[] {}); - - scriptEngine.Verify( - e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.Is>(x => x.Contains("Foo.Bar")), - It.IsAny()), - Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddReferencesFromScriptLibrary( - [Frozen] Mock scriptEngine, - Mock repl - ) - { - repl.Protected(); - repl.Setup(r => r.InjectScriptLibraries(It.IsAny(), It.IsAny(), It.IsAny>())) - .Callback((string p, FilePreProcessorResult r, IDictionary s) => - { - r.References.Add("Foo.Bar"); - }); - - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - - repl.Object.Initialize(Enumerable.Empty(), Enumerable.Empty()); - repl.Object.Execute("", new string[] { }); - - scriptEngine.Verify( - e => e.Execute( - It.IsAny(), - It.IsAny(), - It.Is(x => x.Paths.Contains("Foo.Bar")), - It.IsAny>(), - It.IsAny()), - Times.Once()); - } - - [Fact] - public void ShouldRemoveInvalidNamespacesIfScriptResultContainsany() - { - _mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.Is(i => i == "#load foo.csx"))) - .Returns(new FilePreProcessorResult { Namespaces = new List { "Foo", "Bar" } }); - _mocks.ScriptEngine.Setup( - x => x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())).Returns(new ScriptResult(invalidNamespaces: new string[] { "Foo" })); - - _repl.Execute("#load foo.csx"); - _repl.Namespaces.Count().ShouldEqual(ScriptExecutor.DefaultNamespaces.Count() + 1); - _repl.Namespaces.ShouldNotContain("Foo"); - } - - [Fact] - public void CatchesExceptionsAndWritesThemInRed() - { - _mocks.ScriptEngine.Setup( - x => x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Throws(); - - _repl.Execute("foo"); - - _mocks.Console.VerifySet(x => x.ForegroundColor = ConsoleColor.Red); - _mocks.Console.Verify(x => x.WriteLine(It.IsAny())); - } - - [Fact] - public void SetsTheForegroundColorBackToTheDefault() - { - _mocks.Console.VerifySet(x => x.ForegroundColor = ConsoleColor.White); - } - - [Fact] - public void ShouldProcessFileIfLineIsALoad() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(x => x.FileExists("file.csx")).Returns(true); - - _repl = GetRepl(mocks); - _repl.Execute("#load \"file.csx\""); - - mocks.FilePreProcessor.Verify(i => i.ProcessScript("#load \"file.csx\""), Times.Once()); - } - - [Fact] - public void ShouldExecuteLoadedFileIfLineIsALoad() - { - var mocks = new Mocks(); - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult()); - mocks.FileSystem.Setup(x => x.FileExists("file.csx")).Returns(true); - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty(), ""); - _repl.Execute("#load \"file.csx\""); - - mocks.ScriptEngine.Verify( - i => i.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny()), - Times.Once()); - } - - [Fact] - public void ShouldNotExecuteLoadedFileIfFileDoesNotExist() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(x => x.FileExists("file.csx")).Returns(false); - - _repl = GetRepl(mocks); - _repl.Execute("#load \"file.csx\""); - - mocks.ScriptEngine.Verify( - i => i.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny()), - Times.Never()); - } - - [Fact] - public void ShouldReferenceAssemblyIfLineIsAReference() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - mocks.FileSystem.Setup(i => i.GetFullPath(It.IsAny())).Returns(@"c:/my.dll"); - mocks.FileSystem.Setup(x => x.FileExists("c:/my.dll")).Returns(true); - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult { References = new List { "my.dll" } }); - - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("#r \"my.dll\""); - - //default references = 10, + 1 we just added - _repl.References.Paths.Count().ShouldEqual(11); - } - - [Fact] - public void ShouldReferenceAssemblyBasedOnFullPathIfFileExists() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - mocks.FileSystem.Setup(i => i.GetFullPath(It.IsAny())).Returns(@"C:/my.dll"); - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult { References = new List { "my.dll" } }); - - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("#r \"my.dll\""); - - mocks.FileSystem.Verify(x => x.FileExists("C:/my.dll"), Times.Once()); - } - - [Fact] - public void ShouldReferenceAssemblyBasedOnNameIfFileDoesNotExistBecauseItLooksInGACThen() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - mocks.FileSystem.Setup(i => i.FileExists(It.IsAny())).Returns(false); - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult { References = new List { "PresentationCore" } }); - - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("#r \"PresentationCore\""); - - _repl.References.Paths.Contains("PresentationCore").ShouldBeTrue(); - } - - [Fact] - public void ShouldReferenceAssemblyBasedOnNameWithExtensionIfFileDoesNotExistBecauseItLooksInGACThen() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - mocks.FileSystem.Setup(i => i.GetFullPath(It.IsAny())).Returns(@"C:/my.dll"); - mocks.FileSystem.Setup(i => i.FileExists(It.IsAny())).Returns(false); - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult { References = new List { "my.dll" } }); - - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("#r \"my.dll\""); - - _repl.References.Paths.Contains("my.dll").ShouldBeTrue(); - } - - [Fact] - public void ShouldRemoveReferenceIfAssemblyIsNotFound() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - mocks.FileSystem.Setup(i => i.GetFullPath(It.IsAny())).Returns(@"C:/my.dll"); - mocks.FileSystem.Setup(i => i.FileExists(It.IsAny())).Returns(false); - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult { References = new List { "my.dll" } }); - mocks.ScriptEngine.Setup( - i => - i.Execute(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny>(), It.IsAny())) - .Throws(new FileNotFoundException("error", "my.dll")); - - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("#r \"my.dll\""); - _repl.References.Paths.Contains("my.dll").ShouldBeFalse(); - } - - [Fact] - public void ShouldNotExecuteAnythingIfLineIsAReference() - { - var mocks = new Mocks(); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("#r \"my.dll\""); - - mocks.ScriptEngine.Verify( - i => i.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny()), - Times.Never()); - } - - [Fact] - public void ShouldSetBufferIfLineIsFirstOfMultilineConstruct() - { - var mocks = new Mocks(); - mocks.ScriptEngine.Setup( - x => - x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(ScriptResult.Incomplete); - - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult { Code = "var x = 1;" }); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - _repl = GetRepl(mocks); - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("var x = 1;"); - - _repl.Buffer.ShouldNotBeNull(); - } - - [Fact] - public void ShouldResetBufferIfLineIsNoLongerMultilineConstruct() - { - var mocks = new Mocks(); - mocks.ScriptEngine.Setup( - x => - x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(ScriptResult.Empty); - mocks.FilePreProcessor.Setup(x => x.ProcessScript(It.IsAny())) - .Returns(new FilePreProcessorResult { Code = "}" }); - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - _repl = GetRepl(mocks); - _repl.Buffer = "class test {"; - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("}"); - - _repl.Buffer.ShouldBeNull(); - } - - [Fact] - public void ShouldResubmitEverytingIfLineIsNoLongerMultilineConstruct() - { - var mocks = new Mocks(); - mocks.ScriptEngine.Setup(x => x.Execute( - It.Is(i => i == "class test {}"), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(ScriptResult.Empty); - - mocks.FileSystem.Setup(i => i.CurrentDirectory).Returns("C:/"); - _repl = GetRepl(mocks); - _repl.Buffer = "class test {"; - _repl.Initialize(Enumerable.Empty(), Enumerable.Empty()); - _repl.Execute("}"); - - mocks.ScriptEngine.Verify(); - } - - [Fact] - public void ShouldPickReplCommandIfLineStartsWithColon() - { - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - _repl = GetRepl(mocks); - - _repl.Execute(":hello", null); - - helloCommand.Verify(x => x.Execute(_repl, It.Is(i => i.Length == 0)), Times.Once); - } - - [Fact] - public void ShouldEvaluateArgs() - { - var dummyObject = new DummyClass { Hello = "World" }; - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - mocks.ScriptEngine.Setup(x => x.Execute( - "myObj", - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(new ScriptResult(returnValue: dummyObject)); - - mocks.ScriptEngine.Setup(x => x.Execute( - "100", - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(new ScriptResult(returnValue: 100)); - - _repl = GetRepl(mocks); - - _repl.Execute(":hello 100 myObj", null); - - helloCommand.Verify( - x => x.Execute( - _repl, - It.Is(i => - i[0].GetType() == typeof(int) && - (int)i[0] == 100 && - i[1].Equals(dummyObject))), - Times.Once); - } - - [Fact] - public void ShouldEvaluateStrings() - { - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - mocks.ScriptEngine.Setup(x => x.Execute( - "\"world\"", - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(new ScriptResult(returnValue: "world")); - - _repl = GetRepl(mocks); - - _repl.Execute(":hello \"world\"", null); - - helloCommand.Verify( - x => x.Execute( - _repl, - It.Is(i => - i[0].GetType() == typeof(string) && - (string)i[0] == "world")), - Times.Once); - } - - [Fact] - public void ShouldSurfaceArgumentCompilationErrors() - { - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - mocks.ScriptEngine.Setup(x => x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(new ScriptResult(compilationException: new Exception())); - - _repl = GetRepl(mocks); - - var result = _repl.Execute(":hello foo", null); - - result.ExecuteExceptionInfo.SourceException.Message.ShouldContain( - "argument is not a valid expression: foo", StringComparison.OrdinalIgnoreCase); - } - - [Fact] - public void ShouldSurfaceArgumentExecutionErrors() - { - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - mocks.ScriptEngine.Setup(x => x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(new ScriptResult(executionException: new Exception())); - - _repl = GetRepl(mocks); - - var result = _repl.Execute(":hello foo", null); - - result.ExecuteExceptionInfo.SourceException.Message.ShouldContain( - "argument is not a valid expression: foo", StringComparison.OrdinalIgnoreCase); - } - - [Fact] - public void ShouldSurfaceIncompleteArguments() - { - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - mocks.ScriptEngine.Setup(x => x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())) - .Returns(ScriptResult.Incomplete); - - _repl = GetRepl(mocks); - - var result = _repl.Execute(":hello foo", null); - - result.ExecuteExceptionInfo.SourceException.Message.ShouldContain( - "argument is not a valid expression: foo", StringComparison.OrdinalIgnoreCase); - } - - [Fact] - public void ShouldPrintTheReturnToConsoleIfCommandHasReturnValue() - { - object returnValue = new DummyClass { Hello = "World" }; - - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - helloCommand.Setup(x => x.Execute(It.IsAny(), It.IsAny())) - .Returns(returnValue); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - mocks.ObjectSerializer.Setup(x => x.Serialize(returnValue)).Returns("hello world"); - - _repl = GetRepl(mocks); - - _repl.Execute(":hello", null); - - mocks.ObjectSerializer.Verify(x => x.Serialize(returnValue), Times.Once); - mocks.Console.Verify(x => x.WriteLine("hello world"), Times.Once); - } - - [Fact] - public void ShouldReturnCommandsScriptResultfCommandHasReturnValueThatAlreadyIsScriptResult() - { - var returnValue = new ScriptResult("hello world"); - - var helloCommand = new Mock(); - helloCommand.SetupGet(x => x.CommandName).Returns("hello"); - helloCommand.Setup(x => x.Execute(It.IsAny(), It.IsAny())) - .Returns(returnValue); - - var mocks = new Mocks { ReplCommands = new[] { helloCommand } }; - mocks.ObjectSerializer.Setup(x => x.Serialize(returnValue.ReturnValue)).Returns("hello world"); - - _repl = GetRepl(mocks); - - var result = _repl.Execute(":hello", null); - - mocks.ObjectSerializer.Verify(x => x.Serialize(returnValue.ReturnValue), Times.Once); - mocks.Console.Verify(x => x.WriteLine("hello world"), Times.Once); - result.ShouldBeSameAs(returnValue); - } - } - - private class DummyClass - { - public string Hello { get; set; } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ScriptCs.Core.Tests.csproj b/test/ScriptCs.Core.Tests/ScriptCs.Core.Tests.csproj deleted file mode 100644 index 322ee903..00000000 --- a/test/ScriptCs.Core.Tests/ScriptCs.Core.Tests.csproj +++ /dev/null @@ -1,27 +0,0 @@ - - - net461 - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/ScriptExecutorTests.cs b/test/ScriptCs.Core.Tests/ScriptExecutorTests.cs deleted file mode 100644 index 7f79667d..00000000 --- a/test/ScriptCs.Core.Tests/ScriptExecutorTests.cs +++ /dev/null @@ -1,676 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Moq; -using Moq.Protected; -using ScriptCs.Contracts; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class ScriptExecutorTests - { - public class TheInitializeMethod - { - [Theory, ScriptCsAutoData] - public void ShouldSetEngineBaseDirectoryBasedOnCurrentDirectoryAndBinFolder( - [Frozen] Mock scriptEngine, - [Frozen] Mock fileSystem, - [Frozen] Mock preProcessor, - ScriptExecutor scriptExecutor) - { - // arrange - preProcessor.Setup(x => x.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult()); - scriptEngine.SetupProperty(e => e.BaseDirectory); - - // act - scriptExecutor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // assert - scriptEngine.Object.BaseDirectory.ShouldEqual( - Path.Combine(fileSystem.Object.CurrentDirectory, "scriptcs_bin")); - } - - [Theory, ScriptCsAutoData] - public void ShouldSetEngineDllCacheDirectoryBasedOnCurrentDirectory( - [Frozen] Mock scriptEngine, - [Frozen] Mock fileSystem, - [Frozen] Mock preProcessor, - ScriptExecutor scriptExecutor) - { - // arrange - preProcessor.Setup(x => x.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult()); - scriptEngine.SetupProperty(e => e.CacheDirectory); - - // act - scriptExecutor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // assert - scriptEngine.Object.CacheDirectory.ShouldEqual( - Path.Combine(fileSystem.Object.CurrentDirectory, ".scriptcs_cache")); - } - - [Theory, ScriptCsAutoData] - public void ShouldInitializeScriptPacks( - [Frozen] Mock preProcessor, - [Frozen] Mock scriptPack, - ScriptExecutor scriptExecutor) - { - // arrange - preProcessor.Setup(p => p.ProcessFile(It.IsAny())) - .Returns(new FilePreProcessorResult { Code = "var a = 0;" }); - - scriptPack.Setup(p => p.Initialize(It.IsAny())); - scriptPack.Setup(p => p.GetContext()).Returns(Mock.Of()); - - // act - scriptExecutor.Initialize(Enumerable.Empty(), new[] { scriptPack.Object }); - - // assert - scriptPack.Verify(p => p.Initialize(It.IsAny())); - } - } - - public class TheTerminateMethod - { - [Theory, ScriptCsAutoData] - public void ShouldTerminateScriptPacksWhenTerminateIsCalled( - [Frozen] Mock preProcessor, - [Frozen] Mock scriptPack, - ScriptExecutor executor) - { - // arrange - preProcessor.Setup(p => p.ProcessFile(It.IsAny())) - .Returns(new FilePreProcessorResult { Code = "var a = 0;" }); - - scriptPack.Setup(p => p.Initialize(It.IsAny())); - scriptPack.Setup(p => p.GetContext()).Returns(Mock.Of()); - scriptPack.Setup(p => p.Terminate()); - - executor.Initialize(Enumerable.Empty(), new[] { scriptPack.Object }); - executor.Execute("script.csx"); - - // act - executor.Terminate(); - - // assert - scriptPack.Verify(p => p.Terminate()); - } - } - - public class TheEngineExecuteMethod - { - [Theory, ScriptCsAutoData] - public void ShouldSetScriptInfo_ScriptPath( - [Frozen] Mock scriptEngine, - ScriptExecutor scriptExecutor) - { - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - scriptExecutor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - var result = new FilePreProcessorResult(); - result.ScriptPath = "Main.csx"; - scriptExecutor.EngineExecute("", new string[] {}, result); - scriptExecutor.ScriptInfo.ScriptPath.ShouldEqual("Main.csx"); - } - - [Theory, ScriptCsAutoData] - public void ShouldPopulateScriptInfo_LoadedScript( - [Frozen] Mock scriptEngine, - ScriptExecutor scriptExecutor) - { - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - scriptExecutor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - var result = new FilePreProcessorResult(); - result.ScriptPath = "Main.csx"; - result.LoadedScripts.Add("Child.csx"); - scriptExecutor.EngineExecute("", new string[] { }, result); - scriptExecutor.ScriptInfo.LoadedScripts.First().ShouldEqual("Child.csx"); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddReferenceToEachDestinationFile( - [Frozen] Mock scriptEngine, - ScriptExecutor scriptExecutor) - { - // arrange - var defaultReferences = ScriptExecutor.DefaultReferences; - - var currentDirectory = @"C:\"; - var destinationFilePath1 = Path.Combine(currentDirectory, @"bin\fileName1.cs"); - var destinationFilePath2 = Path.Combine(currentDirectory, @"bin\fileName2.cs"); - var destinationFilePath3 = Path.Combine(currentDirectory, @"bin\fileName3.cs"); - var destinationFilePath4 = Path.Combine(currentDirectory, @"bin\fileName4.cs"); - - var destPaths = new[] - { - "System", - "System.Core", - destinationFilePath1, - destinationFilePath2, - destinationFilePath3, - destinationFilePath4, - }; - - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - scriptExecutor.AddReferences("a"); - scriptExecutor.AddReferences(new[] { "a", "a", "b", "c", "d" }); - scriptExecutor.AddReference(); - scriptExecutor.AddReferences(typeof(TheInitializeMethod)); - var explicitReferences = new[] - { - "a", - "b", - "c", - "d", - typeof(FactAttribute).Assembly.Location, - typeof(TheInitializeMethod).Assembly.Location, - }; - - scriptExecutor.Initialize(destPaths, Enumerable.Empty()); - - // act - scriptExecutor.EngineExecute("", new string[] {}, new FilePreProcessorResult()); - - // assert - var expectedReferencePaths = explicitReferences.Union(destPaths).Distinct(); - expectedReferencePaths.All(path => scriptExecutor.References.Paths.Contains(path)).ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldExecuteScriptReturnedFromFileProcessorInScriptEngine( - [Frozen] Mock scriptEngine, - ScriptExecutor scriptExecutor) - { - // arrange - var code = Guid.NewGuid().ToString(); - - scriptEngine.Setup(e => e.Execute( - code, It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - scriptExecutor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // act - scriptExecutor.EngineExecute("", new string[] {}, new FilePreProcessorResult() {Code = code}); - - // assert - - scriptEngine.Verify( - s => s.Execute( - "Env.Initialize();" + Environment.NewLine + code, - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny()), - Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddNamespacesFromScriptLibrary( - [Frozen] Mock scriptEngine, - Mock scriptExecutor - ) - { - scriptExecutor.Protected(); - var result = new FilePreProcessorResult(); - scriptExecutor.Setup(e => e.InjectScriptLibraries(It.IsAny(), result, It.IsAny>())) - .Callback((string p, FilePreProcessorResult r, IDictionary s) => - { - r.Namespaces.Add("Foo.Bar"); - }); - - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - - scriptExecutor.Object.Initialize(Enumerable.Empty(), Enumerable.Empty()); - scriptExecutor.Object.EngineExecute("", new string[] {}, result); - - scriptEngine.Verify( - e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.Is>(x => x.Contains("Foo.Bar")), - It.IsAny()), - Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddReferencesFromScriptLibrary( - [Frozen] Mock scriptEngine, - Mock scriptExecutor - ) - { - scriptExecutor.Protected(); - var result = new FilePreProcessorResult(); - scriptExecutor.Setup(e => e.InjectScriptLibraries(It.IsAny(), result, It.IsAny>())) - .Callback((string p, FilePreProcessorResult r, IDictionary s) => - { - r.References.Add("Foo.Bar"); - }); - - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - - scriptExecutor.Object.Initialize(Enumerable.Empty(), Enumerable.Empty()); - scriptExecutor.Object.EngineExecute("", new string[] {}, result); - - scriptEngine.Verify( - e => e.Execute( - It.IsAny(), - It.IsAny(), - It.Is(x => x.Paths.Contains("Foo.Bar")), - It.IsAny>(), - It.IsAny()), - Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddNamespaces( - [Frozen] Mock scriptEngine, - ScriptExecutor scriptExecutor) - { - scriptEngine.Setup(e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - scriptExecutor.ImportNamespaces("a"); - scriptExecutor.ImportNamespaces(new[] { "a", "a", "b", "c", "d" }.ToArray()); - scriptExecutor.ImportNamespace(); - scriptExecutor.ImportNamespaces(typeof(TheInitializeMethod)); - var explicitNamespaces = new[] - { - "a", - "b", - "c", - "d", - typeof(FactAttribute).Namespace, - typeof(TheInitializeMethod).Namespace - }; - - scriptExecutor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // act - scriptExecutor.EngineExecute("", new string[] {}, new FilePreProcessorResult()); - - // assert - scriptEngine.Verify( - e => e.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.Is>(x => - x.SequenceEqual(ScriptExecutor.DefaultNamespaces.Union(explicitNamespaces))), - It.IsAny()), - Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void ShouldPassDefaultNamespacesToEngine( - [Frozen] Mock engine, - ScriptExecutor executor) - { - // arrange - - executor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // act - executor.EngineExecute("", new string[] {}, new FilePreProcessorResult()); - - // assert - engine.Verify( - i => i.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.Is>(x => !x.Except(ScriptExecutor.DefaultNamespaces).Any()), - It.IsAny()), - Times.Exactly(1)); - } - - [Theory, ScriptCsAutoData] - public void ShouldPassDefaultReferencesToEngine( - [Frozen] Mock engine, - ScriptExecutor executor) - { - // arrange - - executor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // act - executor.EngineExecute("", new string[] {}, new FilePreProcessorResult()); - - // assert - engine.Verify( - i => i.Execute( - It.IsAny(), - It.IsAny(), - It.Is(x => - !x.Paths.Except(ScriptExecutor.DefaultReferences).Any()), - It.IsAny>(), - It.IsAny()), - Times.Exactly(1)); - } - - [Theory, ScriptCsAutoData] - public void ShouldInvokeInjectScriptLibraries(Mock executor) - { - executor.Protected(); - executor.Object.Initialize(Enumerable.Empty(), Enumerable.Empty(), ""); - executor.Setup(e => e.InjectScriptLibraries( - It.IsAny(), It.IsAny(), It.IsAny>())); - - var result = new FilePreProcessorResult(); - - executor.Object.EngineExecute("", new string[] { }, result); - - executor.Verify(e => e.InjectScriptLibraries( - It.IsAny(), result, It.IsAny>())); - } - } - - public class TheExecuteMethod - { - private readonly string _tempPath; - - public TheExecuteMethod() - { - _tempPath = Path.GetTempPath(); - } - - [Theory, ScriptCsAutoData] - public void ShouldInvokeEngineExecute( - [Frozen] Mock preProcessor, - Mock executor, - FilePreProcessorResult result - ) - { - executor.Protected(); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(result); - executor.Setup( - e => e.EngineExecute( - It.IsAny(), - It.IsAny(), - It.IsAny())); - - var args = new string[] {}; - - executor.Object.Execute("", args); - - executor.Verify( - e => e.EngineExecute( - "", - args, - result)); - } - - [Theory, ScriptCsAutoData] - public void ConstructsAbsolutePathBeforePreProcessingFile( - [Frozen] Mock preProcessor, - [Frozen] Mock fileSystem, - ScriptExecutor executor) - { - // arrange - fileSystem.Setup(f => f.CurrentDirectory).Returns(Path.Combine(_tempPath, "my_script")); - fileSystem.Setup(f => f.GetWorkingDirectory(It.IsAny())) - .Returns(Path.Combine(_tempPath, "my_script")); - - preProcessor.Setup(p => p.ProcessFile(It.IsAny())) - .Returns(new FilePreProcessorResult { Code = "var a = 0;" }); - - executor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // act - executor.Execute("script.csx"); - - // assert - preProcessor.Verify(p => p.ProcessFile( - Path.Combine(_tempPath, "my_script", "script.csx"))); - } - - [Theory, ScriptCsAutoData] - public void DoNotChangePathIfAbsolute( - [Frozen] Mock preProcessor, - [Frozen] Mock fileSystem, - ScriptExecutor executor) - { - // arrange - fileSystem.Setup(f => f.GetWorkingDirectory(It.IsAny())) - .Returns(Path.Combine(_tempPath, "my_script")); - - fileSystem.Setup(f => f.CurrentDirectory).Returns(Path.Combine(_tempPath, "my_script")); - - preProcessor.Setup(p => p.ProcessFile(It.IsAny())) - .Returns(new FilePreProcessorResult { Code = "var a = 0;" }); - - executor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - - // act - executor.Execute("script.csx"); - - // assert - preProcessor.Verify(p => p.ProcessFile( - Path.Combine(_tempPath, "my_script", "script.csx"))); - } - - [Theory, ScriptCsAutoData] - public void ShouldSetEngineFileName( - [Frozen] Mock scriptEngine, - [Frozen] Mock preProcessor, - ScriptExecutor scriptExecutor) - { - // arrange - scriptEngine.SetupProperty(e => e.FileName); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult()); - scriptExecutor.Initialize(Enumerable.Empty(), Enumerable.Empty()); - var scriptName = "script.csx"; - - // act - scriptExecutor.Execute(scriptName); - - // assert - scriptEngine.Object.FileName.ShouldEqual(scriptName); - } - } - - public class TheExecuteScriptMethod - { - [Theory, ScriptCsAutoData] - public void ShouldInvokeEngineExecute( - [Frozen] Mock preProcessor, - Mock executor, - FilePreProcessorResult result - ) - { - executor.Protected(); - preProcessor.Setup(p => p.ProcessScript(It.IsAny())).Returns(result); - executor.Setup( - e => e.EngineExecute( - It.IsAny(), - It.IsAny(), - It.IsAny())); - - var args = new string[] { }; - - executor.Object.ExecuteScript("", args); - - executor.Verify( - e => e.EngineExecute( - "workingdirectory", - args, - result)); - } - - } - - public class TheInjectScriptLibrariesMethod - { - private IDictionary _state = new Dictionary(); - private FilePreProcessorResult _result = new FilePreProcessorResult(); - private FilePreProcessorResult _scriptLibrariesPreProcessorResult = new FilePreProcessorResult(); - - public TheInjectScriptLibrariesMethod() - { - _scriptLibrariesPreProcessorResult.Code = "Test"; - _result.Code = ""; - } - - [Theory, ScriptCsAutoData] - public void ShouldExitIfPreProcessorResultIsNull(ScriptExecutor executor) - { - executor.InjectScriptLibraries("", _result, _state); - _result.Code.ShouldEqual("Env.Initialize();" + Environment.NewLine); - } - - [Theory, ScriptCsAutoData] - public void ShouldExitIfSessionPackageScriptsInjectedIsSet(Mock executor) - { - _state["ScriptLibrariesInjected"] = null; - executor.Protected(); - executor.Setup(e => e.LoadScriptLibraries(It.IsAny())).Returns(_scriptLibrariesPreProcessorResult); - executor.Object.InjectScriptLibraries("", _result, _state); - _result.Code.ShouldBeEmpty(); - } - - [Theory, ScriptCsAutoData] - public void ShouldInjectResultCode(Mock executor) - { - executor.Protected(); - executor.Setup(e => e.LoadScriptLibraries(It.IsAny())).Returns(_scriptLibrariesPreProcessorResult); - executor.Object.InjectScriptLibraries("", _result, _state); - _result.Code.ShouldEqual("Test" + Environment.NewLine + "Env.Initialize();" + Environment.NewLine); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddResultReferences(Mock executor) - { - executor.Protected(); - executor.Setup(e => e.LoadScriptLibraries(It.IsAny())).Returns(_scriptLibrariesPreProcessorResult); - _scriptLibrariesPreProcessorResult.References.Add("ref1"); - executor.Object.InjectScriptLibraries("", _result, _state); - _result.References.ShouldContain("ref1"); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddResultNamespaces(Mock executor) - { - executor.Protected(); - executor.Setup(e => e.LoadScriptLibraries(It.IsAny())).Returns(_scriptLibrariesPreProcessorResult); - _scriptLibrariesPreProcessorResult.Namespaces.Add("ns1"); - executor.Object.InjectScriptLibraries("", _result, _state); - _result.Namespaces.ShouldContain("ns1"); - } - - [Theory, ScriptCsAutoData] - public void ShouldSetPackageScriptsInjectedInSession(Mock executor) - { - executor.Protected(); - executor.Setup(e => e.LoadScriptLibraries(It.IsAny())).Returns(_scriptLibrariesPreProcessorResult); - executor.Object.InjectScriptLibraries("", _result, _state); - _state.ContainsKey("ScriptLibrariesInjected"); - } - } - - public class TheLoadScriptLibrariesMethod - { - [Theory, ScriptCsAutoData] - public void ShouldPreProcessTheScriptLibrariesFileIfPresent( - [Frozen] Mock fileSystem, - [Frozen] Mock preProcessor, - [Frozen] Mock engine, - [Frozen] TestLogProvider logProvider, - [Frozen] Mock composer) - { - // arrange - fileSystem.Setup(fs => fs.FileExists(It.IsAny())).Returns(true); - var executor = new ScriptExecutor( - fileSystem.Object, preProcessor.Object, engine.Object, logProvider, composer.Object, new ScriptInfo()); - - // act - executor.LoadScriptLibraries(""); - - // assert - preProcessor.Verify(p => p.ProcessFile(It.IsAny())); - } - } - - public class TheAddReferencesMethod - { - [Theory, ScriptCsAutoData] - public void ShouldAddReferenceToEachAssembly(ScriptExecutor executor) - { - // arrange - var assembly1 = typeof(Mock).Assembly; - var assembly2 = typeof(FrozenAttribute).Assembly; - var assembly3 = typeof(Assert).Assembly; - - // act - executor.AddReferences(assembly1, assembly2, assembly3, assembly3); - - // assert - executor.References.Assemblies.ShouldContain(assembly1); - executor.References.Assemblies.ShouldContain(assembly2); - executor.References.Assemblies.ShouldContain(assembly3); - } - } - - public class TheRemoveReferencesMethod - { - [Theory, ScriptCsAutoData] - public void ShouldRemoveReferenceToEachAssembly(ScriptExecutor executor) - { - // arrange - var assembly1 = typeof(Mock).Assembly; - var assembly2 = typeof(FrozenAttribute).Assembly; - var assembly3 = typeof(Assert).Assembly; - executor.AddReferences(assembly1, assembly2, assembly3); - - // act - executor.RemoveReferences(assembly1, assembly2); - - // assert - executor.References.Assemblies.ShouldNotContain(assembly1); - executor.References.Assemblies.ShouldNotContain(assembly2); - executor.References.Assemblies.ShouldContain(assembly3); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ScriptHostTests.cs b/test/ScriptCs.Core.Tests/ScriptHostTests.cs deleted file mode 100644 index a90e169a..00000000 --- a/test/ScriptCs.Core.Tests/ScriptHostTests.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Moq; - -using ScriptCs.Contracts; - -using Should; - -using Xunit; - -namespace ScriptCs.Tests -{ - public class ScriptHostTests - { - public class TheGetMethod - { - private readonly Mock _mockContext = new Mock(); - private readonly Mock _mockScriptPackManager = new Mock(); - private readonly Mock _mockConsole = new Mock(); - private readonly Mock _mockSerializer = new Mock(); - - private readonly ScriptHost _scriptHost; - - public TheGetMethod() - { - _scriptHost = new ScriptHost(_mockScriptPackManager.Object, new ScriptEnvironment(new string[0], _mockConsole.Object, new Printers(_mockSerializer.Object))); - _mockScriptPackManager.Setup(s => s.Get()).Returns(_mockContext.Object); - } - - [Fact] - public void ShoulGetScriptPackFromScriptPackManagerWhenInvoked() - { - _scriptHost.Require(); - _mockScriptPackManager.Verify(s => s.Get()); - } - } - - public class TheConstructor - { - private readonly Mock _mockConsole = new Mock(); - private readonly Mock _mockSerializer = new Mock(); - - [Fact] - public void ShouldSetScriptEnvironment() - { - var environment = new ScriptEnvironment(new string[0], _mockConsole.Object, new Printers(_mockSerializer.Object)); - var scriptHost = new ScriptHost(new Mock().Object, environment); - - scriptHost.Env.ShouldEqual(environment); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ScriptLibraryComposerTests.cs b/test/ScriptCs.Core.Tests/ScriptLibraryComposerTests.cs deleted file mode 100644 index 7818f2bb..00000000 --- a/test/ScriptCs.Core.Tests/ScriptLibraryComposerTests.cs +++ /dev/null @@ -1,250 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using ScriptCs.Contracts; -using Moq; -using Moq.Protected; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class ScriptLibraryComposerTests - { - public class TheGetMainScriptMethod - { - [Theory, ScriptCsAutoData] - public void ShouldReturnTheScript(Mock package, ScriptLibraryComposer composer) - { - var files = new[] {"file.csx", "fileMain.csx", "file"}; - package.Setup(p => p.GetContentFiles()).Returns(files); - var script = composer.GetMainScript(package.Object); - script.ShouldEqual("fileMain.csx"); - } - } - - public class TheProcessPackageMethod - { - [Theory, ScriptCsAutoData] - public void ShouldFindThePackage( - [Frozen] Mock packageContainer, - ScriptLibraryComposer composer, - Mock reference, - Mock package) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns(package.Object); - package.Setup(p=>p.GetContentFiles()).Returns(Enumerable.Empty()); - composer.ProcessPackage("", reference.Object, new StringBuilder(), new List(), new List()); - packageContainer.Verify(c => c.FindPackage(It.IsAny(), It.IsAny())); - } - - [Theory, ScriptCsAutoData] - public void ShouldLogAWarningIfThePackageIsMissing( - [Frozen] TestLogProvider logProvider, - [Frozen] Mock packageContainer, - ScriptLibraryComposer composer, - Mock reference) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns((IPackageObject)null); - reference.SetupGet(r => r.PackageId).Returns("test"); - composer.ProcessPackage("", reference.Object, new StringBuilder(), new List(), new List()); - packageContainer.Verify(c => c.FindPackage(It.IsAny(), It.IsAny())); - logProvider.Output.ShouldContain("WARN: Package missing: test"); - } - - - - [Theory, ScriptCsAutoData] - public void ShouldPreProcessTheScriptFile( - [Frozen] Mock packageContainer, - [Frozen] Mock preProcessor, - ScriptLibraryComposer composer, - Mock reference, - Mock package) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns(package.Object); - package.Setup(p => p.GetContentFiles()).Returns(new List {"TestMain.csx"}); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult()); - composer.ProcessPackage(@"c:\packages", reference.Object, new StringBuilder(), new List(), new List()); - preProcessor.Verify(p=> p.ProcessFile(It.IsAny())); - } - - [Theory, ScriptCsAutoData] - public void ShouldWarnIfMultipleMainFilesArePresent( - [Frozen] Mock packageContainer, - [Frozen] Mock preProcessor, - [Frozen] TestLogProvider logProvider, - ScriptLibraryComposer composer, - Mock reference, - Mock package) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns(package.Object); - package.SetupGet(p => p.FullName).Returns("Test"); - package.Setup(p => p.GetContentFiles()).Returns(new List { "Test1Main.csx", "Test2Main.csx" }); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult()); - composer.ProcessPackage(@"c:\packages", reference.Object, new StringBuilder(), new List(), new List()); - logProvider.Output.ShouldContain( - "WARN: Script Libraries in 'Test' ignored due to multiple Main files being present"); - } - - - - [Theory, ScriptCsAutoData] - public void ShouldAppendTheClassWrapper( - [Frozen] Mock packageContainer, - [Frozen] Mock preProcessor, - ScriptLibraryComposer composer, - Mock reference, - Mock package) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns(package.Object); - package.Setup(p => p.GetContentFiles()).Returns(new List {"TestMain.csx"}); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult()); - var builder = new StringBuilder(); - composer.ProcessPackage(@"c:\packages", reference.Object, builder, new List(), new List()); - builder.ToString().ShouldContain("public class Test : ScriptCs.ScriptLibraryWrapper {"); - } - - [Theory, ScriptCsAutoData] - public void ShouldAppendThePreProcessedCode( - [Frozen] Mock packageContainer, - [Frozen] Mock preProcessor, - ScriptLibraryComposer composer, - Mock reference, - Mock package) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns(package.Object); - package.Setup(p => p.GetContentFiles()).Returns(new List { "TestMain.csx" }); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult {Code="TestCode"}); - var builder = new StringBuilder(); - composer.ProcessPackage(@"c:\packages", reference.Object, builder, new List(), new List()); - builder.ToString().EndsWith("TestCode}"); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddsReferences( - [Frozen] Mock packageContainer, - [Frozen] Mock preProcessor, - ScriptLibraryComposer composer, - Mock reference, - Mock package) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns(package.Object); - package.Setup(p => p.GetContentFiles()).Returns(new List { "TestMain.csx" }); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult { Code = "TestCode", References = new List {"ref1", "ref2"}}); - var refs = new List(); - composer.ProcessPackage(@"c:\packages", reference.Object, new StringBuilder(), refs, new List()); - refs.ShouldContain("ref1"); - refs.ShouldContain("ref2"); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddNamespaces( - [Frozen] Mock packageContainer, - [Frozen] Mock preProcessor, - ScriptLibraryComposer composer, - Mock reference, - Mock package) - { - packageContainer.Setup(c => c.FindPackage(It.IsAny(), It.IsAny())) - .Returns(package.Object); - package.Setup(p => p.GetContentFiles()).Returns(new List { "TestMain.csx" }); - preProcessor.Setup(p => p.ProcessFile(It.IsAny())).Returns(new FilePreProcessorResult { Code = "TestCode", Namespaces = new List { "ns1", "ns2" } }); - var ns = new List(); - composer.ProcessPackage(@"c:\packages", reference.Object, new StringBuilder(), new List(), ns); - ns.ShouldContain("ns1"); - ns.ShouldContain("ns2"); - } - } - - public class TheComposeMethod - { - [Theory, ScriptCsAutoData] - public void ShouldProcessEachPackage( - [Frozen] Mock resolver, - Mock composer, - Mock reference1, - Mock reference2) - { - composer.Protected(); - resolver.Setup(r=>r.GetPackages(It.IsAny())).Returns(new List { reference1.Object, reference2.Object }); - composer.Setup(c=>c.ProcessPackage(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny>())); - composer.Object.Compose("workingdirectory"); - composer.Verify(c=>c.ProcessPackage(It.IsAny(),reference1.Object, It.IsAny(), It.IsAny>(), It.IsAny>())); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddEachNamespace( - [Frozen] Mock resolver, - Mock composer, - Mock reference1 - ) - { - composer.Protected(); - var builder = new StringBuilder(); - composer.Setup( - c => - c.ProcessPackage(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny>(), It.IsAny>())) - .Callback((string p, IPackageReference r, StringBuilder b, List refs, List ns) => - { - ns.Add("ns1"); - ns.Add("ns2"); - }); - resolver.Setup(r=>r.GetPackages(It.IsAny())).Returns(new List { reference1.Object }); - composer.Object.Compose("workingdirectory", builder); - var code = builder.ToString(); - code.ShouldContain(string.Format("using ns1;{0}", Environment.NewLine)); - code.ShouldContain(string.Format("using ns2;{0}", Environment.NewLine)); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddEachReference( - [Frozen] Mock resolver, - Mock composer, - Mock reference1 - ) - { - composer.Protected(); - var builder = new StringBuilder(); - composer.Setup( - c => - c.ProcessPackage(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny>(), It.IsAny>())) - .Callback((string p, IPackageReference r, StringBuilder b, List refs, List ns) => - { - refs.Add("ref1"); - refs.Add("ref2"); - }); - resolver.Setup(r => r.GetPackages(It.IsAny())).Returns(new List { reference1.Object }); - composer.Object.Compose("workingdirectory", builder); - var code = builder.ToString(); - code.ShouldContain(string.Format("#r ref1{0}", Environment.NewLine)); - code.ShouldContain(string.Format("#r ref2{0}", Environment.NewLine)); - } - - [Theory, ScriptCsAutoData] - public void ShouldWriteTheScriptLibrariesFile( - [Frozen] Mock fileSystem, - [Frozen] Mock resolver, - ScriptLibraryComposer composer - ) - { - var builder = new StringBuilder("Test"); - fileSystem.Setup(fs => fs.WriteToFile(It.IsAny(), It.IsAny())); - resolver.Setup(r => r.GetPackages(It.IsAny())).Returns(Enumerable.Empty()); - composer.Compose("workingdirectory", builder); - fileSystem.Verify(fs => fs.WriteToFile(It.IsAny(), It.Is(v=>v.Equals("Test")))); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ScriptPackResolverTests.cs b/test/ScriptCs.Core.Tests/ScriptPackResolverTests.cs deleted file mode 100644 index a22562df..00000000 --- a/test/ScriptCs.Core.Tests/ScriptPackResolverTests.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class ScriptPackResolverTests - { - public class TheGetPacksMethod - { - private ScriptPackResolver _packManager; - private Mock _pack1; - private Mock _pack2; - - public TheGetPacksMethod() - { - _pack1 = new Mock(); - _pack2 = new Mock(); - var packs = new[] { _pack1.Object, _pack2.Object }; - _packManager = new ScriptPackResolver(packs); - } - - [Fact] - public void ShouldRetrieveAnyPacksInTheCatalog() - { - var packs = _packManager.GetPacks(); - packs.Count().ShouldEqual(2); - packs.ShouldContain(_pack1.Object); - packs.ShouldContain(_pack2.Object); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ScriptPackSessionTests.cs b/test/ScriptCs.Core.Tests/ScriptPackSessionTests.cs deleted file mode 100644 index 68f1216a..00000000 --- a/test/ScriptCs.Core.Tests/ScriptPackSessionTests.cs +++ /dev/null @@ -1,144 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class ScriptPackSessionTests - { - public class TheConstructor - { - private ScriptPackSession _scriptPackSession; - private Mock _scriptPackMock; - private Mock _contextMock; - - public TheConstructor() - { - _scriptPackMock = new Mock(); - _contextMock = new Mock(); - _scriptPackMock.Setup(p => p.GetContext()).Returns(_contextMock.Object); - _scriptPackSession = new ScriptPackSession(new List{_scriptPackMock.Object}, new string[0]); - } - - [Fact] - public void ShouldPopulateContexts() - { - _scriptPackSession.Contexts.ShouldContain(_contextMock.Object); - } - - [Fact] - public void ShouldInitializeReferences() - { - _scriptPackSession.References.ShouldNotBeNull(); - } - - [Fact] - public void ShouldInitializeNamespaces() - { - _scriptPackSession.Namespaces.ShouldNotBeNull(); - } - - [Fact] - public void ShouldAddContextNamespaces() - { - _scriptPackSession.Namespaces.Contains(_contextMock.Object.GetType().Namespace); - } - - [Fact] - public void ShouldInitializeState() - { - _scriptPackSession.State.ShouldNotBeNull(); - } - } - - public class TheInitializeMethod - { - private ScriptPackSession _scriptPackSession; - private Mock _scriptPackMock1; - private Mock _scriptPackMock2; - private Mock _contextMock1; - private Mock _contextMock2; - - public TheInitializeMethod() - { - _contextMock1 = new Mock(); - _contextMock2 = new Mock(); - _scriptPackMock1 = new Mock(); - _scriptPackMock1.Setup(p => p.GetContext()).Returns(_contextMock1.Object); - _scriptPackMock2 = new Mock(); - _scriptPackMock2.Setup(p => p.GetContext()).Returns(_contextMock2.Object); - _scriptPackSession = new ScriptPackSession(new List { _scriptPackMock1.Object, _scriptPackMock2.Object }, new string[0]); - _scriptPackSession.InitializePacks(); - } - - [Fact] - public void ShouldCallInitializeOnAllScriptPacks() - { - _scriptPackMock1.Verify(p => p.Initialize(_scriptPackSession)); - _scriptPackMock2.Verify(p => p.Initialize(_scriptPackSession)); - } - } - - public class TheTerminateMethod - { - private ScriptPackSession _scriptPackSession; - private Mock _scriptPackMock1; - private Mock _scriptPackMock2; - private Mock _contextMock1; - private Mock _contextMock2; - - public TheTerminateMethod() - { - _contextMock1 = new Mock(); - _contextMock2 = new Mock(); - _scriptPackMock1 = new Mock(); - _scriptPackMock1.Setup(p => p.GetContext()).Returns(_contextMock1.Object); - _scriptPackMock2 = new Mock(); - _scriptPackMock2.Setup(p => p.GetContext()).Returns(_contextMock2.Object); - _scriptPackSession = new ScriptPackSession(new List { _scriptPackMock1.Object, _scriptPackMock2.Object }, new string[0]); - _scriptPackSession.TerminatePacks(); - } - - [Fact] - public void ShouldCallTerminateOnAllScriptPacks() - { - _scriptPackMock1.Verify(p => p.Terminate()); - _scriptPackMock2.Verify(p => p.Terminate()); - } - } - - public class TheAddReferenceMethod - { - private ScriptPackSession _scriptPackSession = new ScriptPackSession(Enumerable.Empty(), new string[0]); - - public TheAddReferenceMethod() - { - ((IScriptPackSession)_scriptPackSession).AddReference("ref"); - } - - [Fact] - public void ShouldAddTheReference() - { - _scriptPackSession.References.ShouldContain("ref"); - } - } - - public class TheImportNamespaceMethod - { - private ScriptPackSession _scriptPackSession = new ScriptPackSession(Enumerable.Empty(), new string[0]); - - public TheImportNamespaceMethod() - { - ((IScriptPackSession)_scriptPackSession).ImportNamespace("ns"); - } - - public void ShouldAddTheNamespace() - { - _scriptPackSession.Namespaces.ShouldContain("ns"); - } - } - } -} diff --git a/test/ScriptCs.Core.Tests/ShebangLineProcessorTests.cs b/test/ScriptCs.Core.Tests/ShebangLineProcessorTests.cs deleted file mode 100644 index 7b8ff5a3..00000000 --- a/test/ScriptCs.Core.Tests/ShebangLineProcessorTests.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Should; -using Xunit.Extensions; -using ScriptCs.Contracts; -using Xunit; - -namespace ScriptCs.Tests -{ - public class ShebangLineProcessorTests - { - public class TheProcessLineMethod - { - [Theory, ScriptCsAutoData] - public void ShouldReturnTrueOnShebangLine(IFileParser parser, ShebangLineProcessor processor) - { - // Arrange - const string Line = @"#!/usr/bin/env scriptcs"; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), Line, true); - - // Assert - result.ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnFalseOtherwise(IFileParser parser, ShebangLineProcessor processor) - { - // Arrange - const string Line = @"var x = new Test();"; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), Line, true); - - // Assert - result.ShouldBeFalse(); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/UsingLineProcessorTests.cs b/test/ScriptCs.Core.Tests/UsingLineProcessorTests.cs deleted file mode 100644 index 59cfc05b..00000000 --- a/test/ScriptCs.Core.Tests/UsingLineProcessorTests.cs +++ /dev/null @@ -1,79 +0,0 @@ -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class UsingLineProcessorTests - { - public class TheProcessLineMethod - { - [Theory, ScriptCsAutoData] - public void ShouldReturnTrueOnUsingLine(IFileParser parser, UsingLineProcessor processor) - { - // Arrange - const string UsingLine = "using System.Data;"; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), UsingLine, true); - - // Assert - result.ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnFalseOtherwise(IFileParser parser, UsingLineProcessor processor) - { - // Arrange - const string UsingLine = @"using (var x = new Disposable())"; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), UsingLine, true); - - // Assert - result.ShouldBeFalse(); - } - - [Theory, ScriptCsAutoData] - public void ShouldIgnoreAliases(IFileParser parser, UsingLineProcessor processor) - { - // Arrange - const string UsingLine = "using Path = System.IO.Path"; - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), UsingLine, true); - - // Assert - result.ShouldBeFalse(); - } - - [Theory, ScriptCsAutoData] - public void ShouldIgnoreUsingStatic(IFileParser parser, UsingLineProcessor processor) - { - // Arrange - const string UsingLine = "using static System.Console;"; - var context = new FileParserContext(); - - // Act - var result = processor.ProcessLine(parser, new FileParserContext(), UsingLine, true); - - // Assert - result.ShouldBeFalse(); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddNamespaceToContext(IFileParser parser, UsingLineProcessor processor) - { - // Arrange - const string UsingLine = "using System.Data;"; - var context = new FileParserContext(); - - // Act - processor.ProcessLine(parser, context, UsingLine, true); - - // Assert - context.Namespaces.Count.ShouldEqual(1); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Core.Tests/app.config b/test/ScriptCs.Core.Tests/app.config deleted file mode 100644 index 31080f45..00000000 --- a/test/ScriptCs.Core.Tests/app.config +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/test/ScriptCs.Engine.Roslyn.Tests/CSharpModuleTests.cs b/test/ScriptCs.Engine.Roslyn.Tests/CSharpModuleTests.cs deleted file mode 100644 index d10fed92..00000000 --- a/test/ScriptCs.Engine.Roslyn.Tests/CSharpModuleTests.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.Collections.Generic; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.Engine.Roslyn; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class CSharpModuleTests - { - public class TheInitializeMethod - { - private readonly Mock _configMock = new Mock(); - private readonly IModuleConfiguration _config; - private readonly RoslynModule _module = new RoslynModule(); - private readonly IDictionary _overrides = new Dictionary(); - - public TheInitializeMethod() - { - _configMock.SetupGet(c => c.Debug).Returns(false); - _configMock.SetupGet(c => c.IsRepl).Returns(false); - _configMock.SetupGet(c => c.Cache).Returns(false); - _configMock.SetupGet(c => c.Overrides).Returns(_overrides); - _config = _configMock.Object; - } - - [Fact] - public void ShouldNotOverrideTheEngineIfOneIsRegistered() - { - var engine = new Mock(); - _overrides[typeof (IScriptEngine)] = engine.Object; - _module.Initialize(_config); - _overrides[typeof(IScriptEngine)].ShouldEqual(engine.Object); - } - - [Fact] - public void ShouldRegisterThePersistantScriptEngineWhenCacheIsEnabled() - { - _configMock.SetupGet(c => c.Cache).Returns(true); - _module.Initialize(_config); - _overrides[typeof(IScriptEngine)].ShouldEqual(typeof(CSharpPersistentEngine)); - } - - [Fact] - public void ShouldRegisterTheScriptEngineWhenCacheIsDisabled() - { - _module.Initialize(_config); - _overrides[typeof(IScriptEngine)].ShouldEqual(typeof(CSharpScriptEngine)); - } - - [Fact] - public void ShouldRegisterTheInMemoryEngineWhenDebugIsEnabled() - { - _configMock.Setup(c => c.Debug).Returns(true); - _module.Initialize(_config); - _overrides[typeof(IScriptEngine)].ShouldEqual(typeof(CSharpScriptInMemoryEngine)); - } - - [Fact] - public void ShouldRegisterTheReplEngineWhenReplIsEnabled() - { - _configMock.Setup(c => c.IsRepl).Returns(true); - _module.Initialize(_config); - _overrides[typeof(IScriptEngine)].ShouldEqual(typeof(CSharpReplEngine)); - } - } - } -} diff --git a/test/ScriptCs.Engine.Roslyn.Tests/CSharpReplEngineTests.cs b/test/ScriptCs.Engine.Roslyn.Tests/CSharpReplEngineTests.cs deleted file mode 100644 index 85a5b243..00000000 --- a/test/ScriptCs.Engine.Roslyn.Tests/CSharpReplEngineTests.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System.Collections.ObjectModel; -using System.Linq; -using Microsoft.CodeAnalysis.CSharp.Scripting; -using Microsoft.CodeAnalysis.Scripting; -using ScriptCs.Contracts; -using ScriptCs.Engine.Roslyn; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class CSharpReplEngineTests - { - public class GetLocalVariablesMethod - { - [Theory, ScriptCsAutoData] - public void ShouldReturnDeclaredVariables([NoAutoProperties]CSharpReplEngine engine, ScriptPackSession scriptPackSession) - { - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - - engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty(), - scriptPackSession); - engine.Execute(@"var y = ""www"";", new string[0], new AssemblyReferences(), Enumerable.Empty(), - scriptPackSession); - - engine.GetLocalVariables(scriptPackSession).ShouldEqual(new Collection { "System.Int32 x", "System.String y" }); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnOnlyLastValueOfVariablesDeclaredManyTimes([NoAutoProperties]CSharpReplEngine engine, ScriptPackSession scriptPackSession) - { - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - - engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty(), scriptPackSession); - engine.Execute("int x = 2;", new string[0], new AssemblyReferences(), Enumerable.Empty(), scriptPackSession); - - engine.GetLocalVariables(scriptPackSession).ShouldEqual(new Collection { "System.Int32 x" }); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturn0VariablesAfterReset([NoAutoProperties]CSharpReplEngine engine, ScriptPackSession scriptPackSession) - { - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - - engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty(), - scriptPackSession); - engine.Execute(@"var y = ""www"";", new string[0], new AssemblyReferences(), Enumerable.Empty(), - scriptPackSession); - - scriptPackSession.State[CommonScriptEngine.SessionKey] = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - - engine.GetLocalVariables(scriptPackSession).ShouldBeEmpty(); - } - } - - public class TheExecuteMethod - { - [Theory, ScriptCsAutoData] - public void ShouldSetIsCompleteSubmissionToFalseIfCodeIsMissingCurlyBracket( - [NoAutoProperties] CSharpReplEngine engine, ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "class test {"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System" }); - - // Act - var result = engine.Execute( - Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.IsCompleteSubmission.ShouldBeFalse(); - } - - [Theory, ScriptCsAutoData] - public void ShouldSetIsCompleteSubmissionToFalseIfCodeIsMissingParenthesis( - [NoAutoProperties] CSharpReplEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "System.Diagnostics.Debug.WriteLine(\"a\""; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.IsCompleteSubmission.ShouldBeFalse(); - } - - [Theory, ScriptCsAutoData] - public void ShouldSetIsCompleteSubmissionToFalseIfCodeIsMissingSquareBracket( - [NoAutoProperties] CSharpReplEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "var x = new[1] { 1 }; var y = x[0"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.IsCompleteSubmission.ShouldBeFalse(); - } - } - } -} diff --git a/test/ScriptCs.Engine.Roslyn.Tests/CSharpScriptEngineTests.cs b/test/ScriptCs.Engine.Roslyn.Tests/CSharpScriptEngineTests.cs deleted file mode 100644 index 30dd958b..00000000 --- a/test/ScriptCs.Engine.Roslyn.Tests/CSharpScriptEngineTests.cs +++ /dev/null @@ -1,369 +0,0 @@ -using System.Linq; -using System.Reflection; -using Microsoft.CodeAnalysis.CSharp.Scripting; -using Microsoft.CodeAnalysis.Scripting; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.Engine.Roslyn; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class CSharpScriptEngineTests - { - public class TheExecuteMethod - { - private IConsole _console = new Mock().Object; - private IObjectSerializer _serializer = new Mock().Object; - private Printers _printers; - - public TheExecuteMethod() - { - _printers = new Printers(_serializer); - } - - [Theory, ScriptCsAutoData] - public void ShouldCreateScriptHostWithContexts( - [Frozen] Mock scriptHostFactory, - [Frozen] Mock scriptPack, - ScriptPackSession scriptPackSession, - [NoAutoProperties] CSharpScriptEngine engine) - { - // Arrange - const string Code = "var a = 0;"; - - scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny(), It.IsAny())) - .Returns((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers))); - - scriptPack.Setup(p => p.Initialize(It.IsAny())); - scriptPack.Setup(p => p.GetContext()).Returns((IScriptPackContext)null); - - // Act - engine.Execute(Code, new string[0], new AssemblyReferences(), Enumerable.Empty(), scriptPackSession); - - // Assert - scriptHostFactory.Verify(f => f.CreateScriptHost(It.IsAny(), It.IsAny())); - } - - [Theory, ScriptCsAutoData] - public void ShouldReuseExistingSessionIfProvided( - [Frozen] Mock scriptHostFactory, - [NoAutoProperties] CSharpTestScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "var a = 0;"; - - scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny(), It.IsAny())) - .Returns((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers))); - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - - // Act - engine.Execute(Code, new string[0], new AssemblyReferences(), Enumerable.Empty(), scriptPackSession); - - // Assert - engine.Session.ShouldEqual(session.Session); - } - - [Fact] - public void ShouldCreateNewSessionIfNotProvided() - { - var scriptHostFactory = new Mock(); - scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny(), It.IsAny())) - .Returns((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers))); - - // Arrange - var engine = new CSharpTestScriptEngine(scriptHostFactory.Object, new TestLogProvider()); - const string Code = "var a = 0;"; - - // Act - engine.Execute(Code, new string[0], new AssemblyReferences(), Enumerable.Empty(), new ScriptPackSession(new IScriptPack[0], new string[0])); - - // Assert - engine.Session.ShouldNotBeNull(); - } - - [Theory, ScriptCsAutoData] - public void ShouldAddNewReferencesIfTheyAreProvided( - [NoAutoProperties] CSharpTestScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "var a = 0;"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - ((SessionState)scriptPackSession.State[CommonScriptEngine.SessionKey]).References.Paths.Count().ShouldEqual(2); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnAScriptResult( - [NoAutoProperties] CSharpTestScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - var code = string.Empty; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.ShouldBeType(); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnCompileExceptionIfCodeDoesNotCompile( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "this shold not compile"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.CompileExceptionInfo.ShouldNotBeNull(); - } - - [Theory(Skip = "this feature is not supported in Roslyn 1.0.0-rc2: see https://github.com/dotnet/roslyn/issues/1012"), ScriptCsAutoData] - public void ShouldReturnInvalidNamespacesIfCS0241Encountered( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - - // Act - var result = engine.Execute(string.Empty, new string[0], new AssemblyReferences(), new[] { "foo" }, scriptPackSession); - - // Assert - result.CompileExceptionInfo.ShouldNotBeNull(); - result.InvalidNamespaces.ShouldContain("foo"); - } - - //todo: filip: this test will not even compile as it used to do reflection old roslyn assemblies! - //[Theory, ScriptCsAutoData] - //public void ShouldRemoveInvalidNamespacesFromSessionStateAndEngine( - // [Frozen] Mock scriptHostFactory, - // [NoAutoProperties] CSharpScriptEngine engine, - // ScriptPackSession scriptPackSession) - //{ - // var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - // scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - - // // Act - // engine.Execute(string.Empty, new string[0], new AssemblyReferences(), new[] { "System", "foo" }, scriptPackSession); - - // // Assert - // session.Namespaces.ShouldNotBeEmpty(); - // session.Namespaces.ShouldNotContain("foo"); - // var pendingNamespacesField = session.Session.GetType().GetField("pendingNamespaces", BindingFlags.Instance | BindingFlags.NonPublic); - // var pendingNamespacesValue = (ReadOnlyArray)pendingNamespacesField.GetValue(session.Session); - // pendingNamespacesValue.AsEnumerable().ShouldNotBeEmpty(); - // pendingNamespacesValue.AsEnumerable().ShouldNotContain("foo"); - //} - - [Theory, ScriptCsAutoData] - public void ShouldNotReturnCompileExceptionIfCodeDoesCompile( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "var theNumber = 42; //this should compile"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.CompileExceptionInfo.ShouldBeNull(); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnExecuteExceptionIfCodeExecutionThrowsException( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "throw new System.Exception(); //this should throw an Exception"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.ExecuteExceptionInfo.ShouldNotBeNull(); - } - - [Theory, ScriptCsAutoData] - public void ShouldNotReturnExecuteExceptionIfCodeExecutionDoesNotThrowAnException( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "var theNumber = 42; //this should not throw an Exception"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.ExecuteExceptionInfo.ShouldBeNull(); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnReturnValueIfCodeExecutionReturnsValue( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - const string Code = "\"Hello\" //this should return \"Hello\""; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.ReturnValue.ShouldEqual("Hello"); - } - - [Theory, ScriptCsAutoData] - public void ShouldNotReturnReturnValueIfCodeExecutionDoesNotReturnValue( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "var theNumber = 42; //this should not return a value"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.ReturnValue.ShouldBeNull(); - } - - [Theory, ScriptCsAutoData] - public void ShouldNotMarkSubmissionsAsIncompleteWhenRunningScript( - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "class test {"; - - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - engine.FileName = "test.csx"; - - // Act - var result = engine.Execute( - Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.IsCompleteSubmission.ShouldBeTrue(); - result.CompileExceptionInfo.ShouldNotBeNull(); - } - - [Theory, ScriptCsAutoData] - public void ShouldCompileWhenUsingClassesFromAPassedAssemblyInstance( - [Frozen] Mock scriptHostFactory, - [Frozen] ScriptPackSession scriptPackSession) - { - // Arrange - const string Code = "var x = new ScriptCs.Tests.TestMarkerClass();"; - - scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny(), It.IsAny())) - .Returns((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers))); - - var engine = new CSharpScriptEngine(scriptHostFactory.Object, new TestLogProvider()); - var session = new SessionState { Session = CSharpScript.RunAsync("").GetAwaiter().GetResult() }; - scriptPackSession.State[CommonScriptEngine.SessionKey] = session; - var refs = new AssemblyReferences(new[] { Assembly.GetExecutingAssembly() }, new[] { "System", "System.Runtime" }); - - // Act - var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - result.CompileExceptionInfo.ShouldBeNull(); - result.ExecuteExceptionInfo.ShouldBeNull(); - } - - - [Theory, ScriptCsAutoData] - public void ShouldInitializeScriptLibraryWrapperHost( - [Frozen] Mock scriptHostFactory, - Mock manager, - [NoAutoProperties] CSharpScriptEngine engine, - ScriptPackSession scriptPackSession - ) - { - // Arrange - const string Code = "var theNumber = 42; //this should compile"; - - var refs = new AssemblyReferences(new[] { "System", "System.Runtime" }); - - scriptHostFactory.Setup(s => s.CreateScriptHost(It.IsAny(), It.IsAny())) - .Returns(new ScriptHost(manager.Object, null)); - - // Act - engine.Execute(Code, new string[0], refs, Enumerable.Empty(), scriptPackSession); - - // Assert - ScriptLibraryWrapper.ScriptHost.ShouldNotEqual(null); - } - } - - public class CSharpTestScriptEngine : CSharpScriptEngine - { - public CSharpTestScriptEngine(IScriptHostFactory scriptHostFactory, TestLogProvider logProvider) - : base(scriptHostFactory, logProvider) - { - } - - public ScriptState Session { get; private set; } - - protected override ScriptResult Execute(string code, object globals, SessionState sessionState) - { - base.Execute(code, globals, sessionState); - Session = sessionState.Session; - return ScriptResult.Empty; - } - } - } - - public class TestMarkerClass { } -} diff --git a/test/ScriptCs.Engine.Roslyn.Tests/CSharpScriptInMemoryEngineTests.cs b/test/ScriptCs.Engine.Roslyn.Tests/CSharpScriptInMemoryEngineTests.cs deleted file mode 100644 index 1de12db6..00000000 --- a/test/ScriptCs.Engine.Roslyn.Tests/CSharpScriptInMemoryEngineTests.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.Engine.Roslyn; -using ScriptCs.Exceptions; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class CSharpScriptInMemoryEngineTests - { - public class TheExecuteMethod - { - private IConsole _console = new Mock().Object; - private IObjectSerializer _serializer = new Mock().Object; - private Printers _printers; - - public TheExecuteMethod() - { - _printers = new Printers(_serializer); - } - - [Fact] - public void ShouldExposeExceptionThrownByScriptWhenErrorOccurs() - { - var scriptEngine = new CSharpScriptInMemoryEngine(new ScriptHostFactory(_console, _printers, new ScriptInfo()), new TestLogProvider()); - // Arrange - var lines = new List - { - "using System;", - @"throw new InvalidOperationException(""InvalidOperationExceptionMessage."");" - }; - - var code = string.Join(Environment.NewLine, lines); - var session = new ScriptPackSession(Enumerable.Empty(), new string[0]); - - // Act - var result = scriptEngine.Execute(code, new string[0], new AssemblyReferences(), Enumerable.Empty(), - session); - - // Assert - var exception = Assert.Throws(() => result.ExecuteExceptionInfo.Throw()); - exception.StackTrace.ShouldContain("Submission#0"); - exception.Message.ShouldContain("InvalidOperationExceptionMessage"); - } - - [Fact] - public void ShouldExposeExceptionThrownByCompilation() - { - var scriptEngine = new CSharpScriptInMemoryEngine(new ScriptHostFactory(_console, _printers, new ScriptInfo()), new TestLogProvider()); - - // Arrange - var lines = new List - { - "Sysasdasdasdtem;" - }; - - var code = string.Join(Environment.NewLine, lines); - var session = new ScriptPackSession(Enumerable.Empty(), new string[0]); - - // Act - var result = scriptEngine.Execute(code, new string[0], new AssemblyReferences(), Enumerable.Empty(), - session); - - // Assert - var exception = Assert.Throws(() => result.CompileExceptionInfo.Throw()); - exception.Message.ShouldContain("error CS0103: The name 'Sysasdasdasdtem' does not exist in the current context"); - } - } - } -} diff --git a/test/ScriptCs.Engine.Roslyn.Tests/ScriptCs.Engine.Roslyn.Tests.csproj b/test/ScriptCs.Engine.Roslyn.Tests/ScriptCs.Engine.Roslyn.Tests.csproj deleted file mode 100644 index bb8f8ea1..00000000 --- a/test/ScriptCs.Engine.Roslyn.Tests/ScriptCs.Engine.Roslyn.Tests.csproj +++ /dev/null @@ -1,29 +0,0 @@ - - - net461 - win - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/test/ScriptCs.Engine.Roslyn.Tests/app.config b/test/ScriptCs.Engine.Roslyn.Tests/app.config deleted file mode 100644 index 2aaf612c..00000000 --- a/test/ScriptCs.Engine.Roslyn.Tests/app.config +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/test/ScriptCs.Hosting.Tests/ModuleConfigurationTests.cs b/test/ScriptCs.Hosting.Tests/ModuleConfigurationTests.cs deleted file mode 100644 index 68b8ccb2..00000000 --- a/test/ScriptCs.Hosting.Tests/ModuleConfigurationTests.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Hosting.Tests -{ - public class ModuleConfigurationTests - { - public class TheLineProcessorMethod - { - [Fact] - public void ShouldAddTheLineProcessorTypeToTheOverridesDictionary() - { - var overrides = new Dictionary(); - - var moduleConfiguration = new ModuleConfiguration(false, "script1.csx", false, LogLevel.Debug, false, overrides); - moduleConfiguration.LineProcessor(); - - var processors = overrides[typeof(ILineProcessor)] as List; - processors.ShouldContain(typeof(UsingLineProcessor)); - } - - [Fact] - public void ShouldReturnTheModuleConfiguration() - { - var moduleConfiguration = new ModuleConfiguration(false, "script1.csx", false, LogLevel.Debug, false, new Dictionary()); - var config = moduleConfiguration.LineProcessor(); - config.ShouldImplement(); - config.ShouldEqual(moduleConfiguration); - } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/ModuleLoaderTests.cs b/test/ScriptCs.Hosting.Tests/ModuleLoaderTests.cs deleted file mode 100644 index 505b8d3d..00000000 --- a/test/ScriptCs.Hosting.Tests/ModuleLoaderTests.cs +++ /dev/null @@ -1,167 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition.Hosting; -using System.IO; -using System.Linq; -using System.Reflection; -using Moq; -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Hosting.Tests -{ - using ScriptCs.Tests; - - public class ModuleLoaderTests - { - public class TheLoadMethod - { - private Mock _mockAssemblyResolver = new Mock(); - private IList> _modules = new List>(); - private Func>> _getModules; - private TestLogProvider _logProvider = new TestLogProvider(); - private Mock _mockModule1 = new Mock(); - private Mock _mockModule2 = new Mock(); - private Mock _mockModule3 = new Mock(); - private Mock _mockModule4 = new Mock(); - private Mock _mockFileSystem = new Mock(); - private Mock _mockAssemblyUtility = new Mock(); - - public TheLoadMethod() - { - var paths = new[] { "path1.dll", "path2.dll" }; - _mockAssemblyResolver.Setup(r => r.GetAssemblyPaths(It.IsAny(), true)).Returns(paths); - _modules.Add( - new Lazy( - () => _mockModule1.Object, new ModuleMetadata { Extensions = "ext1,ext2", Name = "module1" })); - _modules.Add( - new Lazy( - () => _mockModule2.Object, new ModuleMetadata { Extensions = "ext3,ext4", Name = "module2" })); - _modules.Add(new Lazy(() => _mockModule3.Object, new ModuleMetadata { Name = "module3" })); - _modules.Add(new Lazy(() => _mockModule4.Object, new ModuleMetadata { Name = "module4", Autoload = true })); - _getModules = c => _modules; - _mockFileSystem.Setup(f=>f.EnumerateFiles(It.IsAny(), It.IsAny(), It.IsAny())).Returns(Enumerable.Empty()); - _mockAssemblyUtility.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true); - } - - [Fact] - public void ShouldResolvePathsFromTheAssemblyResolver() - { - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (p, c) => { }, c => Enumerable.Empty>(), _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new[] { "c:\test" }, null, null); - _mockAssemblyResolver.Verify(r => r.GetAssemblyPaths("c:\test", true)); - } - - [Fact] - public void ShouldInvokeTheCatalogActionForEachFile() - { - var assemblies = new List(); - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => assemblies.Add(a), c => Enumerable.Empty>(), _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new[] { "c:\test" }, null, null); - assemblies.Count.ShouldEqual(2); - } - - [Fact] - public void ShouldIgnoreLoadingNativeAssemblies() - { - _mockAssemblyUtility.Setup(a => a.IsManagedAssembly(It.Is(f => f == "managed.dll"))).Returns(true); - _mockAssemblyUtility.Setup(a => a.IsManagedAssembly(It.Is(f => f == "native.dll"))).Returns(false); - var mockAssemblies = new List {"managed.dll", "native.dll"}; - _mockAssemblyResolver.Setup(a => a.GetAssemblyPaths(It.IsAny(), true)).Returns(mockAssemblies); - var assemblies = new List(); - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => assemblies.Add(a), c => Enumerable.Empty>(), _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new[] { "c:\test" }, null, null); - assemblies.Count.ShouldEqual(1); - } - - [Fact] - public void ShouldInitializeModulesThatMatchOnExtension() - { - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new string[0], null, "ext1"); - _mockModule1.Verify(m => m.Initialize(It.IsAny()), Times.Once()); - _mockModule2.Verify(m => m.Initialize(It.IsAny()), Times.Never()); - _mockModule3.Verify(m => m.Initialize(It.IsAny()), Times.Never()); - } - - [Fact] - public void ShouldInitializeModulesThatMatchBasedOnName() - { - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new string[0], null, null, "module3"); - _mockModule1.Verify(m => m.Initialize(It.IsAny()), Times.Never()); - _mockModule2.Verify(m => m.Initialize(It.IsAny()), Times.Never()); - _mockModule3.Verify(m => m.Initialize(It.IsAny()), Times.Once()); - } - - [Fact] - public void ShouldInitializeModulesThatAreSetToAutoload() - { - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new string[0], null, null); - _mockModule4.Verify(m => m.Initialize(It.IsAny()), Times.Once()); - } - - [Fact] - public void ShouldNotInitializeModulesThatAreNotSetToAutoload() - { - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new string[0], null, null); - _mockModule1.Verify(m => m.Initialize(It.IsAny()), Times.Never()); - } - - [Fact] - public void ShouldLoadEngineAssemblyByHandIfItsTheOnlyModule() - { - var path = Path.Combine("c:\\foo", ModuleLoader.DefaultCSharpModules["roslyn"]); - _mockAssemblyUtility.Setup(x => x.LoadFile(path)); - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object); - loader.Load(null, new string[0], "c:\\foo", ModuleLoader.DefaultCSharpExtension, "roslyn"); - - _mockAssemblyUtility.Verify(x => x.LoadFile(path), Times.Once()); - } - - [Fact] - public void ShouldLoadEngineModuleFromFile() - { - _mockAssemblyUtility.Setup(x => x.LoadFile(It.IsAny())).Returns(typeof (DummyModule).Assembly); - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object); - - var config = new ModuleConfiguration(true, string.Empty, false, LogLevel.Debug, true, - new Dictionary {{typeof (string), "not loaded"}}); - loader.Load(config, new string[0], "c:\\foo", ModuleLoader.DefaultCSharpExtension, "roslyn"); - - config.Overrides[typeof(string)].ShouldEqual("module loaded"); - } - - [Fact] - public void ShouldNotLoadEngineAssemblyByHandIfItsTheOnlyModuleButExtensionIsNotDefault() - { - var path = Path.Combine("c:\\foo", ModuleLoader.DefaultCSharpModules["roslyn"]); - _mockAssemblyUtility.Setup(x => x.LoadFile(path)); - var loader = new ModuleLoader(_mockAssemblyResolver.Object, _logProvider, (a, c) => { }, _getModules, _mockFileSystem.Object, _mockAssemblyUtility.Object); - - loader.Load(null, new string[0], "c:\\foo", ".fsx", "roslyn"); - _mockAssemblyUtility.Verify(x => x.LoadFile(It.IsAny()), Times.Never); - } - - public class ModuleMetadata : IModuleMetadata - { - public string Name { get; set; } - - public string Extensions { get; set; } - - public bool Autoload { get; set; } - } - - public class DummyModule : IModule - { - public void Initialize(IModuleConfiguration config) - { - config.Overrides[typeof (string)] = "module loaded"; - } - } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/ObjectSerializerTests.cs b/test/ScriptCs.Hosting.Tests/ObjectSerializerTests.cs deleted file mode 100644 index 0be8e25f..00000000 --- a/test/ScriptCs.Hosting.Tests/ObjectSerializerTests.cs +++ /dev/null @@ -1,132 +0,0 @@ -using System; -using Should; -using Xunit; -using System.Diagnostics; - -namespace ScriptCs.Hosting.Tests -{ - public class ObjectSerializerTests - { - public class TheSerializeMethod - { - private readonly ObjectSerializer _serializer; - - public TheSerializeMethod() - { - _serializer = new ObjectSerializer(); - } - - [Fact] - public void ShouldSerialize() - { - // arrange - var obj = new Foo - { - Bar = new Bar - { - Baz = true, - Bazz = 123.4, - Bazzz = "hello", - }, - }; - - // act - var result = _serializer.Serialize(obj); - - // assert - result.ShouldEqual( -@"{ - ""Bar"": { - ""Baz"": true, - ""Bazz"": 123.4, - ""Bazzz"": ""hello"" - } -}"); - } - - [Fact] - public void ShouldSerializeTypeMethods() - { - // arrange - var obj = typeof(Type).GetMethods(); - - // act - var exception = Record.Exception(() => _serializer.Serialize(obj)); - - // assert - exception.ShouldBeNull(); - } - - [Fact] - public void ShouldSerializeDelegates() - { - // arrange - var obj = new FuncAndAction - { - Action = () => { }, - Func = () => "Hello World", - }; - - // act - var exception = Record.Exception(() => _serializer.Serialize(obj)); - - // assert - exception.ShouldBeNull(); - } - - [Fact] - public void ShouldNotShowExceptionForUnserializableTypes() - { - var obj = new Process(); - - string result = null; - var exception = Record.Exception(() => result = _serializer.Serialize(obj)); - exception.ShouldBeNull(); - Assert.Equal("Couldn't serialize a returned instance of System.Diagnostics.Process", result); - } - - [Fact] - public void ShouldSerializeWithCircularReferences() - { - // arrange - var obj = new Circular(); - obj.Parent = obj; - - // act - var result = _serializer.Serialize(obj); - - // assert - result.ShouldEqual( -@"{ - ""$id"": ""1"", - ""Parent"": { - ""$ref"": ""1"" - } -}"); - } - - private class Foo - { - public Bar Bar { get; set; } - } - - private class Bar - { - public bool Baz { get; set; } - public double Bazz { get; set; } - public string Bazzz { get; set; } - } - - private class FuncAndAction - { - public Action Action { get; set; } - public Func Func { get; set; } - } - - private class Circular - { - public Circular Parent { get; set; } - } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/PackageInstallerTests.cs b/test/ScriptCs.Hosting.Tests/PackageInstallerTests.cs deleted file mode 100644 index c0947536..00000000 --- a/test/ScriptCs.Hosting.Tests/PackageInstallerTests.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using Moq; -using NuGet; -using ScriptCs.Contracts; -using ScriptCs.Hosting.Package; -using ScriptCs.Tests; -using Should; -using Xunit; - -namespace ScriptCs.Hosting.Tests -{ - public class PackageInstallerTests - { - public class InstallPackagesMethod - { - [Fact] - public void ShouldThrowArgumentNullExWhenNoPackageIdsPassed() - { - var installer = new PackageInstaller(new Mock().Object, new TestLogProvider()); - Assert.Throws(() => installer.InstallPackages(null)); - } - - [Fact] - public void ShouldInstallAllPassedPackages() - { - var provider = new Mock(); - - var references = new List - { - new PackageReference("testId", VersionUtility.ParseFrameworkName("net40"), new Version("3.0")), - new PackageReference("testId2", VersionUtility.ParseFrameworkName("net40"), new Version("4.0")), - new PackageReference("testId3", VersionUtility.ParseFrameworkName("net40"), new Version("5.0")) - }; - - var installer = new PackageInstaller(provider.Object, new TestLogProvider()); - installer.InstallPackages(references); - - provider.Verify(i => i.InstallPackage(It.IsAny(), It.IsAny()), Times.Exactly(3)); - } - - [Fact] - public void ShouldShowErrorIfOneOfPackagesFail() - { - var provider = new Mock(); - provider.Setup( - i => i.InstallPackage(It.Is(x => x.PackageId == "testId"), It.IsAny())) - .Throws(); - - var references = new List - { - new PackageReference("testId", VersionUtility.ParseFrameworkName("net40"), new Version("3.0")), - new PackageReference("testId2", VersionUtility.ParseFrameworkName("net40"), new Version("4.0")), - new PackageReference("testId3", VersionUtility.ParseFrameworkName("net40"), new Version("5.0")) - }; - - var installer = new PackageInstaller(provider.Object, new TestLogProvider()); - var exception = Record.Exception(() => installer.InstallPackages(references, true)); - - provider.Verify(i => i.InstallPackage(It.IsAny(), It.IsAny()), Times.Exactly(3)); - exception.ShouldBeType(); - ((AggregateException)exception).InnerExceptions.Count.ShouldEqual(1); - } - - [Fact] - public void ShouldNotInstallExistingPackages() - { - var provider = new Mock(); - provider.Setup( - i => i.IsInstalled(It.Is(x => x.PackageId == "testId"), It.IsAny())) - .Returns(true); - - var references = new List - { - new PackageReference("testId", VersionUtility.ParseFrameworkName("net40"), new Version("3.0")), - new PackageReference("testId2", VersionUtility.ParseFrameworkName("net40"), new Version("4.0")), - new PackageReference("testId3", VersionUtility.ParseFrameworkName("net40"), new Version("5.0")) - }; - - var installer = new PackageInstaller(provider.Object, new TestLogProvider()); - installer.InstallPackages(references); - - provider.Verify(i => i.InstallPackage(It.Is(x => x.PackageId == "testId"), It.IsAny()), Times.Never()); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Hosting.Tests/ReplCommands/OpenVSCommandTests.cs b/test/ScriptCs.Hosting.Tests/ReplCommands/OpenVSCommandTests.cs deleted file mode 100644 index ef8f66f1..00000000 --- a/test/ScriptCs.Hosting.Tests/ReplCommands/OpenVSCommandTests.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Moq; -using Moq.Protected; -using ScriptCs.Contracts; -using ScriptCs.Hosting.ReplCommands; -using System; -using System.Collections.Generic; -using Xunit; - -namespace ScriptCs.Hosting.Tests.ReplCommands -{ - public class OpenVsCommandTests - { - public class TheExecuteCommand - { - private Mock _mockCommand; - private OpenVsCommand _command; - private Mock _mockConsole; - private Mock _mockWriter; - private Mock _mockRepl; - - public TheExecuteCommand() - { - _mockConsole = new Mock(); - _mockWriter = new Mock(); - _mockRepl = new Mock(); - _mockRepl.SetupGet(r => r.FileSystem).Returns(new FileSystem()); - _mockCommand = new Mock(_mockConsole.Object, _mockWriter.Object); - _mockCommand.Setup(c => c.PlatformID).Returns(PlatformID.Win32NT); - _mockCommand.Setup(c => c.LaunchSolution(It.IsAny())); - _mockCommand.Protected(); - _command = _mockCommand.Object; - } - - [Fact] - public void OutputsAMessageIfNotWindows8() - { - _mockCommand.Setup(c => c.PlatformID).Returns(PlatformID.MacOSX); - _command.Execute(_mockRepl.Object, new object[] { "test.csx" }); - _mockConsole.Verify(c => c.WriteLine("Requires Windows 8 or later to run")); - } - - [Fact] - public void CreatesTheSolution() - { - _command.Execute(_mockRepl.Object, new object[] { "test.csx" }); - _mockWriter.Verify( - w => - w.WriteSolution(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny>())); - } - - [Fact] - public void LaunchesTheSolution() - { - _command.Execute(_mockRepl.Object, new object[] { "test.csx" }); - _mockCommand.Verify(c => c.LaunchSolution(It.IsAny())); - } - - [Fact] - public void LaunchesTheSolutionEmptyParameter() - { - _command.Execute(_mockRepl.Object, new object[] { }); - _mockCommand.Verify(c => c.LaunchSolution(It.IsAny())); - } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/RuntimeServicesTests.cs b/test/ScriptCs.Hosting.Tests/RuntimeServicesTests.cs deleted file mode 100644 index b5b888c3..00000000 --- a/test/ScriptCs.Hosting.Tests/RuntimeServicesTests.cs +++ /dev/null @@ -1,858 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using Autofac; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.Tests; -using Should; -using Xunit; - -namespace ScriptCs.Hosting.Tests -{ - public class RuntimeServicesTests - { - public class TheContainerProperty - { - private readonly Mock _mockConsole = new Mock(); - private readonly Type _scriptExecutorType; - private readonly Type _replType; - private readonly Type _scriptEngineType; - private readonly TestLogProvider _logProvider = new TestLogProvider(); - private readonly IDictionary _overrides = new Dictionary(); - private readonly RuntimeServices _runtimeServices; - - public TheContainerProperty() - { - _overrides[typeof(ILineProcessor)] = new List(); - var mockScriptExecutor = new MockScriptExecutor(); - _scriptExecutorType = mockScriptExecutor.GetType(); - - var mockReplType = new Mock(); - _replType = mockReplType.Object.GetType(); - - var mockScriptEngine = new MockScriptEngine(); - _scriptEngineType = mockScriptEngine.GetType(); - - var initializationServices = new InitializationServices(_logProvider, _overrides); - _runtimeServices = new RuntimeServices( - _logProvider, - _overrides, - _mockConsole.Object, - _scriptEngineType, - _scriptExecutorType, - _replType, - false, - initializationServices, - "script.csx"); - } - - [Fact] - public void ShouldRegisterTheLoggerInstance() - { - var logProvider = _runtimeServices.Container.Resolve(); - logProvider.ShouldEqual(_logProvider); - } - - [Fact] - public void ShouldRegisterTheScriptEngine() - { - var engine = _runtimeServices.Container.Resolve(); - engine.GetType().ShouldEqual(_scriptEngineType); - } - - [Fact] - public void ShouldRegisterTheExecutor() - { - var executor = _runtimeServices.Container.Resolve(); - executor.GetType().ShouldEqual(_scriptExecutorType); - } - - [Fact] - public void ShouldRegisterTheConsoleInstance() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheScriptServices() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultScriptHostFactoryIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultFilePreProcessorIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultScriptPackResolverIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultInstallationProviderIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultPackageInstallerIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultScriptServiceRootIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultFileSystemIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultAssemblyUtilityIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultPackageContainerIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultPackageAssemblyResolverIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultAssemblyResolverIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultVisualStudioSolutionWriterIfNoOverride() - { - _runtimeServices.Container.IsRegistered().ShouldBeTrue(); - } - - [Fact] - public void ShouldRegisterTheDefaultLineProcessors() - { - var processors = _runtimeServices.Container.Resolve>(); - processors.ShouldNotBeNull(); - processors = processors.ToArray(); - processors.Where(p => p is IUsingLineProcessor).ShouldNotBeEmpty(); - processors.Where(p => p is IReferenceLineProcessor).ShouldNotBeEmpty(); - processors.Where(p => p is ILoadLineProcessor).ShouldNotBeEmpty(); - processors.Where(p => p is IShebangLineProcessor).ShouldNotBeEmpty(); - } - - [Fact] - public void ShouldRegisterACustomLineProcessor() - { - var processorList = _overrides[typeof(ILineProcessor)] as List; - processorList.ShouldNotBeNull(); - processorList.Add(typeof(MockLineProcessor)); - - var processors = _runtimeServices.Container.Resolve>(); - processors.ShouldNotBeNull(); - processors.Where(p => p.GetType() == typeof(MockLineProcessor)).ShouldNotBeEmpty(); - } - - [Fact] - public void ShouldRegisterTheOverriddenScriptHostFactory() - { - _overrides[typeof(IScriptHostFactory)] = typeof(MockScriptHostFactory); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockScriptHostFactory)); - } - - [Fact] - public void ShouldRegisterTheOverriddenFilePreProcessor() - { - var mock = new Mock(); - _overrides[typeof(IFilePreProcessor)] = typeof(MockFilePreProcessor); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockFilePreProcessor)); - } - - [Fact] - public void ShouldRegisterTheOverriddenScriptPackResolver() - { - _overrides[typeof(IScriptPackResolver)] = typeof(MockScriptPackResolver); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockScriptPackResolver)); - } - - [Fact] - public void ShouldRegisterTheOverriddenInstallationProvider() - { - _overrides[typeof(IInstallationProvider)] = typeof(MockInstallationProvider); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockInstallationProvider)); - } - - [Fact] - public void ShouldRegisterTheOverriddenPackageInstaller() - { - _overrides[typeof(IPackageInstaller)] = typeof(MockPackageInstaller); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockPackageInstaller)); - } - - [Fact] - public void ShouldRegisterTheOverriddenFileSystem() - { - _overrides[typeof(IFileSystem)] = typeof(MockFileSystem); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockFileSystem)); - } - - [Fact] - public void ShouldRegisterTheOverriddenAssemblyUtility() - { - _overrides[typeof(IAssemblyUtility)] = typeof(MockAssemblyUtility); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockAssemblyUtility)); - } - - [Fact] - public void ShouldRegisterTheOverriddenConsole() - { - _overrides[typeof(IConsole)] = typeof(MockConsole); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockConsole)); - } - - [Fact] - public void ShouldRegisterTheOverriddenPackageContainer() - { - _overrides[typeof(IPackageContainer)] = typeof(MockPackageContainer); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockPackageContainer)); - } - - [Fact] - public void ShouldRegisterTheOverriddenPackageAssemblyResolver() - { - _overrides[typeof(IPackageAssemblyResolver)] = typeof(MockPackageAssemblyResolver); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockPackageAssemblyResolver)); - } - - [Fact] - public void ShouldRegisterTheOverriddenAssemblyResolver() - { - _overrides[typeof(IAssemblyResolver)] = typeof(MockAssemblyResolver); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockAssemblyResolver)); - } - - [Fact] - public void ShouldRegisterTheOverriddenAssemblyResolverInstance() - { - var mock = new MockAssemblyResolver(); - _overrides[typeof(IAssemblyResolver)] = mock; - _runtimeServices.Container.Resolve().ShouldEqual(mock); - } - - [Fact] - public void ShouldRegisterTheOverriddenVisualStudioSolutionWriter() - { - _overrides[typeof(IVisualStudioSolutionWriter)] = typeof(MockVisualStudioSolutionWriter); - _runtimeServices.Container.Resolve().ShouldBeType(typeof(MockVisualStudioSolutionWriter)); - } - - [Fact] - public void ShouldRegisterTheOverriddenRepl() - { - var mock = new Mock(); - _overrides[ typeof( IRepl ) ] = mock.Object.GetType(); - _runtimeServices.Container.Resolve().ShouldBeType( mock.Object.GetType() ); - } - - [Fact] - public void ShouldLogOnDebugAnAssemblyLoadFailure() - { - // arrange - var mockResolver = new Mock(); - mockResolver.Setup(a => a.GetAssemblyPaths(It.IsAny(), false)).Returns(new[] { "/foo.dll" }); - _overrides[typeof(IAssemblyResolver)] = mockResolver.Object; - - var mockAssemblyUtility = new Mock(); - mockAssemblyUtility.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true); - _overrides[typeof(IAssemblyUtility)] = mockAssemblyUtility.Object; - - var initializationServices = new InitializationServices(_logProvider, _overrides); - var runtimeServices = new RuntimeServices( - _logProvider, - _overrides, - _mockConsole.Object, - _scriptEngineType, - _scriptExecutorType, - _replType, - true, - initializationServices, - "script.csx"); - - // act - var container = runtimeServices.Container; - - // assert - _logProvider.Output.ShouldContain( - "DEBUG: Failure loading assembly: /foo.dll. Exception: Could not load file or assembly 'foo.dll' or one of its dependencies. The system cannot find the file specified."); - } - - [Fact] - public void ShouldLogAGeneralWarningOnAnAssemblyLoadFailureWhenRunningScript() - { - // arrange - var mockResolver = new Mock(); - mockResolver.Setup(a => a.GetAssemblyPaths(It.IsAny(), false)).Returns(new[] { "/foo.dll" }); - _overrides[typeof(IAssemblyResolver)] = mockResolver.Object; - - var mockAssemblyUtility = new Mock(); - mockAssemblyUtility.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true); - _overrides[typeof(IAssemblyUtility)] = mockAssemblyUtility.Object; - - var initializationServices = new InitializationServices(_logProvider, _overrides); - var runtimeServices = new RuntimeServices( - _logProvider, - _overrides, - _mockConsole.Object, - _scriptEngineType, - _scriptExecutorType, - _replType, - true, - initializationServices, - "script.csx"); - - // act - var container = runtimeServices.Container; - - // assert - _logProvider.Output.ShouldContain( - "WARN: Some assemblies failed to load. Launch with '-loglevel debug' to see the details"); - } - - [Fact] - public void ShouldLogAGeneralWarningOnAnAssemblyLoadFailureWhenRunningInRepl() - { - // arrange - var mockResolver = new Mock(); - mockResolver.Setup(a => a.GetAssemblyPaths(It.IsAny(), false)).Returns(new[] { "/foo.dll" }); - _overrides[typeof(IAssemblyResolver)] = mockResolver.Object; - - var mockAssemblyUtility = new Mock(); - mockAssemblyUtility.Setup(a => a.IsManagedAssembly(It.IsAny())).Returns(true); - _overrides[typeof (IAssemblyUtility)] = mockAssemblyUtility.Object; - - var initializationServices = new InitializationServices(_logProvider, _overrides); - var runtimeServices = new RuntimeServices( - _logProvider, - _overrides, - _mockConsole.Object, - _scriptEngineType, - _scriptExecutorType, - _replType, true, - initializationServices, - ""); - - // act - var container = runtimeServices.Container; - - // assert - _logProvider.Output.ShouldContain( - "WARN: Some assemblies failed to load. Launch with '-repl -loglevel debug' to see the details"); - } - - [Fact] - public void ShouldResolveAssembliesBasedOnScriptWorkingDirectory() - { - // arrange - var fsmock = new Mock(); - fsmock.Setup(a => a.GetWorkingDirectory(It.IsAny())).Returns("c:/scripts"); - - var resolvermock = new Mock(); - resolvermock.Setup(a => a.GetAssemblyPaths("c:/scripts", false)).Returns(new[] { "foo.dll" }); - - _overrides[typeof(IFileSystem)] = fsmock.Object; - _overrides[typeof(IAssemblyResolver)] = resolvermock.Object; - - var initializationServices = new InitializationServices(_logProvider, _overrides); - var runtimeServices = new RuntimeServices( - _logProvider, - _overrides, - _mockConsole.Object, - _scriptEngineType, - _scriptExecutorType, - _replType, - true, - initializationServices, - "c:/scriptcs/script.csx"); - - // act - var container = runtimeServices.Container; - - // assert - resolvermock.Verify(x => x.GetAssemblyPaths("c:/scripts", false), Times.Exactly(1)); - } - - private class MockScriptEngine : IScriptEngine - { - public string BaseDirectory { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public string CacheDirectory { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public string FileName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable namespaces, ScriptPackSession scriptPackSession) - { - throw new NotImplementedException(); - } - } - - private class MockScriptExecutor : IScriptExecutor - { - public AssemblyReferences References => throw new NotImplementedException(); - - public IReadOnlyCollection Namespaces => throw new NotImplementedException(); - - public IScriptEngine ScriptEngine => throw new NotImplementedException(); - - public IFileSystem FileSystem => throw new NotImplementedException(); - - public ScriptPackSession ScriptPackSession => throw new NotImplementedException(); - - public void AddReferences(params Assembly[] references) - { - throw new NotImplementedException(); - } - - public void AddReferences(params string[] references) - { - throw new NotImplementedException(); - } - - public ScriptResult Execute(string script, params string[] scriptArgs) - { - throw new NotImplementedException(); - } - - public ScriptResult ExecuteScript(string script, params string[] scriptArgs) - { - throw new NotImplementedException(); - } - - public void ImportNamespaces(params string[] namespaces) - { - throw new NotImplementedException(); - } - - public void Initialize(IEnumerable paths, IEnumerable scriptPacks, params string[] scriptArgs) - { - throw new NotImplementedException(); - } - - public void RemoveNamespaces(params string[] namespaces) - { - throw new NotImplementedException(); - } - - public void RemoveReferences(params Assembly[] references) - { - throw new NotImplementedException(); - } - - public void RemoveReferences(params string[] references) - { - throw new NotImplementedException(); - } - - public void Reset() - { - throw new NotImplementedException(); - } - - public void Terminate() - { - throw new NotImplementedException(); - } - } - - private class MockFileSystem : IFileSystem - { - public IEnumerable EnumerateFiles( - string dir, string search, SearchOption searchOption = SearchOption.AllDirectories) - { - throw new NotImplementedException(); - } - - public IEnumerable EnumerateDirectories( - string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories) - { - throw new NotImplementedException(); - } - - public IEnumerable EnumerateFilesAndDirectories( - string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories) - { - throw new NotImplementedException(); - } - - public void Copy(string source, string dest, bool overwrite) - { - throw new NotImplementedException(); - } - - public void CopyDirectory(string source, string dest, bool overwrite) - { - throw new NotImplementedException(); - } - - public bool DirectoryExists(string path) - { - throw new NotImplementedException(); - } - - public void CreateDirectory(string path, bool hidden = false) - { - throw new NotImplementedException(); - } - - public void DeleteDirectory(string path) - { - throw new NotImplementedException(); - } - - public string ReadFile(string path) - { - throw new NotImplementedException(); - } - - public string[] ReadFileLines(string path) - { - throw new NotImplementedException(); - } - - public DateTime GetLastWriteTime(string file) - { - throw new NotImplementedException(); - } - - public bool IsPathRooted(string path) - { - throw new NotImplementedException(); - } - - public string GetFullPath(string path) - { - throw new NotImplementedException(); - } - - public string TempPath { get; private set; } - - public string CurrentDirectory - { - get { throw new NotImplementedException(); } - set { throw new NotImplementedException(); } - } - - public string NewLine - { - get { throw new NotImplementedException(); } - } - - public string GetWorkingDirectory(string path) - { - throw new NotImplementedException(); - } - - public void Move(string source, string dest) - { - throw new NotImplementedException(); - } - - public void MoveDirectory(string source, string dest) - { - throw new NotImplementedException(); - } - - public bool FileExists(string path) - { - throw new NotImplementedException(); - } - - public void FileDelete(string path) - { - throw new NotImplementedException(); - } - - public IEnumerable SplitLines(string value) - { - throw new NotImplementedException(); - } - - public void WriteToFile(string path, string text) - { - throw new NotImplementedException(); - } - - public Stream CreateFileStream(string filePath, FileMode mode) - { - throw new NotImplementedException(); - } - - public void WriteAllBytes(string filePath, byte[] bytes) - { - throw new NotImplementedException(); - } - - public string GlobalFolder - { - get { throw new NotImplementedException(); } - } - - public string HostBin - { - get { throw new NotImplementedException(); } - } - - public string BinFolder - { - get { return "bin"; } - } - - public string DllCacheFolder - { - get { throw new NotImplementedException(); } - } - - public string PackagesFile - { - get { return "packages.config"; } - } - - public string PackagesFolder - { - get { return "packages"; } - } - - public string NugetFile - { - get { throw new NotImplementedException(); } - } - - public string GlobalOptsFile - { - get { throw new NotImplementedException(); } - } - - public string PackageScriptsFile - { - get { throw new NotImplementedException(); } - } - } - - private class MockLineProcessor : ILineProcessor - { - public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode) - { - throw new NotImplementedException(); - } - } - - private class MockScriptHostFactory : IScriptHostFactory - { - public IScriptHost CreateScriptHost(IScriptPackManager scriptPackManager, string[] scriptArgs) - { - throw new NotImplementedException(); - } - } - - private class MockFilePreProcessor : IFilePreProcessor - { - public void ParseFile(string path, FileParserContext context) - { - throw new NotImplementedException(); - } - - public void ParseScript(List scriptLines, FileParserContext context) - { - throw new NotImplementedException(); - } - - public FilePreProcessorResult ProcessFile(string path) - { - throw new NotImplementedException(); - } - - public FilePreProcessorResult ProcessScript(string script) - { - throw new NotImplementedException(); - } - } - - private class MockScriptPackResolver : IScriptPackResolver - { - public IEnumerable GetPacks() - { - throw new NotImplementedException(); - } - } - - private class MockInstallationProvider : IInstallationProvider - { - public IEnumerable GetRepositorySources(string path) - { - throw new NotImplementedException(); - } - - public void Initialize() - { - throw new NotImplementedException(); - } - - public void InstallPackage(IPackageReference packageId, bool allowPreRelease = false) - { - throw new NotImplementedException(); - } - - public bool IsInstalled(IPackageReference packageId, bool allowPreRelease = false) - { - throw new NotImplementedException(); - } - } - - private class MockPackageInstaller : IPackageInstaller - { - public void InstallPackages(IEnumerable packageIds, bool allowPreRelease = false) - { - throw new NotImplementedException(); - } - } - - private class MockAssemblyUtility : IAssemblyUtility - { - public AssemblyName GetAssemblyName(string path) - { - throw new NotImplementedException(); - } - - public bool IsManagedAssembly(string path) - { - throw new NotImplementedException(); - } - - public Assembly Load(AssemblyName assemblyRef) - { - throw new NotImplementedException(); - } - - public Assembly LoadFile(string path) - { - throw new NotImplementedException(); - } - } - - private class MockConsole : IConsole - { - public ConsoleColor ForegroundColor { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public int Width => throw new NotImplementedException(); - - public void Clear() - { - throw new NotImplementedException(); - } - - public void Exit() - { - throw new NotImplementedException(); - } - - public string ReadLine(string prompt) - { - throw new NotImplementedException(); - } - - public void ResetColor() - { - throw new NotImplementedException(); - } - - public void Write(string value) - { - throw new NotImplementedException(); - } - - public void WriteLine() - { - throw new NotImplementedException(); - } - - public void WriteLine(string value) - { - throw new NotImplementedException(); - } - } - - private class MockPackageContainer : IPackageContainer - { - public void CreatePackageFile() - { - throw new NotImplementedException(); - } - - public IPackageObject FindPackage(string path, IPackageReference packageReference) - { - throw new NotImplementedException(); - } - - public IEnumerable FindReferences(string path) - { - throw new NotImplementedException(); - } - } - - private class MockPackageAssemblyResolver : IPackageAssemblyResolver - { - public IEnumerable GetAssemblyNames(string workingDirectory) - { - throw new NotImplementedException(); - } - - public IEnumerable GetPackages(string workingDirectory) - { - throw new NotImplementedException(); - } - - public void SavePackages() - { - throw new NotImplementedException(); - } - } - - private class MockAssemblyResolver : IAssemblyResolver - { - public IEnumerable GetAssemblyPaths(string path, bool binariesOnly = false) - { - throw new NotImplementedException(); - } - } - private class MockVisualStudioSolutionWriter : IVisualStudioSolutionWriter - { - public string WriteSolution(IFileSystem fs, string script, IVisualStudioSolution solution, IList nestedItems = null) - { - throw new NotImplementedException(); - } - } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/ScriptCs.Hosting.Tests.csproj b/test/ScriptCs.Hosting.Tests/ScriptCs.Hosting.Tests.csproj deleted file mode 100644 index cb5cc998..00000000 --- a/test/ScriptCs.Hosting.Tests/ScriptCs.Hosting.Tests.csproj +++ /dev/null @@ -1,30 +0,0 @@ - - - net461 - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/test/ScriptCs.Hosting.Tests/ScriptServicesBuilderTests.cs b/test/ScriptCs.Hosting.Tests/ScriptServicesBuilderTests.cs deleted file mode 100644 index ed54c105..00000000 --- a/test/ScriptCs.Hosting.Tests/ScriptServicesBuilderTests.cs +++ /dev/null @@ -1,200 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using ScriptCs.Tests; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Hosting.Tests -{ - public class ScriptServicesBuilderTests - { - public class TheBuildMethod - { - [Theory, ScriptCsAutoData] - public void ShouldResolveScriptServices(ScriptServices scriptServices, [Frozen] Mock runtimeServicesMock, ScriptServicesBuilder builder) - { - runtimeServicesMock.Setup(r => r.GetScriptServices()).Returns(scriptServices); - builder.Overrides[typeof(IScriptEngine)] = null; - builder.Build().ShouldEqual(scriptServices); - } - - [Theory, ScriptCsAutoData] - public void ShouldLoadScriptPacksIfReplIsTrue(IConsole console, TestLogProvider logProvider) - { - var builder = new ScriptServicesBuilder(console, logProvider); - builder.Overrides[typeof(IScriptEngine)] = typeof(MockScriptEngine); - builder.Repl(); - builder.Build(); - var runtimeServices = (RuntimeServices) builder.RuntimeServices; - runtimeServices.InitDirectoryCatalog.ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldLoadScriptPacksIfScriptNameIsSet(IConsole console, TestLogProvider logProvider) - { - var builder = new ScriptServicesBuilder(console, logProvider); - builder.Overrides[typeof(IScriptEngine)] = typeof(MockScriptEngine); - builder.ScriptName(""); - builder.Build(); - var runtimeServices = (RuntimeServices)builder.RuntimeServices; - runtimeServices.InitDirectoryCatalog.ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShoulLoadScriptPacksIfLoadScriptPacksIsTrue(IConsole console, TestLogProvider logProvider) - { - var builder = new ScriptServicesBuilder(console, logProvider); - builder.Overrides[typeof(IScriptEngine)] = typeof(MockScriptEngine); - builder.LoadScriptPacks(); - builder.Build(); - var runtimeServices = (RuntimeServices)builder.RuntimeServices; - runtimeServices.InitDirectoryCatalog.ShouldBeTrue(); - } - - [Theory, ScriptCsAutoData] - public void ShouldNotLoadScriptPacksIfLoadScriptPacksIsFalse(IConsole console, TestLogProvider logProvider) - { - var builder = new ScriptServicesBuilder(console, logProvider); - builder.Overrides[typeof(IScriptEngine)] = typeof(MockScriptEngine); - builder.LoadScriptPacks(false); - builder.Build(); - var runtimeServices = (RuntimeServices)builder.RuntimeServices; - runtimeServices.InitDirectoryCatalog.ShouldBeFalse(); - } - - private class MockScriptEngine : IScriptEngine - { - public string BaseDirectory { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public string CacheDirectory { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public string FileName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable namespaces, ScriptPackSession scriptPackSession) - { - throw new NotImplementedException(); - } - } - } - - public class TheLoadModulesMethod - { - [Theory, ScriptCsAutoData] - public void ShouldLoadTheMonoModuleWhenTheMonoRuntimeIsPresent([Frozen] Mock typeResolver, [Frozen] Mock moduleLoader, ScriptServicesBuilder builder) - { - typeResolver.Setup(r => r.ResolveType("Mono.Runtime")).Returns(typeof(string)); - moduleLoader.Setup( - m => - m.Load(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny())) - .Callback( - (config, paths, hostBin, extension, module) => module.Single().ShouldEqual("mono")); - builder.LoadModules(null); - } - - [Theory, ScriptCsAutoData] - public void ShouldLoadTheMonoModuleWhenTheMonoModuleIsPassedInTheListOfModules([Frozen] Mock typeResolver, [Frozen] Mock moduleLoader, ScriptServicesBuilder builder) - { - typeResolver.Setup(r => r.ResolveType("Mono.Runtime")).Returns((Type)null); - moduleLoader.Setup( - m => - m.Load(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny())) - .Callback( - (config, paths, hostBin, extension, module) => module.Single().ShouldEqual("mono")); - builder.LoadModules(null, "mono"); - } - - [Theory, ScriptCsAutoData] - public void ShouldLoadTheRoslynModuleWhenTheMonoModuleIsNotSelected([Frozen] Mock typeResolver, [Frozen] Mock moduleLoader, ScriptServicesBuilder builder) - { - typeResolver.Setup(r => r.ResolveType("Mono.Runtime")).Returns((Type)null); - moduleLoader.Setup( - m => - m.Load(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny())) - .Callback( - (config, paths, hostBin, extension, module) => module.Single().ShouldEqual("roslyn")); - builder.LoadModules(null); - - } - - [Theory, ScriptCsAutoData] - public void ShouldFindAllModulesInTheFileSystem([Frozen] Mock typeResolver, [Frozen] Mock moduleLoader, [Frozen] Mock fileSystem, [Frozen] Mock initializationServices) - { - typeResolver.Setup(r => r.ResolveType("Mono.Runtime")).Returns((Type)null); - fileSystem.SetupGet(fs => fs.GlobalFolder).Returns(@"c:\modules"); - fileSystem.SetupGet(fs => fs.GlobalFolder).Returns(@"c:\current"); - fileSystem.SetupGet(fs => fs.HostBin).Returns(@"c:\hostbin"); - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - moduleLoader.Setup( - m => - m.Load(It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny())) - .Callback( - (config, paths, hostBin, extension, module) => - { - paths.ShouldContain(@"c:\modules"); - paths.ShouldContain(@"c:\current"); - paths.ShouldContain(@"c:\hostbin"); - }); - - } - } - - public class TheSetOverrideMethods - { - [Theory, ScriptCsAutoData] - public void ShouldReturnTheBuilder(ScriptServicesBuilder builder) - { - var someValue = new SomeOverride(); - var returnedBuilder = builder.SetOverride(someValue); - - returnedBuilder.ShouldBeSameAs(builder); - } - - [Theory, ScriptCsAutoData] - public void ShouldSetTheValueUsingTheKey(ScriptServicesBuilder builder) - { - var someValue = new SomeOverride(); - var key = typeof(ISomeOverride); - builder.SetOverride(someValue); - - var overrides = builder.Overrides; - overrides.ContainsKey(key).ShouldBeTrue(); - overrides[key].ShouldBeSameAs(someValue); - } - - [Theory, ScriptCsAutoData] - public void ShouldReplaceTheValueWhenKeyAlreadyExists(ScriptServicesBuilder builder) - { - var key = typeof(ISomeOverride); - var firstValue = new SomeOverride(); - var secondValue = new SomeOverride(); - - builder.SetOverride(firstValue); - builder.SetOverride(secondValue); - - var overrides = builder.Overrides; - overrides[key].ShouldNotBeSameAs(firstValue); - overrides[key].ShouldBeSameAs(secondValue); - } - - [Theory, ScriptCsAutoData] - public void ShouldUseTheValueTypeWhenNoInstanceIsProvided(ScriptServicesBuilder builder) - { - var key = typeof(ISomeOverride); - builder.SetOverride(); - - var overrides = builder.Overrides; - overrides.ContainsKey(key).ShouldBeTrue(); - overrides[key].ShouldBeSameAs(typeof(SomeOverride)); - } - - public interface ISomeOverride { } - public class SomeOverride : ISomeOverride { } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/VisualStudioSolutionTests.cs b/test/ScriptCs.Hosting.Tests/VisualStudioSolutionTests.cs deleted file mode 100644 index 6114bc92..00000000 --- a/test/ScriptCs.Hosting.Tests/VisualStudioSolutionTests.cs +++ /dev/null @@ -1,172 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Should; -using Xunit; -using System.IO; -using ScriptCs.Contracts; - -namespace ScriptCs.Hosting.Tests -{ - public class VisualStudioSolutionTests - { - public class TheConstructor - { - private VisualStudioSolution _solution = new VisualStudioSolution(); - - [Fact] - public void ShouldInitializeVariables() - { - _solution.Header.ShouldNotBeNull(); - _solution.Projects.ShouldNotBeNull(); - _solution.Global.ShouldNotBeNull(); - } - - [Fact] - public void ShouldAppendTheHeader() - { - var headerBuilder = new StringBuilder(); - headerBuilder.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00"); - headerBuilder.AppendLine("# Visual Studio 2013"); - headerBuilder.AppendLine("VisualStudioVersion = 12.0.30501.0"); - headerBuilder.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1"); - _solution.Header.ToString().ShouldEqual(headerBuilder.ToString()); - } - } - - public class TheAddGlobalHeaderMethod - { - private Guid _projectGuid = Guid.NewGuid(); - private VisualStudioSolution _builder = new VisualStudioSolution(); - - [Fact] - public void ShouldAppendTheGlobalHeader() - { - _builder.AddGlobalHeader(_projectGuid); - var globalBuilder = new StringBuilder(); - globalBuilder.AppendLine("Global"); - globalBuilder.AppendLine("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution"); - globalBuilder.AppendLine("\t\tDebug|Any CPU = Debug|Any CPU"); - globalBuilder.AppendLine("\tEndGlobalSection"); - globalBuilder.AppendLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution"); - globalBuilder.AppendFormat("\t\t{{{0}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU{1}", _projectGuid, Environment.NewLine); - globalBuilder.AppendLine("\tEndGlobalSection"); - globalBuilder.AppendLine("\tGlobalSection(SolutionProperties) = preSolution"); - globalBuilder.AppendLine("\t\tHideSolutionNode = FALSE"); - globalBuilder.AppendLine("\tEndGlobalSection"); - globalBuilder.ToString().ShouldEqual(_builder.Global.ToString()); - } - } - - public class TheAddGlobalNestedProjectsMethod - { - [Fact] - public void ShouldAppenedGlobalSectionEntriesForEachProject() - { - var builder = new VisualStudioSolution(); - - var nestedItems = new List(); - var a = Guid.NewGuid(); - var b = Guid.NewGuid(); - var c = Guid.NewGuid(); - - nestedItems.Add(new ProjectItem(a, b)); - nestedItems.Add(new ProjectItem(b, c)); - builder.AddGlobalNestedProjects(nestedItems); - var nestedBuilder = new StringBuilder(); - nestedBuilder.AppendLine("\tGlobalSection(NestedProjects) = preSolution"); - nestedBuilder.AppendFormat("\t\t{{{0}}} = {{{1}}}{2}", nestedItems[0].Project, nestedItems[0].Parent, - Environment.NewLine); - nestedBuilder.AppendFormat("\t\t{{{0}}} = {{{1}}}{2}", nestedItems[1].Project, nestedItems[1].Parent, - Environment.NewLine); - nestedBuilder.AppendLine("\tEndGlobalSection"); - builder.Global.ToString().Contains(nestedBuilder.ToString()); - } - } - - public class TheAddScriptcsProjectMethod - { - private VisualStudioSolution _builder = new VisualStudioSolution(); - private const string _scriptcsPath = "scriptcs.exe"; - private const string _workingPath = "working"; - private const string _args = "test.csx"; - private const bool _attach = true; - private Guid _projectGuid = Guid.NewGuid(); - private string _projects; - - public TheAddScriptcsProjectMethod() - { - _builder.AddScriptcsProject(_scriptcsPath, _workingPath, _args, _attach, _projectGuid); - _projects = _builder.Projects.ToString(); - } - - [Fact] - public void ShouldAppendTheScriptcsProjectSection() - { - var projectBuilder = new StringBuilder(); - - projectBuilder.AppendFormat(@"Project(""{{911E67C6-3D85-4FCE-B560-20A9C3E3FF48}}"") = ""scriptcs"", ""{0}"", ""{{{1}}}""{2}", _scriptcsPath, _projectGuid, Environment.NewLine); - projectBuilder.AppendLine("\tProjectSection(DebuggerProjectSystem) = preProject"); - projectBuilder.AppendLine("\t\tPortSupplier = 00000000-0000-0000-0000-000000000000"); - projectBuilder.AppendFormat("\t\tExecutable = {0}{1}", _scriptcsPath, Environment.NewLine); - projectBuilder.AppendLine("\t\tRemoteMachine = localhost"); - projectBuilder.AppendFormat("\t\tStartingDirectory = {0}{1}", _workingPath, Environment.NewLine); - projectBuilder.AppendFormat("\t\tArguments = {0}{1}", _args, Environment.NewLine); - projectBuilder.AppendLine("\t\tEnvironment = Default"); - projectBuilder.AppendLine("\t\tLaunchingEngine = 00000000-0000-0000-0000-000000000000"); - projectBuilder.AppendLine("\t\tUseLegacyDebugEngines = No"); - projectBuilder.AppendLine("\t\tLaunchSQLEngine = No"); - projectBuilder.AppendFormat("\t\tAttachLaunchAction = {0}{1}", _attach ? "Yes" : "No", Environment.NewLine); - projectBuilder.AppendLine("\tEndProjectSection"); - projectBuilder.AppendLine("EndProject"); - _projects.ShouldEqual(projectBuilder.ToString()); - } - - [Fact] - public void ShouldSetTheProjectElementScriptcsPath() - { - _projects.Contains(string.Format(@"Project(""{{911E67C6-3D85-4FCE-B560-20A9C3E3FF48}}"") = ""scriptcs"", ""{0}""", _scriptcsPath)); - } - - [Fact] - public void ShouldSetTheExecutable() - { - _projects.Contains(string.Format("Executable = {0}", _scriptcsPath)); - } - - [Fact] - public void ShouldSetTheStartingDirectory() - { - _projects.Contains(string.Format("StartingDirectory = {0}", _workingPath)); - } - - [Fact] - public void ShouldSetTheArguements() - { - _projects.Contains(string.Format("Arguments = {0}", _args)); - } - - [Fact] - public void ShouldSetTheAttachAction() - { - _projects.Contains(string.Format("AttachLaunchAction = {0}", _attach ? "Yes" : "No")); - } - } - - public class TheToStringMethod - { - [Fact] - public void BuildsTheSolution() - { - var builder = new VisualStudioSolution(); - builder.Header = new StringBuilder("A"); - builder.Projects = new StringBuilder("B"); - builder.Global = new StringBuilder("C"); - var solution = builder.ToString(); - solution.ShouldEqual("ABC"); - } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/VisualStudioSolutionWriterTests.cs b/test/ScriptCs.Hosting.Tests/VisualStudioSolutionWriterTests.cs deleted file mode 100644 index 47268a3a..00000000 --- a/test/ScriptCs.Hosting.Tests/VisualStudioSolutionWriterTests.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Moq; -using ScriptCs.Contracts; -using Should; -using Xunit; - -namespace ScriptCs.Hosting.Tests -{ - public class VisualStudioSolutionWriterTests - { - public class TheWriteSolutionMethod - { - private readonly Mock _solutionMock; - private readonly Mock _fsMock; - private readonly VisualStudioSolutionWriter _writer; - private readonly IList _nestedItems; - private readonly string _launcher; - - public TheWriteSolutionMethod() - { - _writer = new VisualStudioSolutionWriter(); - _solutionMock = new Mock(); - _fsMock = new Mock(); - _fsMock.SetupGet(fs => fs.PackagesFolder).Returns(@"packages"); - _fsMock.SetupGet(fs => fs.HostBin).Returns("bin"); - _fsMock.SetupGet(fs => fs.CurrentDirectory).Returns("root"); - _fsMock.Setup(fs=>fs.EnumerateFilesAndDirectories(It.IsAny(), It.IsAny(), SearchOption.AllDirectories)).Returns(new[] {Path.Combine("root","file1.csx"), Path.Combine("root", "child1", "file2.csx"), Path.Combine("root", "child1", "child2", "file3.csx")}); - _fsMock.Setup(fs => fs.FileExists(It.IsAny())).Returns(false); - _fsMock.SetupGet(fs => fs.TempPath).Returns("temp"); - _nestedItems = new List(); - _launcher = _writer.WriteSolution(_fsMock.Object, "test.csx", _solutionMock.Object, _nestedItems); - } - - [Fact] - public void ShouldAddTheScriptcsProject() - { - var scriptcsPath = Path.Combine("bin", "scriptcs.exe"); - _solutionMock.Verify(fs=>fs.AddScriptcsProject(scriptcsPath, "root", "test.csx -debug -loglevel info", false, It.IsAny())); - } - - [Fact] - public void ShoulGetDirectoryInfo() - { - _writer.Root.Files.ShouldContain("file1.csx"); - var child = _writer.Root.Directories.Values.First(); - child.Files.ShouldContain("file2.csx"); - child = child.Directories.Values.First(); - child.Files.ShouldContain("file3.csx"); - } - - [Fact] - public void ShouldCallAddDirectoryProjectForChild() - { - var child1 = _writer.Root.Directories.Values.First(); - var child2 = child1.Directories.Values.First(); - _nestedItems.Count(i => i.Project == child1.Guid).ShouldEqual(1); - _nestedItems.Count(i => i.Project == child2.Guid).ShouldEqual(1); - } - - [Fact] - public void ShouldAddGlobal() - { - _solutionMock.Verify(s=>s.AddGlobal(It.IsAny(), _nestedItems)); - } - - [Fact] - public void ShouldWriteTheSolution() - { - _fsMock.Verify(fs=>fs.WriteToFile(It.IsAny(), It.IsAny())); - } - - [Fact] - public void ShouldReturnALauncherInTheTempFolder() - { - _launcher.ShouldNotBeNull(); - _launcher.ShouldStartWith("temp"); - } - } - } -} diff --git a/test/ScriptCs.Hosting.Tests/app.config b/test/ScriptCs.Hosting.Tests/app.config deleted file mode 100644 index fe865d69..00000000 --- a/test/ScriptCs.Hosting.Tests/app.config +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/ScriptCs.Tests.Acceptance/CommandLine.cs b/test/ScriptCs.Tests.Acceptance/CommandLine.cs deleted file mode 100644 index b8e78bef..00000000 --- a/test/ScriptCs.Tests.Acceptance/CommandLine.cs +++ /dev/null @@ -1,36 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - using Xunit; - - public class CommandLine - { - [Scenario] - public static void UnexpectedOption(Exception exception) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "When I execute scriptcs with an unknown option" - .x(() => exception = Record.Exception(() => ScriptCsExe.Run( - new[] - { - "-unknownoption" - }, - ScenarioDirectory.Create(scenario)))); - - "Then scriptcs errors" - .x(() => exception.ShouldBeType()); - - "And I see an error message regarding the unknown option" - .x(() => - { - Console.WriteLine(exception); - exception.Message.ShouldContain("Usage: scriptcs options"); - }); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Configuration.cs b/test/ScriptCs.Tests.Acceptance/Configuration.cs deleted file mode 100644 index 2c0861ed..00000000 --- a/test/ScriptCs.Tests.Acceptance/Configuration.cs +++ /dev/null @@ -1,52 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - - public static class Configuration - { - [Scenario] - public static void LocalConfiguration(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a hello world script" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(""Hello world!"");")); - - "And a local config file specfying the log level as debug" - .x(() => directory.WriteLine("scriptcs.opts", @"{ logLevel: ""debug"" }")); - - "When I execute the script without the log level option" - .x(() => output = ScriptCsExe.Run("foo.csx", false, directory)); - - "Then I see debug messages" - .x(() => output.ShouldContain("DEBUG:")); - } - - [Scenario] - public static void CustomConfiguration(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a hello world script" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(""Hello world!"");")); - - "And a local config file specfying to run as debug" - .x(() => directory.WriteLine("custom.opts", @"{ logLevel: ""debug"" }")); - - "When I execute the script without the log level option but specifying the custom config" - .x(() => - { - var args = new[] { "--config", "custom.opts", }; - output = ScriptCsExe.Run("foo.csx", false, args, directory); - }); - - "Then I see debug messages" - .x(() => output.ShouldContain("DEBUG:")); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/DirectoryCleaning.cs b/test/ScriptCs.Tests.Acceptance/DirectoryCleaning.cs deleted file mode 100644 index c7cc8e5e..00000000 --- a/test/ScriptCs.Tests.Acceptance/DirectoryCleaning.cs +++ /dev/null @@ -1,35 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System.IO; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - - public static class DirectoryCleaning - { - [Scenario] - public static void CleaningADirectory(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a directory" - .x(() => directory = ScenarioDirectory.Create(scenario)); - - "And the directory has an installed package" - .x(() => ScriptCsExe.Install("ScriptCs.Adder.Local", directory)); - - "And the directory has an assembly cache" - .x(() => directory.WriteLine(Path.Combine(directory.Map(ScriptCsExe.DllCacheFolder), "foo.txt"), null)); - - "When I clean the directory" - .x(() => ScriptCsExe.Clean(directory)); - - "Then the packages folder is removed" - .x(() => Directory.Exists(directory.Map(ScriptCsExe.PackagesFolder)).ShouldBeFalse()); - - "And the assembly cache folder is removed" - .x(() => Directory.Exists(directory.Map(ScriptCsExe.DllCacheFolder)).ShouldBeFalse()); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/ImplicitInstallation.cs b/test/ScriptCs.Tests.Acceptance/ImplicitInstallation.cs deleted file mode 100644 index 9b2edf33..00000000 --- a/test/ScriptCs.Tests.Acceptance/ImplicitInstallation.cs +++ /dev/null @@ -1,53 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System.IO; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - - public static class ImplicitInstallation - { - [Scenario] - public static void Execute(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses ScriptCs.Adder to print the sum of 1234 and 5678" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(Require().Add(1234, 5678));")); - - "And a packages file declaring the ScriptCs.Adder dependency" - .x(() => - { - var nugetConfig = -@" - - - - - - - - - -"; - var packagesConfig = -@" - - - -"; - - directory.WriteLine("scriptcs_nuget.config", nugetConfig); - directory.WriteLine("scriptcs_packages.config", packagesConfig); - }); - - "When execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then I see 6912" - .x(() => output.ShouldContain("6912")); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/LooseScriptExecution.cs b/test/ScriptCs.Tests.Acceptance/LooseScriptExecution.cs deleted file mode 100644 index 71af4017..00000000 --- a/test/ScriptCs.Tests.Acceptance/LooseScriptExecution.cs +++ /dev/null @@ -1,96 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - using Xunit; - - public static class LooseScriptExecution - { - [Scenario] - [Example(true)] - [Example(false)] - public static void HelloWorld(bool debug, ScenarioDirectory directory, string output, string[] args, string script) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a hello world script" - .x(() => - { - directory = ScenarioDirectory.Create(scenario); - if (Environment.OSVersion.Platform == PlatformID.Win32NT) - { - script = @"Console.WriteLine(""""""Hello World!"""""");"; - } - else - { - script = @"'Console.WriteLine(""Hello World!"");'"; - } - args = new[] {"-e", script}; - }); - - "When I execute the script with debug set to {0}" - .x(() => output = ScriptCsExe.Run(args, debug, directory)); - - "Then I see 'Hello World!'" - .x(() => output.ShouldContain("Hello World!")); - } - - [Scenario] - [Example(true)] - [Example(false)] - public static void ScriptThrowsAnException(bool debug, ScenarioDirectory directory, Exception exception, string[] args, string script) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which throws an exception" - .x(() => - { - directory = ScenarioDirectory.Create(scenario); - if (Environment.OSVersion.Platform == PlatformID.Win32NT) - { - script = @"""throw new Exception(""""""BOOM!"""""");"""; - } - else - { - script = @"'throw new Exception(""BOOM!"");'"; - } - - args = new[] {"-e", script}; - }); - - "When I execute the script with debug set to {0}" - .x(() => exception = Record.Exception(() => ScriptCsExe.Run(args, debug, directory))); - - "Then scriptcs fails" - .x(() => exception.ShouldBeType()); - - "And I see the exception message" - .x(() => - { - exception.Message.ShouldContain("BOOM!"); - }); - } - - [Scenario] - public static void ScriptCanAccessEnv(ScenarioDirectory directory, string output, string[] args, string script) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which access Env" - .x(() => - { - directory = ScenarioDirectory.Create(scenario); - script = "Console.WriteLine(Env)"; - args = new[] {"-e", script}; - }); - "When I execute the script" - .x(() => output = ScriptCsExe.Run(args, directory)); - - "Then the Env object is displayed" - .x(() => output.ShouldContain("ScriptCs.ScriptEnvironment")); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/PackageInstallation.cs b/test/ScriptCs.Tests.Acceptance/PackageInstallation.cs deleted file mode 100644 index f04b3e20..00000000 --- a/test/ScriptCs.Tests.Acceptance/PackageInstallation.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System.IO; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - - public static class PackageInstallation - { - [Scenario] - public static void InstallingAPackage(ScenarioDirectory directory) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "When I install ScriptCs.Adder" - .x(() => ScriptCsExe.Install("ScriptCs.Adder.Local", directory = ScenarioDirectory.Create(scenario))); - - "Then the ScriptCs.Adder NuGet package is added to the packages folder" - .x(() => File.Exists( - Path.Combine( - directory.Map(ScriptCsExe.PackagesFolder), - "ScriptCs.Adder.Local.0.1.1/ScriptCs.Adder.Local.0.1.1.nupkg")) - .ShouldBeTrue()); - - "And the ScriptCs.Adder assembly is extracted" - .x(() => - - - File.Exists( - Path.Combine( - directory.Map(ScriptCsExe.PackagesFolder), - "ScriptCs.Adder.Local.0.1.1/lib/net45/ScriptCs.Adder.dll")) - .ShouldBeTrue() - ); - - "And ScriptCs.Adder is added to the packages file" - .x(() => File.ReadAllText(directory.Map(ScriptCsExe.PackagesFile)).ShouldContain( - @"")); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/PackageSaving.cs b/test/ScriptCs.Tests.Acceptance/PackageSaving.cs deleted file mode 100644 index dbc11804..00000000 --- a/test/ScriptCs.Tests.Acceptance/PackageSaving.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System.IO; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - - public static class PackageSaving - { - [Scenario] - public static void SavingAPackage(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "When I install ScriptCs.Adder manually" - .x(() => - { - ScriptCsExe.Install("ScriptCs.Adder.Local", directory = ScenarioDirectory.Create(scenario)); - directory.DeleteFile(ScriptCsExe.PackagesFile); - }); - - "And I save packages" - .x(() => ScriptCsExe.Save(directory)); - - "Then ScriptCs.Adder is added to the packages file" - .x(() => File.ReadAllText(directory.Map(ScriptCsExe.PackagesFile)).ShouldContain( - @"")); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Packages.cs b/test/ScriptCs.Tests.Acceptance/Packages.cs deleted file mode 100644 index 7527fc5c..00000000 --- a/test/ScriptCs.Tests.Acceptance/Packages.cs +++ /dev/null @@ -1,53 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Should.Core.Assertions; - using Xbehave; - - public static class Packages - { - [Scenario(Skip = "Failing with a path length exception")] - public static void PackageContainsAFrameworkAssemblyReference(ScenarioDirectory directory, Exception exception) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a simple script" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", "Console.WriteLine();")); - - "When I install a package which contains a framework assembly reference" - .x(() => ScriptCsExe.Install("FrameworkAssemblyReferencer", directory)); - - "And I execute the script" - .x(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", directory))); - - "Then there should be no errors" - .x(() => exception.ShouldBeNull()); - } - - [Scenario] - public static void PackagesWithDuplicateAssemblies(ScenarioDirectory directory, Exception exception) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a simple script" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", "Console.WriteLine();")); - - "When I install a package" - .x(() => ScriptCsExe.Install("Duplicate.A", directory)); - - "And I install another package containing the same assembly" - .x(() => ScriptCsExe.Install("Duplicate.B", directory)); - - "And I execute the script" - .x(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", directory))); - - "Then there should be no errors" - .x(() => exception.ShouldBeNull()); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/ScriptCs.Tests.Acceptance.csproj b/test/ScriptCs.Tests.Acceptance/ScriptCs.Tests.Acceptance.csproj deleted file mode 100644 index 765fdba3..00000000 --- a/test/ScriptCs.Tests.Acceptance/ScriptCs.Tests.Acceptance.csproj +++ /dev/null @@ -1,43 +0,0 @@ - - - net461 - - - - - - - - - - - - - - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - Always - - - \ No newline at end of file diff --git a/test/ScriptCs.Tests.Acceptance/ScriptCs.Tests.Acceptance.csproj.DotSettings b/test/ScriptCs.Tests.Acceptance/ScriptCs.Tests.Acceptance.csproj.DotSettings deleted file mode 100644 index 73e96563..00000000 --- a/test/ScriptCs.Tests.Acceptance/ScriptCs.Tests.Acceptance.csproj.DotSettings +++ /dev/null @@ -1,2 +0,0 @@ - - CSharp60 \ No newline at end of file diff --git a/test/ScriptCs.Tests.Acceptance/ScriptExecution.cs b/test/ScriptCs.Tests.Acceptance/ScriptExecution.cs deleted file mode 100644 index aa3af367..00000000 --- a/test/ScriptCs.Tests.Acceptance/ScriptExecution.cs +++ /dev/null @@ -1,177 +0,0 @@ -using System.IO; - -namespace ScriptCs.Tests.Acceptance -{ - using System; - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - using Xunit; - - public static class ScriptExecution - { - [Scenario] - [Example(true)] - [Example(false)] - public static void HelloWorld(bool debug, ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a hello world script" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(""Hello world!"");")); - - "When I execute the script with debug set to {0}" - .x(() => output = ScriptCsExe.Run("foo.csx", debug, directory)); - - "Then I see 'Hello world!'" - .x(() => output.ShouldContain("Hello world!")); - } - - [Scenario] - [Example(true)] - [Example(false)] - public static void ScriptThrowsAnException(bool debug, ScenarioDirectory directory, Exception exception) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which throws an exception" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"throw new Exception(""BOOM!"");")); - - "When I execute the script with debug set to {0}" - .x(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", debug, directory))); - - "Then scriptcs fails" - .x(() => exception.ShouldBeType()); - - "And I see the exception message" - .x(() => exception.Message.ShouldContain("BOOM!")); - } - - [Scenario] - public static void ScriptCanWorkWithUsingStatic(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which defined a static import" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", "using static System.Console;" + Environment.NewLine + @"WriteLine(""Hello world!"");")); - - "When I execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then I see 'Hello world!'" - .x(() => output.ShouldContain("Hello world!")); - } - - [Scenario] - public static void ScriptingEngineShouldSupportCSharp71(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses C# 7.1 language feature - named tuples" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"var x = 1; var y = 2; var tuple = (x,y); Console.WriteLine(""Sum="" + (tuple.x + tuple.y))")); - - "When I execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then I see the output from the named tuples." - .x(() => output.ShouldContain("Sum=3")); - } - - [Scenario] - public static void ScriptCanAccessEnv(ScenarioDirectory directory, string output ) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which accesses Env" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", "Console.WriteLine(Env)")); - - "When I execute the script" - .x(()=> output = ScriptCsExe.Run("foo.csx", directory)); - - "Then the Env object is displayed" - .x(() => output.ShouldContain("ScriptCs.ScriptEnvironment")); - } - - [Scenario] - public static void ScriptCanUseDynamic(ScenarioDirectory directory, string output ) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses dynamic" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"dynamic obj = new ExpandoObject(); obj.foo = ""bar""; Console.WriteLine(obj.foo); ;")); - - "When I execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then the dynamic value is properly returned " - .x(() => output.ShouldContain("bar")); - - } - - [Scenario] - public static void ScriptAssemblyIsSet(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which accesses Env.ScriptAssembly" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", "Console.WriteLine(Env.ScriptAssembly)")); - - "When I execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then the Assembly is displayed" - .x(() => output.ShouldContain("Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")); - - } - - [Scenario] - public static void ScriptPathIsSet(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which accesses Env.ScriptPath" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", "Console.WriteLine(Env.ScriptPath)")); - - "When I execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then the ScriptPath is displayed" - .x(() => output.ShouldContain("foo.csx")); - } - - [Scenario] - public static void LoadedScriptsIsSet(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which loads another script and accesses Env.LoadedScripts" - .x(() => - { - directory = ScenarioDirectory.Create(scenario) - .WriteLine( - "foo.csx", "#load bar.csx;" + Environment.NewLine + - "Console.WriteLine(Env.LoadedScripts.First());" - ); - directory.WriteLine("bar.csx", ""); - }); - - - "When I execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then the loaded script path is displayed" - .x(() => output.ShouldContain("bar.csx")); - } - - - } -} diff --git a/test/ScriptCs.Tests.Acceptance/ScriptLibraries.cs b/test/ScriptCs.Tests.Acceptance/ScriptLibraries.cs deleted file mode 100644 index 1049173e..00000000 --- a/test/ScriptCs.Tests.Acceptance/ScriptLibraries.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using Should; -using ScriptCs.Tests.Acceptance.Support; -using Xbehave; - -namespace ScriptCs.Tests.Acceptance -{ - public class ScriptLibraries - { - [Scenario] - public static void UsingAMethodInAScriptLibrary(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses ScriptCs.Calculator to print the sum of 40 and 2" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(new Calculator().Add(40, 2));")); - - "And ScriptCs.Calculator is installed" - .x(() => ScriptCsExe.Install("ScriptCs.Calculator", directory)); - - "When executing the script" - .x(() => - { - var scriptArgs = new[] { "-loglevel", "info" }; - output = ScriptCsExe.Run("foo.csx", false, Enumerable.Empty(), scriptArgs, directory); - }); - - "Then I see 42" - .x(() => output.ShouldContain("42")); - - "Then I see INFO outputted from the required Logger script pack" - .x(() => output.ShouldContain("INFO")); - } - - [Scenario(Skip = "Failing with a path length exception")] - public static void UsingAMethodInAScriptLibraryInTheRepl(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses ScriptCs.Calculator" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(""Type:"" + new Calculator().GetType().Name);" + Environment.NewLine + "Environment.Exit(0);")); - - "And ScriptCs.Calculator is installed" - .x(() => ScriptCsExe.Install("ScriptCs.Calculator", directory)); - - "When executing the script into REPL" - .x(() => output = ScriptCsExe.Run("foo.csx", false, new[] { "-r" }, directory)); - - "Then the ScriptCs.Calculator instance is created" - .x(() => output.ShouldContain("Type:Calculator")); - } - - [Scenario] - public static void LoadingFromANonRootedScript(ScenarioDirectory directory, ScenarioDirectory scriptDirectory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses ScriptCs.Calculator and it is non-rooted" - .x(() => - { - directory = ScenarioDirectory.Create(scenario); - scriptDirectory = ScenarioDirectory.Create(Path.Combine(scenario, "script")) - .WriteLine("foo.csx", @"Console.WriteLine(""Type:"" + new Calculator().GetType().Name);"); - }); - - "And ScriptCs.Calculator is installed" - .x(() => ScriptCsExe.Install("ScriptCs.Calculator", scriptDirectory)); - - "When executing the script" - .x(() => output = ScriptCsExe.Run(Path.Combine("script", "foo.csx"), false, directory)); - - "Then the ScriptCs.Calculator instance is created" - .x(() => output.ShouldContain("Type:Calculator")); - } - - - [Scenario] - public static void UsingALoadedMethodInAScriptLibrary(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses ScriptCs.Calculator to print the product of 7 and 6" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(new Calculator().Multiply(7, 6));")); - - "And ScriptCs.Calculator is installed" - .x(() => ScriptCsExe.Install("ScriptCs.Calculator", directory)); - - "When executing the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then I see 42" - .x(() => output.ShouldContain("42")); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/ScriptPacks.cs b/test/ScriptCs.Tests.Acceptance/ScriptPacks.cs deleted file mode 100644 index d5f16da3..00000000 --- a/test/ScriptCs.Tests.Acceptance/ScriptPacks.cs +++ /dev/null @@ -1,56 +0,0 @@ -namespace ScriptCs.Tests.Acceptance -{ - using System.Reflection; - using ScriptCs.Tests.Acceptance.Support; - using Should; - using Xbehave; - using System; - using Should.Core.Assertions; - using ScriptCs.Exceptions; - - public static class ScriptPacks - { - [Scenario] - public static void UsingAScriptPack(ScenarioDirectory directory, string output) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses ScriptCs.Adder to print the sum of 1234 and 5678" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"Console.WriteLine(Require().Add(1234, 5678));")); - - "And ScriptCs.Adder is installed" - .x(() => ScriptCsExe.Install("ScriptCs.Adder.Local", directory)); - - "When execute the script" - .x(() => output = ScriptCsExe.Run("foo.csx", directory)); - - "Then I see 6912" - .x(() => output.ShouldContain("6912")); - } - - [Scenario] - public static void UsingAnUnavailableScriptPack(ScenarioDirectory directory, string output, Exception exception) - { - var scenario = MethodBase.GetCurrentMethod().GetFullName(); - - "Given a script which uses a non-existing script pack 'Foo'" - .x(() => directory = ScenarioDirectory.Create(scenario) - .WriteLine("foo.csx", @"var fooContext = Require(); - class Foo : ScriptCs.Contracts.IScriptPackContext{} - Console.WriteLine(""hi"");")); - - "When I execute the script" - .x(() => exception = Record.Exception(() => ScriptCsExe.Run("foo.csx", directory))); - - "Then scriptcs fails" - .x(() => exception.ShouldBeType()); - - "With a script pack exception" - .x(() => - { - exception.Message.ShouldContain("Tried to resolve a script pack 'Submission#0+Foo', but such script pack is not available in the current execution context."); - }); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Support/FileSystem.cs b/test/ScriptCs.Tests.Acceptance/Support/FileSystem.cs deleted file mode 100644 index d21f4580..00000000 --- a/test/ScriptCs.Tests.Acceptance/Support/FileSystem.cs +++ /dev/null @@ -1,115 +0,0 @@ -using System.Diagnostics; - -namespace ScriptCs.Tests.Acceptance.Support -{ - using System; - using System.Globalization; - using System.IO; - using Should; - - // NOTE (adamralph): difficult to believe the retry stuff is required, but it is. System.IO and the filesystem race. - public static class FileSystem - { - public static void EnsureDirectoryCreated(string path) - { - Debug.WriteLine($"Current Directory [{Environment.CurrentDirectory.Length}]: {Environment.CurrentDirectory}"); - - if (Directory.Exists(path)) - { - return; - } - - var timeout = 0.05d; - - var createTimeout = DateTime.Now.AddSeconds(timeout); - while (true) - { - try - { - Directory.CreateDirectory(path); - - var existsTimeout = DateTime.Now.AddSeconds(timeout); - while (true) - { - if (Directory.Exists(path)) - { - break; - } - - if (DateTime.Now < existsTimeout) - { - continue; - } - - throw new IOException( - string.Format(CultureInfo.InvariantCulture, "Failed to create directory '{0}'", path)); - } - } - catch (Exception) - { - if (DateTime.Now < createTimeout) - { - continue; - } - - throw; - } - - break; - } - } - - public static void EnsureDirectoryDeleted(string path) - { - if (!Directory.Exists(path)) - { - return; - } - - var timeout = 0.05d; - - var deleteTimeout = DateTime.Now.AddSeconds(timeout); - while (true) - { - try - { - Directory.Delete(path, true); - - var goneTimeout = DateTime.Now.AddSeconds(timeout); - while (true) - { - if (!Directory.Exists(path)) - { - break; - } - - if (DateTime.Now < goneTimeout) - { - continue; - } - - throw new IOException( - string.Format(CultureInfo.InvariantCulture, "Failed to delete directory '{0}'", path)); - } - } - catch (Exception) - { - if (DateTime.Now < deleteTimeout) - { - continue; - } - - throw; - } - - break; - } - } - - public static void EnsureFileDeleted(string fileName) - { - File.Delete(fileName); - File.Exists(fileName).ShouldBeFalse(fileName + " should be deleted"); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Support/Gallery/Duplicate.A.1.0.0.nupkg b/test/ScriptCs.Tests.Acceptance/Support/Gallery/Duplicate.A.1.0.0.nupkg deleted file mode 100644 index f29cc9b3..00000000 Binary files a/test/ScriptCs.Tests.Acceptance/Support/Gallery/Duplicate.A.1.0.0.nupkg and /dev/null differ diff --git a/test/ScriptCs.Tests.Acceptance/Support/Gallery/Duplicate.B.1.0.0.nupkg b/test/ScriptCs.Tests.Acceptance/Support/Gallery/Duplicate.B.1.0.0.nupkg deleted file mode 100644 index f80b14d4..00000000 Binary files a/test/ScriptCs.Tests.Acceptance/Support/Gallery/Duplicate.B.1.0.0.nupkg and /dev/null differ diff --git a/test/ScriptCs.Tests.Acceptance/Support/Gallery/FrameworkAssemblyReferencer.1.0.0.nupkg b/test/ScriptCs.Tests.Acceptance/Support/Gallery/FrameworkAssemblyReferencer.1.0.0.nupkg deleted file mode 100644 index 25061b67..00000000 Binary files a/test/ScriptCs.Tests.Acceptance/Support/Gallery/FrameworkAssemblyReferencer.1.0.0.nupkg and /dev/null differ diff --git a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Adder.Local.0.1.1.nupkg b/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Adder.Local.0.1.1.nupkg deleted file mode 100644 index ea727f28..00000000 Binary files a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Adder.Local.0.1.1.nupkg and /dev/null differ diff --git a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Calculator.0.1.0.nupkg b/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Calculator.0.1.0.nupkg deleted file mode 100644 index f3e45144..00000000 Binary files a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Calculator.0.1.0.nupkg and /dev/null differ diff --git a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Contracts.0.9.0.nupkg b/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Contracts.0.9.0.nupkg deleted file mode 100644 index 17d6a053..00000000 Binary files a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Contracts.0.9.0.nupkg and /dev/null differ diff --git a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Logger.ScriptPack.0.1.0.nupkg b/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Logger.ScriptPack.0.1.0.nupkg deleted file mode 100644 index cc657944..00000000 Binary files a/test/ScriptCs.Tests.Acceptance/Support/Gallery/ScriptCs.Logger.ScriptPack.0.1.0.nupkg and /dev/null differ diff --git a/test/ScriptCs.Tests.Acceptance/Support/MethodBaseExtensions.cs b/test/ScriptCs.Tests.Acceptance/Support/MethodBaseExtensions.cs deleted file mode 100644 index 0acc3ce1..00000000 --- a/test/ScriptCs.Tests.Acceptance/Support/MethodBaseExtensions.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Security.Cryptography; -using System.Text; - -namespace ScriptCs.Tests.Acceptance.Support -{ - using System.Reflection; - using ScriptCs; - - public static class MethodBaseExtensions - { - public static string GetFullName(this MethodBase method) - { - Guard.AgainstNullArgument(nameof(method), method); - - return method.DeclaringType == null - ? method.Name - : string.Concat(GenerateSimpleHash(method.DeclaringType.FullName), ".", method.Name); - } - - /// - /// Windows cannot handle long path, so create a short path - /// - /// - /// - private static string GenerateSimpleHash(string name) - { - // use a hash instead of random characters - // so we have some consistency in debugging folders - using (var md5Hash = MD5.Create()) - { - var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(name)); - var hash = new StringBuilder(10); - - for (var i = 0; i < 5; i++) - { - hash.Append(data[i].ToString("x2")); - } - - return hash.ToString(); - } - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Support/ProcessStartInfoExtensions.cs b/test/ScriptCs.Tests.Acceptance/Support/ProcessStartInfoExtensions.cs deleted file mode 100644 index 037a2e8f..00000000 --- a/test/ScriptCs.Tests.Acceptance/Support/ProcessStartInfoExtensions.cs +++ /dev/null @@ -1,49 +0,0 @@ -namespace ScriptCs.Tests.Acceptance.Support -{ - using System; - using System.Diagnostics; - using System.IO; - using System.Text; - - public static class ProcessStartInfoExtensions - { - public static Tuple Run(this ProcessStartInfo info, string outputFile) - { - var output = new StringBuilder(); - int exitCode; - using (var process = new Process()) - { - process.StartInfo = info; - process.OutputDataReceived += (sender, e) => output.AppendLine(e.Data); - process.ErrorDataReceived += (sender, e) => output.AppendLine(e.Data); - process.Start(); - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - if (!process.WaitForExit(30000)) - { - try - { - process.Kill(); - } - catch (Exception ex) - { - throw new TimeoutException( - "The process took too longer than 30 seconds to exit and killing the process failed.", ex); - } - - throw new TimeoutException("The process took longer than 30 seconds to exit."); - } - - using (var writer = new StreamWriter(outputFile, true)) - { - writer.WriteLine(output.ToString()); - writer.Flush(); - } - - exitCode = process.ExitCode; - } - - return Tuple.Create(exitCode, output.ToString()); - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Support/ScenarioDirectory.cs b/test/ScriptCs.Tests.Acceptance/Support/ScenarioDirectory.cs deleted file mode 100644 index d102e21e..00000000 --- a/test/ScriptCs.Tests.Acceptance/Support/ScenarioDirectory.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; - -namespace ScriptCs.Tests.Acceptance.Support -{ - using System.Diagnostics; - using System.IO; - - public sealed class ScenarioDirectory - { - private static readonly string rootDirectory = "scenarios"; - - private readonly string _name; - - public static ScenarioDirectory Create(string scenario) - { - var name = Path.Combine(rootDirectory, scenario); - if (name == null) throw new Exception("Invalid directory"); - Debug.WriteLine($"Scenarios Dir [{name.Length}]: {name}"); - FileSystem.EnsureDirectoryDeleted(name); - FileSystem.EnsureDirectoryCreated(name); - return new ScenarioDirectory(name); - } - - private ScenarioDirectory(string name) - { - _name = name; - } - - public string Name - { - get { return _name; } - } - - public ScenarioDirectory WriteLine(string fileName, string text) - { - fileName = Map(fileName); - FileSystem.EnsureDirectoryCreated(Path.GetDirectoryName(fileName)); - using (var writer = new StreamWriter(fileName, true)) - { - writer.WriteLine(text); - writer.Flush(); - writer.Close(); - } - - return this; - } - - public void DeleteFile(string fileName) - { - FileSystem.EnsureFileDeleted(Map(fileName)); - } - - public string Map(string path) - { - var mapPath = Path.Combine(_name, path); - Debug.WriteLine($"Map [{mapPath.Length}]: {mapPath}"); - return mapPath; - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Support/ScriptCsException.cs b/test/ScriptCs.Tests.Acceptance/Support/ScriptCsException.cs deleted file mode 100644 index 2a8f62e4..00000000 --- a/test/ScriptCs.Tests.Acceptance/Support/ScriptCsException.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace ScriptCs.Tests.Acceptance.Support -{ - [Serializable] - public class ScriptCsException : Exception - { - public ScriptCsException() - { - } - - public ScriptCsException(string message) - : base(message) - { - } - - public ScriptCsException(string message, Exception innerException) - : base(message, innerException) - { - } - - protected ScriptCsException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/Support/ScriptCsExe.cs b/test/ScriptCs.Tests.Acceptance/Support/ScriptCsExe.cs deleted file mode 100644 index d2013d71..00000000 --- a/test/ScriptCs.Tests.Acceptance/Support/ScriptCsExe.cs +++ /dev/null @@ -1,169 +0,0 @@ -namespace ScriptCs.Tests.Acceptance.Support -{ - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.Globalization; - using System.IO; - using System.Linq; - using System.Text; - - public static class ScriptCsExe - { - private static readonly bool isMono = Type.GetType("Mono.Runtime") != null; - - public static string BinFolder - { - get { return "scriptcs_bin"; } - } - - public static string DllCacheFolder - { - get { return ".scriptcs_cache"; } - } - - public static string PackagesFile - { - get { return "scriptcs_packages.config"; } - } - - public static string PackagesFolder - { - get { return "scriptcs_packages"; } - } - - public static string NugetFile - { - get { return "scriptcs_nuget.config"; } - } - - public static string Run(IEnumerable args, ScenarioDirectory directory) - { - return Run(null, true, args, Enumerable.Empty(), directory); - } - - public static string Run(IEnumerable args, bool debug, ScenarioDirectory directory) - { - return Run(null, debug, args, Enumerable.Empty(), directory); - } - - public static string Run(string scriptName, ScenarioDirectory directory) - { - return Run(scriptName, true, Enumerable.Empty(), Enumerable.Empty(), directory); - } - - public static string Run(string scriptName, bool debug, ScenarioDirectory directory) - { - return Run(scriptName, debug, Enumerable.Empty(), Enumerable.Empty(), directory); - } - - public static string Run(string scriptName, bool debug, IEnumerable args, ScenarioDirectory directory) - { - return Run(scriptName, debug, args, Enumerable.Empty(), directory); - } - - public static string Run( - string scriptName, - bool debug, - IEnumerable args, - IEnumerable scriptArgs, - ScenarioDirectory directory) - { - var debugArgs = - debug && - !args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-DEBUG") && - !args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-D") - ? new[] { "--debug" } - : new string[0]; - - return Execute( - (scriptName == null ? Enumerable.Empty() : new[] { scriptName }).Concat(debugArgs).Concat(args), - scriptArgs, - directory); - } - - public static string Install(string package, ScenarioDirectory directory) - { - using (var writer = new StreamWriter(directory.Map(NugetFile), false)) - { - writer.Write( -@" - - - - - - - - -" - ); - - writer.Flush(); - } - - return Execute(new[] { "install", package }, Enumerable.Empty(), directory); - } - - public static string Save(ScenarioDirectory directory) - { - return Execute(new[] { "install", "--save" }, Enumerable.Empty(), directory); - } - - public static string Clean(ScenarioDirectory directory) - { - return Execute(new[] { "install", "--clean" }, Enumerable.Empty(), directory); - } - - private static string Execute( - IEnumerable args, IEnumerable scriptArgs, ScenarioDirectory directory) - { - var commandArgs = new List(args); - var scriptArgsArray = scriptArgs.ToArray(); - if (scriptArgsArray.Any()) - { - commandArgs.Add("--"); - commandArgs.AddRange(scriptArgsArray); - } - -#if DEBUG - var config = "Debug"; -#else - var config = "Release"; -#endif - - var exe = Path.GetFullPath( - Path.Combine("..", "..", "..", "..", "..", "src", "ScriptCs", "bin", config, "net461", "scriptcs.exe")); - - var info = new ProcessStartInfo - { - FileName = isMono - ? "mono" - : exe, - Arguments = isMono - ? string.Concat(exe, " ", string.Join(" ", commandArgs)) - : string.Join(" ", commandArgs), - WorkingDirectory = directory.Name, - UseShellExecute = false, - CreateNoWindow = true, - RedirectStandardOutput = true, - RedirectStandardError = true, - StandardErrorEncoding = Encoding.UTF8, - StandardOutputEncoding = Encoding.UTF8 - }; - var result = info.Run(Path.GetFileName(directory.Name) + ".log"); - if (result.Item1 != 0) - { - var message = string.Format( - CultureInfo.InvariantCulture, - "scriptcs.exe exited with code {0}. The output was: {1}", - result.Item1.ToString(CultureInfo.InvariantCulture), - result.Item2); - - throw new ScriptCsException(message); - } - - return result.Item2; - } - } -} diff --git a/test/ScriptCs.Tests.Acceptance/app.config b/test/ScriptCs.Tests.Acceptance/app.config deleted file mode 100644 index 74979dd4..00000000 --- a/test/ScriptCs.Tests.Acceptance/app.config +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/ScriptCs.Tests.Acceptance/xunit.runner.json b/test/ScriptCs.Tests.Acceptance/xunit.runner.json deleted file mode 100644 index edb23212..00000000 --- a/test/ScriptCs.Tests.Acceptance/xunit.runner.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "parallelizeTestCollections": false -} \ No newline at end of file diff --git a/test/ScriptCs.Tests/CleanCommandTests.cs b/test/ScriptCs.Tests/CleanCommandTests.cs deleted file mode 100644 index d49cfffc..00000000 --- a/test/ScriptCs.Tests/CleanCommandTests.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Moq; -using AutoFixture.Xunit2; -using ScriptCs.Command; -using ScriptCs.Contracts; -using ScriptCs.Hosting; -using Xunit; - -namespace ScriptCs.Tests -{ - public class CleanCommandTests - { - public class ExecuteMethod - { - [ScriptCsAutoData("scriptcs_packages")] - [ScriptCsAutoData(".scriptcs_cache")] - [Theory] - public void ShouldDeletePackagesFolder(string folder, - [Frozen] Mock fileSystem, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // Arrange - var args = new Config { Clean = true }; - - fileSystem.Setup(i => i.DirectoryExists(It.Is(x => x.Contains(folder)))).Returns(true); - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - - // Act - factory.CreateCommand(args, new string[0]).Execute(); - - // Assert - fileSystem.Verify(i => i.DirectoryExists(It.Is(x => x.Contains(folder))), Times.Once()); - fileSystem.Verify(i => i.DeleteDirectory(It.Is(x => x.Contains(folder))), Times.Once()); - } - } - } -} diff --git a/test/ScriptCs.Tests/CommandFactoryTests.cs b/test/ScriptCs.Tests/CommandFactoryTests.cs deleted file mode 100644 index 31eaede7..00000000 --- a/test/ScriptCs.Tests/CommandFactoryTests.cs +++ /dev/null @@ -1,220 +0,0 @@ -using Moq; -using AutoFixture; -using AutoFixture.AutoMoq; -using ScriptCs.Command; -using ScriptCs.Contracts; -using ScriptCs.Hosting; -using Should; -using Xunit; - -namespace ScriptCs.Tests -{ - public class CommandFactoryTests - { - public class CreateCommandMethod - { - private static IScriptServicesBuilder CreateBuilder(bool packagesFileExists = true, bool packagesFolderExists = true) - { - const string CurrentDirectory = "C:\\"; - const string PackagesFile = "C:\\packages.config"; - const string PackagesFolder = "C:\\packages"; - - var fixture = new Fixture().Customize(new AutoMoqCustomization()); - var fileSystem = fixture.Freeze>(); - fileSystem.SetupGet(x => x.CurrentDirectory).Returns(CurrentDirectory); - fileSystem.SetupGet(x => x.PackagesFile).Returns("packages.config"); - fileSystem.SetupGet(x => x.PackagesFolder).Returns("packages"); - fileSystem.SetupGet(x => x.DllCacheFolder).Returns(".cache"); - fileSystem.Setup(x => x.FileExists(PackagesFile)).Returns(packagesFileExists); - fileSystem.Setup(x => x.DirectoryExists(PackagesFolder)).Returns(packagesFolderExists); - - var builder = fixture.Freeze>(); - var services = fixture.Create(); - builder.Setup(b => b.Build()).Returns(services); - - var initServices = fixture.Freeze>(); - initServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - builder.SetupGet(b => b.InitializationServices).Returns(initServices.Object); - return fixture.Create(); - } - - [Fact] - public void ShouldInstallAndSaveWhenInstallFlagIsOn() - { - // Arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = string.Empty, - ScriptName = null - }; - - // Act - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - var compositeCommand = result as ICompositeCommand; - compositeCommand.ShouldNotBeNull(); - - compositeCommand.Commands.Count.ShouldEqual(2); - compositeCommand.Commands[0].ShouldImplement(); - compositeCommand.Commands[1].ShouldImplement(); - } - - [Fact] - public void ShouldExecuteLooseScriptWhenExecIsPassed() - { - // Arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = null, - Eval = "foo" - }; - - // Act - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - result.ShouldImplement(); - } - - [Fact] - public void ShouldExecuteWhenScriptNameIsPassed() - { - // Arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = null, - ScriptName = "test.csx" - }; - - // Act - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - result.ShouldImplement(); - } - - [Fact] - public void ShouldInstallAndExecuteWhenScriptNameIsPassedAndPackagesFolderDoesNotExist() - { - // Arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = null, - ScriptName = "test.csx" - }; - - // Act - var factory = new CommandFactory(CreateBuilder(true, false)); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - var compositeCommand = result as ICompositeCommand; - compositeCommand.ShouldNotBeNull(); - - compositeCommand.Commands.Count.ShouldEqual(2); - compositeCommand.Commands[0].ShouldImplement(); - compositeCommand.Commands[1].ShouldImplement>(); - } - - [Fact] - public void ShouldExecuteWhenBothNameAndInstallArePassed() - { - // Arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = string.Empty, - ScriptName = "test.csx" - }; - - // Act - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - result.ShouldImplement(); - } - - [Fact] - public void ShouldSaveAndCleanWhenCleanFlagIsPassed() - { - // Arrange - var args = new Config { Clean = true, ScriptName = null }; - - // Act - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - var compositeCommand = result as ICompositeCommand; - compositeCommand.ShouldNotBeNull(); - - compositeCommand.Commands.Count.ShouldEqual(2); - compositeCommand.Commands[0].ShouldImplement(); - compositeCommand.Commands[1].ShouldImplement(); - } - - [Fact] - public void ShouldSaveWhenSaveFlagIsPassed() - { - // Arrange - var args = new Config { Save = true, ScriptName = null }; - - // Act - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - result.ShouldNotBeNull(); - result.ShouldImplement(); - } - - [Fact] - public void ShouldReturnReplWhenNoNameOrInstallSet() - { - // Arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = null, - ScriptName = null - }; - - // Act - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, new string[0]); - - // Assert - result.ShouldImplement(); - } - - [Fact] - public void ShouldPassScriptArgsToExecuteCommandConstructor() - { - // Arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = null, - ScriptName = "test.csx" - }; - - // Act - var scriptArgs = new string[0]; - var factory = new CommandFactory(CreateBuilder()); - var result = factory.CreateCommand(args, scriptArgs) as IScriptCommand; - - // Assert - result.ScriptArgs.ShouldEqual(scriptArgs); - } - } - } -} \ No newline at end of file diff --git a/test/ScriptCs.Tests/ConfigTests.cs b/test/ScriptCs.Tests/ConfigTests.cs deleted file mode 100644 index 040084d0..00000000 --- a/test/ScriptCs.Tests/ConfigTests.cs +++ /dev/null @@ -1,54 +0,0 @@ -using Should; -using Xunit.Extensions; - -namespace ScriptCs.Tests -{ - using ScriptCs.Contracts; - using Xunit; - - public class ConfigTests - { - public class TheApplyMethod - { - [Theory] - [InlineData(true, null, LogLevel.Error, LogLevel.Debug)] - [InlineData(false, null, LogLevel.Error, LogLevel.Error)] - [InlineData(true, LogLevel.Error, LogLevel.Trace, LogLevel.Error)] - [InlineData(true, null, LogLevel.Trace, LogLevel.Trace)] - public void CalculatesTheLogLevel( - bool debug, LogLevel? logLevel, LogLevel currentLogLevel, LogLevel expectedLogLevel) - { - // arrange - var mask = new ConfigMask { Debug = debug, LogLevel = logLevel }; - var config = new Config { LogLevel = currentLogLevel }; - - // act - config = config.Apply(mask); - - // assert - config.LogLevel.ShouldEqual(expectedLogLevel); - } - - [Theory] - [InlineData(null, null)] - [InlineData(".csx", ".csx")] - [InlineData(".fsx", ".fsx")] - [InlineData("a", "a.csx")] // :eyes: here it is! - [InlineData("a.", "a.")] - [InlineData("a.csx", "a.csx")] - [InlineData("a.fsx", "a.fsx")] - public void AddsTheDefaultExtension(string scriptName, string expectedScriptName) - { - // arrange - var mask = new ConfigMask { ScriptName = scriptName }; - var config = new Config(); - - // act - config = config.Apply(mask); - - // assert - config.ScriptName.ShouldEqual(expectedScriptName); - } - } - } -} diff --git a/test/ScriptCs.Tests/ExecuteLooseScriptCommandTests.cs b/test/ScriptCs.Tests/ExecuteLooseScriptCommandTests.cs deleted file mode 100644 index b9e17f2d..00000000 --- a/test/ScriptCs.Tests/ExecuteLooseScriptCommandTests.cs +++ /dev/null @@ -1,215 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Moq; -using ScriptCs.Command; -using ScriptCs.Contracts; -using ScriptCs.Hosting; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class ExecuteLooseScriptCommandTests - { - public class ExecuteMethod - { - [Theory, ScriptCsAutoData] - public void LooseScriptExecCommandShouldInvokeWithScriptPassedFromArgs( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config { AllowPreRelease = false, PackageName = "", Eval = "foo", }; - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - sut.Execute(); - - // assert - executor.Verify( - i => i.Initialize( - It.IsAny>(), It.IsAny>()), Times.Once()); - - executor.Verify( - i => i.ExecuteScript(It.Is(x => x == "foo"), It.IsAny()), Times.Once()); - - executor.Verify( - i => i.Terminate(), Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void NonManagedAssembliesAreExcluded( - [Frozen] Mock fileSystem, - [Frozen] Mock assemblyUtility, - [Frozen] Mock executor, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - const string NonManaged = "non-managed.dll"; - - var args = new Config { AllowPreRelease = false, PackageName = "", Eval = "foo", }; - - fileSystem.Setup( - x => x.EnumerateFiles(It.IsAny(), It.IsAny(), SearchOption.AllDirectories)) - .Returns(new[] { "managed.dll", NonManaged }); - - assemblyUtility.Setup(x => x.IsManagedAssembly(It.Is(y => y == NonManaged))).Returns(false); - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - sut.Execute(); - - // assert - executor.Verify( - i => i.Initialize( - It.Is>(x => !x.Contains(NonManaged)), It.IsAny>()), - Times.Once()); - - executor.Verify( - i => i.ExecuteScript(It.Is(x => x == "foo"), It.IsAny()), Times.Once()); - - executor.Verify( - i => i.Terminate(), Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnErrorIfThereIsCompileException( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] TestLogProvider logProvider, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = "", - Eval = "foo" - }; - - executor.Setup(i => i.ExecuteScript(It.IsAny(), It.IsAny())) - .Returns(new ScriptResult(compilationException: new Exception("test"))); - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - var result = sut.Execute(); - - // assert - result.ShouldEqual(CommandResult.Error); - logProvider.Output.ShouldContain("ERROR:"); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnErrorIfThereIsExecutionException( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] TestLogProvider logProvider, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = "", - Eval = "foo" - }; - - executor.Setup(i => i.ExecuteScript(It.IsAny(), It.IsAny())) - .Returns(new ScriptResult(executionException: new Exception("test"))); - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - var result = sut.Execute(); - - // assert - result.ShouldEqual(CommandResult.Error); - logProvider.Output.ShouldContain("ERROR:"); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnErrorIfTheScriptIsIncomplete( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] TestLogProvider logProvider, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config { Eval = "foo" }; - - executor.Setup(i => i.ExecuteScript(It.IsAny(), It.IsAny())) - .Returns(ScriptResult.Incomplete); - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - var result = sut.Execute(); - - // assert - result.ShouldEqual(CommandResult.Error); - logProvider.Output.ShouldContain("ERROR:"); - } - - [Theory, ScriptCsAutoData] - public void ShouldComposeScripts([Frozen] Mock fileSystem, Mock composer) - { - var cmd = new ExecuteLooseScriptCommand( - null, - null, - fileSystem.Object, - new Mock().Object, - new Mock().Object, - new TestLogProvider(), - new Mock().Object, - composer.Object); - - cmd.Execute(); - - composer.Verify(c => c.Compose(It.IsAny(), null)); - } - - - } - } -} diff --git a/test/ScriptCs.Tests/ExecuteReplCommandTests.cs b/test/ScriptCs.Tests/ExecuteReplCommandTests.cs deleted file mode 100644 index 0607feb9..00000000 --- a/test/ScriptCs.Tests/ExecuteReplCommandTests.cs +++ /dev/null @@ -1,131 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Moq; -using ScriptCs.Command; -using ScriptCs.Contracts; -using ScriptCs.Hosting; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class ExecuteReplCommandTests - { - public class TheExecuteMethod - { - [Theory, ScriptCsAutoData] - public void ShouldPromptForInput( - [Frozen] Mock fileSystem, - [Frozen] Mock console, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var config = new Config { Repl = true }; - - console.Setup(x => x.ReadLine(It.IsAny())).Returns((string)null); - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.SetupGet(b => b.ConsoleInstance).Returns(console.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(config, new string[0]); - - // act - sut.Execute(); - - // assert - console.Verify(x=>x.ReadLine("> ")); - } - - [Theory, ScriptCsAutoData] - public void WhenPassedAScript_ShouldPressedReplWithScript( - [Frozen] Mock scriptEngine, - [Frozen] Mock console, - [Frozen] Mock fileSystem, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config { Repl = true, ScriptName = "test.csx", }; - - scriptEngine.Setup(x => x.Execute( - "#load test.csx", - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny())); - - console.Setup(x => x.ReadLine("")).Throws(new Exception()); - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - sut.Execute(); - - // assert - scriptEngine.Verify(); - } - - [Theory, ScriptCsAutoData] - public void WhenNotPassedAScript_ShouldNotCallTheEngineAutomatically( - [Frozen] Mock scriptEngine, - [Frozen] Mock console, - [Frozen] Mock fileSystem, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var config = new Config { Repl = true }; - - console.Setup(x => x.ReadLine("")).Throws(new Exception()); - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(config, new string[0]); - - // act - sut.Execute(); - - // assert - scriptEngine.Verify( - x => x.Execute( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny()), - Times.Never()); - } - - [Theory, ScriptCsAutoData] - public void ShouldComposeScripts([Frozen] Mock fileSystem, Mock composer) - { - var cmd = new ExecuteScriptCommand( - null, - null, - fileSystem.Object, - new Mock().Object, - new Mock().Object, - new TestLogProvider(), - new Mock().Object, - composer.Object); - - cmd.Execute(); - - composer.Verify(c => c.Compose(It.IsAny(),null)); - } - } - } -} diff --git a/test/ScriptCs.Tests/ExecuteScriptCommandTests.cs b/test/ScriptCs.Tests/ExecuteScriptCommandTests.cs deleted file mode 100644 index 9c3c302f..00000000 --- a/test/ScriptCs.Tests/ExecuteScriptCommandTests.cs +++ /dev/null @@ -1,215 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Moq; -using ScriptCs.Command; -using ScriptCs.Contracts; -using ScriptCs.Hosting; -using Should; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class ExecuteScriptCommandTests - { - public class ExecuteMethod - { - [Theory, ScriptCsAutoData] - public void ScriptExecCommandShouldInvokeWithScriptPassedFromArgs( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config { AllowPreRelease = false, PackageName = "", ScriptName = "test.csx", }; - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - sut.Execute(); - - // assert - executor.Verify( - i => i.Initialize( - It.IsAny>(), It.IsAny>()), Times.Once()); - - executor.Verify( - i => i.Execute(It.Is(x => x == "test.csx"), It.IsAny()), Times.Once()); - - executor.Verify( - i => i.Terminate(), Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void NonManagedAssembliesAreExcluded( - [Frozen] Mock fileSystem, - [Frozen] Mock assemblyUtility, - [Frozen] Mock executor, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - const string NonManaged = "non-managed.dll"; - - var args = new Config { AllowPreRelease = false, PackageName = "", ScriptName = "test.csx", }; - - fileSystem.Setup( - x => x.EnumerateFiles(It.IsAny(), It.IsAny(), SearchOption.AllDirectories)) - .Returns(new[] { "managed.dll", NonManaged }); - - assemblyUtility.Setup(x => x.IsManagedAssembly(It.Is(y => y == NonManaged))).Returns(false); - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - sut.Execute(); - - // assert - executor.Verify( - i => i.Initialize( - It.Is>(x => !x.Contains(NonManaged)), It.IsAny>()), - Times.Once()); - - executor.Verify( - i => i.Execute(It.Is(x => x == "test.csx"), It.IsAny()), Times.Once()); - - executor.Verify( - i => i.Terminate(), Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnErrorIfThereIsCompileException( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] TestLogProvider logProvider, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = "", - ScriptName = "test.csx", - }; - - executor.Setup(i => i.Execute(It.IsAny(), It.IsAny())) - .Returns(new ScriptResult(compilationException: new Exception("test"))); - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - var result = sut.Execute(); - - // assert - result.ShouldEqual(CommandResult.Error); - logProvider.Output.ShouldContain("ERROR:"); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnErrorIfThereIsExecutionException( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] TestLogProvider logProvider, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config - { - AllowPreRelease = false, - PackageName = "", - ScriptName = "test.csx" - }; - - executor.Setup(i => i.Execute(It.IsAny(), It.IsAny())) - .Returns(new ScriptResult(executionException: new Exception("test"))); - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - var result = sut.Execute(); - - // assert - result.ShouldEqual(CommandResult.Error); - logProvider.Output.ShouldContain("ERROR:"); - } - - [Theory, ScriptCsAutoData] - public void ShouldReturnErrorIfTheScriptIsIncomplete( - [Frozen] Mock fileSystem, - [Frozen] Mock executor, - [Frozen] TestLogProvider logProvider, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // arrange - var args = new Config { ScriptName = "test.csx" }; - - executor.Setup(i => i.Execute(It.IsAny(), It.IsAny())) - .Returns(ScriptResult.Incomplete); - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - servicesBuilder.Setup(b => b.Build()).Returns(services); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - var result = sut.Execute(); - - // assert - result.ShouldEqual(CommandResult.Error); - logProvider.Output.ShouldContain("ERROR:"); - } - - [Theory, ScriptCsAutoData] - public void ShouldComposeScripts([Frozen] Mock fileSystem, Mock composer) - { - var cmd = new ExecuteScriptCommand( - null, - null, - fileSystem.Object, - new Mock().Object, - new Mock().Object, - new TestLogProvider(), - new Mock().Object, - composer.Object); - - cmd.Execute(); - - composer.Verify(c=>c.Compose(It.IsAny(),null)); - } - - - } - } -} diff --git a/test/ScriptCs.Tests/InstallCommandTests.cs b/test/ScriptCs.Tests/InstallCommandTests.cs deleted file mode 100644 index cc1c5393..00000000 --- a/test/ScriptCs.Tests/InstallCommandTests.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Versioning; -using Moq; -using AutoFixture; -using AutoFixture.AutoMoq; -using ScriptCs.Command; -using ScriptCs.Contracts; -using ScriptCs.Hosting; -using Xunit; -using AutoFixture.Xunit2; - -namespace ScriptCs.Tests -{ - public class InstallCommandTests - { - public class ExecuteMethod - { - [Theory, ScriptCsAutoData] - public void InstallCommandShouldInstallSinglePackageIfNamePassed( - [Frozen] Mock fileSystem, - [Frozen] Mock packageInstaller, - [Frozen] Mock resolver, - [Frozen] Mock initializationServices, - ScriptServices services) - { - // Arrange - var args = new Config { AllowPreRelease = false, PackageName = "mypackage", }; - var fixture = new Fixture().Customize(new AutoMoqCustomization()); - var servicesBuilder = fixture.Freeze>(); - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - initializationServices.Setup(i => i.GetPackageInstaller()).Returns(packageInstaller.Object); - initializationServices.Setup(i => i.GetPackageAssemblyResolver()).Returns(resolver.Object); - - servicesBuilder.Setup(b => b.Build()).Returns(services); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - var factory = fixture.Create(); - - // Act - factory.CreateCommand(args, new string[0]).Execute(); - - // Assert - packageInstaller.Verify( - i => i.InstallPackages( - It.Is>(x => x.Count() == 1 && x.First().PackageId == "mypackage"), - It.IsAny()), - Times.Once()); - } - - [Theory, ScriptCsAutoData] - public void InstallCommandShouldInstallFromPackagesConfigIfNoNamePassed( - [Frozen] Mock packageInstaller, - [Frozen] Mock fileSystem, - [Frozen] Mock resolver, - [Frozen] Mock initializationServices, - [Frozen] Mock servicesBuilder, - ScriptServices services) - { - // Arrange - var args = new Config { AllowPreRelease = false, PackageName = string.Empty, }; - - initializationServices.Setup(i => i.GetFileSystem()).Returns(fileSystem.Object); - initializationServices.Setup(i => i.GetPackageInstaller()).Returns(packageInstaller.Object); - initializationServices.Setup(i => i.GetPackageAssemblyResolver()).Returns(resolver.Object); - - servicesBuilder.Setup(b => b.Build()).Returns(services); - servicesBuilder.SetupGet(b => b.InitializationServices).Returns(initializationServices.Object); - - resolver.Setup(i => i.GetPackages(It.IsAny())).Returns(new List - { - new PackageReference("a", new FrameworkName(".NETFramework,Version=v4.0"), new Version()), - new PackageReference("b", new FrameworkName(".NETFramework,Version=v4.0"), new Version()) - }); - - var factory = new CommandFactory(servicesBuilder.Object); - var sut = factory.CreateCommand(args, new string[0]); - - // act - sut.Execute(); - - // Assert - packageInstaller.Verify(i => i.InstallPackages(It.Is>(x => x.Count() == 2), It.IsAny()), Times.Once()); - } - } - } -} diff --git a/test/ScriptCs.Tests/ScriptCs.Tests.csproj b/test/ScriptCs.Tests/ScriptCs.Tests.csproj deleted file mode 100644 index 25bde541..00000000 --- a/test/ScriptCs.Tests/ScriptCs.Tests.csproj +++ /dev/null @@ -1,27 +0,0 @@ - - - net461 - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/test/ScriptCs.Tests/app.config b/test/ScriptCs.Tests/app.config deleted file mode 100644 index 8cea6cbd..00000000 --- a/test/ScriptCs.Tests/app.config +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/ScriptCsAutoDataAttribute.cs b/test/ScriptCsAutoDataAttribute.cs deleted file mode 100644 index cdbc5b65..00000000 --- a/test/ScriptCsAutoDataAttribute.cs +++ /dev/null @@ -1,25 +0,0 @@ -using AutoFixture; -using AutoFixture.AutoMoq; -using AutoFixture.Xunit2; -using Xunit; - -namespace ScriptCs.Tests -{ - public class ScriptCsAutoDataAttribute : CompositeDataAttribute - { - public ScriptCsAutoDataAttribute(params object[] values) - : base( - new InlineDataAttribute(values), - new InternalScriptCsAutoDataAttribute() - ) - { - } - } - - public class InternalScriptCsAutoDataAttribute : AutoDataAttribute - { - public InternalScriptCsAutoDataAttribute() : base(() => new Fixture().Customize(new ScriptCsMoqCustomization())) - { - } - } -} \ No newline at end of file diff --git a/test/ScriptCsMoqCustomization.cs b/test/ScriptCsMoqCustomization.cs deleted file mode 100644 index 51467257..00000000 --- a/test/ScriptCsMoqCustomization.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System.Collections.Generic; -using Moq; -using AutoFixture; -using AutoFixture.AutoMoq; -using ScriptCs.Contracts; - -namespace ScriptCs.Tests -{ - public class ScriptCsMoqCustomization : AutoMoqCustomization, ICustomization - { - void ICustomization.Customize(IFixture fixture) - { - this.Customize(fixture); - - fixture.Register(() => - { - var fileSystem = new Mock(); - fileSystem.SetupGet(f => f.PackagesFile).Returns("scriptcs_packages.config"); - fileSystem.SetupGet(f => f.PackagesFolder).Returns("scriptcs_packages"); - fileSystem.SetupGet(f => f.BinFolder).Returns("scriptcs_bin"); - fileSystem.SetupGet(f => f.DllCacheFolder).Returns(".scriptcs_cache"); - fileSystem.SetupGet(f => f.NugetFile).Returns("scriptcs_nuget.config"); - fileSystem.SetupGet(f => f.CurrentDirectory).Returns("workingdirectory"); - fileSystem.Setup(f => f.FileExists(@"workingdirectory\scriptcs_packages\PackageScripts.csx")) - .Returns(false); - fileSystem.Setup(f => f.DirectoryExists(@"workingdirectory\scriptcs_packages")).Returns(true); - fileSystem.Setup(f => f.GetWorkingDirectory(It.IsAny())).Returns("workingdirectory"); - return fileSystem; - }); - - fixture.Register(() => - { - var composer = new Mock(); - composer.SetupGet(c => c.ScriptLibrariesFile).Returns("ScriptLibraries.csx"); - return composer; - }); - - var logProvider = new TestLogProvider(); - fixture.Register(() => logProvider); - fixture.Register(() => logProvider); - - fixture.Register(() => new AppDomainAssemblyResolver( - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create>())); - - fixture.Register(() => new ScriptLibraryComposer( - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create())); - - fixture.Register(() => new ScriptServices( - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create>(), - fixture.Create(), - fixture.Create(), - fixture.Create())); - - fixture.Register(() => new ScriptExecutor( - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - fixture.Create(), - new ScriptInfo())); - } - } -} diff --git a/test/TestLogProvider.cs b/test/TestLogProvider.cs deleted file mode 100644 index 9348134a..00000000 --- a/test/TestLogProvider.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Globalization; -using System.Text; - -namespace ScriptCs.Tests -{ - using ScriptCs.Contracts; - - public class TestLogProvider : ILogProvider - { - private static readonly Disposable disposable = new Disposable(); - private readonly StringBuilder _output = new StringBuilder(); - - public string Output - { - get { return _output.ToString(); } - } - - public Logger GetLogger(string name) - { - return Log; - } - - public IDisposable OpenNestedContext(string message) - { - return disposable; - } - - public IDisposable OpenMappedContext(string key, string value) - { - return disposable; - } - - private bool Log( - LogLevel logLevel, Func messageFunc, Exception exception = null, params object[] formatParameters) - { - if (messageFunc != null) - { - var line = string.Format( - CultureInfo.InvariantCulture, - "{0}: {1}", - logLevel.ToString().ToUpper(), - string.Format(CultureInfo.InvariantCulture, messageFunc(), formatParameters)); - - _output.AppendLine(line); - } - - return true; - } - - private sealed class Disposable : IDisposable - { - public void Dispose() - { - } - } - } -}