Пример #1
0
        private void bwUploadZip_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                var    now     = DateTime.Now;
                string pathTmp = _projModel.LibDebugPath;
                if (_projModel.ProjType == "Library")
                {
                    pathTmp = new DirectoryInfo(pathTmp).Parent?.FullName ?? pathTmp;
                }
                string zipFullPath = Path.Combine(pathTmp, _projModel.LibName + ".zip");

                if (bwUploadZip.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                bwUploadZip.ReportProgress(50, "压缩文件中...");
                ZipHelper.BatchZip(_projModel.LastChooseInfo.LastChoosePublishFiles, zipFullPath, _projModel.LastChooseInfo.LastChoosePublishDir, (progressValue) =>
                {
                    bwUploadZip.ReportProgress(50 + (int)(progressValue * 0.4), "压缩文件中...");
                    if (bwUploadZip.CancellationPending)
                    {
                        e.Cancel = true;
                        return(true);
                    }
                    return(false);
                });

                NameValueCollection dic = new NameValueCollection();
                dic.Add("Type", _projModel.ProjType == "Library" ? "iis" : "exe");
                dic.Add("AppId", e.Argument.ToString());

                if (bwUploadZip.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                bwUploadZip.ReportProgress(90, "文件上传中...");

                string url          = $"{PublishService.GetSettingPage().GetApiUrl()}/UploadZip";
                string uploadResStr = HttpHelper.HttpPostData(url, 30000, _projModel.LibName + ".zip", zipFullPath, dic);
                var    uploadRes    = uploadResStr.DeserializeObject <Result>();
                string msg          = uploadRes.IsSucceed ? "部署成功" : "部署失败:" + uploadRes.Message;
                var    timeSpan     = (DateTime.Now - now).TotalMilliseconds;
                bwUploadZip.ReportProgress(100, msg + ",耗时:" + timeSpan);
            }
            catch (Exception exception)
            {
                bwUploadZip.ReportProgress(0, $"发布失败:{exception.Message }");
            }
        }
Пример #2
0
        private void ucStep_IndexChecked(object sender, EventArgs e)
        {
            switch (this.ucStep.StepIndex)
            {
            case 1:
            {
                SetProcessVal(0);

                ControlHelper.ThreadRunExt(this, () =>
                    {
                        BuildProj();
                        ControlHelper.ThreadInvokerControl(this, () =>
                        {
                            SetProcessVal(100);
                            SetStep(2);
                        });
                    }, null, this, false);
            }
            break;

            case 2:
            {
                // 弹出选择文件窗口
                var fileForm = new SelectFilesForm();
                fileForm.Ini(_publishFilesDir, _selectedFileList, null);
                SelectFilesForm.FileSaveEvent = list =>
                {
                    _selectedFileList    = list;
                    this.ucStep.Steps[1] = $"(已选择{list?.Where(n => !n.EndsWith("pdb"))?.Count() ?? 0}个文件)";
                    this.ucStep.Refresh();
                    SetProcessVal(0);

                    // 打包文件
                    ControlHelper.ThreadRunExt(this, () =>
                        {
                            LogAppend("开始压缩选中的文件");
                            var zipPath = ProjectHelper.GetZipPath(_projectModel.ProjName);
                            ZipHelper.BatchZip(list, zipPath, _publishFilesDir, (progressValue) =>
                            {
                                SetProcessVal(progressValue);
                                return(false);
                            });

                            ControlHelper.ThreadInvokerControl(this, () =>
                            {
                                SetProcessVal(100);
                                LogAppend("文件压缩完成");
                                SetStep(3);
                            });
                        }, null, this, false);
                };

                var res = fileForm.ShowDialog();
            }
            break;

            case 3:
            {
                // 弹出选择服务器窗口
                var serviceForm = new ServiceForm();
                serviceForm.ShowDialog();
            }
            break;
            }
        }
Пример #3
0
        private void DeployStepOnIndexChecked(object sender, EventArgs e)
        {
            try
            {
                StepControl control = (StepControl)sender;
                switch (control.StepIndex)
                {
                case 1:
                    SetProcessVal(0);
                    if (_projectModel?.ProjType == 1)
                    {
                        _projectSetting = ProjectHelper.LoadProjectSettingInfo(this._projectModel.ProjName);
                        if (_projectModel.IsNetCore)
                        {
                            this.metroCbDeployType.Visible = true;
                            _isUpdatingDeployType          = true;
                            for (int i = 0; i < _deployTypesStr.Length; i++)
                            {
                                if (_deployTypesStr[i] == _projectSetting.DeployType)
                                {
                                    this.metroCbDeployType.SelectedIndex = i;
                                }
                            }

                            if (this.metroCbDeployType.SelectedIndex < 0)
                            {
                                this.metroCbDeployType.SelectedIndex = 0;
                            }
                            _isUpdatingDeployType = false;
                        }
                        else
                        {
                            this.metroCbDeployType.Visible = false;
                        }
                        ControlHelper.ThreadRunExt(this, () =>
                        {
                            bool isBuildSuccess = BuildProj();
                            if (!isBuildSuccess)
                            {
                                return;
                            }
                            SetProcessVal(100);
                            SetStepIndex(2);
                        }, null, this);
                    }
                    else
                    {
                        // 打开选择窗口,让人选择项目或者文件夹
                        SelectProjectForm form = new SelectProjectForm(_settingInfo);
                        form.Activate();
                        SelectProjectForm.ProjSelectedEvent = (item) =>
                        {
                            try
                            {
                                if (item.Type == 2)
                                {
                                    // c# 项目
                                    _projectModel   = ProjectHelper.ParseProject(item.Path);
                                    _projectSetting = ProjectHelper.LoadProjectSettingInfo(this._projectModel.ProjName);
                                    if (_projectModel.IsNetCore)
                                    {
                                        SetDeployTypeVisible(true);
                                        bool find = false;
                                        for (int i = 0; i < _deployTypesStr.Length; i++)
                                        {
                                            if (_deployTypesStr[i] == _projectSetting.DeployType)
                                            {
                                                find = true;
                                                SetDeployTypeIndex(i);
                                            }
                                        }

                                        if (!find)
                                        {
                                            SetDeployTypeIndex(0);
                                        }
                                    }
                                    else
                                    {
                                        SetDeployTypeVisible(false);
                                    }

                                    bool isBuildSuccess = BuildProj();
                                    SetProcessVal(100);
                                    if (!isBuildSuccess)
                                    {
                                        return;
                                    }
                                    SetStepIndex(2);
                                }
                                else
                                {
                                    _projectModel = new ProjectModel()
                                    {
                                        Key        = Guid.NewGuid().ToString(),
                                        OutPutType = string.Empty,
                                        ProjName   = $"{item.Name}{item.Guid}",
                                        ProjPath   = item.Path,
                                        ProjType   = item.Type,
                                    };
                                    _projectSetting = ProjectHelper.LoadProjectSettingInfo(this._projectModel.ProjName);
                                    SetDeployTypeVisible(false);
                                    _publishFilesDir = item.Path;
                                    SetStepIndex(2);
                                }
                            }
                            catch (Exception ex)
                            {
                                MetroMessageBox.Show(this, ex.Message, "项目选择处理错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        };
                        form.ShowDialog();
                    }

                    break;

                case 2:
                {
                    if (_publishFilesDir.IsNullOrEmpty())
                    {
                        MetroMessageBox.Show(this, "请先选择文件目录或者项目文件", "无法打开文件选择窗口", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        SetStepAsync(1);
                        return;
                    }
                    //_projectSetting = ProjectHelper.LoadProjectSettingInfo(this._projectModel.ProjName);
                    SelectFilesForm filesForm = new SelectFilesForm(_projectSetting, _publishFilesDir, _settingInfo);
                    filesForm.Activate();
                    SelectFilesForm.FileSaveEvent = list =>
                    {
                        try
                        {
                            // 打开发布服务器窗口
                            _projectSetting.SelectedFiles = list;
                            ProjectHelper.SaveProjectSettingInfo(_projectSetting);
                            SetSteps(1, $"(已选择{list?.Where(n => !n.EndsWith("pdb"))?.Count() ?? 0}个文件)");
                            SetProcessVal(0);
                            // 打包文件
                            LogAppend("开始压缩选中的文件");
                            _zipFilePath = ProjectHelper.GetZipPath(_projectModel.ProjName);
                            ZipHelper.BatchZip(list, _zipFilePath, _publishFilesDir, (progressValue) =>
                                {
                                    SetProcessVal(progressValue);
                                    return(false);
                                });
                            SetProcessVal(100);
                            LogAppend("文件压缩完成");
                            SetStepIndex(3);
                        }
                        catch (Exception ex)
                        {
                            MetroMessageBox.Show(this, ex.Message, "文件处理错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            _zipFilePath = string.Empty;
                        }
                    };
                    filesForm.ShowDialog();
                }
                break;

                case 3:
                {
                    if (_projectSetting == null)
                    {
                        MetroMessageBox.Show(this, "请选择文件目录或者项目文件", "无法打开发布到服务器窗口", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        SetStepAsync(1);
                        return;
                    }

                    if (_zipFilePath.IsNullOrEmpty())
                    {
                        MetroMessageBox.Show(this, "重新选择需要发布的文件进行压缩", "未找到压缩文件", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        SetStepAsync(2);
                        return;
                    }
                    ServiceForm form = new ServiceForm(_projectModel, _projectSetting, _settingInfo);
                    form.Activate();
                    ServiceForm.ServiceSelectedEvent = (type, appId, serviceInfo) =>
                    {
                        try
                        {
                            SetProcessVal(20, true);
                            LogAppend("开始上传文件");
                            ApiHelper.UploadZipFile(_settingInfo, serviceInfo, type, appId, _zipFilePath);
                            SetProcessVal(100);
                            LogAppend("文件部署完成");
                        }
                        catch (Exception ex)
                        {
                            MetroMessageBox.Show(this, ex.Message, "发布到服务器错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    };
                    form.ShowDialog();
                }
                break;
                }
            }
            catch (Exception exception)
            {
                //TxtLogService.WriteLog(exception, SettingHelper.GetLogDirPath(), "步骤执行失败");
                LogAppend($"步骤执行失败:{exception.Message}", Color.Red);
                MetroMessageBox.Show(this, exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }