示例#1
0
        private bool Replace(string file, Dictionary <string, string> dic)
        {
            Watch?.Info($"开始替换文件: {file}");
            if (File.Exists(file))
            {
                try
                {
                    var content = File.ReadAllText(file);
                    foreach (var pair in dic)
                    {
                        content = content.Replace(pair.Key, pair.Value);
                    }

                    File.WriteAllText(file, content);
                    Watch?.Info("替换成功。");
                    return(true);
                }
                catch (Exception ex)
                {
                    Watch?.Error("替换失败:" + ex.Message);
                    return(false);
                }
            }

            Watch?.Error("替换失败:文件不存在。");
            return(false);
        }
示例#2
0
        private bool ExtractionFile(ZipArchiveEntry entry, FileInfo info, FileOverride fileOverride, ExtractionOptions extractionOptions)
        {
            var isExist = info.Exists;

            if (fileOverride == FileOverride.Never && isExist)
            {
                Watch?.Info($"文件已存在 [{info.FullName}]。");
                return(true);
            }
            if (fileOverride == FileOverride.Newer && isExist && entry.LastModifiedTime <= info.LastWriteTime)
            {
                Watch?.Info($"文件已存在 [{info.FullName}]。");
                return(true);
            }

            bool retry;

            do
            {
                retry = false;
                try
                {
                    entry.WriteToFile(info.FullName, extractionOptions);
                    if (!isExist)
                    {
                        newFiles.Add(info.FullName);
                    }
                    return(true);
                }
                catch (Exception e)
                {
                    if (fileOverride == FileOverride.Try)
                    {
                        Watch?.Info($"覆盖文件失败,自动忽略并继续。失败原因:{e.Message}");
                        return(true);
                    }
                    var dr = Helper.AbortRetryIgnore(this.Owner, $"覆盖文件失败,是否重试?失败原因:{Environment.NewLine}{e.Message}");
                    if (dr == DialogResult.Abort)
                    {
                        Watch?.Error($"覆盖文件失败,失败原因:{e.Message}");
                        Watch?.Faild();
                        return(false);
                    }
                    if (dr == DialogResult.Retry)
                    {
                        Watch?.Info($"覆盖文件失败,正在重试。失败原因:{e.Message}");
                        retry = true;
                    }
                    else if (dr == DialogResult.Ignore)
                    {
                        Watch?.Info($"覆盖文件失败,忽略该操作。失败原因:{e.Message}");
                        return(true);
                    }
                }
            } while (retry);

            return(true);
        }
示例#3
0
        private bool UnZip(string tarFile, List <FileItem> files, string installPath)
        {
            using (var archive = ZipArchive.Open(tarFile, Compress.ReaderOptions))
            {
                try
                {
                    var               entries = archive.Entries.ToDictionary(x => x.Key.Replace("/", "\\"));
                    ZipArchiveEntry   entry;
                    ExtractionOptions extractionOptions = new ExtractionOptions
                    {
                        ExtractFullPath  = true,
                        Overwrite        = true,
                        PreserveFileTime = true
                    };
                    if (entries.ContainsKey(Context.UninstallerKey))
                    {
                        entry = entries[Context.UninstallerKey];
                        var path = Path.Combine(installPath, Context.UninstallerKey);
                        entry.WriteToFile(path, extractionOptions);
                    }
                    foreach (var file in files)
                    {
                        if (entries.TryGetValue(file.Key, out entry))
                        {
                            Watch?.Info($"抽取 [{file.Name}]");
                            var path = file.Path;
                            FileHelper.RemoveReadonly(path);
                            var info = new FileInfo(path);
                            if (!Directory.Exists(info.Directory.FullName))
                            {
                                Directory.CreateDirectory(info.Directory.FullName);
                                newDirs.Push(info.Directory.FullName);
                            }

                            var flag = ExtractionFile(entry, info, file.FileOverride, extractionOptions);
                            if (!flag)
                            {
                                return(false);
                            }
                        }

                        Watch?.AddValue(1);
                    }
                    return(true);
                }
                catch (Exception e)
                {
                    Watch?.Error(e.Message);
                    return(false);
                }
            }
        }
示例#4
0
        private bool Start(string name, string args)
        {
            try
            {
                var     iserror = false;
                Process process = new Process
                {
                    StartInfo =
                    {
                        WorkingDirectory       = AppDomain.CurrentDomain.BaseDirectory,
                        FileName               = name,
                        CreateNoWindow         = true,
                        UseShellExecute        = false,
                        RedirectStandardInput  = true,
                        RedirectStandardOutput = true,
                        RedirectStandardError  = true,
                    }
                };
                if (!string.IsNullOrEmpty(args))
                {
                    process.StartInfo.Arguments = args;
                }
                process.Start();

                var output = process.StandardOutput.ReadToEnd();
                if (string.IsNullOrEmpty(output))
                {
                    Watch?.Info("执行成功。");
                }
                else
                {
                    iserror = errorRegex.IsMatch(output) || errorRegex2.IsMatch(output);
                    if (iserror)
                    {
                        Watch?.Error(output);
                    }
                    else
                    {
                        Watch?.Info(output);
                    }
                }
                process.WaitForExit();
                process.Close();
                return(!iserror);
            }
            catch (Exception ex)
            {
                Watch?.Error("脚本执行错误:" + ex.Message);
                return(false);
            }
        }
示例#5
0
        private bool RestoreBackup(string backupName)
        {
            Watch?.Info("正在还原IIS配置");
            var result = DoCmd(string.Format(RESTORE_BACKUP, backupName));
            var rst    = !error.IsMatch(result);

            if (rst)
            {
                return(true);
            }

            Watch?.Error($"还原IIS配置失败:{result}");
            return(false);
        }
示例#6
0
        private bool SetSitePool(string siteName, string poolName)
        {
            Watch?.Info($"设置网站应用程序池 {siteName} -> {poolName}");
            var result = DoCmd(string.Format(SET_SITE_POOL, siteName, poolName));
            var rst    = !error.IsMatch(result);

            if (rst)
            {
                return(true);
            }

            Watch?.Error($"设置网站应用程序池 {siteName} -> {poolName} 失败:{result}");
            return(false);
        }
示例#7
0
        private bool StartSite(string siteName, string site)
        {
            Watch?.Info($"启动网站 {siteName}");
            var result = DoCmd(string.Format(START_SITE, siteName));
            var rst    = !error.IsMatch(result);

            if (rst)
            {
                Watch?.Info($"启动网站 {siteName} 成功:{result}");
                return(true);
            }

            Watch?.Error($"启动网站 {siteName} 失败:{result}");
            return(false);
        }
示例#8
0
        private bool ExecuteFile(string file, out string message)
        {
            var statements = sqlHelper.ParseCommands(file);

            foreach (var statement in statements)
            {
                if (!sqlHelper.TryExecuteNonQuery(statement, out message))
                {
                    Watch?.Error(message.Trim());
                    return(false);
                }
            }

            message = null;
            return(true);
        }
示例#9
0
        private bool MakeAppPool(string poolName)
        {
            Watch?.Info($"检查应用程序池 {poolName} 是否存在");
            var result = DoCmd(string.Format(LIST_POOL, poolName));

            if (!result.Contains(poolName))
            {
                Watch?.Info($"正在添加应用程序池 {poolName}");
                result = DoCmd(string.Format(ADD_POOL, poolName));
                var rst = !error.IsMatch(result);
                if (rst)
                {
                    return(true);
                }

                Watch?.Error($"添加应用程序池 {poolName} 失败:{result}");
                return(false);
            }

            Watch?.Info($"应用程序池 {poolName} 已存在");
            return(true);
        }
示例#10
0
        private bool MakeSite(string siteName, string logicPath, string bindings)
        {
            Watch?.Info($"检查网站 {siteName} 是否存在");
            var result = DoCmd(string.Format(LIST_SITE, siteName));

            if (!result.Contains(siteName))
            {
                Watch?.Info($"添加网站 {siteName}");
                result = DoCmd(string.Format(ADD_SITE, siteName, logicPath, bindings));
                var rst = !error.IsMatch(result);
                if (rst)
                {
                    return(true);
                }

                Watch?.Error($"添加网站 {siteName} 失败:{result}");
                return(false);
            }

            Watch?.Error($"网站 {siteName} 已存在");
            return(false);
        }
示例#11
0
        private bool TryExecuteFile(ScriptFile file)
        {
            bool retry;

            do
            {
                var flag = ExecuteFile(file.File, out string message);
                if (flag)
                {
                    file.Status = ScriptStatus.Success;
                    return(true);
                }

                retry = false;
                var dr = Helper.AbortRetryIgnore(this.Owner, $"执行脚本失败,是否重试?失败原因:{Environment.NewLine}{message}");
                if (dr == DialogResult.Abort)
                {
                    Watch?.Error($"执行脚本失败,失败原因:{message}");
                    Watch?.Faild();
                    file.Status = ScriptStatus.Failed;
                    return(false);
                }

                if (dr == DialogResult.Retry)
                {
                    Watch?.Info("重试该脚本。");
                    retry = true;
                }
                else if (dr == DialogResult.Ignore)
                {
                    Watch?.Info("忽略该脚本。");
                    file.Status = ScriptStatus.Ignore;
                    return(true);
                }
            } while (retry);

            return(true);
        }