Exemplo n.º 1
0
        public virtual Task <ComposedLocaleResource> ComposeLocaleResourceAsync(LocaleResource resource)
        {
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(resource.Name);

            if (String.IsNullOrEmpty(fileNameWithoutExtension))
            {
                return(null);
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation("Composing locale file found at '{0}', attempting to load.", resource.Path);
            }

            var configurationContainer =
                new ConfigurationBuilder()
                .SetBasePath(_fileSystem.RootPath)
                .AddJsonFile(_fileSystem.Combine(resource.Location, fileNameWithoutExtension + ".json"), false)
                .AddXmlFile(_fileSystem.Combine(resource.Location, fileNameWithoutExtension + ".xml"), true)
                .AddYamlFile(_fileSystem.Combine(resource.Location, fileNameWithoutExtension + ".txt"), true);
            var config = configurationContainer.Build();

            var composedLocaleResource = new ComposedLocaleResource
            {
                LocaleResource = resource
            };


            switch (fileNameWithoutExtension.ToLower())
            {
            case EmailsFileName:
            {
                composedLocaleResource.Configure <LocaleEmail>(model => new LocalizedValues <LocaleEmail>
                    {
                        Resource = resource,
                        Values   = EmailsSerializer.Parse(config)
                    });
                break;
            }

            default:
            {
                composedLocaleResource.Configure <LocaleString>(model => new LocalizedValues <LocaleString>
                    {
                        Resource = resource,
                        Values   = StringSerializer.Parse(config)
                    });
                break;
            }
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation("Completed composing locale files found in '{0}'.", resource.Path);
            }

            return(Task.FromResult(composedLocaleResource));
        }
Exemplo n.º 2
0
        private async Task <IList <ModuleDescriptor> > AvailableModulesInFolder(
            string path,
            string moduleType,
            string manifestName,
            bool manifestIsOptional)
        {
            var localList = new List <ModuleDescriptor>();

            if (string.IsNullOrEmpty(path))
            {
                return(localList);
            }

            var subfolders = _fileSystem.ListDirectories(path);

            foreach (var subfolder in subfolders)
            {
                var moduleId     = subfolder.Name;
                var manifestPath = _fileSystem.Combine(path, moduleId, manifestName);
                try
                {
                    var descriptor = await GetModuleDescriptorAsync(
                        path,
                        moduleId,
                        moduleType,
                        manifestPath,
                        manifestIsOptional);

                    if (descriptor == null)
                    {
                        continue;
                    }

                    if (descriptor.Location == null)
                    {
                        descriptor.Location = descriptor.Name.IsValidUrlSegment()
                                              ? descriptor.Name
                                              : descriptor.Id;
                    }

                    localList.Add(descriptor);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"An exception occurred whilst parsing the manifest file for module '{moduleId}' at '{manifestPath}'.");
                }
            }

            return(localList);
        }
Exemplo n.º 3
0
        private IEnumerable <IThemeDescriptor> AvailableThemesInFolder(
            string path,
            string themeType,
            string manifestName,
            bool manifestIsOptional)
        {
            var localList = new List <IThemeDescriptor>();

            if (string.IsNullOrEmpty(path))
            {
                return(localList);
            }

            var subfolders = _fileSystem.ListDirectories(path);

            foreach (var subfolder in subfolders)
            {
                var themeId      = subfolder.Name;
                var manifestPath = _fileSystem.Combine(path, themeId, manifestName);
                try
                {
                    var descriptor = GetThemeDescriptor(
                        path,
                        themeId,
                        themeType,
                        manifestPath,
                        manifestIsOptional);

                    if (descriptor == null)
                    {
                        continue;
                    }

                    localList.Add(descriptor);
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return(localList);
        }
Exemplo n.º 4
0
        public ICommandResult <IThemeDescriptor> CreateTheme(string sourceThemeId, string newThemeName)
        {
            // Create result
            var result = new CommandResult <ThemeDescriptor>();

            // Get base theme
            var baseDescriptor =
                _themeLoader.AvailableThemes.FirstOrDefault(t =>
                                                            t.Id.Equals(sourceThemeId, StringComparison.OrdinalIgnoreCase));

            // Ensure base theme exists
            if (baseDescriptor == null)
            {
                throw new Exception($"Could not locate the base theme \"{sourceThemeId}\".");
            }

            try
            {
                var newThemeId = newThemeName.ToSafeFileName();
                if (!string.IsNullOrEmpty(newThemeId))
                {
                    newThemeId = newThemeId.ToLower()
                                 .Replace(" ", "-");
                }

                // Path to the new directory for our theme
                var targetPath = _platoFileSystem.Combine(
                    _siteThemeLoader.RootPath, newThemeId);

                // Copy base theme to new directory within /Sites/{SiteName}/themes/
                _platoFileSystem.CopyDirectory(
                    baseDescriptor.FullPath,
                    targetPath,
                    true);

                // Update theme name
                baseDescriptor.Name     = newThemeName;
                baseDescriptor.FullPath = targetPath;

                // Update YAML manifest
                var update = _themeUpdater.UpdateTheme(targetPath, baseDescriptor);
                if (!update.Succeeded)
                {
                    return(result.Failed(update.Errors.ToArray()));
                }
            }
            catch (Exception e)
            {
                return(result.Failed(e.Message));
            }

            return(result.Success(baseDescriptor));
        }
Exemplo n.º 5
0
        async Task <IList <LocaleDescriptor> > AvailableLocalesInFolder(string path)
        {
            var localList = new List <LocaleDescriptor>();

            if (string.IsNullOrEmpty(path))
            {
                return(localList);
            }

            var subfolders = _fileSystem.ListDirectories(path);

            foreach (var subfolder in subfolders)
            {
                var localeId   = subfolder.Name;
                var localePath = _fileSystem.Combine(path, localeId);
                try
                {
                    var descriptor = await GetLocaleDescriptorAsync(
                        localeId,
                        localePath);

                    if (descriptor == null)
                    {
                        continue;
                    }
                    descriptor.DirectoryInfo = subfolder;
                    localList.Add(descriptor);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"An exception occurred whilst reading a locale file for locale '{localeId}' at '{localePath}'.");
                }
            }

            return(localList);
        }
Exemplo n.º 6
0
        public ICommandResult <IThemeDescriptor> UpdateTheme(
            string pathToThemeFolder,
            IThemeDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            if (string.IsNullOrEmpty(descriptor.Name))
            {
                throw new ArgumentNullException(nameof(descriptor.Name));
            }

            // Path to theme manifest file
            var fileName     = string.Format(ByThemeFileNameFormat, "txt");
            var manifestPath = _platoFileSystem.MapPath(
                _platoFileSystem.Combine(pathToThemeFolder, fileName));

            // Configure YAML configuration
            var configurationProvider = new YamlConfigurationProvider(new YamlConfigurationSource
            {
                Path     = manifestPath,
                Optional = false
            });

            // Build configuration
            foreach (var key in descriptor.Keys)
            {
                if (!string.IsNullOrEmpty(descriptor[key]))
                {
                    configurationProvider.Set(key, descriptor[key]);
                }
            }

            var result = new CommandResult <ThemeDescriptor>();

            try
            {
                configurationProvider.Commit();
            }
            catch (Exception e)
            {
                return(result.Failed(e.Message));
            }

            return(result.Success(descriptor));
        }
Exemplo n.º 7
0
        public ThemeLoader(
            IHostingEnvironment hostingEnvironment,
            IOptions <ThemeOptions> themeOptions,
            IThemeLocator themeLocator,
            IPlatoFileSystem platoFileSystem)
        {
            _themeLocator = themeLocator;

            var contentRootPath           = hostingEnvironment.ContentRootPath;
            var virtualPathToThemesFolder = themeOptions.Value.VirtualPathToThemesFolder;

            RootPath = platoFileSystem.Combine(
                contentRootPath,
                virtualPathToThemesFolder);

            InitializeThemes();
        }
Exemplo n.º 8
0
        public SiteThemeLoader(
            IOptions <ThemeOptions> themeOptions,
            IPlatoFileSystem platoFilesystem,
            IShellSettings shellSettings,
            IThemeLocator themeLocator,
            ISitesFolder sitesFolder)
        {
            _platoFileSystem = platoFilesystem;
            _themeLocator    = themeLocator;

            RootPath = platoFilesystem.Combine(
                sitesFolder.RootPath,
                shellSettings.Location,
                themeOptions.Value.VirtualPathToThemesFolder?.ToLower());;

            InitializeThemes();
        }
Exemplo n.º 9
0
 public string Combine(params string[] paths)
 {
     return(_fileSystem.Combine(paths));
 }