private static void PublishUpdatedRemoteSourceConfig(RemoteSourcePublishOptions options, TextWriter output)
        {
            output.WriteLine();
            output.WriteCommandText("Generating updated configuration file info (config.json)...");

            var targetFile = Path.Combine(Path.Combine(Path.GetTempPath(), "config.json"));

            Fs.SafeDeleteFile(targetFile);

            TemplatesSourceConfig config = RemoteSource.GetTemplatesSourceConfig(options.StorageAccount, options.Env);

            using (FileStream fs = new FileStream(targetFile, FileMode.CreateNew, FileAccess.Write))
            {
                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.NullValueHandling = NullValueHandling.Ignore;
                settings.Converters.Add(new StringEnumConverter());
                settings.Converters.Add(new VersionConverter());
                string content = JsonConvert.SerializeObject(config, settings);

                sw.WriteLine(content);
                sw.Flush();
            }

            output.WriteCommandText("Updating CND configuration file (config.json)...");
            output.WriteCommandText(RemoteSource.UploadElement(options.StorageAccount, options.AccountKey, options.Env.ToString().ToLowerInvariant(), targetFile));
        }
        public static TemplatesSourceConfig GetTemplatesSourceConfig(string storageAccount, EnvEnum environment)
        {
            string env = environment.ToString().ToLowerInvariant();

            CloudBlobContainer container = GetContainerAnonymous(storageAccount, env);
            var remoteElements           = RemoteSource.GetAllElements(container);
            var remotePackages           = remoteElements.Where(e => e != null && e.Name.StartsWith(env, StringComparison.OrdinalIgnoreCase) && e.Name.EndsWith(".mstx", StringComparison.OrdinalIgnoreCase))
                                           .Select((e) =>
                                                   new TemplatesPackageInfo()
            {
                Name           = e.Name,
                Bytes          = e.Properties.Length,
                Date           = e.Properties.LastModified.Value.DateTime,
                Platform       = e.Metadata.ContainsKey("platform") ? e.Metadata["platform"] : string.Empty,
                Language       = e.Metadata.ContainsKey("language") ? e.Metadata["language"] : string.Empty,
                WizardVersions = e.Metadata.ContainsKey("wizardversion") ? e.Metadata["wizardversion"].Split(';').Select(v => new Version(v)).ToList() : new List <Version>(),
            })
                                           .OrderByDescending(e => e.Date)
                                           .OrderByDescending(e => e.Version)
                                           .GroupBy(e => new { e.MainVersion, e.Language, e.Platform })
                                           .Select(e => e.FirstOrDefault());

            TemplatesSourceConfig config = new TemplatesSourceConfig()
            {
                Latest   = remotePackages.FirstOrDefault(),
                Versions = remotePackages.ToList(),
                RootUri  = container.Uri,
            };

            return(config);
        }
        private static TemplatesSourceConfig GetConfigFromCdn(EnvEnum env)
        {
            Fs.SafeDeleteFile(Path.Combine(Path.GetTempPath(), "config.json"));
            string configFile = RemoteSource.DownloadCdnElement(Environments.CdnUrls[env], "config.json", Path.GetTempPath());

            TemplatesSourceConfig config = TemplatesSourceConfig.LoadFromFile(configFile);

            return(config);
        }
 private static TemplatesSourceConfig GetConfig(RemoteSourceListOptions options, TextWriter output)
 {
     if (options.Build)
     {
         output.WriteCommandText("Building Remote Templates Source Configuration information...");
         return(RemoteSource.GetTemplatesSourceConfig(options.StorageAccount, options.Env));
     }
     else
     {
         output.WriteCommandText("Getting config file from the CDN (config.json)...");
         return(GetConfigFromCdn(options.Env));
     }
 }
        public static void PublishContent(RemoteSourcePublishOptions options, TextWriter output, TextWriter error)
        {
            try
            {
                var shortLanguage = ProgrammingLanguages.GetShortProgrammingLanguage(options.Language);
                output.WriteCommandHeader($"Publishing {options.File} for environment {options.Env.ToString()} ({options.StorageAccount}), Platform: {options.Platform}, Language: {shortLanguage} and WizardVersion {options.WizardVersion}");
                output.WriteCommandText("Uploading template package...");
                output.WriteCommandText(RemoteSource.UploadTemplatesContent(options.StorageAccount, options.AccountKey, options.Env.ToString().ToLowerInvariant(), options.File, options.Version, options.Platform, shortLanguage, options.WizardVersion));

                PublishUpdatedRemoteSourceConfig(options, output);
            }
            catch (Exception ex)
            {
                error.WriteException(ex, $"Unable to publish the file {options.File} content to the specified environment container.");
            }
        }
        public static void DownloadConfig(RemoteSourceDownloadOptions options, TextWriter output, TextWriter error)
        {
            try
            {
                output.WriteCommandHeader($"Downloading template source config file from environment {options.Env.ToString()} ({options.StorageAccount})");
                output.WriteLine();

                var result = RemoteSource.DownloadCdnElement(Environments.CdnUrls[options.Env], "config.json", options.Destination);

                output.WriteCommandText($"Successfully downloaded '{result}'");
                output.WriteLine();
            }
            catch (Exception ex)
            {
                error.WriteException(ex, $"Unable to download the config file from the specified environment.");
            }
        }
        public static void DownloadContent(RemoteSourceDownloadOptions options, TextWriter output, TextWriter error)
        {
            try
            {
                var shortLanguage = ProgrammingLanguages.GetShortProgrammingLanguage(options.Language);

                output.WriteCommandHeader($"Downloading templates content from environment {options.Env.ToString()} ({options.StorageAccount})");

                TemplatesSourceConfig config = GetConfigFromCdn(options.Env);

                TemplatesPackageInfo package = null;
                if (options.Version != null)
                {
                    package = ResolvePackageForVersion(config, options.Version, options.Platform, shortLanguage, output);
                }
                else
                {
                    package = config.Latest;
                }

                if (package != null)
                {
                    Fs.EnsureFolderExists(options.Destination);

                    var result = RemoteSource.DownloadCdnElement(Environments.CdnUrls[options.Env], package.Name, options.Destination);
                    output.WriteLine();
                    output.WriteCommandText($"Successfully downloaded '{result}'");
                    output.WriteLine();
                }
                else
                {
                    output.WriteLine();
                    output.WriteCommandText($"Package not found for the version '{options.Version}', platform '{options.Platform}' and laguage '{options.Language}'");
                    output.WriteLine();
                }
            }
            catch (Exception ex)
            {
                error.WriteException(ex, $"Unable to download the file content from the specified environment.");
            }
        }