Skip to content

Instantly share code, notes, and snippets.

@mems
Last active February 14, 2023 22:33
Show Gist options
  • Save mems/a43a91adcce76a89ab19bc49ca1b6830 to your computer and use it in GitHub Desktop.
Save mems/a43a91adcce76a89ab19bc49ca1b6830 to your computer and use it in GitHub Desktop.
Build dependency graph of an ASP.Net project

For all files: HTML, JS, CSS, LESS, CSHTML, ASCX, ASPX, CS (SVC) And config files XML, JSON

Write a webpack loader to load backend templates dependencies.

C# Razor and Web Forms templates

Entry points: ^[^_].*\.cshtml$ and .aspx$ should be an entries points

ASPX template syntax

Generate .cs from .aspx/.ascx (Aka precompile, auto-generated class):

Razor syntax

Razor syntax:

  • @(...): Explicit Razor expressions
  • @if(){...}, @each(){...}, @for(){...}, @functions{...}, etc.
  • @*...*@
  • @{...}
  • @@: escape @
  • @myVar: implicit Razor expressions

Code block:

  • @ followed by identifiers, keywords, comments, "(" and "{"
  • code block body can contains:
    • special tag <text>...</text> to output inside (remove enclosing tag)
    • tags
    • explicit line transition: @:...
    • implicit Razor expressions @myVar: (le @ n'est pas précédé par un caractère alpha num: [a-z0-9])

Tags:

Root level is similar to a tag body:

  • body can contains tags (just the attributes value are parsed, EOF are ignored), code blocks or text node

See also:

Use Roslyn:

Generate CSharp from Razor with RazorTemplateEngine then read the CodeCompileUnit to output AST or JS:

Generate CSharp from Razor with RazorTemplateEngine then tokenize with CSharpSyntaxTree.ParseText() to output AST or JS:

Use RazorJS:

Generate .cs from .cshtml (Aka precompile, auto-generated class):

WASM / Blazor:

#!/usr/bin/env node
// https://github.com/evanw/esbuild/blob/master/npm/esbuild-wasm/bin/esbuild

// Forward to the automatically-generated WebAssembly loader from the Go compiler
const wasm_exec = require.resolve('esbuild-wasm/wasm_exec.js');
const esbuild_wasm = require.resolve('esbuild-wasm/esbuild.wasm');
const code = require('fs').readFileSync(wasm_exec, 'utf8');
const wrapper = new Function('require', 'process', code);
const argv = ['node', wasm_exec, esbuild_wasm].concat(process.argv.slice(2));
wrapper(require, Object.assign(Object.create(process), { argv }));

Compile C# to JS:

Other:

Mustache syntax

Language server

Aka LSP (Language Server Protocol)

Note: LSP doesn't expose AST

Stdio (RPC/IPC):

Grammars / syntax tree generators

Parsers produce syntax trees, Lexers produce tokens.

                 | (E)BNF | ANTLR
-----------------+--------+------
'a' zero or once | [a]    | a?
'a' zero or more | {a}    | a*
'a' once or more | a {a}  | a+

Need context-free grammars, not just an highlighter

Parse using native libs:

Stuff related to webpack:

Antlr:

Custom parser

Syntax tree format

Consume syntax tree

Graph

digraph G {
	size="100,50"
	center=""
	ratio=All
	node[width=.25,hight=.375,fontsize=12,color=lightblue2,style=filled]
	1 -> 3;
	1 -> 17;
	3 -> 15;
	1 [label="Drew.DependencyAnalyser.Tests"];
	3 [label="Drew.DependencyAnalyser"];
	15 [label="Interop.WINGRAPHVIZLib"];
	17 [label="nunit.framework"];
}

digraph "graph" {
	"test1" -> "test2";
	"test1" -> "test3";
	"test2" -> "test4";
	"test3" -> "test4";
}

Use Hierarchical Edge Bundling or Collapsible Force Layout or Drag and Drop Collapsible Tree Layout

Similar files

WebStorm: Code > Find duplicates...

Aka Similar Data Detection (SDD)

Compare tokens:

  • ignore spaces
  • ignore braces: if (foo) {x++} is same as if(foo)x++;
  • ignore function/variable names

Find files with similarities (ex: same parts without whitespaces), find peak (number) or tokens occurence (text: words)

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