Giter Site home page Giter Site logo

oruchreis / scriptsourcegenerator Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 63 KB

A Source Generator that generate output from .csx files

License: GNU General Public License v3.0

C# 99.68% Smalltalk 0.32%
csharp csx csx-script dotnet source-generator

scriptsourcegenerator's Introduction

ScriptSourceGenerator

Nuget

This geneator can compile and run csx files and generate files from csx scripts using Roslyn scripting.

Usage

  • Add nuget package into your project Nuget
  • Add a .csx script file into project:
var httpClient = new HttpClient();
var result = await client.GetStringAsync("http://www.github.com");
  • Change .csx file Build Action property to C# analyzer additional file or modify csproj like this:
<ItemGroup>
	<None Remove="MyScript.csx" />
	<AdditionalFiles Include="MyScript.csx" />
</ItemGroup>
  • If you want to create files as an output, you can use global Output indexer property which is a StringBuilder:
Output["Github.html"].Append(result);
  • Even you can create .cs files to generate compile time source codes:
Output["MyModel.cs"].AppendLine("""
public class MyModel
{
""");
for(var i=0; i < 5; i++)
{
    Output["MyModel.cs"].AppendLine($$"""
        public string Prop{{i}} { get; set; } 
    """);
}

Output["MyModel.cs"].AppendLine("}");

and it will be generate this file:

public class MyModel
{
    public string Prop0 { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
    public string Prop4 { get; set; }
}
  • To add assembly references and nuget references use #r directives:
#r "nuget:Microsoft.OpenApi.Readers/1.4.5"
#r "System.Net.Http"
using System;
using System.Net.Http;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Readers;

var httpClient = new HttpClient
{
    BaseAddress = new Uri("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/")
};

var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml");

// Read V3 as YAML
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);

// Write V2 as JSON
var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json);
Output["GeneratedModels.cs"].Append(outputString);
  • External csx files can be imported with #load directive
#load "NetStandard20Fixes.csx"
#r "nuget: NSwag.Core.Yaml/13.18.0"
#r "nuget: NSwag.CodeGeneration.CSharp/13.18.0"            
#r "System.Net.Http"
using System.Net.Http;
using NJsonSchema.Generation;
using NJsonSchema.Yaml;
using NSwag;
using NSwag.CodeGeneration.CSharp;

string yaml;
using (var httpClient = new HttpClient())
    yaml = await httpClient.GetStringAsync("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml");

var openApiDocument = await OpenApiYamlDocument.FromYamlAsync(yaml);

var settings = new CSharpClientGeneratorSettings
{    

};

var generator = new CSharpClientGenerator(openApiDocument, settings);    

Output["g.cs"].Append(generator.GenerateFile());

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.