コード例 #1
0
 public void TestTagLiteral()
 {
     foreach (var parameters in new string[] {
         "\"Hello\"",
         "\"Hello world\", Double",
         "UInt test, \"World is small\"",
         "Plural, \"Test\"",
         "String test, Plural, String test2, \"Hello\"",
         "Variant v, \"AAAA\"",
         "String test, \"BBB\", Variant v",
         "String test, \"Hello\", \"Hello\", Plural Int",
         "\"Hello\", \"Hello\", Plural Int test",
         "\"Hello ] World\"",    //literal with ']'
         "\"Hello \\\" World\"", //literal with '"'
         "\"Hello , World\"",    //literal with ','
         "\"Hello ( World\"",    //literal with ')'
         "\"Hello ) World\"",    //literal with '('
         "\"Hello () World\"",   //literal with '()'
     })
     {
         var res = ReswClassGenerator.ParseTag($"#Format[{parameters}]");
         Assert.False(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
         Assert.Equal(res.format, parameters);
         res = ReswClassGenerator.ParseTag($"#FormatNet[{parameters}]");
         Assert.True(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
     }
 }
コード例 #2
0
        public void TestTagParsing()
        {
            foreach (var type in FormatTag.AcceptedTypes)
            {
                var res = ReswClassGenerator.ParseTag($"#Format[{type.Key}]");
                Assert.True(res.format == type.Key);

                //Text before the tag
                var parameters = $"Int, {type.Key}, String test";
                res = ReswClassGenerator.ParseTag($"This is a comment #Format[{parameters}]");
                Assert.True(res.format == parameters);

                //Text after the tag
                res = ReswClassGenerator.ParseTag($"#Format[{parameters}] This is a text");
                Assert.True(res.format == parameters);

                //Text before and after the tag
                res = ReswClassGenerator.ParseTag($"Test #FormatNet[{parameters}] This is a text");
                Assert.True(res.format == parameters);

                //Remove spaces
                res = ReswClassGenerator.ParseTag($"Hello world #FormatNet[  {parameters}  ] This is a text!");
                Assert.True(res.format == parameters);
            }
        }
コード例 #3
0
        public int Generate(ProjectItem projectItem, string inputFileContents,
                            string defaultNamespace, out byte[] output, IVsGeneratorProgress generateProgress)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            VSUIIntegration.Instance?.ClearErrors();

            output = null;
            try
            {
                var resourceFileInfo = ResourceInfo.ResourceFileInfoBuilder.Create(projectItem);
                if (resourceFileInfo == null)
                {
                    return(VSConstants.E_FAIL);
                }
                var reswCodeGenerator = ReswClassGenerator.CreateGenerator(resourceFileInfo, VSUIIntegration.Instance);
                if (reswCodeGenerator == null)
                {
                    return(VSConstants.E_FAIL);
                }
                VSUIIntegration.Instance?.SetStatusBar($"Generating class for {projectItem.Document.Name}...");

                var baseFilename     = "resources.generated." + GetCodeProvider().FileExtension; //won't be used.
                var generationResult = reswCodeGenerator.GenerateCode(baseFilename, inputFileContents, defaultNamespace, _isAdvanced);
                if (generationResult.Files.Count() != 1)
                {
                    return(VSConstants.E_FAIL);
                }

                // IVsSingleFileGenerator supports only 1 file.
                output = Encoding.UTF8.GetBytes(generationResult.Files.First().Content);

                if (generationResult.MustInstallReswPlusLib)
                {
                    projectItem.ContainingProject.InstallNuGetPackage("ReswPlusLib", true);
                }
            }
            catch (Exception)
            {
                return(VSConstants.E_FAIL);
            }
            finally
            {
                VSUIIntegration.Instance?.CleanStatusBar();
            }
            return(VSConstants.S_OK);
        }
コード例 #4
0
        public void TestTagIncorrectParsing()
        {
            var incorrectTags = new string[]
            {
                "",
                "   ",
                " #Format  ",
                "hello  #Format[ world",
                "#Firmat[Int]",
                "Hello Format[Int] World",
                "Format[Int]",
                "#Format(Int)",
            };

            foreach (var tag in incorrectTags)
            {
                var res = ReswClassGenerator.ParseTag(tag);
                Assert.Null(res.format);
            }
        }
コード例 #5
0
 public void TestTagMacros()
 {
     foreach (var parameters in new string[] {
         "SHORT_WEEKDAY",
         "LOCALE_NAME, Double",
         "UInt test, Char character",
         "TIME",
         "String test, Plural, VERSION_X, UInt test3",
         "Variant v",
         "APP_NAME, Variant v",
         "String test, VERSION_XY, Plural Int",
         "String test, Variant v, TIME",
     })
     {
         var res = ReswClassGenerator.ParseTag($"#Format[{parameters}]");
         Assert.False(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
         res = ReswClassGenerator.ParseTag($"#FormatNet[{parameters}]");
         Assert.True(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
     }
 }
コード例 #6
0
 public void TestTagStringRef()
 {
     foreach (var parameters in new string[] {
         "Test()",
         "Int, String()",
         "Hello(), Char character",
         "Plural, Welcome_Message()",
         "String test, Hello(), String test2, UInt test3",
         "Variant v, Test(), Test()",
         "hello(), String test, World(), Variant v",
         "String test, Variant v, Hello(), Plural Int",
         "String test, Hello(), Variant v, Plural Int test",
     })
     {
         var res = ReswClassGenerator.ParseTag($"#Format[{parameters}]");
         Assert.False(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
         res = ReswClassGenerator.ParseTag($"#FormatNet[{parameters}]");
         Assert.True(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
     }
 }
コード例 #7
0
 public void TestTagType()
 {
     foreach (var parameters in new string[] {
         "Double",
         "Int, Double",
         "UInt test, Char character",
         "Plural",
         "String test, Plural, String test2, UInt test3",
         "Variant v",
         "String test, Variant v",
         "String test, Variant v, Plural Int",
         "String test, Variant v, Plural Int test",
     })
     {
         var res = ReswClassGenerator.ParseTag($"#Format[{parameters}]");
         Assert.False(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
         res = ReswClassGenerator.ParseTag($"#FormatNet[{parameters}]");
         Assert.True(res.isDotNetFormatting);
         Assert.True(res.format != null && res.format.Any());
     }
 }
コード例 #8
0
        private int GenerateResourceFile(bool isAdvanced)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            VSUIIntegration.Instance?.ClearErrors();

            try
            {
                var projectItem = GetCurrentProjectItem();
                if (!projectItem.Name.EndsWith(".resw"))
                {
                    VsShellUtilities.ShowMessageBox(
                        package,
                        "File not compatible with ReswPlus",
                        "ReswPlus",
                        OLEMSGICON.OLEMSGICON_INFO,
                        OLEMSGBUTTON.OLEMSGBUTTON_OK,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    return(VSConstants.E_FAIL);
                }
                VSUIIntegration.Instance?.SetStatusBar($"Generating class for {Path.GetFileName(projectItem.Name)}...");

                var language = projectItem.ContainingProject.GetLanguage();
                if (language == Language.CSHARP || language == Language.VB)
                {
                    // Reset CustomTool to force the file generation.
                    projectItem.Properties.Item("CustomTool").Value = "";
                    projectItem.Properties.Item("CustomTool").Value = isAdvanced ? "ReswPlusAdvancedGenerator" : "ReswPlusGenerator";
                    return(VSConstants.S_OK);
                }
                else if (language == Language.CPPCX || language == Language.CPPWINRT)
                {
                    // CPP projects doesn't support custom tools, we need to create the file ourselves.
                    var filepath      = (string)projectItem.Properties.Item("FullPath").Value;
                    var fileNamespace = (string)projectItem.ContainingProject.Properties.Item("RootNamespace").Value;

                    var relativeDirectoryPath = Path.GetDirectoryName((string)projectItem.Properties.Item("RelativePath").Value);
                    var reswNamespace         = relativeDirectoryPath.StartsWith("..") ? "" : relativeDirectoryPath;
                    if (!string.IsNullOrEmpty(reswNamespace))
                    {
                        fileNamespace += "." + reswNamespace.Replace("\\", ".");
                    }

                    var resourceFileInfo = ResourceFileInfoBuilder.Create(projectItem);
                    if (resourceFileInfo == null)
                    {
                        return(VSConstants.E_FAIL);
                    }
                    var reswCodeGenerator = ReswClassGenerator.CreateGenerator(resourceFileInfo, VSUIIntegration.Instance);
                    if (reswCodeGenerator == null)
                    {
                        return(VSConstants.E_FAIL);
                    }

                    var inputFilepath    = projectItem.Properties.Item("FullPath").Value as string;
                    var baseFilename     = Path.GetFileNameWithoutExtension(filepath) + ".generated";
                    var generationResult = reswCodeGenerator.GenerateCode(baseFilename, File.ReadAllText(inputFilepath), fileNamespace, isAdvanced);
                    foreach (var file in generationResult.Files)
                    {
                        var generatedFilePath = Path.Combine(Path.GetDirectoryName(filepath), file.Filename);
                        using (var streamWriter = File.Create(generatedFilePath))
                        {
                            var contentBytes = System.Text.Encoding.UTF8.GetBytes(file.Content);
                            streamWriter.Write(contentBytes, 0, contentBytes.Length);
                        }
                        try
                        {
                            projectItem.ProjectItems.AddFromFile(generatedFilePath);
                        }
                        catch { }
                    }

                    if (generationResult.MustInstallReswPlusLib)
                    {
                        projectItem.ContainingProject.InstallNuGetPackage("ReswPlusLib", true);
                    }
                    return(VSConstants.S_OK);
                }
                else
                {
                    VsShellUtilities.ShowMessageBox(
                        package,
                        "Project language not compatible with ReswPlus",
                        "ReswPlus",
                        OLEMSGICON.OLEMSGICON_INFO,
                        OLEMSGBUTTON.OLEMSGBUTTON_OK,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    return(VSConstants.E_UNEXPECTED);
                }
            }
            finally
            {
                VSUIIntegration.Instance?.CleanStatusBar();
            }
        }