[r62]: / trunk / cforge / cforge / Program.cs  Maximize  Restore  History

Download this file

200 lines (166 with data), 7.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace cforge
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.WriteLine("");
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("cforge started");
            Console.WriteLine("Build date:     " + Resources.BuildTime);
            Console.WriteLine("Revision based: " + Resources.CurrentSVNRevision);
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("");
            if (Helper.EasyAttachEnabled())
            {
                Console.WriteLine("Easy attach enabled. Now attach with debugger, and then press Enter here!");
                Console.ReadLine();
            }

            if (args.Length == 1 && args[0].StartsWith("cforge:"))
            {
                // invokation via URL Handler:
                // format is
                // "cforge:<command>:additionalargs_or_path_or_whatever
                // now we can remove the start
                // split for the first ":" and the rest is like the normal commandline                
                String arg = args[0].Remove(0, "cforge:".Length);
                List<string> newargs = new List<string>();

                String command = "--" + arg.Split(':')[0];
                newargs.Add(command);
                if (arg.Split(':').Length > 1)
                {
                    string s = System.Uri.UnescapeDataString(arg.Remove(0, arg.Split(':')[0].Length + 1));
                    s = System.Uri.UnescapeDataString(s);
                    newargs.Add(s);
                }

                if(Helper.EasyAttachEnabled())
                {
                    Console.WriteLine("converting arguments from: ");
                    foreach (String s in args)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine("to: ");
                    foreach (String s in newargs)
                    {
                        Console.WriteLine(s);
                    }
                    
                }

                args = newargs.ToArray();
            }

            if (Helper.EasyAttachEnabled())
            {
                Console.WriteLine("CWD: " + Directory.GetCurrentDirectory());
                Console.WriteLine("User elevated: " + Helper.IsUserElevated());
            }


            if (!Helper.CheckRegistryProtocol())
            {
                Console.WriteLine(@"[WARNING] URL Handler is not registered. CFORGE: links will not work properly. Use Setup.bat to do this (at <CODESYS Installation folder>\CFORGE\ )");
            }

            List<String> liScripts = Helper.GetAllScripts();
            
            bool bDebug = false;

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                    case "-d":
                        bDebug = true;
                        if (Helper.IsUserElevated())
                            Console.WriteLine("Running elevated");
                        break;

                    // fallthrough intended
                    case "-v":
                    case "--version":
                        
                        Version version = Assembly.GetEntryAssembly().GetName().Version;
                        Console.WriteLine("Version: " + version.ToString());
                        break;

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

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

                    case "--license-info":
                        Helper.ShowLicenseInfo();
                        break;

                    
                    
                    /* fallthrough intended ! */
                    case "--help":
                    case "-h":
                        ShowUsage();
                        break;

                    default:
                        if (Helper.EasyAttachEnabled())
                        {
                            Console.WriteLine("default case: " + args[i]);
                        }
                        // check if we have matching script:
                        if (args[i].StartsWith("--"))
                        {
                            String command = args[i].Replace("--", "");
                            if (liScripts.Contains(command))
                            {
                                // the rest of the command is passed as argument to the script
                                string[] newargs = args.Skip(i + 1).ToArray();
                                
                                Helper.ExecuteIPyScript(command, newargs, bDebug);
                                // as we "used" all params...
                                i = args.Length;
                            }
                            else
                            {
                                if (Helper.EasyAttachEnabled())
                                {
                                    Console.WriteLine("no matching script found: " + command);
                                }
                                ShowUsage();
                            }
                        }
                        break;
                }
            }

            if (args.Length == 0)
                ShowUsage();
            
            Console.WriteLine("");
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("cforge finished.");
            Console.WriteLine(@"----------------------------------------------------------------------------");
            Console.WriteLine("");

            Console.ReadLine();
        }

        static void ShowUsage()
        {
            // pad right 32 for description ;-)
            Console.WriteLine(@"usage: cforge <command> <arguments>");
            Console.WriteLine(@"");
            Console.WriteLine(@"internal commands:");
            Console.WriteLine(@"-h/--help                       show this help");
            Console.WriteLine(@"--setup                         register cforge url handler and add cforge to path");
            Console.WriteLine(@"--license-info                  show license information");
            Console.WriteLine(@"-v/--version                    show version information");
            Console.WriteLine(@"");
            
            Console.WriteLine(@"");
            Console.WriteLine(@"external commands (scripts):");
            Console.WriteLine(@"------------------------------");

            // generic part
            
            List<String> liCommands = Helper.GetAllScripts();
            foreach (String  command in liCommands)
            {
                Helper.ShowUsageIPyScript(command, false);
            }
            Console.WriteLine(@"");
            Console.WriteLine(@"");

        }

    }
}