Ejemplo n.º 1
0
        /// <summary>
        /// Creates the view models for each asset, directory, profile, project and reference of this package.
        /// </summary>
        /// <param name="loggerResult">The logger result of the current operation.</param>
        /// <param name="workProgress">A <see cref="WorkProgressViewModel"/> instance to update on progresses. Can be <c>null</c>.</param>
        /// <param name="cancellationToken">A cancellation token to cancel the load process. Can be <c>null</c>.</param>
        internal void LoadPackageInformation(LoggerResult loggerResult, WorkProgressViewModel workProgress, CancellationToken?cancellationToken = null)
        {
            if (workProgress == null)
            {
                throw new ArgumentNullException(nameof(workProgress));
            }
            var progress = workProgress.ProgressValue;

            workProgress.UpdateProgressAsync($"Processing asset {progress + 1}/{workProgress.Maximum}...", progress);

            foreach (var profile in Package.Profiles)
            {
                if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested)
                {
                    return;
                }

                var viewModel = new ProfileViewModel(Session, Package, profile, this);
                Profiles.Add(viewModel);

                foreach (var project in viewModel.Projects)
                {
                    AddProject(project);
                }
            }

            foreach (var localPackage in Package.LocalDependencies)
            {
                var viewModel = Session.AllPackages.SingleOrDefault(x => x.Match(localPackage.Id));
                if (viewModel != null)
                {
                    // ReSharper disable once ObjectCreationAsStatement - The PackageReferenceViewModel will register itself in the proper collection to keep being referenced
                    new LocalPackageReferenceViewModel(localPackage, viewModel, this, Dependencies, false);
                }
            }

            foreach (var storeDependency in Package.Meta.Dependencies)
            {
                // TODO: Use Guid at some point to retrieve the package
                var viewModel = Session.AllPackages.SingleOrDefault(x => x.Name == storeDependency.Name);
                if (viewModel != null)
                {
                    // ReSharper disable once ObjectCreationAsStatement - The PackageReferenceViewModel will register itself in the proper collection to keep being referenced
                    new StorePackageReferenceViewModel(storeDependency, viewModel, this, Dependencies, false);
                }
            }

            foreach (var asset in Package.Assets.ToList())
            {
                if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested)
                {
                    return;
                }

                var message = $"Processing asset {progress + 1}/{workProgress.Maximum}...";
                workProgress.UpdateProgressAsync(message, progress);

                var url = asset.Location;
                DirectoryBaseViewModel directory;
                var projectSourceCodeAsset = asset.Asset as IProjectAsset;
                if (projectSourceCodeAsset != null && asset.SourceProject != null)
                {
                    var project = Content.OfType <ProjectViewModel>().First(x => string.Compare(asset.SourceProject.GetFileNameWithoutExtension(), x.Name, StringComparison.InvariantCultureIgnoreCase) == 0);
                    directory = GetOrCreateProjectDirectory(project, url.GetDirectory() ?? "", false);
                }
                else
                {
                    directory = GetOrCreateAssetDirectory(url.GetDirectory() ?? "", false);
                }
                CreateAsset(directory, asset, false, loggerResult, true);
                ++progress;
            }

            FillRootAssetCollection();

            workProgress.UpdateProgressAsync("Package processed", progress);

            foreach (var explicitDirectory in Package.ExplicitFolders)
            {
                GetOrCreateAssetDirectory(explicitDirectory, false);
            }

            var pluginService = Session.ServiceProvider.Get <IAssetsPluginService>();

            foreach (var plugin in pluginService.Plugins)
            {
                foreach (var property in plugin.ProfileSettings)
                {
                    RegisterSettings(property);
                }
            }
        }