Ejemplo n.º 1
0
        private string BuidService(StringBuilderIndented builder)
        {
            builder
            .AppendLine("@Injectable()")
            .AppendLine("export class ResourceService {")
            .IncrementIndent()
            .AppendLine(Config.TypeScript.Resource.LazyLoad ? "constructor(private http: Http) {" : "constructor() {")
            .IncrementIndent();

            BuildLanguageSuportedContructor(builder);

            builder
            .AppendLine($"this.SetLanguage('{Config.TypeScript.Resource.CultureDefault.Name}')")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine();

            if (Config.TypeScript.Resource.LazyLoad)
            {
                builder
                .AppendLine("private Resources: Map<IResource> = {};");
            }

            builder
            .AppendLine("public Languages: ApplicationCulture[] = [];")
            .AppendLine("private Language: ApplicationCulture;")
            .AppendLine();

            foreach (var resource in Resources)
            {
                builder.AppendLine($"public {resource.ResourceName}: I{resource.ResourceName};");
            }

            builder
            .AppendLine()
            .AppendLine("public GetLanguage(): ApplicationCulture {")
            .IncrementIndent()
            .AppendLine("return this.Language;")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine()
            .AppendLine("public SetLanguage(language: string): void {")
            .IncrementIndent()
            .AppendLine()

            .AppendLine("if (this.Language && this.Language.Name === language) {")
            .IncrementIndent()
            .AppendLine("return;")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine()

            .AppendLine("let value = this.Languages.find((item: ApplicationCulture) => item.Name == language);")
            .AppendLine()
            .AppendLine("if (!value) {")
            .IncrementIndent()
            .AppendLine($"console.warn(`Unknown language: ${{language}}! Set up current culture as the default language: {Config.TypeScript.Resource.CultureDefault.Name}`);")
            .AppendLine($"this.SetLanguage('{Config.TypeScript.Resource.CultureDefault.Name}');")
            .AppendLine("return;")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine()
            .AppendLine("this.Language = value;")
            .AppendLine();


            if (!Config.TypeScript.Resource.LazyLoad)
            {
                BuildNormalService(builder);
            }

            if (Config.TypeScript.Resource.LazyLoad)
            {
                BuildLazyService(builder);
            }

            builder
            .DecrementIndent()
            .AppendLine("}")
            .DecrementIndent()
            .AppendLine("}");

            return(builder.ToString());
        }
Ejemplo n.º 2
0
 public void WriteFile(string path, string file, StringBuilderIndented builder, bool overrideIfExists)
 => WriteFile(path, file, builder.ToString(), overrideIfExists);
Ejemplo n.º 3
0
        private string WriteService(StringBuilderIndented builder)
        {
            // Service declaration
            builder
            .AppendLine("@Injectable()")
            .AppendLine($"export class {Config.TypeScript.Resource.ServiceName}Service {{")
            .IncrementIndent()
            .AppendLine("");

            var visibility = "public ";
            var setter     = " = ";
            var finalizer  = ";";
            var resources  = Resources[Config.TypeScript.Resource.CultureDefault.Code];
            var count      = 0;

            if (Config.TypeScript.Resource.Extractor)
            {
                visibility = "";
                setter     = ": ";
                finalizer  = ",";

                builder
                .AppendLine("public XCommon = {")
                .IncrementIndent();
            }

            // Resource Properties
            foreach (var resource in resources)
            {
                count++;

                if (resources.Count == count)
                {
                    finalizer = "";
                }

                builder
                .AppendLine($"{visibility}{resource.ResourceName}{setter}new {resource.ResourceName}Resource(){finalizer}");
            }

            if (Config.TypeScript.Resource.Extractor)
            {
                builder
                .DecrementIndent()
                .AppendLine("}")
                .AppendLine();
            }

            // Current Culture
            builder
            .AppendLine("public CurrentCulture = {")
            .IncrementIndent()
            .AppendLine($"Code: '{Config.TypeScript.Resource.CultureDefault.Code}',")
            .AppendLine($"Name: '{Config.TypeScript.Resource.CultureDefault.Name}'")
            .DecrementIndent()
            .AppendLine("};")
            .AppendLine();

            // Available cultures
            builder
            .AppendLine("public Cultures = [")
            .IncrementIndent();

            foreach (var culture in Config.TypeScript.Resource.Cultures)
            {
                var last  = Config.TypeScript.Resource.Cultures.Last().Code.Equals(culture.Code);
                var comma = last ? string.Empty : ",";
                builder.AppendLine($"{{ Code: '{culture.Code}', Name: '{culture.Name}' }}{comma}");
            }

            builder
            .DecrementIndent()
            .AppendLine("];")
            .AppendLine("");

            // Service constructor
            builder
            .AppendLine("constructor(private http: HttpClient) {")
            .AppendLine("}")
            .AppendLine("");

            // Set culture method
            builder
            .AppendLine("public setCulture(cultureCode: string): Observable<boolean> {")
            .AppendLine("")
            .IncrementIndent()
            .AppendLine("const newCulture = this.Cultures.find(c => c.Code === cultureCode);")
            .AppendLine("")
            .AppendLine("if (!newCulture) {")
            .IncrementIndent()
            .AppendLine("throw new Error(`Invalid culture: ${cultureCode}`);")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine("")
            .AppendLine("return new Observable<boolean>(observer => {")
            .AppendLine("")
            .IncrementIndent()
            .AppendLine("if (cultureCode === this.CurrentCulture.Code) {")
            .IncrementIndent()
            .AppendLine("observer.next(true);")
            .AppendLine("observer.complete();")
            .AppendLine("return;")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine("")
            .AppendLine($"this.http.get<any>(`{Config.TypeScript.Resource.RequestAddress}{Config.TypeScript.Resource.JsonPrefix}-${{cultureCode}}.json`)")
            .IncrementIndent()
            .AppendLine(".subscribe(res => {")
            .IncrementIndent();

            // Set the properties with the new language

            foreach (var item in Resources[Config.TypeScript.Resource.CultureDefault.Code])
            {
                if (Config.TypeScript.Resource.Extractor)
                {
                    builder
                    .AppendLine($"this.XCommon.{item.ResourceName} = res.{item.ResourceName};");
                }
                else
                {
                    builder
                    .AppendLine($"this.{item.ResourceName} = res.{item.ResourceName};");
                }
            }

            // Complete the service
            builder
            .AppendLine("")
            .AppendLine("this.CurrentCulture = newCulture;")
            .AppendLine("")
            .AppendLine("observer.next(true);")
            .AppendLine("observer.complete();")
            .DecrementIndent()
            .AppendLine("});")
            .DecrementIndent()
            .DecrementIndent()
            .AppendLine("});")
            .DecrementIndent()
            .AppendLine("}")
            .DecrementIndent();

            // Close service class
            builder
            .DecrementIndent()
            .AppendLine("}");

            return(builder.ToString());
        }