1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-14 22:34:09 +02:00

Enhanced var capabilities for scripts and building

This commit is contained in:
Brian Fiete 2020-05-21 11:47:15 -07:00
parent 38551d7a1d
commit f8b27c13a2
4 changed files with 171 additions and 12 deletions

View file

@ -47,6 +47,16 @@ namespace System
}
}
public void ReleaseLastRef()
{
int refCount = Interlocked.Decrement(ref mRefCount);
Debug.Assert(refCount == 0);
if (refCount == 0)
{
delete this;
}
}
public int ReleaseRefNoDelete()
{
int refCount = Interlocked.Decrement(ref mRefCount);

View file

@ -35,6 +35,7 @@ namespace IDE
delete val;
delete _;
};
public ScriptManager.Context mScriptContext = new .() ~ _.ReleaseLastRef();
public ScriptManager mScriptManager ~ delete _;
public bool Failed
@ -152,7 +153,7 @@ namespace IDE
if (mScriptManager == null)
{
mScriptManager = new .();
mScriptManager = new .(mScriptContext);
mScriptManager.mProjectName = new String(project.mProjectName);
mScriptManager.mIsBuildScript = true;
mScriptManager.mSoftFail = true;

View file

@ -7459,6 +7459,7 @@ namespace IDE
startInfo.SetFileName(fileName);
startInfo.SetWorkingDirectory(workingDir);
startInfo.SetArguments(args);
if ((!runFlags.HasFlag(.NoRedirect)) && (!runFlags.HasFlag(.NoWait)))
{
startInfo.RedirectStandardOutput = true;
@ -7848,6 +7849,16 @@ namespace IDE
else if (next is ExecutionQueueCmd)
{
var executionQueueCmd = (ExecutionQueueCmd)next;
ReplaceVariables(executionQueueCmd.mFileName);
ReplaceVariables(executionQueueCmd.mArgs);
ReplaceVariables(executionQueueCmd.mWorkingDir);
if (executionQueueCmd.mEnvVars != null)
{
for (let kv in executionQueueCmd.mEnvVars)
ReplaceVariables(kv.value);
}
var executionInstance = DoRun(executionQueueCmd.mFileName, executionQueueCmd.mArgs, executionQueueCmd.mWorkingDir, executionQueueCmd.mUseArgsFile, executionQueueCmd.mEnvVars, executionQueueCmd.mStdInData, executionQueueCmd.mRunFlags);
if (executionInstance != null)
{
@ -8724,6 +8735,8 @@ namespace IDE
}
else
cmdErr = "Invalid number of arguments";
case "Var":
break ReplaceBlock;
}
if (newString == null)
@ -8962,6 +8975,98 @@ namespace IDE
return !hadError;
}
public void ReplaceVariables(String result)
{
int i = 0;
for ( ; i < result.Length - 2; i++)
{
if ((result[i] == '$') && (result[i + 1] == '('))
{
int parenPos = -1;
int openCount = 1;
bool inString = false;
char8 prevC = 0;
for (int checkIdx = i + 2; checkIdx < result.Length; checkIdx++)
{
char8 c = result[checkIdx];
if (inString)
{
if (prevC == '\\')
{
// Slashed char
prevC = 0;
continue;
}
if (c == '"')
inString = false;
}
else
{
if (c == '"')
inString = true;
else if (c == '(')
openCount++;
else if (c == ')')
{
openCount--;
if (openCount == 0)
{
parenPos = checkIdx;
break;
}
}
}
prevC = c;
}
if (parenPos != -1)
ReplaceBlock:
do
{
String replaceStr = scope String(result, i + 2, parenPos - i - 2);
if (!replaceStr.StartsWith("Var "))
continue;
String varName = scope String(replaceStr, 4);
String newString = null;
if (mScriptManager.mContext.mVars.TryGetValue(varName, var value))
{
if (value.VariantType == typeof(String))
{
newString = scope:ReplaceBlock String(value.Get<String>());
}
}
if (newString == null)
{
if (mBuildContext != null)
{
if (mBuildContext.mScriptContext.mVars.TryGetValue(varName, var value))
{
if (value.VariantType == typeof(String))
{
newString = scope:ReplaceBlock String(value.Get<String>());
}
}
}
}
if (newString != null)
{
result.Remove(i, parenPos - i + 1);
result.Insert(i, newString);
i--;
}
}
}
}
}
public bool ResolveConfigString(String platformName, Workspace.Options workspaceOptions, Project project, Project.Options options, StringView configString, String errorContext, String outResult)
{
String errorString = scope String();

View file

@ -17,6 +17,19 @@ namespace IDE
{
public static ScriptManager sActiveManager;
public class Context : RefCounted
{
public Dictionary<String, Variant> mVars = new .() ~
{
for (var kv in _)
{
delete kv.key;
kv.value.Dispose();
}
delete _;
};
}
class Target
{
public class Cmd
@ -54,15 +67,8 @@ namespace IDE
ScriptHelper mScriptHelper = new ScriptHelper(this) ~ delete _;
Target mRoot = new Target() ~ delete _;
Dictionary<String, Variant> mVars = new .() ~
{
for (var kv in _)
{
delete kv.key;
kv.value.Dispose();
}
delete _;
};
public Context mContext ~ _.ReleaseRef();
List<QueuedCmd> mCmdList = new .() ~ DeleteContainerAndItems!(_);
public bool mFailed;
public bool mCancelled;
@ -93,8 +99,16 @@ namespace IDE
}
}
public this()
public this(Context context = null)
{
if (context != null)
{
context.AddRef();
mContext = context;
}
else
mContext = new Context();
AddTarget(mScriptHelper);
//Exec("OutputLine(\"Hey bro!\", 2)");
@ -520,7 +534,7 @@ namespace IDE
{
String* keyPtr;
Variant* valuePtr;
if (mVars.TryAdd(scope String(varName), out keyPtr, out valuePtr))
if (mContext.mVars.TryAdd(scope String(varName), out keyPtr, out valuePtr))
*keyPtr = new String(varName);
else
valuePtr.Dispose();
@ -2451,5 +2465,34 @@ namespace IDE
Windows.MessageBoxA((Windows.HWnd)0, "Waiting for user input", "Beef IDE", 0);
#endif
}
[IDECommand]
public void SetVal(String valName, String value)
{
bool added = mScriptManager.mContext.mVars.TryAdd(valName, var keyPtr, var valuePtr);
if (added)
*keyPtr = new String(valName);
else
valuePtr.Dispose();
*valuePtr = Variant.Create<String>(new String(value), true);
}
[IDECommand]
public void ReadFile(String filePath, String valName)
{
String value = scope .();
if (File.ReadAllText(filePath, value) case .Err)
{
mScriptManager.Fail(scope String()..AppendF("Failed to read file '{}'", filePath));
return;
}
bool added = mScriptManager.mContext.mVars.TryAdd(valName, var keyPtr, var valuePtr);
if (added)
*keyPtr = new String(valName);
else
valuePtr.Dispose();
*valuePtr = Variant.Create<String>(new String(value), true);
}
}
}