using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace copyARX { public class ItemData { public ItemData() { Platform = string.Empty; Configuration = string.Empty; PlatformToolset = string.Empty; arx = string.Empty; useSrcRoot = true; useDescRoot = true; src_path = string.Empty; desc_path = string.Empty; } public ItemData(string platform, string config, string toolset, string arx) { Platform = platform; Configuration = config; PlatformToolset = toolset; this.arx = arx; } public bool compare(ItemData data) { if (Platform == data.Platform && Configuration == data.Configuration && PlatformToolset == data.PlatformToolset && arx == data.arx) return true; return false; } public string Platform; public string Configuration; public string PlatformToolset; public string arx; public bool useSrcRoot; public bool useDescRoot; public string src_path; public string desc_path; } class Program { static void Main(string[] args) { // 共七个参数,最后一个可以没有 bool bCMake = args[0] == "CMAKE"; if (bCMake) { if (args.Length < 4) { System.Console.WriteLine("参数不足,请重新配置. -cmake"); } string sArxPath = args[1], // 需要copy的文件名 sSDKVer = args[2], // sdk版本 sJson = args[3]; // json文件 if (!File.Exists(sJson)) { Console.WriteLine(sJson + "文件未找到。"); return; } if (!File.Exists(sArxPath)) { Console.WriteLine(sArxPath + "文件未找到。"); return; } string sSHEnvPath = string.Empty; using (StreamReader reader = File.OpenText(sJson)) { JObject obj = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); JToken jtok = string.Empty; if (!obj.TryGetValue("desc_root", out jtok)) { Console.WriteLine("文件未找到 desc_root 配置。"); return; } sSHEnvPath = jtok.ToObject(); } string dest_dir = sSHEnvPath + "\\" + sSDKVer; if (!Directory.Exists(dest_dir)) { Console.WriteLine(string.Format("{0} 文件夹未找到!", dest_dir)); return; } string dest_arx = dest_dir + "\\" + Path.GetFileName(sArxPath); File.Copy(sArxPath, dest_arx, true); Console.WriteLine(string.Format("拷贝文件从:\n {0}\n 到:\n {1} \n成功.", sArxPath, dest_arx)); return; } if (args.Length < 6) { System.Console.WriteLine("参数不足,请重新配置," , "(例如:$(ProjectName) $(PlatformToolset) $(Configuration) $(Platform) $(TargetExt) exepath.txt sysPath)"); } string project = args[0] // 工程名 , toolset = args[1] // v90~v140 , config = args[2] // debug | release , platform = args[3] // win32 | x64 , ext = args[4] // 扩展名 , cnfPath = args[5] // 记录 earthworm 所在主目录的文件 , srcPath = Process.GetCurrentProcess().MainModule.FileName; // 执行文件所在目录 string subSys = string.Empty; if (args.Length > 6) subSys = args[6]; srcPath = srcPath.Substring(0, srcPath.LastIndexOf("\\")); cnfPath = srcPath + "\\" + cnfPath; srcPath = srcPath.Substring(0, srcPath.LastIndexOf("\\")) + "\\"; if (!File.Exists(cnfPath)) { Console.WriteLine(cnfPath + "文件未找到。"); return; } using (StreamReader reader = File.OpenText(cnfPath)) { JObject obj = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); JToken jtok = string.Empty; if (!obj.TryGetValue("desc_root", out jtok)) { Console.WriteLine("文件未找到 desc_root 配置。"); return; } string desc_root = jtok.ToObject(); if (!Directory.Exists(desc_root)) { Console.WriteLine("desc_root 文件夹未找到!"); return; } if (!obj.TryGetValue("src_root", out jtok)) { Console.WriteLine("文件未找到 src_root 配置。"); return; } string src_root = jtok.ToObject(); if (!Directory.Exists(src_root)) { Console.WriteLine("src_root 文件夹未找到!"); return; } if (!obj.TryGetValue("items", out jtok)) { Console.WriteLine("文件未找到 items 配置。"); return; } JArray jar = jtok.ToObject(); List items = new List(); foreach (JObject jo in jar) { ItemData data = new ItemData(); if (!jo.TryGetValue("Platform", out jtok)) continue; data.Platform = jtok.ToObject(); if (!jo.TryGetValue("Configuration", out jtok)) continue; data.Configuration = jtok.ToObject(); if (!jo.TryGetValue("PlatformToolset", out jtok)) continue; data.PlatformToolset = jtok.ToObject(); if (!jo.TryGetValue("arx", out jtok)) continue; data.arx = jtok.ToObject(); if (!jo.TryGetValue("useSrcRoot", out jtok)) continue; data.useSrcRoot = jtok.ToObject(); if (!jo.TryGetValue("useDescRoot", out jtok)) continue; data.useDescRoot = jtok.ToObject(); if (!jo.TryGetValue("src_path", out jtok)) continue; data.src_path = jtok.ToObject(); if (!jo.TryGetValue("desc_path", out jtok)) continue; data.desc_path = jtok.ToObject(); items.Add(data); } ItemData input = new ItemData(platform, config, toolset, subSys); string srcDirArx = string.Empty; string srcDirCore = string.Empty; string srcfile = string.Empty; string descFile = string.Empty; bool bFindItem = false; ItemData item = new ItemData(); foreach (ItemData it in items) { if (!it.compare(input)) continue; else { item = it; bFindItem = true; } } if (!bFindItem) { Console.WriteLine("没有找到相关的配置项!"); return; } if (item.useSrcRoot) { srcDirArx = string.Format("{0}\\{1}\\{2}{3}" , src_root , item.src_path , project , ext); srcDirCore = string.Format("{0}\\{1}-[]-[]\\{2}{3}" , src_root , item.src_path , project , ext); if (File.Exists(srcDirArx) && !File.Exists(srcDirCore)) { srcfile = srcDirArx; } else if (!File.Exists(srcDirArx) && File.Exists(srcDirCore)) { srcfile = srcDirCore; } else if (File.Exists(srcDirArx) && File.Exists(srcDirCore)) { DateTime dtArx = File.GetCreationTime(srcDirArx); DateTime dtCore = File.GetCreationTime(srcDirCore); if (dtArx >= dtCore) // 两个目录下都有这个文件,取新的 srcfile = srcDirArx; else srcfile = srcDirCore; } else { Console.WriteLine(string.Format("{0}未找到!", project + ext)); return; } } else { srcfile = item.src_path + "\\" + project + ext; } if (!File.Exists(srcfile)) { Console.WriteLine(string.Format("{0}未找到!", srcfile)); return; } if (item.useDescRoot) { descFile = desc_root + "\\" + item.desc_path + "\\"; } else { descFile = item.desc_path + "\\"; } if (!Directory.Exists(descFile)) { Console.WriteLine(string.Format("{0}未找到!"), descFile); return; } descFile += project + ext; File.Copy(srcfile, descFile, true); Console.WriteLine(string.Format("拷贝文件从:\n {0}\n 到:\n {1} \n成功.", srcfile, descFile)); return; } } } }