Skip to content

Commit

Permalink
fixes and many tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vlada-shubina committed Jun 2, 2021
1 parent adac6f8 commit 3ae29c7
Show file tree
Hide file tree
Showing 34 changed files with 1,229 additions and 271 deletions.
1 change: 0 additions & 1 deletion Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ namespace Microsoft.TemplateEngine
<ItemGroup>
<Compile Include="$(GitInfoFile)" Condition="Exists('$(GitInfoFile)')" Link="%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Include="$(SrcDir)GitInfo.cs" Condition="!Exists('$(GitInfoFile)')" Link="%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Include="$(SrcDir)IsExternalInit.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<Import Project="$(GitInfoProps)" Condition="Exists('$(GitInfoProps)')" />
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Authoring_InvalidJsonElementInLocalizationFile" xml:space="preserve">
<value>"templatestrings.json" file should only contain elements with type "string". Remove elements that are not strings.</value>
<value>Localization file should only contain elements with type 'string'. Remove elements that are not strings.</value>
</data>
<data name="Authoring_InvalidManualInstructionLocalizationIndex" xml:space="preserve">
<value>In templatestrings.json file under the post action with "id" "{1}", there are localized strings for manual instruction(s) with ids "{0}". These manual instructions do not exist in the template.json file and should be removed from templatestrings.json file.</value>
<value>In localization file under the post action with id '{1}', there are localized strings for manual instruction(s) with ids '{0}'. These manual instructions do not exist in the template.json file and should be removed from localization file.</value>
<comment>{0} is a list of single-word identifier strings separated by comma. Such as "myManualInstruction, someUserPickedText, instruction3".
{1} is a single-word identifier string. Such as "myPostAction".</comment>
</data>
<data name="Authoring_InvalidPostActionLocalizationIndex" xml:space="preserve">
<value>Post action(s) with id(s) "{0}" specified in the localization file do not exist in the template.json file. Remove the localized strings from the templatestrings.json file.</value>
<value>Post action(s) with id(s) '{0}' specified in the localization file do not exist in the template.json file. Remove the localized strings from the localization file.</value>
<comment>{0} is a list of single-word identifier strings separated by comma. Such as "myManualInstruction, someUserPickedText, instruction3".</comment>
</data>
<data name="Authoring_InvalidRegex" xml:space="preserve">
Expand All @@ -136,7 +136,7 @@
<value>One or more postActions have a malformed or missing manualInstructions value.</value>
</data>
<data name="Authoring_ManualInstructionIdIsNotUnique" xml:space="preserve">
<value>Id of the manual instruction "{0}" at index {1} is not unique. Only the first manual instruction that uses this id will be localized.</value>
<value>id of the manual instruction '{0}' at index {1} is not unique. Only the first manual instruction that uses this id will be localized.</value>
<comment>{0} is any identifier string that user provided in the file. Such as: "myInstruction", "firstInstruction", "instruction1" etc.
{1} is a whole number representing the zero-based index of the manual instruction. Such as "5", or "0".</comment>
</data>
Expand All @@ -147,12 +147,12 @@
<value>A non-bool DataType was specified for a regexMatch type symbol</value>
</data>
<data name="Authoring_PostActionIdIsNotUnique" xml:space="preserve">
<value>Id of the post action "{0}" at index {1} is not unique. Only the first post action that uses this Id will be localized.</value>
<value>[{0}]: id of the post action '{1}' at index '{2}' is not unique. Only the first post action that uses this id will be localized.</value>
<comment>{0} is any identifier string that user provided in the file. Such as "myPostAction", "firstPostAction", "postAction1" etc.
{1} is a number representing the zero-based index of the post action. Such as "5", or "0".</comment>
</data>
<data name="Authoring_PostActionMustHaveActionId" xml:space="preserve">
<value>Post action at index {0} should have an "actionId" to declare the action to be executed.</value>
<value>[{0}]: Post action at index '{1}' should have an 'actionId' to declare the action to be executed.</value>
<comment>{0} is a whole number representing the zero-based index of the post action. Such as "5" or "0".
Do not localize: actionId</comment>
</data>
Expand Down Expand Up @@ -181,6 +181,9 @@ Details: {1}</value>
<data name="Authoring_TemplateRootOutsideInstallSource" xml:space="preserve">
<value>The template root is outside the specified install source location.</value>
</data>
<data name="LocalizationModelDeserializer_Error_FailedToParse" xml:space="preserve">
<value>Failed to read parse localization file {0}, it will be skipped from further processing.</value>
</data>
<data name="RunnableProjectGenerator_Exception_ConfigShouldBeFile" xml:space="preserve">
<value>Template configuration must be a file, but {0} is not a file.</value>
<comment>{0} is template configuration location.</comment>
Expand All @@ -196,6 +199,10 @@ Details: {1}</value>
<data name="RunnableProjectGenerator_Exception_TemplateVersionNotSupported" xml:space="preserve">
<value>The template is not compatible with generator version: allowed versions: {0}, generator version: {1}.</value>
</data>
<data name="RunnableProjectGenerator_Warning_LocFileSkipped" xml:space="preserve">
<value>Localization file {0} is not compatible with base configuration {1}, and will be skipped.</value>
<comment>{0} and {1} are template configuration locations.</comment>
</data>
<data name="SimpleConfigModel_AuthoringException_MergeConfiguration_FileNotFound" xml:space="preserve">
<value>Failed to load additional configuration file {0}, the file does not exist.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Mount;
using Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Localization;
using Newtonsoft.Json.Linq;

Expand All @@ -21,38 +21,30 @@ internal static class LocalizationModelDeserializer
private const char KeySeparator = '/';

/// <summary>
/// Deserializes the given json data into an <see cref="ILocalizationModel"/>.
/// Deserializes the given localization file into an <see cref="ILocalizationModel"/>.
/// </summary>
/// <param name="data">Json data to be deserialized.</param>
/// <param name="logger"><see cref="ILogger"/> to be used for logging diagnostics messages.</param>
/// <param name="localizationModel">Deserialized model. Null, if the operation failed.</param>
/// <returns>True if deserialization succeeded. False, otherwise.</returns>
public static bool TryDeserialize(JObject data, ILogger logger, out ILocalizationModel? localizationModel)
/// <param name="file">File to be deserialized.</param>
/// <returns>loaded localization model.</returns>
public static ILocalizationModel Deserialize(IFile file)
{
try
{
var parameterLocalizations = new Dictionary<string, ParameterSymbolLocalizationModel>();

List<(string Key, string Value)> localizedStrings = data.Properties()
.Select(p => p.Value.Type == JTokenType.String ? (p.Name, p.Value.ToString()) : throw new Exception(LocalizableStrings.Authoring_InvalidJsonElementInLocalizationFile))
.ToList();

var symbols = LoadSymbolModels(localizedStrings);
var postActions = LoadPostActionModels(localizedStrings);

localizationModel = new LocalizationModel(
name: localizedStrings.FirstOrDefault(s => s.Key == "name").Value,
description: localizedStrings.FirstOrDefault(s => s.Key == "description").Value,
author: localizedStrings.FirstOrDefault(s => s.Key == "author").Value,
symbols,
postActions);
return true;
}
catch
{
localizationModel = null;
return false;
}
_ = file ?? throw new ArgumentNullException(nameof(file));

JObject srcObject = file.ReadJObjectFromIFile();
var parameterLocalizations = new Dictionary<string, ParameterSymbolLocalizationModel>();

List<(string Key, string Value)> localizedStrings = srcObject.Properties()
.Select(p => p.Value.Type == JTokenType.String ? (p.Name, p.Value.ToString()) : throw new Exception(LocalizableStrings.Authoring_InvalidJsonElementInLocalizationFile))
.ToList();

var symbols = LoadSymbolModels(localizedStrings);
var postActions = LoadPostActionModels(localizedStrings);

return new LocalizationModel(
name: localizedStrings.FirstOrDefault(s => s.Key == "name").Value,
description: localizedStrings.FirstOrDefault(s => s.Key == "description").Value,
author: localizedStrings.FirstOrDefault(s => s.Key == "author").Value,
symbols,
postActions);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Localize(IPostActionLocalizationModel locModel, ILogger logger)
}
}

internal static IReadOnlyList<PostActionModel> LoadListFromJArray(JArray jArray, ILogger logger)
internal static IReadOnlyList<PostActionModel> LoadListFromJArray(JArray jArray, ILogger logger, string filename)
{
List<PostActionModel> localizedPostActions = new List<PostActionModel>();
if (jArray == null)
Expand All @@ -77,13 +77,13 @@ internal static IReadOnlyList<PostActionModel> LoadListFromJArray(JArray jArray,
if (postActionId != null && !postActionIds.Add(postActionId))
{
// There is already a post action with the same id. Localization won't work properly. Let user know.
logger.LogWarning(LocalizableStrings.Authoring_PostActionIdIsNotUnique, postActionId, postActionIndex);
logger.LogWarning(LocalizableStrings.Authoring_PostActionIdIsNotUnique, filename, postActionId, postActionIndex);
postActionId = null;
}

if (actionId == default)
{
logger.LogError(LocalizableStrings.Authoring_PostActionMustHaveActionId, postActionIndex);
logger.LogError(LocalizableStrings.Authoring_PostActionMustHaveActionId, filename, postActionIndex);
continue;
}

Expand Down
Loading

0 comments on commit 3ae29c7

Please sign in to comment.