~ubuntu-branches/ubuntu/trusty/mono-addins/trusty-proposed

« back to all changes in this revision

Viewing changes to Mono.Addins.Setup/Mono.Addins.Setup/SetupTool.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-04-25 11:11:33 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110425111133-t05u5p7o5fxx70fu
Tags: 0.6-2
Upload to Unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
namespace Mono.Addins.Setup
40
40
{
 
41
        /// <summary>
 
42
        /// A command line add-in manager.
 
43
        /// </summary>
 
44
        /// <remarks>
 
45
        /// This class can be used to provide an add-in management command line tool to applications.
 
46
        /// </remarks>
41
47
        public class SetupTool
42
48
        {
43
49
                Hashtable options = new Hashtable ();
49
55
                string setupAppName = "";
50
56
                int uniqueId = 0;
51
57
                
52
 
                bool verbose;
 
58
                int verbose = 1;
53
59
                
 
60
                /// <summary>
 
61
                /// Creates a new instance
 
62
                /// </summary>
 
63
                /// <param name="registry">
 
64
                /// Add-in registry to manage.
 
65
                /// </param>
54
66
                public SetupTool (AddinRegistry registry)
55
67
                {
56
68
                        this.registry = registry;
58
70
                        CreateCommands ();
59
71
                }
60
72
                
 
73
                /// <summary>
 
74
                /// Display name of the host application
 
75
                /// </summary>
61
76
                public string ApplicationName {
62
77
                        get { return applicationName; }
63
78
                        set { applicationName = value; }
64
79
                }
65
80
                
 
81
                /// <summary>
 
82
                /// Default add-in namespace of the application (optional). If set, only add-ins that belong to that namespace
 
83
                /// will be shown in add-in lists.
 
84
                /// </summary>
66
85
                public string ApplicationNamespace {
67
86
                        get { return service.ApplicationNamespace; }
68
87
                        set { service.ApplicationNamespace = value; }
69
88
                }
70
89
                
 
90
                /// <summary>
 
91
                /// Enables or disables verbose output
 
92
                /// </summary>
71
93
                public bool VerboseOutput {
 
94
                        get { return verbose > 1; }
 
95
                        set { verbose = value ? 2 : 1; }
 
96
                }
 
97
                
 
98
                /// <summary>
 
99
                /// Sets or gets the verbose output level (0: normal output, 1:verbose, 2+:extra verbose)
 
100
                /// </summary>
 
101
                public int VerboseOutputLevel {
72
102
                        get { return verbose; }
73
103
                        set { verbose = value; }
74
104
                }
75
105
 
 
106
                /// <summary>
 
107
                /// Runs the command line tool.
 
108
                /// </summary>
 
109
                /// <param name="args">
 
110
                /// Array that contains the command line arguments
 
111
                /// </param>
 
112
                /// <param name="firstArgumentIndex">
 
113
                /// Index of the arguments array that has the first argument for the management tool
 
114
                /// </param>
 
115
                /// <returns>
 
116
                /// 0 if it succeeds. != 0 otherwise
 
117
                /// </returns>
76
118
                public int Run (string[] args, int firstArgumentIndex)
77
119
                {
78
120
                        string[] aa = new string [args.Length - firstArgumentIndex];
80
122
                        return Run (aa);
81
123
                }
82
124
                
 
125
                /// <summary>
 
126
                /// Runs the command line tool.
 
127
                /// </summary>
 
128
                /// <param name="args">
 
129
                /// Command line arguments
 
130
                /// </param>
 
131
                /// <returns>
 
132
                /// 0 if it succeeds. != 0 otherwise
 
133
                /// </returns>
83
134
                public int Run (string[] args)
84
135
                {
85
136
                        if (args.Length == 0) {
92
143
                        
93
144
                        try {
94
145
                                ReadOptions (parms);
95
 
                                verbose = verbose || HasOption ("v");
 
146
                                if (HasOption ("v"))
 
147
                                        verbose++;
96
148
                                return RunCommand (args [0], parms);
97
149
                        } catch (InstallException ex) {
98
150
                                Console.WriteLine (ex.Message);
333
385
                                service.Repositories.RegisterRepository (new ConsoleProgressStatus (verbose), rep);
334
386
                }
335
387
                
 
388
                string GetRepositoryUrl (string url)
 
389
                {
 
390
                        AddinRepository[] reps = GetRepositoryList ();
 
391
                        int nr;
 
392
                        if (int.TryParse (url, out nr)) {
 
393
                                if (nr < 0 || nr >= reps.Length)
 
394
                                        throw new InstallException ("Invalid repository number.");
 
395
                                return reps[nr].Url;
 
396
                        } else {
 
397
                                if (!service.Repositories.ContainsRepository (url))
 
398
                                        throw new InstallException ("Repository not registered.");
 
399
                                return url;
 
400
                        }
 
401
                }
 
402
                
336
403
                void RemoveRepository (string[] args)
337
404
                {
338
 
                        foreach (string rep in args)
339
 
                                service.Repositories.RemoveRepository (rep);
 
405
                        foreach (string rep in args) {
 
406
                                service.Repositories.RemoveRepository (GetRepositoryUrl (rep));
 
407
                        }
 
408
                }
 
409
                
 
410
                void EnableRepository (string[] args)
 
411
                {
 
412
                        foreach (string rep in args)
 
413
                                service.Repositories.SetRepositoryEnabled (GetRepositoryUrl(rep), true);
 
414
                }
 
415
                
 
416
                void DisableRepository (string[] args)
 
417
                {
 
418
                        foreach (string rep in args)
 
419
                                service.Repositories.SetRepositoryEnabled (GetRepositoryUrl(rep), false);
 
420
                }
 
421
                
 
422
                AddinRepository[] GetRepositoryList ()
 
423
                {
 
424
                        AddinRepository[] reps = service.Repositories.GetRepositories ();
 
425
                        Array.Sort (reps, (r1,r2) => r1.Title.CompareTo(r2.Title));
 
426
                        return reps;
340
427
                }
341
428
                
342
429
                void ListRepositories (string[] args)
343
430
                {
344
 
                        AddinRepository[] reps = service.Repositories.GetRepositories ();
 
431
                        AddinRepository[] reps = GetRepositoryList ();
345
432
                        if (reps.Length == 0) {
346
433
                                Console.WriteLine ("No repositories have been registered.");
347
434
                                return;
348
435
                        }
 
436
                        int n = 0;
349
437
                        Console.WriteLine ("Registered repositories:");
350
438
                        foreach (RepositoryRecord rep in reps) {
351
 
                                Console.WriteLine (" - " + rep.Title);
 
439
                                string num = n.ToString ();
 
440
                                Console.Write (num + ") ");
 
441
                                if (!rep.Enabled)
 
442
                                        Console.Write ("(Disabled) ");
 
443
                                Console.WriteLine (rep.Title);
 
444
                                if (rep.Title != rep.Url)
 
445
                                        Console.WriteLine (new string (' ', num.Length + 2) + rep.Url);
 
446
                                n++;
352
447
                        }
353
448
                }
354
449
                
367
462
                        service.BuildPackage (new ConsoleProgressStatus (verbose), GetOption ("d", "."), GetArguments ());
368
463
                }
369
464
                
 
465
                void PrintLibraries (string[] args)
 
466
                {
 
467
                        if (GetArguments ().Length < 1)
 
468
                                throw new InstallException ("An add-in id is required.");
 
469
                        
 
470
                        bool refFormat = HasOption ("r");
 
471
                        
 
472
                        System.Text.StringBuilder sb = new System.Text.StringBuilder ();
 
473
                        foreach (string id in GetArguments ()) {
 
474
                                Addin addin = service.Registry.GetAddin (id);
 
475
                                if (addin != null) {
 
476
                                        foreach (string asm in addin.Description.MainModule.Assemblies) {
 
477
                                                string file = Path.Combine (addin.Description.BasePath, asm);
 
478
                                                if (sb.Length > 0)
 
479
                                                        sb.Append (' ');
 
480
                                                if (refFormat)
 
481
                                                        sb.Append ("-r:");
 
482
                                                sb.Append (file);
 
483
                                        }
 
484
                                }
 
485
                        }
 
486
                        Console.WriteLine (sb);
 
487
                }
 
488
                
 
489
                void PrintApplications (string[] args)
 
490
                {
 
491
                        foreach (Application app in SetupService.GetExtensibleApplications ()) {
 
492
                                string line = app.Name;
 
493
                                if (!string.IsNullOrEmpty (app.Description))
 
494
                                        line += " - " + app.Description;
 
495
                                Console.WriteLine (line);
 
496
                        }
 
497
                }
 
498
                
370
499
                void UpdateRegistry (string[] args)
371
500
                {
372
501
                        registry.Update (new ConsoleProgressStatus (verbose));
728
857
                        arguments = (string[]) list.ToArray (typeof(string));
729
858
                }
730
859
                
 
860
                /// <summary>
 
861
                /// Adds a custom command to the add-in manager
 
862
                /// </summary>
 
863
                /// <param name="category">
 
864
                /// Category under which the command has to be shown in the help text
 
865
                /// </param>
 
866
                /// <param name="command">
 
867
                /// Name of the command
 
868
                /// </param>
 
869
                /// <param name="shortName">
 
870
                /// Short name of the command (it's an alias of the normal name)
 
871
                /// </param>
 
872
                /// <param name="arguments">
 
873
                /// Formal description of the arguments that the command accepts. For example: "[addin-id|addin-file] [--xml] [--all] [--full] [--namespace <namespace>]"
 
874
                /// </param>
 
875
                /// <param name="description">
 
876
                /// Short description of the command
 
877
                /// </param>
 
878
                /// <param name="longDescription">
 
879
                /// Long description of the command
 
880
                /// </param>
 
881
                /// <param name="handler">
 
882
                /// Delegate to be invoked to run the command
 
883
                /// </param>
731
884
                public void AddCommand (string category, string command, string shortName, string arguments, string description, string longDescription, SetupCommandHandler handler)
732
885
                {
733
886
                        SetupCommand cmd = new SetupCommand (category, command, shortName, handler);
755
908
                        return null;
756
909
                }
757
910
 
 
911
                /// <summary>
 
912
                /// Prints help about the add-in management tool, or about a specific command
 
913
                /// </summary>
 
914
                /// <param name="parms">
 
915
                /// Optional command name and arguments
 
916
                /// </param>
758
917
                public void PrintHelp (params string[] parms)
759
918
                {
760
919
                        if (parms.Length == 0) {
761
920
                                string lastCat = null;
762
921
                                foreach (SetupCommand cmd in commands) {
 
922
                                        if (cmd.Command == "help")
 
923
                                                continue;
763
924
                                        if (lastCat != cmd.Category) {
764
925
                                                Console.WriteLine ();
765
926
                                                Console.WriteLine (cmd.Category + ":");
783
944
                                        Console.WriteLine ();
784
945
                                        Console.WriteLine ("Usage: {0}{1}", setupAppName, cmd.Usage);
785
946
                                        Console.WriteLine ();
786
 
                                        Console.WriteLine (cmd.LongDescription);
 
947
                                        
 
948
                                        TextFormatter fm = new TextFormatter ();
 
949
                                        fm.Wrap = WrappingType.Word;
 
950
                                        fm.Append (cmd.LongDescription);
 
951
                                        Console.WriteLine (fm.ToString ());
787
952
                                }
788
953
                                else
789
954
                                        Console.WriteLine ("Unknown command: " + parms [0]);
801
966
                        cmd.Usage = "[package-name|package-file] ...";
802
967
                        cmd.AppendDesc ("Installs an add-in or set of addins. The command argument is a list");
803
968
                        cmd.AppendDesc ("of files and/or package names. If a package name is provided");
804
 
                        cmd.AppendDesc ("the package will be looked out in the registered repositories.");
 
969
                        cmd.AppendDesc ("the package will be looked up in the registered repositories.");
805
970
                        cmd.AppendDesc ("A specific add-in version can be specified by appending it to.");
806
971
                        cmd.AppendDesc ("the package name using '/' as a separator, like in this example:");
807
972
                        cmd.AppendDesc ("MonoDevelop.SourceEditor/0.9.1");
855
1020
 
856
1021
                        cmd = new SetupCommand (cat, "rep-remove", "rr", new SetupCommandHandler (RemoveRepository));
857
1022
                        cmd.Description = "Unregisters repositories.";
858
 
                        cmd.Usage = "<url> ...";
 
1023
                        cmd.Usage = "<url or number> ...";
859
1024
                        cmd.AppendDesc ("Unregisters an add-in repository. Several URLs can be provided.");
 
1025
                        cmd.AppendDesc ("Instead of an url, a repository number can be used (repository numbers are");
 
1026
                        cmd.AppendDesc ("shown by the rep-list command.");
 
1027
                        commands.Add (cmd);
 
1028
 
 
1029
                        cmd = new SetupCommand (cat, "rep-enable", "re", new SetupCommandHandler (EnableRepository));
 
1030
                        cmd.Description = "Enables repositories.";
 
1031
                        cmd.Usage = "<url or number> ...";
 
1032
                        cmd.AppendDesc ("Enables an add-in repository which has been disabled. Several URLs can be");
 
1033
                        cmd.AppendDesc ("provided. Instead of an url, a repository number can be used (repository");
 
1034
                        cmd.AppendDesc ("numbers are shown by the rep-list command.");
 
1035
                        commands.Add (cmd);
 
1036
 
 
1037
                        cmd = new SetupCommand (cat, "rep-disable", "rd", new SetupCommandHandler (DisableRepository));
 
1038
                        cmd.Description = "Disables repositories.";
 
1039
                        cmd.Usage = "<url> ...";
 
1040
                        cmd.AppendDesc ("Disables an add-in repository. Several URLs can be provided");
 
1041
                        cmd.AppendDesc ("When a repository is disabled, it will be ignored when using the update and");
 
1042
                        cmd.AppendDesc ("install commands.");
 
1043
                        cmd.AppendDesc ("Instead of an url, a repository number can be used (repository numbers are");
 
1044
                        cmd.AppendDesc ("shown by the rep-list command.");
860
1045
                        commands.Add (cmd);
861
1046
 
862
1047
                        cmd = new SetupCommand (cat, "rep-update", "ru", new SetupCommandHandler (UpdateAvailableAddins));
883
1068
                        commands.Add (cmd);
884
1069
 
885
1070
                        cmd = new SetupCommand (cat, "info", null, new SetupCommandHandler (PrintAddinInfo));
886
 
                        cmd.Description = "Prints information about an add-in.";
887
 
                        cmd.AppendDesc ("Prints information about an add-in.");
 
1071
                        cmd.Usage = "[addin-id|addin-file] [--xml] [--all] [--full] [--namespace <namespace>]";
 
1072
                        cmd.Description = "Prints information about add-ins.";
 
1073
                        cmd.AppendDesc ("Prints information about add-ins. Options:\n");
 
1074
                        cmd.AppendDesc (" --xml: Dump the information using an XML format.\n");
 
1075
                        cmd.AppendDesc (" --all: Dump information from all add-ins.\n");
 
1076
                        cmd.AppendDesc (" --full: Include add-ins which don't define extension points.\n");
 
1077
                        cmd.AppendDesc (" --namespace ns: Include only add-ins from the specified 'ns' namespace.");
888
1078
                        commands.Add (cmd);
889
1079
 
890
1080
                        cat = "Packaging Commands";
911
1101
                        cmd.Usage = "<command>";
912
1102
                        commands.Add (cmd);
913
1103
 
 
1104
                        cat = "Build Commands";
 
1105
 
 
1106
                        cmd = new SetupCommand (cat, "libraries", "libs", new SetupCommandHandler (PrintLibraries));
 
1107
                        cmd.Description = "Lists add-in assemblies.";
 
1108
                        cmd.Usage = "[-r] <addin-id> ...";
 
1109
                        cmd.AppendDesc ("Prints a list of assemblies exported by the add-in or add-ins provided");
 
1110
                        cmd.AppendDesc ("as arguments. This list of assemblies can be used as references for");
 
1111
                        cmd.AppendDesc ("building add-ins that depend on them. If the -r option is specified,");
 
1112
                        cmd.AppendDesc ("each assembly is prefixed with '-r:'.");
 
1113
                        commands.Add (cmd);
 
1114
 
 
1115
                        cmd = new SetupCommand (cat, "applications", "apps", new SetupCommandHandler (PrintApplications));
 
1116
                        cmd.Description = "Lists extensible applications.";
 
1117
                        cmd.AppendDesc ("Prints a list of registered extensible applications.");
 
1118
                        commands.Add (cmd);
 
1119
                        
914
1120
                        cat = "Debug Commands";
915
1121
 
916
1122
                        cmd = new SetupCommand (cat, "dump-file", null, new SetupCommandHandler (DumpRegistryFile));
935
1141
                
936
1142
                public void AppendDesc (string s)
937
1143
                {
938
 
                        LongDescription += s + "\n";
 
1144
                        LongDescription += s + " ";
939
1145
                }
940
1146
                
941
1147
                public string Category;
961
1167
                public string LongDescription = "";
962
1168
        }
963
1169
        
 
1170
        /// <summary>
 
1171
        /// A command handler
 
1172
        /// </summary>
964
1173
        public delegate void SetupCommandHandler (string[] args);
965
1174
}
966
1175