123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- #include "StdAfx.h"
- #include "SVNProc.h"
- #include <afxinet.h>
- #include "CharEncoding.h"
- IMPLEMENT_SERIAL(CSVNProc, CObject, 0)
- CSVNProc::CSVNProc(void) :CObject()
- {
- m_bNeedUpdate = FALSE;
- m_strSVNAddress = _T("");
- m_strSVNSavePath = _T("");
- m_dwSVNVersion = 0;
- m_svnpath = PATH_RES;
- }
- CSVNProc::~CSVNProc(void)
- {
- }
- CSVNProc& CSVNProc::operator=(CSVNProc& obj)
- {
- // TODO: 在此处插入 return 语句
- m_bNeedUpdate = obj.m_bNeedUpdate;
- m_strSVNAddress = obj.m_strSVNAddress;
- m_strSVNSavePath = obj.m_strSVNSavePath;
- m_dwSVNVersion = obj.m_dwSVNVersion;
- return *this;
- }
- void CSVNProc::Serialize(CArchive& ar)
- {
- CObject::Serialize(ar);
- // 读写判断
- if(ar.IsStoring())
- {// 保存;
- ar << m_bNeedUpdate;
- ar << m_strSVNAddress;
- ar << m_strSVNSavePath;
- ar << m_dwSVNVersion;
- }
- else
- {// 加载;
- ar >> m_bNeedUpdate;
- ar >> m_strSVNAddress;
- ar >> m_strSVNSavePath;
- ar >> m_dwSVNVersion;
- }
- }
- void CSVNProc::SVNProcess(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion, SVNTYPE type)
- {
- TCHAR szFileName[MAX_PATH] = { 0 };
- TCHAR szCommand[MAX_PATH] = { 0 };
- // 删除之前的;
- _stprintf_s(szFileName, _T("%ld"), m_svnpath);
- DeleteFile(szFileName);
- if ( type == SVN_EXPORT ) {
- // 先删除要导出的路径;
- _stprintf_s(szCommand, _T("/c rd /s /q %s"), strSavePath.c_str());
- ShellExecute(NULL, _T("open"), _T("cmd"), szCommand, NULL, SW_HIDE);
- // 再执行导出;
- //_stprintf_s(szCommand, _T("/c svn export -r %d %s %s > data.jg &pause"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str());
- _stprintf_s(szCommand, _T("/c svn export -r %d %s %s > %d"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str(), m_svnpath);
- }
- else if ( type == SVN_CHECKOUT ) {
- // 先删除要导出的路径;
- _stprintf_s(szCommand, _T("/c rd /s /q %s"), strSavePath.c_str());
- ShellExecute(NULL, _T("open"), _T("cmd"), szCommand, NULL, SW_HIDE);
- // 再执行导出;
- //_stprintf_s(szCommand, _T("/c svn checkout -r %d %s %s > data.jg &pause"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str());
- _stprintf_s(szCommand, _T("/c svn checkout -r %d %s %s > %d"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str(), m_svnpath);
- }
- else if ( type == SVN_ROLLBACK ) {
- //_stprintf_s(szCommand, _T("/c svn update -r %d %s > data.jg &pause"), dwSVNVersion, strSavePath.c_str());
- _stprintf_s(szCommand, _T("/c svn update -r %d %s > %d"), dwSVNVersion, strSavePath.c_str(), m_svnpath);
- }
- SHELLEXECUTEINFO sei;
- memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
- sei.cbSize = sizeof(SHELLEXECUTEINFO);
- sei.hwnd = NULL;
- sei.lpVerb = _T("open");
- sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
- sei.lpFile = "cmd";
- sei.lpParameters = szCommand;
- sei.lpDirectory = NULL;
- sei.nShow = SW_HIDE;
- sei.hInstApp = NULL;
- if (!ShellExecuteEx(&sei)) {
- return;
- }
- if (sei.hProcess != NULL) {
- WaitForSingleObject(sei.hProcess, INFINITE);
- if (sei.hProcess)
- CloseHandle(sei.hProcess);
- }
-
- // 结束后进入文本结果识别;
- CString str;
- CStdioFile file;
- if (file.Open(szFileName, CFile::modeRead)) {
- // 文件大小;
- ULONGLONG ulSize = file.SeekToEnd();
- file.SeekToBegin();
- if ( ulSize > 60)
- file.Seek(ulSize - 60, CFile::begin);
- while (file.GetPosition() != ulSize) {
- file.ReadString(str);
- }
- // 关闭文件;
- file.Close();
- }
- // 目前只针对更新指定版本;
- TCHAR szCommand2[MAX_PATH] = { 0 };
- _stprintf_s(szCommand, _T("At revision %ld."), dwSVNVersion);
- _stprintf_s(szCommand2, _T("Updated to revision %ld."), dwSVNVersion);
- if (!str.IsEmpty() && (str.CompareNoCase(szCommand) == 0 || str.CompareNoCase(szCommand2) == 0)) {
- // 表示更新成功;
- m_bNeedUpdate = FALSE;
- }
- }
- void CSVNProc::Export(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
- {
- SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_EXPORT);
- }
- void CSVNProc::Checkout(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
- {
- SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_CHECKOUT);
- }
- void CSVNProc::Rollback(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
- {
- SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_ROLLBACK);
- }
- BOOL CSVNMgr::ParseJson(std::string json)
- {
- cJSON* pRoot = cJSON_Parse(json.c_str());
- if (pRoot) {
- cJSON* pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
- if (pMsg) {
- std::string data;
- std::string code = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
- std::string msg = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
- if (code == "00") {
- cJSON* pObj = cJSON_GetObjectItem(pMsg, "data");
- if (pObj != NULL) {
- pObj = cJSON_GetObjectItem(pObj, "svnVersionArray");
- int nSise = cJSON_GetArraySize(pObj);
- for (int i = 0; i < nSise; i++) {
- cJSON* pInfo = cJSON_GetArrayItem(pObj, i);
- if (pInfo) {
- std::string type = cJSON_GetObjectItem(pInfo, "type") ? cJSON_GetObjectItem(pInfo, "type")->valuestring : "";
- std::string path = cJSON_GetObjectItem(pInfo, "svnPath") ? cJSON_GetObjectItem(pInfo, "svnPath")->valuestring : "";
- std::string version = cJSON_GetObjectItem(pInfo, "svnVersion") ? cJSON_GetObjectItem(pInfo, "svnVersion")->valuestring : "";
- CSVNProc* pSVN = NULL;
- // 查找该地址是否存在;
- if ((pSVN = IsSVNExist(path)) != NULL) {
- UpdatedSVN(pSVN, _tstol(version.c_str()));
- }
- else
- {
- pSVN = new CSVNProc();
- pSVN->m_bNeedUpdate = TRUE;
- pSVN->m_strSVNAddress = path.c_str();
- pSVN->m_dwSVNVersion = _tstol(version.c_str());
- if (type == "SatTest") {
- pSVN->m_svnpath = PATH_TESTER;
- pSVN->m_strSVNSavePath = _T("D:\\SAT\\Tester");
- }
- else if (type == "SatScript") {
- pSVN->m_svnpath = PATH_SCRIPT;
- pSVN->m_strSVNSavePath = _T("D:\\SAT\\Script");
- }
- else if (type == "SatResource") {
- pSVN->m_svnpath = PATH_RES;
- pSVN->m_strSVNSavePath = _T("D:\\SAT\\resource");
- }
- else {
- delete pSVN;
- pSVN = NULL;
- }
-
- // 加入数组;
- if ( pSVN )
- m_arySVN.Add(pSVN);
- }
- }
- }
- }
- // 释放内存;
- cJSON_Delete(pRoot);
- // 返回结果;
- return TRUE;
- }
- }
- // 错误产生,可输出msg方便查询;
- cJSON_Delete(pRoot);
- }
- return FALSE;
- }
- BOOL CSVNMgr::HttpRequest(std::string url, std::string strContent, std::string& data)
- {
- // 分解地址;
- DWORD dwServiceType;
- CString strServer;
- CString strObject;
- INTERNET_PORT nPort;
- AfxParseURL(url.c_str(), dwServiceType, strServer, strObject, nPort);
- if ((AFX_INET_SERVICE_HTTP != dwServiceType) && (AFX_INET_SERVICE_HTTPS != dwServiceType)) {
- return FALSE;
- }
- // 设置超时
- CInternetSession session(_T("HTTP Session"));;
- session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000);
- session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
- session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 10000);
- session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 20000);
- // 打开HTTP连接
- CHttpConnection* pHttpConnection = session.GetHttpConnection(strServer, dwServiceType == AFX_INET_SERVICE_HTTP ? INTERNET_FLAG_KEEP_CONNECTION : INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_SECURE, nPort);
- if (NULL == pHttpConnection) {
- return FALSE;
- }
- // 开启一个HTTP请求
- url.append("!" + strContent);
- url = CharEncoding::EnCode_UTF8URL(url.c_str());
- CHttpFile* pHttpFile = pHttpConnection->OpenRequest(_T("POST"), strObject, NULL, 1, NULL, NULL, dwServiceType == AFX_INET_SERVICE_HTTP ? INTERNET_FLAG_KEEP_CONNECTION : INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_SECURE);
- if (NULL == pHttpFile) {
- return FALSE;
- }
- // 设置HTTP请求包头
- std::string output;
- TCHAR szHeaders[MAX_PATH] = { 0 };
- pHttpFile->AddRequestHeaders(_T("User-Agent: PostmanRuntime/7.24.1"));
- pHttpFile->AddRequestHeaders(_T("Accept: */*"));
- pHttpFile->AddRequestHeaders(_T("Accept-Encoding: gzip, deflate, br"));
- pHttpFile->AddRequestHeaders(_T("Connection: keep-alive"));
- pHttpFile->AddRequestHeaders(_T("Content-Type: application/json;charset=utf-8"));
- pHttpFile->AddRequestHeaders(_T("Cache-Control: no-cache"));
- pHttpFile->AddRequestHeaders(_T("Accept-Language: zh-cn"));
- _stprintf_s(szHeaders, _T("Host:%s"), strServer.GetString());
- pHttpFile->AddRequestHeaders(szHeaders);
- pHttpFile->AddRequestHeaders(_T("Content-Length:0"));
- // 发送数据
- BOOL bResult = pHttpFile->SendRequest(NULL, 0, NULL, 0);
- if (!bResult) {
- return FALSE;
- }
- // 查询状态
- DWORD dwHttpCode = 0;
- bResult = pHttpFile->QueryInfoStatusCode(dwHttpCode);
- if (!bResult) {
- return FALSE;
- }
- // 出错的原因
- char szBuffer[4096] = { 0 };
- DWORD dwBufferSize;
- if ((dwHttpCode < 200) || (dwHttpCode >= 300)) {
- BOOL bResult = pHttpFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, szBuffer, &dwBufferSize);
- return FALSE;
- }
- // 接收响应
- DWORD dwReadBytes;
- while ((dwReadBytes = pHttpFile->Read((void*)szBuffer, 4096)) > 0) {
- data.append(szBuffer, dwReadBytes);
- memset(szBuffer, 0, 4096 * sizeof(char));
- }
- // 释放资源
- if (NULL != pHttpFile) {
- pHttpFile->Close();
- delete pHttpFile;
- pHttpFile = NULL;
- }
- if (NULL != pHttpConnection) {
- pHttpConnection->Close();
- delete pHttpConnection;
- pHttpConnection = NULL;
- }
- session.Close();
- data = CharEncoding::DeCode_URLUTF8(data.c_str());
- TRACE1(_T("请求结果:%s"), data.c_str());
- return TRUE;
- }
- CSVNProc* CSVNMgr::IsSVNExist(std::string strSVNAddress)
- {
- CSVNProc* pSVN = NULL;
- int nSize = m_arySVN.GetSize();
- for ( int i = 0; i < nSize; i++ ) {
- pSVN = m_arySVN.GetAt(i);
- if (pSVN) {
- if (pSVN->m_strSVNAddress.CompareNoCase(strSVNAddress.c_str()) == 0)
- return pSVN;
- }
- }
- return NULL;
- }
- void CSVNMgr::UpdatedSVN(CSVNProc* pSVN, DWORD dwVersion)
- {
- if (pSVN->m_dwSVNVersion != dwVersion) {
- pSVN->m_dwSVNVersion = dwVersion;
- pSVN->m_bNeedUpdate = TRUE;
- //pSVN->Rollback(pSVN->m_strSVNAddress.GetString(), pSVN->m_strSVNSavePath.GetString(), pSVN->m_dwSVNVersion);
- }
- }
- BOOL CSVNMgr::HasNewVersion()
- {
- CSVNProc* pSVN = NULL;
- int nSize = m_arySVN.GetSize();
- if (nSize == 0)
- return FALSE;
- for (int i = 0; i < nSize; i++) {
- pSVN = m_arySVN.GetAt(i);
- if (pSVN) {
- if (pSVN->m_bNeedUpdate)
- return TRUE;
- }
- }
- return FALSE;
- }
- void CSVNMgr::Load(LPCTSTR lpFileName)
- {
- CFile file;
- if (file.Open(lpFileName, CFile::modeRead)) {
- CArchive ar(&file, CArchive::load);
- m_arySVN.Serialize(ar);
- ar.Close();
- file.Close();
- }
- }
- void CSVNMgr::Store(LPCTSTR lpFileName)
- {
- CFile file;
- if (file.Open(lpFileName, CFile::modeWrite | CFile::modeCreate)) {
- CArchive ar(&file, CArchive::store);
- m_arySVN.Serialize(ar);
- ar.Flush();
- ar.Close();
- file.Flush();
- file.Close();
- }
- }
- CString CSVNMgr::GetVersion()
- {
- CSVNProc* pSVN = NULL;
- int nSize = m_arySVN.GetSize();
- if (nSize == 0)
- return CString("【获取版本失败!】");
- CString strVersion;
- strVersion.Format(_T("%ld"), m_arySVN.GetAt(0)->m_dwSVNVersion);
- return strVersion;
- }
- BOOL CSVNMgr::CheckNewVersion(std::string url, std::string strContent)
- {
- std::string data;
- if (!HttpRequest(url, strContent, data))
- return FALSE;
- // 将结果转换为结构体;
- ParseJson(data);
- return HasNewVersion();
- }
- void CSVNMgr::Update()
- {
- CSVNProc* pSVN = NULL;
- int nSize = m_arySVN.GetSize();
- for (int i = 0; i < nSize; i++) {
- pSVN = m_arySVN.GetAt(i);
- if (pSVN) {
- if (pSVN->m_bNeedUpdate)
- pSVN->Rollback(pSVN->m_strSVNAddress.GetString(), pSVN->m_strSVNSavePath.GetString(), pSVN->m_dwSVNVersion);
- }
- }
- }
|