Skip to content

Roslyn version for .NET Standard 🚧#46

Open
atifaziz wants to merge 14 commits into
KvanTTT:masterfrom
atifaziz:roslyn-dotnet
Open

Roslyn version for .NET Standard 🚧#46
atifaziz wants to merge 14 commits into
KvanTTT:masterfrom
atifaziz:roslyn-dotnet

Conversation

@atifaziz

@atifaziz atifaziz commented Feb 20, 2019

Copy link
Copy Markdown

This PR builds on top of PR #45 from @codeFather2 and updates the library to target .NET Standard 2.0 and tests to target .NET Core 2.1. While I realize that #45 is work-in-progress, I wanted to share my results and findings in case we'd like to get this merged.

Why do this?

Because there is no reason to limit the library to .NET Framework.

Summary of changes

The GUI was upgraded from .NET Framework 4.6 to 4.6.1 to neatly align with .NET Standard 2.0.

The biggest change needed was in the CompileUtils class that relied on System.CodeDom for compilation, but which is unsupported for now (see dotnet/corefx#12180). So what I did, instead, was to emulate some of the types to have the least impact on the rest of the code and instead spawn csc.exe for the real work. Its CanCompile and Compile methods were then changed to take an additional parameter that's the path to the C# compiler executable. I've also added a small batch to the root of the project called install.cmd that will download the compiler via the Microsoft.Net.Compilers package. If someone clones the repo, they have to run install.cmd. This can be improved to happen automagically if need be. However, I feel that CompileUtils should be moved out of the library project and into common code shared by the GUI and the test project that really use it.

@KvanTTT

KvanTTT commented Mar 7, 2019

Copy link
Copy Markdown
Owner

Sorry, I'm too busy now. I hope to look at it at the beginning of April. @codeFather2 could you please make a review and point something about tests you wrote to me?

@atifaziz

Copy link
Copy Markdown
Author

Meanwhile, I published my own minifier, which isn't as ambitious. It doesn't have any external dependencies (not even Roslyn) and filters comments and unnecessary whitespace (so no minification of identifiers) with the main use case being to generate stable hashes/digests (avoid wasteful work if there's no useful change to content).

@KvanTTT

KvanTTT commented Mar 27, 2019

Copy link
Copy Markdown
Owner

It's quite interesting, but it sounds like your minifier is not able to shorten identifiers?

@thomasdiemar

thomasdiemar commented Mar 27, 2019

Copy link
Copy Markdown

I made my own completely from scratch also based on Roselyn. It can shortnen identifers, although it dos'nt take into account all the different scopes. Så the good identifer replacement like "a" will only be used as a replacement for one original identifer. But its okay because the size of programs i'm using it for will only generate identifiers up to 3 long.

@KvanTTT

KvanTTT commented Mar 27, 2019

Copy link
Copy Markdown
Owner

Is your project available on GitHub?

@atifaziz

atifaziz commented Mar 27, 2019

Copy link
Copy Markdown
Author

It's quite interesting, but it sounds like your minifier is not able to shorten identifiers?

Yes and I already mentioned that:

(so no minification of identifiers)

It's strictly a minifier, not an obfuscator or an uglifier, which I see as two separate things. I wanted to minify without resorting to Roslyn because that requires dragging in a lot (not to mention a specific version that you have to keep updating) and isn't necessarily fast or cheap on memory. My goal was to cheaply & quickly generate more stable hashes/digests of sources such that changes in whitespace (tabs/spaces, line-endings, formatting, etc.), comments and regions do not change the hash. For example, most watchers will trigger a re-compilation on any physical change to a file. With a minified version you avoid doing work unless the code changes. Here's an example scenario cited in the README:

A monitor that actively watches source files for changes and triggers a re-compilation of binaries would be one application that could benefit from minification of sources. The monitor could compute a hash from the minified sources and only trigger a re-compilation if the hashes of the minified and original versions no longer compare equal (instead of whenever a physical change occurs). Minification removes C# comments and extraneous whitespace in the source as they do not constitute a useful change that could affect behaviour of the code at run-time.

It could benefit from minification of private program details such that changing a local variable name that isn't publicly visible anywhere doesn't change the hash, but then that requires semantic analysis and adds complexity. The level of minification I chose seemed Good Enough™ and way better than any physical change. Since it's cheap and fast, there's no cost to using it on the desktop, server, cloud or in a library.

Also, since it parses the absolute minimum C# grammar, leaving the rest as raw text, you can technically cheat and minify C# snippets like scripts and expressions provided that they are syntactically valid.

For quick and ad-hoc syntactic validation using Roslyn, I have published CSharpSyntaxValidator as a separate tool.

@atifaziz

Copy link
Copy Markdown
Author

FWIW, I've also published my C# minifier for anyone to try out directly in the browser. It's a client-side Blazor application so no code is round-tripped over the wire (can also be cloned and tested locally via dotnet run).

@KvanTTT

KvanTTT commented May 28, 2019

Copy link
Copy Markdown
Owner

Interesting! Will take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants