add support for DUB packages locations in custom folders, close #234

This commit is contained in:
Basile Burg 2018-05-04 14:09:15 +02:00
parent 2f86ab0459
commit a1d0f3d1e2
1 changed files with 122 additions and 52 deletions

View File

@ -234,6 +234,59 @@ const
dubCmd2PreMsg: array[TDubCommand] of string = ('compiling ', 'running ', 'testing '); dubCmd2PreMsg: array[TDubCommand] of string = ('compiling ', 'running ', 'testing ');
dubCmd2PostMsg: array[TDubCommand] of string = ('compiled', 'executed', 'tested'); dubCmd2PostMsg: array[TDubCommand] of string = ('compiled', 'executed', 'tested');
procedure getPackagesLocations(loc: TStringList);
var
p: string;
j: TJSONParser;
m: TMemoryStream;
a: TJSONArray;
o: TJSONObject = nil;
d: TJSONData;
r: TJSONData;
i: integer;
begin
{$IFDEF WINDOWS}
p := GetEnvironmentVariable('APPDATA') + '\dub\packages\';
{$ELSE}
p := GetEnvironmentVariable('HOME') + '/.dub/packages/';
{$ENDIF}
if p.dirExists then
loc.Add(p);
p += 'local-packages.json';
if not p.fileExists then
exit;
m := TMemoryStream.Create;
try
m.LoadFromFile(p);
j := TJSONParser.Create(m, [joIgnoreTrailingComma, joUTF8]);
try
r := j.Parse;
finally
j.Free;
end;
if r.JSONType = jtArray then
begin
a := TJSONArray(r);
for i := 0 to a.Count-1 do
begin
o := a.Objects[i];
if not o.findAny('path', d) then
continue;
p := d.AsString;
if (p.length <> 0) and (p[p.length] <> DirectorySeparator) then
p += DirectorySeparator;
if DirectoryExistsUTF8(p) then
loc.Add(p);
end;
end;
finally
m.Free;
if r.isNotNil then
r.Free;
end;
end;
{$REGION TDubLocalPackages -----------------------------------------------------} {$REGION TDubLocalPackages -----------------------------------------------------}
constructor TDubLocalPackage.create; constructor TDubLocalPackage.create;
begin begin
@ -320,62 +373,67 @@ end;
procedure TDubLocalPackages.update; procedure TDubLocalPackages.update;
var var
p: TStringList; p: TStringList;
r: TStringList;
s: string; s: string;
n: string; n: string;
v: string = ''; v: string = '';
i: integer; i: integer;
j: integer = 0; j: integer = 0;
k: integer;
d: PDubLocalPackage = nil; d: PDubLocalPackage = nil;
h: TStringRange = (ptr: nil; pos: 0; len: 0); h: TStringRange = (ptr: nil; pos: 0; len: 0);
begin begin
setLength(fLocalPackages, 0); setLength(fLocalPackages, 0);
{$IFDEF WINDOWS} r := TStringList.Create;
fRoot := GetEnvironmentVariable('APPDATA') + '\dub\packages\'; getPackagesLocations(r);
{$ELSE}
fRoot := GetEnvironmentVariable('HOME') + '/.dub/packages/';
{$ENDIF}
if not fRoot.dirExists then
exit;
p := TStringList.Create; try for k := 0 to r.Count -1 do
try begin
listFolders(p, fRoot); fRoot := r[k];
for i := 0 to p.Count-1 do p := TStringList.Create;
begin try
j := 0; listFolders(p, fRoot);
s := p[i]; for i := 0 to p.Count-1 do
h.init(s);
while true do
begin begin
h.popUntil('-'); j := 0;
if h.empty then s := p[i];
break; h.init(s);
if (h.popFront^.front in ['0'..'9']) or while true do
h.endsWith('master') then
begin begin
j := h.position; h.popUntil('-');
break; if h.empty then
break;
if (h.popFront^.front in ['0'..'9']) or
h.endsWith('master') then
begin
j := h.position;
break;
end;
end; end;
end; if (j = 0) then
if (j = 0) then continue;
continue;
n := s[1..j-1]; n := s[1..j-1];
n := n.extractFileName; n := n.extractFileName;
if not find(n, d) then if not find(n, d) then
begin begin
setLength(fLocalPackages, length(fLocalPackages) + 1); setLength(fLocalPackages, length(fLocalPackages) + 1);
fLocalPackages[high(fLocalPackages)] := TDubLocalPackage.create; fLocalPackages[high(fLocalPackages)] := TDubLocalPackage.create;
d := @fLocalPackages[high(fLocalPackages)]; d := @fLocalPackages[high(fLocalPackages)];
d^.name := n; d^.name := n;
end; end;
v := 'v' + s[j+1 .. length(s)]; v := 'v' + s[j+1 .. length(s)];
d^.addVersion(v); d^.addVersion(v);
end;
finally
p.Free;
end; end;
finally
p.Free;
end; end;
finally
r.Free;
end;
end; end;
function TDubLocalPackages.find(const name: string; out package: PDubLocalPackage): boolean; function TDubLocalPackages.find(const name: string; out package: PDubLocalPackage): boolean;
@ -1301,21 +1359,21 @@ procedure TCEDubProject.updateImportPathsFromJson;
v: string; v: string;
n: string; n: string;
o: string; o: string;
z: string;
r: TStringRange = (ptr: nil; pos: 0; len: 0); r: TStringRange = (ptr: nil; pos: 0; len: 0);
q: TSemVer; q: TSemVer;
u: PSemVer; u: PSemVer;
i: integer; i: integer;
k: integer;
c: TJSONObject; c: TJSONObject;
b: TStringList;
begin begin
if obj.findObject('dependencies' + suffix, deps) then if obj.findObject('dependencies' + suffix, deps) then
begin begin
{$IFDEF WINDOWS}
z := GetEnvironmentVariable('APPDATA') + '\dub\packages\'; b := TStringList.Create;
{$ELSE} getPackagesLocations(b);
z := GetEnvironmentVariable('HOME') + '/.dub/packages/';
{$ENDIF} try for i := 0 to deps.Count-1 do
for i := 0 to deps.Count-1 do
begin begin
n := deps.Names[i]; n := deps.Names[i];
@ -1330,7 +1388,6 @@ procedure TCEDubProject.updateImportPathsFromJson;
continue; continue;
end; end;
s := z + n;
// Try to fetch if not present at all // Try to fetch if not present at all
if not fLocalPackages.find(n, pck) and dubBuildOptions.autoFetch then if not fLocalPackages.find(n, pck) and dubBuildOptions.autoFetch then
begin begin
@ -1399,14 +1456,27 @@ procedure TCEDubProject.updateImportPathsFromJson;
// Set the imports, used in particular by DCD // Set the imports, used in particular by DCD
if assigned(u) then if assigned(u) then
begin begin
p := s + '-' + u^.asString + DirectorySeparator + n + DirectorySeparator; for k := 0 to b.Count-1 do
if (p + 'source').dirExists then begin
fImportPaths.Add(p + 'source') s := b[k] + n;
else if (p + 'src').dirExists then p := s + '-' + u^.asString + DirectorySeparator + n + DirectorySeparator;
fImportPaths.Add(p + 'src'); if (p + 'source').dirExists then
begin
fImportPaths.Add(p + 'source') ;
break;
end
else if (p + 'src').dirExists then
begin
fImportPaths.Add(p + 'src');
break;
end;
end;
end; end;
end; end;
end; end;
finally
b.Free;
end;
end; end;
end; end;