HowTo: use scripts in scripts

Summary
When you have a larger project or you want to make something like a ‘code library’ this guide offers a solution.

Robin works with just one file. For larger projects this file gets longer and longer and normally it is very soon that you do not find any more the details you are looking for.
So, why not split the large file in sub-files and have ‘something’ that glues all the required files at the end to the one large file?

Example
We have four files (could be any number not just four :grinning:):
Definition.robin


… contains all global variables and the import statement

Utility.robin
uTILITY
… contains functions that e.g. check if input files are available

UtilityWeb.robin
UtilityWeb
… contain functions that e.g. wait for web elements to be available

Main.robin
Main
… the starting point that has the program logic in it. For that it has to call the functions and uses the variables from the other robin files

Now the console application is run that glues everything together:
RobinMerge.exe Main.robin Final.robin

The key word for the console application is
include “filename”

You can put it everywhere in your script. Even includes inside an included file is supported (see Utility.robin)

and you get the complete Final.robin file

Here is the source code for the console application:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RobinMerge
{
class Program
{
static List Includes = new List();
static string OutFilename = string.Empty;

    static void Main(string[] args)
    {
        if (args.Count() != 2)
        {
            Console.WriteLine("RobinMerge <main.robin> <output.robin>");
        }
        else
        {
            OutFilename = args[1];
            using (StreamWriter w = new StreamWriter(OutFilename))
            {
                ProcessFile(args[0], w);
            }
        }
    }

    private static void ProcessFile(string filename, StreamWriter w)
    {
        string line;
        using (var reader = new System.IO.StreamReader(filename))
        {
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Trim().StartsWith("include"))
                {
                    string[] splitParts = line.Split('"');
                    if (splitParts.Count() == 3)
                    {   // valid include found
                        if (!Includes.Contains(splitParts[1]))
                        {
                            Includes.Add(splitParts[1]);
                            ProcessFile(splitParts[1], w);
                        }
                    }
                }
                else
                {   // normal line -> add to output
                    w.WriteLine(line);
                }
            }
        }
    }
}

}

Happy coding :grinning:

3 Likes

@Code4FunVienna, that’s an excellent summary and update. Thank you!
Best regards,
burque505

2 Likes

In case anybody running a recent version of VS has problems compiling @Code4FunVienna’s code above, you could try this and see if works for you (working here with VS 2019 CE, latest updates, compiled against .NET 4.61):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace RobinMerge
{
	class Program
	{
		static List<string> Includes = new List<string>();
		static string OutFilename = string.Empty;

		static void Main(string[] args)
		{
			if (args.Count() != 2)
			{
				Console.WriteLine("RobinMerge <main.robin> <output.robin>");
			}
			else
			{
				OutFilename = args[1];
				using (StreamWriter w = new StreamWriter(OutFilename))
				{
					ProcessFile(args[0], w);
				}
			}
		}

		private static void ProcessFile(string filename, StreamWriter w)
		{
			string line;
			using (var reader = new System.IO.StreamReader(filename))
			{
				while ((line = reader.ReadLine()) != null)
				{
					if (line.Trim().StartsWith("include"))
					{
						string[] splitParts = line.Split('"');
						if (splitParts.Count() == 3)
						{   // valid include found
							if (!Includes.Contains(splitParts[1]))
							{
								Includes.Add(splitParts[1]);
								ProcessFile(splitParts[1], w);
							}
						}
					}
					else
					{   // normal line -> add to output
						w.WriteLine(line);
					}
				}
			}
		}
	}
}

Best regards,
burque505

1 Like

Some people may not have a compiler installed, or the ability to install one at work. While that might mean you can’t install AutoHotkey either, it’s more likely that you can.

If you find yourself in this situation, you might take a look at this gist.

Using AutoHotkey and its CLR library, I compiled a version of @Code4FunVienna’s code above, with changes I needed to make it work with Autohotkey.

Please let me know if it crashes and burns - it’s working fine for me so far.

Usage is just as @Code4FunVienna describes above.

(Hint: If all else fails, there’s an executable of RobinMerge.exe in RobinMerge.zip at the gist page) :slight_smile:

Best regards,
burque505

Thanks a lot for the executables @burque505

1 Like

It was my pleasure, @Code4FunVienna! Thank you for the code!
Best regards,
burque505