{"id":37044473,"url":"https://github.com/dartk/csharp-sourcegen-csx","last_synced_at":"2026-01-14T05:08:39.092Z","repository":{"id":65779183,"uuid":"594726357","full_name":"dartk/csharp-sourcegen-csx","owner":"dartk","description":"Generate C# code from C# scripts","archived":false,"fork":false,"pushed_at":"2023-02-16T18:18:20.000Z","size":20,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-08T00:37:48.409Z","etag":null,"topics":["code-generation","csharp","scripting"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dartk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-29T13:09:35.000Z","updated_at":"2025-09-15T04:52:19.000Z","dependencies_parsed_at":"2023-02-15T23:20:32.813Z","dependency_job_id":null,"html_url":"https://github.com/dartk/csharp-sourcegen-csx","commit_stats":null,"previous_names":["dartk/csxsourcegenerator"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dartk/csharp-sourcegen-csx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartk%2Fcsharp-sourcegen-csx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartk%2Fcsharp-sourcegen-csx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartk%2Fcsharp-sourcegen-csx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartk%2Fcsharp-sourcegen-csx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dartk","download_url":"https://codeload.github.com/dartk/csharp-sourcegen-csx/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dartk%2Fcsharp-sourcegen-csx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28410224,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generation","csharp","scripting"],"created_at":"2026-01-14T05:08:38.405Z","updated_at":"2026-01-14T05:08:39.086Z","avatar_url":"https://github.com/dartk.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSharp.SourceGen.Csx\n\nA C# source generator that runs [C# scripts](https://github.com/dotnet-script/dotnet-script).\n\n- [Prerequisites](#prerequisites)\n- [Installation](#installation)\n- [Source generation](#source-generation)\n- [Saving generated files](#saving-generated-files)\n- [Example](#example)\n- [See also](#see-also)\n\n\n## Prerequisites\n\n* [.NET SDK](https://dotnet.microsoft.com/en-us/download)\n\n* [dotnet-script](https://github.com/dotnet-script/dotnet-script) tool to run C# scripts:\n\n    ```text\n    dotnet tool install -g dotnet-script\n    ```\n    \n    \u003e **Info**: If you don't have access to `dotnet-script`, than take a look at [CSharp.SourceGen.Fsx](https://github.com/dartk/csharp-sourcegen-fsx) - a C# code generator that runs F# scripts using standard `dotnet fsi` command from .NET SDK.\n\n\n# Installation\n\n```text\ndotnet add package Dartk.CSharp.SourceGen.Csx\n```\n\nTo avoid propagating dependency on the package set the option `PrivateAssets=\"all\"` in the project\nfile:\n\n```xml\n\u003cItemGroup\u003e\n    \u003cPackageReference Include=\"Dartk.CSharp.SourceGen.Csx\" Version=\"0.3.0\" PrivateAssets=\"All\" /\u003e\n\u003c/ItemGroup\u003e\n```\n\n\n## Source generation\n\nInclude C# script files with *.csx* extension to the project as `AdditionalFiles`. For example, to execute all scripts in the *Scripts* folder add this to the project file:\n\n```xml\n\u003cItemGroup\u003e\n    \u003cAdditionalFiles Include=\"Scripts/**\" /\u003e\n\u003c/ItemGroup\u003e\n```\n\n*.csx* script's output will be treated as a source code. Meaning that, the following script:\n\n```c#\nConsole.Write(\"\"\"\npublic static class HelloWorld\n{\n    public const string Str = \"Hello, World!\";\n}\n\"\"\");\n```\n\nwill generate a class:\n\n```c#\npublic static class HelloWorld\n{\n    public const string Str = \"Hello, World!\";\n}\n```\n\nA [complete example](#example) is presented below.\n\nScripts that have file names starting with an underscore will not be executed. But they can be included in other scripts using `#load` statement. For instance:\n\n* *_script.csx* - will not be executed, can be included in other scripts\n* *other-script.csx* - will be executed\n\nIf a script references any other files, they should be included to the project as `AdditionalFiles` as well. Because the generator is [incremental](https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md), it can only cache and detect changes for \"additional\" files.\n\n\u003e **Warning**: Microsoft Visual Studio 22 (tested on version 17.4.3 on Windows OS) will call a source generator on every edit of the files that are being cached by the generator. Thus, every character insertion or deletion in a *.csx* script will cause the script execution. Therefore, edit those files in an external editor for better performance.\n\n\n## Saving generated files\n\nTo save the generated source files set properties `EmitCompilerGeneratedFiles` and `CompilerGeneratedFilesOutputPath` in the project file:\n\n```xml\n\u003cPropertyGroup\u003e\n    \u003cEmitCompilerGeneratedFiles\u003etrue\u003c/EmitCompilerGeneratedFiles\u003e\n    \u003c!--Generated files will be saved to 'obj\\GeneratedFiles' folder--\u003e\n    \u003cCompilerGeneratedFilesOutputPath\u003e$(BaseIntermediateOutputPath)\\GeneratedFiles\u003c/CompilerGeneratedFilesOutputPath\u003e\n\u003c/PropertyGroup\u003e\n```\n\n\n## Example\n\nCreate a new console C# project:\n\n```text\ndotnet new console -n Example\n```\n\nInstall the package `Dartk.CSharp.SourceGen.Csx` and set the property `PrivateAssets=\"All\"` by editing the project file *Example.csproj*:\n\n```xml\n\u003cItemGroup\u003e\n    \u003cPackageReference Include=\"Dartk.CSharp.SourceGen.Csx\" Version=\"0.3.0\" PrivateAssets=\"All\"/\u003e\n\u003c/ItemGroup\u003e\n```\n\nCreate a *Scripts* folder in the project directory and include files within as `AdditionalFiles`:\n\n```xml\n\u003cItemGroup\u003e\n    \u003cAdditionalFiles Include=\"Scripts/**\" /\u003e\n\u003c/ItemGroup\u003e\n```\n\nAdd the following file to the *Scripts* folder:\n\n*Number.csx*\n\n```c#\n#r \"nuget: Scriban, 5.4.6\"\n\nusing Scriban;\n\nvar template = \"\"\"\nnamespace Generated.Csx;\n\npublic record Number(int Int, string String) {\n    {{- i = 0 }}\n    {{- for number in numbers }}\n    public static readonly Number {{ string.capitalize number }} = new ({{ ++i }}, \"{{ number }}\");\n    {{- end }}\n}\n\"\"\";\n\nvar output = Template.Parse(template).Render(new {\n    numbers = new [] { \"one\", \"two\", \"three\" }\n});\n\nWrite(output);\n```\n\nThe script above will generate source file *Number.g.cs*:\n\n```c#\n// Generated from 'Number.csx'\nnamespace Generated;\n\npublic record Number(int Int, string String) {\n    public static readonly Number One = new (1, \"one\");\n    public static readonly Number Two = new (2, \"two\");\n    public static readonly Number Three = new (3, \"three\");\n}\n```\n\nNow `Generated.Number` record can be used in your code.\n\nPut this in the *Program.cs*:\n\n```c#\nusing static System.Console;\n\nWriteLine(Generated.Number.One);\nWriteLine(Generated.Number.Two);\nWriteLine(Generated.Number.Three);\n```\n\nIt will write the following:\n\n```text\nNumber { Int = 1, String = one }\nNumber { Int = 2, String = two }\nNumber { Int = 3, String = three }\n```\n\n\n## See also\n\n* [CSharp.SourceGen.Fsx](https://github.com/dartk/csharp-sourcegen-fsx) - Generate C# code from F# scripts\n* [CSharp.SourceGen.Scriban](https://github.com/dartk/csharp-sourcegen-scriban) - Generate C# code from Scriban templates\n* [CSharp.SourceGen.Examples](https://github.com/dartk/csharp-sourcegen-examples) - Examples that demonstrate how to use `CSharp.SourceGen.*` code generators\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdartk%2Fcsharp-sourcegen-csx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdartk%2Fcsharp-sourcegen-csx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdartk%2Fcsharp-sourcegen-csx/lists"}