Giter Site home page Giter Site logo

devlooped / smallsharp Goto Github PK

View Code? Open in Web Editor NEW
276.0 5.0 12.0 6.87 MB

Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.

Home Page: https://clarius.org/SmallSharp

License: MIT License

C# 98.24% Ruby 0.35% SCSS 1.41%
csharp-sourcegenerator console-application learning visual-studio csharp

smallsharp's Introduction

Icon SmallSharp

Version Downloads License GH CI Status

Icon

Create, edit and run multiple C# top-level programs in the same project ๐Ÿ˜

Why

The new-ish C# top-level programs allow a very intuitive, simple and streamlined experience for quickly spiking or learning C#.

One missing thing since their introduction in Visual Studio is that you can only have one such top-level program in a project. This means that in order to prototype or learn a different area of .NET, you'd be forced to either replace your previous top-level program or change it to be a non-compile item somehow so you can keep it around (i.e. rename to a .txt or change its build action).

SmallSharp allows you to select which file should be the top-level program to run, right from the Start button/dropdown (for compilation and launch/debug). Moreover, it will monitor the active file you're editing, and automatically make it the startup file for you!

start button

This list is automatically kept in sync as you add more .cs files to the project. When you select one target C# file, that becomes the only top-level program to be compiled, so you don't have to modify any of the others since they automatically become None items.

All compile files directly under the project directory root are considered top-level programs for selection and compilation purposes. If you need to share code among them, you can place them in subdirectories and those will behave like normal compile items.

Usage

There is no need to install any Visual Studio extension. SmallSharp works by just installing the SmallSharp nuget package in a C# console project.

  1. Create a new Console project:

New Project Dialog

By default, this new console project may not be set up to target net5.0 or use the latest C# version. So click on the project node, and the project file will open in the editor. Make sure you either:

  • Target the recommended framework version (i.e. net6.0):
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

</Project>
  • Or use latest C# language version if targeting another framework:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

</Project>
  1. Install the SmallSharp nuget package using your preferred method:
  • From the Dependencies node, Manage NuGet Packages dialog:

New Project Dialog

  • By just adding it directly to the .csproj:
  <ItemGroup>
    <PackageReference Include="SmallSharp" Version="*" />
  </ItemGroup>
  • Via the dotnet CLI:
> dotnet add package SmallSharp
  • Via the Package Manager console:
PM> install-package SmallSharp
  1. Now open that Program.cs and make changes to the new concise top-level program such as:
using System;
using static System.Console;

WriteLine("Hello World!");

Keep adding as many top-level programs as you need, and switch between them easily by simply changing the active document.

Demo

How It Works

This nuget package leverages in concert the following standalone and otherwise unrelated features of the compiler, nuget, Visual Studio and MSBuild:

  1. The C# compiler only allows one top-level program per compilation.
  2. Launch profiles (the entries in the Run dropdown) are populated from the Properties\launchSettings.json file
  3. Whenever changed, the dropdown selection is persisted as the $(ActiveDebugProfile) MSBuild property in a file named after the project with the .user extension
  4. This file is imported before NuGet-provided MSBuild targets
  5. The $(DefaultItemExcludesInProjectFolder) MSBuild property allows excluding items at the project-level from the automatically added items by the SDK.

Using the above features in concert, SmallSharp essentially does the following:

  • Monitor the active document in VS and emit it as a launchSettings.json profile and set it as the $(ActiveDebugProfile).

  • Exclude .cs files at the project level from being included as <Compile> by the default SDK includes and include them explicitly as <None> instead so they show up in the solution explorer. This prevents the compiler from causing an error for multiple top-level programs.

  • Explicitly include as <Compile> only the $(ActiveDebugProfile) property value.

This basically mean that this it will also work consistently if you use dotnet run from the command-line, since the "Main" file selection is performed exclusively via MSBuild item manipulation.

Finally, there is some lovely COM-based magic to access the active Visual Studio IDE (via DTE) to monitor the currently opened source file to keep it in sync with the launch profile. This is done purely using public COM primitives and equally public VSSDK nuget packages their APIs. This enables some useful integration with the IDE without requiring installing a VS extension from the marketplace and deal with gracefully degrading functionality.

NOTE: If active document tracking fails to initialize properly restarting Visual Studio will almost always fix it. Once tracking starts, it will work consistently from that point on. The Start dropdown is always available as a fallback in case of issues.

Sponsors

Clarius Org Christian Findlay C. Augusto Proiete Kirill Osenkov MFB Technologies, Inc. SandRock Eric C Andy Gocke

Sponsor this project ย 

Learn more about GitHub Sponsors

smallsharp's People

Contributors

alastairtree avatar dependabot[bot] avatar devlooped-bot avatar kzu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

smallsharp's Issues

Renaming file crashes Visual Studio

VS 16.8.5

SmallSharp 1.0.1

After renaming cs file using F2 in Solution Explorer Visual Studio crashes.

Rename one or two times, try to run.

20210212_102653

When changing active files quickly, opening startup file may fail

This manifests in a "program is busy" indicator and a Debug.Fail (in debug builds), when we invoke File.OpenFile via the DTE.

Add some resiliency there, with some timeout and retries so that we can give VS time to respond to user actions before we run our command.

Generator cannot be used

Hi,
upon trying out your generator a compiler warning is emitted:
Generator 'LaunchSettingsGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.'

My repro is here:
https://gist.github.com/MichalBrylka/17a2b66c2885450278ba9ceaca5fdea8

Also - have you considered testing of such generators. Normal approach does not seem to work here, see:
https://gist.github.com/MichalBrylka/933bd1313a097bec63cabd0ac02fc29b

you cannot create a compilation with 2+ top level programs to begin with.

Failure with VS 16.10 preview

The build always fails with SmallSharp requires Visual Studio 16.8 or greater..

This is because the comparison of MSBuild short version (i.e. 16.8 vs 16.10) is
not done numerically, but as a string, resulting in 16.10 being smaller than 16.8. This
didn't surface before because 16.9 is lexicographically greater than 16.8, but now that's
no longer the case.

Make SmallSharp a development dependency

Since it's just a development aid, you might want to use it in your actual
console app development (i.e. a dotnet global tool) but not want it to
become an actual dependency of your package.

Populate launchSettings with candidate top-level compile files

We used to do this via a C# source generator, but stopped doing it because it interferred with the compilation and sometimes broke intellisense.

Now we have a more reliable auto-selection mechanism based on detecting the active document. In some rare cases, however, the IDE detection stops working and it's impossible to select the actual target we want to run.

We should bring back the launchSettings.json generation, but base it entirely on MSBuild instead, so we don't interfere at all with any of the built-in stuff that's already working. We should also run this right after we select the startup file, so it's basically a complement, not a replacement.

Adding new C# file should result in a new startup file

Currently, when adding a new C# file to the project, an infobar error shows up and the file does not become the new startup file.

Both may be related, since a non-startup file would be marked as None content and by the time the text buffer is being initialized, it might already have assumed the item would be a Compile item, failing to initialize intellisense properly for it since it was turned into a non-compile item under the editor's feet.

This seems to also cause more or less random disablement of intellisense from that point on, making things less predictable.

So we should just fix our logic and welcome the new file as the new startup file instead :)

Editor exception from VS log
System.ArgumentNullException: Value cannot be null.
Parameter name: filePath

   at Microsoft.VisualStudio.CodeSense.Editor.Roslyn.CodeElementTag..ctor(SyntaxNodeInfo syntaxNodeInfo, String filePath, DocumentId documentId, Workspace workspace, Guid projectGuid)
      at Microsoft.VisualStudio.CodeSense.Editor.Roslyn.CodeElementTagger.GetTag(Int32 lineNumber)
         at Microsoft.VisualStudio.CodeSense.Editor.Tagger`1.&lt;GetTagsCore&gt;d__28.MoveNext()
            at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.&lt;GetTagsForBuffer&gt;d__47.MoveNext()
            --- End of stack trace from previous location where exception was thrown ---
               at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

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.