public static UnitySolution Parse(string unitypath) { //check unity project; if (string.IsNullOrEmpty(unitypath)) { return(null); } var dirInfo = new DirectoryInfo(unitypath); var dirname = dirInfo.Name; var slnpath = Path.Combine(unitypath, dirname + ".sln"); if (!File.Exists(slnpath)) { return(null); } string slnContent = File.ReadAllText(slnpath); bool isvs = true; if (slnContent.ToLower().Contains("assembly-csharp.csproj")) { isvs = false; } DirectoryInfo dirinfo = new DirectoryInfo(unitypath); if (!dirinfo.Exists) { return(null); } var assetpath = Path.Combine(dirinfo.FullName, "Assets"); if (!Directory.Exists(assetpath)) { UnityEngine.Debug.Log("Invalid Unity project path"); return(null); } var csprojPathEditor = Path.Combine(dirinfo.FullName, dirinfo.Name + ".Editor.csproj"); if (!isvs) { csprojPathEditor = Path.Combine(dirinfo.FullName, "Assembly-CSharp-Editor.csproj"); } if (!File.Exists(csprojPathEditor)) { UnityEngine.Debug.LogError("Missing " + csprojPathEditor); } var solution = new UnitySolution(); solution.AssemblyCSharpEditor = UnityCSProject.Parse(csprojPathEditor); return(solution); }
public static UnityCSProject Parse(string path) { if (string.IsNullOrEmpty(path)) { return(null); } if (!path.ToLower().EndsWith(".csproj")) { return(null); } if (!File.Exists(path)) { return(null); } var xproject = XElement.Load(path); string xns = xproject.Name.NamespaceName; UnityCSProject csproj = new UnityCSProject(); try { List <CompileItem> compileFiles = new List <CompileItem>(); List <ReferenceItem> referenceItems = new List <ReferenceItem>(); List <AnalyzerItem> analyzerItems = new List <AnalyzerItem>(); var itemgroups = xproject.Elements(XName.Get("ItemGroup", xns)); foreach (var itemgroup in itemgroups) { var compiles = itemgroup.Elements(XName.Get("Compile", xns)); foreach (var compile in compiles) { var compilePath = compile.Attribute(XName.Get("Include")); if (compilePath == null) { continue; } compileFiles.Add(new CompileItem(compilePath.Value)); } var references = itemgroup.Elements(XName.Get("Reference", xns)); foreach (var reference in references) { var attrName = reference.Attribute(XName.Get("Include")).Value; var hintPath = reference.Element(XName.Get("HintPath", xns)).Value(); referenceItems.Add(new ReferenceItem(attrName, hintPath)); } var analyzers = itemgroup.Elements(XName.Get("Analyzer", xns)); foreach (var analyzer in analyzers) { var analyzerPath = analyzer.Attribute(XName.Get("Include")); if (analyzerPath == null) { continue; } analyzerItems.Add(new AnalyzerItem(analyzerPath.Value)); } } csproj.CompileItems = compileFiles; csproj.ReferenceItems = referenceItems; csproj.AnalyzerItems = analyzerItems; //PropettyGroup var propertyGroups = xproject.Elements(XName.Get("PropertyGroup", xns)); foreach (var propertyGroup in propertyGroups) { var condition = propertyGroup.Attribute(XName.Get("Condition")); if (condition == null) { //parse main config csproj.AssemblyName = propertyGroup.Element(XName.Get("AssemblyName", xns)).Value(); csproj.BaseDirectory = propertyGroup.Element(XName.Get("BaseDirectory", xns)).Value(); csproj.FileAlignment = propertyGroup.Element(XName.Get("FileAlignment", xns)).Value(); csproj.OutputType = propertyGroup.Element(XName.Get("OutputType", xns)).Value(); csproj.ProductVersion = propertyGroup.Element(XName.Get("ProductVersion", xns)).Value(); csproj.ProjectGuid = propertyGroup.Element(XName.Get("ProjectGuid", xns)).Value(); csproj.SchemaVersion = propertyGroup.Element(XName.Get("SchemaVersion", xns)).Value(); csproj.TargetFrameworkVersion = propertyGroup.Element(XName.Get("TargetFrameworkVersion", xns)).Value(); } else { var conditionDesc = condition.Value; if (conditionDesc.Contains("Debug")) { csproj.PropertyDebug = new PropertyGroup(propertyGroup, xns); } else if (conditionDesc.Contains("Release")) { csproj.PropertyRelease = new PropertyGroup(propertyGroup, xns); } } } } catch (Exception e) { Debug.LogException(e); } csproj.m_projPath = path; csproj.m_xproject = xproject; csproj.m_xnamespace = xns; return(csproj); }