Beispiel #1
0
        public void OnRegistration(ICollection <IAuthorizationHandler> handlers)
        {
            var methId = GetScriptMethod("ПриРегистрацииОбработчиков", "OnHandlersRegistration");

            if (methId == -1)
            {
                return;
            }

            ArrayImpl newClasses = new ArrayImpl();
            var       args       = new IValue[] { newClasses };

            CallScriptMethod(methId, args);

            foreach (var classPath in newClasses.Select(x => x.AsString()))
            {
                var fInfo = _filesystem.GetFileInfo(classPath);
                if (!fInfo.Exists || fInfo.IsDirectory)
                {
                    throw new InvalidOperationException($"Module {classPath} is not found");
                }

                var codeSource = new FileInfoCodeSource(fInfo);
                var instance   = ScriptedAuthorizationHandler.CreateInstance(codeSource, _runtime);

                handlers.Add(instance);
            }
        }
Beispiel #2
0
        private void FillFeature(List <IFileInfo> sources)
        {
            var typeInfos = new List <TypeInfo>();

            foreach (var virtualPath in sources)
            {
                var code           = new FileInfoCodeSource(virtualPath);
                var compiler       = Engine.GetCompilerService();
                var img            = ScriptedViewComponent.CompileModule(compiler, code);
                var invokatorExist = img.Methods.Any(x =>
                                                     StringComparer.OrdinalIgnoreCase.Compare(ScriptedViewComponent.InvokeMethodNameRu, x.Signature.Name) == 0 ||
                                                     StringComparer.OrdinalIgnoreCase.Compare(ScriptedViewComponent.InvokeMethodNameEn, x.Signature.Name) == 0);

                if (!invokatorExist)
                {
                    continue;
                }

                var module       = Engine.LoadModuleImage(img);
                var baseFileName = System.IO.Path.GetFileNameWithoutExtension(code.SourceDescription);

                var builder = new ClassBuilder <ScriptedViewComponent>();
                var type    = builder.SetModule(module)
                              .SetTypeName(baseFileName + "ViewComponent")
                              .ExportMethods()
                              .ExportProperties()
                              .ExportConstructor((parameters) => new ScriptedViewComponent(builder.Module, builder.TypeName))
                              .ExportClassMethod("Invoke")
                              .Build();

                typeInfos.Add(type.GetTypeInfo());
            }

            _discoveredTypes = typeInfos.ToArray();
        }
        public static ScriptedAuthorizationHandler CreateInstance(FileInfoCodeSource codeSource, IApplicationRuntime runtime)
        {
            var compiler    = runtime.Engine.GetCompilerService();
            var moduleImage = compiler.Compile(codeSource);
            var module      = runtime.Engine.LoadModuleImage(moduleImage);

            return(new ScriptedAuthorizationHandler(module));
        }
Beispiel #4
0
        public ScriptedMiddlewareActivator(RequestDelegate next, IFileProvider scripts, IApplicationRuntime runtime, string scriptName)
        {
            _next    = next;
            _runtime = runtime;
            var codeSrc = new FileInfoCodeSource(scripts.GetFileInfo(scriptName));
            var image   = ScriptedMiddleware.CompileModule(runtime.Engine.GetCompilerService(), codeSrc);

            _module = new LoadedModule(image);
        }
        private void FillContext(IEnumerable <IFileInfo> sources, ApplicationModelProviderContext context)
        {
            var reflector = new TypeReflectionEngine();

            MachineInstance.Current.PrepareThread(_fw.Environment);
            foreach (var virtualPath in sources)
            {
                LoadedModule       module;
                FileInfoCodeSource codeSrc;
                string             typeName;
                if (virtualPath.IsDirectory)
                {
                    var info = FindModule(virtualPath.Name, MODULE_FILENAME)
                               ?? FindModule(virtualPath.Name, virtualPath.Name + ".os");

                    if (info == null)
                    {
                        continue;
                    }

                    codeSrc  = new FileInfoCodeSource(info);
                    typeName = virtualPath.Name;
                }
                else
                {
                    codeSrc  = new FileInfoCodeSource(virtualPath);
                    typeName = System.IO.Path.GetFileNameWithoutExtension(virtualPath.Name);
                }

                module = CompileControllerModule(codeSrc);

                var reflectedType = reflector.Reflect <ScriptedController>(module, typeName);
                var attrList      = MapAnnotationsToAttributes(_classAttribResolver.Attributes);
                var cm            = new ControllerModel(typeof(ScriptedController).GetTypeInfo(), attrList.AsReadOnly());
                cm.ControllerName = reflectedType.Name;
                var recompileInfo = new DynamicCompilationInfo()
                {
                    Module     = module,
                    CodeSource = codeSrc,
                    Tag        = cm
                };

                cm.Properties.Add("CompilationInfo", recompileInfo);
                cm.Properties.Add("type", reflectedType);

                ChangeToken.OnChange(() => CreateWatchToken(codeSrc),
                                     RecompileController,
                                     recompileInfo);

                FillActions(cm, reflectedType);
                FillFilters(cm);

                context.Result.Controllers.Add(cm);
            }
        }
Beispiel #6
0
        private void FillContext(IEnumerable <IFileInfo> sources, ApplicationModelProviderContext context)
        {
            var reflector = new TypeReflectionEngine();

            _fw.Environment.LoadMemory(MachineInstance.Current);
            foreach (var virtualPath in sources)
            {
                LoadedModule module;
                ICodeSource  codeSrc;
                string       typeName;
                if (virtualPath.IsDirectory)
                {
                    var info = FindModule(virtualPath.Name, MODULE_FILENAME)
                               ?? FindModule(virtualPath.Name, virtualPath.Name + ".os");

                    if (info == null)
                    {
                        continue;
                    }

                    codeSrc  = new FileInfoCodeSource(info);
                    typeName = virtualPath.Name;
                }
                else
                {
                    codeSrc  = new FileInfoCodeSource(virtualPath);
                    typeName = System.IO.Path.GetFileNameWithoutExtension(virtualPath.Name);
                }

                try
                {
                    _classAttribResolver.BeforeCompilation();
                    module = LoadControllerCode(codeSrc);
                }
                finally
                {
                    _classAttribResolver.AfterCompilation();
                }

                var reflectedType = reflector.Reflect <ScriptedController>(module, typeName);
                var attrList      = MapAnnotationsToAttributes(_classAttribResolver.Attributes);
                var cm            = new ControllerModel(typeof(ScriptedController).GetTypeInfo(), attrList.AsReadOnly());
                cm.ControllerName = reflectedType.Name;
                cm.Properties.Add("module", module);
                cm.Properties.Add("type", reflectedType);

                FillActions(cm, reflectedType);
                FillFilters(cm);

                context.Result.Controllers.Add(cm);
            }
        }
        private IChangeToken CreateWatchToken(FileInfoCodeSource codeSrc)
        {
            var cPath = codeSrc.FileInfo.PhysicalPath.Replace('\\', '/');
            var root  = Path.GetDirectoryName(_app.Module.ModuleInfo.Origin).Replace('\\', '/');

            if (!cPath.StartsWith(root))
            {
                _logger.LogWarning($"file {cPath} can't be watched since it's not in root folder {root}");
                return(NullChangeToken.Singleton);
            }

            return(_scriptsProvider.Watch(cPath.Substring(root.Length)));
        }
        private void AppendScriptedHandlers(IApplicationRuntime runtime, IFileProvider filesystem)
        {
            var authFile = filesystem.GetFileInfo("auth.os");

            if (!authFile.Exists || authFile.IsDirectory)
            {
                return;
            }

            runtime.Environment.LoadMemory(MachineInstance.Current);

            var codeSource = new FileInfoCodeSource(authFile);

            var registrator = AuthorizationModule.CreateInstance(codeSource, runtime, filesystem);

            registrator.OnRegistration(_handlers);
        }