Ejemplo n.º 1
0
        private static void WriteFiles(GeneratedSourceText sourceCode, string path)
        {
            var directory = Path.Combine(Directory.GetCurrentDirectory(), path);

            Directory.CreateDirectory(directory);
            File.WriteAllText(Path.Combine(directory, sourceCode.HintName), sourceCode.Text.ToString());
        }
Ejemplo n.º 2
0
        public void AddSource(GeneratedSourceText sourceText)
        {
            var syntax = CSharpSyntaxTree.ParseText(sourceText.Text, new CSharpParseOptions(_sourceCompilation.LanguageVersion), "", null, true, _ct);

            _currentCompilation = _currentCompilation.AddSyntaxTrees(syntax);
            OnSourceGenerated?.Invoke(this, sourceText);
        }
Ejemplo n.º 3
0
    private static void ValidateGeneratedCSharpCode(
        string langCode,
        GeneratedSourceText generatedSource,
        string generatedClassName,
        Action <string>?hashCodeChecker = null,
        Action <IEnumerable <FieldInfo> >?fieldChecker = null,
        Action <I18nTextLateBinding>?textTableChecker  = null)
    {
        // The generated i18n text type source should be a valid C# code.
        var typeCode     = generatedSource.Text.ToString();
        var syntaxTree   = CSharpSyntaxTree.ParseText(typeCode);
        var assemblyName = Path.GetRandomFileName();
        var references   = new[] {
            MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
            MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location),
            MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").Location),
            MetadataReference.CreateFromFile(typeof(I18nTextFallbackLanguage).GetTypeInfo().Assembly.Location),
        };
        var compilation = CSharpCompilation.Create(
            assemblyName,
            syntaxTrees: new[] { syntaxTree },
            references: references,
            options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

        var compiledType = default(Type);

        using (var ms = new MemoryStream())
        {
            var result = compilation.Emit(ms);
            result.Success.IsTrue();

            ms.Seek(0, SeekOrigin.Begin);
            var assembly = AssemblyLoadContext.Default.LoadFromStream(ms);
            compiledType = assembly.GetType(generatedClassName);
        }

        // the i18n text type file should contain the i18n text typed public class.
        compiledType.IsNotNull();
        compiledType.IsClass.IsTrue();
        compiledType.IsPublic.IsTrue();

        // the i18n text typed class has fileds that are combined all languages files.
        var fields = compiledType.GetFields(BindingFlags.Instance | BindingFlags.Public);

        fieldChecker?.Invoke(fields);

        var textTableObj = Activator.CreateInstance(compiledType);

        textTableObj.IsNotNull();
        textTableObj.IsInstanceOf <I18nTextFallbackLanguage>().FallBackLanguage.Is(langCode);
        var textTableLateBinding = textTableObj.IsInstanceOf <I18nTextLateBinding>();

        textTableChecker?.Invoke(textTableLateBinding);

        // the i18n text typed class has the filed that is represent its hash code.
        var hashCode = textTableObj.IsInstanceOf <I18nTextTableHash>().Hash;

        hashCodeChecker?.Invoke(hashCode);
    }
Ejemplo n.º 4
0
            public SyntaxTree AddSource(GeneratedSourceText sourceText)
            {
                var compilation = DeclaringCompilation as CSharpCompilation ?? throw new NotSupportedException("Non C# compilation is not supported");
                var syntax      = CSharpSyntaxTree.ParseText(sourceText.SourceText, new CSharpParseOptions(compilation.LanguageVersion), cancellationToken: Parent._context.OnOperationCanceled);

                DeclaringCompilation = DeclaringCompilation.AddSyntaxTrees(syntax);
                GeneratedSourceTexts.Add(sourceText);
                return(syntax);
            }
Ejemplo n.º 5
0
 internal override SyntaxTree ParseGeneratedSourceText(
     GeneratedSourceText input,
     string fileName,
     CancellationToken cancellationToken
     ) =>
 CSharpSyntaxTree.ParseTextLazy(
     input.Text,
     (CSharpParseOptions)_state.ParseOptions,
     fileName
     );
Ejemplo n.º 6
0
 internal override SyntaxTree ParseGeneratedSourceText(GeneratedSourceText input, string fileName, CancellationToken cancellationToken)
 => SyntaxFactory.ParseSyntaxTree(input.Text, _state.ParseOptions, fileName, cancellationToken);
Ejemplo n.º 7
0
    private static void ValidateGeneratedCSharpCode(string langCode, string hashCode, GeneratedSourceText generatedSource, string generatedClassName, string[] generatedFieldNames)
    {
        ValidateGeneratedCSharpCode(langCode, generatedSource, generatedClassName,

                                    hashCodeChecker: actualHashCode =>
        {
            actualHashCode.Is(hashCode);
        },

                                    fieldChecker: fields =>
        {
            foreach (var generatedFieldName in generatedFieldNames)
            {
                fields.Where(f => f.FieldType == typeof(string)).Any(f => f.Name == generatedFieldName).IsTrue();
            }
        },

                                    textTableChecker: textTable =>
        {
            foreach (var generatedFieldName in generatedFieldNames)
            {
                textTable[generatedFieldName].Is(generatedFieldName);
            }
        });
    }
Ejemplo n.º 8
0
 internal override SyntaxTree ParseGeneratedSourceText(GeneratedSourceText input, CancellationToken cancellationToken)
 => SyntaxFactory.ParseSyntaxTree(input.Text, _state.ParseOptions, input.HintName, cancellationToken);     // https://github.com/dotnet/roslyn/issues/42628: hint path/ filename uniqueness