Ejemplo n.º 1
0
        public static void DeleteDirectory(string path)
        {
            if (String.IsNullOrWhiteSpace(path))
            {
                return;
            }

            var retries = 5;

            while (Directory.Exists(path))
            {
                try
                {
                    GenLog.Info($"Deleting directory: {path}...");
                    Directory.Delete(path, true);
                }
                catch (Exception e) when(e is IOException && retries-- >= 0)
                {
                    GenLog.Warning("Unable to delete directory. Will retry...");
                    Thread.Sleep(3000);
                }
                catch (UnauthorizedAccessException) when(retries-- >= 0)
                {
                    GenLog.Warning("Unable to delete directory. Will attempt to remove read-only files...");
                    DeleteReadOnlyDirectory(path);
                }
            }
        }
Ejemplo n.º 2
0
        private static void DeleteDirectory(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return;
            }

            var retries = 10;

            while (Directory.Exists(path))
            {
                try
                {
                    GenLog.Info($"Deleting directory: {path}...");
                    Directory.Delete(path, true);
                }
                catch (IOException e) when(retries-- >= 0)
                {
                    GenLog.Warning("Unable to delete directory. Will retry...");
                    GenLog.Warning(e.Message);
                    Thread.Sleep(5000);
                }
                catch (UnauthorizedAccessException) when(retries-- >= 0)
                {
                    GenLog.Warning("Unable to delete directory. Will attempt to remove read-only files...");
                    RemoveReadOnlyAttributes(path);
                }
            }
        }
Ejemplo n.º 3
0
        public static PackageDefinition GetPackageDefinition(string packageId, string versionRaw)
        {
            try
            {
                var version = NuGetVersion.Parse(versionRaw);
                return(new PackageDefinition(packageId, version));
            }
            catch (Exception)
            {
                GenLog.Warning($"Ignoring unparsable version for {packageId}: {versionRaw}");
            }

            return(null);
        }
Ejemplo n.º 4
0
        public static async Task OnExceptionAsync(
            [InstantHandle] Func <Task> action, string introMessage, CancellationToken cancellationToken, int numRetries = 3, int delay = 3000)

        {
            if (numRetries < 0)
            {
                numRetries = 0;
            }

            while (numRetries-- > 0 && !cancellationToken.IsCancellationRequested)
            {
                try
                {
                    if (!introMessage.IsEmpty())
                    {
                        GenLog.Info(introMessage);
                    }
                    await action();

                    return;
                }
                catch (FatalException e)
                {
                    GenLog.Error($"Aborting due to fatal exception: {e?.InnerException?.Message}");
                    throw;
                }
                catch (Exception e)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        GenLog.Warning("Cancelling retry-able operation");
                        throw;
                    }

                    GenLog.Warning("Caught exception during retry-able operation:");
                    GenLog.Warning(e.Message);
                    if (numRetries == 0)
                    {
                        GenLog.Error("No more retries left");
                        throw;
                    }
                    GenLog.Info($"Retries remaining: {numRetries}");
                    await Task.Delay(delay, cancellationToken);

                    delay *= 2;
                }
            }
        }
Ejemplo n.º 5
0
        private static void DeleteReadOnlyDirectory(string path)
        {
            var retries = 5;

            while (Directory.Exists(path))
            {
                try
                {
                    foreach (var s in Directory.GetDirectories(path))
                    {
                        DeleteReadOnlyDirectory(s);
                    }

                    foreach (var f in Directory.GetFiles(path))
                    {
                        while (File.Exists(f))
                        {
                            try
                            {
                                var attr = File.GetAttributes(f);
                                if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                {
                                    File.SetAttributes(f, attr ^ FileAttributes.ReadOnly);
                                }

                                File.Delete(f);
                                break;
                            }
                            catch (IOException) when(retries-- >= 0)
                            {
                                GenLog.Warning($"Unable to delete {path}. Will retry...");
                                Thread.Sleep(1000);
                            }
                        }
                    }

                    Directory.Delete(path, true);
                    break;
                }
                catch (IOException) when(retries-- >= 0)
                {
                    GenLog.Warning($"Unable to delete {path}. Will retry...");
                    Thread.Sleep(2000);
                }
            }
        }
Ejemplo n.º 6
0
        public static void WriteToFileSafely(string path, string[] content)
        {
            var retries = 4;

            while (true)
            {
                try
                {
                    File.WriteAllLines(path, content);
                    break;
                }
                catch (IOException) when(retries-- >= 0)
                {
                    GenLog.Warning($"Unable to write to {path}. Will retry...");
                    Thread.Sleep(3000);
                }
            }
        }
Ejemplo n.º 7
0
        // Searches local dir PATH environment for command 'name' and returns absolute path to same
        // if found, otherwise null
        public static string GetExecutableInPath(string name)
        {
            if (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(name)))
            {
                if (File.Exists(name))
                {
                    return(name);
                }

                throw new ConfigurationException(
                          $"Argument to GetExecutableInPath should be a filename. '{name}' does not exist.");
            }

            if (string.IsNullOrWhiteSpace(Path.GetExtension(name)))
            {
                // Iterate over platform specific extensions; cmd, bat, ps1, sh, <none>
                name += ".exe";
            }

            GenLog.Info($"Looking for command {name}");

            string DoSearch()
            {
                if (File.Exists(name))
                {
                    return(Path.Combine(Directory.GetCurrentDirectory(), name));
                }

                var fileInScriptDir = Path.Combine(GetScriptDir(), name);

                if (File.Exists(fileInScriptDir))
                {
                    return(fileInScriptDir);
                }

                var dirsInPath = Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator);

                if (dirsInPath == null || dirsInPath.Length == 0)
                {
                    GenLog.Warning("PATH environment variable is empty");
                    return(null);
                }

                foreach (var dir in dirsInPath)
                {
                    var fileInDir = Path.Combine(dir, name);
                    if (File.Exists(fileInDir))
                    {
                        return(fileInDir);
                    }
                }

                return(null);
            }

            var result = DoSearch();

            if (string.IsNullOrWhiteSpace(result))
            {
                return(null);
            }

            GenLog.Info($"Found at {result}");
            return(result);
        }