internal static void GenerateModels(string modelsDirectory, string binPath)
        {
            if (!Directory.Exists(modelsDirectory))
            {
                Directory.CreateDirectory(modelsDirectory);
            }

            foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
            {
                File.Delete(file);
            }

            var umbraco    = Application.GetApplication();
            var typeModels = umbraco.GetAllTypes();

            var ourFiles    = Directory.GetFiles(modelsDirectory, "*.cs").ToDictionary(x => x, File.ReadAllText);
            var parseResult = new CodeParser().ParseWithReferencedAssemblies(ourFiles);
            var builder     = new TextBuilder(typeModels, parseResult, UmbracoConfig.For.ModelsBuilder().ModelsNamespace);

            foreach (var typeModel in builder.GetModelsToGenerate())
            {
                var sb = new StringBuilder();
                builder.Generate(sb, typeModel);
                var filename = Path.Combine(modelsDirectory, typeModel.ClrName + ".generated.cs");
                File.WriteAllText(filename, sb.ToString());
            }

            // the idea was to calculate the current hash and to add it as an extra file to the compilation,
            // in order to be able to detect whether a DLL is consistent with an environment - however the
            // environment *might not* contain the local partial files, and thus it could be impossible to
            // calculate the hash. So... maybe that's not a good idea after all?

            /*
             * var currentHash = HashHelper.Hash(ourFiles, typeModels);
             * ourFiles["models.hash.cs"] = $@"using Umbraco.ModelsBuilder;
             * [assembly:ModelsBuilderAssembly(SourceHash = ""{currentHash}"")]
             * ";
             */

            if (binPath != null)
            {
                //When bin directory is changed by config
                if (!Directory.Exists(binPath))
                {
                    Directory.CreateDirectory(binPath);
                }

                foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
                {
                    ourFiles[file] = File.ReadAllText(file);
                }
                var compiler = new Compiler();
                compiler.Compile(builder.GetModelsNamespace(), ourFiles, binPath);
            }

            OutOfDateModelsStatus.Clear();
        }
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var config = UmbracoConfig.For.ModelsBuilder();

            if (config.Enable)
            {
                FileService.SavingTemplate += FileService_SavingTemplate;
            }

            if (config.ModelsMode.IsLiveNotPure())
            {
                LiveModelsProvider.Install();
            }

            if (config.FlagOutOfDateModels)
            {
                OutOfDateModelsStatus.Install();
            }
        }
Exemplo n.º 3
0
        internal static void GenerateModels(string appData, string bin)
        {
            var modelsDirectory = Path.Combine(appData, "Models");

            if (!Directory.Exists(modelsDirectory))
            {
                Directory.CreateDirectory(modelsDirectory);
            }

            foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
            {
                File.Delete(file);
            }

            var umbraco    = Application.GetApplication();
            var typeModels = umbraco.GetAllTypes();

            var ourFiles    = Directory.GetFiles(modelsDirectory, "*.cs").ToDictionary(x => x, File.ReadAllText);
            var parseResult = new CodeParser().ParseWithReferencedAssemblies(ourFiles);
            var builder     = new TextBuilder(typeModels, parseResult, UmbracoConfig.For.ModelsBuilder().ModelsNamespace);

            foreach (var typeModel in builder.GetModelsToGenerate())
            {
                var sb = new StringBuilder();
                builder.Generate(sb, typeModel);
                var filename = Path.Combine(modelsDirectory, typeModel.ClrName + ".generated.cs");
                File.WriteAllText(filename, sb.ToString());
            }

            if (bin != null)
            {
                foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs"))
                {
                    ourFiles[file] = File.ReadAllText(file);
                }
                var compiler = new Compiler();
                compiler.Compile(builder.GetModelsNamespace(), ourFiles, bin);
            }

            OutOfDateModelsStatus.Clear();
        }
        public void Initialize(UmbracoServices umbracoServices)
        {
            var config = UmbracoConfig.For.ModelsBuilder();

            if (config.Enable)
            {
                FileService.SavingTemplate += FileService_SavingTemplate;
            }

            // fixme LiveModelsProvider should not be static
            if (config.ModelsMode.IsLiveNotPure())
            {
                LiveModelsProvider.Install(umbracoServices);
            }

            // fixme OutOfDateModelsStatus should not be static
            if (config.FlagOutOfDateModels)
            {
                OutOfDateModelsStatus.Install();
            }
        }