Exemplo n.º 1
0
        private static Script genUnzip(ScriptContext context, List<string> filteredArgs)
        {
            Script script;
            string[] s = context.GetStringArray(xs.unzip);
            if (s.Length < 1)
                throw new ArgumentException("Invalid arguments to " + xs.unzip + " command");

            script = createEmptyScript(context, "xsharper //unzip");
            script.Id = "unzip";
            filteredArgs.AddRange(s);

            script.Usage.Options = UsageOptions.IfHelp | UsageOptions.IfNoArguments | UsageOptions.UsageLine | UsageOptions.AutoSuffix;
            script.Parameters.Add(new CommandLineParameter("zip", CommandLineValueCount.Single, null, null) { Required = true, Description = "archive.zip", Value = "Zip archive to extract" });
            script.Parameters.Add(new CommandLineParameter("destination", CommandLineValueCount.Single, ".", null) { Description = "directory", Value = "Directory where to decompress files" });
            script.Parameters.Add(new CommandLineParameter("filter", CommandLineValueCount.Single, "*.*", null) { Description = "filter", Value = "File wildcard" });
            script.Parameters.Add(new CommandLineParameter("dirfilter", CommandLineValueCount.Single, "*", null) { Description = "directory-filter", Value = "Directory wildcard" });
            script.Parameters.Add(new CommandLineParameter(null, "zipTime", CommandLineValueCount.Single, "fileTime", null) { Synonyms = "zt", Value = "How to process time for files created in zip entries ( fileTime/utcFileTime = set to entry time, now/utcNow =ignore)" });
            script.Parameters.Add(new CommandLineParameter(null, "overwrite", CommandLineValueCount.Single, OverwriteMode.Always.ToString(), null) { Synonyms="o",Value = "Overwrite mode" });
            script.Parameters.Add(new CommandLineParameter(null, "password", CommandLineValueCount.Single, null, null) { Synonyms = "p", Value = "Archive password" });
            script.Parameters.Add(new CommandLineParameter(null, "ignore", CommandLineValueCount.None, "0", "1") { Synonyms = "i", Value = "Ignore errors" });
            script.Parameters.Add(new CommandLineParameter(null, "hidden", CommandLineValueCount.None, "0", "1") { Synonyms = "i", Value = "Extract hidden files" });

            script.Add(new PathOperation { Value = "${zip}", Operation = PathOperationType.GetFullPath, OutTo = "zip", Existence = Existence.FileExists});
            script.Add(new If(new Set("zip", "${=Path.ChangeExtension(${zip},'.zip')}"))
            {
                IsEmpty = "${=Path.GetExtension(${zip})}"
            });
            script.Add(new PathOperation { Value= "${destination}", Operation = PathOperationType.ToDirectoryInfo, Backslash = BackslashOption.Add, OutTo = "destination" });
            script.Add(new SetAttr("z", "zipTime", "${zipTime}"));
            script.Add(new SetAttr("z", "overwrite", "${overwrite}"));
            script.Add(new SetAttr("z", "hidden", "${hidden}"));

            script.Add(new Print { Value = "Extracting ${zip} => ${destination} ... " });

            Unzip z = new Unzip()
                          {
                              Id="z",
                              From = "${zip}",
                              To = "${destination}",
                              Transform = TransformRules.Expand,
                              Syntax = FilterSyntax.Auto,
                              Filter = "${filter}",
                              DirectoryFilter = "${dirfilter}",
                              Password = "******"
                          };
            z.Add(new Print { Value = "  ${to}", OutTo = "^info" });

            If f=new If(new[] { new Print("Failed: '${}' : ${=c.CurrentException.Message}") { OutTo = "^error"}});
            f.IsTrue = "${ignore}";
            f.AddElse(new Throw());
            z.AddCatch(f);

            script.Add(z);
            script.Add(new Print { Value = "Completed" });
            return script;
        }
Exemplo n.º 2
0
        private static Script genZip(ScriptContext context, List<string> filteredArgs)
        {
            Script script;
            script = createEmptyScript(context,"xsharper //zip");
            script.Id = "zip";
            filteredArgs.AddRange(context.GetStringArray(xs.zip));
            script.Usage.Options = UsageOptions.IfHelp | UsageOptions.IfNoArguments | UsageOptions.UsageLine | UsageOptions.AutoSuffix;
            script.Parameters.Add(new CommandLineParameter("zip", CommandLineValueCount.Single, null, null) { Required = true, Description = "archive.zip", Value = "Zip archive to create" });
            script.Parameters.Add(new CommandLineParameter("source", CommandLineValueCount.Single, ".",null) { Description = "directory", Value = "Directory to compress" });
            script.Parameters.Add(new CommandLineParameter("filter", CommandLineValueCount.Single, "*.*", null) { Description = "filter", Value = "File wildcard" });
            script.Parameters.Add(new CommandLineParameter("dirfilter", CommandLineValueCount.Single, "*", null) { Description = "directory-filter", Value = "Directory wildcard" });
            script.Parameters.Add(new CommandLineParameter(null,"zipTime", CommandLineValueCount.Single, "fileTime", null) { Synonyms="zt", Value = "What time to store in zip ( fileTime/utcFileTime/now/utcNow )" });
            script.Parameters.Add(new CommandLineParameter(null,"password", CommandLineValueCount.Single, null, null) { Synonyms = "p", Value = "Archive password" });
            script.Parameters.Add(new CommandLineParameter(null,"recursive", CommandLineValueCount.None, "0", "1") { Synonyms = "r", Value = "Recursive" });
            script.Parameters.Add(new CommandLineParameter(null, "ignore", CommandLineValueCount.None, "0", "1") { Synonyms = "i", Value = "Ignore errors" });
            script.Parameters.Add(new CommandLineParameter(null,"emptyDirectories", CommandLineValueCount.None, "0", "1") { Value = "Include empty directories" });
            script.Parameters.Add(new CommandLineParameter(null, "hidden", CommandLineValueCount.None, "0", "1") { Synonyms = "i", Value = "Extract hidden files" });

            script.Add(new PathOperation {Value = "${zip}", Operation = PathOperationType.GetFullPath, OutTo = "zip"});
            script.Add(new If(new Set( "zip", "${=Path.ChangeExtension(${zip},'.zip')}"))
                {
                    IsEmpty = "${=Path.GetExtension(${zip})}"
                });

            script.Add(new SetAttr("z", "zipTime", "${zipTime}"));
            script.Add(new SetAttr("z", "recursive", "${recursive}"));
            script.Add(new SetAttr("z", "hidden", "${hidden}"));
            script.Add(new SetAttr("z", "emptyDirectories", "${emptyDirectories}"));
            script.Add(new Print("Compressing ${source} => ${zip} ... "));
            Zip z=new Zip {
                              Id="z",
                              From = "${source}",
                              To = "${zip}",
                              Transform = TransformRules.Expand,
                              Recursive = true,
                              Syntax = FilterSyntax.Wildcard,
                              Filter = "${filter}",
                              DirectoryFilter = "${dirfilter}",
                              Password = "******"
                          };
            If oif = new If(new Print { Value = "  ${from}", OutTo = "^info" })
                        {
                             IsTrue = "${=$.IsFile}"
                         };

            If f = new If(new[] { new Print("Failed: '${}' : ${=c.CurrentException.Message}") { OutTo = "^error" } });
            f.IsTrue = "${ignore}";
            f.AddElse(new Throw());
            z.AddCatch(f);

            z.Add(oif);
            script.Add(z);
            script.Add(new Print { Value = "Completed" });
            return script;
        }
Exemplo n.º 3
0
        private static Script genDownload(ScriptContext context, List<string> filteredArgs)
        {
            Script script;
            script = createEmptyScript(context,"xsharper //download");
            script.Id = "download";
            filteredArgs.AddRange(context.GetStringArray(xs.download));
            script.Usage.Options = UsageOptions.IfHelp | UsageOptions.IfNoArguments | UsageOptions.UsageLine | UsageOptions.AutoSuffix;
            script.Parameters.Add(new CommandLineParameter("uri", CommandLineValueCount.Single, null, null) { Required = true,  Value = "Source URL" });
            script.Parameters.Add(new CommandLineParameter("file", CommandLineValueCount.Single, ".",null) { Value = "Destination file or directory" });
            script.Parameters.Add(new CommandLineParameter(null, "cache", CommandLineValueCount.Single, Utils.LowercaseFirstLetter(RequestCacheLevel.Default.ToString()), null) { Description = "cache-level", Value = "Cache level, one of "+Utils.AllEnumValuesToString(typeof(RequestCacheLevel)," / ") });
            script.Parameters.Add(new CommandLineParameter("passive", "activeFtp", CommandLineValueCount.None, true, false) { Value = "Use active FTP" });
            script.Parameters.Add(new CommandLineParameter("userAgent","userAgent", CommandLineValueCount.Single, null,null) { Value = "User agent string (http://)" });
            script.Parameters.Add(new CommandLineParameter("post", "post", CommandLineValueCount.Single, null, null) { Value = "HTTP Post string (evaluated as XSharper expression)"});
            script.Parameters.Add(new CommandLineParameter("postContentType", "postContentType", CommandLineValueCount.Single, null, null) { Value = "HTTP Post content type (default is application/x-www-form-urlencoded)" });
            script.Parameters.Add(new CommandLineParameter("timeout", "timeout", CommandLineValueCount.Single, null, null) { Value = "Timeout" });
            script.Parameters.Add(new CommandLineParameter("ignoreCertificateErrors", "ignoreCertificateErrors", CommandLineValueCount.None, true, false) { Value = "Ignore SSL certificate errors " });

            script.Add(new Set("oldReceived", "0"));
            script.Add(new SetAttr("d","cacheLevel","${cache}"));
            script.Add(new SetAttr("d", "passiveFtp", "${passive}"));
            script.Add(new Set("fn", "${=XS.Download.UrlToLocalFilename($uri,$file)}",TransformRules.Expand));
            script.Add(new Print { Value = "Downloading ${=XS.Utils.SecureUri($uri)} => ${fn} ... "});
            If ifs1 = new If()  {   IsTrue = "${ignoreCertificateErrors}"};

            ifs1.Add(new Code("System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };") { Dynamic = true });
            script.Add(ifs1);
            Download d = new Download
                             {
                                 Id = "d",
                                 From="${uri}",
                                 To = "${file}",
                                 UserAgent = "${userAgent|=null}",
                                 Post = "${=.Expand(${post|=null})}",
                                 PostContentType = "${postContentType|=null}",
                                 Timeout = "${timeout|=null}",
                                 Transform = TransformRules.Expand
                             };

            If ifs = new If()
                {
                    Condition = "${= $.bytesReceived/1024/100 #GT# $oldReceived/1024/100}"
                };
            ifs.Add(new Print(".") { OutTo="^info" , NewLine = false});
            d.Add(ifs);

            d.Add(new Set("oldReceived","${= $.bytesReceived}",TransformRules.Expand));

            script.Add(d);
            script.Add(new Print { Value = "Completed. ${oldReceived} bytes downloaded." });
            return script;
        }