/// <summary>
        /// Analyses the script code and returns set of locations for the assemblies referenced from the code with CS-Script directives (//css_ref).
        /// </summary>
        /// <param name="code">The script code.</param>
        /// <param name="searchDirs">The assembly search/probing directories.</param>
        /// <returns>Array of the referenced assemblies</returns>
        public string[] GetReferencedAssemblies(string code, params string[] searchDirs)
        {
            var retval = new List <string>();

            var parser = new csscript.CSharpParser(code);

            var globalProbingDirs = Environment.ExpandEnvironmentVariables(CSScript.GlobalSettings.SearchDirs).Split(",;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            var dirs = searchDirs.Concat(new string[] { Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) })
                       .Concat(parser.ExtraSearchDirs)
                       .Concat(globalProbingDirs)
                       .ToArray();

            dirs = CSScript.RemovePathDuplicates(dirs);

            var asms = new List <string>(parser.RefAssemblies);

            if (!parser.IgnoreNamespaces.Any(x => x == "*"))
            {
                asms.AddRange(parser.RefNamespaces.Except(parser.IgnoreNamespaces));
            }

            foreach (var asm in asms)
            {
                foreach (string asmFile in AssemblyResolver.FindAssembly(asm, dirs))
                {
                    retval.Add(asmFile);
                }
            }

            return(retval.Distinct().ToArray());
        }
예제 #2
0
        /// <summary>
        /// References the given assembly by the assembly path.
        /// <para>It is safe to call this method multiple times for the same assembly. If the assembly already referenced it will not
        /// be referenced again.</para>
        /// </summary>
        /// <param name="assembly">The path to the assembly file.</param>
        /// <returns>The instance of the <see cref="CSScriptLibrary.IEvaluator"/> to allow  fluent interface.</returns>
        public IEvaluator ReferenceAssembly(string assembly)
        {
            var globalProbingDirs = Environment.ExpandEnvironmentVariables(CSScript.GlobalSettings.SearchDirs).Split(",;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
            globalProbingDirs.Add(Assembly.GetCallingAssembly().GetAssemblyDirectoryName());

            var dirs = globalProbingDirs.ToArray();

            string asmFile = AssemblyResolver.FindAssembly(assembly, dirs).FirstOrDefault();
            if (asmFile == null)
                throw new Exception("Cannot find referenced assembly '" + assembly + "'");

            ReferenceAssembly(Utils.AssemblyLoad(asmFile));
            return this;
        }
예제 #3
0
        /// <summary>
        /// Aggregates the references from the script and its imported scripts. It is a logical equivalent od CSExecutor.AggregateReferencedAssemblies
        /// but optimized for later .NET versions (e.g LINQ) and completely decoupled. Thus it has no dependencies on internal state
        /// (e.g. settings, options.shareHostAssemblies).
        /// <para>It is the method to call for generating list of ref asms as part of the project info.</para>
        ///
        /// </summary>
        /// <param name="searchDirs">The search dirs.</param>
        /// <param name="defaultRefAsms">The default ref asms.</param>
        /// <param name="defaultNamespacess">The default namespacess.</param>
        /// <returns></returns>
        public List <string> AgregateReferences(IEnumerable <string> searchDirs, IEnumerable <string> defaultRefAsms, IEnumerable <string> defaultNamespacess)
        {
            var probingDirs = searchDirs.ToArray();

            var refPkAsms = this.ResolvePackages(true); //suppressDownloading

            var refCodeAsms = this.ReferencedAssemblies
                              .SelectMany(asm => AssemblyResolver.FindAssembly(asm.Replace("\"", ""), probingDirs));

            var refAsms = refPkAsms.Union(refPkAsms)
                          .Union(refCodeAsms)
                          .Union(defaultRefAsms.SelectMany(name => AssemblyResolver.FindAssembly(name, probingDirs)))
                          .Distinct()
                          .ToArray();


            //some assemblies are referenced from code and some will need to be resolved from the namespaces
            bool disableNamespaceResolving = (this.IgnoreNamespaces.Count() == 1 && this.IgnoreNamespaces[0] == "*");

            if (!disableNamespaceResolving)
            {
                var asmNames = refAsms.Select(x => Path.GetFileNameWithoutExtension(x).ToUpper()).ToArray();

                var refNsAsms = this.ReferencedNamespaces
                                .Union(defaultNamespacess)
                                .Where(name => !string.IsNullOrEmpty(name))
                                .Where(name => !this.IgnoreNamespaces.Contains(name))
                                .Where(name => !asmNames.Contains(name.ToUpper()))
                                .Distinct()
                                .SelectMany(name =>
                {
                    var asms = AssemblyResolver.FindAssembly(name, probingDirs);
                    return(asms);
                })
                                .ToArray();

                refAsms = refAsms.Union(refNsAsms).ToArray();
            }


            refAsms = FilterDuplicatedAssembliesByFileName(refAsms);
            return(refAsms.ToList());
        }