Skip to content

Commit

Permalink
Add support for nullable (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Nov 2, 2024
1 parent daad23a commit 6f7de3c
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 61 deletions.
5 changes: 3 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="MinVer" Version="5.0.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0"/>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="MSTest" Version="3.5.0" />
<PackageVersion Include="CppAst.CodeGen" Version="0.5.0-alpha-001" />
<PackageVersion Include="PolySharp" Version="1.14.1" />
</ItemGroup>
</Project>
</Project>
2 changes: 2 additions & 0 deletions src/SharpScss/LibSass.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//------------------------------------------------------------------------------
using System;

#pragma warning disable CS8981

namespace SharpScss
{
using System.Runtime.InteropServices;
Expand Down
6 changes: 3 additions & 3 deletions src/SharpScss/LibSass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public StringUtf8(IntPtr pointer)

public bool IsEmpty => pointer == IntPtr.Zero;

public static implicit operator string(StringUtf8 stringUtf8)
public static implicit operator string?(StringUtf8 stringUtf8)
{
if (stringUtf8.pointer != IntPtr.Zero)
{
Expand All @@ -97,7 +97,7 @@ public static implicit operator string(StringUtf8 stringUtf8)
return null;
}

public static unsafe implicit operator StringUtf8(string text)
public static unsafe implicit operator StringUtf8(string? text)
{
if (text == null)
{
Expand Down Expand Up @@ -138,7 +138,7 @@ public UTF8EncodingRelaxed() : base(false, false)
internal abstract class SassAllocator
{
[ThreadStatic]
private static List<IntPtr> PointersToFree;
private static List<IntPtr>? PointersToFree;

public static void FreeNative()
{
Expand Down
67 changes: 25 additions & 42 deletions src/SharpScss/Scss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static class Scss
{
// NOTE: It is important to keep allocated delegate importer function, so that it will not be garbage collected
private static readonly LibSass.Sass_Importer_Fn ScssImporterLock;
private static string version;
private static string languageVersion;
private static string? version;
private static string? languageVersion;
private static readonly ScssOptions DefaultOptions = new ScssOptions();

static Scss()
Expand All @@ -31,7 +31,7 @@ static Scss()
/// </summary>
public static string Version
{
get { return version = version ?? LibSass.libsass_version(); }
get { return version ??= LibSass.libsass_version(); }
}

/// <summary>
Expand All @@ -49,7 +49,7 @@ public static string LanguageVersion
/// <param name="options">The options.</param>
/// <returns>The result of the conversion</returns>
/// <exception cref="System.ArgumentNullException">if scss is null</exception>
public static ScssResult ConvertToCss(string scss, ScssOptions options = null)
public static ScssResult ConvertToCss(string scss, ScssOptions? options = null)
{
if (scss == null) throw new ArgumentNullException(nameof(scss));
return FromCore(scss, false, options);
Expand All @@ -62,7 +62,7 @@ public static ScssResult ConvertToCss(string scss, ScssOptions options = null)
/// <param name="options">The options.</param>
/// <returns>The result of the conversion</returns>
/// <exception cref="System.ArgumentNullException">if scss is null</exception>
public static ScssResult ConvertFileToCss(string scssFile, ScssOptions options = null)
public static ScssResult ConvertFileToCss(string scssFile, ScssOptions? options = null)
{
if (scssFile == null) throw new ArgumentNullException(nameof(scssFile));
return FromCore(scssFile, true, options);
Expand All @@ -75,7 +75,7 @@ public static ScssResult ConvertFileToCss(string scssFile, ScssOptions options =
/// <param name="fromFile">if set to <c>true</c> <paramref name="fromStringOrFile"/> is a scss file; otherwise it is a scss content.</param>
/// <param name="options">The options.</param>
/// <returns>The result of the conversion</returns>
private static ScssResult FromCore(string fromStringOrFile, bool fromFile, ScssOptions options = null)
private static ScssResult FromCore(string fromStringOrFile, bool fromFile, ScssOptions? options = null)
{
var compiler = new LibSass.Sass_Compiler();
GCHandle? tryImportHandle = null;
Expand All @@ -87,18 +87,15 @@ private static ScssResult FromCore(string fromStringOrFile, bool fromFile, ScssO
{
var fileContext = LibSass.sass_make_file_context(fromStringOrFile);
context = fileContext;
if (options.InputFile == null)
{
options.InputFile = fromStringOrFile;
}
tryImportHandle = MarshalOptions(fileContext, options);
var inputFile = options.InputFile ?? fromStringOrFile;
tryImportHandle = MarshalOptions(fileContext, options, inputFile);
compiler = LibSass.sass_make_file_compiler(fileContext);
}
else
{
var dataContext = LibSass.sass_make_data_context(fromStringOrFile);
context = dataContext;
tryImportHandle = MarshalOptions(dataContext, options);
tryImportHandle = MarshalOptions(dataContext, options, options.InputFile);
compiler = LibSass.sass_make_data_compiler(dataContext);
}

Expand All @@ -112,8 +109,8 @@ private static ScssResult FromCore(string fromStringOrFile, bool fromFile, ScssO
var css = LibSass.sass_context_get_output_string(context);

// Gets the map if it was enabled
string map = null;
if (options != null && options.GenerateSourceMap)
string? map = null;
if (options.GenerateSourceMap)
{
map = LibSass.sass_context_get_source_map_string(context);
}
Expand Down Expand Up @@ -154,20 +151,18 @@ private static ScssResult FromCore(string fromStringOrFile, bool fromFile, ScssO
}
}

private static unsafe List<string> GetIncludedFiles(LibSass.Sass_Context context)
private static unsafe List<string>? GetIncludedFiles(LibSass.Sass_Context context)
{
var filesCount = (int)LibSass.sass_context_get_included_files_size(context);
var files = (LibSass.StringUtf8*)LibSass.sass_context_get_included_files(context);
List<string> list = null;
List<string>? list = null;
for(int i = 0; i < filesCount; i++)
{
if (!files->IsEmpty)
{
if (list == null)
{
list = new List<string>();
}
list.Add(*files);
list ??= new List<string>();
var fileAsString = (string)(*files)!;
list.Add(fileAsString);
}
files++;
}
Expand All @@ -189,7 +184,7 @@ private static void CheckStatus(LibSass.Sass_Context context)
}
}

private static GCHandle? MarshalOptions(LibSass.Sass_Context context, ScssOptions options)
private static GCHandle? MarshalOptions(LibSass.Sass_Context context, ScssOptions options, string? inputFile)
{
var nativeOptions = LibSass.sass_context_get_options(context);

Expand Down Expand Up @@ -227,24 +222,17 @@ private static void CheckStatus(LibSass.Sass_Context context)
{
LibSass.sass_option_set_linefeed(nativeOptions, options.Linefeed);
}
if (options.InputFile != null)
if (inputFile != null)
{
var inputFile = GetRootedPath(options.InputFile);
inputFile = GetRootedPath(inputFile);
LibSass.sass_option_set_input_path(nativeOptions, inputFile);
}
string outputFile = null;
if (options.OutputFile != null)
string? outputFile = options.OutputFile;
if (outputFile != null)
{
outputFile = GetRootedPath(options.OutputFile);
outputFile = GetRootedPath(outputFile);
LibSass.sass_option_set_output_path(nativeOptions, outputFile);
}
//if (options.PluginPaths.Count > 0)
//{
// foreach (var path in options.PluginPaths)
// {
// LibSass.sass_option_push_plugin_path(nativeOptions, path);
// }
//}
if (options.IncludePaths.Count > 0)
{
foreach (var path in options.IncludePaths)
Expand All @@ -256,11 +244,6 @@ private static void CheckStatus(LibSass.Sass_Context context)
if (options.GenerateSourceMap)
{
var sourceMapFile = GetRootedPath(options.SourceMapFile ?? (outputFile ?? "result.css") + ".map");
if (options.SourceMapFile == null)
{
options.SourceMapFile = sourceMapFile;
}

LibSass.sass_option_set_source_map_file(nativeOptions, sourceMapFile);
}
if (options.SourceMapRoot != null)
Expand All @@ -284,19 +267,19 @@ private static unsafe LibSass.Sass_Import_List CustomScssImporter(string current
string previousPath = LibSass.sass_import_get_abs_path(previous);

var cookieHandle = GCHandle.FromIntPtr(cookie);
var tryImport = (ScssOptions.TryImportDelegate)cookieHandle.Target;
var tryImport = (ScssOptions.TryImportDelegate?)cookieHandle.Target;

var file = (string)currentPath;
var importList = LibSass.sass_make_import_list(1);
uint line = 0;
uint column = 0;
string errorMessage = null;
string? errorMessage = null;
if (tryImport != null)
{
try
{
string scss;
string map;
string? map;
if (tryImport(ref file, previousPath, out scss, out map))
{
var entry = LibSass.sass_make_import(currentPath, file, scss, map ?? "");
Expand Down
16 changes: 8 additions & 8 deletions src/SharpScss/ScssOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ScssOptions
/// <param name="scss">The output scss if import was found.</param>
/// <param name="map">The output map if import was found. May be null</param>
/// <returns><c>true</c> if import was found; <c>false</c> otherwise</returns>
public delegate bool TryImportDelegate(ref string file, string parentPath, out string scss, out string map);
public delegate bool TryImportDelegate(ref string file, string parentPath, out string scss, out string? map);

/// <summary>
/// Initializes a new instance of the <see cref="ScssOptions"/> class.
Expand Down Expand Up @@ -84,12 +84,12 @@ public ScssOptions()
/// <summary>
/// Gets or sets the indent string. Default is 2 spaces.
/// </summary>
public string Indent { get; set; }
public string? Indent { get; set; }

/// <summary>
/// Gets or sets the linefeed. Default is LF (\n)
/// </summary>
public string Linefeed { get; set; }
public string? Linefeed { get; set; }

/// <summary>
/// Gets or sets the name of the input file. See remarks for more information.
Expand All @@ -98,23 +98,23 @@ public ScssOptions()
/// This is recommended when <see cref="GenerateSourceMap"/> so that they can properly refer back to their intended files.
/// Note also that this is not used to load the data from a file. Use <see cref="Scss.ConvertFileToCss"/> instead.
/// </remarks>
public string InputFile { get; set; }
public string? InputFile { get; set; }

/// <summary>
/// Gets or sets the location of the output file. This is recommended when <see cref="GenerateSourceMap"/> so that they can properly refer back to their intended files.
/// </summary>
public string OutputFile { get; set; }
public string? OutputFile { get; set; }

/// <summary>
/// Gets or sets the intended location of the source map file. Note that is used when <see cref="GenerateSourceMap"/> is set. By default, if this property is not set,
/// the <see cref="OutputFile"/> + ".map" extension will be used. Default is <c>null</c>
/// </summary>
public string SourceMapFile { get; set; }
public string? SourceMapFile { get; set; }

/// <summary>
/// Gets or sets the value that will be emitted as sourceRoot in the source map information. Default is null.
/// </summary>
public string SourceMapRoot { get; set; }
public string? SourceMapRoot { get; set; }

/// <summary>
/// Gets the include paths that will be used to search for @import directives in scss content.
Expand All @@ -124,5 +124,5 @@ public ScssOptions()
/// <summary>
/// Gets or sets a dynamic delegate used to resolve imports dynamically.
/// </summary>
public TryImportDelegate TryImport { get; set; }
public TryImportDelegate? TryImport { get; set; }
}
6 changes: 3 additions & 3 deletions src/SharpScss/ScssResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct ScssResult
/// <param name="css">The Css.</param>
/// <param name="sourceMap">The source SourceMap.</param>
/// <param name="includedFiles">The included files.</param>
public ScssResult(string css, string sourceMap, List<string> includedFiles)
public ScssResult(string css, string? sourceMap, List<string>? includedFiles)
{
Css = css;
SourceMap = sourceMap;
Expand All @@ -32,10 +32,10 @@ public ScssResult(string css, string sourceMap, List<string> includedFiles)
/// <summary>
/// Gets the source map (may be null if <see cref="ScssOptions.GenerateSourceMap"/> was <c>false</c>.
/// </summary>
public string SourceMap { get; }
public string? SourceMap { get; }

/// <summary>
/// Gets the included files used to generate this result when converting the input scss content.
/// </summary>
public List<string> IncludedFiles { get; }
public List<string>? IncludedFiles { get; }
}
10 changes: 7 additions & 3 deletions src/SharpScss/SharpScss.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<IsPackable>true</IsPackable>

<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>

<!-- Enable AOT analyzers -->
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<IsAotCompatible>true</IsAotCompatible>
<IsAotCompatible Condition="'$(TargetFramework)' != 'netstandard2.0'">true</IsAotCompatible>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -55,5 +55,9 @@

<!--Add support for sourcelink-->
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
<PackageReference Include="PolySharp">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

0 comments on commit 6f7de3c

Please sign in to comment.