Esempio n. 1
0
        public void Generate(IEnumerable <TDef> all)
        {
            foreach (var unit in all)
            {
                Cfg = unit;
                if (!CanGenerate())
                {
                    continue;
                }
                var name = GetTypename(unit);

                /*var info = CsFilesManager.Instance.GetFileInfo(name, _nameSpace);
                 *
                 * var file = info.File;
                 * if (!info.IsEmbedded)
                 *  PrepareFile(file);
                 * var ns   = file.GetOrCreateNamespace(_nameSpace);
                 *
                 * Target           = ns.GetOrCreateClass(name);
                 * Target.IsPartial = true;*/
                Target = Get(name, out var file);
                GenerateOne();
                CsFilesManager.AddGeneratorName(file, GetType().Name);
            }
        }
Esempio n. 2
0
        private protected CsClass GetClass(string name)
        {
            var info2 = CsFilesManager.Instance.GetFileInfo(name, _nameSpace);
            var file  = info2.File;

            if (info2.IsEmbedded)
            {
                CsFilesManager.AddGeneratorName(file, GetType().Name);
                //file.BeginContent += "// generator: " + GetType().Name;
                // ProcessFile(file);
                var ns = file.GetOrCreateNamespace(_nameSpace);
                var cl = ns.GetOrCreateClass(name);
                cl.IsPartial = true;

                /*info          = new FileHolder(file, ns, cl);
                 * _clases[name] = info;
                 * return info.Cl;*/
                return(cl);
            }
            if (_clases.TryGetValue(name, out var info))
            {
                return(info.Cl);
            }
            {
                //file.BeginContent += "// generator: " + GetType().Name;
                ProcessFile(file);
                var ns = file.GetOrCreateNamespace(_nameSpace);
                var cl = ns.GetOrCreateClass(name);
                cl.IsPartial  = true;
                info          = new FileHolder(file, ns, cl);
                _clases[name] = info;
                return(info.Cl);
            }
        }
        private void Add_Properties()
        {
            foreach (var i in Cfg.Units)
            {
                var unitTypeName = Cfg.Name.ToUnitTypeName().GetTypename();

                var n2 = i.FieldName + unitTypeName;
                {
                    var constValue = i.UnitConstructor;
                    if (string.IsNullOrEmpty(constValue))
                    {
                        var args = i.UnitShortCode.GetCreationArgs(Related);
                        constValue = args.Create(unitTypeName);
                    }

                    Target.AddField(n2, unitTypeName)
                    .WithIsReadOnly()
                    .WithVisibility(Visibilities.Internal)
                    .WithConstValue(constValue)
                    .WithStatic();
                }
                {
                    var args = new[]
                    {
                        n2,
                        i.ScaleFactor
                    };

                    if (i.Aliases != null)
                    {
                        args = i.Aliases.Plus(args);
                    }

                    var unitDefinitionType = new CsArguments(unitTypeName)
                                             .MakeGenericType("UnitDefinition");

                    // public static readonly UnitDefinition<LengthUnit> Km
                    // = new UnitDefinition<LengthUnit>("km", 1000m);
                    var value = new CsArguments(args).Create(unitDefinitionType);
                    Target.AddField(i.FieldName, unitDefinitionType)
                    .WithIsReadOnly()
                    .WithStatic()
                    .WithConstValue(value);
                }
                if (i.AddFromMethod)
                {
                    var c = Get(Cfg.Name.ValueTypeName, out var file);
                    CsFilesManager.AddGeneratorName(file, GetType().Name);
                    var value = new CsArguments("value", Target.Name + "." + i.FieldName + ".Unit").Create(c.Name);
                    c.AddMethod("From" + i.FieldName, c.Name).WithBody($"return {value};")
                    .WithParameter(new CsMethodParameter("value", "decimal"));
                }
            }
        }