-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapl-pkg.js
More file actions
455 lines (368 loc) · 13.5 KB
/
apl-pkg.js
File metadata and controls
455 lines (368 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* APL Package Manager (apl-pkg)
* Manage APL libraries and dependencies
*/
const fs = require('fs');
const path = require('path');
const https = require('https');
const { execSync } = require('child_process');
class APLPackageManager {
constructor(options = {}) {
this.options = {
registry: options.registry || 'https://registry.apl.aevov.ai',
cacheDir: options.cacheDir || path.join(process.cwd(), '.apl_cache'),
globalDir: options.globalDir || path.join(require('os').homedir(), '.apl', 'packages'),
...options
};
this.packageCache = new Map();
this.installed = new Map();
this.ensureDirectories();
}
/**
* Ensure cache and global directories exist
*/
ensureDirectories() {
[this.options.cacheDir, this.options.globalDir].forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
});
}
/**
* Install a package
*/
async install(packageName, version = 'latest', options = {}) {
console.log(`📦 Installing ${packageName}@${version}...`);
try {
// Check if already installed
if (this.isInstalled(packageName, version) && !options.force) {
console.log(`✓ ${packageName}@${version} already installed`);
return { success: true, cached: true };
}
// Fetch package metadata
const metadata = await this.fetchPackageMetadata(packageName);
if (!metadata) {
throw new Error(`Package ${packageName} not found`);
}
// Resolve version
const resolvedVersion = this.resolveVersion(metadata, version);
const packageInfo = metadata.versions[resolvedVersion];
if (!packageInfo) {
throw new Error(`Version ${version} not found for ${packageName}`);
}
// Download package
const packagePath = await this.downloadPackage(packageName, resolvedVersion, packageInfo.tarball);
// Extract and install
await this.extractPackage(packagePath, packageName, resolvedVersion);
// Install dependencies
if (packageInfo.dependencies && !options.noDependencies) {
await this.installDependencies(packageInfo.dependencies);
}
// Register installation
this.installed.set(packageName, {
version: resolvedVersion,
path: this.getPackagePath(packageName, resolvedVersion)
});
console.log(`✓ ${packageName}@${resolvedVersion} installed successfully`);
return {
success: true,
package: packageName,
version: resolvedVersion
};
} catch (error) {
console.error(`✗ Failed to install ${packageName}: ${error.message}`);
return {
success: false,
error: error.message
};
}
}
/**
* Uninstall a package
*/
async uninstall(packageName) {
console.log(`🗑️ Uninstalling ${packageName}...`);
try {
const info = this.installed.get(packageName);
if (!info) {
throw new Error(`Package ${packageName} is not installed`);
}
// Remove package directory
const packageDir = info.path;
if (fs.existsSync(packageDir)) {
fs.rmSync(packageDir, { recursive: true, force: true });
}
// Unregister
this.installed.delete(packageName);
console.log(`✓ ${packageName} uninstalled`);
return { success: true };
} catch (error) {
console.error(`✗ Failed to uninstall ${packageName}: ${error.message}`);
return { success: false, error: error.message };
}
}
/**
* List installed packages
*/
list() {
console.log('\n📦 Installed APL Packages:\n');
if (this.installed.size === 0) {
console.log(' No packages installed');
return [];
}
const packages = [];
for (const [name, info] of this.installed.entries()) {
console.log(` ${name}@${info.version}`);
packages.push({ name, version: info.version, path: info.path });
}
return packages;
}
/**
* Search for packages
*/
async search(query) {
console.log(`🔍 Searching for "${query}"...`);
try {
const results = await this.fetchSearchResults(query);
console.log(`\nFound ${results.length} results:\n`);
results.forEach(pkg => {
console.log(` ${pkg.name}@${pkg.version}`);
console.log(` ${pkg.description}`);
console.log('');
});
return results;
} catch (error) {
console.error(`✗ Search failed: ${error.message}`);
return [];
}
}
/**
* Update a package
*/
async update(packageName) {
console.log(`⬆️ Updating ${packageName}...`);
try {
const metadata = await this.fetchPackageMetadata(packageName);
const latest = metadata['dist-tags'].latest;
const result = await this.install(packageName, latest, { force: true });
if (result.success) {
console.log(`✓ ${packageName} updated to ${latest}`);
}
return result;
} catch (error) {
console.error(`✗ Update failed: ${error.message}`);
return { success: false, error: error.message };
}
}
/**
* Publish a package
*/
async publish(packageDir = process.cwd()) {
console.log('📤 Publishing package...');
try {
// Read package.json
const packageJsonPath = path.join(packageDir, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
throw new Error('No package.json found');
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
// Validate package
this.validatePackage(packageJson);
// Create tarball
const tarball = await this.createTarball(packageDir);
// Upload to registry
const result = await this.uploadPackage(packageJson, tarball);
console.log(`✓ ${packageJson.name}@${packageJson.version} published`);
return result;
} catch (error) {
console.error(`✗ Publish failed: ${error.message}`);
return { success: false, error: error.message };
}
}
/**
* Initialize a new package
*/
async init(dir = process.cwd()) {
console.log('📝 Initializing new APL package...');
const packageJson = {
name: path.basename(dir),
version: '1.0.0',
description: 'An APL package',
main: 'index.apl',
scripts: {},
keywords: ['apl'],
author: '',
license: 'Apache-2.0'
};
const packageJsonPath = path.join(dir, 'package.json');
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
// Create example file
const exampleCode = `// APL Package
// Generated by apl-pkg
function greet(name) {
print("Hello, " + name + "!")
}
greet("World")
`;
fs.writeFileSync(path.join(dir, 'index.apl'), exampleCode);
console.log('✓ Package initialized');
console.log(` Created: package.json`);
console.log(` Created: index.apl`);
return { success: true };
}
// Helper methods
async fetchPackageMetadata(packageName) {
return new Promise((resolve, reject) => {
const url = `${this.options.registry}/${packageName}`;
https.get(url, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(new Error('Invalid package metadata'));
}
});
}).on('error', reject);
});
}
async fetchSearchResults(query) {
// Simulate search for now
return [
{
name: 'apl-quantum-utils',
version: '1.0.0',
description: 'Quantum computing utilities for APL'
},
{
name: 'apl-neural-networks',
version: '2.1.0',
description: 'Neural network library for APL'
}
].filter(pkg => pkg.name.includes(query) || pkg.description.includes(query));
}
resolveVersion(metadata, version) {
if (version === 'latest') {
return metadata['dist-tags'].latest;
}
return version;
}
async downloadPackage(name, version, tarballUrl) {
const filename = `${name}-${version}.tgz`;
const filepath = path.join(this.options.cacheDir, filename);
if (fs.existsSync(filepath)) {
return filepath;
}
// Download tarball
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filepath);
https.get(tarballUrl, (res) => {
res.pipe(file);
file.on('finish', () => {
file.close();
resolve(filepath);
});
}).on('error', (err) => {
fs.unlinkSync(filepath);
reject(err);
});
});
}
async extractPackage(tarballPath, name, version) {
const extractPath = this.getPackagePath(name, version);
if (!fs.existsSync(extractPath)) {
fs.mkdirSync(extractPath, { recursive: true });
}
// Extract using tar
try {
execSync(`tar -xzf ${tarballPath} -C ${extractPath} --strip-components=1`);
} catch (error) {
throw new Error(`Failed to extract package: ${error.message}`);
}
}
async installDependencies(dependencies) {
for (const [name, version] of Object.entries(dependencies)) {
await this.install(name, version);
}
}
getPackagePath(name, version) {
return path.join(this.options.globalDir, name, version);
}
isInstalled(name, version) {
const info = this.installed.get(name);
return info && (version === 'latest' || info.version === version);
}
validatePackage(packageJson) {
const required = ['name', 'version'];
for (const field of required) {
if (!packageJson[field]) {
throw new Error(`Missing required field: ${field}`);
}
}
// Validate name
if (!/^[a-z0-9-]+$/.test(packageJson.name)) {
throw new Error('Invalid package name (use lowercase letters, numbers, and hyphens only)');
}
}
async createTarball(packageDir) {
const tarballPath = path.join(this.options.cacheDir, 'temp.tgz');
try {
execSync(`tar -czf ${tarballPath} -C ${packageDir} .`);
return tarballPath;
} catch (error) {
throw new Error(`Failed to create tarball: ${error.message}`);
}
}
async uploadPackage(packageJson, tarballPath) {
// In production, upload to registry
console.log(' Uploading to registry...');
// Simulate upload
return new Promise((resolve) => {
setTimeout(() => {
resolve({ success: true });
}, 1000);
});
}
}
// CLI interface
if (require.main === module) {
const [,, command, ...args] = process.argv;
const pm = new APLPackageManager();
switch (command) {
case 'install':
pm.install(args[0], args[1]);
break;
case 'uninstall':
pm.uninstall(args[0]);
break;
case 'list':
pm.list();
break;
case 'search':
pm.search(args[0]);
break;
case 'update':
pm.update(args[0]);
break;
case 'publish':
pm.publish(args[0]);
break;
case 'init':
pm.init(args[0]);
break;
default:
console.log(`
APL Package Manager v1.0.0
Usage:
apl-pkg install <package> [version]
apl-pkg uninstall <package>
apl-pkg list
apl-pkg search <query>
apl-pkg update <package>
apl-pkg publish [dir]
apl-pkg init [dir]
`);
}
}
module.exports = APLPackageManager;