pkgglobals.pp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. {$mode objfpc}
  2. {$h+}
  3. unit pkgglobals;
  4. interface
  5. uses
  6. SysUtils,
  7. Classes;
  8. Const
  9. {$ifdef unix}
  10. ExeExt = '';
  11. AllFiles='*';
  12. {$else unix}
  13. ExeExt = '.exe';
  14. AllFiles='*.*';
  15. {$endif unix}
  16. Type
  17. TVerbosity = (vError,vWarning,vInfo,vCommands,vDebug);
  18. TVerbosities = Set of TVerbosity;
  19. EPackagerError = class(Exception);
  20. // Logging
  21. Function StringToVerbosity (S : String) : TVerbosity;
  22. Function VerbosityToString (V : TVerbosity): String;
  23. Procedure Log(Level: TVerbosity;Msg : String);
  24. Procedure Log(Level: TVerbosity;Fmt : String; const Args : array of const);
  25. Procedure Error(Msg : String);
  26. Procedure Error(Fmt : String; const Args : array of const);
  27. // Utils
  28. function maybequoted(const s:string):string;
  29. Function FixPath(const S : String) : string;
  30. Procedure DeleteDir(const ADir:string);
  31. Procedure SearchFiles(SL:TStringList;const APattern:string);
  32. Function GetCompilerInfo(const ACompiler,AOptions:string):string;
  33. var
  34. Verbosity : TVerbosities;
  35. Implementation
  36. uses
  37. typinfo,
  38. process,
  39. contnrs,
  40. uriparser,
  41. pkgmessages;
  42. function StringToVerbosity(S: String): TVerbosity;
  43. Var
  44. I : integer;
  45. begin
  46. I:=GetEnumValue(TypeInfo(TVerbosity),'v'+S);
  47. If (I<>-1) then
  48. Result:=TVerbosity(I)
  49. else
  50. Raise EPackagerError.CreateFmt(SErrInvalidVerbosity,[S]);
  51. end;
  52. Function VerbosityToString (V : TVerbosity): String;
  53. begin
  54. Result:=GetEnumName(TypeInfo(TVerbosity),Integer(V));
  55. Delete(Result,1,1);// Delete 'v'
  56. end;
  57. procedure Log(Level:TVerbosity;Msg: String);
  58. var
  59. Prefix : string;
  60. begin
  61. if not(Level in Verbosity) then
  62. exit;
  63. Prefix:='';
  64. if Level=vWarning then
  65. Prefix:=SWarning;
  66. Writeln(stdErr,Prefix,Msg);
  67. end;
  68. Procedure Log(Level:TVerbosity; Fmt:String; const Args:array of const);
  69. begin
  70. Log(Level,Format(Fmt,Args));
  71. end;
  72. procedure Error(Msg: String);
  73. begin
  74. Raise EPackagerError.Create(Msg);
  75. end;
  76. procedure Error(Fmt: String; const Args: array of const);
  77. begin
  78. Raise EPackagerError.CreateFmt(Fmt,Args);
  79. end;
  80. function maybequoted(const s:string):string;
  81. const
  82. {$IFDEF MSWINDOWS}
  83. FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  84. '{', '}', '''', '`', '~'];
  85. {$ELSE}
  86. FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  87. '{', '}', '''', ':', '\', '`', '~'];
  88. {$ENDIF}
  89. var
  90. s1 : string;
  91. i : integer;
  92. quoted : boolean;
  93. begin
  94. quoted:=false;
  95. s1:='"';
  96. for i:=1 to length(s) do
  97. begin
  98. case s[i] of
  99. '"' :
  100. begin
  101. quoted:=true;
  102. s1:=s1+'\"';
  103. end;
  104. ' ',
  105. #128..#255 :
  106. begin
  107. quoted:=true;
  108. s1:=s1+s[i];
  109. end;
  110. else begin
  111. if s[i] in FORBIDDEN_CHARS then
  112. quoted:=True;
  113. s1:=s1+s[i];
  114. end;
  115. end;
  116. end;
  117. if quoted then
  118. maybequoted:=s1+'"'
  119. else
  120. maybequoted:=s;
  121. end;
  122. Function FixPath(const S : String) : string;
  123. begin
  124. If (S<>'') then
  125. Result:=IncludeTrailingPathDelimiter(S)
  126. else
  127. Result:='';
  128. end;
  129. Procedure DeleteDir(const ADir:string);
  130. var
  131. Info : TSearchRec;
  132. begin
  133. if FindFirst(ADir+PathDelim+AllFiles,faAnyFile, Info)=0 then
  134. try
  135. repeat
  136. if (Info.Attr and faDirectory)=faDirectory then
  137. begin
  138. if (Info.Name<>'.') and (Info.Name<>'..') then
  139. DeleteDir(ADir+PathDelim+Info.Name)
  140. end
  141. else
  142. DeleteFile(ADir+PathDelim+Info.Name);
  143. until FindNext(Info)<>0;
  144. finally
  145. FindClose(Info);
  146. end;
  147. end;
  148. Procedure SearchFiles(SL:TStringList;const APattern:string);
  149. var
  150. Info : TSearchRec;
  151. ADir : string;
  152. begin
  153. ADir:=ExtractFilePath(APattern);
  154. if FindFirst(APattern,faAnyFile, Info)=0 then
  155. try
  156. repeat
  157. if (Info.Attr and faDirectory)=faDirectory then
  158. begin
  159. if (Info.Name<>'.') and (Info.Name<>'..') then
  160. SearchFiles(SL,ADir+Info.Name+PathDelim+ExtractFileName(APattern))
  161. end;
  162. SL.Add(ADir+Info.Name);
  163. until FindNext(Info)<>0;
  164. finally
  165. FindClose(Info);
  166. end;
  167. end;
  168. Function GetCompilerInfo(const ACompiler,AOptions:string):string;
  169. Const
  170. BUFSIZE=1024;
  171. Var
  172. S : TProcess;
  173. Buf : Array[0..BUFSIZE-1] of char;
  174. Count : longint;
  175. begin
  176. S:=TProcess.Create(Nil);
  177. S.Commandline:=ACOmpiler+' '+AOptions;
  178. S.Options:=[poUsePipes,poNoConsole];
  179. S.execute;
  180. Count:=s.output.read(buf,BufSize);
  181. SetLength(Result,Count);
  182. Move(Buf,Result[1],Count);
  183. S.Free;
  184. end;
  185. end.