ResManager: fix

This commit is contained in:
pavelbannov 2021-02-09 20:53:49 +03:00
parent 10542fe4b4
commit d05f7b70ec
3 changed files with 97 additions and 15 deletions

View File

@ -106,7 +106,7 @@ namespace ASC.Resource.Manager
}
}
public static void Export(IServiceProvider serviceProvider, string project, string module, string fName, string language, string exportPath, string key = null)
public static bool Export(IServiceProvider serviceProvider, string project, string module, string fName, string language, string exportPath, string key = null)
{
var filter = new ResCurrent
{
@ -123,7 +123,7 @@ namespace ASC.Resource.Manager
if (!words.Any())
{
Console.WriteLine("Error!!! Can't find appropriate project and module. Possibly wrong names!");
return;
return false;
}
foreach (var fileWords in words)
@ -184,6 +184,8 @@ namespace ASC.Resource.Manager
writer.Write(obj);
}
return true;
}
private static string GetCultureFromFileName(string fileName)

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using ASC.Common;
using ASC.Common.Utils;
@ -18,6 +19,12 @@ namespace ASC.Resource.Manager
{
class Program
{
private const string CsProjScheme = "http://schemas.microsoft.com/developer/msbuild/2003";
private static readonly XName ItemGroupXname = XName.Get("ItemGroup", CsProjScheme);
private static readonly XName EmbededXname = XName.Get("EmbeddedResource", CsProjScheme);
private static readonly XName DependentUpon = XName.Get("DependentUpon", CsProjScheme);
private const string IncludeAttribute = "Include";
public static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args).WithParsed(Export);
@ -36,15 +43,15 @@ namespace ASC.Resource.Manager
var cultures = new List<string>();
var projects = new List<ResFile>();
var enabledSettings = new EnabledSettings();
Action<IServiceProvider, string, string, string, string, string, string> export = null;
Func<IServiceProvider, string, string, string, string, string, string, bool> export = null;
try
{
var (project, module, filePath, exportPath, culture, format, key) = options;
project = "WebStudio";
module = "WebStudio";
filePath = "MonitoringResource.resx";
project = "Projects";
module = "Messages";
filePath = "MessageResource.resx";
exportPath = @"C:\Git\portals\";
key = "*";
@ -151,6 +158,10 @@ namespace ASC.Resource.Manager
{
var filePath = Directory.GetFiles(exportPath, $"{fileName}", SearchOption.AllDirectories).FirstOrDefault();
if (string.IsNullOrEmpty(filePath)) return;
var resultFiles = new ConcurrentBag<string>();
var asmbl = "";
var assmlPath = Path.GetDirectoryName(filePath);
var name = Path.GetFileNameWithoutExtension(fileName);
if (key == "*")
@ -167,13 +178,14 @@ namespace ASC.Resource.Manager
File.Delete(designerPath);
var nsp = matches[0].Groups[1].Value;
var asmbl = "";
var assmlPath = Path.GetDirectoryName(filePath);
do
{
asmbl = Directory.GetFiles(assmlPath, "*.csproj").FirstOrDefault();
assmlPath = Path.GetFullPath(Path.Combine(assmlPath, ".."));
if (string.IsNullOrEmpty(asmbl))
{
assmlPath = Path.GetFullPath(Path.Combine(assmlPath, ".."));
}
}
while (string.IsNullOrEmpty(asmbl));
@ -183,9 +195,8 @@ namespace ASC.Resource.Manager
{
return;
}
asmbl = matches[0].Groups[1].Value;
key = CheckExist(fileName, $"{nsp}.{name},{asmbl}", exportPath);
key = CheckExist(fileName, $"{nsp}.{name},{matches[0].Groups[1].Value}", exportPath);
}
exportPath = Path.GetDirectoryName(filePath);
@ -196,8 +207,14 @@ namespace ASC.Resource.Manager
}
ParallelEnumerable.ForAll(cultures.AsParallel(), c => {
export(serviceProvider, projectName, moduleName, fileName, c, exportPath, key);
var any = export(serviceProvider, projectName, moduleName, fileName, c, exportPath, key);
if (any)
{
resultFiles.Add($"{filePath.Replace(".resx", (c == "Neutral" ? $".resx" : $".{c}.resx"))}".Substring(assmlPath.Length + 1));
}
});
AddResourceForCsproj(asmbl, filePath.Substring(assmlPath.Length + 1), resultFiles.OrderBy(r=> r));
}
}
}
@ -242,6 +259,67 @@ namespace ASC.Resource.Manager
return string.Join(',', bag.ToArray().Distinct());
}
private static void AddResourceForCsproj(string csproj, string fileName, IEnumerable<string> files)
{
if (!files.Any()) return;
var doc = XDocument.Parse(File.ReadAllText(csproj));
if (doc.Root == null) return;
foreach (var file in files)
{
var node = doc.Root.Elements().FirstOrDefault(r =>
r.Name == ItemGroupXname &&
r.Elements(EmbededXname).Any(x=>
{
var attr = x.Attribute(IncludeAttribute);
return attr != null && attr.Value == fileName;
})) ??
doc.Root.Elements().FirstOrDefault(r =>
r.Name == ItemGroupXname &&
r.Elements(EmbededXname).Any());
XElement reference;
bool referenceNotExist;
if (node == null)
{
node = new XElement(ItemGroupXname);
doc.Root.Add(node);
reference = new XElement(EmbededXname);
referenceNotExist = true;
}
else
{
var embeded = node.Elements(EmbededXname).ToList();
reference = embeded.FirstOrDefault(r =>
{
var attr = r.Attribute(IncludeAttribute);
return attr != null && attr.Value == file;
});
referenceNotExist = reference == null;
if (referenceNotExist)
{
reference = new XElement(EmbededXname);
if (file != fileName)
{
reference.Add(new XElement(DependentUpon, Path.GetFileName(fileName)));
}
}
}
if (referenceNotExist)
{
reference.SetAttributeValue(IncludeAttribute, file);
node.Add(reference);
}
}
doc.Save(csproj);
}
}
[Scope]

View File

@ -37,7 +37,7 @@ namespace ASC.Resource.Manager
{
public class ResxManager
{
public static void Export(IServiceProvider serviceProvider, string project, string module, string fName, string language, string exportPath, string key = null)
public static bool Export(IServiceProvider serviceProvider, string project, string module, string fName, string language, string exportPath, string key = null)
{
var filter = new ResCurrent
{
@ -54,7 +54,7 @@ namespace ASC.Resource.Manager
if (!words.Any())
{
Console.WriteLine("Error!!! Can't find appropriate project and module. Possibly wrong names!");
return;
return false;
}
foreach (var fileWords in words)
@ -121,6 +121,8 @@ namespace ASC.Resource.Manager
resXResourceWriter.Generate();
resXResourceWriter.Close();
}
return true;
}
}
}