示例#1
0
        public List <ImportedScript> Merge(IEnumerable <string> importPaths)
        {
            if (!importPaths.Any())
            {
                throw new InvalidOperationException("ImportMerger requires at least one path to import.");
            }
            // step 2
            // merge the code in such a way that it can be parsed and interpreted (what a novel idea..)

            // TODO: handle imports that cannot be read directly (maybe external libraries in the form of dlls or w/e)
            var importedScripts = new List <ImportedScript>();

            foreach (var importPath in importPaths)
            {
                var source               = File.ReadAllText(importPath);
                var reader               = new ScriptReader(source, importPath);
                var importLexer          = new ImportLexer(reader, new CommonLexer(reader));
                var afterImportsPosition = importLexer.GetIndexAfterImports();
                var codeWithoutImports   = source.Substring(afterImportsPosition);
                var imported             = new ImportedScript(importPath, codeWithoutImports, reader.Line);
                importedScripts.Add(imported);
            }

            return(importedScripts);
        }
示例#2
0
        public IEnumerable <string> ResolveImportsFromFile(string pathToMain)
        {
            // step 1
            // read unique imports in code, add to list of new imports
            // recurse through all new imports and look for new unique imports
            // return an enumerable of unique imports
            var fileInfo      = new FileInfo(pathToMain);
            var rootDirectory = fileInfo.DirectoryName;
            var fileName      = fileInfo.Name;

            var check    = new HashSet <string>();
            var mainPath = ResolveImportPath(fileName.EndsWith(".ei") ? fileName : $"{fileName}.ei",
                                             rootDirectory);
            var allImports = new HashSet <string> {
                mainPath
            };

            ResolveInner(mainPath);

            void ResolveInner(string path)
            {
                if (check.Contains(path))
                {
                    return;
                }
                check.Add(path);
                var code   = GetCode(path);
                var reader = new ScriptReader(code, path);
                var lexer  = new ImportLexer(reader,
                                             new CommonLexer(reader)); // maybe inject ImportResolver with a LexerFactory,
                // not really necessary at this point I feel
                var imports = lexer.GetImports();

                foreach (var import in imports)
                {
                    var canonicalized =
                        ResolveImportPath(import.EndsWith(".ei") ? import : $"{import}.ei", path);
                    allImports.Add(canonicalized);
                    ResolveInner(canonicalized);
                }
            }

            return(allImports);
        }