Ejemplo n.º 1
0
        public static RemoteTemplatesSourceConfig GetRemoteTemplatesSourceConfig(string storageAccount, EnvEnum environment)
        {
            string env = environment.ToString().ToLowerInvariant();

            CloudBlobContainer container = RemoteSource.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 RemoteTemplatesPackage()
            {
                Name       = e.Name,
                StorageUri = e.Uri,
                Bytes      = e.Properties.Length,
                Date       = e.Properties.LastModified.Value.DateTime,
                Version    = ParseVersion(e.Name),
            })
                                           .OrderByDescending(e => e.Date)
                                           .OrderByDescending(e => e.Version)
                                           .GroupBy(e => e.MainVersion)
                                           .Select(e => e.FirstOrDefault());

            RemoteTemplatesSourceConfig config = new RemoteTemplatesSourceConfig()
            {
                VersionCount = remotePackages.Count(),
                Latest       = remotePackages.FirstOrDefault(),
                Versions     = remotePackages.ToList()
            };

            return(config);
        }
Ejemplo n.º 2
0
        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);

            RemoteTemplatesSourceConfig config = RemoteSource.GetRemoteTemplatesSourceConfig(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());
                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));
        }
Ejemplo n.º 3
0
        private static void WriteSummary(RemoteTemplatesSourceConfig config, TextWriter output)
        {
            output.WriteCommandText($"Templates Package count: {config.VersionCount}");
            output.WriteCommandText($"Latest Version: {config.Latest?.MainVersion} ({config.Latest.Version?.ToString()})");
            output.WriteCommandText($"Latest Version Uri: {config.Latest.StorageUri}");

            output.WriteLine();
            output.WriteCommandText($"Available Versions: {string.Join(", ", config.Versions.Select(e => e.MainVersion).ToArray())}");
        }
Ejemplo n.º 4
0
        private static RemoteTemplatesSourceConfig GetConfigFromCdn(EnvEnum env)
        {
            Fs.SafeDeleteFile(Path.Combine(Path.GetTempPath(), "config.json"));
            string configFile = RemoteSource.DownloadCdnElement(Environments.CdnUrls[env], "config.json", Path.GetTempPath());

            RemoteTemplatesSourceConfig config = RemoteTemplatesSourceConfig.LoadFromFile(configFile);

            return(config);
        }
Ejemplo n.º 5
0
        private static RemoteTemplatesPackage ResolvePackageForVersion(RemoteTemplatesSourceConfig config, string version, TextWriter output)
        {
            Version v = new Version(version);

            if (v.Build != 0 || v.Revision != 0)
            {
                output.WriteCommandText($"WARN: Downloading main version for {v.Major}.{v.Minor}, ignoring the version parts build ({v.Build}) and revision ({v.Revision}).");
            }

            RemoteTemplatesPackage match = config.Versions.Where(p => p.Version.Major == v.Major && p.Version.Minor == v.Minor)
                                           .OrderByDescending(p => p.Date).FirstOrDefault();

            return(match);
        }
Ejemplo n.º 6
0
        public static void ListSummaryInfo(RemoteSourceListOptions options, TextWriter output, TextWriter error)
        {
            try
            {
                output.WriteCommandHeader($"Summary Info for environment {options.Env.ToString()} ({options.StorageAccount})");
                RemoteTemplatesSourceConfig config = GetConfig(options, output);
                output.WriteLine();

                WriteSummary(config, output);
            }
            catch (Exception ex)
            {
                error.WriteException(ex, $"Unable to list remote source info from enviorment {options.Env.ToString()} (storage account: {options.StorageAccount}).");
            }
        }
Ejemplo n.º 7
0
        public static void DownloadContent(RemoteSourceDownloadOptions options, TextWriter output, TextWriter error)
        {
            try
            {
                output.WriteCommandHeader($"Downloading templates content from environment {options.Env.ToString()} ({options.StorageAccount})");

                RemoteTemplatesSourceConfig config = GetConfigFromCdn(options.Env);

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

                if (package != null)
                {
                    Fs.EnsureFolder(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}'");
                    output.WriteLine();
                }
            }
            catch (Exception ex)
            {
                error.WriteException(ex, $"Unable to download the file content from the specified environment.");
            }
        }
Ejemplo n.º 8
0
        public static RemoteTemplatesSourceConfig LoadFromFile(string filePath)
        {
            RemoteTemplatesSourceConfig result = null;

            try
            {
                if (File.Exists(filePath))
                {
                    string configData = File.ReadAllText(filePath, Encoding.UTF8);

                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    settings.NullValueHandling = NullValueHandling.Ignore;
                    settings.Converters.Add(new StringEnumConverter());
                    result = JsonConvert.DeserializeObject <RemoteTemplatesSourceConfig>(configData, settings);
                }
            }
            catch (Exception)
            {
                // TODO: Log error
                result = null;
            }

            return(result);
        }