Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zs-3 authored Aug 1, 2024
1 parent 9e0f352 commit d455bb2
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 47 deletions.
39 changes: 39 additions & 0 deletions ZS/Argument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.SmallBasic.Library;
using System;

/// <summary>
/// The Arguments object provides operations to access the command-line arguments that were passed at the start of this program.
/// This class has been taken from Small Basic version 0.2.
/// </summary>
[SmallBasicType]
public static class ZSArguments
{
private static string[] args = Environment.GetCommandLineArgs();

/// <summary>
/// Gets the number of command-line arguments passed to this program.
/// </summary>
public static Primitive Count
{
get { return (args.Length != 0) ? (args.Length - 1) : 0; }
}

/// <summary>
/// Returns the specified argument.
/// </summary>
/// <param name="index">
/// Index of the argument.
/// </param>
/// <returns>
/// The command-line argument at the specified index.
/// </returns>
public static Primitive GetArgument(Primitive index)
{
int num = index;
if (num >= 1 && num < args.Length)
{
return new Primitive(args[num]);
}
return new Primitive("");
}
}
116 changes: 116 additions & 0 deletions ZS/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Microsoft.SmallBasic.Library;
using System;
using System.Collections.Generic;
using System.Reflection;

/// <summary>
/// The Platform object provides a way to generically invoke other .NET libraries.
/// This class has been taken from Small Basic version 0.2.
/// </summary>
[SmallBasicType]
public static class ZSPlatform
{
private static Dictionary<string, int> _nameGenerationMap = new Dictionary<string, int>();

private static Dictionary<string, object> _objectMap = new Dictionary<string, object>();

/// <summary>
/// Creates an instance of a specified .NET type.
/// For Example : System.DateTime
/// </summary>
/// <param name="typeName">The fully qualified name of the type.</param>
/// <returns>A unique identifier for the created instance or "ERROR" if creation fails.</returns>
public static Primitive CreateInstance(Primitive typeName)
{
try
{
Type type = Type.GetType(typeName);
object value = Activator.CreateInstance(type);
string text = GenerateNewName("Instance");
_objectMap[text] = value;
return text;
}
catch (Exception ex)
{
return "ERROR : " + ex.Message;
}
}

/// <summary>
/// Invokes a method on an instance of a .NET type.
/// </summary>
/// <param name="instanceId">The unique identifier of the instance.</param>
/// <param name="methodName">The name of the method to invoke.</param>
/// <param name="argumentsStackName">The name of the stack containing the method arguments.</param>
/// <returns>The result of the method invocation or "ERROR" if invocation fails.</returns>
public static Primitive InvokeInstanceMethod(Primitive instanceId, Primitive methodName, Primitive argumentsStackName)
{
try
{
object obj = _objectMap[instanceId];
MethodInfo method = obj.GetType().GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
int num = Stack.GetCount(argumentsStackName);
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length == num)
{
List<object> list = new List<object>();
for (int num2 = num - 1; num2 >= 0; num2--)
{
list[num2] = Stack.PopValue(argumentsStackName);
}
return new Primitive(method.Invoke(obj, list.ToArray()));
}
return "ERROR";
}
catch (Exception ex)
{
return "ERROR : " + ex.Message;
}
}

/// <summary>
/// Invokes a static method on a .NET type.
/// </summary>
/// <param name="typeName">The fully qualified name of the type.</param>
/// <param name="methodName">The name of the method to invoke.</param>
/// <param name="argumentsStackName">The name of the stack containing the method arguments.</param>
/// <returns>The result of the method invocation or "ERROR" if invocation fails.</returns>
public static Primitive InvokeStaticMethod(Primitive typeName, Primitive methodName, Primitive argumentsStackName)
{
try
{
Type type = Type.GetType(typeName);
MethodInfo method = type.GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public);
int num = Stack.GetCount(argumentsStackName);
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length == num)
{
List<object> list = new List<object>();
for (int num2 = num - 1; num2 >= 0; num2--)
{
list[num2] = Stack.PopValue(argumentsStackName);
}
return new Primitive(method.Invoke(null, list.ToArray()));
}
return "ERROR";
}
catch (Exception ex)
{
return "ERROR : " + ex.Message;
}
}

/// <summary>
/// Generates a new unique name with a specified prefix.
/// </summary>
/// <param name="prefix">The prefix for the generated name.</param>
/// <returns>A new unique name.</returns>
internal static string GenerateNewName(string prefix)
{
int num = 0;
_nameGenerationMap.TryGetValue(prefix, out num);
num++;
_nameGenerationMap[prefix] = num;
return prefix + num.ToString();
}
}
158 changes: 117 additions & 41 deletions ZS/PowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,124 @@

namespace ZS
{
/// <summary>
/// Provides methods for running PowerShell commands.
/// </summary>
[SmallBasicType]
public class ZSPowerShell
{
/// <summary>
/// Executes a PowerShell command and returns the result.
/// </summary>
/// <param name="command">The PowerShell command to execute.</param>
/// <returns>A string containing the output and errors from the PowerShell command.</returns>
/// <remarks>
/// This method starts a new PowerShell process to execute the specified command.
/// Output and errors are combined into a single string.
/// </remarks>
public static Primitive Run(Primitive command)
{
try
{
using (Process process = new Process())
{
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = "-NoProfile -ExecutionPolicy Bypass -Command \"" + command + "\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
/// <summary>
/// Provides methods for running PowerShell commands.
/// </summary>
[SmallBasicType]
public class ZSPowerShell
{
/// <summary>
/// Executes a PowerShell command and returns the result.
/// </summary>
/// <param name="command">The PowerShell command to execute.</param>
/// <returns>A string containing the output and errors from the PowerShell command.</returns>
/// <remarks>
/// This method starts a new PowerShell process to execute the specified command.
/// Output and errors are combined into a single string.
/// </remarks>
public static Primitive Run(Primitive command)
{
try {
using (Process process = new Process()) {
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = "-NoProfile -ExecutionPolicy Bypass -Command \"" + command + "\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

process.WaitForExit();
process.WaitForExit();

// Combine output and error into a single result
return "Output:\n" + output + "\nError:\n" + error;
}
}
catch (Exception ex)
{
return "Exception:\n" + ex.Message;
}
}
}
// Combine output and error into a single result
return "Output:\n" + output + "\nError:\n" + error;
}
} catch (Exception ex) {
return "Exception:\n" + ex.Message;
}
}


/// <summary>
/// Executes a PowerShell script provided as an array of strings.
/// </summary>
/// <param name="scriptLines">Array of strings, each representing a line of the PowerShell script.</param>
/// <returns>A string containing the output and errors from the PowerShell script execution.</returns>
public static Primitive RunPowerShellScriptFromArray(Primitive[] scriptLines)
{
try {
// Combine script lines into a single script
string script = string.Empty;
foreach (Primitive line in scriptLines) {
script += line.ToString() + Environment.NewLine;
}

using (Process process = new Process()) {
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = "-NoProfile -ExecutionPolicy Bypass -Command \"" + script + "\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

process.WaitForExit();

return "Output:\n" + output + "\nError:\n" + error;
}
} catch (Exception ex) {
return "Exception:\n" + ex.Message;
}
}

/// <summary>
/// Executes a PowerShell script file and returns the output.
/// </summary>
/// <param name="filePath">The path to the PowerShell script file.</param>
/// <returns>A string containing the output and errors from the PowerShell script execution.</returns>
public static Primitive RunPowerShellFile(Primitive filePath)
{
try {
// Validate the file path
if (string.IsNullOrWhiteSpace(filePath)) {
return "Error: File path is null or empty.";
}

if (!System.IO.File.Exists(filePath)) {
return "Error: File not found at path " + filePath + ".";
}

using (Process process = new Process()) {
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = string.Format("-NoProfile -ExecutionPolicy Bypass -File \"{0}\"", filePath);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

process.WaitForExit();

return "Output:\n" + output + "\nError:\n" + error;
}
} catch (Exception ex) {
return "Exception:\n" + ex.Message;
}
}





}

}
Loading

0 comments on commit d455bb2

Please sign in to comment.