示例#1
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;
        }
示例#2
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;
        }