...
The API is still evolving rapidly so we consider this release a beta, but we have already developed a C# version of the WPF transformation logic using this API and we have used it with C# to implement several very large multi-feature upgrade solutions. Additional documentation and samples using the API will be published soon. Contact us directly if you are interested in being part of the beta-test or discussing how this technology can help you.
Info |
---|
Preview the draft .NET API documentation here. |
A sample upgrade program using the API is shown here. Notice that it use WPF Subsystem logic that is implemented in a C# assembly and available as C# source.
...
language | c# |
---|---|
title | Sample gmslAPI Upgrade Program (beta) |
linenumbers | true |
collapse | true |
...
The example also shows how to load source and edit the source in memory prior to translation. To use this, API you need to reference gmslAPI.dll from the gmStudio distribution and also make sure you choose Platform Target x86 since the gmBasic engine is presently a 32bit technology. Contact Great Migrations if you are interested in a 64bit build.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
using System.Collections.Generic; using System.Linq; using gmslLibrary.Services; using gmslLibrary.Model; using gmslLibrary.Enums; using GM = gmslAPI; namespace gmUpgrade { public static class Program { public static class Program { [System.STAThread] // Much faster using STA, ~1.6 vs 8.6 seconds static void Main(string[] args) { const string taskArg = "TaskInfo="; const string startupArg = "startup="; [System.STAThread] // faster using STA static void Main(string[] args) { string cmlStartup = System.Array.Find(args, str => str.StartsWith(startupArg)); string startupPath = null; if (cmlStartup == null) { // default startup file path startupPath = gmslAPI.Common.DefaultStartUpFile; } else { // parse startup path from the command line, and remove it from the list of arguments int i = 0; foreach (string arg in args) { if (arg.StartsWith(startupArg)) { args[i] = ""; startupPath = arg.Split('=')[1]; } i++; } } // When started by gmStudio, TaskInfo argument is passed on the command line. // The TaskInfo XML file is generated by gmStudio. It contains details about // the upgrade task and the upgrade environment. // For debugging or direct operation, load upgrade tasks here. // Note: you can redirect the output of this to the log file for // debugging (> log) in project properties, but doing so will block // gmStudio from running the EXE running when VS is open. string cmlTask = System.Array.Find(args, str => str.StartsWith(taskArg)); List<string> taskList = new List<string>(); if (cmlTask == null) { // batch taskList.Add(taskArg + @"C:\gmSpec\Util\APITest\proj\log\APITest-APITest-exe-csh.TRAN.Xml"); } else { // parse taskarg item from the command line int i = 0; foreach (string arg in args) { if (arg.StartsWith(taskArg)) { args[i] = ""; taskList.Add(arg); } i++; } } if (taskList.Count == 0) { System.Console.Write("USAGE: " + args[0] + " " + taskArg + " TaskInfoFilePath"); System.Environment.Exit(1); } RunAllTranslations(taskList, startupPath, args); System.Environment.Exit(0); } static void RunAllTranslations(List<string> taskList, string startupPath, string[] args) { GM.Common.LoadLicense(logInfo: true); string taskInfoPath = ""; foreach (string arg in taskList) { if (arg.StartsWith(taskArg)) { taskInfoPath = arg.Split('=')[1]; if (!System.IO.File.Exists(taskInfoPath)) { System.Console.WriteLine("ERROR: TaskInfo File Not Found:" + taskInfoPath); System.Environment.Exit(999); } else { repair(taskInfoPath); GM.gmStudioTask task = GM.Common.Load(taskInfoPath); RunUpgrade(task, startupPath, args); } } } } private static void RunUpgrade(gmslAPI.gmStudioTask task, string startupPath, string[] args) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); // Go to Upgrade project working folder so relative file references are correct System.IO.Directory.SetCurrentDirectory(task.WorkFolder); System.Console.WriteLine("Loaded task:" + task.JobId); string projectStartup = @"C:\Program Files (x86)\GreatMigrations\gmStudio\support\tools\gmBasic.xml"; if (!System.IO.File.Exists(projectStartup)) projectStartup = null; gmBasic.StartUp(projectStartup, null, args); // Detailed translation rules are stored in a ScriptRules.xml // This format can be used by both XML-based and API-based upgrade tasks // The ScriptRules class makes these rules available to API calls. GM.ScriptRules rules = new GM.ScriptRules(@"..\usr\ScriptRules.xml"); rules.AddRules(task, "BasicRules"); rules.logEnable = true; log(rules.logEnable, rules.AllRulesReport()); log(rules.logEnable, "Startup(ms):" + sw.ElapsedMilliseconds); Execute.Storage(action: "Create", identifier: task.JobId); if (rules.OptionRules != 0) { Execute.gmPL(commands: rules.OptionRules); log(rules.logEnable, "Options(ms):" + sw.ElapsedMilliseconds); } bool useWPF = Select.SubSystem == Dialects.wpf; log(rules.logEnable, "GUI: " + (useWPF ? "WPF" : "WinForms")); Execute.Load(project: task.SrcPath, sourcecode: "on"); AddCommentOnErrorHandlers(); Execute.Compile(resx: task.ResxFolder, commands: rules.CompileRules); //Execute.Compile(project: task.SrcPath, resx: task.ResxFolder, commands: rules.CompileRules); log(rules.logEnable, "Compile(ms):" + sw.ElapsedMilliseconds); if (rules.PreAnalyseRules != 0) { Execute.gmPL(commands: rules.PreAnalyseRules); log(rules.logEnable, "PreAnalyse(ms):" + sw.ElapsedMilliseconds); } Execute.Analyse(); log(rules.logEnable, "Analyze(ms):" + sw.ElapsedMilliseconds); if (rules.PostAnalyseRules != 0) { Execute.gmPL(commands: rules.PostAnalyseRules); log(rules.logEnable, "PostAnalyse(ms):" + sw.ElapsedMilliseconds); } Execute.Output(status: "New", filename: task.BndPath); Execute.Author(commands: rules.AuthorRules); log(rules.logEnable, "Author(ms):" + sw.ElapsedMilliseconds); Execute.Output(status: "Close"); if (rules.PostAuthorRules != 0) { // When started by gmStudio, TaskInfo is passed on the command line. Execute.gmPL(commands: rules.PostAuthorRules); // The TaskInfo XML file is generated by gmStudio. It contains log(rules.logEnable, "PostAuthor(ms):" + sw.ElapsedMilliseconds); // details about} the upgrade task and the upgrade environment. Execute.Storage(action: "Close"); // For debugging or direct operation, load upgrade tasks heregmBasic.Terminate(); if log(args.Length == 0) rules.logEnable, "Total(ms):" + sw.ElapsedMilliseconds); { } private static void log(bool doLog, string msg) { args = new string[] { if (doLog) System.Console.WriteLine(msg); } @"TaskInfo=c:\Upgrades\proj\log\Upgrade-task1-exe-csh.xml", private static void repair(string taskInfoPath) { @"TaskInfo=c:\Upgrades\proj\log\Upgrade-task2-exe-csh.xml", string data = System.IO.File.ReadAllText(taskInfoPath); data = @"TaskInfo=c:\Upgrades\proj\log\Upgrade-task3-exe-csh.xml", data.Replace("UsrCm nt=", "UsrCmnt= "); data = @"TaskInfo=c:\Upgrades\proj\log\Upgrade-task4-exe-csh.xml" data.Replace("UsrDes c=", "UsrDesc= "); }; System.IO.File.WriteAllText(taskInfoPath, data); } private static void RunAllTranslationsAddCommentOnErrorHandlers(args); { System.Environment.Exit(0); }int[] levels = new int[19]; static void RunAllTranslations(string[] args) int iRoot; { GM.gmslAPI.LoadLicense(logInfo:true); // Must load gmStudio LicensetInfoFile formFile; Text textStream; string taskInfoPath = ""; int nRecord; foreach (string arg in args)int curRecord; { int length = 0; if (arg.StartsWith("TaskInfo=")) int rai = 0; string record; { for (iRoot = Store.FindFirstChild(levels, 0); iRoot != 0; taskInfoPathiRoot = argStore.Split('=')[1];FindNextChild(levels)) { if (!System.IO.File.Exists(taskInfoPath)) Store.GetObjectType(iRoot) != ObjectType.FormFile) continue; { string name = Symbol.FullName(iRoot, 0); System.Console.WriteLine("ERROR: TaskInfo File Not Found:FileSystem.LogMessage("name=" + taskInfoPathname); formFile = System.Environment.Exit(999new tInfoFile(iRoot); if } (formFile.textBase == 0) continue; FileSystem.LogMessage("formFile.textBase=" + formFile.textBase); else textStream = new Text(formFile.textBase); { FileSystem.LogMessage("textStream=" + textStream); Run1Translation(taskInfoPath); // sample code changes lines ending with ":" to }lines ending with ": 'TEST" } nRecord = textStream.Maximum(); } } curRecord = 0; private static void Run1Translation(string taskInfoPath) while (curRecord < {nRecord) GM.gmStudioTask task = GM.gmslAPI.Load(taskInfoPath); { bool useWPF = task.UserDesc == "WPF"; curRecord = curRecord + 1; // Go to Upgrade project working folder so relative file references are correct textStream.Position(curRecord); record = System.IO.Directory.SetCurrentDirectory(task.WorkFolder); textStream.Access(ref length, ref rai); // Detailed translation rules are stored in a ScriptRules.xml file. if (length < 2) continue; // This format can be used by both XML-based and API-based upgrade tasks. string last = Character.Substr(record, length - 1, 1); // The ScriptRules class makes the rules available to API calls. if (last // Rules may also be created on the fly using gmAPI calls.== ":") { GM.ScriptRules rules = new GM.ScriptRules(@"..\usr\ScriptRules.xml"); rules.AddRules(task, "SetOptions"); // Inserts a record after the current record and rules.AddRules(task, "PreEdits"); sets Position to new record rules.AddRules(task, "msado15.dll"); rulestextStream.AddRules(task, "scrrun.dllInsert(record + " ' TEST"); rules.AddRules(task, "excel.exe"); // reset position to rules.AddRules(task, "AssessmentRules");original record rules.AddRules(task, "GenericCollections"); textStream.Position(curRecord); rules.logEnable = true; if (rules.logEnable) FileSystem.LogMessage(rules.AllRulesReport()); // delete original record gmBasic.StartUp(@"..\usr\gmBasic.xml", null, null); Execute.Storage(action: "Create", identifier: tasktextStream.JobIdDelete(); Execute.gmPL(commands: rules.OptionRules); continue; Execute.Compile(project: task.SrcPath, commands: rules.CompileRules); } if (rules.PreAnalyseRules != 0) {} ExecutetextStream.gmPL(commands: rules.PreAnalyseRulesClose(); } } Execute.Analyse(); if (rules.PostAnalyseRules!=0) { } } |
This sample also uses a ScriptRules file to organize and specify the stock translation options:
Code Block | ||||
---|---|---|---|---|
| ||||
<ScriptRules> <!-- Here is a sample of the Basic ScriptRule elements that may be used with an gmAPI based translation --> <ScriptRule id="BasicRules"> <Option> <!-- Execute.gmPL(commands: rules.PostAnalyseRules); directories for configuration files --> <select Target="%UserFolder%" /> } <Select Local="%IdfFromCodeFolder%" /> int projectRoot<Select System= (useWPF ? wpfSubsystem.WPFCodeScan() : 0); // WPF "%IdfFromIdlFolder%" /> <!-- translation options --> Execute.Output(status: "New", filename: task.BndPath);<Select Progress="1" /> <Select DevEnv="%DevEnv%" /> <Select Dialect="%Dialect%" /> if (useWPF) wpfSubsystem.WPFAuthorProject(projectRoot, rules.AuthorRules); // WPF<Select BuildFile="local" /> <Registry type="GUID" source="Project" target="{%TaskGuid%}"/> else Execute.Author(commands: rules.AuthorRules); <!-- directories for deployment and external assemblies --> Execute.Storage(action: "Close"); <select VirtualRoot="%VirtualRoot%" /> gmBasic.Terminate(); <Select DeployLocation="%NetProjFolder%" /> <Select } Library="%RuntimeFolder%" /> } } </Option> </ScriptRule> </ScriptRules> |