Skip to content

Commit 45e0c1d

Browse files
authored
Merge pull request #20 from advanced-security/jsinglet/repo-library-issue
exception for installing packs
2 parents 6f4697d + c564763 commit 45e0c1d

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

src/CodeQLToolkit.Features/Query/Commands/Targets/InstallQueryPacksCommandTarget.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CodeQLToolkit.Shared.CodeQL;
2+
using CodeQLToolkit.Shared.Utils;
23
using Microsoft.VisualBasic;
34
using System;
45
using System.Collections.Generic;
@@ -26,7 +27,35 @@ public override void Run()
2627

2728
installation.EnableCustomCodeQLBundles = UseBundle;
2829

30+
//
2931
installation.IsInstalledOrDie();
32+
//
33+
34+
35+
// filter the packs that are part of a custom bundle if we are using bundles.
36+
if(UseBundle)
37+
{
38+
// load the config
39+
var config = QLTConfig.LoadFromFile(Base);
40+
41+
Log<InstallQueryPacksCommandTarget>.G().LogInformation("In bundle mode so filtering bundled packs...");
42+
43+
44+
foreach (var pack in config.ExportedCustomizationPacks)
45+
{
46+
Log<InstallQueryPacksCommandTarget>.G().LogInformation($"Pack {pack} will NOT installed because it is part of the bundle...");
47+
}
48+
49+
files = files.Where(f => !config.ExportedCustomizationPacks.Any(p => CodeQLPackReader.read(f).Name == p)).ToArray();
50+
51+
Log<InstallQueryPacksCommandTarget>.G().LogInformation($"Got {files.Length} packs after filtering...");
52+
53+
foreach (var file in files)
54+
{
55+
Log<InstallQueryPacksCommandTarget>.G().LogInformation($"Pack {CodeQLPackReader.read(file).Name} in {file} will installed because it is not part of the bundle...");
56+
}
57+
}
58+
3059

3160
foreach ( string file in files )
3261
{

src/CodeQLToolkit.Shared/CodeQLToolkit.Shared.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.2" />
1414
<PackageReference Include="Scriban" Version="5.7.0" />
1515
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
16+
<PackageReference Include="YamlDotNet" Version="15.1.2" />
1617
</ItemGroup>
1718

1819
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using YamlDotNet.RepresentationModel;
7+
8+
namespace CodeQLToolkit.Shared.Utils
9+
{
10+
public class CodeQLPack
11+
{
12+
public string Name { get; set; }
13+
}
14+
public class CodeQLPackReader
15+
{
16+
public static CodeQLPack read(string path)
17+
{
18+
var pack = new CodeQLPack();
19+
20+
using (var reader = new StreamReader(path))
21+
{
22+
var yaml = new YamlStream();
23+
yaml.Load(reader);
24+
25+
var root = (YamlMappingNode)yaml.Documents[0].RootNode;
26+
27+
foreach (var e in root.Children)
28+
{
29+
if(e.Key.ToString() == "name")
30+
{
31+
pack.Name = e.Value.ToString();
32+
}
33+
}
34+
35+
36+
37+
}
38+
39+
return pack;
40+
}
41+
}
42+
}

src/CodeQLToolkit.Shared/Utils/QLTConfig.cs

+16
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,21 @@ public void ToFile()
5252
var data = JsonConvert.SerializeObject(this, Formatting.Indented);
5353
File.WriteAllText(CodeQLConfigFilePath, data);
5454
}
55+
56+
public static QLTConfig? LoadFromFile(string baseDir)
57+
{
58+
var config = new QLTConfig()
59+
{
60+
Base = baseDir
61+
};
62+
63+
64+
if (File.Exists(config.CodeQLConfigFilePath))
65+
{
66+
return config.FromFile();
67+
}
68+
69+
return null;
70+
}
5571
}
5672
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CodeQLToolkit.Shared.Tests.Utils
8+
{
9+
public class PackReaderTests
10+
{
11+
public string TestFile { get; set; }
12+
[SetUp]
13+
public void Setup()
14+
{
15+
var doc = @"
16+
---
17+
library: true
18+
name: qlt2/stuff2
19+
version: 0.0.1
20+
description: Default description
21+
suites:
22+
license:
23+
dependencies:
24+
codeql/cpp-all: ""0.12.2""
25+
";
26+
27+
TestFile = Path.Combine(Path.GetTempPath(), "qlpack.yml");
28+
29+
File.WriteAllText(TestFile, doc);
30+
}
31+
32+
[Test]
33+
public void TestReadPackName()
34+
{
35+
Assert.AreEqual("qlt2/stuff2", CodeQLPackReader.read(TestFile).Name);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)