gmslAPI: a .NET API for Developing Advanced Upgrade Solutions

April 2018.  Great Migrations is pleased to announce a major break through in software re-engineering: a .NET API for automating the gmBasic upgrade engine and accessing the semantic models produced by our unique linguistic compilation technology.

Our goal for this API is to provide a more powerful development platform for building advanced upgrade solutions. The API integrates the features of the Great Migrations Programming Language (gmPL) with Great Migrations Scripting Language (gmSL). The XML-based gmPL has been used for upgrade scripts as well as meta-language and COM interface description files. The C-like gmSL API has been used extensively for dynamic code templates, deep code analysis, and advanced transformations. gmPL and gmSL provide access to an extensive collection of gmBasic commands, events, and operations, but they lack key features of modern development platforms such as interactive debugging and intellisense. The new .NET-based gmslAPI addresses these limitations and provides greater access to the full power of the gmBasic translation engine. Of course, developers building upgrade solutions using gmslAPI can also use other .NET language and framework features allowing more sophisticated upgrade solutions integrating a wider array of information.

The API includes special purposes classes to help you integrate API-based EXE upgrade tasks with gmStudio and customize their behavior.  EXE upgrades tasks can be used in gmStudio using procedures very similar to those for gmPL script tasks. The main difference is the EXE tasks are implemented in C# (or VB.NET) using Visual Studio while gmPL script tasks are implemented in XML using your favorite code editor.

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. 

Preview the draft .NET API documentation here.

A sample upgrade program using the API is shown here.  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.

Sample gmslAPI Upgrade Program (beta)
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
    {
        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)
            {
                Execute.gmPL(commands: rules.PostAuthorRules);
                log(rules.logEnable, "PostAuthor(ms):" + sw.ElapsedMilliseconds);
            }

            Execute.Storage(action: "Close");
            gmBasic.Terminate();
            log(rules.logEnable, "Total(ms):" + sw.ElapsedMilliseconds);
        }
        private static void log(bool doLog, string msg)
        {
            if (doLog) System.Console.WriteLine(msg);
        }
        private static void repair(string taskInfoPath)
        {
            string data = System.IO.File.ReadAllText(taskInfoPath);
            data = data.Replace("UsrCm nt=", "UsrCmnt= ");
            data = data.Replace("UsrDes c=", "UsrDesc= ");
            System.IO.File.WriteAllText(taskInfoPath, data);
        }

        private static void AddCommentOnErrorHandlers()
        {
            int[] levels = new int[19];
            int iRoot;
            tInfoFile formFile;
            Text textStream;
            int nRecord;
            int curRecord;
            int length = 0;
            int rai = 0;
            string record;

            for (iRoot = Store.FindFirstChild(levels, 0); iRoot != 0; iRoot = Store.FindNextChild(levels))
            {
                if (Store.GetObjectType(iRoot) != ObjectType.FormFile) continue;
                string name = Symbol.FullName(iRoot, 0);
                FileSystem.LogMessage("name=" + name);

                formFile = new tInfoFile(iRoot);
                if (formFile.textBase == 0) continue;
                FileSystem.LogMessage("formFile.textBase=" + formFile.textBase);

                textStream = new Text(formFile.textBase);
                FileSystem.LogMessage("textStream=" + textStream);

                // sample code changes lines ending with ":" to lines ending with ": 'TEST"
                nRecord = textStream.Maximum();
                curRecord = 0;
                while (curRecord < nRecord)
                {
                    curRecord = curRecord + 1;
                    textStream.Position(curRecord);
                    record = textStream.Access(ref length, ref rai);

                    if (length < 2) continue;
                    string last = Character.Substr(record, length - 1, 1);
                    if (last == ":")
                    {
                        // Inserts a record after the current record and sets Position to new record
                        textStream.Insert(record + " ' TEST");
                        // reset position to original record
                        textStream.Position(curRecord);
                        // delete original record
                        textStream.Delete();
                        continue;
                    }
                }
                textStream.Close();
            }
        }
    }
}

This sample also uses a ScriptRules file to organize and specify the stock translation options:

<ScriptRules>
<!-- Here is a sample of the Basic ScriptRule elements that may be used with an gmAPI based translation -->
<ScriptRule id="BasicRules">
   <Option>
      <!-- directories for configuration files -->

      <select Target="%UserFolder%" />
      <Select Local="%IdfFromCodeFolder%" />
      <Select System="%IdfFromIdlFolder%" />

      <!-- translation options -->

      <Select Progress="1" />
      <Select DevEnv="%DevEnv%" />
      <Select Dialect="%Dialect%" />
      <Select BuildFile="local" />
      <Registry type="GUID" source="Project" target="{%TaskGuid%}"/>   

      <!-- directories for deployment and external assemblies -->

      <select VirtualRoot="%VirtualRoot%" />
      <Select DeployLocation="%NetProjFolder%" />
      <Select Library="%RuntimeFolder%" />

   </Option>
</ScriptRule>
</ScriptRules>