fppkg.pp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. program fppkg;
  2. {$mode objfpc}{$H+}
  3. uses
  4. // General
  5. {$ifdef unix}
  6. baseunix,
  7. {$endif}
  8. Classes, SysUtils, TypInfo, custapp,
  9. // Repository handler objects
  10. fprepos, fpxmlrep,fpmktype, pkgropts,
  11. // Package Handler components
  12. pkghandler, pkgmkconv, pkgdownload,
  13. pkgfpmake, pkgmessages, pkgcommands
  14. // Downloaders
  15. {$if defined(unix) or defined(windows)}
  16. ,pkgwget
  17. {$endif}
  18. ;
  19. Type
  20. { TMakeTool }
  21. TMakeTool = Class(TCustomApplication)
  22. Private
  23. ParaAction : string;
  24. ParaPackages : TStringList;
  25. FDefaults: TPackagerOptions;
  26. FRepository : TFPRepository;
  27. FCompilerConfig : String;
  28. procedure GenerateParaActions;
  29. procedure LoadRepository;
  30. procedure MaybeCreateLocalDirs;
  31. procedure ShowUsage;
  32. Public
  33. Constructor Create;
  34. Destructor Destroy;override;
  35. Function GetConfigFileName : String;
  36. Procedure LoadGlobalDefaults;
  37. Procedure LoadCompilerDefaults;
  38. Procedure ProcessCommandLine;
  39. Procedure DoRun; Override;
  40. procedure ExecuteAction(APackage:TFPPackage; const AAction:string; const Args:TActionArgs);
  41. end;
  42. EMakeToolError = Class(Exception);
  43. { TMakeTool }
  44. function TMakeTool.GetConfigFileName: String;
  45. var
  46. G : Boolean;
  47. begin
  48. if HasOption('C','config-file') then
  49. Result:=GetOptionValue('C','config-file')
  50. else
  51. begin
  52. {$ifdef unix}
  53. g:=(fpgetuid=0);
  54. {$else}
  55. g:=true;
  56. {$endif}
  57. Result:=GetAppConfigFile(G,False);
  58. end
  59. end;
  60. procedure TMakeTool.LoadGlobalDefaults;
  61. var
  62. SL : TStringList;
  63. i : integer;
  64. begin
  65. FDefaults:=TPackagerOptions.Create;
  66. FDefaults.LoadGlobalFromFile(GetConfigFileName);
  67. // Load default verbosity from config
  68. SL:=TStringList.Create;
  69. SL.CommaText:=FDefaults.DefaultVerbosity;
  70. for i:=0 to SL.Count-1 do
  71. Include(Verbosity,StringToVerbosity(SL[i]));
  72. SL.Free;
  73. FCompilerConfig:=FDefaults.DefaultCompilerConfig;
  74. end;
  75. procedure TMakeTool.MaybeCreateLocalDirs;
  76. begin
  77. ForceDirectories(FDefaults.BuildDir);
  78. ForceDirectories(FDefaults.PackagesDir);
  79. ForceDirectories(FDefaults.CompilerConfigDir);
  80. end;
  81. procedure TMakeTool.LoadCompilerDefaults;
  82. var
  83. S : String;
  84. begin
  85. S:=FDefaults.CompilerConfigDir+FCompilerConfig;
  86. if FileExists(S) then
  87. begin
  88. Log(vDebug,SLogLoadingCompilerConfig,[S]);
  89. FDefaults.LoadCompilerFromFile(S)
  90. end
  91. else
  92. begin
  93. Log(vDebug,SLogGeneratingCompilerConfig,[S]);
  94. FDefaults.InitCompilerDefaults;
  95. FDefaults.SaveCompilerToFile(S);
  96. end;
  97. end;
  98. procedure TMakeTool.LoadRepository;
  99. var
  100. S : String;
  101. X : TFPXMLRepositoryHandler;
  102. begin
  103. FRepository:=TFPRepository.Create(Nil);
  104. // Repository
  105. Log(vDebug,SLogLoadingRepository,[FDefaults.LocalRepository]);
  106. if FileExists(FDefaults.LocalRepository) then
  107. begin
  108. X:=TFPXMLRepositoryHandler.Create;
  109. With X do
  110. try
  111. LoadFromXml(FRepository,FDefaults.LocalRepository);
  112. finally
  113. Free;
  114. end;
  115. end;
  116. // Versions
  117. S:=FDefaults.LocalVersions(FCompilerConfig);
  118. Log(vDebug,SLogLoadingVersions,[S]);
  119. if FileExists(S) then
  120. FRepository.LoadStatusFromFile(S);
  121. end;
  122. procedure TMakeTool.ShowUsage;
  123. begin
  124. Writeln('Usage: ',Paramstr(0),' [options] <action> <package>');
  125. Writeln('Options:');
  126. Writeln(' -r --compiler Set compiler');
  127. Writeln(' -h --help This help');
  128. Writeln(' -v --verbose Set verbosity');
  129. Writeln('Actions:');
  130. Writeln(' update Update available packages');
  131. Writeln(' listpackages List available packages');
  132. Writeln(' build Build package');
  133. Writeln(' install Install package');
  134. Writeln(' download Download package');
  135. Writeln(' convertmk Convert Makefile.fpc to fpmake.pp');
  136. Halt(0);
  137. end;
  138. Constructor TMakeTool.Create;
  139. begin
  140. inherited Create(nil);
  141. ParaPackages:=TStringList.Create;
  142. end;
  143. Destructor TMakeTool.Destroy;
  144. begin
  145. FreeAndNil(ParaPackages);
  146. inherited Destroy;
  147. end;
  148. procedure TMakeTool.ProcessCommandLine;
  149. Function CheckOption(Index : Integer;Short,Long : String): Boolean;
  150. var
  151. O : String;
  152. begin
  153. O:=Paramstr(Index);
  154. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  155. end;
  156. Function OptionArg(Var Index : Integer) : String;
  157. Var
  158. P : Integer;
  159. begin
  160. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  161. begin
  162. If Index<ParamCount then
  163. begin
  164. Inc(Index);
  165. Result:=Paramstr(Index);
  166. end
  167. else
  168. Error(SErrNeedArgument,[Index,ParamStr(Index)]);
  169. end
  170. else If length(ParamStr(Index))>2 then
  171. begin
  172. P:=Pos('=',Paramstr(Index));
  173. If (P=0) then
  174. Error(SErrNeedArgument,[Index,ParamStr(Index)])
  175. else
  176. begin
  177. Result:=Paramstr(Index);
  178. Delete(Result,1,P);
  179. end;
  180. end;
  181. end;
  182. Var
  183. I : Integer;
  184. HasAction : Boolean;
  185. begin
  186. I:=0;
  187. HasAction:=false;
  188. // We can't use the TCustomApplication option handling,
  189. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  190. While (I<ParamCount) do
  191. begin
  192. Inc(I);
  193. // Check options.
  194. if CheckOption(I,'r','compiler') then
  195. FDefaults.Compiler:=OptionArg(I)
  196. else if CheckOption(I,'v','verbose') then
  197. Include(Verbosity,StringToVerbosity(OptionArg(I)))
  198. else if CheckOption(I,'h','help') then
  199. begin
  200. ShowUsage;
  201. halt(0);
  202. end
  203. else if (Length(Paramstr(i))>0) and (Paramstr(I)[1]='-') then
  204. Raise EMakeToolError.CreateFmt(SErrInvalidArgument,[I,ParamStr(i)])
  205. else
  206. // It's a command or target.
  207. begin
  208. if HasAction then
  209. ParaPackages.Add(Paramstr(i))
  210. else
  211. begin
  212. ParaAction:=Paramstr(i);
  213. HasAction:=true;
  214. end;
  215. end;
  216. end;
  217. if not HasAction then
  218. ShowUsage;
  219. end;
  220. procedure TMakeTool.GenerateParaActions;
  221. var
  222. ActionPackage : TFPPackage;
  223. i : integer;
  224. begin
  225. if GetPkgHandler(ParaAction)<>nil then
  226. begin
  227. if ParaPackages.Count=0 then
  228. begin
  229. Log(vDebug,SLogCommandLineAction,['[<currentdir>]',ParaAction]);
  230. ActionStack.Push(nil,ParaAction,[]);
  231. end
  232. else
  233. begin
  234. for i:=0 to ParaPackages.Count-1 do
  235. begin
  236. ActionPackage:=FRepository.PackageByName(ParaPackages[i]);
  237. Log(vDebug,SLogCommandLineAction,['['+ActionPackage.Name+']',ParaAction]);
  238. ActionStack.Push(ActionPackage,ParaAction,[]);
  239. end;
  240. end;
  241. end
  242. else
  243. Raise EMakeToolError.CreateFmt(SErrInvalidCommand,[ParaAction]);
  244. end;
  245. procedure TMakeTool.ExecuteAction(APackage:TFPPackage;const AAction:string;const Args:TActionArgs);
  246. var
  247. pkghandlerclass : TPackageHandlerClass;
  248. i : integer;
  249. logargs : string;
  250. begin
  251. pkghandlerclass:=GetPkgHandler(AAction);
  252. With pkghandlerclass.Create(Self,FDefaults,APackage) do
  253. try
  254. logargs:='';
  255. for i:=Low(Args) to High(Args) do
  256. begin
  257. if logargs='' then
  258. logargs:=Args[i]
  259. else
  260. logargs:=logargs+','+Args[i];
  261. end;
  262. Log(vDebug,PackageLogPrefix+SLogRunAction,[AAction,logargs]);
  263. Execute(Args);
  264. finally
  265. Free;
  266. end;
  267. end;
  268. procedure TMakeTool.DoRun;
  269. var
  270. Action : string;
  271. ActionPackage : TFPPackage;
  272. Args : TActionArgs;
  273. OldCurrDir : String;
  274. begin
  275. LoadGlobalDefaults;
  276. OldCurrDir:=GetCurrentDir;
  277. Try
  278. ProcessCommandLine;
  279. MaybeCreateLocalDirs;
  280. LoadCompilerDefaults;
  281. LoadRepository;
  282. GenerateParaActions;
  283. repeat
  284. if not ActionStack.Pop(ActionPackage,Action,Args) then
  285. break;
  286. ExecuteAction(ActionPackage,Action,Args);
  287. until false;
  288. Terminate;
  289. except
  290. On E : Exception do
  291. begin
  292. Writeln(StdErr,SErrRunning);
  293. Writeln(StdErr,E.Message);
  294. Halt(1);
  295. end;
  296. end;
  297. SetCurrentDir(OldCurrDir);
  298. end;
  299. begin
  300. With TMakeTool.Create do
  301. try
  302. run;
  303. finally
  304. Free;
  305. end;
  306. end.