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 PreferDefaultName property #5744

Merged
merged 21 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion docs/Reference-for-template.json.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ See [the article](Binding-and-project-context-evaluation.md#bind-symbols).
|Name|Description|Default|Mandatory|
|---|---|---|---|
|`sourceName`| The text in the source content to replace with the name the user specifies. The value to be replaced with can be given using the `-n` `--name` options while running a template. The template engine will look for any occurrence of the `sourceName` present in the config file and replace it in file names and file contents. If no name is specified by the host, the current directory is used. The value of the `sourceName` is available in built-in `name` symbol and can be used as the source for creating other symbols and condition expressions. Due to implicit forms applied to `sourceName` it is important to select the replacement that doesn't cause the conflicts. See more details about `sourceName` in [the article](Naming-and-default-value-forms.md)||no|
|`preferDefaultName`| Boolean value that decides which name will be used if no `--name` is specified during creation. If `false` it will use the fallback name (output folder name), if `true` it will use the template's `defaultName`. If no `defaultName` is specified and `preferDefaultName` is set to `true`, the template creation will not succeed. | If not specified, `false` is used. | no
|`preferDefaultName`| Boolean value that decides which name will be used if no `--name` is specified during creation. If `false` it will use the fallback name (output folder name), if `true` it will use the template's `defaultName`. If no `defaultName` and no fallback name is specified and `preferDefaultName` is set to `true`, the template creation will not succeed with a `MissingMandatoryParam` exception. | If not specified, `false` is used. | no
|`preferNameDirectory`| Boolean value, indicates whether to create a directory for the template if name is specified but an output directory is not set (instead of creating the content directly in the current directory).|If not specified, `false` is used.|no|
|`placeholderFilename`|A filename that will be completely ignored, used to indicate that its containing directory should be copied. This allows creation of empty directory in the created template, by having a corresponding source directory containing just the placeholder file. By default, empty directories are ignored.|If not specified, a default value of `"-.-"` is used.|no|
|`primaryOutputs`|A list of template files for further processing by the host (including post-actions). The path should contain the relative path to the file prior to the symbol based renaming that may happen during template generation. It is defined as an array of [Primary Outputs](#primary-output-definition)||no|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public TemplateSearchData(ITemplateInfo templateInfo, IDictionary<string, object
/// <inheritdoc/>
public IReadOnlyList<string> ShortNameList => TemplateInfo.ShortNameList;

public bool PreferDefaultName => TemplateInfo.PreferDefaultName;
/// <inheritdoc/>
bool ITemplateInfo.PreferDefaultName => TemplateInfo.PreferDefaultName;

/// <inheritdoc/>
public string? Author => TemplateInfo.Author;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Microsoft.TemplateSearch.Common.TemplateSearchData.PreferDefaultName.get -> bool

Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ public void CanHandleParameters()
Assert.Equal(2, readTemplate.ParameterDefinitions["param3"].Choices!.Count);
}

[Theory]
[InlineData(true, "defaultName")]
[InlineData(true, null)]
[InlineData(false, "anotherDefault")]
public void CanHandleDefaultName(bool preferDefaultName, string? defaultName)
{
IEngineEnvironmentSettings environmentSettings = _environmentSettingsHelper.CreateEnvironment(virtualize: true);
SettingsFilePaths paths = new SettingsFilePaths(environmentSettings);

ITemplate template = A.Fake<ITemplate>();
A.CallTo(() => template.Identity).Returns("testIdentity");
A.CallTo(() => template.Name).Returns("testName");
A.CallTo(() => template.ShortNameList).Returns(new[] { "testShort" });
A.CallTo(() => template.PreferDefaultName).Returns(preferDefaultName);
A.CallTo(() => template.DefaultName).Returns(defaultName);
A.CallTo(() => template.MountPointUri).Returns("testMount");
A.CallTo(() => template.ConfigPlace).Returns(".template.config/template.json");
IMountPoint mountPoint = A.Fake<IMountPoint>();
A.CallTo(() => mountPoint.MountPointUri).Returns("testMount");

ScanResult result = new ScanResult(mountPoint, new[] { template }, Array.Empty<ILocalizationLocator>(), Array.Empty<(string AssemblyPath, Type InterfaceType, IIdentifiedComponent Instance)>());
TemplateCache templateCache = new TemplateCache(new[] { result }, new Dictionary<string, DateTime>(), NullLogger.Instance);

WriteObject(environmentSettings.Host.FileSystem, paths.TemplateCacheFile, templateCache);
var readCache = new TemplateCache(ReadObject(environmentSettings.Host.FileSystem, paths.TemplateCacheFile), NullLogger.Instance);

Assert.Single(readCache.TemplateInfo);
var readTemplate = readCache.TemplateInfo[0];
if (preferDefaultName)
{
Assert.True(readTemplate.PreferDefaultName);

}
else
{
Assert.False(readTemplate.PreferDefaultName);
}
maridematte marked this conversation as resolved.
Show resolved Hide resolved
Assert.Equal(defaultName, readTemplate.DefaultName);
}

private static JObject ReadObject(IPhysicalFileSystem fileSystem, string path)
{
using (var fileStream = fileSystem.OpenRead(path))
Expand Down