michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: const { platform, pathFor } = require('sdk/system'); michael@0: const { defer } = require('sdk/core/promise'); michael@0: const { emit } = require('sdk/event/core'); michael@0: const { join } = require('sdk/fs/path'); michael@0: const { writeFile, unlinkSync, existsSync } = require('sdk/io/fs'); michael@0: const PROFILE_DIR= pathFor('ProfD'); michael@0: const isWindows = platform.toLowerCase().indexOf('win') === 0; michael@0: const isOSX = platform.toLowerCase().indexOf('darwin') === 0; michael@0: michael@0: let scripts = { michael@0: 'args.sh': 'echo $1 $2 $3 $4', michael@0: 'args.bat': 'echo %1 %2 %3 %4', michael@0: 'check-env.sh': 'echo $CHILD_PROCESS_ENV_TEST', michael@0: 'check-env.bat': 'echo %CHILD_PROCESS_ENV_TEST%', michael@0: 'check-pwd.sh': 'echo $PWD', michael@0: 'check-pwd.bat': 'cd', michael@0: 'large-err.sh': 'for n in `seq 0 $1` ; do echo "E" 1>&2; done', michael@0: 'large-err-mac.sh': 'for ((i=0; i<$1; i=i+1)); do echo "E" 1>&2; done', michael@0: 'large-err.bat': 'FOR /l %%i in (0,1,%1) DO echo "E" 1>&2', michael@0: 'large-out.sh': 'for n in `seq 0 $1` ; do echo "O"; done', michael@0: 'large-out-mac.sh': 'for ((i=0; i<$1; i=i+1)); do echo "O"; done', michael@0: 'large-out.bat': 'FOR /l %%i in (0,1,%1) DO echo "O"', michael@0: 'wait.sh': 'sleep 2', michael@0: // Use `ping` to an invalid IP address because `timeout` isn't michael@0: // on all environments? http://stackoverflow.com/a/1672349/1785755 michael@0: 'wait.bat': 'ping 1.1.1.1 -n 1 -w 2000 > nul' michael@0: }; michael@0: michael@0: Object.keys(scripts).forEach(filename => { michael@0: if (/\.sh$/.test(filename)) michael@0: scripts[filename] = '#!/bin/sh\n' + scripts[filename]; michael@0: else if (/\.bat$/.test(filename)) michael@0: scripts[filename] = '@echo off\n' + scripts[filename]; michael@0: }); michael@0: michael@0: function getScript (name) { michael@0: // Use specific OSX script if exists michael@0: if (isOSX && scripts[name + '-mac.sh']) michael@0: name = name + '-mac'; michael@0: let fileName = name + (isWindows ? '.bat' : '.sh'); michael@0: return createFile(fileName, scripts[fileName]); michael@0: } michael@0: exports.getScript = getScript; michael@0: michael@0: function createFile (name, data) { michael@0: let { promise, resolve, reject } = defer(); michael@0: let fileName = join(PROFILE_DIR, name); michael@0: writeFile(fileName, data, function (err) { michael@0: if (err) reject(); michael@0: else { michael@0: makeExecutable(fileName); michael@0: resolve(fileName); michael@0: } michael@0: }); michael@0: return promise; michael@0: } michael@0: michael@0: // TODO Use fs.chmod once implemented, bug 914606 michael@0: function makeExecutable (name) { michael@0: let { CC } = require('chrome'); michael@0: let nsILocalFile = CC('@mozilla.org/file/local;1', 'nsILocalFile', 'initWithPath'); michael@0: let file = nsILocalFile(name); michael@0: file.permissions = parseInt('0777', 8); michael@0: } michael@0: michael@0: function deleteFile (name) { michael@0: let file = join(PROFILE_DIR, name); michael@0: if (existsSync(file)) michael@0: unlinkSync(file); michael@0: } michael@0: michael@0: function cleanUp () { michael@0: Object.keys(scripts).forEach(deleteFile); michael@0: } michael@0: exports.cleanUp = cleanUp;