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

(#365) skip multiple Cake tasks of the same name #367

Merged
merged 2 commits into from
Sep 27, 2023
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
86 changes: 55 additions & 31 deletions src/dotnet/cake-rider/CakeFrostingProjectsHost.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -70,10 +71,9 @@ public CakeFrostingProjectsHost(

public void Refresh(IProjectMark projectMark)
{
_logger.Trace($"Handle change for project: {projectMark.Location}");
using (_solution.Locks.UsingReadLock())
{
_logger.Trace("Handle change for project: {0}", projectMark.Location);

// Do not dispose IProject!
#pragma warning disable IDISP001
var projectByMark = _solution.GetProjectByMark(projectMark);
Expand Down Expand Up @@ -124,23 +124,25 @@ public void ProcessTasks(ICSharpFile file, IDocument document)
foreach (var classDeclaration in classDeclarations)
{
var declaredElement = classDeclaration.DeclaredElement;
if (declaredElement != null)
if (declaredElement == null)
{
if (!declaredElement.IsCakeFrostingTask())
{
continue;
}
continue;
}

var name = declaredElement.GetCakeFrostingTaskName();
if (!declaredElement.IsCakeFrostingTask())
{
continue;
}

if (!cakeFrostingProject.Tasks.Contains(name))
{
toAdd.Add(name);
}
var name = declaredElement.GetCakeFrostingTaskName();

currentFileTasks.Add(name);
newTasks.Add(name);
if (!cakeFrostingProject.Tasks.Contains(name))
{
toAdd.Add(name);
}

currentFileTasks.Add(name);
newTasks.Add(name);
}

foreach (var currentFileTask in currentFileTasks)
Expand Down Expand Up @@ -174,6 +176,7 @@ private void ProjectRemoved(IProjectMark projectMark)
{
if (_cakeFrostingProjects.TryGetValue(projectMark, out var cakeFrostingProject) && _isSolutionLoaded)
{
_logger.Trace($"removing project {projectMark.Name} from loaded tasks");
_model.Projects.Remove(cakeFrostingProject);
_cakeFrostingProjects.Remove(projectMark);
_cakeFrostingTasks.Remove(projectMark);
Expand All @@ -191,45 +194,66 @@ private void ProjectAdded(IProject project)
return;
}

_logger.Trace($"Project {projectMark.Name} was added.");
var cakeFrostingProject = new CakeFrostingProject();
cakeFrostingProject.Name.Set(project.Name);
cakeFrostingProject.ProjectFilePath.Set(
project.ProjectFileLocation.NormalizeSeparators(FileSystemPathEx.SeparatorStyle.Unix));
_model.Projects.Add(cakeFrostingProject);
_cakeFrostingProjects.Add(projectMark, cakeFrostingProject);
_cakeFrostingTasks.Add(projectMark, new Dictionary<IPsiSourceFile, HashSet<string>>());
LoadTasksForProject(projectMark, cakeFrostingProject);
}

private void LoadAllTasks()
private void LoadTasksForProject(IProjectMark projectMark, CakeFrostingProject cakeFrostingProject)
{
foreach ((IProjectMark projectMark, CakeFrostingProject cakeFrostingProject) in _cakeFrostingProjects)
_logger.Trace($"loading all tasks of project:{cakeFrostingProject.Name}");
foreach (var psiModule in _solution.GetProjectByMark(projectMark)!.GetPsiModules())
{
var fileTaskMap = _cakeFrostingTasks[projectMark];

foreach (var psiModule in _solution.GetProjectByMark(projectMark)!.GetPsiModules())
_logger.Trace($"walking psiModule:{psiModule.Name}");
using (CompilationContextCookie.OverrideOrCreate(psiModule.GetContextFromModule()))
{
using (CompilationContextCookie.OverrideOrCreate(psiModule.GetContextFromModule()))
foreach (var sourceFile in psiModule.SourceFiles)
{
foreach (var sourceFile in psiModule.SourceFiles)
_logger.Trace($"walking file:{sourceFile.Name}");
foreach (var klass in _symbolCache.GetTypesAndNamespacesInFile(sourceFile).OfType<IClass>())
{
var tasks = fileTaskMap[sourceFile] = new HashSet<string>();
_logger.Trace($"walking class:{klass.ShortName}");
Interruption.Current.CheckAndThrow();

if (!klass.IsCakeFrostingTask())
{
_logger.Trace($"{klass.ShortName} is not a Cake.Frosting task");
continue;
}

foreach (var klass in _symbolCache.GetTypesAndNamespacesInFile(sourceFile).OfType<IClass>())
var name = klass.GetCakeFrostingTaskName();
if (cakeFrostingProject.Tasks.Any(x =>
x.Equals(name, StringComparison.OrdinalIgnoreCase)))
{
Interruption.Current.CheckAndThrow();

if (klass.IsCakeFrostingTask())
{
var name = klass.GetCakeFrostingTaskName();
cakeFrostingProject.Tasks.Add(name);
tasks.Add(name);
}
// if the task was already registered, it's either an error,
// or the task was defined twice in partials (in Cake Frosting)
// either way, we can ignore it.
_logger.Trace($"Task '{name}' already added. skipping it here.");
continue;
}

_logger.Trace($"Adding Task '{name}'.");
cakeFrostingProject.Tasks.Add(name);
}
}
}
}
}

private void LoadAllTasks()
{
_logger.Trace("Intially loading all Cake.Frosting tasks");
foreach ((IProjectMark projectMark, CakeFrostingProject cakeFrostingProject) in _cakeFrostingProjects)
{
LoadTasksForProject(projectMark, cakeFrostingProject);
}
}
}

public interface IDetectFrostingModules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,17 @@ class CakeTasksWindow(private val project: Project) : SimpleToolWindowPanel(true
val cakeProject = CakeScriptProject(project)
for (cakeFile in cakeProject.getCakeFiles()) {
val fileNode = DefaultMutableTreeNode(cakeFile)
val children = mutableListOf<String>()
rootNode.add(fileNode)

for (task in cakeFile.getTasks()) {
if (children.contains(task.name)) {
// this is an error and won't compile.
// we simply skip it.
continue
}

children.add(task.name)
val taskNode = DefaultMutableTreeNode(task)
fileNode.add(taskNode)
}
Expand Down