Example #1
0
        /*
         * This function is called through Edge.JS by lb-xm.js.
         * A Json definitions is passed and a DLL or CS code for the SDK is created as a result.
         */
        public async Task <object> Invoke(object input)
        {
            // Process flags
            var inputs = ((Object[])input).Where(p => p != null).Select(p => p.ToString()).ToArray();
            var flags  = new HashSet <string>(inputs.Skip(2));

            // Get json definition of the server
            string jsonModel = inputs[0];

            if (flags.Contains("debug"))
            {
                WriteDefinitionsDebug(jsonModel);
            }

            // Create new templates and pass the definition Json to DynamicModels and DynamicRepos
            var dynamicModelsTemplate = new DynamicModels();
            var dynamicReposTemplate  = new DynamicRepos();
            var constantCode          = new HardcodedModels();

            dynamicModelsTemplate.Session = new Dictionary <string, object>();
            dynamicModelsTemplate.Session["jsonModel"] = jsonModel;
            dynamicReposTemplate.Session = new Dictionary <string, object>();
            dynamicReposTemplate.Session["jsonModel"] = jsonModel;
            constantCode.Session = new Dictionary <string, object>();
            constantCode.Session["jsonModel"] = jsonModel;

            if (flags.Contains("forms"))
            {
                Console.WriteLine(">> Parsing for Xamarin-Forms compatibility...");
                constantCode.Session["XamarinForms"] = true;
            }
            else
            {
                constantCode.Session["XamarinForms"] = false;
            }

            if (flags.Contains("force"))
            {
                Console.WriteLine(">> Forcing SDK creation...");
                dynamicReposTemplate.Session["force"] = true;
            }
            else
            {
                dynamicReposTemplate.Session["force"] = false;
            }

            // Create dynamic code from templates
            dynamicModelsTemplate.Initialize();
            dynamicReposTemplate.Initialize();
            constantCode.Initialize();

            string code = constantCode.TransformText() + dynamicReposTemplate.TransformText() +
                          dynamicModelsTemplate.TransformText();

            if (!flags.Contains("force") && !handleUnsupported(code))
            {
                return(false);
            }

            Directory.CreateDirectory(outputFolder);
            string currentPath = inputs[1];

            if (flags.Contains("dll"))
            {
                Console.WriteLine(">> Compiling...");
                return(await Compile(code, outputFolder + "/LBXamarinSDK.dll", currentPath));
            }
            else
            {
                if (flags.Contains("check"))
                {
                    if (await Compile(code, null, currentPath))
                    {
                        Console.WriteLine(">> Check: Successful.");
                    }
                    else
                    {
                        Console.WriteLine(">> Check: Failed.");
                        return(false);
                    }
                }
                Console.WriteLine(">> Writing CS file: " + outputFolder + "/LBXamarinSDK.cs...");
                System.IO.StreamWriter file = new System.IO.StreamWriter(outputFolder + "/LBXamarinSDK.cs");
                file.Write(code);
                file.Close();
                return(true);
            }
        }
        /*
         * This function is called through Edge.JS by lb-xm.js. 
         * A Json definitions is passed and a DLL or CS code for the SDK is created as a result.
         */
        public async Task<object> Invoke(object input)
        {
            // Process flags
            var inputs = ((Object[])input).Where(p => p != null).Select(p => p.ToString()).ToArray();
            var flags = new HashSet<string>(inputs.Skip(2));

            // Get json definition of the server
            string jsonModel = inputs[0];

            if (flags.Contains("debug"))
            {
                WriteDefinitionsDebug(jsonModel);
            }

            // Create new templates and pass the definition Json to DynamicModels and DynamicRepos
            var dynamicModelsTemplate = new DynamicModels();
            var dynamicReposTemplate = new DynamicRepos();
            var constantCode = new HardcodedModels();

            dynamicModelsTemplate.Session = new Dictionary<string, object>();
            dynamicModelsTemplate.Session["jsonModel"] = jsonModel;
            dynamicReposTemplate.Session = new Dictionary<string, object>();
            dynamicReposTemplate.Session["jsonModel"] = jsonModel;
            constantCode.Session = new Dictionary<string, object>();
            constantCode.Session["jsonModel"] = jsonModel;
            
            if(flags.Contains("forms"))
            {
                Console.WriteLine(">> Parsing for Xamarin-Forms compatibility...");
                constantCode.Session["XamarinForms"] = true;
            }
            else
            {
                constantCode.Session["XamarinForms"] = false;
            }

            if (flags.Contains("force"))
            {
                Console.WriteLine(">> Forcing SDK creation...");
                dynamicReposTemplate.Session["force"] = true;
            }
            else
            {
                dynamicReposTemplate.Session["force"] = false;
            }

            // Create dynamic code from templates
            dynamicModelsTemplate.Initialize();
            dynamicReposTemplate.Initialize();
            constantCode.Initialize();

            string code = constantCode.TransformText() + dynamicReposTemplate.TransformText() +
                          dynamicModelsTemplate.TransformText();

            if(!flags.Contains("force") && !handleUnsupported(code))
            {
                return false;
            }

            Directory.CreateDirectory(outputFolder);
            string currentPath = inputs[1];
            if (flags.Contains("dll"))
            {
                Console.WriteLine(">> Compiling...");
                return await Compile(code, outputFolder + "/LBXamarinSDK.dll", currentPath);
            }
            else
            {
                if (flags.Contains("check"))
                {
                    if (await Compile(code, null, currentPath))
                    {
                        Console.WriteLine(">> Check: Successful.");
                    }
                    else 
                    {
                        Console.WriteLine(">> Check: Failed.");
                        return false;
                    }
                } 
                Console.WriteLine(">> Writing CS file: " + outputFolder + "/LBXamarinSDK.cs...");
                System.IO.StreamWriter file = new System.IO.StreamWriter(outputFolder + "/LBXamarinSDK.cs");
                file.Write(code);
                file.Close();
                return true;
            }
        }