You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
4.1 KiB
110 lines
4.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace P_Generate
|
|
{
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
string str = "..\\proto-generator-bin\\protoc --nanopb_out=. *.proto";
|
|
Cmd(str);
|
|
|
|
SourceFileChanger();
|
|
}
|
|
|
|
static void Cmd(string str)
|
|
{
|
|
System.Diagnostics.Process p = new System.Diagnostics.Process();
|
|
p.StartInfo.FileName = "cmd.exe";
|
|
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
|
|
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
|
|
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
|
|
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
|
|
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
|
|
p.Start();//启动程序
|
|
|
|
|
|
//向cmd窗口发送输入信息
|
|
p.StandardInput.WriteLine(str + "&exit");
|
|
|
|
p.StandardInput.AutoFlush = true;
|
|
//p.StandardInput.WriteLine("exit");
|
|
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
|
|
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
|
|
|
|
|
|
|
|
//获取cmd窗口的输出信息
|
|
string output = p.StandardOutput.ReadToEnd();
|
|
|
|
//StreamReader reader = p.StandardOutput;
|
|
//string line=reader.ReadLine();
|
|
//while (!reader.EndOfStream)
|
|
//{
|
|
// str += line + " ";
|
|
// line = reader.ReadLine();
|
|
//}
|
|
|
|
p.WaitForExit();//等待程序执行完退出进程
|
|
p.Close();
|
|
|
|
|
|
Console.WriteLine(output);
|
|
|
|
//对所有文件进行修改
|
|
//读取所有的文件
|
|
}
|
|
|
|
static void SourceFileChanger()
|
|
{
|
|
var BaseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
|
var fileName = Directory.GetFiles(BaseDirectory);
|
|
|
|
foreach (var _fileName in fileName)
|
|
{
|
|
if (_fileName.ToLower().EndsWith(".c")|| _fileName.ToLower().EndsWith(".h"))
|
|
{
|
|
var fileNmaeLines = File.ReadAllLines(_fileName);
|
|
for (int i = 0; i < fileNmaeLines.Length; i++)
|
|
{
|
|
/*修改Random=> rand*/
|
|
if (fileNmaeLines[i].Trim().StartsWith("#include"))
|
|
{
|
|
if (fileNmaeLines[i].Contains("<"))
|
|
{
|
|
fileNmaeLines[i] = fileNmaeLines[i].Replace("<", "\"");
|
|
}
|
|
if (fileNmaeLines[i].Contains(">"))
|
|
{
|
|
fileNmaeLines[i] = fileNmaeLines[i].Replace(">", "\"");
|
|
}
|
|
|
|
}
|
|
//if (fileNmaeLines[i].Contains("random()"))
|
|
//{
|
|
// fileNmaeLines[i] = fileNmaeLines[i].Replace("random", "rand");
|
|
//}
|
|
}
|
|
|
|
|
|
File.WriteAllLines(_fileName, fileNmaeLines);
|
|
var fileInfo = new FileInfo(_fileName);
|
|
Console.WriteLine($" 郭世卿(akeguo@126.com)=》 {fileInfo.Name} is completed!");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
Console.WriteLine($" 郭世卿(akeguo@126.com)=》 生成结束!");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
}
|
|
}
|
|
|