示例#1
0
        public virtual void AddReference(string referencePath, Stream stream)
        {
            string name = Path.GetFileNameWithoutExtension(referencePath);

            try {
                // Get the full path to the reference
                string fullPath = PathUtility.GetAbsolutePath(Root, referencePath);

                // Add a reference to the project
                Reference reference = Project.Object.References.Add(fullPath);

                // Always set copy local to true for references that we add
                reference.CopyLocal = true;

                // This happens if the assembly appears in any of the search paths that VS uses to locate assembly references.
                // Most commmonly, it happens if this assembly is in the GAC or in the output path.
                if (!reference.Path.Equals(fullPath, StringComparison.OrdinalIgnoreCase))
                {
                    // Get the msbuild project for this project
                    MsBuildProject buildProject = Project.AsMSBuildProject();

                    if (buildProject != null)
                    {
                        // Get the assembly name of the reference we are trying to add
                        AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath);

                        // Try to find the item for the assembly name
                        MsBuildProjectItem item = (from assemblyReferenceNode in buildProject.GetAssemblyReferences()
                                                   where AssemblyNamesMatch(assemblyName, assemblyReferenceNode.Item2)
                                                   select assemblyReferenceNode.Item1).FirstOrDefault();

                        if (item != null)
                        {
                            // Add the <HintPath> metadata item as a relative path
                            item.SetMetadataValue("HintPath", referencePath);

                            // Save the project after we've modified it.
                            Project.Save();
                        }
                    }
                }

                Logger.Log(MessageLevel.Debug, VsResources.Debug_AddReference, name, ProjectName);
            }
            catch (Exception e) {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, VsResources.FailedToAddReference, name), e);
            }
        }
示例#2
0
        public virtual void AddReference(string referencePath, Stream stream)
        {
            string name = Path.GetFileNameWithoutExtension(referencePath);

            try
            {
                // Get the full path to the reference
                string fullPath = PathUtility.GetAbsolutePath(Root, referencePath);

                string assemblyPath = fullPath;
                bool   usedTempFile = false;

                // There is a bug in Visual Studio whereby if the fullPath contains a comma,
                // then calling Project.Object.References.Add() on it will throw a COM exception.
                // To work around it, we copy the assembly into temp folder and add reference to the copied assembly
                if (fullPath.Contains(","))
                {
                    string tempFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(fullPath));
                    File.Copy(fullPath, tempFile, true);
                    assemblyPath = tempFile;
                    usedTempFile = true;
                }

                // Add a reference to the project
                Reference reference = Project.Object.References.Add(assemblyPath);

                // if we copied the assembly to temp folder earlier, delete it now since we no longer need it.
                if (usedTempFile)
                {
                    try
                    {
                        File.Delete(assemblyPath);
                    }
                    catch
                    {
                        // don't care if we fail to delete a temp file
                    }
                }

                TrySetCopyLocal(reference);

                // This happens if the assembly appears in any of the search paths that VS uses to locate assembly references.
                // Most commonly, it happens if this assembly is in the GAC or in the output path.
                if (!reference.Path.Equals(fullPath, StringComparison.OrdinalIgnoreCase))
                {
                    // Get the msbuild project for this project
                    MsBuildProject buildProject = Project.AsMSBuildProject();

                    if (buildProject != null)
                    {
                        // Get the assembly name of the reference we are trying to add
                        AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath);

                        // Try to find the item for the assembly name
                        MsBuildProjectItem item = (from assemblyReferenceNode in buildProject.GetAssemblyReferences()
                                                   where AssemblyNamesMatch(assemblyName, assemblyReferenceNode.Item2)
                                                   select assemblyReferenceNode.Item1).FirstOrDefault();

                        if (item != null)
                        {
                            // Add the <HintPath> metadata item as a relative path
                            item.SetMetadataValue("HintPath", referencePath);

                            // Save the project after we've modified it.
                            Project.Save();
                        }
                    }
                }

                Logger.Log(MessageLevel.Debug, VsResources.Debug_AddReference, name, ProjectName);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, VsResources.FailedToAddReference, name), e);
            }
        }
 private ProjectProperty GetLinkedProperty(Project p)
 {
     return(p.AsMSBuildProject().GetProperty(LinkedPropertyName));
 }
 public static void RemoveImportStatement(this Project project, string targetsPath)
 {
     NuGet.MSBuildProjectUtility.RemoveImportStatement(project.AsMSBuildProject(), targetsPath);
 }
 public static void AddImportStatement(this Project project, string targetsPath, ProjectImportLocation location)
 {
     NuGet.MSBuildProjectUtility.AddImportStatement(project.AsMSBuildProject(), targetsPath, location);
 }
示例#6
0
 private ProjectProperty GetDisableFastUpToDateCheck(Project p)
 {
     return(p.AsMSBuildProject().GetProperty(DisableFastUpToDateCheckPropertyName));
 }
        private ProjectProperty GetLinkedProperty(Project p)
        {
            return p.AsMSBuildProject().GetProperty(LinkedPropertyName);
   

        }
        private ProjectProperty GetDisableFastUpToDateCheck(Project p)
        {
            return p.AsMSBuildProject().GetProperty(DisableFastUpToDateCheckPropertyName);


        }