-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
290 lines (218 loc) · 19.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,maximum-scale=2">
<link rel="stylesheet" type="text/css" media="screen" href="/SmallSharp/assets/css/style.css?v=dac83180423bdc7536f9936f881cd1994640256d">
<!-- Begin Jekyll SEO tag v2.8.0 -->
<title>SmallSharp | Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.</title>
<meta name="generator" content="Jekyll v3.9.5" />
<meta property="og:title" content="SmallSharp" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button." />
<meta property="og:description" content="Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button." />
<meta property="og:site_name" content="SmallSharp" />
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="SmallSharp" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"WebSite","description":"Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.","headline":"SmallSharp","name":"SmallSharp","url":"/SmallSharp/"}</script>
<!-- End Jekyll SEO tag -->
<!-- start custom head snippets, customize with your own _includes/head-custom.html file -->
<!-- Setup Google Analytics -->
<!-- You can set your favicon here -->
<!-- link rel="shortcut icon" type="image/x-icon" href="/SmallSharp/favicon.ico" -->
<!-- end custom head snippets -->
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/devlooped/SmallSharp">View on GitHub</a>
<h1 id="project_title">SmallSharp</h1>
<h2 id="project_tagline">Create, edit and run multiple C# top-level programs in the same project by just selecting the startup program from the start button.</h2>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h1 id="-smallsharp"><img src="https://raw.githubusercontent.com/devlooped/SmallSharp/main/assets/img/icon-32.png" alt="Icon" /> SmallSharp</h1>
<p><a href="https://www.nuget.org/packages/SmallSharp"><img src="https://img.shields.io/nuget/v/SmallSharp.svg?color=royalblue" alt="Version" /></a> <a href="https://www.nuget.org/packages/SmallSharp"><img src="https://img.shields.io/nuget/dt/SmallSharp?color=darkmagenta" alt="Downloads" /></a> <a href="https://github.com/devlooped/SmallSharp/blob/main/LICENSE"><img src="https://img.shields.io/github/license/devlooped/SmallSharp.svg?color=blue" alt="License" /></a> <a href="https://github.com/devlooped/avatar/actions?query=branch%3Amain+workflow%3Abuild+"><img src="https://github.com/devlooped/SmallSharp/workflows/build/badge.svg?branch=main" alt="GH CI Status" /></a></p>
<p><img src="https://raw.githubusercontent.com/devlooped/SmallSharp/main/assets/img/SmallSharpDemo.gif" alt="Icon" /></p>
<!-- #content -->
<p>Create, edit and run multiple C# top-level programs in the same project 😍</p>
<h2 id="why">Why</h2>
<p>The new-ish C# <a href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements">top-level programs</a>
allow a very intuitive, simple and streamlined experience for quickly spiking or learning C#.</p>
<p>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 <code class="language-plaintext highlighter-rouge">.txt</code> or change its build action).</p>
<p><strong>SmallSharp</strong> 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!</p>
<p><img src="https://raw.githubusercontent.com/devlooped/SmallSharp/main/assets/img/launchSettings.png" alt="start button" /></p>
<p>This list is automatically kept in sync as you add more <code class="language-plaintext highlighter-rouge">.cs</code> 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 <em>None</em> items.</p>
<p>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.</p>
<h2 id="usage">Usage</h2>
<p>There is no need to install any Visual Studio extension. SmallSharp works by just installing the
<a href="https://nuget.org/packages/SmallSharp">SmallSharp</a> nuget package in a C# console project.</p>
<ol>
<li>Create a new Console project:</li>
</ol>
<p><img src="https://raw.githubusercontent.com/devlooped/SmallSharp/main/assets/img/NewConsoleProject.png" alt="New Project Dialog" /></p>
<p>By default, this new console project may not be set up to target <code class="language-plaintext highlighter-rouge">net5.0</code> 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:</p>
<ul>
<li>Target the recommended framework version (i.e. net6.0):</li>
</ul>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><Project</span> <span class="na">Sdk=</span><span class="s">"Microsoft.NET.Sdk"</span><span class="nt">></span>
<span class="nt"><PropertyGroup></span>
<span class="nt"><OutputType></span>Exe<span class="nt"></OutputType></span>
<span class="nt"><TargetFramework></span>net6.0<span class="nt"></TargetFramework></span>
<span class="nt"></PropertyGroup></span>
<span class="nt"></Project></span>
</code></pre></div></div>
<ul>
<li>Or use latest C# language version if targeting another framework:</li>
</ul>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><Project</span> <span class="na">Sdk=</span><span class="s">"Microsoft.NET.Sdk"</span><span class="nt">></span>
<span class="nt"><PropertyGroup></span>
<span class="nt"><OutputType></span>Exe<span class="nt"></OutputType></span>
<span class="nt"><TargetFramework></span>net472<span class="nt"></TargetFramework></span>
<span class="nt"><LangVersion></span>latest<span class="nt"></LangVersion></span>
<span class="nt"></PropertyGroup></span>
<span class="nt"></Project></span>
</code></pre></div></div>
<ol>
<li>Install the <strong>SmallSharp</strong> nuget package using your preferred method:</li>
</ol>
<ul>
<li>From the Dependencies node, Manage NuGet Packages dialog:</li>
</ul>
<p><img src="https://raw.githubusercontent.com/devlooped/SmallSharp/main/assets/img/NuGetPackage.png" alt="New Project Dialog" /></p>
<ul>
<li>By just adding it directly to the .csproj:</li>
</ul>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nt"><ItemGroup></span>
<span class="nt"><PackageReference</span> <span class="na">Include=</span><span class="s">"SmallSharp"</span> <span class="na">Version=</span><span class="s">"*"</span> <span class="nt">/></span>
<span class="nt"></ItemGroup></span>
</code></pre></div></div>
<ul>
<li>Via the dotnet CLI:</li>
</ul>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>> dotnet add package SmallSharp
</code></pre></div></div>
<ul>
<li>Via the Package Manager console:</li>
</ul>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>PM> install-package SmallSharp
</code></pre></div></div>
<ol>
<li>Now open that Program.cs and make changes to the new concise top-level program such as:</li>
</ol>
<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">static</span> <span class="n">System</span><span class="p">.</span><span class="n">Console</span><span class="p">;</span>
<span class="nf">WriteLine</span><span class="p">(</span><span class="s">"Hello World!"</span><span class="p">);</span>
</code></pre></div></div>
<p>Keep adding as many top-level programs as you need, and switch between them easily by simply
changing the active document.</p>
<p><img src="https://raw.githubusercontent.com/devlooped/SmallSharp/main/assets/img/TrackSelection.gif" alt="Demo" /></p>
<h2 id="how-it-works">How It Works</h2>
<p>This nuget package leverages in concert the following standalone and otherwise
unrelated features of the compiler, nuget, Visual Studio and MSBuild:</p>
<ol>
<li>The C# compiler only allows one top-level program per compilation.</li>
<li>Launch profiles (the entries in the Run dropdown) are populated from the Properties\launchSettings.json file</li>
<li>Whenever changed, the dropdown selection is persisted as the <code class="language-plaintext highlighter-rouge">$(ActiveDebugProfile)</code> MSBuild property in a file
named after the project with the <code class="language-plaintext highlighter-rouge">.user</code> extension</li>
<li>This file is imported before NuGet-provided MSBuild targets</li>
<li>The <code class="language-plaintext highlighter-rouge">$(DefaultItemExcludesInProjectFolder)</code> MSBuild property allows excluding items at the project-level from
the automatically added items by the SDK.</li>
</ol>
<p>Using the above features in concert, <strong>SmallSharp</strong> essentially does the following:</p>
<ul>
<li>
<p>Monitor the active document in VS and emit it as a <code class="language-plaintext highlighter-rouge">launchSettings.json</code> profile and set it as
the <code class="language-plaintext highlighter-rouge">$(ActiveDebugProfile)</code>.</p>
</li>
<li>
<p>Exclude <code class="language-plaintext highlighter-rouge">.cs</code> files at the project level from being included as <code class="language-plaintext highlighter-rouge"><Compile></code> by the default SDK
includes and include them explicitly as <code class="language-plaintext highlighter-rouge"><None></code> instead so they show up in the solution explorer.
This prevents the compiler from causing an error for multiple top-level programs.</p>
</li>
<li>
<p>Explicitly include as <code class="language-plaintext highlighter-rouge"><Compile></code> only the <code class="language-plaintext highlighter-rouge">$(ActiveDebugProfile)</code> property value.</p>
</li>
</ul>
<p>This basically mean that this it will also work consistently if you use <code class="language-plaintext highlighter-rouge">dotnet run</code> from the command-line,
since the “Main” file selection is performed exclusively via MSBuild item manipulation.</p>
<p>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.</p>
<blockquote>
<p>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.</p>
</blockquote>
<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
<h1 id="sponsors">Sponsors</h1>
<!-- sponsors.md -->
<p><a href="https://github.com/clarius"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/clarius.png" alt="Clarius Org" title="Clarius Org" /></a>
<a href="https://github.com/KirillOsenkov"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/KirillOsenkov.png" alt="Kirill Osenkov" title="Kirill Osenkov" /></a>
<a href="https://github.com/MFB-Technologies-Inc"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/MFB-Technologies-Inc.png" alt="MFB Technologies, Inc." title="MFB Technologies, Inc." /></a>
<a href="https://github.com/decriptor"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/decriptor.png" alt="Stephen Shaw" title="Stephen Shaw" /></a>
<a href="https://github.com/torutek-gh"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/torutek-gh.png" alt="Torutek" title="Torutek" /></a>
<a href="https://github.com/drivenet"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/drivenet.png" alt="DRIVE.NET, Inc." title="DRIVE.NET, Inc." /></a>
<a href="https://github.com/AshleyMedway"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/AshleyMedway.png" alt="Ashley Medway" title="Ashley Medway" /></a>
<a href="https://github.com/Keflon"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/Keflon.png" alt="Keith Pickford" title="Keith Pickford" /></a>
<a href="https://github.com/tbolon"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/tbolon.png" alt="Thomas Bolon" title="Thomas Bolon" /></a>
<a href="https://github.com/kfrancis"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/kfrancis.png" alt="Kori Francis" title="Kori Francis" /></a>
<a href="https://github.com/twenzel"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/twenzel.png" alt="Toni Wenzel" title="Toni Wenzel" /></a>
<a href="https://github.com/Giorgi"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/Giorgi.png" alt="Giorgi Dalakishvili" title="Giorgi Dalakishvili" /></a>
<a href="https://github.com/unoplatform"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/unoplatform.png" alt="Uno Platform" title="Uno Platform" /></a>
<a href="https://github.com/dansiegel"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/dansiegel.png" alt="Dan Siegel" title="Dan Siegel" /></a>
<a href="https://github.com/rbnswartz"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/rbnswartz.png" alt="Reuben Swartz" title="Reuben Swartz" /></a>
<a href="https://github.com/jfoshee"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/jfoshee.png" alt="Jacob Foshee" title="Jacob Foshee" /></a>
<a href="https://github.com/eajhnsn1"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/Mrxx99.png" alt="" title="")](https://github.com/Mrxx99)
[![Eric Johnson](https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/eajhnsn1.png "Eric Johnson" /></a>
<a href="https://github.com/IxTechnologies"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/IxTechnologies.png" alt="Ix Technologies B.V." title="Ix Technologies B.V." /></a>
<a href="https://github.com/davidjenni"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/davidjenni.png" alt="David JENNI" title="David JENNI" /></a>
<a href="https://github.com/Jonathan-Hickey"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/Jonathan-Hickey.png" alt="Jonathan " title="Jonathan " /></a>
<a href="https://github.com/okyrylchuk"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/okyrylchuk.png" alt="Oleg Kyrylchuk" title="Oleg Kyrylchuk" /></a>
<a href="https://github.com/akunzai"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/akunzai.png" alt="Charley Wu" title="Charley Wu" /></a>
<a href="https://github.com/jakobt"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/jakobt.png" alt="Jakob Tikjøb Andersen" title="Jakob Tikjøb Andersen" /></a>
<a href="https://github.com/seanalexander"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/seanalexander.png" alt="Seann Alexander" title="Seann Alexander" /></a>
<a href="https://github.com/tinohager"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/tinohager.png" alt="Tino Hager" title="Tino Hager" /></a>
<a href="https://github.com/ploeh"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/ploeh.png" alt="Mark Seemann" title="Mark Seemann" /></a>
<a href="https://github.com/KenBonny"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/KenBonny.png" alt="Ken Bonny" title="Ken Bonny" /></a>
<a href="https://github.com/SimonCropp"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/SimonCropp.png" alt="Simon Cropp" title="Simon Cropp" /></a>
<a href="https://github.com/agileworks-eu"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/agileworks-eu.png" alt="agileworks-eu" title="agileworks-eu" /></a>
<a href="https://github.com/sorahex"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/sorahex.png" alt="sorahex" title="sorahex" /></a>
<a href="https://github.com/arsdragonfly"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/arsdragonfly.png" alt="Zheyu Shen" title="Zheyu Shen" /></a>
<a href="https://github.com/vezel-dev"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/vezel-dev.png" alt="Vezel" title="Vezel" /></a>
<a href="https://github.com/ChilliCream"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/ChilliCream.png" alt="ChilliCream" title="ChilliCream" /></a>
<a href="https://github.com/4OTC"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/.github/avatars/4OTC.png" alt="4OTC" title="4OTC" /></a></p>
<!-- sponsors.md -->
<p><a href="https://github.com/sponsors/devlooped"><img src="https://raw.githubusercontent.com/devlooped/sponsors/main/sponsor.png" alt="Sponsor this project" title="Sponsor this project" /></a>
</p>
<p><a href="https://github.com/sponsors">Learn more about GitHub Sponsors</a></p>
<!-- https://github.com/devlooped/sponsors/raw/main/footer.md -->
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p class="copyright">SmallSharp maintained by <a href="https://github.com/devlooped">devlooped</a></p>
<p>Published with <a href="https://pages.github.com">GitHub Pages</a></p>
</footer>
</div>
</body>
</html>