GithubHelp home page GithubHelp logo

71 / scry Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 2.0 81 KB

Visual Studio extension that provides the ability to run C# scripts, giving them access to the current Roslyn workspace.

License: MIT License

C# 100.00%
roslyn vsix dotnet csharp csx scripting code-generation

scry's Introduction

Scry

Icon

Visual Studio extension that provides the ability to run C# scripts, giving them access to the current Roslyn workspace.

Getting started

Visual Studio 2017+ required.

1. Install extension

Download the VSIX file from the Releases, and open any project in Visual Studio.

2. Create a Scry script

Right-click on a project or folder, and select Add > New Item.... Scroll a bit, and create a new Scry Script.

3. Run the script

Right-click on the .csx script, and select Run script with Scry.

Features

Imports

By default, the following namespaces (and the matching references) are imported in Scry scripts:

  • System
  • System.Collections.Generic
  • System.Linq
  • System.Reflection
  • System.Threading.Tasks
  • Microsoft.CodeAnalysis
  • Microsoft.CodeAnalysis.CSharp
  • Microsoft.CodeAnalysis.CSharp.Syntax

Globals

Properties and methods of the Globals class are globally available in Scry scripts. Its definition is (roughly):

class Globals
{
    // Reference to itself.
    public Globals Context => this;

    // VS Workspace.
    public VisualStudioWorkspace Workspace { get; }
    
    // Project in which the script file is.
    public Project Project { get; }

    // TextWriter for the generated file.
    public TextWriter Output { get; }
    
    // Extension of the generated file (default: '.g.cs').
    public string Extension { get; set; }
  
    // Path of the script file.
    public string ScriptFile { get; }

    // Indentation used by WriteIndentation().
    public int Indentation { get; set; }
    
    // Alias for 'Indentation'.
    public int Indent { get; set; }

    // Whether or not WriteLine() methods should automatically
    // call WriteIndentation() before writing the given arguments.
    public bool AutoWriteIndentation { get; set; }

    // Shortcuts to Output.WriteLine.
    public Globals WriteLine(string format, params object[] args);
    public Globals WriteLine(params object[] args);
    
    // Shortcuts to Output.Write.
    public Globals Write(string format, params object[] args);
    public Globals Write(params object[] args);

    // Utility to write multiple using statements.
    public Globals WriteUsings(params string[] usings);

    // Utilities to write the start and end of a namespace.
    public Globals WriteNamespace(string @namespace);
    public Globals WriteEnd();

    // Change indentation.
    public Globals WriteIndentation();
    public Globals IncreaseIndentation(int indent = 4);
    public Globals DecreaseIndentation(int indent = 4);

    // Get root nodes, syntax trees and semantic models.
    public SyntaxNode Syntax(string filename);
    public CSharpSyntaxTree Tree(string filename);
    public SemanticModel Model(string filename);

    public IEnumerable<SyntaxNode> Syntaxes { get; }
    public IEnumerable<CSharpSyntaxTree> Trees { get; }
    public IEnumerable<SemanticModel> Models { get; }
}

Example script

AutoWriteIndentation = true;

Context
    .WriteUsings("System", "System.Reflection")
    .WriteLine()
    .WriteNamespace("TestNamespace")

    .WriteLine("public static class Files")
    .WriteLine('{')
    .IncreaseIndentation(4);

foreach (CSharpSyntaxTree tree in Trees)
    Context.WriteLine("public static readonly string {0} = \"{1}\";",
                      Path.GetFileNameWithoutExtension(tree.FilePath ?? ""), tree.FilePath);
    
Context
    .DecreaseIndentation(4)
    .WriteLine('}')

    .WriteEnd();

Nested generated file

You can nest the generated file by copying and pasting the following snippet in the .csproj file, and replacing the filenames accordingly.

<ItemGroup>
  <None Update="ScryScript.csx" />
  <Compile Update="ScryScript.g.cs">
    <DesignTime>True</DesignTime>
    <AutoGen>True</AutoGen>
    <DependentUpon>ScryScript.csx</DependentUpon>
  </Compile>
</ItemGroup>

Why not Scripty?

Scripty is much more stable, and has more features than Scry. However, using the former was impossible for me, due to Scripty#50.

Requiring a build for code generation simply isn't possible for me, which is why I created Scry instead. Unlike Scripty, it should be able to run in any C# project, even new ones.

scry's People

Contributors

6a avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

axlbdev jamcar23

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.