Skip to content
Open
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
15 changes: 11 additions & 4 deletions src/Compiler/CompilerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
using System.Text;
using Tasks = System.Threading.Tasks;
using EnvDTE80;
using Microsoft.VisualStudio.Threading;

namespace LessCompiler
{
internal static class CompilerService
{
public static async Tasks.Task CompileProjectAsync(Project project)
{
var tf = new JoinableTaskFactory(Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskContext);
await tf.SwitchToMainThreadAsync();
if (project == null || !LessCatalog.Catalog.TryGetValue(project.UniqueName, out ProjectMap map))
return;

Expand All @@ -24,7 +27,7 @@ public static async Tasks.Task CompileProjectAsync(Project project)
foreach (CompilerOptions option in map.LessFiles.Keys)
{
if (option.Compile)
compileTasks.Add(CompileSingleFile(option));
compileTasks.Add(CompileSingleFileAsync(option));
}

await Tasks.Task.WhenAll(compileTasks);
Expand All @@ -34,6 +37,8 @@ public static async Tasks.Task CompileProjectAsync(Project project)

public static async Tasks.Task CompileAsync(CompilerOptions options, Project project)
{
var tf = new JoinableTaskFactory(Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskContext);
await tf.SwitchToMainThreadAsync();
if (options == null || project == null || !LessCatalog.Catalog.TryGetValue(project.UniqueName, out ProjectMap map))
return;

Expand All @@ -50,7 +55,7 @@ public static async Tasks.Task CompileAsync(CompilerOptions options, Project pro

foreach (CompilerOptions parentOptions in parents.Where(p => p.Compile))
{
compilerTaks.Add(CompileSingleFile(parentOptions));
compilerTaks.Add(CompileSingleFileAsync(parentOptions));
}

await Tasks.Task.WhenAll(compilerTaks);
Expand All @@ -60,7 +65,7 @@ public static async Tasks.Task CompileAsync(CompilerOptions options, Project pro
VsHelpers.WriteStatus($"LESS file compiled in {Math.Round(sw.Elapsed.TotalSeconds, 2)} seconds");
}

private static async Tasks.Task<bool> CompileSingleFile(CompilerOptions options)
private static async Tasks.Task<bool> CompileSingleFileAsync(CompilerOptions options)
{
try
{
Expand Down Expand Up @@ -104,6 +109,8 @@ public static bool SupportsCompilation(this Project project)

private static void AddFilesToProject(CompilerOptions options)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

ProjectItem item = VsHelpers.DTE.Solution.FindProjectItem(options.InputFilePath);

if (item?.ContainingProject != null)
Expand Down Expand Up @@ -139,7 +146,7 @@ public static void Minify(CompilerOptions options)
CommentMode = CssComment.Important
};

UgliflyResult result = Uglify.Css(cssContent, settings);
NUglify.UglifyResult result = Uglify.Css(cssContent, settings);

if (result.HasErrors)
return;
Expand Down
29 changes: 24 additions & 5 deletions src/Compiler/ProjectMap.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Threading;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -36,14 +37,20 @@ public async Task BuildMap(Project project)
var sw = new Stopwatch();
sw.Start();

var tf = new JoinableTaskFactory(Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskContext);
await tf.SwitchToMainThreadAsync();
IEnumerable<string> lessFiles = FindLessFiles(project.ProjectItems);

var tasklist = new List<Task>();
foreach (string file in lessFiles)
{
await AddFile(file);
tasklist.Add(AddFile(file));
}

await Task.WhenAll(tasklist);

sw.Stop();
await tf.SwitchToMainThreadAsync();
Logger.Log($"LESS file catalog for {project.Name} built in {Math.Round(sw.Elapsed.TotalSeconds, 2)} seconds");
}

Expand Down Expand Up @@ -78,7 +85,7 @@ private async Task AddFile(string lessFilePath)
if (LessFiles.Keys.Any(c => c.InputFilePath == lessFilePath))
return;

string lessContent = File.ReadAllText(lessFilePath);
string lessContent = await VsHelpers.ReadFileAsync(lessFilePath);

CompilerOptions options = await CompilerOptions.Parse(lessFilePath, lessContent);
LessFiles.Add(options, new List<CompilerOptions>());
Expand All @@ -93,10 +100,19 @@ private async Task AddOption(CompilerOptions options, string lessContent = null)

foreach (Match match in _import.Matches(lessContent))
{
string childFilePath = new FileInfo(Path.Combine(lessDir, match.Groups["url"].Value)).FullName;

string childFileName =Path.Combine(lessDir, match.Groups["url"].Value);
if (!File.Exists(childFileName))
{
Logger.Log($"{childFileName} is inaccessible");
continue;
}

string childFilePath = new FileInfo(childFileName).FullName;
if (!File.Exists(childFilePath))
{
Logger.Log($"{childFilePath} is inaccessible");
continue;
}

CompilerOptions import = LessFiles.Keys.FirstOrDefault(c => c.InputFilePath == childFilePath);

Expand All @@ -114,6 +130,7 @@ private async Task AddOption(CompilerOptions options, string lessContent = null)

private static IEnumerable<string> FindLessFiles(ProjectItems items, List<string> files = null)
{
Dispatcher.CurrentDispatcher.VerifyAccess();
if (files == null)
files = new List<string>();

Expand Down Expand Up @@ -143,11 +160,13 @@ private void OnProjectItemRenamed(ProjectItem item, string OldName)

ThreadHelper.Generic.BeginInvoke(DispatcherPriority.ApplicationIdle, () =>
{
Dispatcher.CurrentDispatcher.VerifyAccess();
LessFiles.Clear();

Project itemContainingProject = item.ContainingProject;
Task.Run(async () =>
{
await BuildMap(item.ContainingProject);
await BuildMap(itemContainingProject);
});
});
}
Expand Down
Loading