⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runautobuild.cpp

📁 自动化编译工具代码
💻 CPP
字号:
// RunAutoBuild.cpp: implementation of the CRunAutoBuild class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RunAutoBuild.h"

#include "Common.h"

#include "resultxsd.h"
#include <atlconv.h>
#include <iostream>

#include "VSSTool.h"
#include "BuildRunListener.h"
#include "ComplierFactory.h"
#include <memory>
using namespace std;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

const CString G_strVC60		= "VC60";
const CString G_strVC2002	= "VC2002";
const CString G_strVC2003	= "VC2003";
const CString G_strVC2005	= "VC2005";

const CString G_strBuildConfigName = "release";

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRunAutoBuild::CRunAutoBuild(const CString &strConfigFile,
							 CBuildRunListener *pListener) _THROW0()
		:m_strConfigFile(strConfigFile),
		 m_pVSSDB(new CVSSTool()),
		 m_pListener(pListener),
		 m_pConfig(NULL)
{
	if (!CFileFind().FindFile(strConfigFile))
		AfxThrowFileException(CFileException::fileNotFound, -1, strConfigFile); 

	// 此处load失败, 会抛出异常 ----------------------- 一定要做失败测试
	m_pConfig  = new CConfigsType;
	*m_pConfig = m_pConfigXML.Load(m_strConfigFile);

	CVSSInfoType vssInfo = m_pConfig->GetVSSInfo();
	m_pVSSDB->Open(vssInfo.GetDataBasePath(), vssInfo.GetUserName(), vssInfo.GetPassword());
	if (!m_pVSSDB->IsValid())
	{
		AfxThrowUserException();
	}
}

CRunAutoBuild::~CRunAutoBuild()
{
	delete m_pConfig;
	m_pConfig = NULL;
}

CComplier* CreateComplierObject(CTestProjectType  project)
{
	const CString strProjectVersion = project.GetProjectVersion();

	if      (strProjectVersion == G_strVC60)	
		return CComplierFactory::CreateComplierVC60();
	else if (strProjectVersion == G_strVC2002)
		return CComplierFactory::CreateComplierVCNET(CComplier::NET2002);
	else if (strProjectVersion == G_strVC2003)
		return CComplierFactory::CreateComplierVCNET(CComplier::NET2003);
	else if (strProjectVersion == G_strVC2005)
		return CComplierFactory::CreateComplierVCNET(CComplier::NET2005);

	ASSERT(FALSE);
	return NULL;
}

void CRunAutoBuild::Exec()
{
	BOOL bOk = FALSE;

	m_pListener->Clean();
	
	CTestProjectsType projects = m_pConfig->GetTestProjects();
	for (int i = 0; i < projects.GetTestProjectCount(); i++)
	{
		CTestProjectType  project  = projects.GetTestProjectAt(i);
		
		CString strTempPath;		// 产生一个临时目录
		strTempPath.Format("%stemp\\%d\\", g_strAppPath, CCommon::RandNum(5));

		const CString strLocalPath = strTempPath + project.GetProjectName();
		// 从VSS上取得代码
		// 编译工程
		m_pListener->SetProjectDir(strLocalPath);
		m_pListener->ReportExecStepMsg(CBuildRunListener::msg_StartTest);
		m_pListener->ReportExecStepMsg(CBuildRunListener::msg_StartGetSourceCode);
		
		if (m_pVSSDB->GetFiles(project.GetVSSPath(), strLocalPath))
		{
			m_pListener->ReportExecStepMsg(CBuildRunListener::msg_GetSourceCodeSuccessed);

			auto_ptr<CComplier> complierObject(CreateComplierObject(project));

			CStringArray arsFiles;
			CCommon::FindFiles(strLocalPath, arsFiles);
			ASSERT(arsFiles.GetSize() > 0);
			const int nFindPos = CCommon::FindStringPos(arsFiles, CString(project.GetProjectFile()));
			ASSERT(nFindPos != -1);

			if (nFindPos != -1 && CFileFind().FindFile(arsFiles.GetAt(nFindPos)))
			{
				m_pListener->ReportExecStepMsg(CBuildRunListener::msg_StartComplierProject);
				if (complierObject->Build(arsFiles.GetAt(nFindPos), G_strBuildConfigName))
				{
					m_pListener->ReportExecStepMsg(CBuildRunListener::msg_ComplierProjectSuccessed);
					// ----
					CStringArray arsExeFile;
					CCommon::FindFiles(strLocalPath+"\\"+G_strBuildConfigName, arsExeFile, "exe");
					if (arsExeFile.GetSize() == 1)
					{
						CCommon::WinExecAndWait32(arsExeFile.GetAt(0)
												  ,NULL
												  ,strLocalPath+"\\"+G_strBuildConfigName+"\\");

						m_pListener->ReportExecStepMsg(CBuildRunListener::msg_RunExeSuccessed);

						const CString strTestResultFile = strLocalPath+"\\"
												+G_strBuildConfigName+"\\"+"tests.xml";
						if (CFileFind().FindFile(strTestResultFile))
						{
							m_pListener->ReportExecStepMsg(CBuildRunListener::msg_FindTestResultXMLFileSuccessed);
							if (m_pListener->ReportExecResult(strTestResultFile, project))
								CCommon::DeleteDirectory(strLocalPath);	
						}
						else
							m_pListener->ReportExecStepMsg(CBuildRunListener::msg_NotFindTestResultXMLFile);
					}
					else
						m_pListener->ReportExecStepMsg(CBuildRunListener::msg_ComplierFileException);
				}
				else
					m_pListener->ReportExecStepMsg(CBuildRunListener::msg_ComplierProjectFailed);
			}
			else
				m_pListener->ReportExecStepMsg(CBuildRunListener::msg_NotFindProjectFile);
		}
		else
			m_pListener->ReportExecStepMsg(CBuildRunListener::msg_GetSourceCodeFailed);

		m_pListener->ReportExecStepMsg(CBuildRunListener::msg_EndTest);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -