Beispiel #1
0
        public void Add(Assembly assembly, string projectSourcePath = null)
        {
            // retrieve absolute path if available
            projectSourcePath = string.IsNullOrWhiteSpace(projectSourcePath)
                ? null
                : Path.IsPathRooted(projectSourcePath)
                    ? projectSourcePath
                    : new DirectoryInfo((Path.Combine(HttpRuntime.AppDomainAppPath, projectSourcePath))).FullName;

            // scan folders not to prevent non-optimal disk read in future
            var scannedFolders = string.IsNullOrWhiteSpace(projectSourcePath) || !Directory.Exists(projectSourcePath)
                ? new List <string>()
                : Directory.GetDirectories(projectSourcePath, "*", SearchOption.AllDirectories).ToList();

            var assemblyInfo = new VppAssemblyInfo()
            {
                Assembly          = assembly,
                ProjectSourcePath = projectSourcePath,
                ScannedSources    = scannedFolders
            };

            RegisteredAssemblies.Add(assemblyInfo);

            var assemblyName      = assembly.GetName().Name;
            var assemblyResources = assembly.GetManifestResourceNames();

            var assemblyFiles     = new List <string>();
            var resourcesFileName = assemblyName + ".vpp.json";

            using (var stream = assembly.GetManifestResourceStream(resourcesFileName))
            {
                if (stream != null)
                {
                    using (var streamReader = new StreamReader(stream))
                    {
                        var filesInfo = streamReader.ReadToEnd();
                        assemblyFiles = filesInfo.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).ToList();
                    }
                }
            }

            foreach (var resourcePath in assemblyResources.Where(r => r.StartsWith(assemblyName, true, CultureInfo.InvariantCulture)))
            {
                var key = resourcePath.ToUpperInvariant().Substring(assemblyName.Length).TrimStart('.');

                var fullName = assemblyFiles.FirstOrDefault(f => f.Replace('\\', '.').Equals(key, StringComparison.InvariantCultureIgnoreCase));

                var resource = new EmbeddedResource(assemblyInfo, resourcePath)
                {
                    FullName = fullName
                };

                resources[key] = resource;
            }
        }
        public EmbeddedResource(VppAssemblyInfo assemblyInfo, string resourcePath)
        {
            AssemblyName = assemblyInfo.Assembly.GetName().Name;
            AssemblyInfo = assemblyInfo;

            var fileInfo = new FileInfo(assemblyInfo.Assembly.Location);

            AssemblyLastModified = fileInfo.LastWriteTime;
            ResourcePath         = resourcePath;

            if (!string.IsNullOrWhiteSpace(assemblyInfo.ProjectSourcePath))
            {
                FileName = GetFileNameFromProjectSourceDirectory();

                if (FileName != null) //means that the source file was found, or a copy was in the web apps folders
                {
                    GetCacheDependency = (utcStart) => new CacheDependency(FileName, utcStart);
                    GetStream          = () => File.OpenRead(FileName);
                    return;
                }
            }
            GetCacheDependency = (utcStart) => new CacheDependency(assemblyInfo.Assembly.Location);
            GetStream          = () => assemblyInfo.Assembly.GetManifestResourceStream(resourcePath);
        }