Example #1
0
        /// <summary>
        /// Add or remove library directories and linker dependencies in VC-project.
        /// </summary>
        public static void ManageLinkerDefinitions(this Project project, PackageReference packageReference, IEnumerable<Project> projects, IEnumerable<LibraryReference> libraries)
        {
            var path = string.Format(@"{0}\lib\{1}\",
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                packageReference.CanonicalName.Architecture);

            foreach (var configuration in project.GetConfigurations())
            {
                var value = project.GetDefinitionValue(configuration, "AdditionalLibraryDirectories");

                var paths = new HashSet<string>(value.Split(';'));

                if (projects.Any(n => n.FullName == project.FullName))
                {
                    paths.Add(path);
                }
                else
                {
                    paths.Remove(path);
                }

                project.SetDefinitionValue(configuration, "AdditionalLibraryDirectories", string.Join(";", paths));

                var configLibraries = libraries.Where(lib => lib.ConfigurationName == configuration);

                value = project.GetDefinitionValue(configuration, "AdditionalDependencies");

                var current = value.Split(';');

                var removed = configLibraries.Where(n => !n.IsChecked)
                                             .Select(n => Path.GetFileNameWithoutExtension(n.Name) + "-" + packageReference.CanonicalName.Version + ".lib");

                var added = configLibraries.Where(n => n.IsChecked)
                                           .Select(n => Path.GetFileNameWithoutExtension(n.Name) + "-" + packageReference.CanonicalName.Version + ".lib");

                var result = current.Except(removed)
                                    .Union(added);

                project.SetDefinitionValue(configuration, "AdditionalDependencies", string.Join(";", result));
            }
        }
Example #2
0
        /// <summary>
        /// Add or remove references in .NET-project.
        /// </summary>
        public static void ManageReferences(this Project project, PackageReference packageReference, IEnumerable<LibraryReference> libraries)
        {
            string path = string.Format(@"{0}\ReferenceAssemblies\{1}\{2}\{3}-{4}\",
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                packageReference.CanonicalName.Flavor, packageReference.CanonicalName.Architecture, packageReference.CanonicalName.Name, packageReference.CanonicalName.Version);

            var buildProject = project.AsMsBuildProject();
            var vsProject = (VSProject)project.Object;

            if (buildProject != null && vsProject != null)
            {
                foreach (var lib in libraries)
                {
                    var assemblyName = Path.GetFileNameWithoutExtension(lib.Name);
                    var assemblyPath = path + lib.Name;

                    Reference reference = null;

                    // Remove old references
                    do
                    {
                        try
                        {
                            reference = vsProject.References.Find(assemblyName);
                        }
                        catch { }

                        if (reference != null)
                        {
                            reference.Remove();
                        }
                    }
                    while (reference != null);

                    // Add new reference
                    if (lib.IsChecked)
                    {
                        vsProject.References.Add(assemblyPath);

                        var references = buildProject.GetAssemblyReferences();
                        var referenceItem = references.First(n => n.Item2.Name == assemblyName).Item1;

                        referenceItem.SetMetadataValue("HintPath", assemblyPath);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Add or remove include directories in VC-project.
        /// </summary>
        public static void ManageIncludeDirectories(this Project project, PackageReference packageReference, IEnumerable<Project> projects)
        {
            var path = string.Format(@"{0}\include\{1}-{2}\",
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                packageReference.CanonicalName.Name.Split('-')[0],
                packageReference.CanonicalName.Version);

            foreach (var configuration in project.GetConfigurations())
            {
                var value = project.GetDefinitionValue(configuration, "AdditionalIncludeDirectories");

                var paths = new HashSet<string>(value.Split(';'));

                if (projects.Any(n => n.FullName == project.FullName))
                {
                    paths.Add(path);
                }
                else
                {
                    paths.Remove(path);
                }

                project.SetDefinitionValue(configuration, "AdditionalIncludeDirectories", string.Join(";", paths));
            }
        }