SVNProc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. return;
  85. }
  86. if (sei.hProcess != NULL) {
  87. WaitForSingleObject(sei.hProcess, INFINITE);
  88. if (sei.hProcess)
  89. CloseHandle(sei.hProcess);
  90. }
  91. // 结束后进入文本结果识别;
  92. CString str;
  93. CStdioFile file;
  94. if (file.Open(szFileName, CFile::modeRead)) {
  95. // 文件大小;
  96. ULONGLONG ulSize = file.SeekToEnd();
  97. file.SeekToBegin();
  98. if ( ulSize > 60)
  99. file.Seek(ulSize - 60, CFile::begin);
  100. while (file.GetPosition() != ulSize) {
  101. file.ReadString(str);
  102. }
  103. // 关闭文件;
  104. file.Close();
  105. }
  106. // 目前只针对更新指定版本;
  107. TCHAR szCommand2[MAX_PATH] = { 0 };
  108. _stprintf_s(szCommand, _T("At revision %ld."), dwSVNVersion);
  109. _stprintf_s(szCommand2, _T("Updated to revision %ld."), dwSVNVersion);
  110. if (!str.IsEmpty() && (str.CompareNoCase(szCommand) == 0 || str.CompareNoCase(szCommand2) == 0)) {
  111. // 表示更新成功;
  112. m_bNeedUpdate = FALSE;
  113. }
  114. }
  115. void CSVNProc::Export(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
  116. {
  117. SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_EXPORT);
  118. }
  119. void CSVNProc::Checkout(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
  120. {
  121. SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_CHECKOUT);
  122. }
  123. void CSVNProc::Rollback(std::string strSVNAddress, std::string strSavePath, DWORD dwSVNVersion)
  124. {
  125. SVNProcess(strSVNAddress, strSavePath, dwSVNVersion, SVN_ROLLBACK);
  126. }
  127. BOOL CSVNMgr::ParseJson(std::string json)
  128. {
  129. cJSON* pRoot = cJSON_Parse(json.c_str());
  130. if (pRoot) {
  131. cJSON* pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
  132. if (pMsg) {
  133. std::string data;
  134. std::string code = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
  135. std::string msg = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
  136. if (code == "00") {
  137. cJSON* pObj = cJSON_GetObjectItem(pMsg, "data");
  138. if (pObj != NULL) {
  139. pObj = cJSON_GetObjectItem(pObj, "svnVersionArray");
  140. int nSise = cJSON_GetArraySize(pObj);
  141. for (int i = 0; i < nSise; i++) {
  142. cJSON* pInfo = cJSON_GetArrayItem(pObj, i);
  143. if (pInfo) {
  144. std::string type = cJSON_GetObjectItem(pInfo, "type") ? cJSON_GetObjectItem(pInfo, "type")->valuestring : "";
  145. std::string path = cJSON_GetObjectItem(pInfo, "svnPath") ? cJSON_GetObjectItem(pInfo, "svnPath")->valuestring : "";
  146. std::string version = cJSON_GetObjectItem(pInfo, "svnVersion") ? cJSON_GetObjectItem(pInfo, "svnVersion")->valuestring : "";
  147. CSVNProc* pSVN = NULL;
  148. // 查找该地址是否存在;
  149. if ((pSVN = IsSVNExist(path)) != NULL) {
  150. UpdatedSVN(pSVN, _tstol(version.c_str()));
  151. }
  152. else
  153. {
  154. pSVN = new CSVNProc();
  155. pSVN->m_bNeedUpdate = TRUE;
  156. pSVN->m_strSVNAddress = path.c_str();
  157. pSVN->m_dwSVNVersion = _tstol(version.c_str());
  158. if (type == "SatTest") {
  159. pSVN->m_svnpath = PATH_TESTER;
  160. pSVN->m_strSVNSavePath = _T("D:\\SAT\\Tester");
  161. }
  162. else if (type == "SatScript") {
  163. pSVN->m_svnpath = PATH_SCRIPT;
  164. pSVN->m_strSVNSavePath = _T("D:\\SAT\\Script");
  165. }
  166. else if (type == "SatResource") {
  167. pSVN->m_svnpath = PATH_RES;
  168. pSVN->m_strSVNSavePath = _T("D:\\SAT\\resource");
  169. }
  170. else {
  171. delete pSVN;
  172. pSVN = NULL;
  173. }
  174. // 加入数组;
  175. if ( pSVN )
  176. m_arySVN.Add(pSVN);
  177. }
  178. }
  179. }
  180. }
  181. // 释放内存;
  182. cJSON_Delete(pRoot);
  183. // 返回结果;
  184. return TRUE;
  185. }
  186. }
  187. // 错误产生,可输出msg方便查询;
  188. cJSON_Delete(pRoot);
  189. }
  190. return FALSE;
  191. }
  192. BOOL CSVNMgr::HttpRequest(std::string url, std::string strContent, std::string& data)
  193. {
  194. // 分解地址;
  195. DWORD dwServiceType;
  196. CString strServer;
  197. CString strObject;
  198. INTERNET_PORT nPort;
  199. AfxParseURL(url.c_str(), dwServiceType, strServer, strObject, nPort);
  200. if ((AFX_INET_SERVICE_HTTP != dwServiceType) && (AFX_INET_SERVICE_HTTPS != dwServiceType)) {
  201. return FALSE;
  202. }
  203. // 设置超时
  204. CInternetSession session(_T("HTTP Session"));;
  205. session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000);
  206. session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
  207. session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 10000);
  208. session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 20000);
  209. // 打开HTTP连接
  210. CHttpConnection* pHttpConnection = session.GetHttpConnection(strServer, dwServiceType == AFX_INET_SERVICE_HTTP ? INTERNET_FLAG_KEEP_CONNECTION : INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_SECURE, nPort);
  211. if (NULL == pHttpConnection) {
  212. return FALSE;
  213. }
  214. // 开启一个HTTP请求
  215. url.append("!" + strContent);
  216. url = CharEncoding::EnCode_UTF8URL(url.c_str());
  217. 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);
  218. if (NULL == pHttpFile) {
  219. return FALSE;
  220. }
  221. // 设置HTTP请求包头
  222. std::string output;
  223. TCHAR szHeaders[MAX_PATH] = { 0 };
  224. pHttpFile->AddRequestHeaders(_T("User-Agent: PostmanRuntime/7.24.1"));
  225. pHttpFile->AddRequestHeaders(_T("Accept: */*"));
  226. pHttpFile->AddRequestHeaders(_T("Accept-Encoding: gzip, deflate, br"));
  227. pHttpFile->AddRequestHeaders(_T("Connection: keep-alive"));
  228. pHttpFile->AddRequestHeaders(_T("Content-Type: application/json;charset=utf-8"));
  229. pHttpFile->AddRequestHeaders(_T("Cache-Control: no-cache"));
  230. pHttpFile->AddRequestHeaders(_T("Accept-Language: zh-cn"));
  231. _stprintf_s(szHeaders, _T("Host:%s"), strServer.GetString());
  232. pHttpFile->AddRequestHeaders(szHeaders);
  233. pHttpFile->AddRequestHeaders(_T("Content-Length:0"));
  234. // 发送数据
  235. BOOL bResult = pHttpFile->SendRequest(NULL, 0, NULL, 0);
  236. if (!bResult) {
  237. return FALSE;
  238. }
  239. // 查询状态
  240. DWORD dwHttpCode = 0;
  241. bResult = pHttpFile->QueryInfoStatusCode(dwHttpCode);
  242. if (!bResult) {
  243. return FALSE;
  244. }
  245. // 出错的原因
  246. char szBuffer[4096] = { 0 };
  247. DWORD dwBufferSize;
  248. if ((dwHttpCode < 200) || (dwHttpCode >= 300)) {
  249. BOOL bResult = pHttpFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, szBuffer, &dwBufferSize);
  250. return FALSE;
  251. }
  252. // 接收响应
  253. DWORD dwReadBytes;
  254. while ((dwReadBytes = pHttpFile->Read((void*)szBuffer, 4096)) > 0) {
  255. data.append(szBuffer, dwReadBytes);
  256. memset(szBuffer, 0, 4096 * sizeof(char));
  257. }
  258. // 释放资源
  259. if (NULL != pHttpFile) {
  260. pHttpFile->Close();
  261. delete pHttpFile;
  262. pHttpFile = NULL;
  263. }
  264. if (NULL != pHttpConnection) {
  265. pHttpConnection->Close();
  266. delete pHttpConnection;
  267. pHttpConnection = NULL;
  268. }
  269. session.Close();
  270. data = CharEncoding::DeCode_URLUTF8(data.c_str());
  271. TRACE1(_T("请求结果:%s"), data.c_str());
  272. return TRUE;
  273. }
  274. CSVNProc* CSVNMgr::IsSVNExist(std::string strSVNAddress)
  275. {
  276. CSVNProc* pSVN = NULL;
  277. int nSize = m_arySVN.GetSize();
  278. for ( int i = 0; i < nSize; i++ ) {
  279. pSVN = m_arySVN.GetAt(i);
  280. if (pSVN) {
  281. if (pSVN->m_strSVNAddress.CompareNoCase(strSVNAddress.c_str()) == 0)
  282. return pSVN;
  283. }
  284. }
  285. return NULL;
  286. }
  287. void CSVNMgr::UpdatedSVN(CSVNProc* pSVN, DWORD dwVersion)
  288. {
  289. if (pSVN->m_dwSVNVersion != dwVersion) {
  290. pSVN->m_dwSVNVersion = dwVersion;
  291. pSVN->m_bNeedUpdate = TRUE;
  292. //pSVN->Rollback(pSVN->m_strSVNAddress.GetString(), pSVN->m_strSVNSavePath.GetString(), pSVN->m_dwSVNVersion);
  293. }
  294. }
  295. BOOL CSVNMgr::HasNewVersion()
  296. {
  297. CSVNProc* pSVN = NULL;
  298. int nSize = m_arySVN.GetSize();
  299. if (nSize == 0)
  300. return FALSE;
  301. for (int i = 0; i < nSize; i++) {
  302. pSVN = m_arySVN.GetAt(i);
  303. if (pSVN) {
  304. if (pSVN->m_bNeedUpdate)
  305. return TRUE;
  306. }
  307. }
  308. return FALSE;
  309. }
  310. void CSVNMgr::Load(LPCTSTR lpFileName)
  311. {
  312. CFile file;
  313. if (file.Open(lpFileName, CFile::modeRead)) {
  314. CArchive ar(&file, CArchive::load);
  315. m_arySVN.Serialize(ar);
  316. ar.Close();
  317. file.Close();
  318. }
  319. }
  320. void CSVNMgr::Store(LPCTSTR lpFileName)
  321. {
  322. CFile file;
  323. if (file.Open(lpFileName, CFile::modeWrite | CFile::modeCreate)) {
  324. CArchive ar(&file, CArchive::store);
  325. m_arySVN.Serialize(ar);
  326. ar.Flush();
  327. ar.Close();
  328. file.Flush();
  329. file.Close();
  330. }
  331. }
  332. CString CSVNMgr::GetVersion()
  333. {
  334. CSVNProc* pSVN = NULL;
  335. int nSize = m_arySVN.GetSize();
  336. if (nSize == 0)
  337. return CString("【获取版本失败!】");
  338. CString strVersion;
  339. strVersion.Format(_T("%ld"), m_arySVN.GetAt(0)->m_dwSVNVersion);
  340. return strVersion;
  341. }
  342. BOOL CSVNMgr::CheckNewVersion(std::string url, std::string strContent)
  343. {
  344. std::string data;
  345. if (!HttpRequest(url, strContent, data))
  346. return FALSE;
  347. // 将结果转换为结构体;
  348. ParseJson(data);
  349. return HasNewVersion();
  350. }
  351. void CSVNMgr::Update()
  352. {
  353. CSVNProc* pSVN = NULL;
  354. int nSize = m_arySVN.GetSize();
  355. for (int i = 0; i < nSize; i++) {
  356. pSVN = m_arySVN.GetAt(i);
  357. if (pSVN) {
  358. if (pSVN->m_bNeedUpdate)
  359. pSVN->Rollback(pSVN->m_strSVNAddress.GetString(), pSVN->m_strSVNSavePath.GetString(), pSVN->m_dwSVNVersion);
  360. }
  361. }
  362. }