public CommandResult <(List <FileSystemPath> items, FindCounts counts)> Find(
     CommandEvaluationContext context,
     [Parameter("search path")] DirectoryPath path,
     [Option("p", "select names that matches the pattern", true, true)] string pattern,
     [Option("i", "if set and p is set, perform a non case sensisitive search")] bool ignoreCase,
     [Option("f", "check pattern on fullname instead of name")] bool checkPatternOnFullName,
     [Option("c", "files that contains the string", true, true)] string contains,
     [Option("a", "print file system attributes")] bool attributes,
     [Option("s", "print short pathes")] bool shortPathes,
     [Option("all", "select files and directories")] bool all,
     [Option("d", "select only directories")] bool dirs,
     [Option("t", "search in top directory only")] bool top
     )
 {
     if (path.CheckExists())
     {
         var sp     = string.IsNullOrWhiteSpace(pattern) ? "*" : pattern;
         var counts = new FindCounts();
         var items  = FindItems(context, path.FullName, sp, top, all, dirs, attributes, shortPathes, contains, checkPatternOnFullName, counts, true, false, ignoreCase);
         var f      = DefaultForegroundCmd;
         counts.Elapsed = DateTime.Now - counts.BeginDateTime;
         if (items.Count > 0)
         {
             context.Out.Echoln();
         }
         context.Out.Echoln($"found {ColorSettings.Numeric}{Plur("file",counts.FilesCount,f)} and {ColorSettings.Numeric}{Plur("folder",counts.FoldersCount,f)}. scanned {ColorSettings.Numeric}{Plur("file",counts.ScannedFilesCount,f)} in {ColorSettings.Numeric}{Plur("folder",counts.ScannedFoldersCount,f)} during {TimeSpanDescription(counts.Elapsed, ColorSettings.Numeric.ToString(), f)}");
         return(new CommandResult <(List <FileSystemPath>, FindCounts)>((items, counts)));
     }
     return(new CommandResult <(List <FileSystemPath>, FindCounts)>((new List <FileSystemPath>(), new FindCounts()), ReturnCode.Error));
 }
 public CommandResult <DirectoryPath> Cd(
     CommandEvaluationContext context,
     [Parameter("path where to list files and folders. if not specified is equal to the current directory", true)] DirectoryPath path
     )
 {
     path ??= new DirectoryPath(Path.GetPathRoot(Environment.CurrentDirectory));
     if (path.CheckExists())
     {
         var bkpath = Environment.CurrentDirectory;
         try
         {
             Environment.CurrentDirectory = path.FullName;
         }
         catch (UnauthorizedAccessException)
         {
             Errorln($"unauthorized access to {path.PrintableFullName}");
             Environment.CurrentDirectory = bkpath;
             return(new CommandResult <DirectoryPath>(path, ReturnCode.Error));
         }
         return(new CommandResult <DirectoryPath>(path, ReturnCode.OK));
     }
     else
     {
         return(new CommandResult <DirectoryPath>(path, ReturnCode.Error));
     }
 }
 public CommandVoidResult Rmdir(
     CommandEvaluationContext context,
     [Parameter("path of the directory to be deleted")] DirectoryPath path,
     [Option("r", "recurse", "recurse delete in sub folders, also delete files and sub folders")] bool recurse
     )
 {
     if (path.CheckExists(context))
     {
         Directory.Delete(path.FullName, recurse);
     }
     return(CommandVoidResult.Instance);
 }
        public CommandResult <DirectoryPath> Pwd(
            CommandEvaluationContext context,
            [Option("na", "do not print file system attributes")] bool noattributes
            )
        {
            var path = new DirectoryPath(Environment.CurrentDirectory);

            if (path.CheckExists())
            {
                path.Print(!noattributes, false, "", Br);
                return(new CommandResult <DirectoryPath>(path));
            }
            else
            {
                return(new CommandResult <DirectoryPath>());
            }
        }
        public CommandResult <DirectoryPath> Pwd(
            CommandEvaluationContext context,
            [Option("s", "short", "short display: do not print file system attributes")] bool noattributes
            )
        {
            var path = new DirectoryPath(Environment.CurrentDirectory);

            if (path.CheckExists(context))
            {
                path.Echo(new EchoEvaluationContext(context.Out, context, new FileSystemPathFormattingOptions(!noattributes, false, "", Br)));
                return(new CommandResult <DirectoryPath>(path));
            }
            else
            {
                return(new CommandResult <DirectoryPath>());
            }
        }
Beispiel #6
0
        public bool ShouldKeep(
            DirectoryPath localDir,
            GetResponse <string> remoteUrl)
        {
            if (!localDir.CheckExists(_fileSystem))
            {
                _logger.Information("No local repository exists at {LocalDirectory}.  No cleaning to do", localDir);
                return(false);
            }
            if (remoteUrl.Failed)
            {
                _logger.Warning("No remote repository.  Deleting local at {LocalDirectory}", localDir);
                return(false);
            }
            try
            {
                using var repoCheckout = RepoCheckouts.Get(localDir);

                if (!IfRepositoryDesirable.IsDesirable(repoCheckout.Repository))
                {
                    return(false);
                }

                // If it's the same remote repo, don't delete
                if (repoCheckout.Repository.MainRemoteUrl?.Equals(remoteUrl.Value) ?? false)
                {
                    _logger.Information("Remote repository target matched local folder's repo at {LocalDirectory}.  Keeping clone", localDir);
                    return(true);
                }
            }
            catch (RepositoryNotFoundException)
            {
                _logger.Error("Repository corrupted.  Deleting local at {LocalDirectory}", localDir);
                return(false);
            }

            _logger.Information("Remote address targeted a different repository.  Deleting local at {LocalDirectory}", localDir);
            return(false);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }