コード例 #1
0
ファイル: Downloader.cs プロジェクト: clonly/vshdp
        /// <summary>
        /// Retrieves a collection of locales available to download the help for
        /// </summary>
        /// <returns>
        /// Collection of Locales available
        /// </returns>
        /// <exception cref="WebException">
        /// If the data cannot be downloaded
        /// </exception>
        /// <exception cref="XmlException">
        /// If the data cannot be processed
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the data cannot be processed
        /// </exception>
        public ICollection<Locale> LoadAvailableLocales( Catalog nameCatalog )
        {
            if (nameCatalog == null)
            {
                throw new ArgumentNullException("nameCatalog");
            }

            return HelpIndexManager.LoadLocales( client.DownloadData( nameCatalog.Catalog_Link ) );
        }
コード例 #2
0
        /// <summary>
        /// Download the requested books and create the appropriate index files for MSDN HelpViewer
        /// </summary>
        /// <param name="bookGroups">
        /// The collection of bookGroups to with the books to download indicated by the Book.Wanted
        /// property
        /// </param>
        /// <param name="cachePath">
        /// The path where the downloaded books are cached
        /// </param>
        /// <param name="progress">
        /// Interface used to report the percentage progress back to the GUI
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If any of the parameters are null
        /// </exception>
        /// <exception cref="WebException">
        /// If the data cannot be downloaded
        /// </exception>
        /// <exception cref="XmlException">
        /// If the data cannot be processed
        /// </exception>
        /// <exception cref="IOException">
        /// If there was a problem reading or writing to the cache directory
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        /// If the user does not have permission to write to the cache directory
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the data cannot be processed
        /// </exception>
        public void DownloadBooks(ICollection <BookGroup> bookGroups, string cachePath, Catalog objCatalog, CatalogLocale objLocale, IProgress <int> progress)
        {
            if (bookGroups == null)
            {
                throw new ArgumentNullException("bookGroups");
            }

            if (cachePath == null)
            {
                throw new ArgumentNullException("cachePath");
            }

            if (objCatalog == null)
            {
                throw new ArgumentNullException("nameCatalog");
            }

            if (objLocale == null)
            {
                throw new ArgumentNullException("catalogLocale");
            }

            string nameCatalog = objCatalog.DisplayName;
            string codeLocale  = objLocale.Locale;

            //if ( progress == null )
            //{
            //	throw new ArgumentNullException( "progress" );
            //}

            DateTime      lastModifiedTimeBook          = new DateTime(2000, 1, 1, 0, 0, 0);
            DateTime      lastModifiedTimeBookGroup     = new DateTime(2000, 1, 1, 0, 0, 0);
            DateTime      lastModifiedTimeCatalogLocale = new DateTime(2000, 1, 1, 0, 0, 0);
            List <string> strLocales = new List <string>();
            // Create list of unique packages for possible download and write the book group and
            // book index files
            Dictionary <string, Package> packages = new Dictionary <string, Package>();

            if (objCatalog.Name == "dev10")
            {
                var lastModifiedTime = new DateTime(2000, 1, 1, 0, 0, 0);

                // Add branding packages
                var brandingPackgeName1 = new Package
                {
                    Name                         = BRANDING_PACKAGE_NAME1,
                    Deployed                     = @"true",
                    LastModified                 = DateTime.Now,
                    PackageEtag                  = null,
                    CurrentLink                  = string.Format(CultureInfo.InvariantCulture, "{0}{1}.cab", _BRANDING_PACKAGE_URL, BRANDING_PACKAGE_NAME1),
                    PackageSizeBytes             = 0,
                    PackageSizeBytesUncompressed = 0,
                    PackageConstituentLink       = string.Format(CultureInfo.InvariantCulture, "{0}{1}", @"../../serviceapi/packages/brands/", BRANDING_PACKAGE_NAME1)
                };
                brandingPackgeName1.LastModified                 = FetchLastModified(brandingPackgeName1.CurrentLink);
                brandingPackgeName1.PackageSizeBytes             = FetchContentLength(brandingPackgeName1.CurrentLink);
                brandingPackgeName1.PackageSizeBytesUncompressed = brandingPackgeName1.PackageSizeBytes;
                packages.Add(brandingPackgeName1.Name, brandingPackgeName1);
                if (brandingPackgeName1.LastModified > lastModifiedTime)
                {
                    lastModifiedTime = brandingPackgeName1.LastModified;
                }

                var brandingPackgeName2 = new Package
                {
                    Name                         = BRANDING_PACKAGE_NAME2,
                    Deployed                     = @"true",
                    LastModified                 = DateTime.Now,
                    PackageEtag                  = null,
                    CurrentLink                  = string.Format(CultureInfo.InvariantCulture, "{0}{1}.cab", _BRANDING_PACKAGE_URL, BRANDING_PACKAGE_NAME2),
                    PackageSizeBytes             = 0,
                    PackageSizeBytesUncompressed = 0,
                    PackageConstituentLink       = string.Format(CultureInfo.InvariantCulture, "{0}{1}", @"../../serviceapi/packages/brands/", BRANDING_PACKAGE_NAME2)
                };
                brandingPackgeName2.LastModified                 = FetchLastModified(brandingPackgeName2.CurrentLink);
                brandingPackgeName2.PackageSizeBytes             = FetchContentLength(brandingPackgeName2.CurrentLink);
                brandingPackgeName2.PackageSizeBytesUncompressed = brandingPackgeName2.PackageSizeBytes;
                packages.Add(brandingPackgeName2.Name, brandingPackgeName2);
                if (brandingPackgeName2.LastModified > lastModifiedTime)
                {
                    lastModifiedTime = brandingPackgeName2.LastModified;
                }
            }

            foreach (BookGroup bookGroup in bookGroups)
            {
                //File.WriteAllText(
                //    Path.Combine( cachePath, bookGroup.CreateFileName() ),
                //    HelpIndexManager.CreateBookGroupBooksIndex( bookGroup ),
                //    Encoding.UTF8 );
                //Debug.Print( "BookGroup: {0}", bookGroup.Name );
                lastModifiedTimeBookGroup = new DateTime(2000, 1, 1, 0, 0, 0);
                foreach (Book book in bookGroup.Books)
                {
                    lastModifiedTimeBook = new DateTime(2000, 1, 1, 0, 0, 0);
                    foreach (Package package in book.Packages)
                    {
                        if (package.LastModified > lastModifiedTimeBook)
                        {
                            lastModifiedTimeBook = package.LastModified;
                        }

                        if (book.Wanted)
                        {
                            string name = package.Name.ToLowerInvariant();
                            //Debug.Print( "      Package: {0}", name );

                            if (!packages.ContainsKey(name))
                            {
                                packages.Add(name, package);
                            }
                        }
                    }
                    book.LastModified = lastModifiedTimeBook;

                    if (book.LastModified > lastModifiedTimeBookGroup)
                    {
                        lastModifiedTimeBookGroup = book.LastModified;
                    }

                    if (book.Wanted)
                    {
                        //Debug.Print( "   Book: {0}", book.Name );
                        //File.WriteAllText(
                        //    Path.Combine( cachePath, book.CreateFileName() ),
                        //    HelpIndexManager.CreateBookPackagesIndex( bookGroup, book ),
                        //    Encoding.UTF8 );
                        if (!strLocales.Contains(book.Locale))
                        {
                            strLocales.Add(book.Locale);
                        }
                    }
                }
                bookGroup.LastModified = lastModifiedTimeBookGroup;
                if (bookGroup.LastModified > lastModifiedTimeCatalogLocale)
                {
                    lastModifiedTimeCatalogLocale = bookGroup.LastModified;
                }
            }

            bool include_en_us = false;

            if (0 == strLocales.Count)
            {
                return;
            }
            else if (1 == strLocales.Count)
            {
                codeLocale = strLocales[0];
            }
            else if (2 == strLocales.Count)
            {
                include_en_us = true;
                foreach (string strLoc in strLocales)
                {
                    if (strLoc.ToLowerInvariant() != "en-us")
                    {
                        codeLocale = strLoc;
                        break;
                    }
                }
            }

            if (!Directory.Exists(cachePath))
            {
                Directory.CreateDirectory(cachePath);
            }

            string vsDirectory = Path.Combine(cachePath, nameCatalog);

            if (!Directory.Exists(vsDirectory))
            {
                Directory.CreateDirectory(vsDirectory);
            }

            // Generate Download File List
            try
            {
                string listFileName = objLocale.CreatePackageListFileName();
                string listFilePath = Path.Combine(cachePath, listFileName);

                ///
                if (File.Exists(listFilePath))
                {
                    File.Delete(listFilePath);
                }

                listFilePath = Path.Combine(vsDirectory, listFileName);
                if (File.Exists(listFilePath))
                {
                    File.Delete(listFilePath);
                }

                StreamWriter writer = new StreamWriter(listFilePath);
                //IEnumerable<Package> query = packages.Values.OrderBy(package => package.CurrentLink);
                var query = packages.Values.ToList <Package>();
                query.Sort();
                foreach (Package package in query)
                {
                    writer.WriteLine(package.CurrentLink);
                }
                writer.Close();

                FileLastModifiedTime(listFilePath, lastModifiedTimeCatalogLocale);
                //File.SetCreationTime( listFilePath, lastModifiedTimeCatalogLocale );
                //File.SetLastAccessTime( listFilePath, lastModifiedTimeCatalogLocale );
                //File.SetLastWriteTime( listFilePath, lastModifiedTimeCatalogLocale );
            }
            catch (Exception e)
            {
                Program.LogException(e);
            }

            ///
            Directory.GetFiles(cachePath, "*.xml").ForEach(File.Delete);

            foreach (string file in Directory.GetFiles(cachePath, "*.msha"))
            {
                string fileName = Path.GetFileName(file);
                if (!string.IsNullOrEmpty(fileName))
                {
                    if (!fileName.Contains(@"HelpContentSetup"))
                    {
                        File.Delete(file);
                    }

                    if (fileName == @"(" + codeLocale + @")HelpContentSetup")
                    //if ( fileName == @"HelpContentSetup(" + codeLocale + @")" )
                    {
                        File.Delete(file);
                        //break;
                    }
                }
            }

            foreach (string file in Directory.GetFiles(vsDirectory, "*.msha"))
            {
                string fileName = Path.GetFileName(file);
                if (!string.IsNullOrEmpty(fileName))
                {
                    if (!fileName.Contains(@"HelpContentSetup"))
                    {
                        File.Delete(file);
                    }

                    if (fileName == @"(" + codeLocale + @")HelpContentSetup")
                    {
                        File.Delete(file);
                        //break;
                    }
                }
            }

            // Creating setup indexes
            //File.WriteAllText(
            //Path.Combine( cachePath, "HelpContentSetup.msha" ), HelpIndexManager.CreateSetupIndex( bookGroups, nameCatalog, catalogLocale ), Encoding.UTF8 );

            string xmlname = Path.Combine(vsDirectory, objLocale.CreateFileName());

            if (objCatalog.Name == "dev10")
            {
                File.WriteAllText(xmlname, HelpIndexManager.CreateSetupIndex10(bookGroups, objCatalog, objLocale, vsDirectory), Encoding.UTF8);
            }
            else
            {
                File.WriteAllText(xmlname, HelpIndexManager.CreateSetupIndex(bookGroups, objCatalog, objLocale), Encoding.UTF8);
            }

            FileLastModifiedTime(xmlname, lastModifiedTimeCatalogLocale);
            //File.SetCreationTime( xmlname, lastModifiedTimeCatalogLocale );
            //File.SetLastAccessTime( xmlname, lastModifiedTimeCatalogLocale );
            //File.SetLastWriteTime( xmlname, lastModifiedTimeCatalogLocale );

            string packagesDirectory        = Path.Combine(vsDirectory, "packages");
            string packagesDirectory_Locale = Path.Combine(packagesDirectory, codeLocale.ToLowerInvariant());
            string packagesDirectory_en_us  = Path.Combine(packagesDirectory, "en-us");

            // Cleanup old files
            //Directory.GetFiles( packagesDirectory, "*.cab" ).ForEach( File.Delete );
            CleanupOldPackages(packages, null, cachePath, false);

            string oldVsDirName = objCatalog.Name;

            if (oldVsDirName == "dev10")
            {
                oldVsDirName = "VisualStudio10";
            }
            string oldvsDirectory              = Path.Combine(cachePath, oldVsDirName);
            string oldpackagesDirectory        = Path.Combine(oldvsDirectory, @"packages");
            string oldpackagesDirectory_Locale = Path.Combine(oldpackagesDirectory, codeLocale.ToLowerInvariant());
            string oldpackagesDirectory_en_us  = Path.Combine(oldpackagesDirectory, "en-us");

            CleanupOldPackages(packages, null, oldvsDirectory, false);
            CleanupOldPackages(packages, null, oldpackagesDirectory, false);
            CleanupOldPackages(packages, codeLocale, oldpackagesDirectory_Locale, false);
            if (!codeLocale.ToLowerInvariant().Contains(@"en-us") && include_en_us)
            {
                CleanupOldPackages(packages, @"en-us", oldpackagesDirectory_en_us, false);
            }

            CleanupOldPackages(packages, null, Path.Combine(cachePath, @"packages"), false);
            CleanupOldPackages(packages, null, Path.Combine(cachePath, @"packages", codeLocale.ToLowerInvariant()), false);
            if (!codeLocale.ToLowerInvariant().Contains(@"en-us") && include_en_us)
            {
                CleanupOldPackages(packages, null, Path.Combine(cachePath, @"packages", @"en-us"), false);
            }

            CleanupOldPackages(packages, null, vsDirectory, false);
            CleanupOldPackages(packages, null, packagesDirectory, false);
            CleanupOldPackages(packages, codeLocale, packagesDirectory_Locale, true);
            if (!codeLocale.ToLowerInvariant().Contains(@"en-us") && include_en_us)
            {
                CleanupOldPackages(packages, @"en-us", packagesDirectory_en_us, true);
            }

            // Create cachePath
            if (!Directory.Exists(packagesDirectory))
            {
                Directory.CreateDirectory(packagesDirectory);
            }

            if (!Directory.Exists(packagesDirectory_Locale))
            {
                Directory.CreateDirectory(packagesDirectory_Locale);
            }

            if (include_en_us && !Directory.Exists(packagesDirectory_en_us))
            {
                Directory.CreateDirectory(packagesDirectory_en_us);
            }


            // Download the packages
            DateTime lastModifiedDir_Loc   = new DateTime(2000, 1, 1, 0, 0, 0);
            DateTime lastModifiedDir_en_us = new DateTime(2000, 1, 1, 0, 0, 0);
            int      packagesCountCurrent  = 0;

            foreach (Package package in packages.Values)
            {
                string destDirectory;
                if (package.Name.ToLowerInvariant().Contains(codeLocale.ToLowerInvariant()))
                {
                    destDirectory = packagesDirectory_Locale;
                    if (package.LastModified > lastModifiedDir_Loc)
                    {
                        lastModifiedDir_Loc = package.LastModified;
                    }
                }
                else if (package.Name.ToLowerInvariant().Contains(@"en-us"))
                {
                    destDirectory = packagesDirectory_en_us;
                    if (package.LastModified > lastModifiedDir_en_us)
                    {
                        lastModifiedDir_en_us = package.LastModified;
                    }
                }
                else
                {
                    destDirectory = packagesDirectory;
                }

                string targetFileName = Path.Combine(destDirectory, package.CreateFileName());

                // If file exist and file length is the same, skip it
                if (package.State != PackageState.NotDownloaded)
                //if ( package.State == PackageState.OutOfDate )
                {
                    if (File.Exists(targetFileName))
                    {
                        if (FetchContentLength(package.CurrentLink) != new FileInfo(targetFileName).Length)
                        {
                            package.State = PackageState.NotDownloaded;
                            File.Delete(targetFileName);
                        }
                    }
                }

                if (package.State == PackageState.NotDownloaded /*|| package.State == PackageState.OutOfDate*/)
                {
                    //Debug.Print( "         Downloading : '{0}' to '{1}'", package.CurrentLink, targetFileName );
                    client.DownloadFile(package.CurrentLink, targetFileName);
                }

                FileLastModifiedTime(targetFileName, package.LastModified);
                //File.SetCreationTime( targetFileName, package.LastModified );
                //File.SetLastAccessTime( targetFileName, package.LastModified );
                //File.SetLastWriteTime( targetFileName, package.LastModified );

                package.State = PackageState.Ready;
                progress.Report(100 * ++packagesCountCurrent / packages.Count);
            }

            FileLastModifiedTime(packagesDirectory_Locale, lastModifiedDir_Loc, true);
            if (!codeLocale.ToLowerInvariant().Contains(@"en-us") && include_en_us)
            {
                FileLastModifiedTime(packagesDirectory_en_us, lastModifiedDir_en_us, true);
            }
            FileLastModifiedTime(packagesDirectory, lastModifiedTimeCatalogLocale, true);
            FileLastModifiedTime(vsDirectory, lastModifiedTimeCatalogLocale, true);
            FileLastModifiedTime(cachePath, lastModifiedTimeCatalogLocale, true);
        }
コード例 #3
0
ファイル: Downloader.cs プロジェクト: clonly/vshdp
        /// <summary>
        /// Download the requested books and create the appropriate index files for MSDN HelpViewer
        /// </summary>
        /// <param name="bookGroups">
        /// The collection of bookGroups to with the books to download indicated by the Book.Wanted
        /// property
        /// </param>
        /// <param name="cachePath">
        /// The path where the downloaded books are cached
        /// </param>
        /// <param name="progress">
        /// Interface used to report the percentage progress back to the GUI
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If any of the parameters are null
        /// </exception>
        /// <exception cref="WebException">
        /// If the data cannot be downloaded
        /// </exception>
        /// <exception cref="XmlException">
        /// If the data cannot be processed
        /// </exception>
        /// <exception cref="IOException">
        /// If there was a problem reading or writing to the cache directory
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        /// If the user does not have permission to write to the cache directory
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If the data cannot be processed
        /// </exception>
        public void DownloadBooks( ICollection<BookGroup> bookGroups, string cachePath, Catalog objCatalog, Locale objLocale, IProgress<int> progress )
        {
            if ( bookGroups == null )
            {
                throw new ArgumentNullException( "bookGroups" );
            }

            if ( cachePath == null )
            {
                throw new ArgumentNullException( "cachePath" );
            }

            if ( objCatalog == null)
            {
                throw new ArgumentNullException( "nameCatalog" );
            }

            if ( objLocale == null)
            {
                throw new ArgumentNullException( "codeLocale" );
            }

            string nameCatalog = objCatalog.Name;
            string codeLocale = objLocale.Code;

            //if ( progress == null )
            //{
            //	throw new ArgumentNullException( "progress" );
            //}

            DateTime lastModifiedTime = new DateTime( 2000, 1, 1, 0, 0, 0 );
            List<string> strLocales = new List<string>();
            bool include_en_us = false;
            // Create list of unique packages for possible download and write the book group and
            // book index files
            Dictionary<string, Package> packages = new Dictionary<string, Package>();
            foreach ( BookGroup bookGroup in bookGroups )
            {
                //File.WriteAllText(
                //    Path.Combine( cachePath, bookGroup.CreateFileName() ),
                //    HelpIndexManager.CreateBookGroupBooksIndex( bookGroup ),
                //    Encoding.UTF8 );
                Debug.Print( "BookGroup: {0}", bookGroup.Name );
                foreach ( Book book in bookGroup.Books )
                {
                    if ( book.Wanted )
                    {
                        Debug.Print( "   Book: {0}", book.Name );
                        //File.WriteAllText(
                        //    Path.Combine( cachePath, book.CreateFileName() ),
                        //    HelpIndexManager.CreateBookPackagesIndex( bookGroup, book ),
                        //    Encoding.UTF8 );
                        if ( !strLocales.Contains( book.Locale.Name ) )
                            strLocales.Add( book.Locale.Name );

                        foreach ( Package package in book.Packages )
                        {
                            string name = string.Format( CultureInfo.InvariantCulture, "{0}({1})", package.Name, package.Tag );
                            Debug.Print( "      Package: {0}", name );

                            if ( !packages.ContainsKey( name.ToLowerInvariant() ) )
                            {
                                packages.Add( name.ToLowerInvariant(), package );
                                if ( package.LastModified > lastModifiedTime )
                                    lastModifiedTime = package.LastModified;
                            }
                        }
                    }
                }
            }

            if ( 0 == strLocales.Count )
                return;
            else if ( 1 == strLocales.Count )
                codeLocale = strLocales[0];
            else if ( 2 == strLocales.Count )
            {
                include_en_us = true;
                foreach ( string strLoc in strLocales )
                    if ( strLoc.ToLowerInvariant() != "en-us" )
                    {
                        codeLocale = strLoc;
                        break;
                    }
            }

            if (!Directory.Exists(cachePath))
                Directory.CreateDirectory(cachePath);

            string vsDirectory = Path.Combine(cachePath, nameCatalog);
            if (!Directory.Exists(vsDirectory))
                Directory.CreateDirectory(vsDirectory);

            // Generate Download File List
            try
            {
                string listFileName = string.Format( CultureInfo.InvariantCulture, "PackageList({0}).txt", codeLocale );
                string listFilePath = Path.Combine( cachePath, listFileName );

                if ( File.Exists( listFilePath ) )
                    File.Delete( listFilePath );

                listFilePath = Path.Combine( vsDirectory, listFileName );

                StreamWriter writer = new StreamWriter( listFilePath );
                foreach ( Package package in packages.Values )
                    writer.WriteLine( package.Link );
                writer.Close();

                FileLastModifiedTime( listFilePath, lastModifiedTime );
                //File.SetCreationTime( listFilePath, lastModifiedTime );
                //File.SetLastAccessTime( listFilePath, lastModifiedTime );
                //File.SetLastWriteTime( listFilePath, lastModifiedTime );
            }
            catch ( Exception e )
            {
                Program.LogException( e );
            }

            // Cleanup index files
            //Directory.GetFiles( cachePath, "*.msha" ).ForEach( File.Delete );
            Directory.GetFiles( cachePath, "*.xml" ).ForEach( File.Delete );

            foreach ( string file in Directory.GetFiles( cachePath, "*.msha" ) )
            {
                string fileName = Path.GetFileNameWithoutExtension( file );
                if ( !string.IsNullOrEmpty( fileName ) )
                {
                    if ( !fileName.Contains( @"HelpContentSetup" ) )
                    {
                        File.Delete( file );
                    }

                    if ( fileName == @"HelpContentSetup(" + codeLocale + @")" )
                    {
                        File.Delete( file );
                        //break;
                    }
                }
            }

            foreach ( string file in Directory.GetFiles( vsDirectory, "*.msha" ) )
            {
                string fileName = Path.GetFileNameWithoutExtension( file );
                if ( !string.IsNullOrEmpty( fileName ) )
                {
                    if ( !fileName.Contains( @"HelpContentSetup" ) )
                    {
                        File.Delete( file );
                    }

                    if ( fileName == @"HelpContentSetup( " + codeLocale + @")" )
                    {
                        File.Delete( file );
                        //break;
                    }
                }
            }

            // Creating setup indexes
            //File.WriteAllText(
            //Path.Combine( cachePath, "HelpContentSetup.msha" ), HelpIndexManager.CreateSetupIndex( bookGroups, nameCatalog, codeLocale ), Encoding.UTF8 );

            string xmlname = string.Format( CultureInfo.InvariantCulture, "HelpContentSetup({0}).msha", codeLocale );
            xmlname = Path.Combine( vsDirectory, xmlname );
            File.WriteAllText( xmlname, HelpIndexManager.CreateSetupIndex( bookGroups, nameCatalog, codeLocale ), Encoding.UTF8 );

            FileLastModifiedTime( xmlname, lastModifiedTime );
            //File.SetCreationTime( xmlname, lastModifiedTime );
            //File.SetLastAccessTime( xmlname, lastModifiedTime );
            //File.SetLastWriteTime( xmlname, lastModifiedTime );

            string targetDirectory = Path.Combine( vsDirectory, "packages" );
            string targetDirectory_Locale = Path.Combine( targetDirectory, codeLocale.ToLowerInvariant() );
            string targetDirectory_en_us = Path.Combine( targetDirectory, "en-us" );

            // Cleanup old files
            //Directory.GetFiles( targetDirectory, "*.cab" ).ForEach( File.Delete );
            CleanupOldPackages( packages, null, cachePath, false );

            CleanupOldPackages( packages, null, Path.Combine( cachePath, @"packages" ), false);
            CleanupOldPackages( packages, null, Path.Combine( cachePath, @"packages", codeLocale.ToLowerInvariant() ), false) ;
            if ( !codeLocale.ToLowerInvariant().Contains(@"en-us" ) && include_en_us )
                CleanupOldPackages( packages, null, Path.Combine( cachePath, @"packages", @"en-us" ), false );

            CleanupOldPackages( packages, null, vsDirectory, false );
            CleanupOldPackages( packages, null, targetDirectory, false );
            CleanupOldPackages( packages, codeLocale, targetDirectory_Locale, true );
            if ( !codeLocale.ToLowerInvariant().Contains( @"en-us" ) && include_en_us )
                CleanupOldPackages( packages, @"en-us", targetDirectory_en_us, true );

            // Create cachePath
            if ( !Directory.Exists( targetDirectory ) )
                Directory.CreateDirectory( targetDirectory );

            if ( !Directory.Exists( targetDirectory_Locale ) )
                Directory.CreateDirectory( targetDirectory_Locale );

            if ( include_en_us && !Directory.Exists( targetDirectory_en_us ) )
                Directory.CreateDirectory( targetDirectory_en_us );

            // Download the packages
            DateTime lastModifiedDir_Loc = new DateTime( 2000, 1, 1, 0, 0, 0 );
            DateTime lastModifiedDir_en_us = new DateTime( 2000, 1, 1, 0, 0, 0 );
            int packagesCountCurrent = 0;
            foreach ( Package package in packages.Values )
            {
                string destDirectory;
                if ( package.Name.ToLowerInvariant().Contains( codeLocale.ToLowerInvariant() ) )
                {
                    destDirectory = targetDirectory_Locale;
                    if ( package.LastModified > lastModifiedDir_Loc )
                        lastModifiedDir_Loc = package.LastModified;
                }
                else if ( package.Name.ToLowerInvariant().Contains( @"en-us" ) )
                {
                    destDirectory = targetDirectory_en_us;
                    if ( package.LastModified > lastModifiedDir_en_us )
                        lastModifiedDir_en_us = package.LastModified;
                }
                else
                    destDirectory = targetDirectory;

                string targetFileName = Path.Combine( destDirectory, package.CreateFileName() );

                // If file exist and file length is the same, skip it
                if ( package.State != PackageState.NotDownloaded)
                //if ( package.State == PackageState.OutOfDate )
                {
                    if ( File.Exists( targetFileName ) )
                    {
                        if ( FetchContentLength( package.Link ) != new FileInfo( targetFileName ).Length )
                        {
                            package.State = PackageState.NotDownloaded;
                            File.Delete( targetFileName );
                        }
                    }
                }

                if ( package.State == PackageState.NotDownloaded /*|| package.State == PackageState.OutOfDate*/ )
                {
                    Debug.Print( "         Downloading : '{0}' to '{1}'", package.Link, targetFileName );
                    client.DownloadFile( package.Link, targetFileName );
                }

                FileLastModifiedTime( targetFileName, package.LastModified );
                //File.SetCreationTime( targetFileName, package.LastModified );
                //File.SetLastAccessTime( targetFileName, package.LastModified );
                //File.SetLastWriteTime( targetFileName, package.LastModified );

                package.State = PackageState.Ready;
                progress.Report( 100 * ++packagesCountCurrent / packages.Count );
            }

            FileLastModifiedTime( targetDirectory_Locale, lastModifiedDir_Loc, true );
            if ( !codeLocale.ToLowerInvariant().Contains(@"en-us") && include_en_us )
                FileLastModifiedTime( targetDirectory_en_us, lastModifiedDir_en_us, true );
            FileLastModifiedTime( targetDirectory, lastModifiedTime, true );
            FileLastModifiedTime( vsDirectory, lastModifiedTime, true);
            FileLastModifiedTime( cachePath, lastModifiedTime, true );
        }
コード例 #4
0
        /// <summary>
        /// Check the current caching status of the packages so that the required downloads can be
        /// determined
        /// </summary>
        /// <param name="bookGroups">
        /// The collection of bookGroups to check the packages for
        /// </param>
        /// <param name="cachePath">
        /// The directory where the packages are locally cached
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If bookGroups or cachePath are null
        /// </exception>
        //public static void CheckPackagesStates(ICollection<BookGroup> bookGroups, string cachePath, string nameCatalog, string codeLocale)// unused
        public static void CheckPackagesStates(ICollection <BookGroup> bookGroups, string cachePath, Catalog catalog, CatalogLocale catalogLocale)// unused

        {
            if (bookGroups == null)
            {
                throw new ArgumentNullException("bookGroups");
            }

            if (cachePath == null)
            {
                throw new ArgumentNullException("cachePath");
            }

            if (catalog == null)
            {
                throw new ArgumentNullException("catalog");
            }

            if (catalogLocale == null)
            {
                throw new ArgumentNullException("catalogLocale");
            }

            //if ( !Directory.Exists( cachePath ) )
            //    return;

            string nameCatalog = catalog.DisplayName;
            string codeLocale  = catalogLocale.Locale;

            string vsPath                 = Path.Combine(cachePath, nameCatalog);
            string packagePath            = Path.Combine(vsPath, @"packages");
            string targetDirectory_Locale = Path.Combine(packagePath, codeLocale.ToLowerInvariant());
            string targetDirectory_en_us  = Path.Combine(packagePath, "en-us");

            List <string> cheakCabDirectoryPath = new List <string>();

            cheakCabDirectoryPath.Add(cachePath);
            cheakCabDirectoryPath.Add(vsPath);
            cheakCabDirectoryPath.Add(packagePath);
            cheakCabDirectoryPath.Add(targetDirectory_Locale);
            if (!codeLocale.ToLowerInvariant().Contains(@"en-us"))
            {
                cheakCabDirectoryPath.Add(targetDirectory_en_us);
            }

            string oldVsDirName = catalog.Name;

            if (oldVsDirName == "dev10")
            {
                oldVsDirName = "VisualStudio10";
            }
            string oldvsPath                 = Path.Combine(cachePath, oldVsDirName);
            string oldpackagePath            = Path.Combine(oldvsPath, @"packages");
            string oldtargetDirectory_Locale = Path.Combine(oldpackagePath, codeLocale.ToLowerInvariant());
            string oldtargetDirectory_en_us  = Path.Combine(oldpackagePath, "en-us");

            cheakCabDirectoryPath.Add(cachePath);
            cheakCabDirectoryPath.Add(oldvsPath);
            cheakCabDirectoryPath.Add(oldpackagePath);
            cheakCabDirectoryPath.Add(oldtargetDirectory_Locale);
            if (!codeLocale.ToLowerInvariant().Contains(@"en-us"))
            {
                cheakCabDirectoryPath.Add(oldtargetDirectory_en_us);
            }

            cheakCabDirectoryPath.Add(Path.Combine(cachePath, @"packages"));
            cheakCabDirectoryPath.Add(Path.Combine(cachePath, @"packages", codeLocale.ToLowerInvariant()));
            if (!codeLocale.ToLowerInvariant().Contains(@"en-us"))
            {
                cheakCabDirectoryPath.Add(Path.Combine(cachePath, @"packages", @"en-us"));
            }


            List <string> cabDirectoryPath = new List <string>();

            foreach (string directoryPath in cheakCabDirectoryPath)
            {
                if (!Directory.Exists(directoryPath))
                {
                    continue;
                }

                if (Directory.GetFiles(directoryPath, "*.cab").Length > 0)
                {
                    cabDirectoryPath.Add(directoryPath);
                }
            }

            if (!Directory.Exists(cachePath))
            {
                Directory.CreateDirectory(cachePath);
            }

            if (!Directory.Exists(vsPath))
            {
                Directory.CreateDirectory(vsPath);
            }

            if (!Directory.Exists(packagePath))
            {
                Directory.CreateDirectory(packagePath);
            }

            if (!Directory.Exists(targetDirectory_Locale))
            {
                Directory.CreateDirectory(targetDirectory_Locale);
            }

            if (!codeLocale.ToLowerInvariant().Contains(@"en-us"))
            {
                if (!Directory.Exists(targetDirectory_en_us))
                {
                    Directory.CreateDirectory(targetDirectory_en_us);
                }
            }

            if (0 == cabDirectoryPath.Count)
            {
                return;
            }

            foreach (BookGroup bookGroup in bookGroups)
            {
                foreach (Book book in bookGroup.Books)
                {
                    foreach (Package package in book.Packages)
                    {
                        string packagePathDest = packagePath;
                        if (package.Name.ToLowerInvariant().Contains(@"en-us"))
                        {
                            packagePathDest = targetDirectory_en_us;
                        }
                        else if (package.Name.ToLowerInvariant().Contains(codeLocale.ToLowerInvariant()))
                        {
                            packagePathDest = targetDirectory_Locale;
                        }

                        packagePathDest = Path.Combine(packagePathDest, package.CreateFileName());
                        FileInfo packageFileDest = new FileInfo(packagePathDest);

                        foreach (string directoryPath in cheakCabDirectoryPath)
                        {
                            string packagePathSrc = Path.Combine(directoryPath, package.CreateFileName());

                            FileInfo packageFileSrc = new FileInfo(packagePathSrc);
                            //packageFileDest = new FileInfo(packagePathDest);

                            //if (packageFileSrc.Exists)
                            //    packageFileSrc.MoveTo(Path.Combine(packagePathDest));

                            if (packagePathSrc != packagePathDest)
                            {
                                if (packageFileSrc.Exists)
                                {
                                    if (!packageFileDest.Exists)
                                    {
                                        packageFileSrc.MoveTo(Path.Combine(packagePathDest));
                                    }
                                    else
                                    {
                                        File.Delete(packagePathSrc);
                                    }
                                }
                            }
                        }

                        packageFileDest = new FileInfo(packagePathDest);
                        if (packageFileDest.Exists)
                        {
                            //if ( packageFileDest.Length == new Downloader().FetchContentLength( package.CurrentLink ) )
                            //{
                            if (packageFileDest.LastWriteTime == package.LastModified)
                            {
                                package.State = PackageState.Ready;
                            }
                            else
                            {
                                package.State = PackageState.OutOfDate;
                            }
                            //}
                            //else
                            //{
                            //    package.State = PackageState.NotDownloaded;
                            //    File.Delete( packagePath );
                            //}
                        }
                        else
                        {
                            package.State = PackageState.NotDownloaded;
                        }
                    }
                }
            }
        }