pas2fpm.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. program pas2fpm;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. Classes, SysUtils, CustApp, passrcutil;
  8. type
  9. { TPas2FPMakeApp }
  10. TPas2FPMakeApp = class(TCustomApplication)
  11. private
  12. procedure AddLine(const ALine: String);
  13. function CheckParams : boolean;
  14. procedure CreateSources;
  15. function GetUnitProps(const FN: String; out Res: Boolean; U: TStrings
  16. ): Boolean;
  17. procedure WriteProgEnd;
  18. procedure WriteProgStart;
  19. procedure WriteSources;
  20. protected
  21. FFiles,
  22. FSrc,
  23. FUnits: TStrings;
  24. FOutputFile : string;
  25. procedure DoRun; override;
  26. public
  27. constructor Create(TheOwner: TComponent); override;
  28. destructor Destroy; override;
  29. procedure WriteHelp; virtual;
  30. end;
  31. { TPas2FPMakeApp }
  32. Function TPas2FPMakeApp.CheckParams : Boolean;
  33. Var
  34. I : Integer;
  35. S : String;
  36. begin
  37. Result:=True;
  38. I:=1;
  39. While I<=ParamCount do
  40. begin
  41. S:=Paramstr(i);
  42. if (S<>'') then
  43. begin
  44. if S[1]<>'-' then
  45. begin
  46. FFiles.Add(S);
  47. FUnits.Add(ChangeFileExt(ExtractFileName(S),''));
  48. end
  49. else
  50. begin
  51. If (s='-o') then
  52. else
  53. begin
  54. Result:=False;
  55. exit;
  56. end;
  57. end;
  58. end;
  59. Inc(i);
  60. end;
  61. Result:=(FFiles.Count>0);
  62. end;
  63. procedure TPas2FPMakeApp.AddLine(Const ALine : String);
  64. begin
  65. FSrc.Add(ALine);
  66. end;
  67. Function TPas2FPMakeApp.GetUnitProps(Const FN : String; Out Res : Boolean; U : TStrings) : Boolean;
  68. Var
  69. I : Integer;
  70. A : TPasSrcAnalysis;
  71. begin
  72. Result:=False;
  73. try
  74. A:=TPasSrcAnalysis.Create(Self);
  75. try
  76. A.FileName:=FN;
  77. Res:=A.HasResourcestrings;
  78. A.GetUsedUnits(U);
  79. For I:=U.Count-1 downto 0 do
  80. if FUnits.IndexOf(U[i])=-1 then
  81. U.Delete(i);
  82. finally
  83. A.Free;
  84. end;
  85. Result:=True;
  86. except
  87. // Ignore
  88. end;
  89. end;
  90. procedure TPas2FPMakeApp.WriteProgStart;
  91. begin
  92. AddLine('program fpmake;');
  93. AddLine('');
  94. AddLine('uses fpmkunit;');
  95. AddLine('');
  96. AddLine('Var');
  97. AddLine(' T : TTarget;');
  98. AddLine(' P : TPackage;');
  99. AddLine('begin');
  100. AddLine(' With Installer do');
  101. AddLine(' begin');
  102. AddLine(' P.Version:=''0.0'';');
  103. // AddLine(' P.Dependencies.Add('fcl-base');
  104. AddLine(' P.Author := ''Your name'';');
  105. AddLine(' P.License := ''LGPL with modification'';');
  106. AddLine(' P.HomepageURL := ''www.yourcompany.com'';');
  107. AddLine(' P.Email := ''[email protected]'';');
  108. AddLine(' P.Description := ''Your very nice program'';');
  109. AddLine(' // P.NeedLibC:= false;');
  110. end;
  111. procedure TPas2FPMakeApp.WriteProgEnd;
  112. begin
  113. AddLine(' Run;');
  114. AddLine(' end;');
  115. AddLine('end.');
  116. end;
  117. procedure TPas2FPMakeApp.CreateSources;
  118. Var
  119. I,j : Integer;
  120. U : TStrings;
  121. FN : String;
  122. R : Boolean;
  123. begin
  124. WriteProgStart;
  125. For I:=0 to FFiles.Count-1 do
  126. begin
  127. FN:=FFiles[i];
  128. AddLine(' T:=P.Targets.AddUnit('''+FN+''');');
  129. U:=TStringList.Create;
  130. if not GetUnitProps(Fn,R,U) then
  131. AddLine(' // Failed to analyse unit '+FN)
  132. else
  133. begin
  134. if R then
  135. AddLine(' T.ResourceStrings := True;');
  136. if (U.Count>0) then
  137. begin
  138. AddLine(' with T.Dependencies do');
  139. AddLine(' begin');
  140. For J:=0 to U.Count-1 do
  141. AddLine(' AddUnit('''+U[j]+''');');
  142. AddLine(' end;');
  143. end;
  144. end;
  145. end;
  146. WriteProgEnd;
  147. end;
  148. procedure TPas2FPMakeApp.WriteSources;
  149. Var
  150. F : Text;
  151. begin
  152. AssignFile(F,FOutputFile);
  153. Rewrite(F);
  154. try
  155. Write(F,FSrc.Text);
  156. finally
  157. CloseFile(F);
  158. end;
  159. end;
  160. procedure TPas2FPMakeApp.DoRun;
  161. var
  162. ErrorMsg: String;
  163. begin
  164. // parse parameters
  165. if HasOption('h','help') or Not CheckParams then
  166. begin
  167. WriteHelp;
  168. Terminate;
  169. exit;
  170. end;
  171. TStringList(FUnits).Sorted:=True;
  172. CreateSources;
  173. WriteSources;
  174. // stop program loop
  175. Terminate;
  176. end;
  177. constructor TPas2FPMakeApp.Create(TheOwner: TComponent);
  178. begin
  179. inherited Create(TheOwner);
  180. StopOnException:=True;
  181. FFiles:=TStringList.Create;
  182. FSrc:=TStringList.Create;
  183. FUnits:=TStringList.Create;
  184. end;
  185. destructor TPas2FPMakeApp.Destroy;
  186. begin
  187. FreeAndNil(FFiles);
  188. FreeAndNil(FSrc);
  189. FreeAndNil(FUnits);
  190. inherited Destroy;
  191. end;
  192. procedure TPas2FPMakeApp.WriteHelp;
  193. begin
  194. { add your help code here }
  195. writeln('Usage: ',ExeName,' [-h] [-o outputfile] file1 .. filen');
  196. end;
  197. var
  198. Application: TPas2FPMakeApp;
  199. begin
  200. Application:=TPas2FPMakeApp.Create(nil);
  201. Application.Title:='Pascal to FPMake application';
  202. Application.Run;
  203. Application.Free;
  204. end.