|
@@ -0,0 +1,144 @@
|
|
1
|
+module backporter.packagefinder;
|
|
2
|
+
|
|
3
|
+import std.algorithm, std.array;
|
|
4
|
+import backporter.git;
|
|
5
|
+import std.stdio;
|
|
6
|
+import std.file;
|
|
7
|
+import std.json;
|
|
8
|
+import std.path;
|
|
9
|
+import std.experimental.logger;
|
|
10
|
+
|
|
11
|
+class Package
|
|
12
|
+{
|
|
13
|
+ string name;
|
|
14
|
+ string gitUrl;
|
|
15
|
+ PackageRevision[] revisions;
|
|
16
|
+
|
|
17
|
+ this(JSONValue value)
|
|
18
|
+ {
|
|
19
|
+ name = value["name"].str;
|
|
20
|
+ auto repo = value["repository"];
|
|
21
|
+ switch (repo["kind"].str)
|
|
22
|
+ {
|
|
23
|
+ case "github":
|
|
24
|
+ gitUrl = "https://github.com/" ~ repo["owner"].str ~ "/" ~ repo["project"].str;
|
|
25
|
+ break;
|
|
26
|
+ default:
|
|
27
|
+ errorf("failed to parse repo info for project %s", name);
|
|
28
|
+ break;
|
|
29
|
+ }
|
|
30
|
+ foreach (revision; value["versions"].array)
|
|
31
|
+ {
|
|
32
|
+ if (!("version" in revision))
|
|
33
|
+ {
|
|
34
|
+ warningf("package %s contains a revision without a version name", name);
|
|
35
|
+ continue;
|
|
36
|
+ }
|
|
37
|
+ if (!("commitID" in revision))
|
|
38
|
+ {
|
|
39
|
+ warningf("package %s contains a revision without a commit id", name);
|
|
40
|
+ continue;
|
|
41
|
+ }
|
|
42
|
+ if (revision["version"].str[0] == '~') continue;
|
|
43
|
+ auto rev = new PackageRevision;
|
|
44
|
+ rev.pkg = this;
|
|
45
|
+ rev.commit = revision["commitID"].str;
|
|
46
|
+ rev.id = revision["version"].str;
|
|
47
|
+ rev.date = revision["date"].str;
|
|
48
|
+ revisions ~= rev;
|
|
49
|
+ }
|
|
50
|
+ import std.algorithm.sorting : sort;
|
|
51
|
+ import std.uni : icmp;
|
|
52
|
+ revisions.sort!((a, b) => a.date < b.date);
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ string checkoutDirectory()
|
|
56
|
+ {
|
|
57
|
+ return buildPath(dataDir, name);
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ bool ensureCloned()
|
|
61
|
+ {
|
|
62
|
+ import std.file : exists;
|
|
63
|
+ import backporter.git;
|
|
64
|
+ auto dir = checkoutDirectory;
|
|
65
|
+ if (dir.exists && chainPath(dir, ".git").exists) return true;
|
|
66
|
+ cd(dataDir);
|
|
67
|
+ return clone(gitUrl, name).status == 0;
|
|
68
|
+ }
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+class PackageRevision
|
|
72
|
+{
|
|
73
|
+ Package pkg;
|
|
74
|
+ string commit;
|
|
75
|
+ string id;
|
|
76
|
+ string date;
|
|
77
|
+
|
|
78
|
+ bool checkout()
|
|
79
|
+ {
|
|
80
|
+ if (!pkg.ensureCloned) return false;
|
|
81
|
+ cd(pkg.checkoutDirectory);
|
|
82
|
+ return reset("--hard", commit).status == 0;
|
|
83
|
+ }
|
|
84
|
+}
|
|
85
|
+
|
|
86
|
+JSONValue getPackageJson(string name)
|
|
87
|
+{
|
|
88
|
+ try
|
|
89
|
+ {
|
|
90
|
+ import std.net.curl : get;
|
|
91
|
+ infof("grabbing package %s", name);
|
|
92
|
+ auto js = get("https://code.dlang.org/api/packages/" ~ name ~ "/info").parseJSON;
|
|
93
|
+ return js;
|
|
94
|
+ }
|
|
95
|
+ catch (Exception e)
|
|
96
|
+ {
|
|
97
|
+ return JSONValue(null);
|
|
98
|
+ }
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+string[] packageNames()
|
|
102
|
+{
|
|
103
|
+ import std.net.curl : get;
|
|
104
|
+ infof("grabbing package list");
|
|
105
|
+ auto js = get("https://code.dlang.org/packages/index.json").parseJSON.array;
|
|
106
|
+ auto names = new string[js.length];
|
|
107
|
+ foreach (i, n; js)
|
|
108
|
+ {
|
|
109
|
+ names[i] = n.str;
|
|
110
|
+ }
|
|
111
|
+ names = names.sort.uniq.array;
|
|
112
|
+ infof("found %s packages", names.length);
|
|
113
|
+ return names;
|
|
114
|
+}
|
|
115
|
+
|
|
116
|
+Package[] grabAllFresh()
|
|
117
|
+{
|
|
118
|
+ auto names = packageNames;
|
|
119
|
+ auto packageJsons = names
|
|
120
|
+ .map!getPackageJson
|
|
121
|
+ .filter!(x => x.type == JSON_TYPE.OBJECT)
|
|
122
|
+ .array;
|
|
123
|
+ infof("downloading packages fresh");
|
|
124
|
+ JSONValue v;
|
|
125
|
+ v["packages"] = packageJsons;
|
|
126
|
+ auto f = File("packagelist.json", "w");
|
|
127
|
+ f.write(v.toString);
|
|
128
|
+ f.close;
|
|
129
|
+ return packageJsons.map!(x => new Package(x)).array;
|
|
130
|
+}
|
|
131
|
+
|
|
132
|
+Package[] grabAll()
|
|
133
|
+{
|
|
134
|
+ if (!exists("packagelist.json"))
|
|
135
|
+ {
|
|
136
|
+ return grabAllFresh;
|
|
137
|
+ }
|
|
138
|
+ auto js = "packagelist.json".readText.parseJSON;
|
|
139
|
+ auto packageJsons = js["packages"].array;
|
|
140
|
+ return packageJsons
|
|
141
|
+ .filter!(x => x.type == JSON_TYPE.OBJECT)
|
|
142
|
+ .map!(x => new Package(x))
|
|
143
|
+ .array;
|
|
144
|
+}
|