| 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 "use strict"; | 
 |   19  | 
 |   20 const {exec} = require("child_process"); | 
 |   21 const fs = require("fs"); | 
 |   22 const path = require("path"); | 
 |   23  | 
 |   24 const {ncp} = require("ncp"); | 
 |   25  | 
 |   26 const {download} = require("./download"); | 
 |   27  | 
 |   28 const {platform} = process; | 
 |   29  | 
 |   30 // macOS specific | 
 |   31 const dmg = platform == "darwin" ? require("dmg") : null; | 
 |   32  | 
 |   33 function extractTar(archive, browserDir) | 
 |   34 { | 
 |   35   return new Promise((resolve, reject) => | 
 |   36   { | 
 |   37     fs.mkdirSync(browserDir); | 
 |   38     exec(["tar", "-jxf", archive, "-C", browserDir].join(" "), | 
 |   39          err => | 
 |   40          { | 
 |   41            if (err) | 
 |   42              reject(err); | 
 |   43            else | 
 |   44              resolve(); | 
 |   45          }); | 
 |   46   }); | 
 |   47 } | 
 |   48  | 
 |   49 function extractDmg(archive, browserDir) | 
 |   50 { | 
 |   51   return new Promise((resolve, reject) => | 
 |   52   { | 
 |   53     dmg.mount(archive, (err, mpath) => | 
 |   54     { | 
 |   55       if (err) | 
 |   56         reject(err); | 
 |   57       else | 
 |   58       { | 
 |   59         let files = fs.readdirSync(mpath); | 
 |   60         let target = files.find(file => /\.app/.test(file)); | 
 |   61         let source = path.join(mpath, target); | 
 |   62         fs.mkdirSync(browserDir); | 
 |   63         ncp(source, path.join(browserDir, target), ncperr => | 
 |   64         { | 
 |   65           dmg.unmount(mpath, dmgerr => | 
 |   66           { | 
 |   67             if (dmgerr) | 
 |   68               console.error(`Error unmounting DMG: ${dmgerr}`); | 
 |   69           }); | 
 |   70           if (ncperr) | 
 |   71           { | 
 |   72             console.error(`Error copying ${source} to ${browserDir}`); | 
 |   73             reject(ncperr); | 
 |   74           } | 
 |   75           else | 
 |   76             resolve(); | 
 |   77         }); | 
 |   78       } | 
 |   79     }); | 
 |   80   }); | 
 |   81 } | 
 |   82  | 
 |   83 function runWinInstaller(archive, browserDir) | 
 |   84 { | 
 |   85   // Procedure inspired from mozinstall: | 
 |   86   // https://hg.mozilla.org/mozilla-central/file/tip/testing/mozbase/mozinstall/
     mozinstall/mozinstall.py | 
 |   87   // Also uninstaller will need to be run. | 
 |   88   return new Promise((resolve, reject) => | 
 |   89   { | 
 |   90     exec([archive, `/extractdir=${browserDir}`].join(" "), | 
 |   91          err => | 
 |   92          { | 
 |   93            if (err) | 
 |   94              reject(err); | 
 |   95            else | 
 |   96              resolve(); | 
 |   97          }); | 
 |   98   }); | 
 |   99 } | 
 |  100  | 
 |  101 function extractArchive(archive, browserDir) | 
 |  102 { | 
 |  103   switch (platform) | 
 |  104   { | 
 |  105     case "win32": | 
 |  106       return runWinInstaller(archive, browserDir); | 
 |  107     case "linux": | 
 |  108       return extractTar(archive, browserDir); | 
 |  109     case "darwin": | 
 |  110       return extractDmg(archive, browserDir); | 
 |  111     default: | 
 |  112       throw new Error("Unexpected platform"); | 
 |  113   } | 
 |  114 } | 
 |  115  | 
 |  116 function getFirefoxExecutable(browserDir) | 
 |  117 { | 
 |  118   switch (platform) | 
 |  119   { | 
 |  120     case "win32": | 
 |  121       return path.join(browserDir, "firefox.exe"); | 
 |  122     case "linux": | 
 |  123       return path.join(browserDir, "firefox", "firefox"); | 
 |  124     case "darwin": | 
 |  125       return path.join(browserDir, "Firefox.app", "Contents", | 
 |  126                        "MacOS", "firefox"); | 
 |  127     default: | 
 |  128       throw new Error("Unexpected platform"); | 
 |  129   } | 
 |  130 } | 
 |  131  | 
 |  132 function ensureFirefox(firefoxVersion) | 
 |  133 { | 
 |  134   let targetPlatform = platform; | 
 |  135   if (platform == "win32") | 
 |  136     targetPlatform += "-" + process.arch; | 
 |  137   let buildTypes = { | 
 |  138     "win32-ia32": ["win32-EME-free", `Firefox Setup ${firefoxVersion}.exe`], | 
 |  139     "win32-x64": ["win64-EME-free", `Firefox Setup ${firefoxVersion}.exe`], | 
 |  140     "linux": ["linux-x86_64", `firefox-${firefoxVersion}.tar.bz2`], | 
 |  141     "darwin": ["mac-EME-free", `Firefox ${firefoxVersion}.dmg`] | 
 |  142   }; | 
 |  143  | 
 |  144   if (!buildTypes.hasOwnProperty(targetPlatform)) | 
 |  145   { | 
 |  146     let err = new Error(`Cannot run browser tests, ${targetPlatform} is unsuppor
     ted`); | 
 |  147     return Promise.reject(err); | 
 |  148   } | 
 |  149  | 
 |  150   return Promise.resolve().then(() => | 
 |  151   { | 
 |  152     let snapshotsDir = path.join(__dirname, "..", "..", "firefox-snapshots"); | 
 |  153     let browserDir = path.join(snapshotsDir, | 
 |  154                                 `firefox-${targetPlatform}-${firefoxVersion}`); | 
 |  155     if (fs.existsSync(browserDir)) | 
 |  156       return browserDir; | 
 |  157  | 
 |  158     if (!fs.existsSync(path.dirname(browserDir))) | 
 |  159       fs.mkdirSync(path.dirname(browserDir)); | 
 |  160  | 
 |  161     let [buildPlatform, fileName] = buildTypes[targetPlatform]; | 
 |  162     let archive = path.join(snapshotsDir, "download-cache", fileName); | 
 |  163  | 
 |  164     return Promise.resolve() | 
 |  165       .then(() => | 
 |  166       { | 
 |  167         if (!fs.existsSync(archive)) | 
 |  168         { | 
 |  169           let url = `https://archive.mozilla.org/pub/firefox/releases/${firefoxV
     ersion}/${buildPlatform}/en-US/${fileName}`; | 
 |  170           console.info("Downloading Firefox..."); | 
 |  171           return download(url, archive); | 
 |  172         } | 
 |  173         console.info(`Reusing cached archive ${archive}`); | 
 |  174       }) | 
 |  175       .then(() => extractArchive(archive, browserDir)) | 
 |  176       .then(() => browserDir); | 
 |  177   }).then(dir => getFirefoxExecutable(dir)); | 
 |  178 } | 
 |  179  | 
 |  180 module.exports.ensureFirefox = ensureFirefox; | 
| OLD | NEW |