replaceunitnames.pp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2022 by Michael Van Canneyt, [email protected]
  4. Replace hardcoded unit names xyz in a makefile by a variable XYZUNIT.
  5. (see genunitnames for how to create the variables)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program replaceunitnames;
  13. uses regexpr,sysutils, classes, types, namespacetool, prefixer;
  14. function ReplaceWord(aLine, aName, aFull: String): String;
  15. var
  16. RE : TRegExpr;
  17. begin
  18. RE:=TRegExpr.Create('\b'+aName+'\b');
  19. try
  20. RE.ModifierI:=True;
  21. Result:=RE.Replace(aLine,aFull);
  22. // Writeln(aLine,': ',aName,' -> ',aFull,' = ',Result);
  23. finally
  24. RE.Free;
  25. end;
  26. end;
  27. function ReplaceUnits(const aLine: string; aUnitNames: TStrings): String;
  28. Var
  29. res,aName,aFull : String;
  30. begin
  31. Res:=aLine;
  32. for aName in aUnitNames do
  33. begin
  34. aFull:='$('+UpperCase(aName)+'UNIT)';
  35. Res:=ReplaceWord(Res,aName,aFull);
  36. end;
  37. Result:=Res;
  38. end;
  39. var
  40. i : Integer;
  41. L,aNames,aMakeFile: TStrings;
  42. aFN,aRule : String;
  43. begin
  44. aNames:=Nil;
  45. aMakeFile:=nil;
  46. L:=TStringList.Create;
  47. try
  48. aMakeFile:=TStringList.Create;
  49. aNames:=TStringList.Create;
  50. L.LoadFromFile(paramstr(1));
  51. for I:=0 to L.Count-1 do
  52. begin
  53. L.GetNameValue(I,aFN,aRule);
  54. aNames.Add(aFN);
  55. end;
  56. aMakeFile.LoadFromFile(Paramstr(2));
  57. aMakeFile.SaveToFile(Paramstr(2)+'.bak');
  58. For I:=0 to aMakefile.Count-1 do
  59. aMakefile[I]:=ReplaceUnits(aMakefile[I],aNames);
  60. aMakeFile.SaveToFile(ParamStr(2)+'.new');
  61. finally
  62. aMakeFile.Free;
  63. aNames.Free;
  64. L.Free;
  65. end;
  66. end.