SVNProc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include "StdAfx.h"
  2. #include "SVNProc.h"
  3. #include <afxinet.h>
  4. #include "CharEncoding.h"
  5. IMPLEMENT_SERIAL(CSVNProc, CObject, 0)
  6. CSVNProc::CSVNProc(void) :CObject()
  7. {
  8. m_bNeedUpdate = FALSE;
  9. m_strSVNAddress = _T("");
  10. m_strSVNSavePath = _T("");
  11. m_dwSVNVersion = 0;
  12. m_svnpath = PATH_RES;
  13. }
  14. CSVNProc::~CSVNProc(void)
  15. {
  16. }
  17. CSVNProc& CSVNProc::operator=(CSVNProc& obj)
  18. {
  19. // TODO: 在此处插入 return 语句
  20. m_bNeedUpdate = obj.m_bNeedUpdate;
  21. m_strSVNAddress = obj.m_strSVNAddress;
  22. m_strSVNSavePath = obj.m_strSVNSavePath;
  23. m_dwSVNVersion = obj.m_dwSVNVersion;
  24. return *this;
  25. }
  26. void CSVNProc::Serialize(CArchive& ar)
  27. {
  28. CObject::Serialize(ar);
  29. // 读写判断
  30. if(ar.IsStoring())
  31. {// 保存;
  32. ar << m_bNeedUpdate;
  33. ar << m_strSVNAddress;
  34. ar << m_strSVNSavePath;
  35. ar << m_dwSVNVersion;
  36. }
  37. else
  38. {// 加载;
  39. ar >> m_bNeedUpdate;
  40. ar >> m_strSVNAddress;
  41. ar >> m_strSVNSavePath;
  42. ar >> m_dwSVNVersion;
  43. }
  44. }
  45. void CSVNProc::SVNProcess(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion, SVNTYPE type)
  46. {
  47. TCHAR szFileName[MAX_PATH] = { 0 };
  48. TCHAR szCommand[MAX_PATH] = { 0 };
  49. // 删除之前的;
  50. _stprintf_s(szFileName, _T("%ld"), m_svnpath);
  51. DeleteFile(szFileName);
  52. if ( type == SVN_EXPORT ) {
  53. // 先删除要导出的路径;
  54. _stprintf_s(szCommand, _T("/c rd /s /q %s"), strSavePath.c_str());
  55. ShellExecute(NULL, _T("open"), _T("cmd"), szCommand, NULL, SW_HIDE);
  56. // 再执行导出;
  57. //_stprintf_s(szCommand, _T("/c svn export -r %d %s %s > data.jg &pause"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str());
  58. _stprintf_s(szCommand, _T("/c svn export -r %d %s %s > %d"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str(), m_svnpath);
  59. }
  60. else if ( type == SVN_CHECKOUT ) {
  61. // 先删除要导出的路径;
  62. _stprintf_s(szCommand, _T("/c rd /s /q %s"), strSavePath.c_str());
  63. ShellExecute(NULL, _T("open"), _T("cmd"), szCommand, NULL, SW_HIDE);
  64. // 再执行导出;
  65. //_stprintf_s(szCommand, _T("/c svn checkout -r %d %s %s > data.jg &pause"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str());
  66. _stprintf_s(szCommand, _T("/c svn checkout -r %d %s %s > %d"), dwSVNVersion, strSVNAddress.c_str(), strSavePath.c_str(), m_svnpath);
  67. }
  68. else if ( type == SVN_ROLLBACK ) {
  69. //_stprintf_s(szCommand, _T("/c svn update -r %d %s > data.jg &pause"), dwSVNVersion, strSavePath.c_str());
  70. _stprintf_s(szCommand, _T("/c svn update -r %d %s > %d"), dwSVNVersion, strSavePath.c_str(), m_svnpath);
  71. }
  72. SHELLEXECUTEINFO sei;
  73. memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
  74. sei.cbSize = sizeof(SHELLEXECUTEINFO);
  75. sei.hwnd = NULL;
  76. sei.lpVerb = _T("open");
  77. sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
  78. sei.lpFile = "cmd";
  79. sei.lpParameters = szCommand;
  80. sei.lpDirectory = NULL;
  81. sei.nShow = SW_HIDE;
  82. sei.hInstApp = NULL;
  83. if (!ShellExecuteEx(&sei)) {
  84. printf(_T("ShellExecuteEx失败:%ld, %s\n"), GetLastError(), szCommand);
  85. return;
  86. }
  87. if (sei.hProcess != NULL) {
  88. WaitForSingleObject(sei.hProcess, INFINITE);
  89. if (sei.hProcess)
  90. CloseHandle(sei.hProcess);
  91. }
  92. // 结束后进入文本结果识别;
  93. CString str;
  94. CStdioFile file;
  95. if (file.Open(szFileName, CFile::modeRead)) {
  96. // 文件大小;
  97. ULONGLONG ulSize = file.SeekToEnd();
  98. file.SeekToBegin();
  99. if ( ulSize > 60)
  100. file.Seek(ulSize - 60, CFile::begin);
  101. while (file.GetPosition() != ulSize) {
  102. file.ReadString(str);
  103. }
  104. // 关闭文件;
  105. file.Close();
  106. }
  107. // 目前只针对更新指定版本;
  108. TCHAR szCommand2[MAX_PATH] = { 0 };
  109. _stprintf_s(szCommand, _T("At revision %ld."), dwSVNVersion);
  110. _stprintf_s(szCommand2, _T("Updated to revision %ld."), dwSVNVersion);
  111. if (!str.IsEmpty() && (str.CompareNoCase(szCommand) == 0 || str.CompareNoCase(szCommand2) == 0)) {
  112. // 表示更新成功;
  113. m_bNeedUpdate = FALSE;
  114. }
  115. }
  116. void CSVNProc::Export(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
  117. {
  118. SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_EXPORT);
  119. }
  120. void CSVNProc::Checkout(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
  121. {
  122. SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_CHECKOUT);
  123. }
  124. void CSVNProc::Rollback(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
  125. {
  126. SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_ROLLBACK);
  127. }
  128. BOOL CSVNMgr::ParseJson(std::string json)
  129. {
  130. cJSON* pRoot = cJSON_Parse(json.c_str());
  131. if (pRoot) {
  132. cJSON* pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
  133. if (pMsg) {
  134. std::string data;
  135. std::string code = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
  136. std::string msg = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
  137. if (code == "00") {
  138. cJSON* pObj = cJSON_GetObjectItem(pMsg, "data");
  139. if (pObj != NULL) {
  140. pObj = cJSON_GetObjectItem(pObj, "svnVersionArray");
  141. int nSise = cJSON_GetArraySize(pObj);
  142. for (int i = 0; i < nSise; i++) {
  143. cJSON* pInfo = cJSON_GetArrayItem(pObj, i);
  144. if (pInfo) {
  145. std::string type = cJSON_GetObjectItem(pInfo, "type") ? cJSON_GetObjectItem(pInfo, "type")->valuestring : "";
  146. std::string path = cJSON_GetObjectItem(pInfo, "svnPath") ? cJSON_GetObjectItem(pInfo, "svnPath")->valuestring : "";
  147. std::string version = cJSON_GetObjectItem(pInfo, "svnVersion") ? cJSON_GetObjectItem(pInfo, "svnVersion")->valuestring : "";
  148. CSVNProc* pSVN = NULL;
  149. // 查找该地址是否存在;
  150. if ((pSVN = IsSVNExist(path)) != NULL) {
  151. UpdatedSVN(pSVN, _tstol(version.c_str()));
  152. }
  153. else
  154. {
  155. pSVN = new CSVNProc();
  156. pSVN->m_bNeedUpdate = TRUE;
  157. pSVN->m_strSVNAddress = path.c_str();
  158. pSVN->m_dwSVNVersion = _tstol(version.c_str());
  159. if (type == "SatTest") {
  160. pSVN->m_svnpath = PATH_TESTER;
  161. pSVN->m_strSVNSavePath = _T("D:\\SAT\\Tester");
  162. }
  163. else if (type == "SatScript") {
  164. pSVN->m_svnpath = PATH_SCRIPT;
  165. pSVN->m_strSVNSavePath = _T("D:\\SAT\\Script");
  166. }
  167. else if (type == "SatResource") {
  168. pSVN->m_svnpath = PATH_RES;
  169. pSVN->m_strSVNSavePath = _T("D:\\SAT\\resource");
  170. }
  171. else {
  172. delete pSVN;
  173. pSVN = NULL;
  174. }
  175. // 加入数组;
  176. if ( pSVN )
  177. m_arySVN.Add(pSVN);
  178. }
  179. }
  180. }
  181. }
  182. // 释放内存;
  183. cJSON_Delete(pRoot);
  184. // 返回结果;
  185. return TRUE;
  186. }
  187. }
  188. // 错误产生,可输出msg方便查询;
  189. cJSON_Delete(pRoot);
  190. }
  191. return FALSE;
  192. }
  193. BOOL CSVNMgr::HttpRequest(std::string url, std::string strContent, std::string& data)
  194. {
  195. // 分解地址;
  196. DWORD dwServiceType;
  197. CString strServer;
  198. CString strObject;
  199. INTERNET_PORT nPort;
  200. AfxParseURL(url.c_str(), dwServiceType, strServer, strObject, nPort);
  201. if ((AFX_INET_SERVICE_HTTP != dwServiceType) && (AFX_INET_SERVICE_HTTPS != dwServiceType)) {
  202. return FALSE;
  203. }
  204. // 设置超时
  205. CInternetSession session(_T("HTTP Session"));;
  206. session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000);
  207. session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
  208. session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 10000);
  209. session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 20000);
  210. // 打开HTTP连接
  211. CHttpConnection* pHttpConnection = session.GetHttpConnection(strServer, dwServiceType == AFX_INET_SERVICE_HTTP ? INTERNET_FLAG_KEEP_CONNECTION : INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_SECURE, nPort);
  212. if (NULL == pHttpConnection) {
  213. return FALSE;
  214. }
  215. // 开启一个HTTP请求
  216. url.append("!" + strContent);
  217. url = CharEncoding::EnCode_UTF8URL(url.c_str());
  218. 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);
  219. if (NULL == pHttpFile) {
  220. return FALSE;
  221. }
  222. // 设置HTTP请求包头
  223. std::string output;
  224. TCHAR szHeaders[MAX_PATH] = { 0 };
  225. pHttpFile->AddRequestHeaders(_T("User-Agent: PostmanRuntime/7.24.1"));
  226. pHttpFile->AddRequestHeaders(_T("Accept: */*"));
  227. pHttpFile->AddRequestHeaders(_T("Accept-Encoding: gzip, deflate, br"));
  228. pHttpFile->AddRequestHeaders(_T("Connection: keep-alive"));
  229. pHttpFile->AddRequestHeaders(_T("Content-Type: application/json;charset=utf-8"));
  230. pHttpFile->AddRequestHeaders(_T("Cache-Control: no-cache"));
  231. pHttpFile->AddRequestHeaders(_T("Accept-Language: zh-cn"));
  232. _stprintf_s(szHeaders, _T("Host:%s"), strServer.GetString());
  233. pHttpFile->AddRequestHeaders(szHeaders);
  234. pHttpFile->AddRequestHeaders(_T("Content-Length:0"));
  235. // 发送数据
  236. BOOL bResult = pHttpFile->SendRequest(NULL, 0, NULL, 0);
  237. if (!bResult) {
  238. return FALSE;
  239. }
  240. // 查询状态
  241. DWORD dwHttpCode = 0;
  242. bResult = pHttpFile->QueryInfoStatusCode(dwHttpCode);
  243. if (!bResult) {
  244. return FALSE;
  245. }
  246. // 出错的原因
  247. char szBuffer[4096] = { 0 };
  248. DWORD dwBufferSize;
  249. if ((dwHttpCode < 200) || (dwHttpCode >= 300)) {
  250. BOOL bResult = pHttpFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, szBuffer, &dwBufferSize);
  251. return FALSE;
  252. }
  253. // 接收响应
  254. DWORD dwReadBytes;
  255. while ((dwReadBytes = pHttpFile->Read((void*)szBuffer, 4096)) > 0) {
  256. data.append(szBuffer, dwReadBytes);
  257. memset(szBuffer, 0, 4096 * sizeof(char));
  258. }
  259. // 释放资源
  260. if (NULL != pHttpFile) {
  261. pHttpFile->Close();
  262. delete pHttpFile;
  263. pHttpFile = NULL;
  264. }
  265. if (NULL != pHttpConnection) {
  266. pHttpConnection->Close();
  267. delete pHttpConnection;
  268. pHttpConnection = NULL;
  269. }
  270. session.Close();
  271. data = CharEncoding::DeCode_URLUTF8(data.c_str());
  272. TRACE1(_T("请求结果:%s"), data.c_str());
  273. return TRUE;
  274. }
  275. CSVNProc* CSVNMgr::IsSVNExist(std::string strSVNAddress)
  276. {
  277. CSVNProc* pSVN = NULL;
  278. int nSize = m_arySVN.GetSize();
  279. for ( int i = 0; i < nSize; i++ ) {
  280. pSVN = m_arySVN.GetAt(i);
  281. if (pSVN) {
  282. if (pSVN->m_strSVNAddress.CompareNoCase(strSVNAddress.c_str()) == 0)
  283. return pSVN;
  284. }
  285. }
  286. return NULL;
  287. }
  288. void CSVNMgr::UpdatedSVN(CSVNProc* pSVN, DWORD dwVersion)
  289. {
  290. if (pSVN->m_dwSVNVersion != dwVersion) {
  291. pSVN->m_dwSVNVersion = dwVersion;
  292. pSVN->m_bNeedUpdate = TRUE;
  293. //pSVN->Rollback(pSVN->m_strSVNAddress.GetString(), pSVN->m_strSVNSavePath.GetString(), pSVN->m_dwSVNVersion);
  294. }
  295. }
  296. BOOL CSVNMgr::HasNewVersion()
  297. {
  298. CSVNProc* pSVN = NULL;
  299. int nSize = m_arySVN.GetSize();
  300. if (nSize == 0)
  301. return FALSE;
  302. for (int i = 0; i < nSize; i++) {
  303. pSVN = m_arySVN.GetAt(i);
  304. if (pSVN) {
  305. if (pSVN->m_bNeedUpdate)
  306. return TRUE;
  307. }
  308. }
  309. return FALSE;
  310. }
  311. void CSVNMgr::Load(LPCTSTR lpFileName)
  312. {
  313. CFile file;
  314. if (file.Open(lpFileName, CFile::modeRead)) {
  315. CArchive ar(&file, CArchive::load);
  316. m_arySVN.Serialize(ar);
  317. ar.Close();
  318. file.Close();
  319. }
  320. }
  321. void CSVNMgr::Store(LPCTSTR lpFileName)
  322. {
  323. CFile file;
  324. if (file.Open(lpFileName, CFile::modeWrite | CFile::modeCreate)) {
  325. CArchive ar(&file, CArchive::store);
  326. m_arySVN.Serialize(ar);
  327. ar.Flush();
  328. ar.Close();
  329. file.Flush();
  330. file.Close();
  331. }
  332. }
  333. CString CSVNMgr::GetVersion()
  334. {
  335. CSVNProc* pSVN = NULL;
  336. int nSize = m_arySVN.GetSize();
  337. if (nSize == 0)
  338. return CString(Global::g_Config.nLanguage ? "[Failed to get version!]" : "【获取版本失败!】");
  339. CString strVersion;
  340. strVersion.Format(_T("%ld"), m_arySVN.GetAt(0)->m_dwSVNVersion);
  341. return strVersion;
  342. }
  343. BOOL CSVNMgr::CheckNewVersion(std::string url, std::string strContent)
  344. {
  345. std::string data;
  346. if (!HttpRequest(url, strContent, data))
  347. return FALSE;
  348. // 将结果转换为结构体;
  349. ParseJson(data);
  350. return HasNewVersion();
  351. }
  352. void CSVNMgr::Update()
  353. {
  354. CSVNProc* pSVN = NULL;
  355. int nSize = m_arySVN.GetSize();
  356. for (int i = 0; i < nSize; i++) {
  357. pSVN = m_arySVN.GetAt(i);
  358. if (pSVN) {
  359. if (pSVN->m_bNeedUpdate)
  360. pSVN->Rollback(pSVN->m_strSVNAddress.GetString(), pSVN->m_strSVNSavePath.GetString(), pSVN->m_dwSVNVersion);
  361. }
  362. }
  363. }