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,23 +373,23 @@ 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;
try for k := 0 to r.Count -1 do
begin
fRoot := r[k];
p := TStringList.Create; p := TStringList.Create;
try try
listFolders(p, fRoot); listFolders(p, fRoot);
@ -377,6 +430,11 @@ begin
p.Free; p.Free;
end; end;
end; end;
finally
r.Free;
end;
end;
function TDubLocalPackages.find(const name: string; out package: PDubLocalPackage): boolean; function TDubLocalPackages.find(const name: string; out package: PDubLocalPackage): boolean;
var var
@ -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,16 +1456,29 @@ 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
for k := 0 to b.Count-1 do
begin
s := b[k] + n;
p := s + '-' + u^.asString + DirectorySeparator + n + DirectorySeparator; p := s + '-' + u^.asString + DirectorySeparator + n + DirectorySeparator;
if (p + 'source').dirExists then if (p + 'source').dirExists then
fImportPaths.Add(p + 'source') begin
fImportPaths.Add(p + 'source') ;
break;
end
else if (p + 'src').dirExists then else if (p + 'src').dirExists then
begin
fImportPaths.Add(p + 'src'); fImportPaths.Add(p + 'src');
break;
end; end;
end; end;
end; end;
end; end;
end; end;
finally
b.Free;
end;
end;
end;
var var
conf: TJSONObject; conf: TJSONObject;