1+ // First run: dnx runfile https://github.com/devlooped/oss/blob/main/oss.cs --yes --alias oss
2+ // Subsequently: dnx runfile oss --yes
3+ #: package Spectre . Console@*
4+ #: package CliWrap@*
5+
6+ using System ;
7+ using System . IO ;
8+ using System . Net . Http ;
9+ using System . Runtime . InteropServices ;
10+ using System . Threading . Tasks ;
11+ using CliWrap ;
12+ using Spectre . Console ;
13+
14+ string dotnet = Path . GetFullPath (
15+ Path . Combine ( RuntimeEnvironment . GetRuntimeDirectory ( ) , ".." , ".." , ".." ,
16+ RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? "dotnet.exe" : "dotnet" ) ) ;
17+
18+ AnsiConsole . Write ( new FigletText ( "devlooped oss" ) . Color ( Color . Green ) ) ;
19+ AnsiConsole . WriteLine ( ) ;
20+
21+ var projectName = AnsiConsole . Prompt (
22+ new TextPrompt < string > ( "[green]Project name[/]:" )
23+ . PromptStyle ( "yellow" )
24+ . ValidationErrorMessage ( "[red]Project name cannot be empty[/]" )
25+ . Validate ( v => ! string . IsNullOrWhiteSpace ( v ) ) ) ;
26+
27+ var repoName = AnsiConsole . Prompt (
28+ new TextPrompt < string > ( "[green]Repo name[/]:" )
29+ . PromptStyle ( "yellow" )
30+ . DefaultValue ( projectName ) ) ;
31+
32+ var packageId = AnsiConsole . Prompt (
33+ new TextPrompt < string > ( "[green]Package ID[/]:" )
34+ . PromptStyle ( "yellow" )
35+ . DefaultValue ( $ "Devlooped.{ projectName } ") ) ;
36+
37+ AnsiConsole . WriteLine ( ) ;
38+ AnsiConsole . Write ( new Rule ( "[yellow]Setting up OSS project[/]" ) . RuleStyle ( "grey" ) . LeftJustified ( ) ) ;
39+ AnsiConsole . WriteLine ( ) ;
40+
41+ await RunDotNet ( "file init https://github.com/devlooped/oss/blob/main/.netconfig" , "Initializing dotnet file sync from devlooped/oss" ) ;
42+ await RunDotNet ( $ "new classlib -n { projectName } -o src/{ projectName } -f net10.0", $ "Creating class library src/{ projectName } ") ;
43+ await RunDotNet ( $ "new xunit -n Tests -o src/Tests -f net10.0", "Creating xUnit test project src/Tests" ) ;
44+ await RunDotNet ( $ "add src/Tests/Tests.csproj reference src/{ projectName } /{ projectName } .csproj", $ "Adding reference from Tests to { projectName } ") ;
45+ await RunDotNet ( $ "new solution -n { projectName } ", $ "Creating solution { projectName } .slnx") ;
46+ await RunDotNet ( $ "sln { projectName } .slnx add src/{ projectName } /{ projectName } .csproj src/Tests/Tests.csproj", $ "Adding projects to { projectName } .slnx") ;
47+
48+ await AnsiConsole . Status ( )
49+ . Spinner ( Spinner . Known . Dots )
50+ . SpinnerStyle ( Style . Parse ( "green" ) )
51+ . StartAsync ( "Downloading readme.md template..." , async ctx =>
52+ {
53+ using var http = new HttpClient ( ) ;
54+ var readmeContent = await http . GetStringAsync (
55+ "https://raw.githubusercontent.com/devlooped/oss/main/readme.tmp.md" ) ;
56+
57+ readmeContent = readmeContent
58+ . Replace ( "{{PROJECT_NAME}}" , projectName )
59+ . Replace ( "{{PACKAGE_ID}}" , packageId )
60+ . Replace ( "{{REPO_NAME}}" , repoName ) ;
61+
62+ await File . WriteAllTextAsync ( "readme.md" , readmeContent ) ;
63+ ctx . Status ( "Downloaded and processed readme.md" ) ;
64+ } ) ;
65+
66+ AnsiConsole . MarkupLine ( "[green]✓[/] Created [yellow]readme.md[/]" ) ;
67+
68+ await File . WriteAllTextAsync (
69+ Path . Combine ( "src" , projectName , "readme.md" ) ,
70+ $ """
71+ [](osmfeula.txt)
72+ [](license.txt)
73+ [](https://github.com/devlooped/{ repoName } )
74+
75+ <!-- include ../../readme.md#content -->
76+
77+ <!-- include https://github.com/devlooped/.github/raw/main/osmf.md -->
78+
79+ <!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
80+
81+ <!-- exclude -->
82+ """ ) ;
83+
84+ AnsiConsole . WriteLine ( ) ;
85+ AnsiConsole . Write ( new Rule ( "[green]Done![/]" ) . RuleStyle ( "grey" ) . LeftJustified ( ) ) ;
86+ AnsiConsole . WriteLine ( ) ;
87+ AnsiConsole . MarkupLine ( $ "[bold]Project:[/] [yellow]{ projectName } [/]") ;
88+ AnsiConsole . MarkupLine ( $ "[bold]Repo:[/] [yellow]{ repoName } [/]") ;
89+ AnsiConsole . MarkupLine ( $ "[bold]Package ID:[/] [yellow]{ packageId } [/]") ;
90+
91+ async Task RunDotNet ( string command , string description )
92+ {
93+ AnsiConsole . MarkupLine ( $ "[grey]{ Markup . Escape ( description ) } ...[/]") ;
94+ await Cli . Wrap ( dotnet )
95+ . WithArguments ( command )
96+ . WithStandardOutputPipe ( PipeTarget . ToStream ( Console . OpenStandardOutput ( ) ) )
97+ . WithStandardErrorPipe ( PipeTarget . ToStream ( Console . OpenStandardError ( ) ) )
98+ . ExecuteAsync ( ) ;
99+ AnsiConsole . MarkupLine ( $ "[green]✓[/] { Markup . Escape ( description ) } ") ;
100+ }
0 commit comments