Diff of /trunk/cforge/cforge/Program.cs [r6] .. [r7]  Maximize  Restore

Switch to unified view

a/trunk/cforge/cforge/Program.cs b/trunk/cforge/cforge/Program.cs
1
using System;
1
using System;
2
using System.Collections.Generic;
2
using System.Collections.Generic;
3
using System.Linq;
3
using System.Linq;
4
using System.Text;
4
using System.Reflection;
5
using System.Threading.Tasks;
5
using System.Text;
6
6
using System.Threading.Tasks;
7
namespace cforge
7
8
{
8
namespace cforge
9
    class Program
9
{
10
    {
10
    class Program
11
        static void Main(string[] args)
11
    {
12
        {
12
        static void Main(string[] args)
13
            Console.WriteLine("cforge\r\n");
13
        {
14
14
            Console.WriteLine("");
15
            if (args.Length == 1 && args[0].StartsWith("cforge:"))
15
            Console.WriteLine(@"----------------------------------------------------------------------------");
16
            {
16
            Console.WriteLine("cforge started");
17
                // invokation via URL Handler:
17
            Console.WriteLine(@"----------------------------------------------------------------------------");
18
                // format is
18
            Console.WriteLine("");
19
                // "cforge:<command>:additionalargs_or_path_or_whatever
19
20
                // now we can remove the start
20
21
                // split for the first ":" and the rest is like the normal commandline                
21
            if (args.Length == 1 && args[0].StartsWith("cforge:"))
22
                String arg = args[0].Remove(0, "cforge:".Length);
22
            {
23
                List<string> newargs = new List<string>();
23
                // invokation via URL Handler:
24
24
                // format is
25
                String command = arg.Split(':')[0];
25
                // "cforge:<command>:additionalargs_or_path_or_whatever
26
                newargs.Add(command);
26
                // now we can remove the start
27
                string s = System.Uri.UnescapeDataString(arg.Remove(0, command.Length + 1));
27
                // split for the first ":" and the rest is like the normal commandline                
28
                s = System.Uri.UnescapeDataString(s);
28
                String arg = args[0].Remove(0, "cforge:".Length);
29
                newargs.Add(s);
29
                List<string> newargs = new List<string>();
30
                
30
31
                args = newargs.ToArray();
31
                String command = arg.Split(':')[0];
32
            }
32
                newargs.Add(command);
33
33
                string s = System.Uri.UnescapeDataString(arg.Remove(0, command.Length + 1));
34
            if(!Helper.CheckRegistryProtocol())
34
                s = System.Uri.UnescapeDataString(s);
35
            {
35
                newargs.Add(s);
36
                Console.WriteLine(@"[WARNING] URL Handler is not registered. CFORGE: links will not work properly. Use RegisterURLHandler.bat to do this (at <CODESYS Installation folder>\CFORGE\ )");
36
37
            }
37
                args = newargs.ToArray();
38
38
            }
39
            List<String> liScripts = Helper.GetAllScripts();
39
            
40
40
41
            for (int i = 0; i < args.Length; i++)
41
            if (!Helper.CheckRegistryProtocol())
42
            {
42
            {
43
                switch (args[i])
43
                Console.WriteLine(@"[WARNING] URL Handler is not registered. CFORGE: links will not work properly. Use RegisterURLHandler.bat to do this (at <CODESYS Installation folder>\CFORGE\ )");
44
                {
44
            }
45
                    case "--register-url-handler":
45
46
                        Helper.RegisterProtocol();
46
            List<String> liScripts = Helper.GetAllScripts();
47
                        break;
47
            
48
                    case "--add-to-path":
48
            bool bDebug = false;
49
                        Helper.AddToSystemPath();
49
50
                        break;
50
            for (int i = 0; i < args.Length; i++)
51
51
            {
52
                    case "--list-scripts":
52
                switch (args[i])
53
53
                {
54
                        Console.WriteLine("List all scripts: ");
54
                    case "-d":
55
                        foreach (String item in liScripts)
55
                        bDebug = true;
56
                        {
56
                        if (Helper.IsUserElevated())
57
                            Console.WriteLine(" -" + item);
57
                            Console.WriteLine("Running elevated");
58
                        }
58
                        break;
59
                        break;
59
60
            
60
                    // fallthrough intended
61
                    /* fallthrough intended ! */
61
                    case "-v":
62
                    case "--help":
62
                    case "--version":
63
                    case "-h":
63
                        
64
                        ShowUsage();
64
                        Version version = Assembly.GetEntryAssembly().GetName().Version;
65
                        break;
65
                        Console.WriteLine("Version: " + version.ToString());
66
66
                        break;
67
                    default:
67
68
                        // check if we have matching script:
68
                    case "--setup":
69
                        if(liScripts.Contains(args[i]))
69
                        if (!Helper.IsUserElevated())
70
                        {
70
                            Helper.RunElevated("--setup");
71
                            // the rest of the command is passed as argument to the script
71
                        else
72
                            string[] newargs = new List<string>(args).GetRange(i, args.Length - i - 1).ToArray();
72
                        {
73
                            Helper.ExecuteIPyScript(args[i], newargs);
73
                            Helper.RegisterProtocol(bDebug);
74
                        }
74
                            Helper.AddToSystemPath(bDebug);
75
                        break;
75
                        }
76
                }
76
                        break;
77
            }
77
                    
78
        }
78
79
79
                    case "--license-info":
80
        static void ShowUsage()
80
                        Helper.ShowLicenseInfo();
81
        {
81
                        break;
82
            Console.WriteLine(@"usage: cforge <command> [options]");
82
83
            Console.WriteLine(@"");
83
                    
84
            Console.WriteLine(@"commands:");
84
                    
85
            Console.WriteLine(@"-h/--help: show this help");
85
                    /* fallthrough intended ! */
86
            Console.WriteLine(@"--register-url-handler      register cforge url handler in windows registry");
86
                    case "--help":
87
            Console.WriteLine(@"--add-to-path               add this cforge folder to windows path variable");
87
                    case "-h":
88
            
88
                        ShowUsage();
89
            Console.WriteLine(@"--list-scripts              list all available scripts (ironpython subcommands)");
89
                        break;
90
90
91
            // generic part
91
                    default:
92
92
                        // check if we have matching script:
93
                        if (args[i].StartsWith("--"))
94
                        {
95
                            String command = args[i].Replace("--", "");
96
                            if (liScripts.Contains(command))
97
                            {
98
                                // the rest of the command is passed as argument to the script
99
100
                                string[] newargs = args.Skip(i + 1).ToArray();
101
                                
102
                                Helper.ExecuteIPyScript(command, newargs, bDebug);
103
                                // as we "used" all params...
104
                                i = args.Length;
105
                            }
106
                            else
107
                            {
108
                                ShowUsage();
109
                            }
110
                        }
111
                        break;
112
                }
113
            }
114
115
            if (args.Length == 0)
116
                ShowUsage();
117
            
118
            Console.WriteLine("");
119
            Console.WriteLine(@"----------------------------------------------------------------------------");
120
            Console.WriteLine("cforge finished.");
121
            Console.WriteLine(@"----------------------------------------------------------------------------");
122
            Console.WriteLine("");
123
124
            //Console.ReadLine();
125
        }
126
127
        static void ShowUsage()
128
        {
129
            // pad right 32 for description ;-)
130
            Console.WriteLine(@"usage: cforge <command> <arguments>");
131
            Console.WriteLine(@"");
132
            Console.WriteLine(@"internal commands:");
133
            Console.WriteLine(@"-h/--help                       show this help");
134
            Console.WriteLine(@"--setup                         register cforge url handler and add cforge to path");
135
            Console.WriteLine(@"--license-info                  show license information");
136
            Console.WriteLine(@"-v/--version                    show version information");
137
            Console.WriteLine(@"");
138
            
139
            Console.WriteLine(@"");
140
            Console.WriteLine(@"external commands (scripts):");
141
            Console.WriteLine(@"------------------------------");
142
143
            // generic part
144
            
145
            List<String> liCommands = Helper.GetAllScripts();
146
            foreach (String  command in liCommands)
147
            {
148
                Helper.ShowUsageIPyScript(command, false);
149
            }
150
            Console.WriteLine(@"");
93
            Console.WriteLine(@"");
151
            Console.WriteLine(@"");
94
152
95
        }
153
        }
96
154
97
    }
155
    }
98
}
156
}