Exemplo n.º 1
0
        private static XmlSchema generateSchema(ScriptContext context)
        {
            // Ensure that all objects are from one namespace
            string ns = null;
            foreach (var type in context.GetKnownTypes())
            {
                var na = (CustomAttributeHelper.First<XsTypeAttribute>(type));
                if (na == null)
                    continue;

                if (ns == null && na.Namespace != ScriptActionBase.XSharperNamespace)
                    ns = na.Namespace;
                else if (ns != na.Namespace && na.Namespace != ScriptActionBase.XSharperNamespace)
                    context.Info.WriteLine("Warning: Object " + na.Name + " has unexpected namespace " + na.Namespace + ". All types must use the same namespace.");
            }

            if (ns == null)
                ns = ScriptActionBase.XSharperNamespace;

            var xmlSchema = XsXsdGenerator.BuildSchema(ns, context.GetKnownTypes(), typeof(Script), new Type[] { typeof(IScriptAction) });
            xmlSchema.Id = ns == ScriptActionBase.XSharperNamespace ? "xsharper" : "custom";

            // Compile it, just to make sure it's valid
            XmlSchemaSet s = new XmlSchemaSet();
            s.Add(xmlSchema);
            s.Compile();

            return xmlSchema;
        }
Exemplo n.º 2
0
        public static int Help(ScriptContext context, UsageGenerator usage, CommandLineParameters xsParams)
        {
            context.WriteLine(OutputType.Bold, GetLogo(context));
            context.WriteLine();

            var command = context.GetStr(xs.help, null);
            if (!string.IsNullOrEmpty(command))
            {
                var tt = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
                tt["param"] = typeof (CommandLineParameter);
                tt["versionInfo"] = typeof(VersionInfo);
                tt["usage"] = typeof(UsageGenerator);

                foreach (var s in context.GetKnownTypes())
                {
                    foreach (var at in CustomAttributeHelper.All<XsTypeAttribute>(s))
                        if (!string.IsNullOrEmpty(at.Name))
                            tt[at.Name] = s;
                }

                Type type;
                if (tt.TryGetValue(command,out type))
                {

                    writeCommandHelp(context, type, usage.CorrectWidth(-1));
                    return -2;
                }
                if (command == "*")
                {
                    List<Var> v = new List<Var>();
                    v.Add(new Var("param",getDescr(typeof(CommandLineParameter))));
                    v.Add(new Var("versioninfo", getDescr(typeof(VersionInfo))));
                    v.Add(new Var("usage", getDescr(typeof(UsageGenerator))));

                    foreach (var s in context.GetKnownTypes())
                    {
                        var xst = CustomAttributeHelper.First<XsTypeAttribute>(s);
                        if (xst == null || string.IsNullOrEmpty(xst.Name))
                            continue;
                        v.Add(new Var(xst.Name, getDescr(s)));
                    }
                    v.Sort((a, b) => string.Compare(a.Name, b.Name));
                    v.Insert(0, new Var("Actions:", null));
                    v.Insert(1, new Var("", null));
                    Utils.WrapTwoColumns(context.Out, v, 30, usage.CorrectWidth(-1));
                    return -2;
                }
                if (command.StartsWith(".", StringComparison.Ordinal))
                {
                    bool success = false;
                    foreach (var nn in ((IEvaluationContext)context).GetNonameObjects())
                    {
                        success =writeTypeHelp(context, nn.Type, new StringFilter(command.Substring(1)),usage.CorrectWidth(-1)) || success;
                    }
                    if (!success)
                        context.Error.WriteLine("Cannot find method '" + command + "'. ");
                    return -2;
                }

                Type t = context.FindType(command);
                if (t == null)
                    t = context.FindType("XS." + command);
                if (t != null)
                    writeTypeHelp(context, t, null, usage.CorrectWidth(-1));
                else if (command.Contains("?") || command.Contains("*"))
                {
                    var r = Utils.WildcardToRegex(command, RegexOptions.IgnoreCase);
                    foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                        foreach (var ttt in a.GetTypes())
                            if (ttt != null && ttt.IsPublic && (r.IsMatch(ttt.Name) || r.IsMatch(Dump.GetFriendlyTypeName(ttt))))
                            {
                                context.WriteLine(Dump.GetFriendlyTypeName(ttt,true));
                            }
                }
                else
                    context.Error.WriteLine("Cannot find command or type '" + command + "'. Use //help * to display the list of commands");
            }
            else
                context.WriteLine(usage.GetUsage(context, null, null, -1, xsParams));
            return -2;
        }