gitver.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // gitver.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "gitver.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #endif
  8. // 唯一的应用程序对象
  9. CWinApp theApp;
  10. using namespace std;
  11. extern TCHAR g_szCurModuleFileName[MAX_PATH] = { 0 }; // 软件名称;
  12. extern TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
  13. extern TCHAR g_szFna[_MAX_FNAME] = { 0 };
  14. extern TCHAR g_szExt[_MAX_EXT] = { 0 };
  15. extern TCHAR g_szFolderName[MAX_PATH] = { 0 };
  16. void GetDirInfo()
  17. {
  18. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  19. TCHAR szDir[_MAX_DIR] = { 0 };
  20. TCHAR szFna[_MAX_FNAME] = { 0 };
  21. TCHAR szExt[_MAX_EXT] = { 0 };
  22. DWORD dwRet = ::GetModuleFileName(NULL, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));
  23. _tsplitpath_s(g_szCurModuleFileName, szDrive, szDir, g_szFna, g_szExt);
  24. strcat_s(g_szCurModuleDir, MAX_PATH, szDrive);
  25. strcat_s(g_szCurModuleDir, MAX_PATH, szDir);
  26. CString strVal = g_szCurModuleDir;
  27. if ( strVal.GetAt(strVal.GetLength()-1) == _T('\\'))
  28. strVal.Delete(strVal.GetLength()-1);
  29. int nPos = strVal.ReverseFind(_T('\\'));
  30. if ( nPos != -1 )
  31. {
  32. strVal = strVal.Right(strVal.GetLength() - nPos -1);
  33. _stprintf_s(g_szFolderName, _T("%s"), strVal.GetString());
  34. }
  35. }
  36. BOOL ReplaceFileContent(LPCTSTR lpFile, std::vector<std::string> &vtOldContent, std::vector<std::string> &vtNewContent)
  37. {
  38. if ( vtOldContent.size() != vtNewContent.size() )
  39. return FALSE;
  40. if ( !PathFileExists(lpFile) )
  41. return FALSE;
  42. CFile myFile;
  43. CFileException fileExp;
  44. if ( !myFile.Open(lpFile, CFile::modeReadWrite, &fileExp) )
  45. return FALSE;
  46. DWORD dwFileLength = myFile.GetLength();
  47. BYTE *pData = new BYTE[dwFileLength];
  48. memset(pData, 0, dwFileLength);
  49. myFile.Read(pData, dwFileLength);
  50. std::string strContent;
  51. strContent.append((char*)pData, dwFileLength);
  52. delete []pData;
  53. pData = NULL;
  54. myFile.Close();
  55. std::vector<std::string>::iterator it_old;
  56. std::vector<std::string>::iterator it_new;
  57. for (it_old = vtOldContent.begin(), it_new = vtNewContent.begin(); it_old != vtOldContent.end(), it_new != vtNewContent.end(); it_old++, it_new++)
  58. {
  59. int nPos = strContent.find(it_old->c_str());
  60. if ( nPos != std::string::npos )
  61. {
  62. strContent.replace(nPos, it_old->size(), it_new->c_str());
  63. }
  64. }
  65. if ( !myFile.Open(lpFile, CFile::modeCreate|CFile::modeWrite, &fileExp) )
  66. return FALSE;
  67. myFile.Write(strContent.c_str(), strContent.size());
  68. myFile.Close();
  69. return TRUE;
  70. }
  71. CString StartProcess(LPCTSTR program, LPTSTR args, LPCTSTR lpCurrentDirectory )
  72. {
  73. CString strValue = _T("");
  74. const int MY_PIPE_BUFFER_SIZE = 8912;
  75. //初始化管道
  76. HANDLE hPipeRead;
  77. HANDLE hPipeWrite;
  78. SECURITY_ATTRIBUTES saOutPipe;
  79. ::ZeroMemory(&saOutPipe, sizeof(saOutPipe));
  80. saOutPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
  81. saOutPipe.lpSecurityDescriptor = NULL;
  82. saOutPipe.bInheritHandle = TRUE;
  83. if (CreatePipe(&hPipeRead, &hPipeWrite, &saOutPipe, MY_PIPE_BUFFER_SIZE))
  84. {
  85. PROCESS_INFORMATION processInfo;
  86. ::ZeroMemory(&processInfo, sizeof(processInfo));
  87. STARTUPINFO startupInfo;
  88. ::ZeroMemory(&startupInfo, sizeof(startupInfo));
  89. startupInfo.cb = sizeof(STARTUPINFO);
  90. startupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  91. startupInfo.hStdOutput = hPipeWrite;
  92. startupInfo.hStdError = hPipeWrite;
  93. startupInfo.wShowWindow = SW_HIDE;
  94. BOOL bRet = ::CreateProcess(program,
  95. args,
  96. NULL, // process security
  97. NULL, // thread security
  98. TRUE, //inheritance
  99. 0, //no startup flags
  100. NULL, // no special environment
  101. lpCurrentDirectory , //default startup directory
  102. &startupInfo,
  103. &processInfo);
  104. WaitForSingleObject(processInfo.hProcess, INFINITE);
  105. if (bRet == 0)
  106. {
  107. int nRet = GetLastError();
  108. printf("CreateProcess last error %d \n", nRet);
  109. }
  110. else
  111. {
  112. DWORD dwReadLen = 0;
  113. DWORD dwStdLen = 0;
  114. if (PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwReadLen, NULL) && dwReadLen > 0)
  115. {
  116. char szPipeOut[MY_PIPE_BUFFER_SIZE];
  117. ::ZeroMemory(szPipeOut, sizeof(szPipeOut));
  118. if (ReadFile(hPipeRead, szPipeOut, dwReadLen, &dwStdLen, NULL))
  119. {
  120. strValue = szPipeOut;
  121. }
  122. }
  123. }
  124. if (processInfo.hProcess)
  125. {
  126. CloseHandle(processInfo.hProcess);
  127. }
  128. if (processInfo.hThread)
  129. {
  130. CloseHandle(processInfo.hThread);
  131. }
  132. }
  133. CloseHandle(hPipeRead);
  134. CloseHandle(hPipeWrite);
  135. return strValue;
  136. }
  137. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  138. {
  139. int nRetCode = 0;
  140. //Sleep(20000);
  141. GetDirInfo();
  142. // 初始化 MFC 并在失败时显示错误
  143. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  144. {
  145. // TODO: 更改错误代码以符合您的需要
  146. _tprintf(_T("错误: MFC 初始化失败\n"));
  147. nRetCode = 1;
  148. }
  149. else
  150. {
  151. // TODO: 在此处为应用程序的行为编写代码。
  152. // 1=VC++ 2=C#;
  153. int nCodeType = 1;
  154. if ( argc == 2 )
  155. nCodeType = _tstoi(argv[1]);
  156. #ifdef _DEBUG
  157. CString strValue = StartProcess(NULL, "cmd /c git rev-parse --short HEAD", _T("E:\\CFG\\Moka_CShare_FactoryTool\\FactoryTool_CShare"));
  158. #else
  159. CString strValue = StartProcess(NULL, "cmd /c git rev-parse --short HEAD", NULL);
  160. #endif
  161. strValue.Replace(_T("\n"),_T(""));
  162. TCHAR szValue[MAX_PATH] = { 0 };
  163. TCHAR szResFile[MAX_PATH] = { 0 };
  164. std::vector<std::string> vtOldContent;
  165. std::vector<std::string> vtNewContent;
  166. if ( nCodeType == 1 )
  167. {
  168. // FILEVERSION 1,0,0,1
  169. // PRODUCTVERSION 1,0,0,1
  170. // VALUE "FileVersion", "1.0.0.1"
  171. // VALUE "ProductVersion", "1.0.0.1"
  172. // VALUE "OriginalFilename", "FactoryAssistTool.exe"
  173. // 问题:当commitid长度超过4位数字时,FileVersion放不下这么多字符;
  174. _stprintf_s(szResFile, _T("%s\\%s.rc"), g_szCurModuleDir, g_szFolderName);
  175. #if 0
  176. vtOldContent.push_back(_T("FILEVERSION 1,0,0,1"));
  177. vtOldContent.push_back(_T("VALUE \"FileVersion\", \"1.0.0.1\""));
  178. _stprintf_s(szValue, _T("FILEVERSION 1.0.%s"), strValue.GetString());
  179. vtNewContent.push_back(szValue);
  180. _stprintf_s(szValue, _T("VALUE \"FileVersion\", \"1.0.%s\""), strValue.GetString());
  181. vtNewContent.push_back(szValue);
  182. #endif
  183. #if 0
  184. strValue = StartProcess(NULL, "cmd /c git rev-parse HEAD", NULL);
  185. strValue.Replace(_T("\n"),_T(""));
  186. _stprintf_s(szValue, "VALUE \"OriginalFilename\", \"%s.exe\"", g_szFolderName);
  187. vtOldContent.push_back(szValue);
  188. _stprintf_s(szValue, "VALUE \"OriginalFilename\", \"%s\"", strValue.GetString());
  189. vtNewContent.push_back(szValue);
  190. #endif
  191. }
  192. else if ( nCodeType == 2 )
  193. {
  194. _stprintf_s(szResFile, _T("%s\\Properties\\AssemblyInfo.cs"), g_szCurModuleDir);
  195. // [assembly: AssemblyFileVersion("1.0.0.1")]
  196. vtOldContent.push_back(_T("[assembly: AssemblyFileVersion(\"1.0.0.1\")]"));
  197. _stprintf_s(szValue, _T("[assembly: AssemblyFileVersion(\"1.0.0.%s\")]"), strValue.GetString());
  198. vtNewContent.push_back(szValue);
  199. }
  200. // 更换内容;
  201. ReplaceFileContent(szResFile, vtOldContent, vtNewContent);
  202. }
  203. return nRetCode;
  204. }