Ejemplo n.º 1
0
        /// <summary>
        /// Echo method
        /// </summary>
        /// <param name="context">echo context</param>
        public void Echo(EchoEvaluationContext context)
        {
            var options = new TableFormattingOptions(context.CommandEvaluationContext.ShellEnv.TableFormattingOptions)
            {
                UnfoldCategories = false,
                UnfoldItems      = false,
                IsRawModeEnabled = true
            };

            var cols = context.CommandEvaluationContext.ShellEnv.Colors;
            var tb   = new Table(
                ("property", typeof(string), $"{cols.Label}{{0}}{cols.Default}")
                );

            tb.AddColumns(("value", typeof(object)));

            tb.AddRow("id", Id);
            tb.AddRow("registration", Registration);
            tb.AddRow("version", Version);
            tb.AddRow("description", Description);
            tb.AddRow("summary", Summary);
            tb.AddRow("title", Title);
            tb.AddRow("icon url", IconUrl);
            tb.AddRow("license url", LicenseUrl);
            tb.AddRow("project url", ProjectUrl);
            tb.AddRow("tags", Tags);
            tb.AddRow("authors", Authors);
            tb.AddRow("total downloads", TotalDownloads);
            tb.AddRow("verified", Verified);
            tb.AddRow("packages types", PackageTypes);
            tb.AddRow("versions", Versions);
            tb.Echo(new EchoEvaluationContext(context, options));
        }
Ejemplo n.º 2
0
        List <FileSystemPath> RecurseInteractiveDeleteDir(
            CommandEvaluationContext context,
            DirectoryPath dir,
            bool simulate,
            bool noattributes,
            bool verbose,
            CancellationTokenSource cancellationTokenSource)
        {
            var fullname = true;
            var r        = new List <FileSystemPath>();

            verbose |= simulate;
            if (cancellationTokenSource.IsCancellationRequested)
            {
                return(r);
            }
            if (dir.IsEmpty || Confirm("rm: descend " + dir.GetPrintableName(fullname)))
            {
                foreach (var subdir in dir.DirectoryInfo.EnumerateDirectories())
                {
                    r.Merge(RecurseInteractiveDeleteDir(context, new DirectoryPath(subdir.FullName), simulate, noattributes, verbose, cancellationTokenSource));
                }

                var options     = new FileSystemPathFormattingOptions(!noattributes, false, "", Br, -1, "removed ");
                var echoContext = new EchoEvaluationContext(context.Out, context, options);

                foreach (var subfile in dir.DirectoryInfo.EnumerateFiles())
                {
                    var subfi = new FilePath(subfile.FullName);
                    if (Confirm("rm: remove file " + subfi.GetPrintableName(fullname)))
                    {
                        if (!simulate)
                        {
                            subfi.FileSystemInfo.Delete();
                        }
                        if (verbose)
                        {
                            subfi.Echo(echoContext);
                        }
                        r.Add(subfi);
                    }
                }
                if (Confirm("rm: remove directory " + dir.GetPrintableName(fullname)))
                {
                    if (!simulate)
                    {
                        dir.DirectoryInfo.Delete(true);
                    }
                    if (verbose)
                    {
                        dir.Echo(echoContext);
                    }
                    r.Add(dir);
                }
            }
            return(r);
        }
 public static void InvokeEcho(
     this MethodInfo echoMethodInfo,
     object obj,
     EchoEvaluationContext ctx
     )
 {
     if (echoMethodInfo.GetParameters().Length == 1)
     {
         echoMethodInfo.Invoke(obj, new object[] { ctx });
     }
     else
     {
         echoMethodInfo.Invoke(obj, new object[] { obj, ctx });
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Echo method
        /// </summary>
        /// <param name="context">echo context</param>
        public void Echo(EchoEvaluationContext context)
        {
            var cols = context.CommandEvaluationContext.ShellEnv.Colors;
            var r    = cols.Default + Environment.NewLine;
            var s    = $"{cols.HighlightSymbol}TotalHits";

            s += $"{cols.Default}=";
            s += $"{cols.Numeric}{TotalHits}{cols.Default}";
            context.Out.Echoln(s);
            if (Data != null && Data.Length > 0)
            {
                context.Out.Echoln($"{r}Packages list:{r}");
                int i = 1;
                foreach (var item in Data)
                {
                    item.Echo(context);
                    if (i < Data.Length)
                    {
                        context.Out.Echoln();
                    }
                    i++;
                }
            }
        }
Ejemplo n.º 5
0
        public CommandVoidResult NewModule(
            CommandEvaluationContext context,
            [Parameter(0, "module ID")] string id,
            [Parameter(1, "module project repository url (if not set do net set project repository properties, thus the project repo is not connected to any remote repository)", true)] string repoUrl = null,
            [Option("o", "out", "output path", true, true)] DirectoryPath output = null,
            [Option("i", "in", "take parameters from json input file", true, true)] string inputFile = defaultSettingsFileName,
            [Option("f", "force", "delete target if already exists")] bool force                   = false,
            [Option("s", "skip-errors", "skip ant error if possible")] bool skipErrors             = false,
            [Option(null, "no-push", "don't perform an initial push of project repo")] bool noPush = false,
            [Option("project template url", "modproj-tpl-url", "module project template url", true, true)] string url = moduleProjectTemplateRepositoryUrl,
            [Option(null, "preserve-curdir", "preserve current dir")] bool preserveCurrentDir = false
            )
        {
            var o = context.Out;
            var c = context.ShellEnv.Colors;

            output ??= new DirectoryPath(Environment.CurrentDirectory);
            output = new DirectoryPath(Path.Combine(output.FullName, id));
            var input = new FilePath(inputFile);
            var ectx  = new EchoEvaluationContext(context);

            ectx.Options.LineBreak = true;
            CommandVoidResult result = CommandVoidResult.Instance;

            o.Echo(_title("shell module dotnet project generator"));

            #region download_pattern_project

            if (force && output.CheckExists())
            {
                Directory.Delete(output.FullName, true);
            }

            o.Echoln(_subTitle("cloning module project repository", url));
            o.Echo(_("into: ")); output.Echo(ectx); o.Echoln("(br)");

            if (!ShellExec(context, skipErrors, "git", $"clone {url} {output.FullName}", out result))
            {
                return(result);
            }

            #endregion

            #region setup project template properties

            if (!input.CheckExists(context))
            {
                return(new CommandVoidResult(ReturnCode.Error));
            }
            o.Echoln(_subTitle($"set project template properties", $"'{id}'"));

            string packageId = !string.IsNullOrWhiteSpace(repoUrl) ?
                               Path.GetFileNameWithoutExtension(repoUrl) : null;
            string repoOwner = !string.IsNullOrWhiteSpace(repoUrl) ?
                               Path.GetFileName(Path.GetDirectoryName(repoUrl)) : null;

            var settingsText = File.ReadAllText(input.FullName);
            var settings     = JsonConvert
                               .DeserializeObject <ModuleSettings>(settingsText)
                               .AutoFill(id, packageId);
            settings.ModuleRepositoryUrl   = repoUrl;
            settings.ModuleRepositoryOwner = repoOwner;

            o.Echoln(_subTitle("using settings", input.FullName));
            settings.Echo(ectx); o.Echoln("(br)");

            var items = fs.FindItems(
                context,
                output.FullName,
                "*",
                false,
                true,
                false,
                true,
                false,
                null,
                false,
                new FindCounts(),
                false);

            var fields = typeof(ModuleSettings).GetFields();
            foreach (var item in items)
            {
                var path = item.FullName;
                if (item.IsFile && fs.IsTextFile(path))
                {
                    var tpl = path;
                    _templateReplace(fields, settings, ref tpl);

                    var txt = File.ReadAllText(path);

                    if (tpl != path)
                    {
                        File.Delete(path);
                        path = tpl;
                    }

                    tpl = txt + "";
                    _templateReplace(fields, settings, ref tpl);
                    if (tpl != txt)
                    {
                        File.WriteAllText(path, tpl);
                    }
                }
            }

            void _deleteIfExists(string n)
            {
                if (File.Exists(n))
                {
                    File.Delete(n);
                }
            }

            _deleteIfExists(Path.Combine(output.FullName, "templateInfo.txt"));
            _deleteIfExists(Path.Combine(output.FullName, "./module-settings.json"));

            #endregion

            #region setup project repository

            o.Echoln(_subTitle("setup project repository", url)); o.Echoln();

            var curDir = Environment.CurrentDirectory;
            Environment.CurrentDirectory = output.FullName;

            if (!ShellExec(context, skipErrors, "git", "remote remove origin", out result))
            {
                return(result);
            }

            if (!string.IsNullOrWhiteSpace(settings.ModuleRepositoryUrl))
            {
                if (!ShellExec(context, skipErrors, "git", $"remote add origin {settings.ModuleRepositoryUrl}", out result))
                {
                    return(result);
                }
                // check this
                if (!noPush && !ShellExec(context, skipErrors, "git", "push --set-upstream origin main", out result))
                {
                    return(result);
                }
            }

            #endregion

            #region restore & build project

            o.Echoln(_subTitle("restore & build module project", "")); o.Echoln();

            if (!ShellExec(context, skipErrors, "dotnet", "build", out result))
            {
                return(result);
            }

            #endregion

            if (preserveCurrentDir)
            {
                Environment.CurrentDirectory = curDir;
            }

            o.Echoln(_subTitle($"module project has been generated", id));

            return(result ?? CommandVoidResult.Instance);
        }
Ejemplo n.º 6
0
        public CommandVoidResult NewModule(
            CommandEvaluationContext context,
            [Parameter(0, "module ID")] string id,
            [Parameter(1, "module project repository url (if not set do net set project repository properties, thus the project repo is not connected to any remote repository)", true)] string repoUrl = null,
            [Option("o", "out", "output path", true, true)] DirectoryPath output = null,
            [Option("i", "in", "take parameters from json input file", true, true)] string inputFile = defaultSettingsFileName,
            [Option("f", "force", "delete target if already exists")] bool force       = false,
            [Option("s", "skip-errors", "skip ant error if possible")] bool skipErrors = false,
            [Option(null, "no-git-init", "don't perform an initial init of the git repository - do not init the project as a git repository")] bool noInitRemote = false,
            [Option("project template url", "modproj-tpl-url", "module project template url", true, true)] string url             = moduleProjectTemplateRepositoryUrl,
            [Option("project archive url", "modproj-arch-url", "module project archive template url", true, true)] string archUrl = moduleProjectTemplateArchiveUrl,
            [Option(null, "preserve-curdir", "preserve current dir")] bool preserveCurrentDir = false
            )
        {
            var  o      = context.Out;
            var  c      = context.ShellEnv.Colors;
            bool noRepo = repoUrl == null;

            noInitRemote |= noRepo;
            string packageId = !string.IsNullOrWhiteSpace(repoUrl) ?
                               Path.GetFileNameWithoutExtension(repoUrl) : null;
            string repoOwner = !string.IsNullOrWhiteSpace(repoUrl) ?
                               Path.GetFileName(Path.GetDirectoryName(repoUrl)) : null;

            output ??= new DirectoryPath(Environment.CurrentDirectory);
            output = new DirectoryPath(Path.Combine(output.FullName, packageId ?? id));
            var targetId = packageId ?? id;
            var input    = new FilePath(inputFile);
            var ectx     = new EchoEvaluationContext(context);

            ectx.Options.LineBreak = true;
            CommandVoidResult result = CommandVoidResult.Instance;

            o.Echo(_title("shell module dotnet project generator"));

            #region download_pattern_project

            try
            {
                if (force && output.CheckExists())
                {
                    Directory.Delete(output.FullName, true);
                }
            } catch (Exception ex)
            {
                o.Errorln(ex.Message);
                if (!skipErrors)
                {
                    throw;
                }
            }

            o.Echoln(_subTitle("cloning module project repository", url));
            o.Echo(_("into: ")); output.Echo(ectx); o.Echoln("(br)");

            if (!noInitRemote)
            {
                // checkout repo
                if (!ShellExec(context, skipErrors, "git", $"clone {url} {output.FullName}", out result) && !skipErrors)
                {
                    return(result);
                }
            }
            else
            {
                // download & unpack as an archive
                o.Echoln(_subTitle("download project template archive", ""));
                var downloadArchRes = context.CommandLineProcessor.Eval(context, @$ "get " "{archUrl}" " -q -b");
                var archFile        = "tpl.zip";
                var res             = downloadArchRes.GetResult <HttpContentBody>();

                File.WriteAllBytes(
                    archFile,
                    (byte[])res.Content);

                if (File.Exists(archFile))
                {
                    _try(() =>
                    {
                        var arc  = ZipFile.OpenRead(archFile);
                        var root = arc.Entries.First();
                        arc.Dispose();
                        var rootName = root.FullName.Replace("/", "");
                        ZipFile.ExtractToDirectory(archFile, ".", true);
                        Directory.Move(rootName, targetId);
                        File.Delete(archFile);
                    });
                }
            }
            #endregion

            #region setup project template properties

            if (!input.CheckExists(context))
            {
                return(new CommandVoidResult(ReturnCode.Error));
            }
            o.Echoln(_subTitle($"set project template properties", $"'{id}'"));

            var settings = new ModuleSettings();

            _try(() =>
            {
                var settingsText = File.ReadAllText(input.FullName);
                var settings     = JsonConvert
                                   .DeserializeObject <ModuleSettings>(settingsText)
                                   .AutoFill(id, packageId);

                if (!noInitRemote)
                {
                    settings.ModuleRepositoryUrl   = repoUrl;
                    settings.ModuleRepositoryOwner = repoOwner;
                }

                o.Echoln(_subTitle("using settings", input.FullName));
                settings.Echo(ectx);
                o.Echoln("(br)");

                var items = fs.FindItems(
                    context,
                    output.FullName,
                    "*",
                    false,
                    true,
                    false,
                    true,
                    false,
                    null,
                    false,
                    new FindCounts(),
                    false);

                var fields = typeof(ModuleSettings).GetFields();
                foreach (var item in items)
                {
                    var path = item.FullName;
                    if (item.IsFile && FilePath.IsTextFile(path))
                    {
                        var tpl = path;
                        _templateReplace(fields, settings, ref tpl);

                        var txt = File.ReadAllText(path);

                        if (tpl != path)
                        {
                            _try(() => File.Delete(path));
                            path = tpl;
                        }

                        tpl = txt + "";
                        _templateReplace(fields, settings, ref tpl);
                        if (tpl != txt)
                        {
                            _try(() => File.WriteAllText(path, tpl));
                        }
                    }
                }
            });

            void _deleteIfExists(string n)
            {
                if (File.Exists(n))
                {
                    File.Delete(n);
                }
            }

            void _try(Action a)
            {
                try
                {
                    a();
                }
                catch (Exception ex)
                {
                    context.Error(ex.Message);
                    if (!skipErrors)
                    {
                        throw;
                    }
                }
            }

            _try(() =>
            {
                _deleteIfExists(Path.Combine(output.FullName, "templateInfo.txt"));
                _deleteIfExists(Path.Combine(output.FullName, "module-settings.json"));
            });

            #endregion

            #region setup project repository

            o.Echoln(_subTitle("setup project repository", url)); o.Echoln();

            var curDir = Environment.CurrentDirectory;
            _try(() => Environment.CurrentDirectory = output.FullName);

            if (!noInitRemote && !ShellExec(context, skipErrors, "git", "remote remove origin", out result) && !skipErrors)
            {
                return(result);
            }

            if (!string.IsNullOrWhiteSpace(settings.ModuleRepositoryUrl) && !noInitRemote)
            {
                if (!ShellExec(context, skipErrors, "git", $"remote add origin {settings.ModuleRepositoryUrl}", out result) && !skipErrors)
                {
                    return(result);
                }
                if (!ShellExec(context, skipErrors, "git", "fetch", out result) && !skipErrors)
                {
                    return(result);
                }
                if (!ShellExec(context, skipErrors, "git", "branch --set-upstream-to=origin/main main", out result) && !skipErrors)
                {
                    return(result);
                }
                if (!ShellExec(context, skipErrors, "git", "pull --allow-unrelated-histories", out result) && !skipErrors)
                {
                    return(result);
                }
                if (!ShellExec(context, skipErrors, "git", "add .", out result) && !skipErrors)
                {
                    return(result);
                }
                if (!ShellExec(context, skipErrors, "git", "commit -a -m \"initial commit\"", out result) && !skipErrors)
                {
                    return(result);
                }
                if (!ShellExec(context, skipErrors, "git", "push", out result) && !skipErrors)
                {
                    return(result);
                }
            }

            #endregion

            #region restore & build project

            o.Echoln(_subTitle("restore & build module project", "")); o.Echoln();

            if (!ShellExec(context, skipErrors, "dotnet", "build", out result) && !skipErrors)
            {
                return(result);
            }

            #endregion

            if (preserveCurrentDir)
            {
                _try(() => Environment.CurrentDirectory = curDir);
            }

            o.Echoln(_subTitle($"module project has been generated", id));

            return(result ?? CommandVoidResult.Instance);
        }
Ejemplo n.º 7
0
        public void Echo(EchoEvaluationContext ctx)
        {
            var(@out, context, opts) = ctx;
            if (context.EchoMap.MappedCall(this, ctx))
            {
                return;
            }

            var options = opts as FileSystemPathFormattingOptions;

            options ??= (FileSystemPathFormattingOptions)
            context.ShellEnv.GetValue <FileSystemPathFormattingOptions>(ShellEnvironmentVar.display_fileSystemPathFormattingOptions)
            .InitFrom(opts);

            if (options.IsRawModeEnabled)
            {
                var rs = options.ShortPath ? FileSystemInfo.Name : UnescapePathSeparators(FileSystemInfo.FullName);
                var q  = rs.Contains(' ') ? "\"" : "";
                rs = q + rs + q;
                @out.Echo(rs, options.LineBreak, options.IsRawModeEnabled);
                return;
            }

            var bg    = GetCmd(EchoDirectives.b + "", DefaultBackground.ToString().ToLower());
            var fg    = GetCmd(EchoDirectives.f + "", DefaultForeground.ToString().ToLower());
            var color = (IsDirectory) ? NormalDirectoryColorization : FileColorization;

            if (!IsSystem && IsDirectory && !IsReadOnly)
            {
                color += WritableDirectoryColorization;
            }
            if (IsSystem && !IsDirectory)
            {
                color += SystemColorization + bg;
            }
            if (IsSystem && IsDirectory && !IsReadOnly)
            {
                color += SystemWritableDirectoryColorization;
            }
            if (IsFile && IsReadOnly)
            {
                color += ReadOnlyFileColorization;
            }
            var    endcolor = bg + fg + ANSI.RSTXTA;
            var    r        = "";
            var    attr     = "";
            string hidden   = "";

            if (options.PrintAttributes)
            {
                var dir = IsDirectory ? "d" : "-";
                var ro  = IsReadOnly ? "r-" : "rw";
                var sys = IsSystem ? "s" : "-";
                var h   = IsHidden ? "h" : "-";
                //var c = IsCompressed ? "c" : "-";
                var a    = IsArchive ? "a" : "-";
                var size = (IsDirectory || FileSystemInfo == null) ? "" : HumanFormatOfSize(((FileInfo)FileSystemInfo).Length, 2);
                hidden = IsHidden ? "*" : "";
                string smoddat = "";
                if (FileSystemInfo != null)
                {
                    var moddat = FileSystemInfo.LastWriteTime;
                    var dat    = (moddat.Year != System.DateTime.Now.Year) ? moddat.Year + "" : "";
                    smoddat = $"{dat,4} {moddat.ToString("MMM", CultureInfo.InvariantCulture),-3} {moddat.Day,-2} {moddat.Hour.ToString().PadLeft(2, '0')}:{moddat.Minute.ToString().PadLeft(2, '0')}";
                }
                attr = $" {dir}{ro}{sys}{h}{a} {size,10} {smoddat}  ";
            }
            var name  = options.ShortPath ? FileSystemInfo.Name : UnescapePathSeparators(FileSystemInfo.FullName);
            var quote = name.Contains(' ') ? "\"" : "";
            var pdr   = options.PaddingRight - name.Length;

            if (!string.IsNullOrWhiteSpace(quote))
            {
                pdr -= 2;
            }
            var rightspace = (options.PaddingRight > -1) ? endcolor + "".PadRight(pdr > 0 ? pdr : 1, ' ') : "";

            r += $"(rsf){options.LinePrefix}{attr}{color}{options.Prefix}{quote}{name}{quote}{hidden}{rightspace}{options.Postfix}";
            @out.Echo(r + context.ShellEnv.Colors.Default);
            if (HasError)
            {
                @out.Echo($" {ErrorColorization}{GetError()}");
            }
            @out.Echo(ANSI.RSTXTA); // @TODO: convention - si modif des couleurs uniquement ?? ou est-ce un hack pour la fin de ligne ?? a pour contrat de resetter f et b + unset text decoration
        }
 /// <summary>
 /// Echo method
 /// </summary>
 /// <param name="context">echo context</param>
 public void Echo(EchoEvaluationContext context)
 {
     context.Out.Echo($"Version={Version} Downloads={Downloads}", context.Options.LineBreak, context.Options.IsRawModeEnabled);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Echo method
 /// </summary>
 /// <param name="context">echo context</param>
 public void Echo(EchoEvaluationContext context)
 {
     context.Out.Echo(Name, context.Options.LineBreak, context.Options.IsRawModeEnabled);
 }