static SourceText?TryLoadSourceText(string directory, string path) { var full = Path.GetFullPath(Path.Combine(directory, Path.ChangeExtension(path, ModuleFileNameExtension) !)); try { return(StringSourceText.FromAsync(full, File.OpenRead(full)).Result); } catch (Exception) { // TODO: Catch more specific exceptions? return(null); } }
int Run(Options options) { var path = options.Module.FullName; if (!options.Module.Exists) { Log.ErrorLine("Module '{0}' could not be found.", path); return(1); } var loader = new StandardModuleLoader(ModuleLoaderMode.Normal); _ = loader.SearchPaths.Add(Path.GetDirectoryName(path) !); var text = StringSourceText.FromAsync(path, File.OpenRead(path)).Result; var context = new SyntaxContext(); try { var mod = loader.LoadModule(text, context); foreach (var decl in mod.Declarations.OfType <Function>()) { decl.Test(); } } catch (ModuleLoadException) { // All errors will be reported in the context. } foreach (var diag in context.Diagnostics) { LogDiagnostic(diag); } // TODO return(context.HasDiagnostics ? 1 : 0); }
protected override SourceText?GetSourceText(ModulePath path) { // Core modules are not subject to any security checks, so make sure they're not loaded // from arbitrary file system locations. if (path.IsCore) { try { var rsc = $"{nameof(Flare)}.{string.Join('.', path.Components)}.{ModuleFileNameExtension}"; var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(rsc); return(stream != null?StringSourceText.FromAsync(Path.Combine(_assemblyPath, rsc), stream).Result : null); } catch (Exception e) { throw new ModuleLoadException($"Core module '{path}' failed to load: {e.Message}", e); } } var str = string.Join(Path.DirectorySeparatorChar, path.Components); foreach (var dir in SearchPaths) { if (TryLoadSourceText(dir, str) is SourceText source) { return(source); } } if (!UseEnvironmentVariable) { return(null); } foreach (var dir in GetSearchPaths(EnvironmentVariableTarget.Process)) { if (TryLoadSourceText(dir, str) is SourceText source) { return(source); } } foreach (var dir in GetSearchPaths(EnvironmentVariableTarget.User)) { if (TryLoadSourceText(dir, str) is SourceText source) { return(source); } } foreach (var dir in GetSearchPaths(EnvironmentVariableTarget.Machine)) { if (TryLoadSourceText(dir, str) is SourceText source) { return(source); } } return(null); }