This repository has been archived by the owner on Jun 5, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
snap-packager.js
156 lines (116 loc) · 4.98 KB
/
snap-packager.js
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
const path = require('path');
const ini = require('ini');
const yaml = require('js-yaml');
const debug = require('debug');
const SnapValues = require('./snap-values.js');
const log = debug('electron-forge-maker-snap:snap-packager');
log.log = console.log.bind(console);
module.exports = class SnapPackager {
constructor(options) {
this.options = options;
this.deps = options.dependencies;
this.values = new SnapValues({
makeOptions: this.options.makeOptions,
makerOptions: this.options.makerOptions
});
log(`SnapPackager constructed with: ${options}`);
}
generateDesktopFile() {
const data = {
Name: this.values.applicationName,
Exec: this.values.executableName,
Icon: `\${SNAP}/meta/gui/${this.values.executableName}.png`,
Type: 'Application',
Encoding: 'UTF-8'
};
return ini.encode(data, {section: 'Desktop Entry'});
}
generateSnapcraftYAML() {
const doc = yaml.load(this.deps.fse.readFileSync(path.join(__dirname, 'snapcraft.template.yaml'), 'utf8'));
doc.name = this.values.executableName;
doc.title = this.values.applicationName;
doc.version = this.values.version;
doc.icon = `./snap/gui/${this.values.executableName}.png`;
doc.summary = this.values.summary;
doc.description = this.values.description;
doc.confinement = this.values.confinement;
doc.grade = this.values.grade;
if (this.values.license) {
doc.license = this.values.license;
}
doc.apps[this.values.executableName] = doc.apps['SNAP-TEMPLATE'];
delete doc.apps['SNAP-TEMPLATE'];
doc.apps[this.values.executableName].command = `${this.values.executableName}/${this.values.executableName} --no-sandbox`;
doc.apps[this.values.executableName].plugs = this.values.plugs;
doc.parts.app['override-build'] = `cp -rv . $SNAPCRAFT_PART_INSTALL/${this.values.executableName}`;
doc.parts.app['stage-packages'] = this.values.stagePackages;
if (this.values.layout) {
doc.layout = this.values.layout;
}
return yaml.dump(doc);
}
createSnapcraftFiles() {
const destDir = path.join(this.options.makeOptions.makeDir, 'snapcraft', 'snap');
if (this.deps.fse.existsSync(destDir)) {
this.deps.fse.rmdirSync(destDir, {recursive: true});
}
this.deps.fse.mkdirSync(path.join(destDir, 'gui'), {recursive: true});
const snapcraftYAML = this.generateSnapcraftYAML();
const snapcraftYAMLPath = path.join(destDir, 'snapcraft.yaml');
log(`Generated snapcraft.yaml file contents:\n\n${snapcraftYAML}`);
this.deps.fse.writeFileSync(snapcraftYAMLPath, snapcraftYAML, 'utf8');
log(`snapcraft.yaml file written to: ${snapcraftYAMLPath}`);
const desktopFile = this.generateDesktopFile();
const desktopFilePath = path.join(destDir, 'gui', `${this.values.executableName}.desktop`);
log(`Generated .desktop file contents:\n\n${desktopFile}`);
this.deps.fse.writeFileSync(desktopFilePath, desktopFile, 'utf8');
log(`.desktop file written to: ${desktopFilePath}`);
const iconFileDestination = path.join(destDir, 'gui', `${this.values.executableName}.png`);
this.deps.fse.copyFileSync(this.values.icon, iconFileDestination);
log(`Icon file copied to: ${iconFileDestination}`);
const appFiles = path.join(destDir, '..', 'app');
this.deps.fse.copySync(this.options.makeOptions.dir, appFiles);
log(`App files copied to: ${appFiles}`);
this.deps.fse.renameSync(path.join(appFiles, this.values.packagedExecutableName), path.join(appFiles, this.values.executableName));
log(`Rename '${this.values.packagedExecutableName} to ${this.values.executableName} in: ${appFiles}`);
return true;
}
async createSnapPackage() {
let result = null;
const snapFile = `${this.values.executableName}-${this.values.version}.snap`;
log(`Snap file artifact name will be: ${snapFile}`);
const pathToSnapFile = path.join(this.options.makeOptions.makeDir, 'snapcraft', snapFile);
log(`Snap file will be created at: ${pathToSnapFile}`);
try {
result = await new Promise((resolve, reject) => {
const spawnSnapcraftInDirectory = path.join(this.options.makeOptions.makeDir, 'snapcraft');
const snapcraft = this.deps.spawn('snapcraft', ['snap', '--output', snapFile], {
cwd: spawnSnapcraftInDirectory
});
log(`Snapcraft is now running with: snapcraft snap --output ${snapFile}`);
log(`Snapcraft has been spawned within the directory: ${spawnSnapcraftInDirectory}`);
snapcraft.on('close', code => {
log(`Snapcraft has finished running, with a status code of: ${code}`);
if (code === 0) {
resolve(code);
return;
}
reject(new Error(`Snapcraft exited with a non-zero status code of: ${code}`));
});
snapcraft.on('error', error => {
log(`Snapcraft has encountered an error and is aborting: ${error}`);
reject(error);
});
snapcraft.stdout.on('data', data => {
log(`Snapcraft stdout: ${data.toString()}`);
});
});
} catch (error) {
log(error.message);
throw error;
}
log(`Snapcraft finished with status code: ${result}`);
log(`Snapcraft file generated to: ${pathToSnapFile}`);
return pathToSnapFile;
}
};