diff --git a/src/ce_common.pas b/src/ce_common.pas index a5e46b88..fe0995c6 100644 --- a/src/ce_common.pas +++ b/src/ce_common.pas @@ -205,7 +205,7 @@ type (** * Clears then fills aList with aProcess output stream. *) - procedure processOutputToStrings(aProcess: TProcess; var aList: TStringList); + procedure processOutputToStrings(aProcess: TProcess; aList: TStrings); (** * Copy available process output to a stream. @@ -856,7 +856,7 @@ begin exit(ExeSearch(anExeName, env)); end; -procedure processOutputToStrings(aProcess: TProcess; var aList: TStringList); +procedure processOutputToStrings(aProcess: TProcess; aList: TStrings); var str: TMemoryStream; sum: Integer = 0; diff --git a/src/ce_dastworx.pas b/src/ce_dastworx.pas new file mode 100644 index 00000000..33f73fc8 --- /dev/null +++ b/src/ce_dastworx.pas @@ -0,0 +1,87 @@ +unit ce_dastworx; +{$I ce_defines.inc} + +interface + +uses + Classes, SysUtils, process, ce_common; + +(** + * Gets the module name and the imports of the source code located in + * "source". The first line of "import" contains the module name, double quoted. + * Each following line contain an import. + *) +procedure getModuleImports(source, imports: TStrings); + +(** + * Gets the module names and the imports of the sources in "files". + * source. Each line in "import" that contains double quoted text indicates + * that a new group of import starts. + *) +procedure getModulesImports(const files: string; results: TStrings); + +implementation + +var + toolname: string; + +function getToolName: string; +begin + if toolname = '' then + toolname := exeFullName('dastworx' + exeExt); + exit(toolname); +end; + +procedure getModuleImports(source, imports: TStrings); +var + str: string; + prc: TProcess; +begin + str := getToolName; + if str.isEmpty then + exit; + prc := TProcess.Create(nil); + try + prc.Executable := str; + prc.Parameters.Add('-i'); + prc.Options := [poUsePipes{$IFDEF WINDOWS}, poNewConsole{$ENDIF}]; + prc.ShowWindow := swoHIDE; + prc.Execute; + str := source.Text; + prc.Input.Write(str[1], str.length); + prc.CloseInput; + while prc.Running do + sleep(1); + processOutputToStrings(prc, imports); + finally + prc.free; + end; +end; + +procedure getModulesImports(const files: string; results: TStrings); +var + str: string; + prc: TProcess; +begin + str := getToolName; + if str.isEmpty then + exit; + prc := TProcess.Create(nil); + try + prc.Executable := str; + prc.Parameters.Add(files); + prc.Parameters.Add('-i'); + prc.Options := [poUsePipes, poStderrToOutPut{$IFDEF WINDOWS}, poNewConsole{$ENDIF}]; + prc.ShowWindow := swoHIDE; + prc.Execute; + prc.CloseInput; + while prc.Running do + sleep(1); + processOutputToStrings(prc, results); + finally + prc.free; + end; +end; + +end. + diff --git a/src/ce_libman.pas b/src/ce_libman.pas index 391ea0f8..68661a47 100644 --- a/src/ce_libman.pas +++ b/src/ce_libman.pas @@ -568,31 +568,30 @@ end; procedure TLibraryManager.getLibsForSource(source, libs, paths: TStrings); var - tks: TLexTokenList; imp: TStringList; i,j: integer; itm: TLibraryItem; dep: TLibraryItem; sel: TLibraryList; begin - tks := TLexTokenList.Create; imp := TStringList.Create; - sel := TLibraryList.Create; + sel := TLibraryList.create; try - lex(source.Text, tks, nil, [lxoNoComments]); - getImports(tks, imp); - for i:= 0 to imp.Count-1 do + getModuleImports(source, imp); + for i:= 1 to imp.Count-1 do begin // get library for import I itm := libraryByImport[imp[i]]; if itm.isNotNil then begin + if sel.contains(itm) then + continue; sel.insert(itm); // get libraries for import I dependencies for j:= itm.dependencies.Count-1 downto 0 do begin dep := libraryByAlias[itm.dependencies[j]]; - if dep.isNotNil then + if dep.isNotNil and not sel.contains(dep) then sel.insert(dep) //auto update: item removed, detect on usage that it has disapeared else @@ -614,7 +613,6 @@ begin end; finally sel.Free; - tks.Free; imp.Free; end; end; @@ -647,7 +645,9 @@ begin if dep.isNil or (dep.libAlias = 'phobos') or (dep.libAlias = 'druntime') then continue; // add deps - libraryByIndex[i].dependencies.Add(dep.libAlias); + if lib.dependencies.IndexOf(dep.libAlias) > -1 then + continue; + lib.dependencies.Add(dep.libAlias); end; end; end;