OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 /* eslint-env node */ |
| 19 /* eslint no-console: "off" */ |
| 20 |
| 21 "use strict"; |
| 22 |
| 23 const {exec} = require("child_process"); |
| 24 const fs = require("fs"); |
| 25 const path = require("path"); |
| 26 |
| 27 const {ncp} = require("ncp"); |
| 28 |
| 29 const {download} = require("./download"); |
| 30 |
| 31 const {platform} = process; |
| 32 |
| 33 // macOS specific |
| 34 const dmg = platform == "darwin" ? require("dmg") : null; |
| 35 |
| 36 function extractTar(archive, browserDir) |
| 37 { |
| 38 return new Promise((resolve, reject) => |
| 39 { |
| 40 fs.mkdirSync(browserDir); |
| 41 exec(["tar", "-jxf", archive, "-C", browserDir].join(" "), |
| 42 err => |
| 43 { |
| 44 if (err) |
| 45 reject(err); |
| 46 else |
| 47 resolve(); |
| 48 }); |
| 49 }); |
| 50 } |
| 51 |
| 52 function extractDmg(archive, browserDir) |
| 53 { |
| 54 return new Promise((resolve, reject) => |
| 55 { |
| 56 dmg.mount(archive, (err, mpath) => |
| 57 { |
| 58 if (err) |
| 59 reject(err); |
| 60 else |
| 61 { |
| 62 let files = fs.readdirSync(mpath); |
| 63 let target = files.find(file => /\.app/.test(file)); |
| 64 let source = path.join(mpath, target); |
| 65 fs.mkdirSync(browserDir); |
| 66 ncp(source, path.join(browserDir, target), ncperr => |
| 67 { |
| 68 dmg.unmount(mpath, dmgerr => |
| 69 { |
| 70 if (dmgerr) |
| 71 console.error(`Error unmounting DMG: ${dmgerr}`); |
| 72 }); |
| 73 if (ncperr) |
| 74 { |
| 75 console.error(`Error copying ${source} to ${browserDir}`); |
| 76 reject(ncperr); |
| 77 } |
| 78 else |
| 79 resolve(); |
| 80 }); |
| 81 } |
| 82 }); |
| 83 }); |
| 84 } |
| 85 |
| 86 function runWinInstaller(archive, browserDir) |
| 87 { |
| 88 // Procedure inspired from mozinstall: |
| 89 // https://hg.mozilla.org/mozilla-central/file/tip/testing/mozbase/mozinstall/
mozinstall/mozinstall.py |
| 90 // Also uninstaller will need to be run. |
| 91 return new Promise((resolve, reject) => |
| 92 { |
| 93 exec([archive, `/extractdir=${browserDir}`].join(" "), |
| 94 err => |
| 95 { |
| 96 if (err) |
| 97 reject(err); |
| 98 else |
| 99 resolve(); |
| 100 }); |
| 101 }); |
| 102 } |
| 103 |
| 104 function extractArchive(archive, browserDir) |
| 105 { |
| 106 switch (platform) |
| 107 { |
| 108 case "win32": |
| 109 return runWinInstaller(archive, browserDir); |
| 110 case "linux": |
| 111 return extractTar(archive, browserDir); |
| 112 case "darwin": |
| 113 return extractDmg(archive, browserDir); |
| 114 default: |
| 115 throw new Error("Unexpected platform"); |
| 116 } |
| 117 } |
| 118 |
| 119 function getFirefoxExecutable(browserDir) |
| 120 { |
| 121 switch (platform) |
| 122 { |
| 123 case "win32": |
| 124 return path.join(browserDir, "firefox.exe"); |
| 125 case "linux": |
| 126 return path.join(browserDir, "firefox", "firefox"); |
| 127 case "darwin": |
| 128 return path.join(browserDir, "Firefox.app", "Contents", |
| 129 "MacOS", "firefox"); |
| 130 default: |
| 131 throw new Error("Unexpected platform"); |
| 132 } |
| 133 } |
| 134 |
| 135 function ensureFirefox(firefoxVersion) |
| 136 { |
| 137 let targetPlatform = platform; |
| 138 if (platform == "win32") |
| 139 targetPlatform += "-" + process.arch; |
| 140 let buildTypes = { |
| 141 "win32-ia32": ["win32-EME-free", `Firefox Setup ${firefoxVersion}.exe`], |
| 142 "win32-x64": ["win64-EME-free", `Firefox Setup ${firefoxVersion}.exe`], |
| 143 "linux": ["linux-x86_64", `firefox-${firefoxVersion}.tar.bz2`], |
| 144 "darwin": ["mac-EME-free", `Firefox ${firefoxVersion}.dmg`] |
| 145 }; |
| 146 |
| 147 if (!buildTypes.hasOwnProperty(targetPlatform)) |
| 148 { |
| 149 let err = new Error(`Cannot run browser tests, ${targetPlatform} is unsuppor
ted`); |
| 150 return Promise.reject(err); |
| 151 } |
| 152 |
| 153 return Promise.resolve().then(() => |
| 154 { |
| 155 let snapshotsDir = path.join(__dirname, "..", "..", "firefox-snapshots"); |
| 156 let browserDir = path.join(snapshotsDir, |
| 157 `firefox-${targetPlatform}-${firefoxVersion}`); |
| 158 if (fs.existsSync(browserDir)) |
| 159 return browserDir; |
| 160 |
| 161 if (!fs.existsSync(path.dirname(browserDir))) |
| 162 fs.mkdirSync(path.dirname(browserDir)); |
| 163 |
| 164 let [buildPlatform, fileName] = buildTypes[targetPlatform]; |
| 165 let archive = path.join(snapshotsDir, "download-cache", fileName); |
| 166 |
| 167 return Promise.resolve() |
| 168 .then(() => |
| 169 { |
| 170 if (!fs.existsSync(archive)) |
| 171 { |
| 172 let url = `https://archive.mozilla.org/pub/firefox/releases/${firefoxV
ersion}/${buildPlatform}/en-US/${fileName}`; |
| 173 console.info("Downloading Firefox..."); |
| 174 return download(url, archive); |
| 175 } |
| 176 console.info(`Reusing cached archive ${archive}`); |
| 177 }) |
| 178 .then(() => extractArchive(archive, browserDir)) |
| 179 .then(() => browserDir); |
| 180 }).then(dir => getFirefoxExecutable(dir)); |
| 181 } |
| 182 |
| 183 module.exports.ensureFirefox = ensureFirefox; |
OLD | NEW |