📜 ⬆️ ⬇️

C # console to run simple "scripts"

Have you ever performed a massive file renaming, or some other, seemingly trivial, but routine task? Never created bat'niki, but you know C #?

Being in a similar situation, within 15 minutes a fairly simple application was created that looks like a console, but which understands C #.

image
')


The application works on the principle of memorizing code, and executing the resulting script. The script is supported from the file that appears in the directory with the application when it is first executed.

The basic commands that are required to create a script through the console and execute it have been added:
! show - shows the memorized script entirely
! del - removes the last line in the script
! clear - erases the script
! run - executes the script and saves its contents to the file “script.cs”
! runsc - executes the script from the file “script.cs” (for the case if you are editing the file with a text editor)

Source Program.cs (actually the entire application):

using System ;
using System.CodeDom.Compiler ;
using System.Collections.Generic ;
using System.IO ;
using System.Reflection ;
using System.Text ;
using Microsoft.CSharp ;

namespace SharpConsole
{
class program
{
const string header = @ "
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Collections.Generic;

namespace CScript
{
public class Script
{
public static void ScriptMethod ()
{
" ;
const string footer = @ "
}
}
} " ;

static void Main ( string [ ] args )
{
Console. Title = "SharpConsole" ;
Console. WriteLine ( "Welcome to SharpConsole v0.1. Type! Help for help." ) ;

List < string > code = new List < string > ( ) ;
while ( true )
{
Console. Write ( "csharp>" ) ;
string line = Console. ReadLine ( ) ;
switch ( line )
{
case "! run" :
{
string program = FormatSources ( code ) ;
File. WriteAllText ( "script.cs" , program ) ;
ExecuteScript ( program ) ;
}
break ;
case "! show" :
{
Console. WriteLine ( ) ;
Console. WriteLine ( FormatSources ( code ) ) ;
Console. WriteLine ( ) ;
}
break ;
case "! del" :
code. RemoveAt ( code. Count - 1 ) ;
break ;
case "! clear" :
{
code = new List < string > ( ) ;
Console. WriteLine ( "Done." ) ;
}
break ;
case "! runsc" :
{
string script = File. ReadAllText ( "script.cs" ) ;
ExecuteScript ( script ) ;
}
break ;
case "! help" :
{
string [ ] commands = new string [ ]
{
"1. Type your code in it , "
"2.! Show will shows your code" ,
"3.! Del remove last line from your code" ,
"4.! Clear remove all lines from your code" ,
"5.! Run will run the script in memory in script.cs" ,
"6.! Runsc will run script.cs" ,
"Have fun! :)" ,
} ;
foreach ( var str in commands )
Console. WriteLine ( str ) ;
}
break ;
default :
{
code. Add ( "" + line ) ;
}
break ;
}



}
}

private static void ExecuteScript ( string program )
{
var CSHarpProvider = CSharpCodeProvider. CreateProvider ( "CSharp" ) ;
CompilerParameters compilerParams = new CompilerParameters ( )
{
GenerateExecutable = false ,
GenerateInMemory = true
} ;
compilerParams. ReferencedAssemblies . AddRange ( new string [ ]
{
"System.dll" ,
"System.Core.dll" ,
"System.Net.dll" ,
} ) ;
var compilerResult = CSHarpProvider. CompileAssemblyFromSource ( compilerParams, program ) ;
if ( compilerResult. Errors . Count == 0 )
{
Console. WriteLine ( "Executing ..." ) ;
try
{
compilerResult. CompiledAssembly . GetType ( "CScript.Script" ) . GetMethod ( "ScriptMethod" ) . Invoke ( null , null ) ;
Console. WriteLine ( "Done." ) ;
}
catch ( Exception e )
{
Console. WriteLine ( e. InnerException . Message + "rn" + e. InnerException . StackTrace ) ;
}
}
else
{
foreach ( var oline in compilerResult. Output )
Console. WriteLine ( oline ) ;
}
}

private static string FormatSources ( List < string > code )
{
string program = header ;
StringBuilder sb = new StringBuilder ( program ) ;

foreach ( var sc in code )
{
sb. AppendLine ( sc ) ;
}
sb. AppendLine ( footer ) ;
return sb. ToString ( ) ;
}
}
}


By default, when executing the script, 3 references are connected to the assembly:

compilerParams. ReferencedAssemblies . AddRange ( new string [ ]
{
"System.dll" ,
"System.Core.dll" ,
"System.Net.dll" ,
} ) ;


But you can add others for your needs.

VS project: download

I hope it will be useful to someone! I will be glad to answer questions.

Source: https://habr.com/ru/post/170385/


All Articles