|
@@ -0,0 +1,245 @@
|
|
|
+// gitver.cpp : 定义控制台应用程序的入口点。
|
|
|
+//
|
|
|
+
|
|
|
+#include "stdafx.h"
|
|
|
+#include "gitver.h"
|
|
|
+
|
|
|
+#ifdef _DEBUG
|
|
|
+#define new DEBUG_NEW
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+// 唯一的应用程序对象
|
|
|
+
|
|
|
+CWinApp theApp;
|
|
|
+
|
|
|
+using namespace std;
|
|
|
+
|
|
|
+extern TCHAR g_szCurModuleFileName[MAX_PATH] = { 0 }; // 软件名称;
|
|
|
+extern TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
|
|
|
+extern TCHAR g_szFna[_MAX_FNAME] = { 0 };
|
|
|
+extern TCHAR g_szExt[_MAX_EXT] = { 0 };
|
|
|
+extern TCHAR g_szFolderName[MAX_PATH] = { 0 };
|
|
|
+
|
|
|
+void GetDirInfo()
|
|
|
+{
|
|
|
+ TCHAR szDrive[_MAX_DRIVE] = { 0 };
|
|
|
+ TCHAR szDir[_MAX_DIR] = { 0 };
|
|
|
+ TCHAR szFna[_MAX_FNAME] = { 0 };
|
|
|
+ TCHAR szExt[_MAX_EXT] = { 0 };
|
|
|
+ DWORD dwRet = ::GetModuleFileName(NULL, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));
|
|
|
+
|
|
|
+ _tsplitpath_s(g_szCurModuleFileName, szDrive, szDir, g_szFna, g_szExt);
|
|
|
+ strcat_s(g_szCurModuleDir, MAX_PATH, szDrive);
|
|
|
+ strcat_s(g_szCurModuleDir, MAX_PATH, szDir);
|
|
|
+
|
|
|
+ CString strVal = g_szCurModuleDir;
|
|
|
+ if ( strVal.GetAt(strVal.GetLength()-1) == _T('\\'))
|
|
|
+ strVal.Delete(strVal.GetLength()-1);
|
|
|
+
|
|
|
+ int nPos = strVal.ReverseFind(_T('\\'));
|
|
|
+ if ( nPos != -1 )
|
|
|
+ {
|
|
|
+ strVal = strVal.Right(strVal.GetLength() - nPos -1);
|
|
|
+ _stprintf_s(g_szFolderName, _T("%s"), strVal.GetString());
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+BOOL ReplaceFileContent(LPCTSTR lpFile, std::vector<std::string> &vtOldContent, std::vector<std::string> &vtNewContent)
|
|
|
+{
|
|
|
+ if ( vtOldContent.size() != vtNewContent.size() )
|
|
|
+ return FALSE;
|
|
|
+
|
|
|
+ if ( !PathFileExists(lpFile) )
|
|
|
+ return FALSE;
|
|
|
+
|
|
|
+
|
|
|
+ CFile myFile;
|
|
|
+ CFileException fileExp;
|
|
|
+ if ( !myFile.Open(lpFile, CFile::modeReadWrite, &fileExp) )
|
|
|
+ return FALSE;
|
|
|
+
|
|
|
+ DWORD dwFileLength = myFile.GetLength();
|
|
|
+ BYTE *pData = new BYTE[dwFileLength];
|
|
|
+ memset(pData, 0, dwFileLength);
|
|
|
+ myFile.Read(pData, dwFileLength);
|
|
|
+
|
|
|
+ std::string strContent;
|
|
|
+ strContent.append((char*)pData, dwFileLength);
|
|
|
+ delete []pData;
|
|
|
+ pData = NULL;
|
|
|
+ myFile.Close();
|
|
|
+
|
|
|
+ std::vector<std::string>::iterator it_old;
|
|
|
+ std::vector<std::string>::iterator it_new;
|
|
|
+ for (it_old = vtOldContent.begin(), it_new = vtNewContent.begin(); it_old != vtOldContent.end(), it_new != vtNewContent.end(); it_old++, it_new++)
|
|
|
+ {
|
|
|
+ int nPos = strContent.find(it_old->c_str());
|
|
|
+ if ( nPos != std::string::npos )
|
|
|
+ {
|
|
|
+ strContent.replace(nPos, it_old->size(), it_new->c_str());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( !myFile.Open(lpFile, CFile::modeCreate|CFile::modeWrite, &fileExp) )
|
|
|
+ return FALSE;
|
|
|
+
|
|
|
+ myFile.Write(strContent.c_str(), strContent.size());
|
|
|
+ myFile.Close();
|
|
|
+
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+CString StartProcess(LPCTSTR program, LPTSTR args, LPCTSTR lpCurrentDirectory )
|
|
|
+{
|
|
|
+ CString strValue = _T("");
|
|
|
+ const int MY_PIPE_BUFFER_SIZE = 8912;
|
|
|
+ //初始化管道
|
|
|
+ HANDLE hPipeRead;
|
|
|
+ HANDLE hPipeWrite;
|
|
|
+ SECURITY_ATTRIBUTES saOutPipe;
|
|
|
+ ::ZeroMemory(&saOutPipe, sizeof(saOutPipe));
|
|
|
+ saOutPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
+ saOutPipe.lpSecurityDescriptor = NULL;
|
|
|
+ saOutPipe.bInheritHandle = TRUE;
|
|
|
+ if (CreatePipe(&hPipeRead, &hPipeWrite, &saOutPipe, MY_PIPE_BUFFER_SIZE))
|
|
|
+ {
|
|
|
+ PROCESS_INFORMATION processInfo;
|
|
|
+ ::ZeroMemory(&processInfo, sizeof(processInfo));
|
|
|
+ STARTUPINFO startupInfo;
|
|
|
+ ::ZeroMemory(&startupInfo, sizeof(startupInfo));
|
|
|
+ startupInfo.cb = sizeof(STARTUPINFO);
|
|
|
+ startupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
|
+ startupInfo.hStdOutput = hPipeWrite;
|
|
|
+ startupInfo.hStdError = hPipeWrite;
|
|
|
+ startupInfo.wShowWindow = SW_HIDE;
|
|
|
+
|
|
|
+ BOOL bRet = ::CreateProcess(program,
|
|
|
+ args,
|
|
|
+ NULL, // process security
|
|
|
+ NULL, // thread security
|
|
|
+ TRUE, //inheritance
|
|
|
+ 0, //no startup flags
|
|
|
+ NULL, // no special environment
|
|
|
+ lpCurrentDirectory , //default startup directory
|
|
|
+ &startupInfo,
|
|
|
+ &processInfo);
|
|
|
+
|
|
|
+ WaitForSingleObject(processInfo.hProcess, INFINITE);
|
|
|
+ if (bRet == 0)
|
|
|
+ {
|
|
|
+ int nRet = GetLastError();
|
|
|
+ printf("CreateProcess last error %d \n", nRet);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ DWORD dwReadLen = 0;
|
|
|
+ DWORD dwStdLen = 0;
|
|
|
+ if (PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwReadLen, NULL) && dwReadLen > 0)
|
|
|
+ {
|
|
|
+ char szPipeOut[MY_PIPE_BUFFER_SIZE];
|
|
|
+ ::ZeroMemory(szPipeOut, sizeof(szPipeOut));
|
|
|
+ if (ReadFile(hPipeRead, szPipeOut, dwReadLen, &dwStdLen, NULL))
|
|
|
+ {
|
|
|
+ strValue = szPipeOut;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (processInfo.hProcess)
|
|
|
+ {
|
|
|
+ CloseHandle(processInfo.hProcess);
|
|
|
+ }
|
|
|
+ if (processInfo.hThread)
|
|
|
+ {
|
|
|
+ CloseHandle(processInfo.hThread);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ CloseHandle(hPipeRead);
|
|
|
+ CloseHandle(hPipeWrite);
|
|
|
+
|
|
|
+ return strValue;
|
|
|
+}
|
|
|
+
|
|
|
+int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
|
|
|
+{
|
|
|
+ int nRetCode = 0;
|
|
|
+ //Sleep(20000);
|
|
|
+
|
|
|
+ GetDirInfo();
|
|
|
+ // 初始化 MFC 并在失败时显示错误
|
|
|
+ if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
|
|
|
+ {
|
|
|
+ // TODO: 更改错误代码以符合您的需要
|
|
|
+ _tprintf(_T("错误: MFC 初始化失败\n"));
|
|
|
+ nRetCode = 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // TODO: 在此处为应用程序的行为编写代码。
|
|
|
+ // 1=VC++ 2=C#;
|
|
|
+ int nCodeType = 1;
|
|
|
+ if ( argc == 2 )
|
|
|
+ nCodeType = _tstoi(argv[1]);
|
|
|
+
|
|
|
+#ifdef _DEBUG
|
|
|
+ CString strValue = StartProcess(NULL, "cmd /c git rev-parse --short HEAD", _T("E:\\CFG\\Moka_CShare_FactoryTool\\FactoryTool_CShare"));
|
|
|
+#else
|
|
|
+ CString strValue = StartProcess(NULL, "cmd /c git rev-parse --short HEAD", NULL);
|
|
|
+#endif
|
|
|
+ strValue.Replace(_T("\n"),_T(""));
|
|
|
+
|
|
|
+ TCHAR szValue[MAX_PATH] = { 0 };
|
|
|
+ TCHAR szResFile[MAX_PATH] = { 0 };
|
|
|
+ std::vector<std::string> vtOldContent;
|
|
|
+ std::vector<std::string> vtNewContent;
|
|
|
+
|
|
|
+ if ( nCodeType == 1 )
|
|
|
+ {
|
|
|
+ // FILEVERSION 1,0,0,1
|
|
|
+ // PRODUCTVERSION 1,0,0,1
|
|
|
+ // VALUE "FileVersion", "1.0.0.1"
|
|
|
+ // VALUE "ProductVersion", "1.0.0.1"
|
|
|
+ // VALUE "OriginalFilename", "FactoryAssistTool.exe"
|
|
|
+ // 问题:当commitid长度超过4位数字时,FileVersion放不下这么多字符;
|
|
|
+ _stprintf_s(szResFile, _T("%s\\%s.rc"), g_szCurModuleDir, g_szFolderName);
|
|
|
+
|
|
|
+#if 0
|
|
|
+ vtOldContent.push_back(_T("FILEVERSION 1,0,0,1"));
|
|
|
+ vtOldContent.push_back(_T("VALUE \"FileVersion\", \"1.0.0.1\""));
|
|
|
+
|
|
|
+ _stprintf_s(szValue, _T("FILEVERSION 1.0.%s"), strValue.GetString());
|
|
|
+ vtNewContent.push_back(szValue);
|
|
|
+
|
|
|
+ _stprintf_s(szValue, _T("VALUE \"FileVersion\", \"1.0.%s\""), strValue.GetString());
|
|
|
+ vtNewContent.push_back(szValue);
|
|
|
+#endif
|
|
|
+
|
|
|
+#if 0
|
|
|
+ strValue = StartProcess(NULL, "cmd /c git rev-parse HEAD", NULL);
|
|
|
+ strValue.Replace(_T("\n"),_T(""));
|
|
|
+
|
|
|
+ _stprintf_s(szValue, "VALUE \"OriginalFilename\", \"%s.exe\"", g_szFolderName);
|
|
|
+ vtOldContent.push_back(szValue);
|
|
|
+
|
|
|
+ _stprintf_s(szValue, "VALUE \"OriginalFilename\", \"%s\"", strValue.GetString());
|
|
|
+ vtNewContent.push_back(szValue);
|
|
|
+#endif
|
|
|
+
|
|
|
+ }
|
|
|
+ else if ( nCodeType == 2 )
|
|
|
+ {
|
|
|
+ _stprintf_s(szResFile, _T("%s\\Properties\\AssemblyInfo.cs"), g_szCurModuleDir);
|
|
|
+ // [assembly: AssemblyFileVersion("1.0.0.1")]
|
|
|
+ vtOldContent.push_back(_T("[assembly: AssemblyFileVersion(\"1.0.0.1\")]"));
|
|
|
+
|
|
|
+ _stprintf_s(szValue, _T("[assembly: AssemblyFileVersion(\"1.0.0.%s\")]"), strValue.GetString());
|
|
|
+ vtNewContent.push_back(szValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更换内容;
|
|
|
+ ReplaceFileContent(szResFile, vtOldContent, vtNewContent);
|
|
|
+ }
|
|
|
+
|
|
|
+ return nRetCode;
|
|
|
+}
|