Exemple #1
0
        protected virtual void CreateChildren(IList <ProjectInfo> referenceProjects)
        {
            if (_context == null)
            {
                throw new InvalidOperationException(
                          "There is no project section context attached to this project.");
            }

            if (referenceProjects == null || referenceProjects.Count == 0)
            {
                return;
            }

            for (int i = 0; i < referenceProjects.Count; i++)
            {
                ProjectInfo referenceProject = referenceProjects[i];
                if (referenceProject == null || !referenceProject.IsValid)
                {
                    continue;
                }

                // First search the registered projects to avoid duplications
                ProjectSection childSection = _context.GetProjectSection(
                    referenceProject.ProjectGuid);
                if (childSection == null)
                {
                    childSection = ProjectSectionFactory.CreateSection(
                        _context, referenceProject.ProjectPath);
                }

                if (childSection != null)
                {
                    this.AddChild(childSection);
                }
            }
        }
Exemple #2
0
        public override bool Parse(ProjectSectionContext context, string projectFile)
        {
            _standardType = StandardProjectType.None;

            if (!base.Parse(context, projectFile))
            {
                return(false);
            }

            _standardType = ProjectSectionFactory.GetStandardType(projectFile);

            bool isSuccessful = this.ParseProperties();

            if (!isSuccessful)
            {
                // If not successful, we try parsing "Choose" elements, since the ff.
                // is equally valid as conditioned property group....

                //<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
                //    <PropertyGroup>
                //        <Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
                //        <OutputType>Exe</OutputType>
                //        <RootNamespace>Application1</RootNamespace>
                //        <AssemblyName>Application1</AssemblyName>
                //        <WarningLevel>4</WarningLevel>
                //    </PropertyGroup>
                //    <Choose>
                //        <When Condition=" '$(Configuration)'=='debug' ">
                //            <PropertyGroup>
                //                <OutputPath>.\bin\Debug\</OutputPath>
                //            </PropertyGroup>
                //        </When>
                //        <When Condition=" '$(Configuration)'=='retail' ">
                //            <PropertyGroup>
                //                <OutputPath>.\bin\Release\</OutputPath>
                //            </PropertyGroup>
                //        </When>
                //        <Otherwise>
                //            <PropertyGroup>
                //                <OutputPath>.\bin\$(Configuration)\</OutputPath>
                //            </PropertyGroup>
                //        </Otherwise>
                //    </Choose>
                //</Project>

                isSuccessful = this.ParseChoose();
            }

            if (!isSuccessful)
            {
                return(isSuccessful);
            }

            List <ProjectInfo> referencedProjects = new List <ProjectInfo>();

            isSuccessful = this.ParseReferenceItems(referencedProjects);

            this.CreateChildren(referencedProjects);

            return(isSuccessful);
        }
        protected virtual bool ParseReferenceItems(IList <ProjectInfo> referencedProjects)
        {
            ICollection <ProjectItemElement> allItems          = _project.Items;
            List <ProjectItemElement>        references        = new List <ProjectItemElement>();
            List <ProjectItemElement>        comReferences     = new List <ProjectItemElement>();
            List <ProjectItemElement>        projectReferences = new List <ProjectItemElement>();

            foreach (ProjectItemElement item in allItems)
            {
                switch (item.ItemType)
                {
                case "Reference":
                    references.Add(item);
                    break;

                case "COMReference":
                    comReferences.Add(item);
                    break;

                case "ProjectReference":
                    projectReferences.Add(item);
                    break;
                }
            }

            IList <string> referencedAssemblies      = this.ReferencedAssemblies;
            IList <string> referencedKnownAssemblies = this.ReferencedKnownAssemblies;

            foreach (ProjectItemElement item in references)
            {
                string referenceInclude = item.Include;
                if (referenceInclude.IndexOf(',') > 0)
                {
                    AssemblyName asmName = new AssemblyName(referenceInclude);

                    referenceInclude = asmName.Name;
                }

                string referencedAssembly = referenceInclude + ".dll";
                if (ProjectSectionFactory.IsKnownAssemblyName(referencedAssembly))
                {
                    referencedKnownAssemblies.Add(referencedAssembly);
                    continue;
                }

                if (item.HasMetadata)
                {
                    ProjectMetadataElement hintMetadata = null;

                    foreach (ProjectMetadataElement metadata in item.Metadata)
                    {
                        if (String.Equals(metadata.Name, "HintPath",
                                          StringComparison.OrdinalIgnoreCase))
                        {
                            hintMetadata = metadata;
                            break;
                        }
                    }

                    if (hintMetadata != null)
                    {
                        string fullPath = Path.GetFullPath(
                            Path.Combine(this.ProjectDir, hintMetadata.Value));
                        if (File.Exists(fullPath))
                        {
                            referencedAssemblies.Add(fullPath);
                        }
                    }
                }
                else if (!String.IsNullOrEmpty(this.OutputType))
                {
                    // Trying some possibilities...
                    string extension = null;
                    switch (this.OutputType.ToLower())
                    {
                    case "dll":
                    case "library":
                    case "dynamiclibrary":
                        extension = ".dll";
                        break;

                    case "exe":
                    case "application":
                        extension = ".exe";
                        break;
                    }
                    if (!String.IsNullOrEmpty(extension))
                    {
                        // A referenced file in the project directory...
                        string fullPath = Path.GetFullPath(Path.Combine(this.ProjectDir,
                                                                        referenceInclude + extension));
                        if (!File.Exists(fullPath) && (!String.IsNullOrEmpty(this.OutputPath) &&
                                                       Directory.Exists(this.OutputPath)))
                        {
                            // A referenced file in the output directory...
                            fullPath = Path.GetFullPath(Path.Combine(
                                                            this.OutputPath, referenceInclude + extension));
                        }
                        if (!String.IsNullOrEmpty(fullPath) && File.Exists(fullPath))
                        {
                            referencedAssemblies.Add(fullPath);
                        }
                    }
                }
            }

            foreach (ProjectItemElement item in comReferences)
            {
                //string referenceInclude = item.Include;
                //if (referenceInclude.IndexOf(',') > 0)
                //{
                //    AssemblyName asmName = new AssemblyName(referenceInclude);

                //    referenceInclude = asmName.Name;
                //}

                //if (item.HasMetadata)
                //{
                //    foreach (ProjectMetadataElement metadata in item.Metadata)
                //    {
                //    }
                //}
            }

            foreach (ProjectItemElement item in projectReferences)
            {
                string referenceInclude = item.Include;

                if (String.IsNullOrEmpty(referenceInclude) || !item.HasMetadata)
                {
                    continue;
                }

                string projectName = String.Empty;
                string projectGuid = String.Empty;
                foreach (ProjectMetadataElement metadata in item.Metadata)
                {
                    if (String.Equals(metadata.Name, "Project",
                                      StringComparison.OrdinalIgnoreCase))
                    {
                        projectGuid = metadata.Value;
                    }
                    else if (String.Equals(metadata.Name, "Name",
                                           StringComparison.OrdinalIgnoreCase))
                    {
                        projectName = metadata.Value;
                    }
                }

                string projectPath = String.Empty;
                if (Path.IsPathRooted(referenceInclude))
                {
                    projectPath = Path.GetFullPath(referenceInclude);
                }
                else
                {
                    projectPath = Path.GetFullPath(
                        Path.Combine(this.ProjectDir, referenceInclude));
                }

                if (File.Exists(projectPath))
                {
                    if (String.IsNullOrEmpty(projectName))
                    {
                        projectName = Path.GetFileNameWithoutExtension(projectPath);
                    }

                    ProjectInfo projectInfo = new ProjectInfo(projectPath,
                                                              projectGuid, projectName);
                    if (projectInfo.IsValid)
                    {
                        referencedProjects.Add(projectInfo);
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(projectGuid))
                    {
                        // If the path does not exist, we try looking through
                        // any available list of references...
                        ProjectInfo projectInfo = this.Context.GetProjectInfo(
                            projectGuid);
                        if (projectInfo != null && projectInfo.IsValid)
                        {
                            referencedProjects.Add(projectInfo);
                        }
                    }
                }
            }

            return(true);
        }
        private bool ParseReferenceItems(IList <ProjectInfo> referencedProjects)
        {
            bool isSuccessful = true;

            string projectDir = this.ProjectDir;

            IList <string> referencedAssemblies      = this.ReferencedAssemblies;
            IList <string> referencedKnownAssemblies = this.ReferencedKnownAssemblies;

            foreach (VcProjectReferencesElement references in _project.References)
            {
                // For the referenced .NET assemblies...
                foreach (VcProjectAssemblyReferenceElement reference
                         in references.AssemblyReferences)
                {
                    string assemblyPath = this.EvaluateMacros(reference.RelativePath);
                    if (String.IsNullOrEmpty(assemblyPath))
                    {
                        // TODO: This property is actually required and will
                        //       not be empty or null...
                        continue;
                    }
                    // For a known assembly (.NET Framework assembly)...
                    string assemblyName = Path.GetFileName(assemblyPath);
                    if (ProjectSectionFactory.IsKnownAssemblyName(assemblyName))
                    {
                        referencedKnownAssemblies.Add(assemblyName);
                        continue;
                    }

                    if (Path.IsPathRooted(assemblyPath))
                    {
                        assemblyPath = Path.GetFullPath(assemblyPath);
                    }
                    else
                    {
                        assemblyPath = Path.GetFullPath(Path.Combine(
                                                            projectDir, assemblyPath));
                    }

                    if (File.Exists(assemblyPath))
                    {
                        referencedAssemblies.Add(assemblyPath);
                    }
                }

                // For the referenced projects...
                foreach (VcProjectProjectReferenceElement reference
                         in references.ProjectReferences)
                {
                    string projectGuid = reference.ReferencedProjectIdentifier;
                    if (String.IsNullOrEmpty(projectGuid))
                    {
                        throw new InvalidDataException(
                                  "The project is not well-formed, it has project Guid.");
                    }

                    string projectPath = this.EvaluateMacros(reference.RelativePathToProject);
                    if (!String.IsNullOrEmpty(projectPath))
                    {
                        if (Path.IsPathRooted(projectPath))
                        {
                            projectPath = Path.GetFullPath(projectPath);
                        }
                        else
                        {
                            projectPath = Path.GetFullPath(Path.Combine(
                                                               projectDir, projectPath));
                        }
                    }

                    if (File.Exists(projectPath))
                    {
                        string projectName = reference.Name;
                        if (String.IsNullOrEmpty(projectName))
                        {
                            projectName = Path.GetFileNameWithoutExtension(projectPath);
                        }

                        ProjectInfo projectInfo = new ProjectInfo(projectPath,
                                                                  projectGuid, projectName);
                        if (projectInfo.IsValid)
                        {
                            referencedProjects.Add(projectInfo);
                        }
                    }
                    else
                    {
                        // If the path does not exist, we try looking through
                        // any available list of references...
                        ProjectInfo projectInfo = this.Context.GetProjectInfo(
                            projectGuid);
                        if (projectInfo != null && projectInfo.IsValid)
                        {
                            referencedProjects.Add(projectInfo);
                        }
                    }
                }

                foreach (VcProjectActiveXReferenceElement reference
                         in references.ActiveXReferences)
                {
                }
            }

            return(isSuccessful);
        }