一、准备工作
(1)安装git,配置git环境变量。
二、思路
通过解析ros2.repos文件,提取各模块的git下载地址,通过控制台的指令调用git进行下载。
三、C++源码
#include <fstream>
#include<iostream>
#include <string>
#include <windows.h>
#include <io.h>
using namespace std;
//检查文件是否存在
bool checkFile(const char* filePath){
//判断文件是否存在
if(access(filePath,0)){
return false;
}else{
return true;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
system("pause");
ifstream ifs;
ifs.open("ros2.repos",ios::in) ;
if (!ifs.is_open())
{
cout << "*打开文件失败!" << endl;
return 0;
}
else
{
cout << "*打开文件成功!" << endl;
}
string c_string;
string dir;
//首行
getline(ifs,c_string);
bool skin;
//循环解析ros2.repos
while(true)
{
skin = false;
//第一行-目录
if(getline(ifs,c_string))
{
//去除前空格
c_string.erase(0, c_string.find_first_not_of(" "));
//去除尾部字符
c_string.erase(c_string.end() - 1);
dir = c_string;
//判断目录是否存在
if(checkFile(dir.c_str()))
{
skin = true;
}
}
else
{
break;
}
//第二行-类型 跳过
if(getline(ifs,c_string))
{
//跳过
}
else
{
break;
}
//第三行-git地址
if(getline(ifs,c_string))
{
if(!skin)
{
//提取git地址
int start_pos = c_string.find(":") + 1;
string s_url = c_string.substr(start_pos, c_string.length() - start_pos + 1);
string s_git = "git clone ";
string s_cmd = s_git + s_url + " " + dir;
//执行指令
system(s_cmd.c_str());
}
}
else
{
break;
}
//第四行-版本
if(getline(ifs,c_string))
{
//跳过
}
else
{
break;
}
}
ifs.close();
system("pause");
return 0;
}