#9 Draft for better bootstrapping and checking prerequisits

1.0
open
nobody
2019-12-03
2019-01-05
aliazzz
No

I extended cForge helper class with a bootstrapper to detect SVN installation and to add the LibDoc.exe to the PATH environmentvariables. Offcourse its written in similair style to the excisting code.

Here are my code snippets;

In Program.cs edit case setup to call the bootstrapper;

case "--setup":
                        if (!Helper.IsUserElevated())
                        {
                            Console.WriteLine("[INFO] Setup URL Handler and add tool to path. ");
                            Helper.RunElevated("--setup");

                        }
                        else
                        {
                            /// bootstrapper 
                            Helper.bootstrap(bDebug);
                            Helper.RegisterProtocol(bDebug);
                            Helper.AddToSystemPath(bDebug);
                        }
                        break;

And extend SystemPath region in Helper.cs with Bootstrapper;

        #region SystemPath

        public static void AddToSystemPath(bool bVerbose)
        {
            string strPath = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
            string[] split = strPath.Split(new Char[] { ';' });
            string path = Path.GetDirectoryName(GetCFORGEAssemblyPath());
            string strNewSystemPath = strPath + ";" + path;

            if (!split.Contains(path))
            {
                System.Environment.SetEnvironmentVariable("Path", strNewSystemPath, EnvironmentVariableTarget.Machine);
            }

        }

        public static void bootstrap(bool bVerbose)
        {
            /// bootstrapper 
            /// warning for svn.exe if not present
            string exepath = Helper.FindExePath("svn.exe");
            if (exepath == String.Empty)
            {
                Console.WriteLine("[WARNING] Please install Turtoise SVN with command line client option!");
            }
            else
            {
                Console.WriteLine("[INFO] Turtoise SVN detected. \r\n\tPath is: " + exepath);
                exepath = String.Empty;
            }

            /// add libdoc.exe to path to enable CODESYS libdocscripting (TODO expand this section)
            exepath = Helper.FindExePath("libdoc.exe");
            string strPath = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
            string path = String.Empty;
            string strNewSystemPath = String.Empty;
            if (exepath == String.Empty)
            {
                /// add path to: "C:\Program Files\CODESYS 3.5.14.0\CODESYS\DocScripting\3.5.14.0"
                /// for now quick and dirty => should become dynamic 
                path = "C:\\Program Files\\CODESYS 3.5.14.0\\CODESYS\\DocScripting\\3.5.14.0";
                strNewSystemPath = strPath + ";" + path;
                System.Environment.SetEnvironmentVariable("Path", strNewSystemPath, EnvironmentVariableTarget.Machine);
                Console.WriteLine("[INFO] Environmentvariable for DocScripting added:  \r\n\tPath is: " + path);
            }
            else
            {
                Console.WriteLine("[INFO] DocScripting detected. \r\n\tPath is: " + exepath);
                exepath = String.Empty;
            }

        }

            /// <summary>
            /// Expands environment variables and, if unqualified, locates the exe in the working directory
            /// or the evironment's path.
            /// </summary>
            /// <param name="exe">The name of the executable file</param>
            /// <returns>The fully-qualified path to the file</returns>
            /// <exception cref="System.IO.FileNotFoundException">Raised when the exe was not found</exception>
            public static string FindExePath(string exe)
        {
            exe = Environment.ExpandEnvironmentVariables(exe);
            if (!File.Exists(exe))
            {
                if (Path.GetDirectoryName(exe) == String.Empty)
                {
                    foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(';'))
                    {
                        string path = test.Trim();
                        if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, exe)))
                            return Path.GetFullPath(path);
                    }
                }
                return String.Empty;
                /// throw new FileNotFoundException(new FileNotFoundException().Message, exe);
            }
            return Path.GetFullPath(exe);
        }

        #endregion

Discussion

  • Ingo

    Ingo - 2019-01-13
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -3,7 +3,8 @@
     Here are my code snippets;
    
     In Program.cs edit case setup to call the bootstrapper;
    -~~~ C#
    +
    +~~~
     case "--setup":
                             if (!Helper.IsUserElevated())
                             {
    
     
  • Ingo

    Ingo - 2019-01-13
    • summary: add --> Draft for better bootstrapping and checking prerequisits
     
  • Ingo

    Ingo - 2019-01-13

    The idea is good. But two things:
    1) I would not mix bootstrapping (or maybe better "prerequisit check") with the libdoc exe detection
    2) FindExePath tries to expand the exe name with environment variables. That makes IMHO not much sense. Maybe this function needs to be reworked before it can be integrated

    But another idea:
    svn.exe is needed only necessary for a few python scripts. But on the other hand, there might be some more prerequisits to check. What's if we don't check it in C# at time of install, but we create a script, named "prerequisits.py", which contains some checks, which can be invoked by all of the other python scripts just on demand.

    This will be easier to implement and extend. No need to recompile anything. And in some automated environments, it might make sense to use only some of the commands, where not all of the prerequisits are necessary. So for example, when I only want to operate a jenkins slave, which is exporting documentation, I migth not need SVN.

     
    • aliazzz

      aliazzz - 2019-01-13

      1) okay, but I only hope that the documentation generation toolchain, at least some commands of it, can be integrated into the cForge tool via some python scripts as this is relatively simple to achieve.
      2) findexepath was merely intended as a proof of concept and is by no means a fully finished function. However your idea about "prerequisits.py" is more flexible/extensible then an integrated bootstrapper as you suggested.
      3) I now see where your cForge tool will be headed towards. It is a good idea to add a small development-roadmap to this project in order to streamline the idea's towards the intent. Otherwise you might end up with a "swiss army knife" application without a clear goal or purpose. This unless the "swiss army knife" tool is your goal ;-)

       
  • Ingo

    Ingo - 2019-01-13

    Honestly?
    The swiss army knife is my first intend ;)

    But you are right. There are new ideas around for a better integration. After a proof of concept, and a decision, you are right, we can illustrate the goal or roadmap.

    For now:
    My current Vision is a similar usability as Crossover office is giving the users. You have a set of tools, which can be used by their own. And then you have a frontend, which gives you easy access to the tools for your personal projects.

     

Log in to post a comment.