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

Add licenses.licx special file provider #5468

Merged
merged 1 commit into from
Sep 12, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<FileExtension Name=".asp" ContentType="AspPage" />
<FileExtension Name=".txt" ContentType="Text" />
<FileExtension Name=".resx" ContentType="EmbeddedResource" />
<FileExtension Name=".licx" ContentType="EmbeddedResource" />
<FileExtension Name=".html" ContentType="HTML" />
<FileExtension Name=".htm" ContentType="HTML" />
<FileExtension Name=".css" ContentType="CSS" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Threading.Tasks;

namespace Microsoft.VisualStudio.ProjectSystem.SpecialFileProviders
{
/// <summary>
/// Provides <see langword="abstract"/> base class for <see cref="ISpecialFileProvider"/> instances
/// that find their special file by file name in the root of the project.
/// </summary>
internal abstract class AbstractFindByNameSpecialFileProvider2 : AbstractSpecialFileProvider
{
private readonly string _fileName;

protected AbstractFindByNameSpecialFileProvider2(string fileName, IPhysicalProjectTree projectTree)
: base(projectTree)
{
_fileName = fileName;
}

protected override Task<IProjectTree?> FindFileAsync(IProjectTreeProvider provider, IProjectTree root)
{
root.TryFindImmediateChild(_fileName, out IProjectTree? node);

return Task.FromResult<IProjectTree?>(node);
}

protected override Task<string?> GetDefaultFileAsync(IProjectTreeProvider provider, IProjectTree root)
{
string? projectPath = provider.GetRootedAddNewItemDirectory(root);
if (projectPath == null) // Root has DisableAddItem
return Task.FromResult<string?>(null);

string path = Path.Combine(projectPath, _fileName);

return Task.FromResult<string?>(path);
}

protected string? GetDefaultFileAsync(string rootPath)
{
return Path.Combine(rootPath, _fileName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;

namespace Microsoft.VisualStudio.ProjectSystem.SpecialFileProviders
{
/// <summary>
/// Provides <see langword="abstract"/> base class for <see cref="ISpecialFileProvider"/> instances
/// that find their special file by file name under the AppDesigner folder, falling back
/// to the root folder if it doesn't exist.
/// </summary>
internal class AbstractFindByNameUnderAppDesignerSpecialFileProvider : AbstractFindByNameSpecialFileProvider2
{
private readonly ISpecialFilesManager _specialFilesManager;

protected AbstractFindByNameUnderAppDesignerSpecialFileProvider(string fileName, ISpecialFilesManager specialFilesManager, IPhysicalProjectTree projectTree)
: base(fileName, projectTree)
{
_specialFilesManager = specialFilesManager;
}

protected override async Task<IProjectTree?> FindFileAsync(IProjectTreeProvider provider, IProjectTree root)
{
// Search AppDesigner folder first if it exists
IProjectTree? appDesignerFolder = await GetAppDesignerFolderAsync(provider, root);
if (appDesignerFolder != null)
{
IProjectTree? node = await base.FindFileAsync(provider, appDesignerFolder);
if (node != null)
return node;
}

// Then fallback to project root
return await base.FindFileAsync(provider, root);
}

protected override async Task<string?> GetDefaultFileAsync(IProjectTreeProvider provider, IProjectTree root)
{
// AppDesigner folder first if it exists
string? appDesignerPath = await GetAppDesignerFolderPathAsync();
if (appDesignerPath != null)
{
return GetDefaultFileAsync(appDesignerPath);
}

// Then fallback to project root
return await base.GetDefaultFileAsync(provider, root);
}

private async Task<IProjectTree?> GetAppDesignerFolderAsync(IProjectTreeProvider provider, IProjectTree root)
{
string? appDesignerPath = await GetAppDesignerFolderPathAsync();
if (appDesignerPath == null)
return null;

return provider.FindByPath(root, appDesignerPath);
}

private Task<string?> GetAppDesignerFolderPathAsync(bool createIfNotExists = false)
{
SpecialFileFlags flags = SpecialFileFlags.FullPath;
if (createIfNotExists)
flags |= SpecialFileFlags.CreateIfNotExist;

return _specialFilesManager.GetFileAsync(SpecialFiles.AppDesigner, flags);
}

protected override async Task CreateFileAsync(string path)
{
await EnsureAppDesignerFolder();

await base.CreateFileAsync(path);
}

private Task EnsureAppDesignerFolder()
{
return GetAppDesignerFolderPathAsync(createIfNotExists: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.ComponentModel.Composition;

namespace Microsoft.VisualStudio.ProjectSystem.SpecialFileProviders
{
/// <summary>
/// Provides a <see cref="ISpecialFileProvider"/> that handles the 'licenses.licx' file;
/// a file that contains a list of licensed (typically Windows Forms) components used by
/// a project.
/// </summary>
[ExportSpecialFileProvider(SpecialFiles.Licenses)]
[AppliesTo(ProjectCapability.DotNet)]
internal class LicensesSpecialFileProvider : AbstractFindByNameUnderAppDesignerSpecialFileProvider
{
[ImportingConstructor]
public LicensesSpecialFileProvider(ISpecialFilesManager specialFilesManager, IPhysicalProjectTree projectTree)
: base("licenses.licx", specialFilesManager, projectTree)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ public static IPhysicalProjectTreeStorage Create()
return Mock.Of<IPhysicalProjectTreeStorage>();
}

public static IPhysicalProjectTreeStorage ImplementCreateEmptyFileAsync(Action<string> action)
{
var mock = new Mock<IPhysicalProjectTreeStorage>();
mock.Setup(p => p.CreateEmptyFileAsync(It.IsAny<string>()))
.ReturnsAsync(action);

return mock.Object;
}

public static IPhysicalProjectTreeStorage ImplementAddFileAsync(Action<string> action)
{
var mock = new Mock<IPhysicalProjectTreeStorage>();
mock.Setup(p => p.AddFileAsync(It.IsAny<string>()))
.ReturnsAsync(action);

return mock.Object;
}

public static IPhysicalProjectTreeStorage ImplementAddFolderAsync(Action<string> action)
{
var mock = new Mock<IPhysicalProjectTreeStorage>();
Expand Down
Loading