public void AddingDuplicateFileToBuildIsIgnored() { ResetGuidGenerator(); PBXProject proj = ReadPBXProject(); string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName()); string fileGuid = proj.AddFile("relative/path1.cc", "Classes/path1.cc", PBXSourceTree.Source); proj.AddFileToBuildWithFlags(target, fileGuid, "-Wno-newline"); proj.AddFileToBuildWithFlags(target, fileGuid, "-Wnewline"); // this call should be ignored Assert.AreEqual(new List <string> { "-Wno-newline" }, proj.GetCompileFlagsForFile(target, fileGuid)); }
//添加其他资源 private static void AddOtherFile(string secondFilePath, string xcodeTargetPath, string xcodeTargetGuid, MOBPathModel pathModel, PBXProject xcodeProj, Hashtable fileFlags) { string[] secondDirectories = Directory.GetFiles(secondFilePath, "*", SearchOption.AllDirectories); foreach (string lastFilePath in secondDirectories) { if ((secondFilePath.EndsWith("SDK/ShareSDK") || secondFilePath.EndsWith("SDK\\ShareSDK")) && (lastFilePath.Contains("Support/PlatformSDK") || lastFilePath.Contains("Support\\PlatformSDK"))) { continue; } // Debug.LogWarning("lastFilePath:" + lastFilePath); if (!lastFilePath.Contains(".framework") && !lastFilePath.Contains(".a") && !lastFilePath.Contains(".h") && !lastFilePath.Contains(".bundle") && !lastFilePath.Contains(".DS_Store") && !lastFilePath.Contains(".meta")) { string otherFilePath = lastFilePath.Replace(pathModel.rootPath, ""); int index = otherFilePath.LastIndexOf("\\"); int fileNameIndex = 2; if (index == -1) { fileNameIndex = 1; index = otherFilePath.LastIndexOf("/"); } //项目目录 string saveOtherFilePath = otherFilePath.Substring(0, index); string fileName = otherFilePath.Substring(index + fileNameIndex); //存放的本地目录 string saveDirectory = xcodeTargetPath + saveOtherFilePath; if (!Directory.Exists(saveDirectory)) { Directory.CreateDirectory(saveDirectory); } //将其他文件拷贝到指定目录 FileInfo fileInfo = new FileInfo(lastFilePath); string savePath = xcodeTargetPath + otherFilePath; fileInfo.CopyTo(savePath, true); //将.a 加入 proj中 if (fileFlags.ContainsKey(fileName)) { string flag = (string)fileFlags[fileName]; //Debug.Log(flag); xcodeProj.AddFileToBuildWithFlags(xcodeTargetGuid, xcodeProj.AddFile(otherFilePath.Substring(1), "MOB" + otherFilePath, PBXSourceTree.Absolute), flag); } else { xcodeProj.AddFileToBuildSection(xcodeTargetGuid, xcodeProj.AddFrameworksBuildPhase(xcodeTargetGuid), xcodeProj.AddFile(otherFilePath.Substring(1), "MOB" + otherFilePath, PBXSourceTree.Absolute)); } } } }
public void CanRemoveCompilerFlagsForFile() { ResetGuidGenerator(); PBXProject proj = ReadPBXProject(); string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName()); string fileGuid = proj.AddFile("relative/path1.cc", "Classes/path1.cc", PBXSourceTree.Source); proj.AddFileToBuildWithFlags(target, fileGuid, "-Wno-newline"); proj.SetCompileFlagsForFile(target, fileGuid, null); Assert.AreEqual(0, proj.GetCompileFlagsForFile(target, fileGuid).Count); }
public void AddSourceFileWithFlagsWorks() { ResetGuidGenerator(); PBXProject proj = ReadPBXProject(); string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName()); // check if duplicate add is ignored (we don't lose flags) proj.AddFileToBuildWithFlags(target, proj.AddFile("relative/path1.cc", "Classes/path1.cc", PBXSourceTree.Source), "-Wno-newline"); // check if we can add flags to an existing file and remove them proj.AddFileToBuild(target, proj.AddFile("relative/path2.cc", "Classes/path2.cc", PBXSourceTree.Source)); proj.AddFileToBuildWithFlags(target, proj.AddFile("relative/path3.cc", "Classes/path3.cc", PBXSourceTree.Source), "-Wno-newline"); proj = Reserialize(proj); target = proj.TargetGuidByName(PBXProject.GetUnityTargetName()); proj.SetCompileFlagsForFile(target, proj.FindFileGuidByProjectPath("Classes/path2.cc"), new List <string> { "-Wno-newline", "-O3" }); proj.SetCompileFlagsForFile(target, proj.FindFileGuidByProjectPath("Classes/path3.cc"), null); TestOutput(proj, "add_file3.pbxproj"); }
//添加其他资源 private static void AddOtherFile(string secondFilePath, string xcodeTargetPath, string xcodeTargetGuid, MOBPathModel pathModel, PBXProject xcodeProj, Hashtable fileFlags) { string[] secondDirectories = Directory.GetFiles(secondFilePath, "*", SearchOption.AllDirectories); foreach (string lastFilePath in secondDirectories) { if (!lastFilePath.Contains(".framework") && !lastFilePath.Contains(".a") && !lastFilePath.Contains(".h") && !lastFilePath.Contains(".bundle") && !lastFilePath.Contains(".DS_Store") && !lastFilePath.Contains(".meta")) { // Debug.Log("lastFilePath" + lastFilePath); string otherFilePath = lastFilePath.Replace(pathModel.rootPath, ""); int index = otherFilePath.LastIndexOf("/"); //项目目录 string saveOtherFilePath = otherFilePath.Substring(0, index); string fileName = otherFilePath.Substring(index + 1); //存放的本地目录 string saveDirectory = xcodeTargetPath + saveOtherFilePath; if (!Directory.Exists(saveDirectory)) { Directory.CreateDirectory(saveDirectory); } //将其他文件拷贝到指定目录 FileInfo fileInfo = new FileInfo(lastFilePath); string savePath = xcodeTargetPath + otherFilePath; fileInfo.CopyTo(savePath, true); //将.a 加入 proj中 Debug.Log(fileName); if (fileFlags.ContainsKey(fileName)) { string flag = (string)fileFlags[fileName]; Debug.Log(flag); xcodeProj.AddFileToBuildWithFlags(xcodeTargetGuid, xcodeProj.AddFile(savePath, "MOB" + otherFilePath, PBXSourceTree.Source), flag); } else { xcodeProj.AddFileToBuild(xcodeTargetGuid, xcodeProj.AddFile(savePath, "MOB" + otherFilePath, PBXSourceTree.Source)); } } } }
//拷贝并增加到项目 public static void CopyAndAddBuildToXcode(PBXProject pbxProject, string targetGuid, string copyDirectoryPath, string buildPath, string currentDirectoryPath, List <string> embedFrameworks, bool needToAddBuild = true) { string unityDirectoryPath = copyDirectoryPath; string xcodeDirectoryPath = buildPath; if (!string.IsNullOrEmpty(currentDirectoryPath)) { unityDirectoryPath = Path.Combine(unityDirectoryPath, currentDirectoryPath); xcodeDirectoryPath = Path.Combine(xcodeDirectoryPath, currentDirectoryPath); Delete(xcodeDirectoryPath); Directory.CreateDirectory(xcodeDirectoryPath); } foreach (string filePath in Directory.GetFiles(unityDirectoryPath)) { //过滤.meta文件 string extension = Path.GetExtension(filePath); if (extension == ExtensionName.META) { continue; } // if (extension == ExtensionName.ARCHIVE) { pbxProject.AddBuildProperty(targetGuid, XcodeProjectSetting.LIBRARY_SEARCH_PATHS_KEY, XcodeProjectSetting.PROJECT_ROOT + currentDirectoryPath); } string fileName = Path.GetFileName(filePath); string copyPath = Path.Combine(xcodeDirectoryPath, fileName); //有可能是.DS_Store文件,直接过滤 if (fileName[0] == '.') { continue; } File.Delete(copyPath); File.Copy(filePath, copyPath); if (needToAddBuild) { string relativePath = Path.Combine(currentDirectoryPath, fileName); //特殊化处理,XMain目录下文件添加flags:-fno-objc-arc if (relativePath.Contains(ExtensionName.XMain)) { pbxProject.AddFileToBuildWithFlags(targetGuid, pbxProject.AddFile(relativePath, relativePath, PBXSourceTree.Source), "-fno-objc-arc"); } else { pbxProject.AddFileToBuild(targetGuid, pbxProject.AddFile(relativePath, relativePath, PBXSourceTree.Source)); } } } foreach (string directoryPath in Directory.GetDirectories(unityDirectoryPath)) { string directoryName = Path.GetFileName(directoryPath); if (directoryName.Contains(ExtensionName.LANGUAGE) && needToAddBuild) { //特殊化处理本地语言,暂时官方PBXProject不支持AddLocalization方法,如果需要,则必须自己扩充 string relativePath = Path.Combine(currentDirectoryPath, directoryName); CopyAndAddBuildToXcode(pbxProject, targetGuid, copyDirectoryPath, buildPath, relativePath, embedFrameworks, false); string[] dirs = Directory.GetDirectories(Path.Combine(xcodeDirectoryPath, directoryName)); if (dirs.Length > 0) { string fileName = Path.GetFileName(Directory.GetFiles(dirs[0], "*.strings")[0]); AddLocalizedStrings(pbxProject, buildPath, fileName, directoryPath, directoryName); } } else { bool nextNeedToAddBuild = needToAddBuild; if (directoryName.Contains(ExtensionName.FRAMEWORK) || directoryName.Contains(ExtensionName.BUNDLE) || directoryName == XcodeProjectSetting.IMAGE_XCASSETS_DIRECTORY_NAME) { nextNeedToAddBuild = false; } CopyAndAddBuildToXcode(pbxProject, targetGuid, copyDirectoryPath, buildPath, Path.Combine(currentDirectoryPath, directoryName), embedFrameworks, nextNeedToAddBuild); if (directoryName.Contains(ExtensionName.FRAMEWORK)) { string relativePath = Path.Combine(currentDirectoryPath, directoryName); string fileGuid = pbxProject.AddFile(relativePath, relativePath, PBXSourceTree.Source); pbxProject.AddFileToBuild(targetGuid, fileGuid); pbxProject.AddBuildProperty(targetGuid, XcodeProjectSetting.FRAMEWORK_SEARCH_PATHS_KEY, XcodeProjectSetting.PROJECT_ROOT + currentDirectoryPath); if (embedFrameworks.Contains(directoryName)) { PBXProjectExtensions.AddFileToEmbedFrameworks(pbxProject, targetGuid, fileGuid); } } else if (directoryName.Contains(ExtensionName.BUNDLE) && needToAddBuild) { string relativePath = Path.Combine(currentDirectoryPath, directoryName); string fileGuid = pbxProject.AddFile(relativePath, relativePath, PBXSourceTree.Source); pbxProject.AddFileToBuild(targetGuid, fileGuid); pbxProject.AddBuildProperty(targetGuid, XcodeProjectSetting.FRAMEWORK_SEARCH_PATHS_KEY, XcodeProjectSetting.PROJECT_ROOT + currentDirectoryPath); } } } }