Advertisement
mixster

mixster

Feb 13th, 2010
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.88 KB | None | 0 0
  1. const
  2.   CheckVersion = False;
  3.   MoveSRLPlugins = False;
  4.   SRLDirectory = 'Includes\SRL\';
  5.   Linux = False;
  6.  
  7. var
  8.   SCARDirectory: string;
  9.  
  10. type
  11.   TFile = record
  12.     path, filename: string;
  13.   end;
  14.  
  15.   TSVNFile = record
  16.     local, remote: TFile;
  17.   end;
  18.  
  19.   TMatch = record
  20.     start, finish: string;
  21.   end;
  22.  
  23.   TFilter = record
  24.     exp, dir, fil: string;
  25.     local, remote: TMatch;
  26.   end;
  27.  
  28.   TSVN = record
  29.     name: string;
  30.     skip: TStringArray;
  31.     baseloc: TSVNFile;
  32.     files: array of TSVNFile;
  33.     filter: TFilter;
  34.     rev: TMatch;
  35.     col: Boolean;
  36.   end;
  37.  
  38. function Explode(str, del: string): TStringArray;
  39. var
  40.   i, l, dL: Integer;
  41. begin
  42.   i := 0;
  43.   l := -1;
  44.   SetLength(Result, 0);
  45.   if (str = '') then
  46.     Exit;
  47.   dL := Length(del) - 1;
  48.   repeat
  49.     Inc(l);
  50.     SetLength(Result, l + 1);
  51.     i := Pos(del, str);
  52.     if i <= 0 then
  53.       Break;
  54.     Result[l] := Copy(str, 1, i - 1);
  55.     Delete(str, 1, i + dL);
  56.   until false
  57.   Result[l] := Copy(str, 1, Length(str));
  58. end;
  59.  
  60. function ReturnPage(svn: TSVN; dir: TSVNFile): string;
  61. var
  62.   url: string;
  63. begin
  64.   url := svn.baseloc.remote.path + dir.remote.path;
  65.  
  66.   try
  67.     Result := GetPage(url);
  68.   except
  69.     Writeln('Downloading url "' + url + '" failed');
  70.     Result := '';
  71.   end;
  72. end;
  73.  
  74. procedure RawCollectSVN(var svn: TSVN; cur_dir: TSVNFile);
  75. var
  76.   input, tmpLoc, tmpRem: string;
  77.   split: TStringArray;
  78.   h, i: Integer;
  79.   new_dir: TSVNFile;
  80.   sk: Boolean;
  81. begin
  82.   input := ReturnPage(svn, cur_dir);
  83.   split := Explode(input, svn.filter.exp);
  84.  
  85.   for i := 0 to High(split) do
  86.   begin
  87.     for h := 0 to High(svn.skip) do
  88.     begin
  89.       sk := Pos(svn.skip[h], split[i]) > 0;
  90.       if sk then
  91.         Break;
  92.     end;
  93.  
  94.     if sk then
  95.       Continue;
  96.  
  97.     tmpLoc := Between(svn.filter.local.start, svn.filter.local.finish, split[i]);
  98.     tmpRem := Between(svn.filter.remote.start, svn.filter.remote.finish, split[i]);
  99.  
  100.     if (tmpLoc = '') or (tmpRem = '') then
  101.       Continue;
  102.  
  103.     if Pos(svn.filter.dir, split[i]) > 0 then // Handle directory
  104.     begin
  105.       new_dir.local.path := cur_dir.local.path + tmpLoc + '\';
  106.       new_dir.remote.path := cur_dir.remote.path + tmpRem;
  107.       Writeln('Discovered directory "' + cur_dir.local.path + tmpLoc + '"');
  108.       RawCollectSVN(svn, new_dir);
  109.     end
  110.     else if Pos(svn.filter.fil, split[i]) > 0 then // Handle file
  111.     begin;
  112.       h := Length(svn.files);
  113.       SetLength(svn.files, h + 1);
  114.       svn.files[h].local.path := cur_dir.local.path;
  115.       svn.files[h].local.filename := tmpLoc;
  116.       svn.files[h].remote.path := cur_dir.remote.path;
  117.       svn.files[h].remote.filename := tmpRem;
  118.     end;
  119.   end;
  120. end;
  121.  
  122. procedure CollectSVN(var svn: TSVN);
  123. var
  124.   start: TSVNFile;
  125.   t: Integer;
  126. begin
  127.   Writeln('Now starting to collect "' + svn.name + '"');
  128.   t := GetSystemTime;
  129.   RawCollectSVN(svn, start);
  130.   svn.col := True;
  131.   t := GetSystemTime - t;
  132.   Writeln('Finished collecting "' + svn.name + '" in ' + IntToStr(t) + ' ms');
  133.   Writeln('A total of ' + IntToStr(Length(svn.files)) + ' files were collected');
  134. end;
  135.  
  136. procedure DownloadSVN(svn: TSVN);
  137. var
  138.   i, f, l, t: Integer;
  139.   s: string;
  140.   opt: TReplaceFlags;
  141. begin
  142.   Writeln('Now starting to download "' + svn.name + '"');
  143.   Writeln('');
  144.  
  145.   opt := [rfReplaceAll];
  146.  
  147.   t := GetSystemTime;
  148.   l := Length(svn.files);
  149.   for i := 0 to l - 1 do
  150.     try
  151.       s := svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename;
  152.  
  153.       if (Linux) then
  154.         s := Replace(s, '\', '/', opt);
  155.  
  156.       f := RewriteFile(s, False);
  157.       if (f = -1) then
  158.       begin
  159.         Writeln('Error writing ''' + s + '''');
  160.         Continue;
  161.       end
  162.       else
  163.         Writeln(s);
  164.  
  165.       WriteFileString(f, GetPage(svn.baseloc.remote.path + svn.files[i].remote.path + svn.files[i].remote.filename));
  166.       CloseFile(f);
  167.     except
  168.       Writeln('Failed to download file.');
  169.     end;
  170.   t := GetSystemTime - t;
  171.   Writeln('Finished downloading "' + svn.name + '" in ' + IntToStr(t) + ' ms');
  172. end;
  173.  
  174. function ReturnSRLSVN: TSVN;
  175. begin
  176.   Result.name := 'SRL';
  177.   Result.baseloc.remote.path := 'http://www.villavu.com/repositories/srl-opendev/';
  178.   Result.baseloc.local.path := AppPath + SRLDirectory;
  179.   Result.filter.exp := '<';
  180.   Result.filter.dir := 'dir';
  181.   Result.filter.fil := 'file';
  182.   Result.filter.local.start := 'name="';
  183.   Result.filter.local.finish := '"';
  184.   Result.filter.remote.start := 'href="';
  185.   Result.filter.remote.finish := '"';
  186.   Result.rev.start := 'rev="';
  187.   Result.rev.finish := '"';
  188.   Result.skip := [];
  189. end;
  190.  
  191. function GetSVNVersion(svn: TSVN): Integer;
  192. var
  193.   input: string;
  194. begin
  195.   input := GetPage(svn.baseloc.remote.path);
  196.   Result := StrToIntDef(Between(svn.rev.start, svn.rev.finish, input), 0);
  197. end;
  198.  
  199. procedure WriteSVNVersion(svn: TSVN);
  200. var
  201.   rev: Integer;
  202. begin
  203.   rev := GetSVNVersion(svn);
  204.   if rev = 0 then
  205.   begin
  206.     Writeln('Collecting rev number failed');
  207.     Writeln('You can manually set svn_settings.ini if this results in a problem');
  208.     exit;
  209.   end;
  210.  
  211.   WriteINI(svn.name, 'REVISION', IntToStr(rev), SCARDirectory + 'svn_settings.ini');
  212. end;
  213.  
  214. function IsSVNOld(svn: TSVN): Boolean;
  215. var
  216.   curRev, locRev: Integer;
  217. begin
  218.   locRev := StrToIntDef(ReadINI(svn.name, 'REVISION', AppPath + 'svn_settings.ini'), 0);
  219.   curRev := GetSVNVersion(svn);
  220.   if curRev = 0 then
  221.   begin
  222.     Writeln('Collecting current rev number failed');
  223.     Writeln('Assuming newer SVN is on local system to avoid problems');
  224.     Result := False;
  225.     Exit;
  226.   end;
  227.  
  228.   Result := locRev < curRev;
  229.  
  230.   if Result then
  231.     Writeln(svn.name + ' has the latest rev downloaded')
  232.   else
  233.     Writeln(svn.name + ' is rev ' + IntToStr(locRev) + ' and latest is ' + IntToStr(curRev));
  234. end;
  235.  
  236. procedure MovePlugins(svn: TSVN; dest: string);
  237. var
  238.   i, f: Integer;
  239.   s: string;
  240.   o: Boolean;
  241. begin
  242.   for i := 0 to High(svn.files) do
  243.     if Pos('.dll', svn.files[i].local.filename) > 0 then
  244.       try
  245.         Writeln('Found plugin "' + svn.files[i].local.filename);
  246.         o := False;
  247.         f := OpenFile(svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename, False);
  248.         ReadFileString(f, s, FileSize(f));
  249.         CloseFile(f);
  250.         o := True;
  251.         f := RewriteFile(dest + svn.files[i].local.filename, False);
  252.         WriteFileString(f, s);
  253.         CloseFile(f);
  254.       except
  255.         Writeln('Failed to save plugin.');
  256.         if o then
  257.           Writeln('Problem involved rewriting the file at target folder')
  258.         else
  259.           Writeln('Problem involved opening the sauce file to read');
  260.       end;
  261. end;
  262.  
  263. var
  264.   srl: TSVN;
  265.  
  266. begin
  267.   Writeln('Begin');
  268.  
  269.   srl := ReturnSRLSVN;
  270.  
  271.   if (not CheckVersion) or (CheckVersion and IsSVNOld(srl)) then
  272.   begin
  273.     CollectSVN(srl);
  274.     DownloadSVN(srl);
  275.  
  276.     if MoveSRLPlugins then
  277.       MovePlugins(srl, AppPath + 'Plugins\');
  278.   end;
  279.  
  280.   Writeln('End');
  281. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement