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 20 commits
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
1 change: 1 addition & 0 deletions docs/Reference-for-template.json.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +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` and no fallback name is specified and `preferDefaultName` is set to `true`, the template creation will not succeed with a `TemplateIssueDetected` 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
5 changes: 5 additions & 0 deletions src/Microsoft.TemplateEngine.Abstractions/ITemplateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,10 @@ public interface ITemplateInfo
IReadOnlyList<Guid> PostActions { get; }

IReadOnlyList<TemplateConstraintInfo> Constraints { get; }

/// <summary>
/// Gets template's preference for using the default name on creation.
/// </summary>
bool PreferDefaultName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Microsoft.TemplateEngine.Abstractions.IVariableCollection
Microsoft.TemplateEngine.Abstractions.IVariableCollection.Parent.get -> Microsoft.TemplateEngine.Abstractions.IVariableCollection?
Microsoft.TemplateEngine.Abstractions.IVariableCollection.Parent.set -> void
Microsoft.TemplateEngine.Abstractions.ITemplateInfo.PreferDefaultName.get -> bool
Microsoft.TemplateEngine.Abstractions.Parameters.ParameterSetDataExtensions
static Microsoft.TemplateEngine.Abstractions.Parameters.ParameterSetData.Empty.get -> Microsoft.TemplateEngine.Abstractions.Parameters.IParameterSetData!
static Microsoft.TemplateEngine.Abstractions.Parameters.ParameterSetDataExtensions.GetValue(this Microsoft.TemplateEngine.Abstractions.Parameters.IParameterSetData! data, string! parameterName) -> Microsoft.TemplateEngine.Abstractions.Parameters.ParameterData!
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.TemplateEngine.Edge/FilterableTemplateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ private FilterableTemplateInfo(ITemplateInfo source)

public IReadOnlyList<string> GroupShortNameList { get; set; }

public bool PreferDefaultName { get; private set; }

public IReadOnlyDictionary<string, ICacheTag> Tags { get; private set; }

public IReadOnlyDictionary<string, ICacheParameter> CacheParameters { get; private set; }
Expand Down Expand Up @@ -92,6 +94,7 @@ public static FilterableTemplateInfo FromITemplateInfo(ITemplateInfo source)
Precedence = source.Precedence,
Name = source.Name,
ShortName = source.ShortName,
PreferDefaultName = source.PreferDefaultName,
Tags = source.Tags,
CacheParameters = source.CacheParameters,
ParameterDefinitions = source.ParameterDefinitions,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Microsoft.TemplateEngine.Edge/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ Details: {1}.</value>
<value>Failed to create template.
Details: {0}</value>
</data>
<data name="TemplateCreator_TemplateCreationResult_Error_NoDefaultName" xml:space="preserve">
<value>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</value>
maridematte marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="TemplateCreator_TemplateCreationResult_Error_DestructiveChanges" xml:space="preserve">
<value>Destructive changes detected.</value>
</data>
Expand Down Expand Up @@ -392,4 +395,4 @@ Template cache will be recreated on the next run.</value>
<value>'IWorkloadsInfoProvider' component provided by host provided some duplicated workloads (duplicates: {0}). Duplicates will be skipped.</value>
<comment>{0} is the list of duplicates</comment>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Microsoft.TemplateEngine.Edge.Settings.ITemplateInfoHostJsonCache.HostData.get -
Microsoft.TemplateEngine.Edge.VirtualEnvironment
Microsoft.TemplateEngine.Edge.VirtualEnvironment.VirtualEnvironment(System.Collections.Generic.IReadOnlyDictionary<string!, string!>? virtualEnvironemnt, bool includeRealEnvironment) -> void
static Microsoft.TemplateEngine.Edge.DefaultEnvironment.FetchEnvironmentVariables() -> System.Collections.Generic.IReadOnlyDictionary<string!, string!>!
Microsoft.TemplateEngine.Edge.FilterableTemplateInfo.PreferDefaultName.get -> bool
4 changes: 4 additions & 0 deletions src/Microsoft.TemplateEngine.Edge/Settings/TemplateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ internal TemplateInfo(ITemplate template, ILocalizationLocator? localizationInfo
Precedence = template.Precedence;
Identity = template.Identity;
DefaultName = template.DefaultName;
PreferDefaultName = template.PreferDefaultName;
HostConfigPlace = template.HostConfigPlace;
ThirdPartyNotices = template.ThirdPartyNotices;
BaselineInfo = template.BaselineInfo;
Expand Down Expand Up @@ -188,6 +189,9 @@ string ITemplateInfo.ShortName

public IReadOnlyList<string> ShortNameList { get; } = new List<string>();

[JsonProperty]
public bool PreferDefaultName { get; private set; }

[JsonIgnore]
[Obsolete]
IReadOnlyDictionary<string, ICacheTag> ITemplateInfo.Tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal static TemplateInfo FromJObject(JObject entry)
}

info.DefaultName = entry.ToString(nameof(DefaultName));
info.PreferDefaultName = entry.ToBool(nameof(PreferDefaultName));
maridematte marked this conversation as resolved.
Show resolved Hide resolved
info.Description = entry.ToString(nameof(Description));
info.GeneratorId = Guid.Parse(entry.ToString(nameof(GeneratorId)));
info.GroupIdentity = entry.ToString(nameof(GroupIdentity));
Expand Down
30 changes: 29 additions & 1 deletion src/Microsoft.TemplateEngine.Edge/Template/TemplateCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,35 @@ public async Task<ITemplateCreationResult> InstantiateAsync(
return new TemplateCreationResult(CreationResultStatus.NotFound, templateInfo.Name, LocalizableStrings.TemplateCreator_TemplateCreationResult_Error_CouldNotLoadTemplate);
}

string? realName = name ?? fallbackName ?? template.DefaultName;
string? realName = null;
if (!string.IsNullOrEmpty(name))
{
realName = name;
}
else if (templateInfo.PreferDefaultName)
{
if (string.IsNullOrEmpty(templateInfo.DefaultName))
{
return new TemplateCreationResult(
CreationResultStatus.TemplateIssueDetected, template.Name, LocalizableStrings.TemplateCreator_TemplateCreationResult_Error_NoDefaultName);
}
realName = templateInfo.DefaultName;
}
else
{
if (string.IsNullOrEmpty(fallbackName))
{
if (!string.IsNullOrEmpty(templateInfo.DefaultName))
{
realName = templateInfo.DefaultName;
}
}
else
{
realName = fallbackName;
}
}

if (string.IsNullOrWhiteSpace(realName))
{
return new TemplateCreationResult(CreationResultStatus.MissingMandatoryParam, template.Name, "--name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Podrobnosti: {0}.</target>
<target state="translated">Rozpoznaly se destruktivní změny.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">Nepovedlo se načíst data hostitele v {0} v {1}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Details: {0}.</target>
<target state="translated">Es wurden destruktive Änderungen erkannt.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">Fehler beim Laden von Host Daten in {0} bei {1}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Detalles: {0}.</target>
<target state="translated">Cambios destructivos detectados.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">No se pudieron cargar los datos de host en {0} en {1}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Détails : {0}.</target>
<target state="translated">Modifications destructrices détectées.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">Échec du chargement des données de l’hôte dans la {0} à {1}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Dettagli: {0}.</target>
<target state="translated">Rilevate modifiche distruttive.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">Non è stato possibile caricare i dati dell'host in {0} in {1}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Details: {0}</source>
<target state="translated">重大な変更が検出されました。</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">{1} の {0} でホスト データを読み込めませんでした。</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Details: {0}</source>
<target state="translated">파괴적 변경이 감지되었습니다.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">{0}에서 {1}의 호스트 데이터를 로드하지 못했습니다.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Szczegóły: {0}.</target>
<target state="translated">Wykryto niszczące zmiany.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">Nie udało się załadować danych hosta w klasie {0} z {1}.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ Detalhes: {0}</target>
<target state="translated">Alterações destrutivas detectadas.</target>
<note />
</trans-unit>
<trans-unit id="TemplateCreator_TemplateCreationResult_Error_NoDefaultName">
<source>Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</source>
<target state="new">Failed to create template: the Template name is not specified. Template configuration does not configure a default name that can be used when name is not specified. Specify the name for the template when instantiating or configure a default name in the template configuration.</target>
<note />
</trans-unit>
<trans-unit id="TemplateInfo_Warning_FailedToReadHostData">
<source>Failed to load host data in {0} at {1}.</source>
<target state="translated">Falha ao carregar os dados do host em {0} às {1}.</target>
Expand Down
Loading