public CompilationResult Execute() { Program2.WriteInfo("Initializing..."); Init(); Program2.WriteInfo("Compiling views..."); foreach (var file in Options.DothtmlFiles) { try { CompileFile(file); } catch (DotvvmCompilationException exception) { result.Files.Add(file, new FileCompilationResult { Errors = new List <Exception>() { exception } }); } } Program2.WriteInfo("Emitting assemblies..."); Save(); Program2.WriteInfo("Building compilation results..."); return(result); }
internal static Assembly ResolveAssembly(object sender, ResolveEventArgs args) { if (Interlocked.CompareExchange(ref isResolveRunning, 1, 0) != 0) { return(null); } if (packagesDlls == null) { LoadDotNetCoreAssemblies(); } try { Program2.WriteInfo($"Resolving assembly `{args.Name}`."); var r = LoadFromAlternativeFolder(args.Name); if (r != null) { return(r); } Program2.WriteInfo($"Assembly `{args.Name}` resolve failed."); //We cannot do typeof(sometyhing).Assembly, because besides the compiler there are no dlls, dointhat will try to load the dll from the disk //which fails, so this event is called again call this event... return(null); } finally { isResolveRunning = 0; } }
private static void Main(string[] args) { if (!AppDomain.CurrentDomain.ShadowCopyFiles) { var appDomain = AppDomain.CreateDomain("SecondaryDomainShadowCopyAllowed", null, new AppDomainSetup { ShadowCopyFiles = "true" }); appDomain.ExecuteAssemblyByName(typeof(Program).Assembly.FullName, args); return; } Program2.ContinueMain(args); }
private void Save() { if (Options.FullCompile) { var bindingsAssemblyPath = bindingCompiler.OutputFileName; bindingCompiler.SaveAssembly(); Program2.WriteInfo($"Bindings saved to {bindingsAssemblyPath}."); compilation = compilation.AddReferences(MetadataReference.CreateFromFile(Path.GetFullPath(bindingsAssemblyPath))); var compiledViewsFileName = Path.Combine(Options.OutputPath, Options.AssemblyName + ".dll"); var result = compilation.Emit(compiledViewsFileName); if (!result.Success) { throw new Exception("The compilation failed!"); } Program2.WriteInfo($"Compiled views saved to {compiledViewsFileName}."); } }
protected ViewCompilationResult CompileView(string fileName) { var file = fileLoader.GetMarkup(configuration, fileName); // parse the document var tokenizer = new DothtmlTokenizer(); tokenizer.Tokenize(file.ContentsReaderFactory()); var parser = new DothtmlParser(); var node = parser.Parse(tokenizer.Tokens); var resolvedView = (ResolvedTreeRoot)controlTreeResolver.ResolveTree(node, fileName); var errorCheckingVisitor = new ErrorCheckingVisitor(); resolvedView.Accept(errorCheckingVisitor); foreach (var n in node.EnumerateNodes()) { if (n.HasNodeErrors) { throw new DotvvmCompilationException(string.Join(", ", n.NodeErrors), n.Tokens); } } var styleVisitor = new StylingVisitor(configuration); resolvedView.Accept(styleVisitor); var validationVisitor = new ControlUsageValidationVisitor(new DefaultControlUsageValidator()); resolvedView.Accept(validationVisitor); if (validationVisitor.Errors.Any()) { var controlUsageError = validationVisitor.Errors.First(); throw new DotvvmCompilationException(controlUsageError.ErrorMessage, controlUsageError.Nodes.SelectMany(n => n.Tokens)); } new LifecycleRequirementsAssigningVisitor().ApplyAction(resolvedView.Accept); DefaultViewCompilerCodeEmitter emitter = null; string fullClassName = null; if (Options.FullCompile) { var namespaceName = DefaultControlBuilderFactory.GetNamespaceFromFileName(file.FileName, file.LastWriteDateTimeUtc); var className = DefaultControlBuilderFactory.GetClassFromFileName(file.FileName) + "ControlBuilder"; fullClassName = namespaceName + "." + className; emitter = new DefaultViewCompilerCodeEmitter(); var compilingVisitor = new ViewCompilingVisitor(emitter, configuration.ServiceProvider.GetRequiredService <IBindingCompiler>(), className); resolvedView.Accept(compilingVisitor); // compile master pages if (resolvedView.Directives.ContainsKey("masterPage")) { CompileFile(resolvedView.Directives["masterPage"].Single().Value); } compilation = compilation .AddSyntaxTrees(emitter.BuildTree(namespaceName, className, fileName) /*.Select(t => SyntaxFactory.ParseSyntaxTree(t.GetRoot().NormalizeWhitespace().ToString()))*/) .AddReferences(emitter.UsedAssemblies .Select(a => CompiledAssemblyCache.Instance.GetAssemblyMetadata(a.Key).WithAliases(ImmutableArray.Create(a.Value)))); } Program2.WriteInfo($"The view { fileName } compiled successfully."); var res = new ViewCompilationResult { BuilderClassName = fullClassName, ControlType = resolvedView.Metadata.Type, DataContextType = emitter?.BuilderDataContextType, ResolvedTree = Options.OutputResolvedDothtmlMap ? resolvedView : null }; BuildFileResult(fileName, res); return(res); }