diff --git a/build/10-ubports.rules b/build/10-ubports.rules deleted file mode 100644 index 5122618c..00000000 --- a/build/10-ubports.rules +++ /dev/null @@ -1,81 +0,0 @@ -SUBSYSTEM=="usb", ATTR{idVendor}=="03f0", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="03fc", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0408", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0409", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0414", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0451", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0471", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0482", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0489", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="04b7", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="04c5", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="04da", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="04dd", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0502", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0531", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="054c", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="067e", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="091e", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0930", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0955", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0b05", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0c2e", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0db0", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0e79", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0e8d", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0f1c", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="0fce", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1004", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="109b", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="10a9", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1219", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="12d1", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1662", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="16d5", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="17ef", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1949", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="19a5", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="19d2", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1b8e", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1bbb", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1d09", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1d45", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1d4d", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1d91", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1e85", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1ebf", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1f3a", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="1f53", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2006", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="201e", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2080", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2116", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2207", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2237", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2257", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="22d9", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2314", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2340", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2420", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="24e3", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="25e3", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2717", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="271d", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2836", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2916", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="297f", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="29a9", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="29e4", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2a45", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2a47", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2a49", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2a70", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="2ae5", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="413c", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="8087", MODE="0666" -SUBSYSTEM=="usb", ATTR{idVendor}=="e040", MODE="0666" \ No newline at end of file diff --git a/src/lib/udev.js b/src/lib/udev.js index d2898288..cba26e94 100644 --- a/src/lib/udev.js +++ b/src/lib/udev.js @@ -22,6 +22,101 @@ const path = require("path"); const log = require("./log.js"); const { ipcMain } = require("electron"); +const vendorIds = [ + "03f0", + "03fc", + "0408", + "0409", + "0414", + "0451", + "0471", + "0482", + "0489", + "04b7", + "04c5", + "04da", + "04dd", + "04e8", + "0502", + "0531", + "054c", + "05c6", + "067e", + "091e", + "0930", + "0955", + "0b05", + "0bb4", + "0c2e", + "0db0", + "0e79", + "0e8d", + "0f1c", + "0fce", + "1004", + "109b", + "10a9", + "1219", + "12d1", + "1662", + "16d5", + "17ef", + "18d1", + "1949", + "19a5", + "19d2", + "1b8e", + "1bbb", + "1d09", + "1d45", + "1d4d", + "1d91", + "1e85", + "1ebf", + "1f3a", + "1f53", + "2006", + "201e", + "2080", + "2116", + "2207", + "2237", + "2257", + "22b8", + "22d9", + "2314", + "2340", + "2420", + "24e3", + "25e3", + "2717", + "271d", + "2836", + "2916", + "297f", + "29a9", + "29e4", + "2a45", + "2a47", + "2a49", + "2a70", + "2ae5", + "413c", + "8087", + "e040" +]; + +const rules = vendorIds + .map(id => `SUBSYSTEM=="usb", ATTR{idVendor}=="${id}", MODE="0666"`) + .join("\n"); + +const udevCommand = [ + `echo '${rules}' > /etc/udev/rules.d/10-ubports.rules`, + "(udevadm control --reload-rules || true)", + "(udevadm trigger || true)", + "(service udev restart || true)" +].join(" && "); + /** * manage udev rules */ @@ -31,15 +126,10 @@ class Udev { */ set() { sudo.exec( - "cp " + - path.join(__dirname, "../build/10-ubports.rules") + - " /etc/udev/rules.d/ && " + - '(udevadm control --reload-rules || echo "") && ' + - '(udevadm trigger || echo "") && ' + - '(service udev restart || echo "")', + udevCommand, { name: "UBports Installer", - icns: path.join(__dirname, "../build/icons/icon.icns") + icns: path.join(__dirname, "../../build/icons/icon.icns") }, error => { if (error) log.warn(`setting udev rules failed: ${error}`); diff --git a/src/lib/udev.spec.js b/src/lib/udev.spec.js index a49bebfb..042f7600 100644 --- a/src/lib/udev.spec.js +++ b/src/lib/udev.spec.js @@ -2,6 +2,29 @@ process.argv = [null, null, "-vv"]; const { ipcMain } = require("electron"); jest.mock("electron"); +const sudo = require("sudo-prompt"); +const udev = require("./udev.js"); + it("should be a singleton", () => { - expect(require("./udev.js")).toBe(require("./udev.js")); + expect(udev).toBe(require("./udev.js")); +}); + +it("should set rules", () => { + sudo.exec.mockImplementation((command, options, cb) => cb()); + expect(udev.set()).toBe(undefined); + expect(sudo.exec).toHaveBeenCalledWith( + expect.any(String), + expect.any(Object), + expect.any(Function) + ); +}); + +it("should fail silently", () => { + sudo.exec.mockImplementation((command, options, cb) => cb("problem")); + expect(udev.set()).toBe(undefined); + expect(sudo.exec).toHaveBeenCalledWith( + expect.any(String), + expect.any(Object), + expect.any(Function) + ); });