Example #1
0
        /// <summary>
        /// Clone a git repository. Caller must be provided.
        /// </summary>
        /// <param name="remote"></param>
        /// <param name="dst"></param>
        /// <param name="options"></param>
        /// <param name="caller">Required for log and progress origin</param>
        /// <returns></returns>
        /// <remarks>
        /// OnCheckoutProgress and OnTransferProgress will be overriden to invoke <see cref="Logging.OnAnyProgress"/>.
        /// OnProgress, RepositoryOperationStarting and RepositoryOperationCompleted will be overriden to log
        /// in the uppm Serilog system.
        /// </remarks>
        public static Repository Clone(string remote, string dst, CloneOptions options = null, ILogging caller = null)
        {
            options = options ?? new CloneOptions();
            var logger = caller?.Log ?? Logging.L;

            options.OnCheckoutProgress = (path, steps, totalSteps) =>
            {
                caller?.InvokeAnyProgress(totalSteps, steps, "Checking Out", path);
            };
            options.OnTransferProgress = progress =>
            {
                caller?.InvokeAnyProgress(progress.TotalObjects, progress.ReceivedObjects, "Transferring");
                return(true);
            };
            options.OnProgress = output =>
            {
                logger.Debug(output);
                return(true);
            };
            options.RepositoryOperationStarting  = RepoOperationStart(caller, "Cloning");
            options.RepositoryOperationCompleted = RepoOperationEnd(caller, "Cloning");

            try
            {
                var resultPath = Repository.Clone(remote, dst, options);
                return(new Repository(resultPath));
            }
            catch (Exception e)
            {
                logger.Fatal(e, "Error during cloning repository {RepoRemoteName}", remote);
            }
            return(null);
        }
Example #2
0
        /// <summary>
        /// Copy a directory recursively with either black~ or white listing.
        /// </summary>
        /// <param name="srcdir">Source directory</param>
        /// <param name="dstdir">Destination directory</param>
        /// <param name="ignore">Ignoring blacklist, can use wildcards</param>
        /// <param name="match">Matching whitelist, can use wildcards</param>
        /// <param name="caller">optional caller </param>
        public static void CopyDirectory(
            string srcdir, string dstdir,
            string[] ignore = null, string[] match = null,
            ILogging caller = null)
        {
            var logger = caller?.Log ?? Logging.L;

            logger.Information(
                "Copy started from {Src} to {Dst}",
                srcdir,
                dstdir
                );

            FileSystem.CopyDirectory(srcdir, dstdir, ignore, match, fsentry =>
            {
                caller.InvokeAnyProgress(
                    message: "Copying " + Path.GetDirectoryName(fsentry.FullName),
                    state: fsentry.Name
                    );
            });

            logger.Debug(
                "Copy ended from {Src} to {Dst}",
                srcdir,
                dstdir
                );
        }
Example #3
0
        /// <summary>
        /// Extract an archive which is supported by the SharpCompress library (i.e.: zip, 7z, rar, tar.bz2, etc...)
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dstdir"></param>
        /// <param name="caller"></param>
        public static void ExtractArchive(string src, string dstdir, ILogging caller = null)
        {
            var logger = caller?.Log ?? Logging.L;

            try
            {
                var archive = ArchiveFactory.Open(src, new ReaderOptions
                {
                    LookForHeader = true
                });

                var entries = archive.Entries.Where(entry => !entry.IsDirectory).ToList();
                var ii      = 0;

                foreach (var entry in entries)
                {
                    caller?.InvokeAnyProgress(entries.Count, ii, entry.Key, $"Extracting");
                    entry.WriteToDirectory(dstdir, new ExtractionOptions
                    {
                        ExtractFullPath = true,
                        Overwrite       = true
                    });
                    ii++;
                }
                archive.Dispose();
            }
            catch (Exception e)
            {
                logger.Error(e, "Error during extracting {Archive}", src);
            }
        }
Example #4
0
        /// <summary>
        /// Synchronize an already existing repository folder.
        /// </summary>
        /// <param name="caller">Required for log and progress origin</param>
        /// <param name="repofolder"></param>
        /// <param name="fetchops"></param>
        /// <param name="checkoutops"></param>
        /// <returns></returns>
        /// <remarks>
        /// OnCheckoutProgress and OnTransferProgress will be overriden to invoke <see cref="Logging.OnAnyProgress"/>.
        /// OnProgress, RepositoryOperationStarting and RepositoryOperationCompleted will be overriden to log
        /// in the uppm Serilog system.
        /// </remarks>
        public static Repository Synchronize(string repofolder, FetchOptions fetchops = null, CheckoutOptions checkoutops = null, ILogging caller = null)
        {
            var logger = caller?.Log ?? Logging.L;

            try
            {
                var repo = new Repository(repofolder);
                fetchops    = fetchops ?? new FetchOptions();
                checkoutops = checkoutops ?? new CheckoutOptions();

                fetchops.RepositoryOperationStarting  = RepoOperationStart(caller, "Fetching");
                fetchops.RepositoryOperationCompleted = RepoOperationEnd(caller, "Fetching");
                fetchops.OnTransferProgress           = progress =>
                {
                    caller?.InvokeAnyProgress(progress.TotalObjects, progress.ReceivedObjects, "Transferring");
                    return(true);
                };
                fetchops.OnProgress = output =>
                {
                    logger.Debug(output);
                    return(true);
                };
                checkoutops.OnCheckoutProgress = (path, steps, totalSteps) =>
                {
                    caller?.InvokeAnyProgress(totalSteps, steps, "Checking Out", path);
                };

                var remote   = repo.Network.Remotes["origin"];
                var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
                Commands.Fetch(repo, remote.Name, refSpecs, fetchops, "");
                Commands.Checkout(repo, "master", checkoutops);
                return(repo);
            }
            catch (Exception e)
            {
                logger.Error(e, "Error opening or checking out locally available repository. ({RepoUrl})", repofolder);
                return(null);
            }
        }
Example #5
0
        private static void DeleteDirectoryRec(
            string srcdir,
            bool recursive  = true,
            string[] ignore = null, string[] match = null,
            ILogging caller = null)
        {
            var logger = caller?.Log ?? Logging.L;

            if (recursive)
            {
                var subfolders = Directory.GetDirectories(srcdir);
                foreach (var s in subfolders)
                {
                    var name = Path.GetFileName(s);
                    if (ignore != null && ignore.Any(path => new WildcardPattern(path).IsMatch(name)))
                    {
                        continue;
                    }
                    if (match != null && !match.Any(path => new WildcardPattern(path).IsMatch(name)))
                    {
                        continue;
                    }

                    caller.InvokeAnyProgress(
                        message: "Deleting " + Path.GetDirectoryName(s),
                        state: Path.GetFileName(s)
                        );

                    DeleteDirectoryRec(s, true, ignore, match);
                }
            }

            var files = Directory.GetFiles(srcdir);

            foreach (var f in files)
            {
                var name = Path.GetFileName(f);
                if (ignore != null && ignore.Any(path => new WildcardPattern(path).IsMatch(name)))
                {
                    continue;
                }
                if (match != null && !match.Any(path => new WildcardPattern(path).IsMatch(name)))
                {
                    continue;
                }

                caller.InvokeAnyProgress(
                    message: "Deleting " + srcdir,
                    state: name
                    );

                try
                {
                    var attr = File.GetAttributes(f);
                    if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    {
                        File.SetAttributes(f, attr ^ FileAttributes.ReadOnly);
                    }
                    File.Delete(f);
                }
                catch (Exception e)
                {
                    logger.Error(
                        e,
                        "Error during deleting {Src}",
                        f
                        );
                }
            }

            try
            {
                Directory.Delete(srcdir);
            }
            catch (Exception e)
            {
                logger.Error(
                    e,
                    "Error during deleting {Src}",
                    srcdir
                    );
            }
        }