Debian Codegraph @

tools/eos-application-manifest/commands/init.js

raw
1 const Gio = imports.gi.Gio;
2 const GLib = imports.gi.GLib;
3 const System = imports.system;
4
5 const DEFAULT_LOCALE = 'en';
6 const CURRENT_MANIFEST_VERSION = 1;
7
8 function execute(args) {
9 let [id, argdict] = parseArgs(args);
10 let manifest = createManifest(id, argdict);
11 let json_manifest = JSON.stringify(manifest, null, " ");
12
13 let filename = createFilenameForManifest(manifest);
14 let cwd = Gio.File.new_for_path(GLib.get_current_dir());
15 let file = cwd.get_child(filename);
16
17 file.replace_contents(json_manifest, null /* etag */,
18 false /* make backup */, Gio.FileCreateFlags.REPLACE_DESTINATION,
19 null /* cancellable */);
20 }
21
22 // Takes a manifest with a valid application ID and creates the appropriate
23 // filename to put it in.
24 function createFilenameForManifest(manifest) {
25 return manifest.applicationId + '.json';
26 }
27
28 // Takes a valid application ID and validated list of command-line arguments
29 // and creates a valid application manifest.
30 function createManifest(id, argdict) {
31 let locale = argdict.locale || DEFAULT_LOCALE;
32 let manifestVersion = CURRENT_MANIFEST_VERSION;
33 if ('manifest-version' in argdict)
34 manifestVersion = parseInt(argdict['manifest-version'], 10);
35
36 let launcherKey, launcherValue;
37 if (typeof argdict.appclass != 'undefined') {
38 launcherKey = 'applicationClass';
39 launcherValue = argdict.appclass;
40 } else {
41 launcherKey = 'exec';
42 launcherValue = argdict.exec;
43 }
44
45 let manifest = {};
46 manifest.manifestVersion = manifestVersion;
47 manifest.applicationId = id;
48 manifest.applicationName = createLocaleDict(locale, argdict.appname);
49 manifest.authorName = argdict.author;
50 manifest.authorWebsite = argdict.website || '';
51 manifest.description = createLocaleDict(locale, argdict.description || '');
52 manifest.version = argdict.appversion;
53 manifest.changes = createLocaleDict(locale, []);
54 manifest.license = argdict.license || '';
55 manifest.resources = [];
56 manifest[launcherKey] = launcherValue;
57 manifest.icons = {};
58 manifest.categories = [];
59 manifest.permissions = [];
60 manifest.metadata = {};
61
62 return manifest;
63 }
64
65 // Takes a locale and a JS object and creates a dictionary such that for
66 // locale == 'pt_BR', return value is { 'pt_BR': value }.
67 function createLocaleDict(locale, value) {
68 let retval = {};
69 retval[locale] = value;
70 return retval;
71 }
72
73 // Parse command line arguments and return a valid application ID and a
74 // dictionary of other parameters passed. Validates the parameters.
75 function parseArgs(args) {
76 if (typeof args == 'undefined' || args.length === 0) {
77 help();
78 throw new Error();
79 }
80
81 // App ID is the mandatory first argument
82 let id = args.shift();
83 if (!Gio.Application.id_is_valid(id))
84 throw new Error('"%s" is not a valid application ID. Rules:\n\
85 - must contain only the ASCII characters "[A-Z][a-z][0-9]_-." and must not\n\
86 begin with a digit.\n\
87 - must contain at least one "."" (period) character (and thus at least three\n\
88 characters).\n\
89 - must not begin or end with a "." (period) character.\n\
90 - must not contain consecutive "."" (period) characters.\n\
91 - must not exceed 255 characters.'.format(id));
92
93 let argdict = parseRemainingArgs(args);
94 if (!validateRemainingArgs(argdict)) {
95 help();
96 throw new Error();
97 }
98
99 return [id, argdict];
100 }
101
102 // Parse all arguments after the application ID has been removed
103 function parseRemainingArgs(args) {
104 let retval = {};
105 for (let count = 0; count < args.length; count++) {
106 if (args[count].startsWith('--')) {
107 if (args[count].indexOf('=') != -1) {
108 // One parameter of the form --parameter=value
109 let expr = args[count].slice(2);
110 let [parameter, value] = expr.split('=');
111 retval[parameter] = value;
112 continue;
113 }
114 // Otherwise, the --parameter consumes the next argument if it's
115 // there and not also a --parameter
116 if (typeof args[count + 1] == 'undefined' ||
117 args[count + 1].startsWith('--')) {
118 let parameter = args[count].slice(2);
119 retval[parameter] = null;
120 continue;
121 }
122 let parameter = args[count].slice(2);
123 retval[parameter] = args[count + 1];
124 count++;
125 }
126 }
127 return retval;
128 }
129
130 // Validate all arguments besides the application ID
131 function validateRemainingArgs(args) {
132 let requiredArgs = ['appname', 'author', 'appversion'];
133 let knownArgs = ['appclass', 'exec', 'manifest-version', 'website',
134 'description', 'locale', 'license'];
135
136 for (let arg of requiredArgs) {
137 if (!(arg in args))
138 return false;
139 }
140
141 // Only one of --appclass or --exec
142 if ('appclass' in args && 'exec' in args)
143 return false;
144 if (!('appclass' in args) && !('exec' in args))
145 return false;
146
147 for (let arg in args) {
148 if (requiredArgs.indexOf(arg) == -1 && knownArgs.indexOf(arg) == -1)
149 return false;
150 if (args[arg] === null)
151 return false;
152 }
153
154 // Validate any content requirements for certain parameters here
155
156 if ('manifest-version' in args) {
157 let versionNumber = parseInt(args['manifest-version'], 10);
158 if (Number.isNaN(versionNumber) || versionNumber < 0 ||
159 versionNumber > CURRENT_MANIFEST_VERSION)
160 return false;
161 }
162
163 return true;
164 }
165
166 function summary() {
167 return 'Generate a minimal valid manifest';
168 }
169
170 function help() {
171 print('Generates a minimal valid manifest in the current directory.\n\n\
172 Usage: %s init <application_id>\n\
173 --appname=<name> --appversion=<version>\n\
174 --author=<author> {--appclass=<application_class>|--exec=<path>}\n\
175 [--manifest-version=<version>] [--website=<website>]\n\
176 [--description=<description>] [--locale=<locale>]\n\
177 [--license=<license>]\n\n\
178 Required options for a minimal valid manifest:\n\
179 <application_id> - A unique application ID, e.g. "com.example.weather"\n\
180 --appname - Human-readable application name, e.g. "Weather Reader"\n\
181 --author - Author name, e.g. "Joe Coder <[email protected]>"\n\
182 --appversion - Application version number\n\
183 --appclass - Name of a class derived from Endless.Application with\n\
184 its module to import, e.g. WeatherReader.App\n\
185 --exec - Path to an executable within the package, that\n\
186 launches the application, e.g. "bin/weather-reader"\n\
187 (Use only one of either --appclass or --exec.)\n\n\
188 Other options:\n\
189 --manifest-version - Version of the manifest file specification to use\n\
190 [default: the current one]\n\
191 --website - URI with more information about the application\n\
192 --description - Long description of the application\n\
193 --locale - Locale information for --appname and --description\n\
194 [default "en"]\n\
195 --license - Software license under which the application is\n\
196 provided to users'.format(System.programInvocationName));
197 }