Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo: Add a utility for reloading applets on code change #2075

Merged
merged 3 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.mo
*.pyc
*/gschemas.compiled
node_modules/
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,13 @@ Test your translations `.po` locally before uploading to Spices:"
More info:
```
./cinnamon-spices-makepot --help
```
```

# Auto-reloading applets

A utility script using Gulp is provided that can automatically reload applets on code change. To use, install the [latest NodeJS LTS release](https://github.com/nodesource/distributions).
- Run `npm install -g gulp@^4.0.0`
- In the root of this repo's directory run `npm install`.
- To use the script, run ```gulp watch --uuid="<applet uuid>"```

For more info run ```gulp help```.
132 changes: 132 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const fs = require('fs');
const os = require('os');
const gulp = require('gulp');
const clear = require('clear');
const {exec, execSync} = require('child_process');

const getArgs = function() {
const argv = require('yargs')
.option('uuid', {
alias: 'u'
})
.argv;

const UUID = argv.u;

if (!UUID) {
throw new Error('Unable to get the UUID.');
}

return UUID;
}

gulp.task('install', (done) => {
const UUID = getArgs();
const homeDir = os.homedir();
const systemXletDir = `${homeDir}/.local/share/cinnamon/applets/${UUID}/`;
const localXletDir = `./${UUID}/files/${UUID}/`;
const systemDirExists = fs.existsSync(systemXletDir);
const localDirExists = fs.existsSync(localXletDir);
const userInfo = os.userInfo();

if (!systemDirExists) {
console.log(
'Xlet does not exist in the system directory. Attempting to create the directory:\n' +
systemXletDir
);
execSync(`mkdir ${systemXletDir}`);
}

if (!localDirExists) {
throw new Error('Xlet does not exist in the local directory.');
}

const {uid, gid} = fs.statSync(systemXletDir);

if (uid !== userInfo.uid || gid !== userInfo.gid) {
throw new Error(`Incorrect permission are set for the applets directory. Please run 'gulp help'.`);
}

exec(`rm -rf ${systemXletDir} && ` +
`cp -arf ${localXletDir} ${systemXletDir}`,
function(err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
done();
}
);
});

const reload = function(done) {
const UUID = getArgs();
exec(
'dbus-send --session --dest=org.Cinnamon.LookingGlass --type=method_call '
+ '/org/Cinnamon/LookingGlass org.Cinnamon.LookingGlass.ReloadExtension '
+ `string:'${UUID}' string:'APPLET'`,
function(err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
done();
}
);
};

gulp.task('reload', gulp.series('install', reload));

gulp.task('_watch', (done) => {
const UUID = getArgs();
const localXletDir = `./${UUID}/files/${UUID}`;
const glob = `${localXletDir}/**/**/**/*.{js,json,py,css,po}`;
const localDirExists = fs.existsSync(localXletDir);

if (!localDirExists) {
throw new Error('Xlet does not exist in the local directory.');
}

console.log(`Watching glob pattern: ${glob}`)
gulp.watch(glob)
.on('change', gulp.parallel('reload'));
done();
});

gulp.task('clear-terminal', (done) => {
clear();
done();
});

gulp.task('watch', gulp.series('clear-terminal', (done) => {
let [, , , uuid] = process.argv;
let spawnWatch = () => {
let proc = require('child_process').spawn('gulp', ['_watch', uuid], {stdio: 'inherit'});
proc.on('close', function(code) {
spawnWatch();
});
};
spawnWatch();
done();
}));

gulp.task('help', gulp.series('clear-terminal', (done) => {
console.log(
`Usage: gulp watch [flags]

This file uses gulp to provide a watch task for xlet development.
It will copy the xlet files from the UUID directory specified and
auto-reload the applet on code change.

Install gulp globally.

npm: 'npm install -g gulp@^4.0.0'
yarn: 'yarn global add gulp@^4.0.0'

To use this script, run 'gulp watch --uuid="<xlet uuid>"'.
Example: 'gulp watch --uuid="grouped-window-list@cinnamon.org"'

Options:
--uuid UUID of the xlet to watch.
`
);
done();
}));

gulp.task('default', gulp.series('watch', (done) => done()));
Loading