DEV Community

Cover image for DevLog 20250724: C# Scripting, Pure 2, Updated + Some Recap

DevLog 20250724: C# Scripting, Pure 2, Updated + Some Recap

Intro

Pure has been our in-house C# scripting solution for a few years now.

After experimenting with the well-known CS-Script and some other solutions I can no longer recall, I began experimenting with Roslyn in .NET around 2022/2023, and it's been working well ever since.

Usage

Below is a basic Pure script and its execution:

using static System.Console; var sum = File.ReadLines("Data.csv") .Skip(1) .Select(l => l.Split(',')) .Sum(parts => double.Parse(parts[1])); WriteLine(sum); 
Enter fullscreen mode Exit fullscreen mode

Result

Recap

Version 0.0.3

v0.0.3

The first version of Pure was just a simple wrapper around Roslyn. Later, we expanded it to support syntax like Import(MathNet.Numerics). It wasn't until we added native libraries like CSV, ODBC, and CentralSnippets that things started to get interesting.

Motivation

Below is an old table I compiled when first drafting the design document for Pure (1), with some updates to mention newer solutions:

Platform Installation Size Stability Package Management/Functionalities Summary
Pure (Original) Requires .NET 8 (ASP.NET) Runtime; distributable is around 100MB C# .NET 8-based, likely never to change, very stable (depends on underlying C#); very few new language constructs. Easily migratable to proper C#. Zero-hassle package management and functionality import; single-file throughout. Provides the most efficient short-snippet authoring capabilities.
C# Proper development with an IDE (Visual Studio or IntelliJ) takes forever to install. Very stable as long as one avoids GUI-heavy setups. Many button clicks (in IDE) or commands or .csproj modifications required to install packages. Self-contained dependency environment; fast and lightweight. Best for strongly typed and highly articulated solutions.
Python Too many versions. Fairly stable. Package management is troublesome; messy distributable/end-script environments. Lacks advanced language constructs; ubiquitous support.
PowerShell Super easy to install and use. Fairly stable. Batteries included, and generally a bad idea to import additional unofficial functionalities. Good for process automation; syntax not suited for efficient OOP programming.
Perl Super lightweight and fast. (PENDING) (PENDING) (PENDING)
Pure 2 300MB (Pending) POS-compliant. Preferable if you want easier version control, a text-based scripting experience, and a close-to-C# feel.
Divooka 500MB (Pending) Battery-packed for everyday use. All you need for daily scripting.

What's Special

Pure 2 is a lightweight C# scripting platform with modern C# 12 syntax and easy NuGet import support. Pure is based on Roslyn. In addition to the base .NET runtime, Pure offers additional libraries for handling common tasks, along with a handy scripting interface (REPL and Notebook) and a place where people can share their snippets.

Pure 2 is conceptually similar to Pure but architecturally very different - it directly builds upon Parcel NExT's Ama runtime (the same runtime used by Divooka). Pure is designed for very short snippets and getting everyday tasks done faster.

Pure 2 uses C# 12 and is similar to top-level statements in C#, with the following features:

  1. You can define types and functions anywhere in the middle of the script.
  2. Pure has its own package import behavior (default namespace usage and package lookup rules).
  3. Default global scope math functions, and default using for System.IO and System.Linq.
  4. Top-level function and variable definitions, alongside classes and other common C# constructs.
  5. Simplifies common tasks even further.
  6. Macros to support text-based parsing, file inclusion, and module imports.

Features

  • Single-word standard package names. One-command NuGet package import and on-the-fly package invocation (through Import).
  • Full compatibility with Divooka packages and Divooka graphs.
  • Pure uses PUREPATH to search for scripts when using Include and when executing script files directly from the command line as the first argument.
  • Pure is very lightweight and will always be a thin layer on top of Roslyn/.NET runtime.
  • CLI REPL; GUI Notebook.

Basic Syntax

Certain macros/syntax are provided to implement language-level scripting constructs:

Macro/Syntax Function Remark
Import() Import modules from PATH, and automatically download packages from NuGet Must be on its own line
Include() Transclude scripts from file path Must be on its own line; currently, it's only safe to include from the top level. Nested Include calls may result in unpredictable behavior. See issue
Help(name) Get help about namespaces, types, and specific variables
Expression evaluation For single-line assignment and variable creation, no need to append ; at the end of the line - for script files, proper formatting is required Only applicable in REPL

Summary

Be aware that adopting a scripting system - any scripting system - comes with significant risks. It's wise to plan carefully and pick the best tools for the task at hand.

References

Top comments (0)