2
0

pkguninstalledsrcsrepo.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. unit pkgUninstalledSrcsRepo;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes,
  6. SysUtils,
  7. fpmkunit,
  8. pkgoptions,
  9. pkgFppkg,
  10. pkgglobals,
  11. pkgmessages,
  12. fprepos,
  13. pkgrepos,
  14. pkghandler,
  15. pkgPackagesStructure;
  16. type
  17. { TFppkgUninstalledSourceRepositoryOptionSection }
  18. TFppkgUninstalledSourceRepositoryOptionSection = class(TFppkgRepositoryOptionSection)
  19. public
  20. function GetRepositoryType: TFPRepositoryType; override;
  21. end;
  22. { TFPUninstalledSourcesAvailablePackagesStructure }
  23. TFPUninstalledSourcesAvailablePackagesStructure = class(TFPCustomFileSystemPackagesStructure)
  24. public
  25. class function GetRepositoryOptionSectionClass: TFppkgRepositoryOptionSectionClass; override;
  26. procedure InitializeWithOptions(ARepoOptionSection: TFppkgRepositoryOptionSection; AnOptions: TFppkgOptions; ACompilerOptions: TCompilerOptions); override;
  27. function AddPackagesToRepository(ARepository: TFPRepository): Boolean; override;
  28. function GetBuildPathDirectory(APackage: TFPPackage): string; override;
  29. end;
  30. { TFppkgUninstalledRepositoryOptionSection }
  31. TFppkgUninstalledRepositoryOptionSection = class(TFppkgRepositoryOptionSection)
  32. private
  33. FSourceRepositoryName: string;
  34. public
  35. procedure AddKeyValue(const AKey, AValue: string); override;
  36. procedure LogValues(ALogLevel: TLogLevel); override;
  37. function GetRepositoryType: TFPRepositoryType; override;
  38. property SourceRepositoryName: string read FSourceRepositoryName write FSourceRepositoryName;
  39. end;
  40. { TFPUninstalledSourcesPackagesStructure }
  41. TFPUninstalledSourcesPackagesStructure = class(TFPCustomFileSystemPackagesStructure)
  42. private
  43. FSourceRepositoryName: string;
  44. FLinkedRepositoryName: string;
  45. public
  46. class function GetRepositoryOptionSectionClass: TFppkgRepositoryOptionSectionClass; override;
  47. procedure InitializeWithOptions(ARepoOptionSection: TFppkgRepositoryOptionSection; AnOptions: TFppkgOptions; ACompilerOptions: TCompilerOptions); override;
  48. function AddPackagesToRepository(ARepository: TFPRepository): Boolean; override;
  49. function IsInstallationNeeded(APackage: TFPPackage): TFPInstallationNeeded; override;
  50. function GetBaseInstallDir: string; override;
  51. function GetConfigFileForPackage(APackage: TFPPackage): string; override;
  52. property SourceRepositoryName: string read FSourceRepositoryName write FSourceRepositoryName;
  53. end;
  54. implementation
  55. const
  56. KeySourceRepository = 'SourceRepository';
  57. SLogSourceRepository = ' SourceRepository:%s';
  58. { TFPUninstalledSourcesPackagesStructure }
  59. class function TFPUninstalledSourcesPackagesStructure.GetRepositoryOptionSectionClass: TFppkgRepositoryOptionSectionClass;
  60. begin
  61. Result := TFppkgUninstalledRepositoryOptionSection;
  62. end;
  63. procedure TFPUninstalledSourcesPackagesStructure.InitializeWithOptions(
  64. ARepoOptionSection: TFppkgRepositoryOptionSection; AnOptions: TFppkgOptions;
  65. ACompilerOptions: TCompilerOptions);
  66. var
  67. RepoOptionSection: TFppkgUninstalledRepositoryOptionSection;
  68. begin
  69. inherited InitializeWithOptions(ARepoOptionSection, AnOptions, ACompilerOptions);
  70. RepoOptionSection := ARepoOptionSection as TFppkgUninstalledRepositoryOptionSection;
  71. path := RepoOptionSection.Path;
  72. SourceRepositoryName := RepoOptionSection.SourceRepositoryName;
  73. FLinkedRepositoryName := RepoOptionSection.RepositoryName;
  74. end;
  75. function TFPUninstalledSourcesPackagesStructure.AddPackagesToRepository(ARepository: TFPRepository): Boolean;
  76. var
  77. SRD : TSearchRec;
  78. SRF : TSearchRec;
  79. P : TFPPackage;
  80. UF,UD : String;
  81. begin
  82. Result:=false;
  83. log(llDebug,SLogFindInstalledPackages,[Path]);
  84. if FindFirst(Path+AllFiles,faDirectory,SRD)=0 then
  85. begin
  86. repeat
  87. // Try new .fpm-file
  88. UD:=Path+SRD.Name+PathDelim;
  89. if FindFirst(UD+'*'+FpmkExt,faAnyFile,SRF)=0 then
  90. begin
  91. repeat
  92. UF := UD+SRF.Name;
  93. P:=ARepository.AddPackage(ChangeFileExt(SRF.Name,''));
  94. P.LoadUnitConfigFromFile(UF);
  95. P.PackagesStructure:=Self;
  96. if P.IsFPMakeAddIn then
  97. AddFPMakeAddIn(P);
  98. until FindNext(SRF)<>0;
  99. end;
  100. FindClose(SRF);
  101. until FindNext(SRD)<>0;
  102. end;
  103. FindClose(SRD);
  104. Result:=true;
  105. end;
  106. function TFPUninstalledSourcesPackagesStructure.IsInstallationNeeded(APackage: TFPPackage): TFPInstallationNeeded;
  107. begin
  108. if (APackage.Repository.RepositoryName=SourceRepositoryName) or
  109. (Assigned(APackage.Repository.DefaultPackagesStructure) and (APackage.Repository.DefaultPackagesStructure.InstallRepositoryName=FLinkedRepositoryName)) then
  110. Result := fpinNoInstallationNeeded
  111. else
  112. Result := fpinInstallationImpossible;
  113. end;
  114. function TFPUninstalledSourcesPackagesStructure.GetBaseInstallDir: string;
  115. begin
  116. Result := Path;
  117. end;
  118. function TFPUninstalledSourcesPackagesStructure.GetConfigFileForPackage(APackage: TFPPackage): string;
  119. begin
  120. if APackage.SourcePath<>'' then
  121. Result := IncludeTrailingPathDelimiter(APackage.SourcePath)
  122. else
  123. Result := IncludeTrailingPathDelimiter(GetBaseInstallDir)+APackage.Name+PathDelim;
  124. Result := Result +APackage.Name+'-'+FCompilerOptions.CompilerTarget+FpmkExt;
  125. end;
  126. { TFppkgUninstalledRepositoryOptionSection }
  127. procedure TFppkgUninstalledRepositoryOptionSection.AddKeyValue(const AKey, AValue: string);
  128. begin
  129. if SameText(AKey,KeySourceRepository) then
  130. SourceRepositoryName := AValue
  131. else
  132. inherited AddKeyValue(AKey, AValue);
  133. end;
  134. procedure TFppkgUninstalledRepositoryOptionSection.LogValues(ALogLevel: TLogLevel);
  135. begin
  136. inherited LogValues(ALogLevel);
  137. log(ALogLevel,SLogSourceRepository,[FSourceRepositoryName]);
  138. end;
  139. function TFppkgUninstalledRepositoryOptionSection.GetRepositoryType: TFPRepositoryType;
  140. begin
  141. Result := fprtInstalled;
  142. end;
  143. { TFppkgUninstalledSourceRepositoryOptionSection }
  144. function TFppkgUninstalledSourceRepositoryOptionSection.GetRepositoryType: TFPRepositoryType;
  145. begin
  146. Result := fprtAvailable;
  147. end;
  148. { TFPUninstalledSourcesPackagesStructure }
  149. class function TFPUninstalledSourcesAvailablePackagesStructure.GetRepositoryOptionSectionClass: TFppkgRepositoryOptionSectionClass;
  150. begin
  151. Result := TFppkgUninstalledSourceRepositoryOptionSection;
  152. end;
  153. procedure TFPUninstalledSourcesAvailablePackagesStructure.InitializeWithOptions(
  154. ARepoOptionSection: TFppkgRepositoryOptionSection; AnOptions: TFppkgOptions;
  155. ACompilerOptions: TCompilerOptions);
  156. begin
  157. inherited InitializeWithOptions(ARepoOptionSection, AnOptions, ACompilerOptions);
  158. path := TFppkgUninstalledSourceRepositoryOptionSection(ARepoOptionSection).Path;
  159. end;
  160. function TFPUninstalledSourcesAvailablePackagesStructure.AddPackagesToRepository(ARepository: TFPRepository): Boolean;
  161. var
  162. SR : TSearchRec;
  163. AManifestFile, AFPMakeFile: String;
  164. i: Integer;
  165. TempPackagesStructure: TFPTemporaryDirectoryPackagesStructure;
  166. TempRepo: TFPRepository;
  167. PackageName: string;
  168. PackageManager: TpkgFppkg;
  169. begin
  170. Result:=false;
  171. TempPackagesStructure := TFPTemporaryDirectoryPackagesStructure.Create(Owner);
  172. TempPackagesStructure.InitializeWithOptions(nil, FOptions, FCompilerOptions);
  173. TempRepo := TFPRepository.Create(Owner);
  174. TempRepo.RepositoryName := 'TempScanUninstPackages';
  175. TempRepo.Description := 'Temp list of packages during scanning of source-packages';
  176. TempRepo.RepositoryType := fprtAvailable;
  177. TempRepo.DefaultPackagesStructure := TempPackagesStructure;
  178. TempPackagesStructure.AddPackagesToRepository(TempRepo);
  179. if Owner is TPkgFppkg then
  180. PackageManager := TPkgfppkg(owner)
  181. else
  182. PackageManager := GFPpkg;
  183. if Assigned(PackageManager) then
  184. PackageManager.RepositoryList.Add(TempRepo);
  185. try
  186. log(llDebug,SLogFindInstalledPackages,[Path]);
  187. if FindFirst(Path+AllFiles,faDirectory,SR)=0 then
  188. begin
  189. repeat
  190. if ((SR.Attr and faDirectory)=faDirectory) and (SR.Name<>'.') and (SR.Name<>'..') then
  191. begin
  192. AFPMakeFile := Path+SR.Name+PathDelim+FPMakePPFile;
  193. if FileExistsLog(AFPMakeFile) then
  194. begin
  195. AManifestFile := Path+SR.Name+PathDelim+ManifestFile;
  196. if not FileExists(AManifestFile) or (FileAge(AManifestFile) < FileAge(AFPMakeFile)) then
  197. begin
  198. // (Re-)create manifest
  199. if assigned(PackageManager) then
  200. begin
  201. try
  202. TempPackagesStructure.SetTempPath(Path+SR.Name);
  203. PackageName := SR.Name + '_create_manifest';
  204. TempPackagesStructure.TempPackageName := PackageName;
  205. pkghandler.ExecuteAction(PackageName,'fpmakemanifest',PackageManager);
  206. except
  207. on E: Exception do
  208. begin
  209. log(llWarning, SLogFailedToCreateManifest ,[AFPMakeFile, E.Message]);
  210. Continue;
  211. end;
  212. end;
  213. end
  214. else
  215. begin
  216. log(llError, SLogFailedToCreateManifest ,[AFPMakeFile, 'No packagemanager available']);
  217. end;
  218. end;
  219. ARepository.AddPackagesFromManifestFile(AManifestFile);
  220. for i := 0 to ARepository.PackageCount -1 do
  221. if ARepository.Packages[i].SourcePath = '' then
  222. ARepository.Packages[i].SourcePath := SR.Name;
  223. end
  224. end;
  225. until FindNext(SR)<>0;
  226. end;
  227. FindClose(SR);
  228. finally
  229. if Assigned(PackageManager) then
  230. PackageManager.RepositoryList.Remove(TempRepo);
  231. TempRepo.Free;
  232. TempPackagesStructure.Free;
  233. end;
  234. for i := 0 to ARepository.PackageCount -1 do
  235. ARepository.Packages[i].PackagesStructure := Self;
  236. Result:=true;
  237. end;
  238. function TFPUninstalledSourcesAvailablePackagesStructure.GetBuildPathDirectory(APackage: TFPPackage): string;
  239. begin
  240. if APackage.SourcePath<>'' then
  241. Result := Path+APackage.SourcePath
  242. else
  243. Result := Path+APackage.Name
  244. end;
  245. initialization
  246. TFPCustomPackagesStructure.RegisterPackagesStructureClass(TFPUninstalledSourcesAvailablePackagesStructure);
  247. TFPCustomPackagesStructure.RegisterPackagesStructureClass(TFPUninstalledSourcesPackagesStructure);
  248. end.