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

Feature: reloadable init scripts #1110

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
55 changes: 55 additions & 0 deletions spec/atom-environment-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,61 @@ describe('AtomEnvironment', () => {
});
});

describe('init script handling', () => {
let pulsar
afterEach(() => {
if(pulsar) {
pulsar.unloadEditorWindow();
pulsar.destroy();
}
})

describe('when the config to reload init scripts is not set', () => {
it('loads the script and requires it, but will not reload the init script', () => {
pulsar = new AtomEnvironment({
applicationDelegate: atom.applicationDelegate
});
pulsar.config.set('core.autoReloadInitScript', false)

let initPath = path.join(__dirname, 'fixtures', 'init-script.js')
pulsar.getUserInitScriptPath = () => initPath
pulsar.requireUserInitScript();
let commands = atom.commands.findCommands({target: window})
.filter(({name}) => name.match(/test-case/))
expect(commands).toEqual([{name: 'test-case', displayName: 'Test Case'}])

initPath = path.join(__dirname, 'fixtures', 'different-init-script.js')
pulsar.requireUserInitScript();
commands = atom.commands.findCommands({target: window})
.filter(({name}) => name.match(/test-case/))
expect(commands).toEqual([{name: 'test-case', displayName: 'Test Case'}])
})
})

describe('when the config to reload init scripts is set', () => {
it('allows for the init script to be reloaded', () => {
pulsar = new AtomEnvironment({
applicationDelegate: atom.applicationDelegate
});
pulsar.config.set('core.autoReloadInitScript', true)

let initPath = path.join(__dirname, 'fixtures', 'reloadable-init-script.js')
pulsar.getUserInitScriptPath = () => initPath
pulsar.requireUserInitScript();
let commands = atom.commands.findCommands({target: window})
.filter(({name}) => name.match(/test-case/))
expect(commands).toEqual([{name: 'test-case', displayName: 'Test Case'}])

initPath = path.join(__dirname, 'fixtures', 'different-init-script.js')
pulsar.getUserInitScriptPath = () => initPath
pulsar.requireUserInitScript();
commands = atom.commands.findCommands({target: window})
.filter(({name}) => name.match(/test-case/))
expect(commands).toEqual([{name: 'test-case-2', displayName: 'Test Case 2'}])
})
})
})

describe('attemptRestoreProjectStateForPaths(state, projectPaths, filesToOpen)', () => {
describe('when the window is clean (empty or has only unnamed, unmodified buffers)', () => {
beforeEach(async () => {
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/different-init-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
disposable.add(atom.commands.add(window, 'test-case-2', () => {
atom.notifications.addInfo("Hello!")
}))
3 changes: 3 additions & 0 deletions spec/fixtures/init-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
atom.commands.add(window, 'test-case', () => {
atom.notifications.addInfo("Hello!")
})
3 changes: 3 additions & 0 deletions spec/fixtures/reloadable-init-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
disposable.add(atom.commands.add(window, 'test-case', () => {
atom.notifications.addInfo("Hello!")
}))
15 changes: 14 additions & 1 deletion src/atom-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AtomEnvironment {
this.loadTime = null;
this.emitter = new Emitter();
this.disposables = new CompositeDisposable();
this.initScriptDisposables = new CompositeDisposable();
this.pathsWithWaitSessions = new Set();

/** @type {DeserializerManager} */
Expand Down Expand Up @@ -1563,7 +1564,19 @@ or use Pane::saveItemAs for programmatic saving.`);
const userInitScriptPath = this.getUserInitScriptPath();
if (userInitScriptPath) {
try {
if (fs.isFileSync(userInitScriptPath)) require(userInitScriptPath);
if (fs.isFileSync(userInitScriptPath)) {
if(this.config.get('core.autoReloadInitScript')) {
this.disposables.delete(this.initScriptDisposables)
this.initScriptDisposables.dispose()
this.initScriptDisposables = new CompositeDisposable();
this.disposables.add(this.initScriptDisposables)
let contents = fs.readFileSync(userInitScriptPath, 'utf-8')
contents = `((disposable) => { ${contents}\n })(this.initScriptDisposables)`
eval(contents)
} else {
require(userInitScriptPath);
}
}
} catch (error) {
this.notifications.addError(
`Failed to load \`${userInitScriptPath}\``,
Expand Down
8 changes: 7 additions & 1 deletion src/config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,13 @@ const configSchema = {
default: false,
title: 'Allow Window Transparency',
description: `Allows editor windows to be see-through. When this setting is enabled, UI themes and user stylesheets can use background colors with an alpha channel to make editor windows translucent. Takes effect after a restart of Pulsar.`
}
},
autoReloadInitScript: {
type: 'boolean',
default: false,
description:
'Automatically reloads the init script when the file was changed. Adds a global `disposable` object that can be used at the init script to dispose old commands, old callbacks, etc. DO NOT enable this without using `disposable` in your init script, otherwise you will have duplicated commands.'
},
}
},
editor: {
Expand Down
Loading