public static void Create(string psscript, bool experimental, string executionpolicy, string scriptname, string scriptversion, bool blockcat, bool selfcontained, bool onedir, bool embedresources, bool noextract, string output, bool verbose) { // Reading powershell script and finding base name string[] PSScriptFileLines = File.ReadAllLines(psscript); string PsScriptBaseName = Path.GetFileNameWithoutExtension(psscript); string MainCsFile = $"{PsScriptBaseName}.cs"; // cli parser utilities PowershellFeatures.PowershellParser ParsePowershellScript = new PowershellFeatures.PowershellParser(PSScriptFileLines, Verbosity: verbose); string ProgramDescription = ParsePowershellScript.ParseDescription(); string ProgramUsage = ParsePowershellScript.ParseUsage(); string[] ParsedExamples = ParsePowershellScript.ParseExamples(); string[] ParsedParameters = ParsePowershellScript.ParseParameters(); string[] ParsedDefaultCode = ParsePowershellScript.ParseDefaultCode(ParsedParameters); string[] ParsedHelp = ParsePowershellScript.ParseHelp(); if (noextract) { // Handling powershell script comments PSScriptFileLines = PowershellFeatures.Passed.RemoveMultilineComments(PSScriptFileLines); PSScriptFileLines = PowershellFeatures.Passed.RemoveComments(PSScriptFileLines); // Experimental Features if (experimental) { PSScriptFileLines = PowershellFeatures.Experimental.RemoveDoubleQuotes(PSScriptFileLines); // PSScriptFileLines = PowershellFeatures.Experimental.RemoveDoubleQuotesDirectly(PSScriptFileLines); } // Checking for potential compile time errors PowershellFeatures.Passed.CheckErrors(PSScriptFileLines); } // Generate main script string from its lines string PSScriptFile = string.Join("", PSScriptFileLines); // Generate a psburn specific cs file from extract.cs or noextract.cs string[] CompileCsLines; if (noextract) { CompileCsLines = Utils.EmeddedFileReadAllLines("psburn.assets.noextract.cs"); } else { CompileCsLines = Utils.EmeddedFileReadAllLines("psburn.assets.extract.cs"); } foreach (string Line in CompileCsLines) { // Updating assembly info if (Line.Contains("[assembly: AssemblyTitle")) { if (scriptname == "basename of input file") { scriptname = PsScriptBaseName; } Utils.StringArrayReplace(CompileCsLines, Line, $"[assembly: AssemblyTitle(\"{scriptname}\")]"); } else if (Line.Contains("[assembly: AssemblyProduct")) { if (scriptname == "basename of input file") { scriptname = PsScriptBaseName; } Utils.StringArrayReplace(CompileCsLines, Line, $"[assembly: AssemblyProduct(\"{scriptname}\")]"); } else if (Line.Contains("[assembly: AssemblyFileVersion")) { Utils.StringArrayReplace(CompileCsLines, Line, $"[assembly: AssemblyFileVersion(\"{scriptversion}\")]"); } // Add custom execution policy else if (Line.Contains("string ExPolicy")) { Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tstring ExPolicy = \"{executionpolicy}\";"); } // Dump powershell script in valid type of c# string in main c# file else if (Line.Contains("string PSScriptFile") && noextract) { Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tstring PSScriptFile = @\"{PSScriptFile}\";"); } // Block/Unblock script cat feature else if (Line.Contains("bool BlockCat") && blockcat) { Utils.StringArrayReplace(CompileCsLines, Line, "\t\t\tbool BlockCat = true;"); } // Manage and set default values of parameters parsed from #@param else if (Line.Contains("string[] DefaultArgumentsCode")) { string ParsedDefaultCodeString = Utils.ArrayStructureToString(ParsedDefaultCode); if (verbose) { Utils.PrintColoredText("info: ", ConsoleColor.Blue); Console.WriteLine($"DefaultArgumentsCode string array structure created e.i.\n{ParsedDefaultCodeString}\n"); } Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tstring[] DefaultArgumentsCode = {ParsedDefaultCodeString};"); } // Parse all parameters from #@param else if (Line.Contains("string[] ParsedParameters")) { string ParsedParametersString = Utils.ArrayStructureToString(ParsedParameters); if (verbose) { Utils.PrintColoredText("info: ", ConsoleColor.Blue); Console.WriteLine($"ParsedParameters string array structure created e.i.\n{ParsedParametersString}\n"); } Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tstring[] ParsedParameters = {ParsedParametersString};"); } // Auto generate help texts from #@param else if (Line.Contains("string[] HelpArgumentsTexts")) { string ParsedHelpString = Utils.ArrayStructureToString(ParsedHelp); if (verbose) { Utils.PrintColoredText("info: ", ConsoleColor.Blue); Console.WriteLine($"HelpArgumentsTexts string array structure created e.i.\n{ParsedHelpString}\n"); } Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tstring[] HelpArgumentsTexts = {ParsedHelpString};"); } // Add custom desciption to program else if (Line.Contains("string ProgramDescription")) { if (ProgramDescription == "") { continue; } Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tstring ProgramDescription = \"{ProgramDescription}\";"); } // Add custom examples to program else if (Line.Contains("string[] AllExamples")) { string ParsedExamplesString = Utils.ArrayStructureToString(ParsedExamples); if (verbose) { Utils.PrintColoredText("info: ", ConsoleColor.Blue); Console.WriteLine($"AllExamples string array structure created e.i.\n{ParsedExamplesString}\n"); } Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tstring[] AllExamples = {ParsedExamplesString};"); } // Add custom usage to program else if (Line.Contains("ProgramUsage = \"\";")) { if (ProgramUsage != "") { Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tProgramUsage = \"{ProgramUsage}\";"); } else { Utils.StringArrayReplace(CompileCsLines, Line, ""); } } // Self-Contained app type else if (Line.Contains("bool OneDir = false;") && onedir) { Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tbool OneDir = true;"); } // Embeddable zips handles else if (Line.Contains("bool UnzipEmbeddedPowershellZip = false;") && selfcontained) { Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tbool UnzipEmbeddedPowershellZip = true;"); } else if (Line.Contains("bool UnzipEmbeddedResourceZip = false;") && embedresources) { Utils.StringArrayReplace(CompileCsLines, Line, $"\t\t\tbool UnzipEmbeddedResourceZip = true;"); } } // Save generated cs file in local drive if (output == "working directory") { File.WriteAllLines(MainCsFile, CompileCsLines); } else { File.WriteAllLines(output, CompileCsLines); } }
public static void Cross(string psscript, string executionpolicy, bool blockcat, bool onedir, string output, bool verbose) { // Reading powershell script and finding base name string[] PSScriptFileLines = File.ReadAllLines(psscript); string PsScriptBaseName = Path.GetFileNameWithoutExtension(psscript); string MainPyFile = $"{PsScriptBaseName}.py"; // cli parser utilities PowershellFeatures.PowershellParser ParsePowershellScript = new PowershellFeatures.PowershellParser(PSScriptFileLines, Verbosity: verbose); string ProgramDescription = ParsePowershellScript.ParseDescription(); string ProgramUsage = ParsePowershellScript.ParseUsage(); string[] ParsedExamples = ParsePowershellScript.ParseExamples(); string[] ParsedParameters = ParsePowershellScript.ParseParameters(HelpText: true); string[] ParsedDefaultCode = ParsePowershellScript.ParseDefaultCode(ParsedParameters); // Generate a psburn specific py file from cross.py string[] CompilePyLines = Utils.EmeddedFileReadAllLines("psburn.assets.cross.py"); foreach (string Line in CompilePyLines) { // Add custom execution policy if (Line.Contains("ExPolicy = \"Bypass\" # will come")) { Utils.StringArrayReplace(CompilePyLines, Line, $"ExPolicy = \"{executionpolicy}\""); } // Add powershell script path else if (Line.Contains("PSScriptFile = \"path/to/psscript.ps1\" # will embed")) { Utils.StringArrayReplace(CompilePyLines, Line, $"PSScriptFile = \"{PsScriptBaseName}.ps1\""); } // Block/Unblock script cat feature else if (Line.Contains("BlockCat = False # will come") && blockcat) { Utils.StringArrayReplace(CompilePyLines, Line, "BlockCat = True"); } // Manage and set default values of parameters parsed from #@param else if (Line.Contains("DefaultArgumentsCode = [] # will come")) { string ParsedDefaultCodeString = Utils.ArrayStructureToString(ParsedDefaultCode, Brackets: "[,]"); if (verbose) { Utils.PrintColoredText("info: ", ConsoleColor.Blue); Console.WriteLine($"DefaultArgumentsCode string array structure created e.i.\n{ParsedDefaultCodeString}\n"); } Utils.StringArrayReplace(CompilePyLines, Line, $"DefaultArgumentsCode = {ParsedDefaultCodeString}"); } // Parse all parameters from #@param else if (Line.Contains("ParsedParameters = [] # will come")) { string ParsedParametersString = Utils.ArrayStructureToString(ParsedParameters, Brackets: "[,]"); if (verbose) { Utils.PrintColoredText("info: ", ConsoleColor.Blue); Console.WriteLine($"ParsedParameters string array structure created e.i.\n{ParsedParametersString}\n"); } Utils.StringArrayReplace(CompilePyLines, Line, $"ParsedParameters = {ParsedParametersString}"); } // Add custom desciption to program else if (Line.Contains("ProgramDescription = \"dynamically auto compiled embedded powershell command script.\" # will come")) { if (ProgramDescription == "") { continue; } Utils.StringArrayReplace(CompilePyLines, Line, $"ProgramDescription = \"{ProgramDescription}\""); } // Add custom examples to program else if (Line.Contains("AllExamples = [] # will come")) { string ParsedExamplesString = Utils.ArrayStructureToString(ParsedExamples, Brackets: "[,]"); if (verbose) { Utils.PrintColoredText("info: ", ConsoleColor.Blue); Console.WriteLine($"AllExamples string array structure created e.i.\n{ParsedExamplesString}\n"); } Utils.StringArrayReplace(CompilePyLines, Line, $"AllExamples = {ParsedExamplesString}"); } // Add custom usage to program else if (Line.Contains("ProgramUsage = \"\" # will come") && ProgramUsage != "") { Utils.StringArrayReplace(CompilePyLines, Line, $"ProgramUsage = \"{ProgramUsage}\""); } // Self-Contained app type else if (Line.Contains("OneDir = False # will come") && onedir) { Utils.StringArrayReplace(CompilePyLines, Line, $"OneDir = True"); } } // Save generated py file in local drive if (output == "working directory") { File.WriteAllLines(MainPyFile, CompilePyLines); } else { File.WriteAllLines(output, CompilePyLines); } }