refact dub project interpreter with json helpers

This commit is contained in:
Basile Burg 2016-11-29 00:34:25 +01:00
parent e36e61b683
commit 00761b740e
No known key found for this signature in database
GPG Key ID: 1868039F415CB8CF
1 changed files with 57 additions and 107 deletions

View File

@ -699,30 +699,25 @@ end;
{$REGION JSON to internal fields -----------------------------------------------} {$REGION JSON to internal fields -----------------------------------------------}
function TCEDubProject.getCurrentCustomConfig: TJSONObject; function TCEDubProject.getCurrentCustomConfig: TJSONObject;
var var
item: TJSONData;
confs: TJSONArray; confs: TJSONArray;
begin begin
result := nil; result := nil;
if fConfigIx = 0 then exit; if fConfigIx = 0 then
// exit;
item := fJSON.Find('configurations'); if fJSON.findArray('configurations', confs) and (fConfigIx < confs.Count) then
if item.isNil then exit; result := confs.Objects[fConfigIx];
//
confs := TJSONArray(item);
if fConfigIx > confs.Count -1 then exit;
//
result := confs.Objects[fConfigIx];
end; end;
procedure TCEDubProject.updatePackageNameFromJson; procedure TCEDubProject.updatePackageNameFromJson;
var var
value: TJSONData; value: TJSONData;
begin begin
if not assigned(fJSON) then if fJSON.isNil then
exit; exit;
value := fJSON.Find('name'); if not fJSON.findAny('name', value) then
if value.isNil then fPackageName := '' fPackageName := ''
else fPackageName := value.AsString; else
fPackageName := value.AsString;
end; end;
procedure TCEDubProject.udpateConfigsFromJson; procedure TCEDubProject.udpateConfigsFromJson;
@ -740,15 +735,13 @@ begin
exit; exit;
// the CE interface for dub doesn't make the difference between build type // the CE interface for dub doesn't make the difference between build type
//and config, instead each possible combination type + build is generated. //and config, instead each possible combination type + build is generated.
if fJSON.Find('configurations') <> nil then if fJSON.findArray('configurations', arr) then
begin begin
arr := fJSON.Arrays['configurations'];
for i:= 0 to arr.Count-1 do for i:= 0 to arr.Count-1 do
begin begin
item := TJSONObject(arr.Items[i]); item := TJSONObject(arr.Items[i]);
if item.Find('name').isNil then if item.findAny('name', dat) then
continue; fConfigs.Add(dat.AsString);
fConfigs.Add(item.Strings['name']);
end; end;
end else end else
begin begin
@ -758,19 +751,15 @@ begin
end; end;
fBuildTypes.AddStrings(DubBuiltTypeName); fBuildTypes.AddStrings(DubBuiltTypeName);
dat := fJSON.Find('buildTypes'); if fJSON.findObject('buildTypes', obj) then for i := 0 to obj.Count-1 do
if dat.isNotNil and (dat.JSONType = jtObject) then
begin begin
obj := fJSON.Objects['buildTypes']; itemname := obj.Names[i];
for i := 0 to obj.Count-1 do // defaults build types can be overridden
begin if fBuildTypes.IndexOf(itemname) <> -1 then
itemname := obj.Names[i]; continue;
// defaults build types can be overridden fBuildTypes.Add(itemname);
if fBuildTypes.IndexOf(itemname) <> -1 then
continue;
fBuildTypes.Add(itemname);
end;
end; end;
deleteDups(fConfigs); deleteDups(fConfigs);
deleteDups(fBuildTypes); deleteDups(fBuildTypes);
fConfigsCount := fConfigs.Count * fBuildTypes.Count; fConfigsCount := fConfigs.Count * fBuildTypes.Count;
@ -787,13 +776,9 @@ procedure getExclusion(from: TJSONObject);
var var
i: integer; i: integer;
begin begin
item := from.Find('excludedSourceFiles'); if from.findArray('excludedSourceFiles', arr) then
if item.isNotNil and (item.JSONType = jtArray) then
begin
arr := TJSONArray(item);
for i := 0 to arr.Count-1 do for i := 0 to arr.Count-1 do
lst.Add(patchPlateformPath(arr.Strings[i])); lst.Add(patchPlateformPath(arr.Strings[i]));
end;
end; end;
procedure tryAddRelOrAbsFile(const fname: string); procedure tryAddRelOrAbsFile(const fname: string);
begin begin
@ -829,17 +814,29 @@ begin
lst := TStringList.Create; lst := TStringList.Create;
try try
// auto folders & files // auto folders & files
item := fJSON.Find('mainSourceFile'); if fJSON.findAny('mainSourceFile', item) then
if item.isNotNil then
fSrcs.Add(patchPlateformPath(ExtractRelativepath(fBasePath, item.AsString))); fSrcs.Add(patchPlateformPath(ExtractRelativepath(fBasePath, item.AsString)));
tryAddFromFolder(fBasePath + 'src'); tryAddFromFolder(fBasePath + 'src');
tryAddFromFolder(fBasePath + 'source'); tryAddFromFolder(fBasePath + 'source');
// custom folders // custom folders
item := fJSON.Find('sourcePaths'); if fJSON.findArray('sourcePaths', arr) then for i := 0 to arr.Count-1 do
if item.isNotNil then
begin begin
arr := TJSONArray(item); pth := TrimRightSet(arr.Strings[i], ['/','\']);
for i := 0 to arr.Count-1 do if pth.dirExists and FilenameIsAbsolute(pth) then
tryAddFromFolder(pth)
else
tryAddFromFolder(expandFilenameEx(fBasePath, pth));
end;
// custom files
if fJSON.findArray('sourceFiles', arr) then for i := 0 to arr.Count-1 do
tryAddRelOrAbsFile(arr.Strings[i]);
conf := getCurrentCustomConfig;
if conf.isNotNil then
begin
if conf.findAny('mainSourceFile', item) then
fSrcs.Add(patchPlateformPath(ExtractRelativepath(fBasePath, item.AsString)));
// custom folders in current config
if conf.findArray('sourcePaths', arr) then for i := 0 to arr.Count-1 do
begin begin
pth := TrimRightSet(arr.Strings[i], ['/','\']); pth := TrimRightSet(arr.Strings[i], ['/','\']);
if pth.dirExists and FilenameIsAbsolute(pth) then if pth.dirExists and FilenameIsAbsolute(pth) then
@ -847,43 +844,9 @@ begin
else else
tryAddFromFolder(expandFilenameEx(fBasePath, pth)); tryAddFromFolder(expandFilenameEx(fBasePath, pth));
end; end;
end;
// custom files
item := fJSON.Find('sourceFiles');
if item.isNotNil then
begin
arr := TJSONArray(item);
for i := 0 to arr.Count-1 do
tryAddRelOrAbsFile(arr.Strings[i]);
end;
conf := getCurrentCustomConfig;
if conf.isNotNil then
begin
item := conf.Find('mainSourceFile');
if item.isNotNil then
fSrcs.Add(patchPlateformPath(ExtractRelativepath(fBasePath, item.AsString)));
// custom folders in current config
item := conf.Find('sourcePaths');
if item.isNotNil then
begin
arr := TJSONArray(item);
for i := 0 to arr.Count-1 do
begin
pth := TrimRightSet(arr.Strings[i], ['/','\']);
if pth.dirExists and FilenameIsAbsolute(pth) then
tryAddFromFolder(pth)
else
tryAddFromFolder(expandFilenameEx(fBasePath, pth));
end;
end;
// custom files in current config // custom files in current config
item := conf.Find('sourceFiles'); if conf.findArray('sourceFiles', arr) then for i := 0 to arr.Count-1 do
if item.isNotNil then tryAddRelOrAbsFile(arr.Strings[i]);
begin
arr := TJSONArray(item);
for i := 0 to arr.Count-1 do
tryAddRelOrAbsFile(arr.Strings[i]);
end;
end; end;
// exclusions // exclusions
lst.Clear; lst.Clear;
@ -923,12 +886,8 @@ var
begin begin
result := true; result := true;
if value.Find('mainSourceFile').isNotNil then if value.Find('mainSourceFile').isNotNil then
begin fBinKind := executable
fBinKind := executable; else if value.findAny('targetType', tt) then
exit;
end;
tt := value.Find('targetType');
if tt.isNotNil then
begin begin
case tt.AsString of case tt.AsString of
'executable': fBinKind := executable; 'executable': fBinKind := executable;
@ -937,7 +896,8 @@ begin
'autodetect': result := false; 'autodetect': result := false;
else fBinKind := executable; else fBinKind := executable;
end; end;
end else result := false; end
else result := false;
end; end;
procedure TCEDubProject.updateTargetKindFromJson; procedure TCEDubProject.updateTargetKindFromJson;
@ -975,22 +935,16 @@ procedure TCEDubProject.updateImportPathsFromJson;
procedure addFrom(obj: TJSONObject); procedure addFrom(obj: TJSONObject);
var var
arr: TJSONArray; arr: TJSONArray;
item: TJSONData;
pth: string; pth: string;
i: integer; i: integer;
begin begin
item := obj.Find('importPaths'); if obj.findArray('importPaths', arr) then for i := 0 to arr.Count-1 do
if assigned(item) then
begin begin
arr := TJSONArray(item); pth := TrimRightSet(arr.Strings[i], ['/','\']);
for i := 0 to arr.Count-1 do if pth.dirExists and FilenameIsAbsolute(pth) then
begin fImportPaths.Add(pth)
pth := TrimRightSet(arr.Strings[i], ['/','\']); else
if pth.dirExists and FilenameIsAbsolute(pth) then fImportPaths.Add(expandFilenameEx(fBasePath, pth));
fImportPaths.Add(pth)
else
fImportPaths.Add(expandFilenameEx(fBasePath, pth));
end;
end; end;
end; end;
// note: dependencies are added as import to allow DCD completion // note: dependencies are added as import to allow DCD completion
@ -999,20 +953,17 @@ procedure TCEDubProject.updateImportPathsFromJson;
var var
folds: TStringList; folds: TStringList;
deps: TJSONObject; deps: TJSONObject;
item: TJSONData;
pth: string; pth: string;
str: string; str: string;
i,j,k: integer; i,j,k: integer;
begin begin
item := obj.Find('dependencies'); if obj.findObject('dependencies', deps) then
if assigned(item) then
begin begin
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
pth := GetEnvironmentVariable('APPDATA') + '\dub\packages\'; pth := GetEnvironmentVariable('APPDATA') + '\dub\packages\';
{$ELSE} {$ELSE}
pth := GetEnvironmentVariable('HOME') + '/.dub/packages/'; pth := GetEnvironmentVariable('HOME') + '/.dub/packages/';
{$ENDIF} {$ENDIF}
deps := TJSONObject(item);
folds := TStringList.Create; folds := TStringList.Create;
listFolders(folds, pth); listFolders(folds, pth);
try try
@ -1068,16 +1019,15 @@ var
var var
n,p: TJSONData; n,p: TJSONData;
begin begin
p := obj.Find('targetPath'); if obj.findAny('targetPath', p) then
n := obj.Find('targetName'); pathPart := p.AsString;
if p.isNotNil then pathPart := p.AsString; if obj.FindAny('targetName', n) then
if n.isNotNil then namePart := n.AsString; namePart := n.AsString;
end; end;
begin begin
fOutputFileName := ''; fOutputFileName := '';
if fJSON.isNil then exit; if fJSON.isNil or not fJSON.findAny('name', item) then
item := fJSON.Find('name'); exit;
if item.isNil then exit;
namePart := item.AsString; namePart := item.AsString;
pathPart := fBasePath; pathPart := fBasePath;