Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of @import directives in sass #378

Merged
merged 3 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions VirtoCommerce.LiquidThemeEngine/ISassFileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using LibSassHost;

namespace VirtoCommerce.LiquidThemeEngine
{
public interface ISassFileManager: IFileManager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to add some comments to this abstraction with a description of the purpose.

{
string CurrentDirectory { get; set; }
}
}
50 changes: 50 additions & 0 deletions VirtoCommerce.LiquidThemeEngine/SassFileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.IO;
using LibSassHost;
using VirtoCommerce.Storefront.Model;
using VirtoCommerce.Storefront.Model.Common;
using VirtoCommerce.Storefront.Model.StaticContent;

namespace VirtoCommerce.LiquidThemeEngine
{
public class SassFileManager: ISassFileManager
{
private readonly IContentBlobProvider _contentBlobProvider;

public bool SupportsConversionToAbsolutePath { get; } = false;

public string CurrentDirectory { get; set; }

public SassFileManager(IContentBlobProvider contentBlobProvider)
{
_contentBlobProvider = contentBlobProvider;
}

public string GetCurrentDirectory() => CurrentDirectory;

public bool FileExists(string path)
asvishnyakov marked this conversation as resolved.
Show resolved Hide resolved
asvishnyakov marked this conversation as resolved.
Show resolved Hide resolved
{
// Workaround for directories
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
return false;
}
return _contentBlobProvider.PathExists(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cosmetic note: we have a stylecop rule which required add empty space after closed brackets.
see: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1513.md
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1508.md

we going to use stylecop in all backend projects so it can be simply corrected now

}

public bool IsAbsolutePath(string path)
{
return Path.GetDirectoryName(path).StartsWith(CurrentDirectory);
}

public string ToAbsolutePath(string path)
{
throw new NotImplementedException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it a part of interface implementation then it must be implemented anyway. Otherwise, this is a violation of SOLID principles

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is 3rd party interface and we can't change it.

}

public string ReadFile(string path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you read the file why you return the string?
Maybe ReadFileAsString should be better named in this context

{
return _contentBlobProvider.OpenRead(path).ReadToString();
}
}
}
11 changes: 7 additions & 4 deletions VirtoCommerce.LiquidThemeEngine/ShopifyLiquidThemeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ public class ShopifyLiquidThemeEngine : ILiquidThemeEngine, ITemplateLoader
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IStorefrontMemoryCache _memoryCache;
private readonly IContentBlobProvider _themeBlobProvider;
private readonly ISassFileManager _sassFileManager;

public ShopifyLiquidThemeEngine(IStorefrontMemoryCache memoryCache, IWorkContextAccessor workContextAccessor,
IHttpContextAccessor httpContextAccessor,
IStorefrontUrlBuilder storeFrontUrlBuilder, IContentBlobProvider contentBlobProvder, IOptions<LiquidThemeEngineOptions> options)
public ShopifyLiquidThemeEngine(IStorefrontMemoryCache memoryCache, IWorkContextAccessor workContextAccessor, IHttpContextAccessor httpContextAccessor,
IStorefrontUrlBuilder storeFrontUrlBuilder, IContentBlobProvider contentBlobProvider, ISassFileManager sassFileManager, IOptions<LiquidThemeEngineOptions> options)
{
_workContextAccessor = workContextAccessor;
_httpContextAccessor = httpContextAccessor;
UrlBuilder = storeFrontUrlBuilder;
_options = options.Value;
_memoryCache = memoryCache;
_themeBlobProvider = contentBlobProvder;
_themeBlobProvider = contentBlobProvider;
_sassFileManager = sassFileManager;
SassCompiler.FileManager = sassFileManager;
}

/// <summary>
Expand Down Expand Up @@ -184,6 +186,7 @@ public async Task<Stream> GetAssetStreamAsync(string filePath)
try
{
//handle scss resources
_sassFileManager.CurrentDirectory = Path.GetDirectoryName(filePath);
var result = SassCompiler.Compile(content);
content = result.CompiledContent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public static void AddLiquidViewEngine(this IServiceCollection services, Action<
{
throw new ArgumentNullException(nameof(services));
}


services.AddSingleton<ISassFileManager, SassFileManager>();
services.AddSingleton<ILiquidThemeEngine, ShopifyLiquidThemeEngine>();
services.AddSingleton<ILiquidViewEngine, LiquidThemedViewEngine>();
if (setupAction != null)
Expand Down